Skip to content

Conversation

vonosmas
Copy link
Contributor

These functions are unlikely to be used in the overlay mode (since they are stateful), but internal llvm-libc implementation is used in tests for FMA family of functions, so we need Bazel support for them to ensure proper test coverage.

These functions are unlikely to be used in the overlay mode (since they
are stateful), but internal llvm-libc implementation is used in tests
for FMA family of functions, so we need Bazel support for them to ensure
proper test coverage.
@llvmbot llvmbot added libc bazel "Peripheral" support tier build system: utils/bazel labels Sep 18, 2025
@llvmbot
Copy link
Member

llvmbot commented Sep 18, 2025

@llvm/pr-subscribers-libc

Author: Alexey Samsonov (vonosmas)

Changes

These functions are unlikely to be used in the overlay mode (since they are stateful), but internal llvm-libc implementation is used in tests for FMA family of functions, so we need Bazel support for them to ensure proper test coverage.


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

2 Files Affected:

  • (modified) utils/bazel/llvm-project-overlay/libc/BUILD.bazel (+43)
  • (modified) utils/bazel/llvm-project-overlay/libc/test/src/stdlib/BUILD.bazel (+9)
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index cee92f2cc56c4..67c292f08be73 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -278,6 +278,14 @@ libc_support_library(
     hdrs = ["hdr/stdio_overlay.h"],
 )
 
+libc_support_library(
+    name = "hdr_stdlib_macros",
+    hdrs = ["hdr/stdlib_macros.h"],
+    deps = [
+        ":hdr_stdlib_overlay",
+    ],
+)
+
 libc_support_library(
     name = "hdr_stdlib_overlay",
     hdrs = ["hdr/stdlib_overlay.h"],
@@ -5066,6 +5074,41 @@ libc_function(
     ],
 )
 
+libc_support_library(
+    name = "rand_util",
+    srcs = ["src/stdlib/rand_util.cpp"],
+    hdrs = ["src/stdlib/rand_util.h"],
+    deps = [
+        ":__support_cpp_atomic",
+        ":__support_macros_attributes",
+        ":__support_macros_config",
+    ],
+)
+
+libc_function(
+    name = "rand",
+    srcs = ["src/stdlib/rand.cpp"],
+    hdrs = ["src/stdlib/rand.h"],
+    deps = [
+        ":__support_common",
+        ":__support_macros_config",
+        ":__support_threads_sleep",
+        ":hdr_stdlib_macros",
+        ":rand_util",
+    ],
+)
+
+libc_function(
+    name = "srand",
+    srcs = ["src/stdlib/srand.cpp"],
+    hdrs = ["src/stdlib/srand.h"],
+    deps = [
+        ":__support_common",
+        ":__support_macros_config",
+        ":rand_util",
+    ],
+)
+
 libc_support_library(
     name = "str_from_util",
     hdrs = ["src/stdlib/str_from_util.h"],
diff --git a/utils/bazel/llvm-project-overlay/libc/test/src/stdlib/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/test/src/stdlib/BUILD.bazel
index ce92ca8caa6f4..fd4389e8c0579 100644
--- a/utils/bazel/llvm-project-overlay/libc/test/src/stdlib/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/test/src/stdlib/BUILD.bazel
@@ -159,6 +159,15 @@ libc_test(
     ],
 )
 
+libc_test(
+    name = "rand_test",
+    srcs = ["rand_test.cpp"],
+    deps = [
+        "//libc:rand",
+        "//libc:srand",
+    ],
+)
+
 libc_test_library(
     name = "strfrom_test_helper",
     hdrs = ["StrfromTest.h"],

@jhuber6
Copy link
Contributor

jhuber6 commented Sep 18, 2025

If this is only for internal it would probably be better to make an internal randomness function that takes the state as an argument instead of it being a global. I already do that in a few places.

@vonosmas
Copy link
Contributor Author

If this is only for internal it would probably be better to make an internal randomness function that takes the state as an argument instead of it being a global. I already do that in a few places.

Good point. Yes, the file in question is libc/test/src/math/FmaTest.h, and I agree it's probably worth migrating it to internal RNG rather than use LIBC_NAMESPACE::(s)rand directly (you even had to fix it on GPU in 71ffc1f). I'll check w/ @lntue , but this can be done independently or rand/srand Bazel support.

@lntue
Copy link
Contributor

lntue commented Sep 18, 2025

If this is only for internal it would probably be better to make an internal randomness function that takes the state as an argument instead of it being a global. I already do that in a few places.

Good point. Yes, the file in question is libc/test/src/math/FmaTest.h, and I agree it's probably worth migrating it to internal RNG rather than use LIBC_NAMESPACE::(s)rand directly (you even had to fix it on GPU in 71ffc1f). I'll check w/ @lntue , but this can be done independently or rand/srand Bazel support.

Yes, we should definitely remove the rand from the tests' dependency.

@vonosmas
Copy link
Contributor Author

If this is only for internal it would probably be better to make an internal randomness function that takes the state as an argument instead of it being a global. I already do that in a few places.

Good point. Yes, the file in question is libc/test/src/math/FmaTest.h, and I agree it's probably worth migrating it to internal RNG rather than use LIBC_NAMESPACE::(s)rand directly (you even had to fix it on GPU in 71ffc1f). I'll check w/ @lntue , but this can be done independently or rand/srand Bazel support.

Yes, we should definitely remove the rand from the tests' dependency.

Do you think we should just move the current rand() implementation to somewhere like libc/src/__support/random.h , so that we can use it both in rand() implementation and in unit tests that need randomness?

@vonosmas vonosmas merged commit 5a402ac into llvm:main Sep 18, 2025
13 checks passed
@vonosmas vonosmas deleted the libc-bazel-rand branch September 18, 2025 21:02
@jhuber6
Copy link
Contributor

jhuber6 commented Sep 18, 2025

Do you think we should just move the current rand() implementation to somewhere like libc/src/__support/random.h , so that we can use it both in rand() implementation and in unit tests that need randomness?

That's what I would assume, we already have a bunch of these floating around so we could consolidate. I have used both 32-bit and 64-bit versions of xorshift* for most of my PRNG needs. You could extract those to a common header and replace the uses there. Off the top of my head, the GPU benchmarks, allocator, and allocator test. Potentially you could replace the implementation of rand and just make it pass in the global state as a reference.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bazel "Peripheral" support tier build system: utils/bazel libc
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants