[llvm] [DirectX] Make DXILOpLowering responsible for cleaning up dead intrin… (PR #138199)
Justin Bogner via llvm-commits
llvm-commits at lists.llvm.org
Thu May 1 13:57:05 PDT 2025
https://github.com/bogner created https://github.com/llvm/llvm-project/pull/138199
…sics
This moves the responsibility for cleaning up dead intrinsics from DXILFinalizeLinkage to DXILOpLowering, and moves DXILFinalizeLinkage back to it's pre-#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 #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 #138180.
>From c681e152e9cad015f5b0997ce4ade440b49ff5ea Mon Sep 17 00:00:00 2001
From: Justin Bogner <mail at justinbogner.com>
Date: Thu, 1 May 2025 13:01:50 -0700
Subject: [PATCH] [DirectX] Make DXILOpLowering responsible for cleaning up
dead intrinsics
This moves the responsibility for cleaning up dead intrinsics from
DXILFinalizeLinkage to DXILOpLowering, and moves DXILFinalizeLinkage back to
it's pre-#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 #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 #138180.
---
.../lib/Target/DirectX/DXILFinalizeLinkage.cpp | 8 --------
llvm/lib/Target/DirectX/DXILOpLowering.cpp | 18 ++++++++++++------
.../Target/DirectX/DirectXTargetMachine.cpp | 2 +-
llvm/test/CodeGen/DirectX/llc-pipeline.ll | 2 +-
4 files changed, 14 insertions(+), 16 deletions(-)
diff --git a/llvm/lib/Target/DirectX/DXILFinalizeLinkage.cpp b/llvm/lib/Target/DirectX/DXILFinalizeLinkage.cpp
index 7651617adc43b..035899205bf8c 100644
--- a/llvm/lib/Target/DirectX/DXILFinalizeLinkage.cpp
+++ b/llvm/lib/Target/DirectX/DXILFinalizeLinkage.cpp
@@ -36,14 +36,6 @@ static bool finalizeLinkage(Module &M) {
M.getFunctionList().erase(F);
}
- // Do a pass over intrinsics that are no longer used and remove them.
- Funcs.clear();
- for (Function &F : M.functions())
- if (F.isIntrinsic() && F.use_empty())
- Funcs.push_back(&F);
- for (Function *F : Funcs)
- F->eraseFromParent();
-
return false;
}
diff --git a/llvm/lib/Target/DirectX/DXILOpLowering.cpp b/llvm/lib/Target/DirectX/DXILOpLowering.cpp
index fdaffb6b5e49e..59db18fd0df6a 100644
--- a/llvm/lib/Target/DirectX/DXILOpLowering.cpp
+++ b/llvm/lib/Target/DirectX/DXILOpLowering.cpp
@@ -756,14 +756,20 @@ class OpLowerer {
case Intrinsic::lifetime_start:
case Intrinsic::lifetime_end:
case Intrinsic::not_intrinsic:
+ if (F.use_empty())
+ F.eraseFromParent();
continue;
- default: {
- SmallString<128> Msg =
- formatv("Unsupported intrinsic {0} for DXIL lowering", F.getName());
- M.getContext().emitError(Msg);
- HasErrors |= true;
+ default:
+ if (F.use_empty())
+ F.eraseFromParent();
+ else {
+ SmallString<128> Msg = formatv(
+ "Unsupported intrinsic {0} for DXIL lowering", F.getName());
+ M.getContext().emitError(Msg);
+ HasErrors |= true;
+ }
break;
- }
+
#define DXIL_OP_INTRINSIC(OpCode, Intrin, ...) \
case Intrin: \
HasErrors |= replaceFunctionWithOp( \
diff --git a/llvm/lib/Target/DirectX/DirectXTargetMachine.cpp b/llvm/lib/Target/DirectX/DirectXTargetMachine.cpp
index 10f4b4ee76619..398abd66dda16 100644
--- a/llvm/lib/Target/DirectX/DirectXTargetMachine.cpp
+++ b/llvm/lib/Target/DirectX/DirectXTargetMachine.cpp
@@ -98,6 +98,7 @@ class DirectXPassConfig : public TargetPassConfig {
FunctionPass *createTargetRegisterAllocator(bool) override { return nullptr; }
void addCodeGenPrepare() override {
+ addPass(createDXILFinalizeLinkageLegacyPass());
addPass(createDXILIntrinsicExpansionLegacyPass());
addPass(createDXILCBufferAccessLegacyPass());
addPass(createDXILDataScalarizationLegacyPass());
@@ -108,7 +109,6 @@ class DirectXPassConfig : public TargetPassConfig {
addPass(createScalarizerPass(DxilScalarOptions));
addPass(createDXILForwardHandleAccessesLegacyPass());
addPass(createDXILLegalizeLegacyPass());
- addPass(createDXILFinalizeLinkageLegacyPass());
addPass(createDXILTranslateMetadataLegacyPass());
addPass(createDXILOpLoweringLegacyPass());
addPass(createDXILPrepareModulePass());
diff --git a/llvm/test/CodeGen/DirectX/llc-pipeline.ll b/llvm/test/CodeGen/DirectX/llc-pipeline.ll
index 55dd86c9fad1d..a2412b6324a05 100644
--- a/llvm/test/CodeGen/DirectX/llc-pipeline.ll
+++ b/llvm/test/CodeGen/DirectX/llc-pipeline.ll
@@ -13,6 +13,7 @@
; CHECK-OBJ-NEXT: Create Garbage Collector Module Metadata
; CHECK-NEXT: ModulePass Manager
+; CHECK-NEXT: DXIL Finalize Linkage
; CHECK-NEXT: DXIL Intrinsic Expansion
; CHECK-NEXT: DXIL CBuffer Access
; CHECK-NEXT: DXIL Data Scalarization
@@ -23,7 +24,6 @@
; CHECK-NEXT: Scalarize vector operations
; CHECK-NEXT: DXIL Forward Handle Accesses
; CHECK-NEXT: DXIL Legalizer
-; CHECK-NEXT: DXIL Finalize Linkage
; CHECK-NEXT: DXIL Resources Analysis
; CHECK-NEXT: DXIL Module Metadata analysis
; CHECK-NEXT: DXIL Shader Flag Analysis
More information about the llvm-commits
mailing list