[llvm] 9276fed - [llvm-stress] Remove global LLVMContext

Arthur Eubanks via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 21 13:55:50 PDT 2022


Author: Arthur Eubanks
Date: 2022-03-21T13:55:08-07:00
New Revision: 9276fed284f6a8fdbb3f4f56eee03b256e8172e1

URL: https://github.com/llvm/llvm-project/commit/9276fed284f6a8fdbb3f4f56eee03b256e8172e1
DIFF: https://github.com/llvm/llvm-project/commit/9276fed284f6a8fdbb3f4f56eee03b256e8172e1.diff

LOG: [llvm-stress] Remove global LLVMContext

Can cause initialization order fiasco issues.

Added: 
    

Modified: 
    llvm/tools/llvm-stress/llvm-stress.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/tools/llvm-stress/llvm-stress.cpp b/llvm/tools/llvm-stress/llvm-stress.cpp
index 46eb8c7026637..e15d1d6048c77 100644
--- a/llvm/tools/llvm-stress/llvm-stress.cpp
+++ b/llvm/tools/llvm-stress/llvm-stress.cpp
@@ -69,41 +69,10 @@ static cl::opt<std::string> OutputFilename("o",
                                            cl::value_desc("filename"),
                                            cl::cat(StressCategory));
 
-static LLVMContext Context;
-
-namespace cl {
-
-template <> class parser<Type*> final : public basic_parser<Type*> {
-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<Type*> AdditionalScalarTypes("types", cl::CommaSeparated,
-  cl::desc("Additional IR scalar types "
-           "(always includes i1, i8, i16, i32, i64, float and double)"));
+static cl::list<StringRef> 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 @@ struct Modifier {
 
   /// Pick a random scalar type.
   Type *pickScalarType() {
-    static std::vector<Type*> 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 @@ struct Modifier {
 
   /// Context
   LLVMContext &Context;
+
+  std::vector<Type *> 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<Module>("/tmp/autogen.bc", Context);
   Function *F = GenEmptyFunction(M.get());
 


        


More information about the llvm-commits mailing list