Skip to content

[libc] Implement 'qsort' and 'bsearch' on the GPU #66230

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 13, 2023

Conversation

jhuber6
Copy link
Contributor

@jhuber6 jhuber6 commented Sep 13, 2023

Summary:
This patch simply adds the necessary config to enable qsort and bsearch
on the GPU. It is highly unlikely that anyone will use these, as they
are single threaded, but we may as well support all entrypoints that we
can.

Summary:
This patch simply adds the necessary config to enable qsort and bsearch
on the GPU. It is *highly* unlikely that anyone will use these, as they
are single threaded, but we may as well support all entrypoints that we
can.
@jhuber6 jhuber6 requested review from a team as code owners September 13, 2023 16:29
@llvmbot llvmbot added clang Clang issues not falling into any other category backend:X86 clang:headers Headers provided by Clang, e.g. for intrinsics libc labels Sep 13, 2023
@llvmbot
Copy link
Member

llvmbot commented Sep 13, 2023

@llvm/pr-subscribers-backend-x86

Changes Summary: This patch simply adds the necessary config to enable qsort and bsearch on the GPU. It is *highly* unlikely that anyone will use these, as they are single threaded, but we may as well support all entrypoints that we can.

--
Full diff: https://github.com/llvm/llvm-project/pull/66230.diff

4 Files Affected:

  • (modified) clang/lib/Headers/llvm_libc_wrappers/stdlib.h (+4-1)
  • (modified) libc/config/gpu/api.td (+3)
  • (modified) libc/config/gpu/entrypoints.txt (+3)
  • (modified) libc/docs/gpu/support.rst (+3)
diff --git a/clang/lib/Headers/llvm_libc_wrappers/stdlib.h b/clang/lib/Headers/llvm_libc_wrappers/stdlib.h
index 9cb2b4e64aa6824..7fce5a1a31d526b 100644
--- a/clang/lib/Headers/llvm_libc_wrappers/stdlib.h
+++ b/clang/lib/Headers/llvm_libc_wrappers/stdlib.h
@@ -23,8 +23,11 @@
 
 #pragma omp begin declare target
 
-// The LLVM C library uses this type so we forward declare it.
+// The LLVM C library uses these named types so we forward declare them.
 typedef void (*__atexithandler_t)(void);
+typedef int (*__bsearchcompare_t)(const void *, const void *);
+typedef int (*__qsortcompare_t)(const void *, const void *);
+typedef int (*__qsortrcompare_t)(const void *, const void *, void *);
 
 // Enforce ABI compatibility with the structs used by the LLVM C library.
 _Static_assert(__builtin_offsetof(div_t, quot) == 0, "ABI mismatch!");
diff --git a/libc/config/gpu/api.td b/libc/config/gpu/api.td
index f8db0742ac82926..dbd212be56a3f10 100644
--- a/libc/config/gpu/api.td
+++ b/libc/config/gpu/api.td
@@ -46,6 +46,9 @@ def StdlibAPI : PublicAPI<"stdlib.h"> {
     "ldiv_t",
     "lldiv_t",
     "size_t",
+    "__bsearchcompare_t",
+    "__qsortcompare_t",
+    "__qsortrcompare_t",
     "__atexithandler_t",
   ];
 }
diff --git a/libc/config/gpu/entrypoints.txt b/libc/config/gpu/entrypoints.txt
index 0e314c60870c6ae..b8d09c9bb8a8bde 100644
--- a/libc/config/gpu/entrypoints.txt
+++ b/libc/config/gpu/entrypoints.txt
@@ -58,9 +58,12 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.stdlib.atoll
     libc.src.stdlib.div
     libc.src.stdlib.labs
+    libc.src.stdlib.bsearch
     libc.src.stdlib.ldiv
     libc.src.stdlib.llabs
     libc.src.stdlib.lldiv
+    libc.src.stdlib.qsort
+    libc.src.stdlib.qsort_r
     libc.src.stdlib.strtod
     libc.src.stdlib.strtof
     libc.src.stdlib.strtol
diff --git a/libc/docs/gpu/support.rst b/libc/docs/gpu/support.rst
index fabdfb968a741ad..6d73addb28d0579 100644
--- a/libc/docs/gpu/support.rst
+++ b/libc/docs/gpu/support.rst
@@ -96,6 +96,9 @@ llabs          |check|
 div            |check|
 ldiv           |check|
 lldiv          |check|
+bsearch        |check|
+qsort          |check|
+qsort_r        |check|
 strtod         |check|
 strtof         |check|
 strtol         |check|

@jhuber6 jhuber6 requested review from sivachandra, jdoerfert and JonChesterfield and removed request for a team September 13, 2023 16:31
Copy link
Collaborator

@sivachandra sivachandra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK for libc changes.

@jhuber6 jhuber6 merged commit bf85f27 into llvm:main Sep 13, 2023
ZijunZhaoCCK pushed a commit to ZijunZhaoCCK/llvm-project that referenced this pull request Sep 19, 2023
Summary:
This patch simply adds the necessary config to enable qsort and bsearch
on the GPU. It is *highly* unlikely that anyone will use these, as they
are single threaded, but we may as well support all entrypoints that we
can.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend:X86 clang:headers Headers provided by Clang, e.g. for intrinsics clang Clang issues not falling into any other category libc
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants