[llvm] r283854 - Make RandomNumberGenerator compatible with <random>

Mehdi Amini via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 11 00:13:01 PDT 2016


Author: mehdi_amini
Date: Tue Oct 11 02:13:01 2016
New Revision: 283854

URL: http://llvm.org/viewvc/llvm-project?rev=283854&view=rev
Log:
Make RandomNumberGenerator compatible with <random>

LLVM's RandomNumberGenerator wasn't compatible with
the random distribution from <random>.

Fixes PR25105

Patch by: Serge Guelton <serge.guelton at telecom-bretagne.eu>

Differential Revision: https://reviews.llvm.org/D25443

Modified:
    llvm/trunk/include/llvm/Support/RandomNumberGenerator.h
    llvm/trunk/lib/Support/RandomNumberGenerator.cpp
    llvm/trunk/unittests/IR/ModuleTest.cpp

Modified: llvm/trunk/include/llvm/Support/RandomNumberGenerator.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/RandomNumberGenerator.h?rev=283854&r1=283853&r2=283854&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/RandomNumberGenerator.h (original)
+++ llvm/trunk/include/llvm/Support/RandomNumberGenerator.h Tue Oct 11 02:13:01 2016
@@ -31,9 +31,20 @@ class StringRef;
 /// Module::createRNG to create a new RNG instance for use with that
 /// module.
 class RandomNumberGenerator {
+
+  // 64-bit Mersenne Twister by Matsumoto and Nishimura, 2000
+  // http://en.cppreference.com/w/cpp/numeric/random/mersenne_twister_engine
+  // This RNG is deterministically portable across C++11
+  // implementations.
+  using generator_type = std::mt19937_64;
+
 public:
+  using result_type = generator_type::result_type;
+
   /// Returns a random number in the range [0, Max).
-  uint_fast64_t operator()();
+  result_type operator()();
+  static constexpr result_type min() { return generator_type::min(); }
+  static constexpr result_type max() { return generator_type::max(); }
 
 private:
   /// Seeds and salts the underlying RNG engine.
@@ -42,11 +53,7 @@ private:
   /// Module::createRNG to create a new RNG salted with the Module ID.
   RandomNumberGenerator(StringRef Salt);
 
-  // 64-bit Mersenne Twister by Matsumoto and Nishimura, 2000
-  // http://en.cppreference.com/w/cpp/numeric/random/mersenne_twister_engine
-  // This RNG is deterministically portable across C++11
-  // implementations.
-  std::mt19937_64 Generator;
+  generator_type Generator;
 
   // Noncopyable.
   RandomNumberGenerator(const RandomNumberGenerator &other) = delete;

Modified: llvm/trunk/lib/Support/RandomNumberGenerator.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/RandomNumberGenerator.cpp?rev=283854&r1=283853&r2=283854&view=diff
==============================================================================
--- llvm/trunk/lib/Support/RandomNumberGenerator.cpp (original)
+++ llvm/trunk/lib/Support/RandomNumberGenerator.cpp Tue Oct 11 02:13:01 2016
@@ -57,7 +57,7 @@ RandomNumberGenerator::RandomNumberGener
   Generator.seed(SeedSeq);
 }
 
-uint_fast64_t RandomNumberGenerator::operator()() {
+RandomNumberGenerator::result_type RandomNumberGenerator::operator()() {
   return Generator();
 }
 

Modified: llvm/trunk/unittests/IR/ModuleTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/IR/ModuleTest.cpp?rev=283854&r1=283853&r2=283854&view=diff
==============================================================================
--- llvm/trunk/unittests/IR/ModuleTest.cpp (original)
+++ llvm/trunk/unittests/IR/ModuleTest.cpp Tue Oct 11 02:13:01 2016
@@ -9,8 +9,11 @@
 
 #include "llvm/IR/GlobalVariable.h"
 #include "llvm/IR/Module.h"
+#include "llvm/Support/RandomNumberGenerator.h"
 #include "gtest/gtest.h"
 
+#include <random>
+
 using namespace llvm;
 
 namespace {
@@ -45,4 +48,28 @@ TEST(ModuleTest, sortGlobalsByName) {
   }
 }
 
+TEST(ModuleTest, randomNumberGenerator) {
+  LLVMContext Context;
+  static char ID;
+  struct DummyPass : ModulePass {
+    DummyPass() : ModulePass(ID) {}
+    bool runOnModule(Module &) { return true; }
+  } DP;
+
+  Module M("R", Context);
+
+  std::uniform_int_distribution<int> dist;
+  constexpr std::size_t NBCheck = 10;
+
+  std::array<int, NBCheck> RandomStreams[2];
+  for (auto &RandomStream : RandomStreams) {
+    std::unique_ptr<RandomNumberGenerator> RNG{M.createRNG(&DP)};
+    std::generate(RandomStream.begin(), RandomStream.end(),
+                  [&]() { return dist(*RNG); });
+  }
+
+  EXPECT_TRUE(std::equal(RandomStreams[0].begin(), RandomStreams[0].end(),
+                         RandomStreams[1].begin()));
+}
+
 } // end namespace




More information about the llvm-commits mailing list