diff options
author | Mehdi Amini <joker.eph@gmail.com> | 2021-07-15 23:52:44 +0000 |
---|---|---|
committer | Mehdi Amini <joker.eph@gmail.com> | 2021-07-16 06:54:26 +0000 |
commit | af9321739b20becf170e6bb5060b8d780e1dc8dd (patch) | |
tree | b6102c00e02d67904fab6a4638b0f0e3d1afca9b /llvm/lib/Support/RandomNumberGenerator.cpp | |
parent | 20113d66c7bfe935cf2b300fc6cc3ef996bb847d (diff) | |
download | llvm-af9321739b20becf170e6bb5060b8d780e1dc8dd.zip llvm-af9321739b20becf170e6bb5060b8d780e1dc8dd.tar.gz llvm-af9321739b20becf170e6bb5060b8d780e1dc8dd.tar.bz2 |
Use ManagedStatic and lazy initialization of cl::opt in libSupport to make it free of global initializer
We can build it with -Werror=global-constructors now. This helps
in situation where libSupport is embedded as a shared library,
potential with dlopen/dlclose scenario, and when command-line
parsing or other facilities may not be involved. Avoiding the
implicit construction of these cl::opt can avoid double-registration
issues and other kind of behavior.
Reviewed By: lattner, jpienaar
Differential Revision: https://reviews.llvm.org/D105959
Diffstat (limited to 'llvm/lib/Support/RandomNumberGenerator.cpp')
-rw-r--r-- | llvm/lib/Support/RandomNumberGenerator.cpp | 24 |
1 files changed, 17 insertions, 7 deletions
diff --git a/llvm/lib/Support/RandomNumberGenerator.cpp b/llvm/lib/Support/RandomNumberGenerator.cpp index f9c41ee..aea0132 100644 --- a/llvm/lib/Support/RandomNumberGenerator.cpp +++ b/llvm/lib/Support/RandomNumberGenerator.cpp @@ -13,6 +13,9 @@ //===----------------------------------------------------------------------===// #include "llvm/Support/RandomNumberGenerator.h" + +#include "DebugOptions.h" + #include "llvm/Support/CommandLine.h" #include "llvm/Support/Debug.h" #include "llvm/Support/raw_ostream.h" @@ -25,13 +28,20 @@ using namespace llvm; #define DEBUG_TYPE "rng" - -static cl::opt<uint64_t> Seed("rng-seed", cl::value_desc("seed"), cl::Hidden, - cl::desc("Seed for the random number generator"), - cl::init(0)); +namespace { +struct CreateSeed { + static void *call() { + return new cl::opt<uint64_t>( + "rng-seed", cl::value_desc("seed"), cl::Hidden, + cl::desc("Seed for the random number generator"), cl::init(0)); + } +}; +} // namespace +static ManagedStatic<cl::opt<uint64_t>, CreateSeed> Seed; +void llvm::initRandomSeedOptions() { *Seed; } RandomNumberGenerator::RandomNumberGenerator(StringRef Salt) { - LLVM_DEBUG(if (Seed == 0) dbgs() + LLVM_DEBUG(if (*Seed == 0) dbgs() << "Warning! Using unseeded random number generator.\n"); // Combine seed and salts using std::seed_seq. @@ -41,8 +51,8 @@ RandomNumberGenerator::RandomNumberGenerator(StringRef Salt) { // twister constructor copies these correctly into its initial state. std::vector<uint32_t> Data; Data.resize(2 + Salt.size()); - Data[0] = Seed; - Data[1] = Seed >> 32; + Data[0] = *Seed; + Data[1] = *Seed >> 32; llvm::copy(Salt, Data.begin() + 2); |