Skip to content

[TLSVariableHoist] Do not introduce bitcast on llvm.threadlocal.address argument #112772

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from

Conversation

abhishek-kaushik22
Copy link
Contributor

The method AddressingModeMatcher::matchOperationAddr and Verifier::visitIntrinsicCall expect a GlobalValue as the operand of the llvm.threadlocal.address.p0 intrinsic. The hoist TLS pass replaces them with a bitcast instruction, causing a crash.

Fixes #112771

The method `AddressingModeMatcher::matchOperationAddr` and `Verifier::visitIntrinsicCall` expect a `GlobalValue` as the operand of the `llvm.threadlocal.address.p0` intrinsic. 
The hoist TLS pass replaces them with a bitcast instruction, causing a crash.
@llvmbot
Copy link
Member

llvmbot commented Oct 17, 2024

@llvm/pr-subscribers-backend-x86

@llvm/pr-subscribers-llvm-transforms

Author: None (abhishek-kaushik22)

Changes

The method AddressingModeMatcher::matchOperationAddr and Verifier::visitIntrinsicCall expect a GlobalValue as the operand of the llvm.threadlocal.address.p0 intrinsic. The hoist TLS pass replaces them with a bitcast instruction, causing a crash.

Fixes #112771


Full diff: https://github.com/llvm/llvm-project/pull/112772.diff

1 Files Affected:

  • (modified) llvm/lib/Transforms/Scalar/TLSVariableHoist.cpp (+6-1)
diff --git a/llvm/lib/Transforms/Scalar/TLSVariableHoist.cpp b/llvm/lib/Transforms/Scalar/TLSVariableHoist.cpp
index 58ea5b68d5488b..93cd736945aace 100644
--- a/llvm/lib/Transforms/Scalar/TLSVariableHoist.cpp
+++ b/llvm/lib/Transforms/Scalar/TLSVariableHoist.cpp
@@ -238,8 +238,13 @@ bool TLSVariableHoistPass::tryReplaceTLSCandidate(Function &Fn,
   auto *CastInst = genBitCastInst(Fn, GV);
 
   // to replace the uses of TLS Candidate
-  for (auto &User : Cand.Users)
+  for (auto &User : Cand.Users) {
+    if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(User.Inst)) {
+      if (II->getIntrinsicID() == Intrinsic::threadlocal_address)
+        continue;
+    }
     User.Inst->setOperand(User.OpndIdx, CastInst);
+  }
 
   return true;
 }

@dtcxzyw dtcxzyw requested a review from nikic October 18, 2024 01:03
@MalaySanghi
Copy link
Contributor

@abhishek-kaushik22
I think we should add a lit test here

@abhishek-kaushik22
Copy link
Contributor Author

@nikic can you please review?

@nikic nikic requested review from phoebewang and xiangzh1 October 21, 2024 12:37
Copy link
Contributor

@nikic nikic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is the right approach to fix this: If llvm.threadlocal.address is used, then we need to hoist that call. Skipping it would defeat the point.

However ... I think this entire pass may just be useless how? Since we switched to using llvm.threadlocal.address, I believe that normal EarlyCSE/GVN and LICM will already perform the optimization this pass is intended to do. Maybe we can just delete this code entirely?

@nikic nikic changed the title Fix crash in CodeGenPrepare.cpp and Verifier.cpp [TLSVariableHoist] Do not introduce bitcast on llvm.threadlocal.address argument Oct 21, 2024
@abhishek-kaushik22
Copy link
Contributor Author

I've opened #114740 to delete the pass.

nikic pushed a commit that referenced this pull request Nov 7, 2024
This reverts commit c310143.

Based on the discussions in #112772, this pass is not needed after the
introduction of `llvm.threadlocal.address` intrinsic.

Fixes #112771.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

llc crashes with -tls-load-hoist=true
4 participants