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

via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 18 00:46:37 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/3] 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/3] 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;
 }
 

>From 48b17552d2dd2fbdc55b176bd6414b6e76dd8417 Mon Sep 17 00:00:00 2001
From: abhishek-kaushik22 <abhishek.kaushik at intel.com>
Date: Fri, 18 Oct 2024 13:16:28 +0530
Subject: [PATCH 3/3] Add lit test

---
 llvm/test/CodeGen/X86/tls-no-bitcast.ll | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)
 create mode 100644 llvm/test/CodeGen/X86/tls-no-bitcast.ll

diff --git a/llvm/test/CodeGen/X86/tls-no-bitcast.ll b/llvm/test/CodeGen/X86/tls-no-bitcast.ll
new file mode 100644
index 00000000000000..b3763570b9ca48
--- /dev/null
+++ b/llvm/test/CodeGen/X86/tls-no-bitcast.ll
@@ -0,0 +1,25 @@
+; RUN: llc -tls-load-hoist=true -stop-after=tlshoist < %s | FileCheck %s 
+
+target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+
+ at I58561 = external thread_local global ptr
+
+define i32 @I59676() {
+entry:
+; CHECK: @I59676
+; CHECK-NOT: bitcast
+; CHECK: tail call ptr @llvm.threadlocal.address.p0(ptr @I58561)
+; CHECK-NEXT; tail call ptr @llvm.threadlocal.address.p0(ptr @I58561)
+  %0 = tail call ptr @llvm.threadlocal.address.p0(ptr @I58561)
+  %1 = tail call ptr @llvm.threadlocal.address.p0(ptr @I58561)
+  ret i32 0
+}
+
+; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
+declare nonnull ptr @llvm.threadlocal.address.p0(ptr nonnull) #0
+
+; uselistorder directives
+uselistorder ptr @llvm.threadlocal.address.p0, { 1, 0 }
+
+attributes #0 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }



More information about the llvm-commits mailing list