[llvm] [DirectX] Avoid crash on non-unique resource error (PR #206396)
Dan Brown via llvm-commits
llvm-commits at lists.llvm.org
Sun Jun 28 20:33:08 PDT 2026
https://github.com/danbrown-amd created https://github.com/llvm/llvm-project/pull/206396
Addresses #205165 and #205168
>From 5ebf30e94f282a907cccbebe4b325223e278f62f Mon Sep 17 00:00:00 2001
From: danbrown-amd <danbrown at amd.com>
Date: Sun, 28 Jun 2026 21:28:53 -0600
Subject: [PATCH] [DirectX] Avoid crash on non-unique resource error Addresses
#205165 and #205168
---
llvm/lib/Analysis/DXILResource.cpp | 16 ++++++++++++++++
llvm/lib/Target/DirectX/DXContainerGlobals.cpp | 3 +++
.../Target/DirectX/DXILForwardHandleAccesses.cpp | 7 +++++++
llvm/lib/Target/DirectX/DXILOpLowering.cpp | 8 ++++++++
llvm/lib/Target/DirectX/DXILResourceAccess.cpp | 9 +++++++++
.../DirectX/DXILResourceImplicitBinding.cpp | 7 +++++++
.../Target/DirectX/DXILWriter/DXILWriterPass.cpp | 6 ++++++
.../CodeGen/DirectX/ResourceAccess/non-unique.ll | 5 ++++-
8 files changed, 60 insertions(+), 1 deletion(-)
diff --git a/llvm/lib/Analysis/DXILResource.cpp b/llvm/lib/Analysis/DXILResource.cpp
index a427a261d82d3..21b8bfcb66097 100644
--- a/llvm/lib/Analysis/DXILResource.cpp
+++ b/llvm/lib/Analysis/DXILResource.cpp
@@ -1101,6 +1101,10 @@ AnalysisKey DXILResourceBindingAnalysis::Key;
DXILResourceMap DXILResourceAnalysis::run(Module &M,
ModuleAnalysisManager &AM) {
+ if (auto *DH = M.getContext().getDiagHandlerPtr())
+ if (DH->HasErrors)
+ return DXILResourceMap();
+
DXILResourceMap Data;
DXILResourceTypeMap &DRTM = AM.getResult<DXILResourceTypeAnalysis>(M);
Data.populate(M, DRTM);
@@ -1109,6 +1113,10 @@ DXILResourceMap DXILResourceAnalysis::run(Module &M,
DXILResourceBindingInfo
DXILResourceBindingAnalysis::run(Module &M, ModuleAnalysisManager &AM) {
+ if (auto *DH = M.getContext().getDiagHandlerPtr())
+ if (DH->HasErrors)
+ return DXILResourceBindingInfo();
+
DXILResourceBindingInfo Data;
DXILResourceTypeMap &DRTM = AM.getResult<DXILResourceTypeAnalysis>(M);
Data.populate(M, DRTM);
@@ -1149,6 +1157,10 @@ void DXILResourceWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
bool DXILResourceWrapperPass::runOnModule(Module &M) {
Map.reset(new DXILResourceMap());
+ if (auto *DH = M.getContext().getDiagHandlerPtr())
+ if (DH->HasErrors)
+ return false;
+
DRTM = &getAnalysis<DXILResourceTypeWrapperPass>().getResourceTypeMap();
Map->populate(M, *DRTM);
@@ -1191,6 +1203,10 @@ void DXILResourceBindingWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
bool DXILResourceBindingWrapperPass::runOnModule(Module &M) {
BindingInfo.reset(new DXILResourceBindingInfo());
+ if (auto *DH = M.getContext().getDiagHandlerPtr())
+ if (DH->HasErrors)
+ return false;
+
DXILResourceTypeMap &DRTM =
getAnalysis<DXILResourceTypeWrapperPass>().getResourceTypeMap();
BindingInfo->populate(M, DRTM);
diff --git a/llvm/lib/Target/DirectX/DXContainerGlobals.cpp b/llvm/lib/Target/DirectX/DXContainerGlobals.cpp
index 4451d46130c45..142ed6e89c72a 100644
--- a/llvm/lib/Target/DirectX/DXContainerGlobals.cpp
+++ b/llvm/lib/Target/DirectX/DXContainerGlobals.cpp
@@ -95,6 +95,9 @@ class DXContainerGlobals : public llvm::ModulePass {
} // namespace
bool DXContainerGlobals::runOnModule(Module &M) {
+ if (auto *DH = M.getContext().getDiagHandlerPtr())
+ if (DH->HasErrors)
+ return false;
llvm::SmallVector<GlobalValue *> Globals;
Globals.push_back(getFeatureFlags(M));
computeShaderHashAndDebugName(M, Globals);
diff --git a/llvm/lib/Target/DirectX/DXILForwardHandleAccesses.cpp b/llvm/lib/Target/DirectX/DXILForwardHandleAccesses.cpp
index 695eacbb2fadc..6fc4794c5d84b 100644
--- a/llvm/lib/Target/DirectX/DXILForwardHandleAccesses.cpp
+++ b/llvm/lib/Target/DirectX/DXILForwardHandleAccesses.cpp
@@ -176,6 +176,10 @@ static bool forwardHandleAccesses(Function &F, DominatorTree &DT) {
PreservedAnalyses DXILForwardHandleAccesses::run(Function &F,
FunctionAnalysisManager &AM) {
+ if (auto *DH = F.getContext().getDiagHandlerPtr())
+ if (DH->HasErrors)
+ return PreservedAnalyses::all();
+
PreservedAnalyses PA;
DominatorTree *DT = &AM.getResult<DominatorTreeAnalysis>(F);
@@ -190,6 +194,9 @@ namespace {
class DXILForwardHandleAccessesLegacy : public FunctionPass {
public:
bool runOnFunction(Function &F) override {
+ if (auto *DH = F.getContext().getDiagHandlerPtr())
+ if (DH->HasErrors)
+ return false;
DominatorTree *DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
return forwardHandleAccesses(F, *DT);
}
diff --git a/llvm/lib/Target/DirectX/DXILOpLowering.cpp b/llvm/lib/Target/DirectX/DXILOpLowering.cpp
index 93d5a08a6e0a2..421f5b41c9aca 100644
--- a/llvm/lib/Target/DirectX/DXILOpLowering.cpp
+++ b/llvm/lib/Target/DirectX/DXILOpLowering.cpp
@@ -1244,6 +1244,10 @@ class OpLowerer {
} // namespace
PreservedAnalyses DXILOpLowering::run(Module &M, ModuleAnalysisManager &MAM) {
+ if (auto *DH = M.getContext().getDiagHandlerPtr())
+ if (DH->HasErrors)
+ return PreservedAnalyses::all();
+
DXILResourceMap &DRM = MAM.getResult<DXILResourceAnalysis>(M);
DXILResourceTypeMap &DRTM = MAM.getResult<DXILResourceTypeAnalysis>(M);
const ModuleMetadataInfo MMDI = MAM.getResult<DXILMetadataAnalysis>(M);
@@ -1263,6 +1267,10 @@ namespace {
class DXILOpLoweringLegacy : public ModulePass {
public:
bool runOnModule(Module &M) override {
+ if (auto *DH = M.getContext().getDiagHandlerPtr())
+ if (DH->HasErrors)
+ return false;
+
DXILResourceMap &DRM =
getAnalysis<DXILResourceWrapperPass>().getResourceMap();
DXILResourceTypeMap &DRTM =
diff --git a/llvm/lib/Target/DirectX/DXILResourceAccess.cpp b/llvm/lib/Target/DirectX/DXILResourceAccess.cpp
index 25d860e615c17..780f82a372b44 100644
--- a/llvm/lib/Target/DirectX/DXILResourceAccess.cpp
+++ b/llvm/lib/Target/DirectX/DXILResourceAccess.cpp
@@ -13,6 +13,7 @@
#include "llvm/Analysis/VectorUtils.h"
#include "llvm/Frontend/HLSL/HLSLResource.h"
#include "llvm/IR/BasicBlock.h"
+#include "llvm/IR/Constants.h"
#include "llvm/IR/DiagnosticInfo.h"
#include "llvm/IR/Dominators.h"
#include "llvm/IR/IRBuilder.h"
@@ -744,6 +745,14 @@ static bool legalizeResourceHandles(Function &F, DXILResourceTypeMap &DRTM) {
if (!SameGlobalBinding) {
diagnoseNonUniqueResourceAccess(&I, Handles);
+ // Replace any result of the load with poison and mark the
+ // load/store and its pointer operand dead. This keeps the IR
+ // structurally valid so that downstream passes do not crash on
+ // unresolved handle intrinsics left in the module.
+ if (!I.getType()->isVoidTy())
+ I.replaceAllUsesWith(PoisonValue::get(I.getType()));
+ DeadInsts.insert(&I);
+ DeadInsts.insert(PtrOp);
continue;
}
diff --git a/llvm/lib/Target/DirectX/DXILResourceImplicitBinding.cpp b/llvm/lib/Target/DirectX/DXILResourceImplicitBinding.cpp
index b0d9ad8da10e9..a5263b4475d84 100644
--- a/llvm/lib/Target/DirectX/DXILResourceImplicitBinding.cpp
+++ b/llvm/lib/Target/DirectX/DXILResourceImplicitBinding.cpp
@@ -132,6 +132,9 @@ static bool assignBindings(Module &M, DXILResourceBindingInfo &DRBI,
PreservedAnalyses DXILResourceImplicitBinding::run(Module &M,
ModuleAnalysisManager &AM) {
+ if (auto *DH = M.getContext().getDiagHandlerPtr())
+ if (DH->HasErrors)
+ return PreservedAnalyses::all();
DXILResourceBindingInfo &DRBI = AM.getResult<DXILResourceBindingAnalysis>(M);
DXILResourceTypeMap &DRTM = AM.getResult<DXILResourceTypeAnalysis>(M);
@@ -155,6 +158,10 @@ class DXILResourceImplicitBindingLegacy : public ModulePass {
DXILResourceImplicitBindingLegacy() : ModulePass(ID) {}
bool runOnModule(Module &M) override {
+ if (auto *DH = M.getContext().getDiagHandlerPtr())
+ if (DH->HasErrors)
+ return false;
+
DXILResourceTypeMap &DRTM =
getAnalysis<DXILResourceTypeWrapperPass>().getResourceTypeMap();
DXILResourceBindingInfo &DRBI =
diff --git a/llvm/lib/Target/DirectX/DXILWriter/DXILWriterPass.cpp b/llvm/lib/Target/DirectX/DXILWriter/DXILWriterPass.cpp
index e78e6775403fa..893db77e133a7 100644
--- a/llvm/lib/Target/DirectX/DXILWriter/DXILWriterPass.cpp
+++ b/llvm/lib/Target/DirectX/DXILWriter/DXILWriterPass.cpp
@@ -52,6 +52,9 @@ class WriteDXILPass : public llvm::ModulePass {
StringRef getPassName() const override { return "Bitcode Writer"; }
bool runOnModule(Module &M) override {
+ if (auto *DH = M.getContext().getDiagHandlerPtr())
+ if (DH->HasErrors)
+ return false;
const auto DIMap = DXILDebugInfoPass::run(M);
WriteDXILToFile(M, OS, DIMap);
return false;
@@ -222,6 +225,9 @@ class EmbedDXILPass : public llvm::ModulePass {
StringRef getPassName() const override { return "DXIL Embedder"; }
bool runOnModule(Module &M) override {
+ if (auto *DH = M.getContext().getDiagHandlerPtr())
+ if (DH->HasErrors)
+ return false;
// Perform late legalization of lifetime intrinsics that would otherwise
// fail the Module Verifier if performed in an earlier pass
legalizeLifetimeIntrinsics(M);
diff --git a/llvm/test/CodeGen/DirectX/ResourceAccess/non-unique.ll b/llvm/test/CodeGen/DirectX/ResourceAccess/non-unique.ll
index 9f62197e805cd..1680075837897 100644
--- a/llvm/test/CodeGen/DirectX/ResourceAccess/non-unique.ll
+++ b/llvm/test/CodeGen/DirectX/ResourceAccess/non-unique.ll
@@ -1,12 +1,15 @@
; RUN: not opt -S -dxil-resource-access -mtriple=dxil--shadermodel6.3-library %s 2>&1 | FileCheck %s
; Test that a error message is generated for illegal resource accesses that do
-; not access a unique global resource.
+; not access a unique global resource. The offending load/store is replaced with
+; poison so the IR remains structurally valid for any pass that runs after the
+; error is diagnosed.
; CHECK: note: At resource access: store i32 %7, ptr %8, align 4
; CHECK: note: Uses resource handle: %1 = tail call target("dx.RawBuffer", i32, 1, 0) @llvm.dx.resource.handlefrombinding.tdx.RawBuffer_i32_1_0t(i32 0, i32 1, i32 1, i32 0, ptr nonnull @.str.2)
; CHECK: note: Uses resource handle: %2 = tail call target("dx.RawBuffer", i32, 1, 0) @llvm.dx.resource.handlefrombinding.tdx.RawBuffer_i32_1_0t(i32 0, i32 2, i32 1, i32 0, ptr nonnull @.str.4)
; CHECK: error: Resource access is not guaranteed to map to a unique global resource
+; CHECK-NOT: store i32 {{.*}}, ptr %8
%__cblayout_c = type <{ i32 }>
More information about the llvm-commits
mailing list