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

via cfe-commits cfe-commits at lists.llvm.org
Thu Jul 4 07:54:49 PDT 2024


================
@@ -1070,13 +1084,20 @@ 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.
+      // Get the thread-local address via intrinsic.
+      if (IsTLS)
+        GuardAddr = GuardAddr.withPointer(
+            Builder.CreateThreadLocalAddress(Guard.getPointer()),
+            NotKnownNonNull);
+      Builder.CreateStore(llvm::ConstantInt::get(GuardVal->getType(), 1),
+                          GuardAddr);
+
+      // Emit invariant start for TLS guard address.
       EmitInvariantStart(
           Guard.getPointer(),
           CharUnits::fromQuantity(
-              CGM.getDataLayout().getTypeAllocSize(GuardVal->getType())));
+              CGM.getDataLayout().getTypeAllocSize(GuardVal->getType())),
+          IsTLS);
----------------
nikola-tesic-ns wrote:

Am I allowed to reuse TLS address for invariant start intrinsic (example 1), or I need to access via intrinsic each time (example 2)? If the latter is true, I would need to recalculate the `GuardAddr` again.
example 1:
```
   %tls_addr1 = call align 1 ptr @llvm.threadlocal.address.p0(ptr align 1 @__tls_guard)
   store i8 1, ptr %tls_addr1, align 1
   %3 = call ptr @llvm.invariant.start.p0(i64 1, ptr %tls_addr1)
```
example 2:
```
  %tls_addr1 = call align 1 ptr @llvm.threadlocal.address.p0(ptr align 1 @__tls_guard)
  store i8 1, ptr %tls_addr1, align 1
  %tls_addr2 = call align 1 ptr @llvm.threadlocal.address.p0(ptr align 1 @__tls_guard)
  %4 = call ptr @llvm.invariant.start.p0(i64 1, ptr %tls_addr2)
```

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


More information about the cfe-commits mailing list