[llvm] [Offload][Conformance] Add `RandomGenerator` for large input spaces (PR #154252)

Leandro Lacerda via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 19 05:33:36 PDT 2025


================
@@ -33,73 +28,45 @@ namespace mathtest {
 
 template <typename... InTypes>
 class [[nodiscard]] ExhaustiveGenerator final
-    : public InputGenerator<InTypes...> {
-  static constexpr std::size_t NumInputs = sizeof...(InTypes);
-  static_assert(NumInputs > 0, "The number of inputs must be at least 1");
+    : public RangeBasedGenerator<ExhaustiveGenerator<InTypes...>, InTypes...> {
+
+  friend class RangeBasedGenerator<ExhaustiveGenerator<InTypes...>, InTypes...>;
+
+  using Base = RangeBasedGenerator<ExhaustiveGenerator<InTypes...>, InTypes...>;
+  using IndexArrayType = std::array<uint64_t, Base::NumInputs>;
+
+  using Base::InputSpaceSize;
+  using Base::RangesTuple;
+  using Base::SizeToGenerate;
 
 public:
   explicit constexpr ExhaustiveGenerator(
       const IndexedRange<InTypes> &...Ranges) noexcept
-      : RangesTuple(Ranges...) {
-    bool Overflowed = getSizeWithOverflow(Ranges..., Size);
-
-    assert(!Overflowed && "The input space size is too large");
-    assert((Size > 0) && "The input space size must be at least 1");
+      : Base(Ranges...) {
+    SizeToGenerate = InputSpaceSize;
 
     IndexArrayType DimSizes = {};
     std::size_t DimIndex = 0;
     ((DimSizes[DimIndex++] = Ranges.getSize()), ...);
 
-    Strides[NumInputs - 1] = 1;
-    if constexpr (NumInputs > 1)
-      for (int Index = static_cast<int>(NumInputs) - 2; Index >= 0; --Index)
+    Strides[Base::NumInputs - 1] = 1;
+    if constexpr (Base::NumInputs > 1)
+      for (int Index = static_cast<int>(Base::NumInputs) - 2; Index >= 0;
----------------
leandrolcampos wrote:

You're right that `size_t` is the idiomatic choice for indexing in general. However, for this specific reverse loop, I've intentionally used `int` to avoid a bug.

The loop termination condition is `Index >= 0`. If `Index` were an unsigned type like `size_t`, this condition would always be true. When `Index` is `0` and the loop decrements (`--Index`), it would underflow and "wrap around" to the largest possible `size_t` value, leading to an infinite loop.

Using a signed `int` ensures that when `Index` becomes `-1`, the condition `Index >= 0` correctly evaluates to `false`, and the loop terminates as expected.

https://github.com/llvm/llvm-project/pull/154252


More information about the llvm-commits mailing list