Skip to content

Revert "[DirectX] Lower llvm.lifetime.* intrinsics to stores when DXIL version is lower than 1.6 (#147432)" #149874

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

Merged
merged 1 commit into from
Jul 21, 2025

Conversation

Icohedron
Copy link
Contributor

This PR reverts commit d47c126 (corresponding to PR #147432) to fix a build failure caused by #149310

…XIL version is lower than 1.6 (llvm#147432)"

This reverts commit d47c126.
@llvmbot
Copy link
Member

llvmbot commented Jul 21, 2025

@llvm/pr-subscribers-backend-directx

Author: Deric C. (Icohedron)

Changes

This PR reverts commit d47c126 (corresponding to PR #147432) to fix a build failure caused by #149310


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

3 Files Affected:

  • (modified) llvm/lib/Target/DirectX/DXILOpLowering.cpp (+14-55)
  • (removed) llvm/test/CodeGen/DirectX/legalize-lifetimes-valver-1.6.ll (-56)
  • (renamed) llvm/test/CodeGen/DirectX/legalize-lifetimes.ll (+2-6)
diff --git a/llvm/lib/Target/DirectX/DXILOpLowering.cpp b/llvm/lib/Target/DirectX/DXILOpLowering.cpp
index 0ec15a629d0a2..995120309a938 100644
--- a/llvm/lib/Target/DirectX/DXILOpLowering.cpp
+++ b/llvm/lib/Target/DirectX/DXILOpLowering.cpp
@@ -39,13 +39,11 @@ class OpLowerer {
   DXILOpBuilder OpBuilder;
   DXILResourceMap &DRM;
   DXILResourceTypeMap &DRTM;
-  const ModuleMetadataInfo &MMDI;
   SmallVector<CallInst *> CleanupCasts;
 
 public:
-  OpLowerer(Module &M, DXILResourceMap &DRM, DXILResourceTypeMap &DRTM,
-            const ModuleMetadataInfo &MMDI)
-      : M(M), OpBuilder(M), DRM(DRM), DRTM(DRTM), MMDI(MMDI) {}
+  OpLowerer(Module &M, DXILResourceMap &DRM, DXILResourceTypeMap &DRTM)
+      : M(M), OpBuilder(M), DRM(DRM), DRTM(DRTM) {}
 
   /// Replace every call to \c F using \c ReplaceCall, and then erase \c F. If
   /// there is an error replacing a call, we emit a diagnostic and return true.
@@ -318,7 +316,8 @@ class OpLowerer {
   /// model and taking into account binding information from
   /// DXILResourceAnalysis.
   bool lowerHandleFromBinding(Function &F) {
-    if (MMDI.DXILVersion < VersionTuple(1, 6))
+    const Triple &TT = M.getTargetTriple();
+    if (TT.getDXILVersion() < VersionTuple(1, 6))
       return lowerToCreateHandle(F);
     return lowerToBindAndAnnotateHandle(F);
   }
@@ -487,6 +486,8 @@ class OpLowerer {
   }
 
   [[nodiscard]] bool lowerRawBufferLoad(Function &F) {
+    const Triple &TT = M.getTargetTriple();
+    VersionTuple DXILVersion = TT.getDXILVersion();
     const DataLayout &DL = F.getDataLayout();
     IRBuilder<> &IRB = OpBuilder.getIRB();
     Type *Int8Ty = IRB.getInt8Ty();
@@ -510,7 +511,7 @@ class OpLowerer {
           ConstantInt::get(Int32Ty, DL.getPrefTypeAlign(ScalarTy).value());
 
       Expected<CallInst *> OpCall =
-          MMDI.DXILVersion >= VersionTuple(1, 2)
+          DXILVersion >= VersionTuple(1, 2)
               ? OpBuilder.tryCreateOp(OpCode::RawBufferLoad,
                                       {Handle, Index0, Index1, Mask, Align},
                                       CI->getName(), NewRetTy)
@@ -585,6 +586,8 @@ class OpLowerer {
   }
 
   [[nodiscard]] bool lowerBufferStore(Function &F, bool IsRaw) {
+    const Triple &TT = M.getTargetTriple();
+    VersionTuple DXILVersion = TT.getDXILVersion();
     const DataLayout &DL = F.getDataLayout();
     IRBuilder<> &IRB = OpBuilder.getIRB();
     Type *Int8Ty = IRB.getInt8Ty();
@@ -651,7 +654,7 @@ class OpLowerer {
       SmallVector<Value *, 9> Args{
           Handle,          Index0,          Index1,          DataElements[0],
           DataElements[1], DataElements[2], DataElements[3], Mask};
-      if (IsRaw && MMDI.DXILVersion >= VersionTuple(1, 2)) {
+      if (IsRaw && DXILVersion >= VersionTuple(1, 2)) {
         Op = OpCode::RawBufferStore;
         // RawBufferStore requires the alignment
         Args.push_back(
@@ -742,37 +745,6 @@ class OpLowerer {
     });
   }
 
-  [[nodiscard]] bool lowerLifetimeIntrinsic(Function &F) {
-    IRBuilder<> &IRB = OpBuilder.getIRB();
-    return replaceFunction(F, [&](CallInst *CI) -> Error {
-      IRB.SetInsertPoint(CI);
-      Value *Ptr = CI->getArgOperand(1);
-      assert(Ptr->getType()->isPointerTy() &&
-             "Expected operand of lifetime intrinsic to be a pointer");
-
-      auto ZeroOrUndef = [&](Type *Ty) {
-        return MMDI.ValidatorVersion < VersionTuple(1, 6)
-                   ? Constant::getNullValue(Ty)
-                   : UndefValue::get(Ty);
-      };
-
-      Value *Val = nullptr;
-      if (auto *GV = dyn_cast<GlobalVariable>(Ptr)) {
-        if (GV->hasInitializer() || GV->isExternallyInitialized())
-          return Error::success();
-        Val = ZeroOrUndef(GV->getValueType());
-      } else if (auto *AI = dyn_cast<AllocaInst>(Ptr))
-        Val = ZeroOrUndef(AI->getAllocatedType());
-
-      assert(Val && "Expected operand of lifetime intrinsic to be a global "
-                    "variable or alloca instruction");
-      IRB.CreateStore(Val, Ptr, false);
-
-      CI->eraseFromParent();
-      return Error::success();
-    });
-  }
-
   [[nodiscard]] bool lowerIsFPClass(Function &F) {
     IRBuilder<> &IRB = OpBuilder.getIRB();
     Type *RetTy = IRB.getInt1Ty();
@@ -831,6 +803,8 @@ class OpLowerer {
       case Intrinsic::dx_resource_casthandle:
       // NOTE: llvm.dbg.value is supported as is in DXIL.
       case Intrinsic::dbg_value:
+      case Intrinsic::lifetime_start:
+      case Intrinsic::lifetime_end:
       case Intrinsic::not_intrinsic:
         if (F.use_empty())
           F.eraseFromParent();
@@ -881,17 +855,6 @@ class OpLowerer {
       case Intrinsic::ctpop:
         HasErrors |= lowerCtpopToCountBits(F);
         break;
-      case Intrinsic::lifetime_start:
-      case Intrinsic::lifetime_end:
-        if (F.use_empty())
-          F.eraseFromParent();
-        else {
-          if (MMDI.DXILVersion < VersionTuple(1, 6))
-            HasErrors |= lowerLifetimeIntrinsic(F);
-          else
-            continue;
-        }
-        break;
       case Intrinsic::is_fpclass:
         HasErrors |= lowerIsFPClass(F);
         break;
@@ -909,9 +872,8 @@ class OpLowerer {
 PreservedAnalyses DXILOpLowering::run(Module &M, ModuleAnalysisManager &MAM) {
   DXILResourceMap &DRM = MAM.getResult<DXILResourceAnalysis>(M);
   DXILResourceTypeMap &DRTM = MAM.getResult<DXILResourceTypeAnalysis>(M);
-  const ModuleMetadataInfo MMDI = MAM.getResult<DXILMetadataAnalysis>(M);
 
-  const bool MadeChanges = OpLowerer(M, DRM, DRTM, MMDI).lowerIntrinsics();
+  bool MadeChanges = OpLowerer(M, DRM, DRTM).lowerIntrinsics();
   if (!MadeChanges)
     return PreservedAnalyses::all();
   PreservedAnalyses PA;
@@ -929,10 +891,8 @@ class DXILOpLoweringLegacy : public ModulePass {
         getAnalysis<DXILResourceWrapperPass>().getResourceMap();
     DXILResourceTypeMap &DRTM =
         getAnalysis<DXILResourceTypeWrapperPass>().getResourceTypeMap();
-    const ModuleMetadataInfo MMDI =
-        getAnalysis<DXILMetadataAnalysisWrapperPass>().getModuleMetadata();
 
-    return OpLowerer(M, DRM, DRTM, MMDI).lowerIntrinsics();
+    return OpLowerer(M, DRM, DRTM).lowerIntrinsics();
   }
   StringRef getPassName() const override { return "DXIL Op Lowering"; }
   DXILOpLoweringLegacy() : ModulePass(ID) {}
@@ -941,7 +901,6 @@ class DXILOpLoweringLegacy : public ModulePass {
   void getAnalysisUsage(llvm::AnalysisUsage &AU) const override {
     AU.addRequired<DXILResourceTypeWrapperPass>();
     AU.addRequired<DXILResourceWrapperPass>();
-    AU.addRequired<DXILMetadataAnalysisWrapperPass>();
     AU.addPreserved<DXILResourceWrapperPass>();
     AU.addPreserved<DXILMetadataAnalysisWrapperPass>();
     AU.addPreserved<ShaderFlagsAnalysisWrapper>();
diff --git a/llvm/test/CodeGen/DirectX/legalize-lifetimes-valver-1.6.ll b/llvm/test/CodeGen/DirectX/legalize-lifetimes-valver-1.6.ll
deleted file mode 100644
index f77df2d812dfe..0000000000000
--- a/llvm/test/CodeGen/DirectX/legalize-lifetimes-valver-1.6.ll
+++ /dev/null
@@ -1,56 +0,0 @@
-; RUN: opt -S -passes='dxil-op-lower' -mtriple=dxil-pc-shadermodel6.3-library %s | FileCheck %s --check-prefixes=CHECK,CHECK-SM63
-; RUN: opt -S -passes='dxil-op-lower' -mtriple=dxil-pc-shadermodel6.6-library %s | FileCheck %s --check-prefixes=CHECK,CHECK-SM66
-; RUN: opt -S -dxil-op-lower -dxil-prepare -mtriple=dxil-pc-shadermodel6.6-library %s | FileCheck %s --check-prefixes=CHECK,CHECK-PREPARE
-
-; CHECK-LABEL: define void @test_legal_lifetime() {
-; 
-; CHECK-SM63-NEXT:    [[ACCUM_I_FLAT:%.*]] = alloca [1 x i32], align 4
-; CHECK-SM63-NEXT:    [[GEP:%.*]] = getelementptr i32, ptr [[ACCUM_I_FLAT]], i32 0
-; CHECK-SM63-NEXT:    store [1 x i32] undef, ptr [[ACCUM_I_FLAT]], align 4
-; CHECK-SM63-NEXT:    store i32 0, ptr [[GEP]], align 4
-; CHECK-SM63-NEXT:    store [1 x i32] undef, ptr [[ACCUM_I_FLAT]], align 4
-; 
-; CHECK-SM66-NEXT:    [[ACCUM_I_FLAT:%.*]] = alloca [1 x i32], align 4
-; CHECK-SM66-NEXT:    [[GEP:%.*]] = getelementptr i32, ptr [[ACCUM_I_FLAT]], i32 0
-; CHECK-SM66-NEXT:    call void @llvm.lifetime.start.p0(i64 4, ptr nonnull [[ACCUM_I_FLAT]])
-; CHECK-SM66-NEXT:    store i32 0, ptr [[GEP]], align 4
-; CHECK-SM66-NEXT:    call void @llvm.lifetime.end.p0(i64 4, ptr nonnull [[ACCUM_I_FLAT]])
-; 
-; CHECK-PREPARE-NEXT:    [[ACCUM_I_FLAT:%.*]] = alloca [1 x i32], align 4
-; CHECK-PREPARE-NEXT:    [[GEP:%.*]] = getelementptr i32, ptr [[ACCUM_I_FLAT]], i32 0
-; CHECK-PREPARE-NEXT:    [[BITCAST:%.*]] = bitcast ptr [[ACCUM_I_FLAT]] to ptr
-; CHECK-PREPARE-NEXT:    call void @llvm.lifetime.start.p0(i64 4, ptr nonnull [[BITCAST]])
-; CHECK-PREPARE-NEXT:    store i32 0, ptr [[GEP]], align 4
-; CHECK-PREPARE-NEXT:    [[BITCAST:%.*]] = bitcast ptr [[ACCUM_I_FLAT]] to ptr
-; CHECK-PREPARE-NEXT:    call void @llvm.lifetime.end.p0(i64 4, ptr nonnull [[BITCAST]])
-; 
-; CHECK-NEXT:    ret void
-;
-define void @test_legal_lifetime()  {
-  %accum.i.flat = alloca [1 x i32], align 4
-  %gep = getelementptr i32, ptr %accum.i.flat, i32 0
-  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %accum.i.flat)
-  store i32 0, ptr %gep, align 4
-  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %accum.i.flat)
-  ret void
-}
-
-; CHECK-PREPARE-DAG: attributes [[LIFETIME_ATTRS:#.*]] = { nounwind }
-
-; CHECK-PREPARE-DAG: ; Function Attrs: nounwind
-; CHECK-PREPARE-DAG: declare void @llvm.lifetime.start.p0(i64, ptr) [[LIFETIME_ATTRS]]
-
-; CHECK-PREPARE-DAG: ; Function Attrs: nounwind
-; CHECK-PREPARE-DAG: declare void @llvm.lifetime.end.p0(i64, ptr) [[LIFETIME_ATTRS]]
-
-; Function Attrs: nounwind memory(argmem: readwrite)
-declare void @llvm.lifetime.end.p0(i64, ptr) #0
-
-; Function Attrs: nounwind memory(argmem: readwrite)
-declare void @llvm.lifetime.start.p0(i64, ptr) #0
-
-attributes #0 = { nounwind memory(argmem: readwrite) }
-
-; Set the validator version to 1.6
-!dx.valver = !{!0}
-!0 = !{i32 1, i32 6}
diff --git a/llvm/test/CodeGen/DirectX/legalize-lifetimes-valver-1.5.ll b/llvm/test/CodeGen/DirectX/legalize-lifetimes.ll
similarity index 74%
rename from llvm/test/CodeGen/DirectX/legalize-lifetimes-valver-1.5.ll
rename to llvm/test/CodeGen/DirectX/legalize-lifetimes.ll
index e485fa20ddfce..44b2419a05d64 100644
--- a/llvm/test/CodeGen/DirectX/legalize-lifetimes-valver-1.5.ll
+++ b/llvm/test/CodeGen/DirectX/legalize-lifetimes.ll
@@ -3,9 +3,9 @@
 ; CHECK-LABEL: define void @test_legal_lifetime() {
 ; CHECK-NEXT:    [[ACCUM_I_FLAT:%.*]] = alloca [1 x i32], align 4
 ; CHECK-NEXT:    [[GEP:%.*]] = getelementptr i32, ptr [[ACCUM_I_FLAT]], i32 0
-; CHECK-NEXT:    store [1 x i32] zeroinitializer, ptr [[ACCUM_I_FLAT]], align 4
+; CHECK-NEXT:    call void @llvm.lifetime.start.p0(i64 4, ptr nonnull [[ACCUM_I_FLAT]])
 ; CHECK-NEXT:    store i32 0, ptr [[GEP]], align 4
-; CHECK-NEXT:    store [1 x i32] zeroinitializer, ptr [[ACCUM_I_FLAT]], align 4
+; CHECK-NEXT:    call void @llvm.lifetime.end.p0(i64 4, ptr nonnull [[ACCUM_I_FLAT]])
 ; CHECK-NEXT:    ret void
 ;
 define void @test_legal_lifetime()  {
@@ -16,7 +16,3 @@ define void @test_legal_lifetime()  {
   call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %accum.i.flat)
   ret void
 }
-
-; Set the validator version to 1.5
-!dx.valver = !{!0}
-!0 = !{i32 1, i32 5}

@Icohedron Icohedron merged commit 509af52 into llvm:main Jul 21, 2025
8 of 12 checks passed
Icohedron added a commit that referenced this pull request Jul 21, 2025
…s when DXIL version is lower than 1.6 (#147432)"" (#149882)

Reverts #149874

Reverted the wrong PR by mistake.
llvm-sync bot pushed a commit to arm/arm-toolchain that referenced this pull request Jul 21, 2025
…cs to stores when DXIL version is lower than 1.6 (#147432)"" (#149882)

Reverts llvm/llvm-project#149874

Reverted the wrong PR by mistake.
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.

2 participants