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 03:33:20 +0000 |
commit | 42f588f39c5ce6f521e3709b8871d1fdd076292f (patch) | |
tree | 5ad9009fe09e82045e3a57cf39167e223ab00a27 /llvm/lib/Support/RandomNumberGenerator.cpp | |
parent | b4c93ece8e4f6e98a15daca10c8a3db33cf8c195 (diff) | |
download | llvm-42f588f39c5ce6f521e3709b8871d1fdd076292f.zip llvm-42f588f39c5ce6f521e3709b8871d1fdd076292f.tar.gz llvm-42f588f39c5ce6f521e3709b8871d1fdd076292f.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); |