Skip to content

Revert "[DirectX] Legalize lifetime intrinsics for DXIL" #149883

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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 3 additions & 20 deletions llvm/lib/Target/DirectX/DXILPrepare.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
#include "llvm/IR/AttributeMask.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/Instruction.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/Module.h"
#include "llvm/InitializePasses.h"
#include "llvm/Pass.h"
Expand Down Expand Up @@ -240,11 +239,6 @@ class DXILPrepareModule : public ModulePass {
for (size_t Idx = 0, End = F.arg_size(); Idx < End; ++Idx)
F.removeParamAttrs(Idx, AttrMask);

// Lifetime intrinsics in LLVM 3.7 do not have the memory FnAttr
if (Intrinsic::ID IID = F.getIntrinsicID();
IID == Intrinsic::lifetime_start || IID == Intrinsic::lifetime_end)
F.removeFnAttr(Attribute::Memory);

for (auto &BB : F) {
IRBuilder<> Builder(&BB);
for (auto &I : make_early_inc_range(BB)) {
Expand All @@ -253,7 +247,7 @@ class DXILPrepareModule : public ModulePass {

// Emtting NoOp bitcast instructions allows the ValueEnumerator to be
// unmodified as it reserves instruction IDs during contruction.
if (auto *LI = dyn_cast<LoadInst>(&I)) {
if (auto LI = dyn_cast<LoadInst>(&I)) {
if (Value *NoOpBitcast = maybeGenerateBitcast(
Builder, PointerTypes, I, LI->getPointerOperand(),
LI->getType())) {
Expand All @@ -263,7 +257,7 @@ class DXILPrepareModule : public ModulePass {
}
continue;
}
if (auto *SI = dyn_cast<StoreInst>(&I)) {
if (auto SI = dyn_cast<StoreInst>(&I)) {
if (Value *NoOpBitcast = maybeGenerateBitcast(
Builder, PointerTypes, I, SI->getPointerOperand(),
SI->getValueOperand()->getType())) {
Expand All @@ -274,7 +268,7 @@ class DXILPrepareModule : public ModulePass {
}
continue;
}
if (auto *GEP = dyn_cast<GetElementPtrInst>(&I)) {
if (auto GEP = dyn_cast<GetElementPtrInst>(&I)) {
if (Value *NoOpBitcast = maybeGenerateBitcast(
Builder, PointerTypes, I, GEP->getPointerOperand(),
GEP->getSourceElementType()))
Expand All @@ -286,17 +280,6 @@ class DXILPrepareModule : public ModulePass {
CB->removeRetAttrs(AttrMask);
for (size_t Idx = 0, End = CB->arg_size(); Idx < End; ++Idx)
CB->removeParamAttrs(Idx, AttrMask);
// LLVM 3.7 Lifetime intrinics require an i8* pointer operand, so we
// insert a bitcast here to ensure that is the case
if (isa<LifetimeIntrinsic>(CB)) {
Value *PtrOperand = CB->getArgOperand(1);
Builder.SetInsertPoint(CB);
PointerType *PtrTy = cast<PointerType>(PtrOperand->getType());
Value *NoOpBitcast = Builder.Insert(
CastInst::Create(Instruction::BitCast, PtrOperand,
Builder.getPtrTy(PtrTy->getAddressSpace())));
CB->setArgOperand(1, NoOpBitcast);
}
continue;
}
}
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Target/DirectX/DXILShaderFlags.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ void ModuleShaderFlags::updateFunctionFlags(ComputedShaderFlags &CSF,
if (!CSF.Int64Ops)
CSF.Int64Ops = I.getType()->isIntegerTy(64);

if (!CSF.Int64Ops && !isa<LifetimeIntrinsic>(&I)) {
if (!CSF.Int64Ops) {
for (const Value *Op : I.operands()) {
if (Op->getType()->isIntegerTy(64)) {
CSF.Int64Ops = true;
Expand Down
36 changes: 1 addition & 35 deletions llvm/lib/Target/DirectX/DXILWriter/DXILBitcodeWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2545,25 +2545,6 @@ void DXILBitcodeWriter::writeInstruction(const Instruction &I, unsigned InstID,
Vals.clear();
}

// HLSL Change
namespace {
struct ValueNameCreator {
MallocAllocator Allocator;
SmallVector<ValueName *, 2>
ValueNames; // SmallVector N = 2 because we currently only expect this
// to hold ValueNames for Lifetime intrinsics
~ValueNameCreator() {
for (auto *VN : ValueNames)
VN->Destroy(Allocator);
}
ValueName *create(StringRef Name, Value *V) {
ValueName *VN = ValueName::create(Name, Allocator, V);
ValueNames.push_back(VN);
return VN;
}
};
} // anonymous namespace

// Emit names for globals/functions etc.
void DXILBitcodeWriter::writeFunctionLevelValueSymbolTable(
const ValueSymbolTable &VST) {
Expand All @@ -2578,24 +2559,9 @@ void DXILBitcodeWriter::writeFunctionLevelValueSymbolTable(
// to ensure the binary is the same no matter what values ever existed.
SmallVector<const ValueName *, 16> SortedTable;

// HLSL Change
ValueNameCreator VNC;
for (auto &VI : VST) {
ValueName *VN = VI.second->getValueName();
// Clang mangles lifetime intrinsic names by appending '.p0' to the end,
// making them invalid lifetime intrinsics in LLVM 3.7. We can't
// demangle in dxil-prepare because it would result in invalid IR.
// Therefore we have to do this in the bitcode writer while writing its
// name to the symbol table.
if (const Function *Fn = dyn_cast<Function>(VI.getValue());
Fn && Fn->isIntrinsic()) {
Intrinsic::ID IID = Fn->getIntrinsicID();
if (IID == Intrinsic::lifetime_start || IID == Intrinsic::lifetime_end)
VN = VNC.create(Intrinsic::getBaseName(IID), VI.second);
}
SortedTable.push_back(VN);
SortedTable.push_back(VI.second->getValueName());
}

// The keys are unique, so there shouldn't be stability issues.
llvm::sort(SortedTable, [](const ValueName *A, const ValueName *B) {
return A->first() < B->first();
Expand Down
36 changes: 0 additions & 36 deletions llvm/test/CodeGen/DirectX/ShaderFlags/lifetimes-noint64op.ll

This file was deleted.

25 changes: 0 additions & 25 deletions llvm/test/CodeGen/DirectX/legalize-lifetimes-valver-1.6.ll
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
; 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() {
;
Expand All @@ -16,14 +15,6 @@
; 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() {
Expand All @@ -35,22 +26,6 @@ define void @test_legal_lifetime() {
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}
38 changes: 0 additions & 38 deletions llvm/test/tools/dxil-dis/lifetimes.ll

This file was deleted.

Loading