Fix LLVM7 partially initialized ManagedStatic

Manually applied https://reviews.llvm.org/rL357714 to fix crash/assert
on loading SwiftShader when compiled with VS 2019 16.3.29324 and later,
and using clang-cl to compile. More information about this bug:
https://bugs.llvm.org/show_bug.cgi?id=41367

Bug: chromium:1015904
Change-Id: I83ad7feb15a6ca5d5eb0fc25fb6674d4678d60a5
Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/37608
Presubmit-Ready: Antonio Maiorano <amaiorano@google.com>
Kokoro-Presubmit: kokoro <noreply+kokoro@google.com>
Reviewed-by: Alexis Hétu <sugoi@google.com>
Tested-by: Antonio Maiorano <amaiorano@google.com>
diff --git a/third_party/llvm-7.0/llvm/include/llvm/Support/Compiler.h b/third_party/llvm-7.0/llvm/include/llvm/Support/Compiler.h
index 4de815f..cd19802 100644
--- a/third_party/llvm-7.0/llvm/include/llvm/Support/Compiler.h
+++ b/third_party/llvm-7.0/llvm/include/llvm/Support/Compiler.h
@@ -242,6 +242,15 @@
 #define LLVM_FALLTHROUGH
 #endif
 
+/// LLVM_REQUIRE_CONSTANT_INITIALIZATION - Apply this to globals to ensure that
+/// they are constant initialized.
+#if __has_cpp_attribute(clang::require_constant_initialization)
+#define LLVM_REQUIRE_CONSTANT_INITIALIZATION                                   \
+  [[clang::require_constant_initialization]]
+#else
+#define LLVM_REQUIRE_CONSTANT_INITIALIZATION
+#endif
+
 /// LLVM_EXTENSION - Support compilers where we have a keyword to suppress
 /// pedantic diagnostics.
 #ifdef __GNUC__
diff --git a/third_party/llvm-7.0/llvm/include/llvm/Support/ManagedStatic.h b/third_party/llvm-7.0/llvm/include/llvm/Support/ManagedStatic.h
index b4bf321..8a2eb94 100644
--- a/third_party/llvm-7.0/llvm/include/llvm/Support/ManagedStatic.h
+++ b/third_party/llvm-7.0/llvm/include/llvm/Support/ManagedStatic.h
@@ -33,18 +33,37 @@
   static void call(void *Ptr) { delete[](T *)Ptr; }
 };
 
+// If the current compiler is MSVC 2017 or earlier, then we have to work around
+// a bug where MSVC emits code to perform dynamic initialization even if the
+// class has a constexpr constructor. Instead, fall back to the C++98 strategy
+// where there are no constructors or member initializers. We can remove this
+// when MSVC 2019 (19.20+) is our minimum supported version.
+#if !defined(__clang__) && defined(_MSC_VER) && _MSC_VER < 1920
+#define LLVM_AVOID_CONSTEXPR_CTOR
+#endif
+
 /// ManagedStaticBase - Common base class for ManagedStatic instances.
 class ManagedStaticBase {
 protected:
+#ifndef LLVM_AVOID_CONSTEXPR_CTOR
+  mutable std::atomic<void *> Ptr{};
+  mutable void (*DeleterFn)(void *) = nullptr;
+  mutable const ManagedStaticBase *Next = nullptr;
+#else
   // This should only be used as a static variable, which guarantees that this
   // will be zero initialized.
   mutable std::atomic<void *> Ptr;
-  mutable void (*DeleterFn)(void*);
+  mutable void (*DeleterFn)(void *);
   mutable const ManagedStaticBase *Next;
+#endif
 
   void RegisterManagedStatic(void *(*creator)(), void (*deleter)(void*)) const;
 
 public:
+#ifndef LLVM_AVOID_CONSTEXPR_CTOR
+  constexpr ManagedStaticBase() = default;
+#endif
+
   /// isConstructed - Return true if this object has not been created yet.
   bool isConstructed() const { return Ptr != nullptr; }
 
diff --git a/third_party/llvm-7.0/llvm/lib/Support/CommandLine.cpp b/third_party/llvm-7.0/llvm/lib/Support/CommandLine.cpp
index a1e659a..3722b0b 100644
--- a/third_party/llvm-7.0/llvm/lib/Support/CommandLine.cpp
+++ b/third_party/llvm-7.0/llvm/lib/Support/CommandLine.cpp
@@ -374,11 +374,16 @@
   GlobalParser->registerCategory(this);
 }
 
-// A special subcommand representing no subcommand
-ManagedStatic<SubCommand> llvm::cl::TopLevelSubCommand;
+// A special subcommand representing no subcommand. It is particularly important
+// that this ManagedStatic uses constant initailization and not dynamic
+// initialization because it is referenced from cl::opt constructors, which run
+// dynamically in an arbitrary order.
+LLVM_REQUIRE_CONSTANT_INITIALIZATION ManagedStatic<SubCommand>
+llvm::cl::TopLevelSubCommand;
 
 // A special subcommand that can be used to put an option into all subcommands.
-ManagedStatic<SubCommand> llvm::cl::AllSubCommands;
+LLVM_REQUIRE_CONSTANT_INITIALIZATION ManagedStatic<SubCommand>
+llvm::cl::AllSubCommands;
 
 void SubCommand::registerSubCommand() {
   GlobalParser->registerSubCommand(this);