llvm.org GIT mirror llvm / 02b4e6e
Revert "Random Number Generator (llvm)" This reverts commit cccba093090d127e0b6d17473b14c264c14c5259. It causes build breakage. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@211146 91177308-0d34-0410-b5e6-96231b3b80d8 JF Bastien 6 years ago
5 changed file(s) with 1 addition(s) and 128 deletion(s). Raw diff Collapse all Expand all
2828 class FunctionType;
2929 class GVMaterializer;
3030 class LLVMContext;
31 class RandomNumberGenerator;
3231 class StructType;
3332 template struct DenseMapInfo;
3433 template class DenseMap;
201200 std::string ModuleID; ///< Human readable identifier for the module
202201 std::string TargetTriple; ///< Platform target triple Module compiled on
203202 void *NamedMDSymTab; ///< NamedMDNode names.
204 // Allow lazy initialization in const method.
205 mutable RandomNumberGenerator *RNG; ///< The random number generator for this module.
206203
207204 // We need to keep the string because the C API expects us to own the string
208205 // representation.
250247 /// Get any module-scope inline assembly blocks.
251248 /// @returns a string containing the module-scope inline assembly blocks.
252249 const std::string &getModuleInlineAsm() const { return GlobalScopeAsm; }
253
254 /// Get the RandomNumberGenerator for this module. The RNG can be
255 /// seeded via -rng-seed= and is salted with the ModuleID.
256 /// The returned RNG should not be shared across threads.
257 RandomNumberGenerator &getRNG() const;
258250
259251 /// @}
260252 /// @name Module Level Mutators
+0
-53
include/llvm/Support/RandomNumberGenerator.h less more
None //==- llvm/Support/RandomNumberGenerator.h - RNG for diversity ---*- C++ -*-==//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
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.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_SUPPORT_RANDOMNUMBERGENERATOR_H_
16 #define LLVM_SUPPORT_RANDOMNUMBERGENERATOR_H_
17
18 #include "llvm/ADT/StringRef.h"
19 #include
20
21 namespace llvm {
22
23 /// A random number generator.
24 /// Instances of this class should not be shared across threads.
25 class RandomNumberGenerator {
26 public:
27 /// Seeds and salts the underlying RNG engine. The salt of type StringRef
28 /// is passed into the constructor. The seed can be set on the command
29 /// line via -rng-seed=.
30 /// The reason for the salt is to ensure different random streams even if
31 /// the same seed is used for multiple invocations of the compiler.
32 /// A good salt value should add additional entropy and be constant across
33 /// different machines (i.e., no paths) to allow for reproducible builds.
34 /// An instance of this class can be retrieved from the current Module.
35 /// \see Module::getRNG
36 RandomNumberGenerator(StringRef Salt);
37
38 /// Returns a random number in the range [0, Max).
39 uint64_t next(uint64_t Max);
40
41 private:
42 // 64-bit Mersenne Twister by Matsumoto and Nishimura, 2000
43 // http://en.cppreference.com/w/cpp/numeric/random/mersenne_twister_engine
44 std::mt19937_64 Generator;
45
46 // Noncopyable.
47 RandomNumberGenerator(const RandomNumberGenerator &other) = delete;
48 RandomNumberGenerator &operator=(const RandomNumberGenerator &other) = delete;
49 };
50 }
51
52 #endif
2323 #include "llvm/IR/LLVMContext.h"
2424 #include "llvm/IR/LeakDetector.h"
2525 #include "llvm/Support/Dwarf.h"
26 #include "llvm/Support/Path.h"
27 #include "llvm/Support/RandomNumberGenerator.h"
2826 #include
2927 #include
3028 #include
4543 //
4644
4745 Module::Module(StringRef MID, LLVMContext &C)
48 : Context(C), Materializer(), ModuleID(MID), RNG(nullptr), DL("") {
46 : Context(C), Materializer(), ModuleID(MID), DL("") {
4947 ValSymTab = new ValueSymbolTable();
5048 NamedMDSymTab = new StringMap();
5149 Context.addModule(this);
6058 NamedMDList.clear();
6159 delete ValSymTab;
6260 delete static_cast *>(NamedMDSymTab);
63 delete RNG;
6461 }
6562
6663 /// getNamedValue - Return the first global value in the module with
357354 return &DL;
358355 }
359356
360 // We want reproducible builds, but ModuleID may be a full path so we just use
361 // the filename to salt the RNG (although it is not guaranteed to be unique).
362 RandomNumberGenerator &Module::getRNG() const {
363 if (RNG == nullptr) {
364 StringRef Salt = sys::path::filename(ModuleID);
365 RNG = new RandomNumberGenerator(Salt);
366 }
367 return *RNG;
368 }
369
370357 //===----------------------------------------------------------------------===//
371358 // Methods to control the materialization of GlobalValues in the Module.
372359 //
4040 MD5.cpp
4141 PluginLoader.cpp
4242 PrettyStackTrace.cpp
43 RandomNumberGenerator.cpp
4443 Regex.cpp
4544 SmallPtrSet.cpp
4645 SmallVector.cpp
+0
-52
lib/Support/RandomNumberGenerator.cpp less more
None //===-- RandomNumberGenerator.cpp - Implement RNG class -------------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements random number generation (RNG).
10 // The current implementation is NOT cryptographically secure as it uses
11 // the C++11 facilities.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #define DEBUG_TYPE "rng"
16 #include "llvm/Support/RandomNumberGenerator.h"
17 #include "llvm/Support/CommandLine.h"
18 #include "llvm/Support/Debug.h"
19
20 using namespace llvm;
21
22 // Tracking BUG: 19665
23 // http://llvm.org/bugs/show_bug.cgi?id=19665
24 //
25 // Do not change to cl::opt since this silently breaks argument parsing.
26 static cl::opt
27 Seed("rng-seed", cl::value_desc("seed"),
28 cl::desc("Seed for the random number generator"), cl::init(0));
29
30 RandomNumberGenerator::RandomNumberGenerator(StringRef Salt) {
31 DEBUG(
32 if (Seed == 0)
33 errs() << "Warning! Using unseeded random number generator.\n"
34 );
35
36 // Combine seed and salt using std::seed_seq.
37 // Entropy: Seed-low, Seed-high, Salt...
38 size_t Size = Salt.size() + 2;
39 uint32_t Data[Size];
40 Data[0] = Seed;
41 Data[1] = Seed >> 32;
42 std::copy_n(Salt.begin(), Salt.size(), Data + 2);
43
44 std::seed_seq SeedSeq(Data, Data + Size);
45 Generator.seed(SeedSeq);
46 }
47
48 uint64_t RandomNumberGenerator::next(uint64_t Max) {
49 std::uniform_int_distribution distribution(0, Max - 1);
50 return distribution(Generator);
51 }