[llvm] Fix crash in `CodeGenPrepare.cpp` and `Verifier.cpp` (PR #112772)

via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 18 00:45:34 PDT 2024


https://github.com/abhishek-kaushik22 updated https://github.com/llvm/llvm-project/pull/112772

>From f8018bf3a86a14e1495d76f7315a2a9fd410b5b0 Mon Sep 17 00:00:00 2001
From: abhishek-kaushik22 <abhishek.kaushik at intel.com>
Date: Fri, 18 Oct 2024 01:58:33 +0530
Subject: [PATCH 1/2] Fix crash in `CodeGenPrepare.cpp` and `Verifier.cpp`

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.
---
 llvm/lib/Transforms/Scalar/TLSVariableHoist.cpp | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

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;
 }

>From 2ed47e70f8535b534202eb36d85c434fbdd947c0 Mon Sep 17 00:00:00 2001
From: abhishek-kaushik22 <abhishek.kaushik at intel.com>
Date: Fri, 18 Oct 2024 13:15:26 +0530
Subject: [PATCH 2/2] Remove extra bitcast instruction in case of no use

---
 llvm/lib/Transforms/Scalar/TLSVariableHoist.cpp | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/llvm/lib/Transforms/Scalar/TLSVariableHoist.cpp b/llvm/lib/Transforms/Scalar/TLSVariableHoist.cpp
index 93cd736945aace..cab22f2b08de9c 100644
--- a/llvm/lib/Transforms/Scalar/TLSVariableHoist.cpp
+++ b/llvm/lib/Transforms/Scalar/TLSVariableHoist.cpp
@@ -236,6 +236,7 @@ bool TLSVariableHoistPass::tryReplaceTLSCandidate(Function &Fn,
 
   // Generate a bitcast (no type change)
   auto *CastInst = genBitCastInst(Fn, GV);
+  bool InstUsed = false;
 
   // to replace the uses of TLS Candidate
   for (auto &User : Cand.Users) {
@@ -243,9 +244,13 @@ bool TLSVariableHoistPass::tryReplaceTLSCandidate(Function &Fn,
       if (II->getIntrinsicID() == Intrinsic::threadlocal_address)
         continue;
     }
+    InstUsed = true;
     User.Inst->setOperand(User.OpndIdx, CastInst);
   }
 
+  if (!InstUsed)
+    CastInst->eraseFromParent();
+
   return true;
 }
 



More information about the llvm-commits mailing list