Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[DirectX] Make DXILOpLowering responsible for cleaning up dead intrinsics #138199

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
May 2, 2025

Conversation

bogner
Copy link
Contributor

@bogner bogner commented May 1, 2025

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.

…sics

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.
@llvmbot
Copy link
Member

llvmbot commented May 1, 2025

@llvm/pr-subscribers-backend-directx

Author: Justin Bogner (bogner)

Changes

…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.


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

4 Files Affected:

  • (modified) llvm/lib/Target/DirectX/DXILFinalizeLinkage.cpp (-8)
  • (modified) llvm/lib/Target/DirectX/DXILOpLowering.cpp (+12-6)
  • (modified) llvm/lib/Target/DirectX/DirectXTargetMachine.cpp (+1-1)
  • (modified) llvm/test/CodeGen/DirectX/llc-pipeline.ll (+1-1)
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

@bogner bogner changed the title [DirectX] Make DXILOpLowering responsible for cleaning up dead intrin… [DirectX] Make DXILOpLowering responsible for cleaning up dead intrinsics May 1, 2025
@bogner bogner merged commit ae34440 into llvm:main May 2, 2025
14 checks passed
@llvm-ci
Copy link
Collaborator

llvm-ci commented May 2, 2025

LLVM Buildbot has detected a new failure on builder ml-opt-dev-x86-64 running on ml-opt-dev-x86-64-b2 while building llvm at step 4 "cmake-configure".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/137/builds/17826

Here is the relevant piece of the build log for the reference
Step 4 (cmake-configure) failure: cmake (failure)
...
-- Targeting XCore
-- Registering ExampleIRTransforms as a pass plugin (static build: OFF)
-- Registering Bye as a pass plugin (static build: OFF)
-- Failed to find LLVM FileCheck
-- Google Benchmark version: v0.0.0, normalized to 0.0.0
-- Looking for shm_open in rt
-- Looking for shm_open in rt - found
-- Performing Test HAVE_CXX_FLAG_WALL
-- Performing Test HAVE_CXX_FLAG_WALL - Success
-- Performing Test HAVE_CXX_FLAG_WEXTRA
-- Performing Test HAVE_CXX_FLAG_WEXTRA - Success
-- Performing Test HAVE_CXX_FLAG_WSHADOW
-- Performing Test HAVE_CXX_FLAG_WSHADOW - Success
-- Performing Test HAVE_CXX_FLAG_WFLOAT_EQUAL
-- Performing Test HAVE_CXX_FLAG_WFLOAT_EQUAL - Success
-- Performing Test HAVE_CXX_FLAG_WOLD_STYLE_CAST
-- Performing Test HAVE_CXX_FLAG_WOLD_STYLE_CAST - Success
-- Performing Test HAVE_CXX_FLAG_WSUGGEST_OVERRIDE
-- Performing Test HAVE_CXX_FLAG_WSUGGEST_OVERRIDE - Success
-- Performing Test HAVE_CXX_FLAG_PEDANTIC
-- Performing Test HAVE_CXX_FLAG_PEDANTIC - Success
-- Performing Test HAVE_CXX_FLAG_PEDANTIC_ERRORS
-- Performing Test HAVE_CXX_FLAG_PEDANTIC_ERRORS - Success
-- Performing Test HAVE_CXX_FLAG_WSHORTEN_64_TO_32
-- Performing Test HAVE_CXX_FLAG_WSHORTEN_64_TO_32 - Failed
-- Performing Test HAVE_CXX_FLAG_FSTRICT_ALIASING
-- Performing Test HAVE_CXX_FLAG_FSTRICT_ALIASING - Success
-- Performing Test HAVE_CXX_FLAG_WNO_DEPRECATED_DECLARATIONS
-- Performing Test HAVE_CXX_FLAG_WNO_DEPRECATED_DECLARATIONS - Success
-- Performing Test HAVE_CXX_FLAG_FNO_EXCEPTIONS
-- Performing Test HAVE_CXX_FLAG_FNO_EXCEPTIONS - Success
-- Performing Test HAVE_CXX_FLAG_WSTRICT_ALIASING
-- Performing Test HAVE_CXX_FLAG_WSTRICT_ALIASING - Success
-- Performing Test HAVE_CXX_FLAG_WD654
-- Performing Test HAVE_CXX_FLAG_WD654 - Failed
-- Performing Test HAVE_CXX_FLAG_WTHREAD_SAFETY
-- Performing Test HAVE_CXX_FLAG_WTHREAD_SAFETY - Failed
-- Performing Test HAVE_CXX_FLAG_COVERAGE
-- Performing Test HAVE_CXX_FLAG_COVERAGE - Success
-- Compiling and running to test HAVE_GNU_POSIX_REGEX
-- Performing Test HAVE_GNU_POSIX_REGEX -- failed to compile
-- Compiling and running to test HAVE_POSIX_REGEX
-- Performing Test HAVE_POSIX_REGEX -- success
-- Compiling and running to test HAVE_STEADY_CLOCK
-- Performing Test HAVE_STEADY_CLOCK -- success
-- Compiling and running to test HAVE_PTHREAD_AFFINITY
-- Performing Test HAVE_PTHREAD_AFFINITY -- failed to compile
-- Configuring incomplete, errors occurred!
See also "/b/ml-opt-dev-x86-64-b1/build/CMakeFiles/CMakeOutput.log".
See also "/b/ml-opt-dev-x86-64-b1/build/CMakeFiles/CMakeError.log".

IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
…sics (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.
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
…sics (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.
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
…sics (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.
GeorgeARM pushed a commit to GeorgeARM/llvm-project that referenced this pull request May 7, 2025
…sics (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.
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.

[DirectX] Running DXILFinalizeLinkage late causes DXILForwardHandleAccesses to fall over in dead code
5 participants