llvm.org GIT mirror llvm / 544f63e
Random Number Generator Refactoring (removing from Module) This patch removes the RNG from Module. Passes should instead create a new RNG for their use as needed. Patch by Stephen Crane @rinon. Differential revision: http://reviews.llvm.org/D4377 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@224444 91177308-0d34-0410-b5e6-96231b3b80d8 JF Bastien 4 years ago
4 changed file(s) with 63 addition(s) and 54 deletion(s). Raw diff Collapse all Expand all
218218 std::string TargetTriple; ///< Platform target triple Module compiled on
219219 ///< Format: (arch)(sub)-(vendor)-(sys0-(abi)
220220 void *NamedMDSymTab; ///< NamedMDNode names.
221 // Allow lazy initialization in const method.
222 mutable RandomNumberGenerator *RNG; ///< The random number generator for this module.
223221
224222 // We need to keep the string because the C API expects us to own the string
225223 // representation.
268266 /// @returns a string containing the module-scope inline assembly blocks.
269267 const std::string &getModuleInlineAsm() const { return GlobalScopeAsm; }
270268
271 /// Get the RandomNumberGenerator for this module. The RNG can be
272 /// seeded via -rng-seed= and is salted with the ModuleID.
273 /// The returned RNG should not be shared across threads.
274 RandomNumberGenerator &getRNG() const;
269 /// Get a RandomNumberGenerator salted for use with this module. The
270 /// RNG can be seeded via -rng-seed= and is salted with the
271 /// ModuleID and the provided pass salt. The returned RNG should not
272 /// be shared across threads or passes.
273 ///
274 /// A unique RNG per pass ensures a reproducible random stream even
275 /// when other randomness consuming passes are added or removed. In
276 /// addition, the random stream will be reproducible across LLVM
277 /// versions when the pass does not change.
278 RandomNumberGenerator *createRNG(const Pass* P) const;
275279
276280 /// @}
277281 /// @name Module Level Mutators
66 //
77 //===----------------------------------------------------------------------===//
88 //
9 // This file defines an abstraction for random number generation (RNG).
10 // Note that the current implementation is not cryptographically secure
11 // as it uses the C++11 facilities.
9 // This file defines an abstraction for deterministic random number
10 // generation (RNG). Note that the current implementation is not
11 // cryptographically secure as it uses the C++11 facilities.
1212 //
1313 //===----------------------------------------------------------------------===//
1414
2323 namespace llvm {
2424
2525 /// A random number generator.
26 /// Instances of this class should not be shared across threads.
26 ///
27 /// Instances of this class should not be shared across threads. The
28 /// seed should be set by passing the -rng-seed= option. Use
29 /// Module::createRNG to create a new RNG instance for use with that
30 /// module.
2731 class RandomNumberGenerator {
2832 public:
29 /// Seeds and salts the underlying RNG engine. The salt of type StringRef
30 /// is passed into the constructor. The seed can be set on the command
31 /// line via -rng-seed=.
32 /// The reason for the salt is to ensure different random streams even if
33 /// the same seed is used for multiple invocations of the compiler.
34 /// A good salt value should add additional entropy and be constant across
35 /// different machines (i.e., no paths) to allow for reproducible builds.
36 /// An instance of this class can be retrieved from the current Module.
37 /// \see Module::getRNG
33 /// Returns a random number in the range [0, Max).
34 uint_fast64_t operator()();
35
36 private:
37 /// Seeds and salts the underlying RNG engine.
38 ///
39 /// This constructor should not be used directly. Instead use
40 /// Module::createRNG to create a new RNG salted with the Module ID.
3841 RandomNumberGenerator(StringRef Salt);
3942
40 /// Returns a random number in the range [0, Max).
41 uint64_t next(uint64_t Max);
42
43 private:
4443 // 64-bit Mersenne Twister by Matsumoto and Nishimura, 2000
4544 // http://en.cppreference.com/w/cpp/numeric/random/mersenne_twister_engine
45 // This RNG is deterministically portable across C++11
46 // implementations.
4647 std::mt19937_64 Generator;
4748
4849 // Noncopyable.
5051 LLVM_DELETED_FUNCTION;
5152 RandomNumberGenerator &
5253 operator=(const RandomNumberGenerator &other) LLVM_DELETED_FUNCTION;
54
55 friend class Module;
5356 };
5457 }
5558
4646 //
4747
4848 Module::Module(StringRef MID, LLVMContext &C)
49 : Context(C), Materializer(), ModuleID(MID), RNG(nullptr), DL("") {
49 : Context(C), Materializer(), ModuleID(MID), DL("") {
5050 ValSymTab = new ValueSymbolTable();
5151 NamedMDSymTab = new StringMap();
5252 Context.addModule(this);
6161 NamedMDList.clear();
6262 delete ValSymTab;
6363 delete static_cast *>(NamedMDSymTab);
64 delete RNG;
65 }
64 }
65
66 RandomNumberGenerator *Module::createRNG(const Pass* P) const {
67 SmallString<32> Salt(P->getPassName());
68
69 // This RNG is guaranteed to produce the same random stream only
70 // when the Module ID and thus the input filename is the same. This
71 // might be problematic if the input filename extension changes
72 // (e.g. from .c to .bc or .ll).
73 //
74 // We could store this salt in NamedMetadata, but this would make
75 // the parameter non-const. This would unfortunately make this
76 // interface unusable by any Machine passes, since they only have a
77 // const reference to their IR Module. Alternatively we can always
78 // store salt metadata from the Module constructor.
79 Salt += sys::path::filename(getModuleIdentifier());
80
81 return new RandomNumberGenerator(Salt);
82 }
83
6684
6785 /// getNamedValue - Return the first global value in the module with
6886 /// the specified name, of arbitrary type. This method returns null
373391 return &DL;
374392 }
375393
376 // We want reproducible builds, but ModuleID may be a full path so we just use
377 // the filename to salt the RNG (although it is not guaranteed to be unique).
378 RandomNumberGenerator &Module::getRNG() const {
379 if (RNG == nullptr) {
380 StringRef Salt = sys::path::filename(ModuleID);
381 RNG = new RandomNumberGenerator(Salt);
382 }
383 return *RNG;
384 }
385
386394 //===----------------------------------------------------------------------===//
387395 // Methods to control the materialization of GlobalValues in the Module.
388396 //
66 //
77 //===----------------------------------------------------------------------===//
88 //
9 // This file implements random number generation (RNG).
9 // This file implements deterministic random number generation (RNG).
1010 // The current implementation is NOT cryptographically secure as it uses
1111 // the C++11 facilities.
1212 //
1313 //===----------------------------------------------------------------------===//
1414
1515 #define DEBUG_TYPE "rng"
16 #include "llvm/Support/RandomNumberGenerator.h"
1716 #include "llvm/Support/CommandLine.h"
1817 #include "llvm/Support/Debug.h"
18 #include "llvm/Support/RandomNumberGenerator.h"
1919
2020 using namespace llvm;
2121
3030 RandomNumberGenerator::RandomNumberGenerator(StringRef Salt) {
3131 DEBUG(
3232 if (Seed == 0)
33 errs() << "Warning! Using unseeded random number generator.\n"
33 dbgs() << "Warning! Using unseeded random number generator.\n"
3434 );
3535
36 // Combine seed and salt using std::seed_seq.
37 // Entropy: Seed-low, Seed-high, Salt...
36 // Combine seed and salts using std::seed_seq.
37 // Data: Seed-low, Seed-high, Salt
38 // Note: std::seed_seq can only store 32-bit values, even though we
39 // are using a 64-bit RNG. This isn't a problem since the Mersenne
40 // twister constructor copies these correctly into its initial state.
3841 std::vector Data;
39 Data.reserve(2 + Salt.size()/4 + 1);
42 Data.reserve(2 + Salt.size());
4043 Data.push_back(Seed);
4144 Data.push_back(Seed >> 32);
4245
43 uint32_t Pack = 0;
44 for (size_t I = 0; I < Salt.size(); ++I) {
45 Pack <<= 8;
46 Pack += Salt[I];
47
48 if (I%4 == 3)
49 Data.push_back(Pack);
50 }
51 Data.push_back(Pack);
46 std::copy(Salt.begin(), Salt.end(), Data.end());
5247
5348 std::seed_seq SeedSeq(Data.begin(), Data.end());
5449 Generator.seed(SeedSeq);
5550 }
5651
57 uint64_t RandomNumberGenerator::next(uint64_t Max) {
58 std::uniform_int_distribution distribution(0, Max - 1);
59 return distribution(Generator);
52 uint_fast64_t RandomNumberGenerator::operator()() {
53 return Generator();
6054 }