Skip to content

Commit 82f5a0c

Browse files
bognerIanWood1
authored andcommitted
[DirectX] Make DXILOpLowering responsible for cleaning up dead intrinsics (llvm#138199)
This moves the responsibility for cleaning up dead intrinsics from DXILFinalizeLinkage to DXILOpLowering, and moves DXILFinalizeLinkage back to it's pre-llvm#136244 place in the pipeline. Doing this avoids issues with DXIL passes running on obviously dead code, and makes it more clear what DXILFinalizeLinkage is really doing. This also helps with the story for llvm#134260, as cleaning up dead intrinsics doesn't make sense if this becomes a more generic pass. Note that test/CodeGen/DirectX/remove-dead-intriniscs.ll already covers most of the testing here. It'd be nice to have something that catches the regression from changing the pass ordering but I couldn't come up with anything that wouldn't be incredibly fragile. Fixes llvm#138180.
1 parent 98fc26c commit 82f5a0c

File tree

4 files changed

+14
-16
lines changed

4 files changed

+14
-16
lines changed

llvm/lib/Target/DirectX/DXILFinalizeLinkage.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,6 @@ static bool finalizeLinkage(Module &M) {
3636
M.getFunctionList().erase(F);
3737
}
3838

39-
// Do a pass over intrinsics that are no longer used and remove them.
40-
Funcs.clear();
41-
for (Function &F : M.functions())
42-
if (F.isIntrinsic() && F.use_empty())
43-
Funcs.push_back(&F);
44-
for (Function *F : Funcs)
45-
F->eraseFromParent();
46-
4739
return false;
4840
}
4941

llvm/lib/Target/DirectX/DXILOpLowering.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -756,14 +756,20 @@ class OpLowerer {
756756
case Intrinsic::lifetime_start:
757757
case Intrinsic::lifetime_end:
758758
case Intrinsic::not_intrinsic:
759+
if (F.use_empty())
760+
F.eraseFromParent();
759761
continue;
760-
default: {
761-
SmallString<128> Msg =
762-
formatv("Unsupported intrinsic {0} for DXIL lowering", F.getName());
763-
M.getContext().emitError(Msg);
764-
HasErrors |= true;
762+
default:
763+
if (F.use_empty())
764+
F.eraseFromParent();
765+
else {
766+
SmallString<128> Msg = formatv(
767+
"Unsupported intrinsic {0} for DXIL lowering", F.getName());
768+
M.getContext().emitError(Msg);
769+
HasErrors |= true;
770+
}
765771
break;
766-
}
772+
767773
#define DXIL_OP_INTRINSIC(OpCode, Intrin, ...) \
768774
case Intrin: \
769775
HasErrors |= replaceFunctionWithOp( \

llvm/lib/Target/DirectX/DirectXTargetMachine.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ class DirectXPassConfig : public TargetPassConfig {
9898

9999
FunctionPass *createTargetRegisterAllocator(bool) override { return nullptr; }
100100
void addCodeGenPrepare() override {
101+
addPass(createDXILFinalizeLinkageLegacyPass());
101102
addPass(createDXILIntrinsicExpansionLegacyPass());
102103
addPass(createDXILCBufferAccessLegacyPass());
103104
addPass(createDXILDataScalarizationLegacyPass());
@@ -108,7 +109,6 @@ class DirectXPassConfig : public TargetPassConfig {
108109
addPass(createScalarizerPass(DxilScalarOptions));
109110
addPass(createDXILForwardHandleAccessesLegacyPass());
110111
addPass(createDXILLegalizeLegacyPass());
111-
addPass(createDXILFinalizeLinkageLegacyPass());
112112
addPass(createDXILTranslateMetadataLegacyPass());
113113
addPass(createDXILOpLoweringLegacyPass());
114114
addPass(createDXILPrepareModulePass());

llvm/test/CodeGen/DirectX/llc-pipeline.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
; CHECK-OBJ-NEXT: Create Garbage Collector Module Metadata
1414

1515
; CHECK-NEXT: ModulePass Manager
16+
; CHECK-NEXT: DXIL Finalize Linkage
1617
; CHECK-NEXT: DXIL Intrinsic Expansion
1718
; CHECK-NEXT: DXIL CBuffer Access
1819
; CHECK-NEXT: DXIL Data Scalarization
@@ -23,7 +24,6 @@
2324
; CHECK-NEXT: Scalarize vector operations
2425
; CHECK-NEXT: DXIL Forward Handle Accesses
2526
; CHECK-NEXT: DXIL Legalizer
26-
; CHECK-NEXT: DXIL Finalize Linkage
2727
; CHECK-NEXT: DXIL Resources Analysis
2828
; CHECK-NEXT: DXIL Module Metadata analysis
2929
; CHECK-NEXT: DXIL Shader Flag Analysis

0 commit comments

Comments
 (0)