[clang] [Clang] Access tls_guard via llvm.threadlocal.address (PR #96633)

via cfe-commits cfe-commits at lists.llvm.org
Thu Jun 27 00:55:46 PDT 2024


================
@@ -1070,13 +1076,26 @@ CodeGenFunction::GenerateCXXGlobalInitFunc(llvm::Function *Fn,
       // Mark as initialized before initializing anything else. If the
       // initializers use previously-initialized thread_local vars, that's
       // probably supposed to be OK, but the standard doesn't say.
-      Builder.CreateStore(llvm::ConstantInt::get(GuardVal->getType(),1), Guard);
-
-      // The guard variable can't ever change again.
-      EmitInvariantStart(
-          Guard.getPointer(),
-          CharUnits::fromQuantity(
-              CGM.getDataLayout().getTypeAllocSize(GuardVal->getType())));
+      if (auto *GV = dyn_cast<llvm::GlobalValue>(Guard.getPointer()))
+        // Get the thread-local address via intrinsic.
+        if (GV->isThreadLocal())
+          GuardAddr = GuardAddr.withPointer(
+              Builder.CreateThreadLocalAddress(GV), NotKnownNonNull);
+      Builder.CreateStore(llvm::ConstantInt::get(GuardVal->getType(), 1),
+                          GuardAddr);
+
+      // Emit invariant start for TLS guard address.
+      if (CGM.getCodeGenOpts().OptimizationLevel > 0) {
+        uint64_t Width =
+            CGM.getDataLayout().getTypeAllocSize(GuardVal->getType());
+        llvm::Value *TLSAddr = Guard.getPointer();
+        if (auto *GV = dyn_cast<llvm::GlobalValue>(Guard.getPointer()))
+          // Get the thread-local address via intrinsic.
+          if (GV->isThreadLocal())
+            TLSAddr = Builder.CreateThreadLocalAddress(GV);
+        Builder.CreateInvariantStart(
----------------
nikola-tesic-ns wrote:

Ok, well `EmitInvariantStart` expects Constant value, which `TLSAddr` cannot be if we are going to set it conditionally. (But maybe it should be unconditionally)

https://github.com/llvm/llvm-project/pull/96633


More information about the cfe-commits mailing list