From 9276fed284f6a8fdbb3f4f56eee03b256e8172e1 Mon Sep 17 00:00:00 2001 From: Arthur Eubanks Date: Mon, 21 Mar 2022 13:54:47 -0700 Subject: [PATCH] [llvm-stress] Remove global LLVMContext Can cause initialization order fiasco issues. --- llvm/tools/llvm-stress/llvm-stress.cpp | 89 +++++++++++++++------------------- 1 file changed, 39 insertions(+), 50 deletions(-) diff --git a/llvm/tools/llvm-stress/llvm-stress.cpp b/llvm/tools/llvm-stress/llvm-stress.cpp index 46eb8c7..e15d1d6 100644 --- a/llvm/tools/llvm-stress/llvm-stress.cpp +++ b/llvm/tools/llvm-stress/llvm-stress.cpp @@ -69,41 +69,10 @@ static cl::opt OutputFilename("o", cl::value_desc("filename"), cl::cat(StressCategory)); -static LLVMContext Context; - -namespace cl { - -template <> class parser final : public basic_parser { -public: - parser(Option &O) : basic_parser(O) {} - - // Parse options as IR types. Return true on error. - bool parse(Option &O, StringRef, StringRef Arg, Type *&Value) { - if (Arg == "half") Value = Type::getHalfTy(Context); - else if (Arg == "fp128") Value = Type::getFP128Ty(Context); - else if (Arg == "x86_fp80") Value = Type::getX86_FP80Ty(Context); - else if (Arg == "ppc_fp128") Value = Type::getPPC_FP128Ty(Context); - else if (Arg == "x86_mmx") Value = Type::getX86_MMXTy(Context); - else if (Arg.startswith("i")) { - unsigned N = 0; - Arg.drop_front().getAsInteger(10, N); - if (N > 0) - Value = Type::getIntNTy(Context, N); - } - - if (!Value) - return O.error("Invalid IR scalar type: '" + Arg + "'!"); - return false; - } - - StringRef getValueName() const override { return "IR scalar type"; } -}; - -} // end namespace cl - -static cl::list AdditionalScalarTypes("types", cl::CommaSeparated, - cl::desc("Additional IR scalar types " - "(always includes i1, i8, i16, i32, i64, float and double)")); +static cl::list AdditionalScalarTypes( + "types", cl::CommaSeparated, + cl::desc("Additional IR scalar types " + "(always includes i1, i8, i16, i32, i64, float and double)")); namespace { @@ -185,7 +154,38 @@ struct Modifier { public: /// C'tor Modifier(BasicBlock *Block, PieceTable *PT, Random *R) - : BB(Block), PT(PT), Ran(R), Context(BB->getContext()) {} + : BB(Block), PT(PT), Ran(R), Context(BB->getContext()) { + ScalarTypes.assign({Type::getInt1Ty(Context), Type::getInt8Ty(Context), + Type::getInt16Ty(Context), Type::getInt32Ty(Context), + Type::getInt64Ty(Context), Type::getFloatTy(Context), + Type::getDoubleTy(Context)}); + + for (auto &Arg : AdditionalScalarTypes) { + Type *Ty = nullptr; + if (Arg == "half") + Ty = Type::getHalfTy(Context); + else if (Arg == "fp128") + Ty = Type::getFP128Ty(Context); + else if (Arg == "x86_fp80") + Ty = Type::getX86_FP80Ty(Context); + else if (Arg == "ppc_fp128") + Ty = Type::getPPC_FP128Ty(Context); + else if (Arg == "x86_mmx") + Ty = Type::getX86_MMXTy(Context); + else if (Arg.startswith("i")) { + unsigned N = 0; + Arg.drop_front().getAsInteger(10, N); + if (N > 0) + Ty = Type::getIntNTy(Context, N); + } + if (!Ty) { + errs() << "Invalid IR scalar type: '" << Arg << "'!\n"; + exit(1); + } + + ScalarTypes.push_back(Ty); + } + } /// virtual D'tor to silence warnings. virtual ~Modifier() = default; @@ -310,20 +310,6 @@ protected: /// Pick a random scalar type. Type *pickScalarType() { - static std::vector ScalarTypes; - if (ScalarTypes.empty()) { - ScalarTypes.assign({ - Type::getInt1Ty(Context), - Type::getInt8Ty(Context), - Type::getInt16Ty(Context), - Type::getInt32Ty(Context), - Type::getInt64Ty(Context), - Type::getFloatTy(Context), - Type::getDoubleTy(Context) - }); - llvm::append_range(ScalarTypes, AdditionalScalarTypes); - } - return ScalarTypes[getRandom() % ScalarTypes.size()]; } @@ -338,6 +324,8 @@ protected: /// Context LLVMContext &Context; + + std::vector ScalarTypes; }; struct LoadModifier: public Modifier { @@ -749,6 +737,7 @@ int main(int argc, char **argv) { cl::HideUnrelatedOptions({&StressCategory, &getColorCategory()}); cl::ParseCommandLineOptions(argc, argv, "llvm codegen stress-tester\n"); + LLVMContext Context; auto M = std::make_unique("/tmp/autogen.bc", Context); Function *F = GenEmptyFunction(M.get()); -- 2.7.4