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

via cfe-commits cfe-commits at lists.llvm.org
Wed Jul 3 04:10:06 PDT 2024


================
@@ -1059,9 +1059,15 @@ CodeGenFunction::GenerateCXXGlobalInitFunc(llvm::Function *Fn,
     if (Guard.isValid()) {
       // If we have a guard variable, check whether we've already performed
       // these initializations. This happens for TLS initialization functions.
-      llvm::Value *GuardVal = Builder.CreateLoad(Guard);
-      llvm::Value *Uninit = Builder.CreateIsNull(GuardVal,
-                                                 "guard.uninitialized");
+      Address GuardAddr = Guard;
+      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);
+      llvm::Value *GuardVal = Builder.CreateLoad(GuardAddr);
----------------
nikola-tesic-ns wrote:

I think this is related to the conversation in the other comment
> The Guard is a ConstantAddress, so I cannot change it, that's why I introduced new variable. If you have some suggestion, I would be happy to adapt the code.

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


More information about the cfe-commits mailing list