[llvm] [DirectX] fix crash in passes when building with LLVM_ENABLE_EXPENSIV… (PR #150483)

Farzon Lotfi via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 24 11:08:56 PDT 2025


https://github.com/farzonl created https://github.com/llvm/llvm-project/pull/150483

…E_CHECKS

fixes #148681

For the scalarizer pass we just need to indicate that scalarization took place, I used the logic for knowing when to eraseFromParent to indicate this.

For the DXILLegalizePass and DXILResourceAccess.cpp preserve all seems to be the wrong behavior so stopped doing that.

>From 13a0295e900c8f5b1a2bd683c965c8a523760329 Mon Sep 17 00:00:00 2001
From: Farzon Lotfi <farzonlotfi at microsoft.com>
Date: Thu, 24 Jul 2025 13:59:28 -0400
Subject: [PATCH] [DirectX] fix crash in passes when building with
 LLVM_ENABLE_EXPENSIVE_CHECKS

fixes #148681

For the scalarizer pass we just need to indicate that scalarization took
place, I used the logic for knowing when to eraseFromParent to indicate
this.

For the DXILLegalizePass and DXILResourceAccess.cpp preserve all seems
to be the wrong behavior so stopped doing that.
---
 llvm/lib/Target/DirectX/DXILLegalizePass.cpp   | 13 ++++++-------
 llvm/lib/Target/DirectX/DXILResourceAccess.cpp | 11 +++++++----
 llvm/lib/Transforms/Scalar/Scalarizer.cpp      |  6 ++++--
 3 files changed, 17 insertions(+), 13 deletions(-)

diff --git a/llvm/lib/Target/DirectX/DXILLegalizePass.cpp b/llvm/lib/Target/DirectX/DXILLegalizePass.cpp
index c73648f21e8d7..4879c8e640d0c 100644
--- a/llvm/lib/Target/DirectX/DXILLegalizePass.cpp
+++ b/llvm/lib/Target/DirectX/DXILLegalizePass.cpp
@@ -629,7 +629,6 @@ class DXILLegalizationPipeline {
 
       for (auto *Inst : reverse(ToRemove))
         Inst->eraseFromParent();
-
       MadeChange |= !ToRemove.empty();
     }
     return MadeChange;
@@ -676,16 +675,16 @@ class DXILLegalizeLegacy : public FunctionPass {
 PreservedAnalyses DXILLegalizePass::run(Function &F,
                                         FunctionAnalysisManager &FAM) {
   DXILLegalizationPipeline DXLegalize;
-  bool MadeChanges = DXLegalize.runLegalizationPipeline(F);
-  if (!MadeChanges)
-    return PreservedAnalyses::all();
-  PreservedAnalyses PA;
-  return PA;
+  DXLegalize.runLegalizationPipeline(F);
+  // Note: Nothing seems to need Legalization to preserve analysis.
+  // Simplest fix was to not preserve analysis.
+  return PreservedAnalyses::none();
 }
 
 bool DXILLegalizeLegacy::runOnFunction(Function &F) {
   DXILLegalizationPipeline DXLegalize;
-  return DXLegalize.runLegalizationPipeline(F);
+  DXLegalize.runLegalizationPipeline(F);
+  return true;
 }
 
 char DXILLegalizeLegacy::ID = 0;
diff --git a/llvm/lib/Target/DirectX/DXILResourceAccess.cpp b/llvm/lib/Target/DirectX/DXILResourceAccess.cpp
index 566f3a98457a4..ae3805189f1bb 100644
--- a/llvm/lib/Target/DirectX/DXILResourceAccess.cpp
+++ b/llvm/lib/Target/DirectX/DXILResourceAccess.cpp
@@ -264,10 +264,10 @@ PreservedAnalyses DXILResourceAccess::run(Function &F,
       MAMProxy.getCachedResult<DXILResourceTypeAnalysis>(*F.getParent());
   assert(DRTM && "DXILResourceTypeAnalysis must be available");
 
-  bool MadeChanges = transformResourcePointers(F, *DRTM);
-  if (!MadeChanges)
-    return PreservedAnalyses::all();
+  transformResourcePointers(F, *DRTM);
 
+  // Note: Regardless of transformation only preserve
+  // DXILResourceTypeAnalysis and DominatorTreeAnalysis.
   PreservedAnalyses PA;
   PA.preserve<DXILResourceTypeAnalysis>();
   PA.preserve<DominatorTreeAnalysis>();
@@ -281,7 +281,10 @@ class DXILResourceAccessLegacy : public FunctionPass {
     DXILResourceTypeMap &DRTM =
         getAnalysis<DXILResourceTypeWrapperPass>().getResourceTypeMap();
 
-    return transformResourcePointers(F, DRTM);
+    transformResourcePointers(F, DRTM);
+    // Note: Always returning true ensures the pass manager knows
+    // we've potentially modified the IR
+    return true;
   }
   StringRef getPassName() const override { return "DXIL Resource Access"; }
   DXILResourceAccessLegacy() : FunctionPass(ID) {}
diff --git a/llvm/lib/Transforms/Scalar/Scalarizer.cpp b/llvm/lib/Transforms/Scalar/Scalarizer.cpp
index ced61cb8e51fe..93cc45a81ceaf 100644
--- a/llvm/lib/Transforms/Scalar/Scalarizer.cpp
+++ b/llvm/lib/Transforms/Scalar/Scalarizer.cpp
@@ -458,8 +458,11 @@ bool ScalarizerVisitor::visit(Function &F) {
       Instruction *I = &*II;
       bool Done = InstVisitor::visit(I);
       ++II;
-      if (Done && I->getType()->isVoidTy())
+      if (Done && I->getType()->isVoidTy()) {
         I->eraseFromParent();
+        // Explicitly mark that we've modified the IR
+        Scalarized = true;
+      }
     }
   }
   return finish();
@@ -1334,7 +1337,6 @@ bool ScalarizerVisitor::finish() {
   Scalarized = false;
 
   RecursivelyDeleteTriviallyDeadInstructionsPermissive(PotentiallyDeadInstrs);
-
   return true;
 }
 



More information about the llvm-commits mailing list