[llvm] [DirectX] Legalize `llvm.lifetime.*` intrinsics in EmbedDXILPass (PR #150100)

Deric C. via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 22 14:49:03 PDT 2025


================
@@ -52,6 +53,51 @@ class WriteDXILPass : public llvm::ModulePass {
   }
 };
 
+static void legalizeLifetimeIntrinsics(Module &M) {
+  for (Function &F : M) {
+    Intrinsic::ID IID = F.getIntrinsicID();
+    if (IID != Intrinsic::lifetime_start && IID != Intrinsic::lifetime_end)
+      continue;
+
+    // Lifetime intrinsics in LLVM 3.7 do not have the memory FnAttr
+    F.removeFnAttr(Attribute::Memory);
+
+    // Lifetime intrinsics in LLVM 3.7 do not have mangled names
+    F.setName(Intrinsic::getBaseName(IID));
+
+    // LLVM 3.7 Lifetime intrinics require an i8* operand, so we insert bitcasts
+    // to ensure that is the case
+    for (auto *User : make_early_inc_range(F.users())) {
+      CallInst *CI = dyn_cast<CallInst>(User);
+      assert(CI && "Expected user of a lifetime intrinsic function to be a "
+                   "lifetime intrinsic call");
+      Value *PtrOperand = CI->getArgOperand(1);
+      PointerType *PtrTy = cast<PointerType>(PtrOperand->getType());
+      Value *NoOpBitCast = CastInst::Create(Instruction::BitCast, PtrOperand,
+                                            PtrTy, "", CI->getIterator());
+      CI->setArgOperand(1, NoOpBitCast);
+    }
+  }
+}
+
+static void removeLifetimeIntrinsics(Module &M) {
+  for (Function &F : make_early_inc_range(M))
+    if (Intrinsic::ID IID = F.getIntrinsicID();
+        IID == Intrinsic::lifetime_start || IID == Intrinsic::lifetime_end) {
+      for (User *U : make_early_inc_range(F.users())) {
+        LifetimeIntrinsic *LI = dyn_cast<LifetimeIntrinsic>(U);
+        assert(LI && "Expected user of lifetime intrinsic function to be "
+                     "a LifetimeIntrinsic instruction");
+        BitCastInst *BCI = dyn_cast<BitCastInst>(LI->getArgOperand(1));
+        assert(BCI && "Expected pointer operand of LifetimeIntrinsic to be a "
+                      "BitCastInst");
+        LI->eraseFromParent();
+        BCI->eraseFromParent();
+      }
+      F.eraseFromParent();
----------------
Icohedron wrote:

It gets erased because the lifetime intrinsic's name is no longer mangled. Otherwise the pass will fail the module verifier with the message `Intrinsic name not mangled correctly for type arguments! Should be: llvm.lifetime.end.p0`

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


More information about the llvm-commits mailing list