[clang] [llvm] [DirectX] Avoid crash on non-unique resource error (PR #206396)
Dan Brown via cfe-commits
cfe-commits at lists.llvm.org
Thu Nov 12 20:09:43 PST 2026
https://github.com/danbrown-amd updated https://github.com/llvm/llvm-project/pull/206396
>From 02cfeef49a1cf9d9d3dca14a5f27864b429ae4d4 Mon Sep 17 00:00:00 2001
From: danbrown-amd <danbrown at amd.com>
Date: Thu, 12 Nov 2026 22:04:46 -0600
Subject: [PATCH] [DirectX] Avoid crash on non-unique resource error
Issue regular error diagnostic instead of fatal_error with stack trace.
Resolves #205165 and #205168
Assisted-by: Claude Sonnet 5
---
.../resources/non-unique-resource-access.hlsl | 41 +++++++++++++++++++
.../lib/Target/DirectX/DXILResourceAccess.cpp | 16 +++++---
.../ResourceAccess/looped-phi-nodes-2.ll | 2 +-
.../ResourceAccess/looped-phi-nodes.ll | 2 +-
.../non-unique-pointer-select.ll | 2 +-
.../non-unique-updatecounter.ll | 2 +-
.../DirectX/ResourceAccess/non-unique.ll | 2 +-
7 files changed, 56 insertions(+), 11 deletions(-)
create mode 100644 clang/test/CodeGenHLSL/resources/non-unique-resource-access.hlsl
diff --git a/clang/test/CodeGenHLSL/resources/non-unique-resource-access.hlsl b/clang/test/CodeGenHLSL/resources/non-unique-resource-access.hlsl
new file mode 100644
index 0000000000000..1de30d5ea35e6
--- /dev/null
+++ b/clang/test/CodeGenHLSL/resources/non-unique-resource-access.hlsl
@@ -0,0 +1,41 @@
+// RUN: not %clang_dxc -T cs_6_0 -E two_globals -Vd -Fo %t.dxil %s 2>&1 | FileCheck %s --check-prefixes=CHECK,NOLOC
+// RUN: not %clang_dxc -T cs_6_0 -E two_globals -Vd -Zi -Fo %t.dxil %s 2>&1 | FileCheck %s --check-prefixes=CHECK,GLOBALS-LOC
+// RUN: not %clang_dxc -T cs_6_0 -E global_and_array -Vd -Fo %t.dxil %s 2>&1 | FileCheck %s --check-prefixes=CHECK,NOLOC
+// RUN: not %clang_dxc -T cs_6_0 -E global_and_array -Vd -Zi -Fo %t.dxil %s 2>&1 | FileCheck %s --check-prefixes=CHECK,ARRAY-LOC
+
+// Test that a resource access that can't be mapped to a unique global resource
+// is reported as a regular error rather than crashing the compiler.
+
+// CHECK-NOT: PLEASE submit a bug report
+// CHECK: warning: assignment of 'Out1' to local resource 'Out' is not to the same unique global resource
+// CHECK: warning: assignment of 'OutArr[0]' to local resource 'Out' is not to the same unique global resource
+// NOLOC: error: Resource access is not guaranteed to map to a unique global resource
+// GLOBALS-LOC: [[@LINE+18]]:{{[0-9]+}}: error: Resource access is not guaranteed to map to a unique global resource
+// ARRAY-LOC: [[@LINE+25]]:{{[0-9]+}}: error: Resource access is not guaranteed to map to a unique global resource
+// CHECK-NOT: PLEASE submit a bug report
+
+RWBuffer<uint> In : register(u0);
+RWStructuredBuffer<uint> Out0 : register(u1);
+RWStructuredBuffer<uint> Out1 : register(u2);
+RWStructuredBuffer<uint> OutArr[];
+
+cbuffer c {
+ bool cond;
+};
+
+[numthreads(64, 1, 1)]
+void two_globals(uint3 tid : SV_DispatchThreadID) {
+ RWStructuredBuffer<uint> Out = Out0;
+ if (cond)
+ Out = Out1;
+ Out[tid.x] = In[tid.x];
+}
+
+[numthreads(64, 1, 1)]
+void global_and_array(uint3 tid : SV_DispatchThreadID) {
+ RWStructuredBuffer<uint> Out = Out0;
+ if (cond)
+ Out = OutArr[0];
+ Out[tid.x] = In[tid.x];
+}
+
diff --git a/llvm/lib/Target/DirectX/DXILResourceAccess.cpp b/llvm/lib/Target/DirectX/DXILResourceAccess.cpp
index c1923caa5e283..b2f3f2323379a 100644
--- a/llvm/lib/Target/DirectX/DXILResourceAccess.cpp
+++ b/llvm/lib/Target/DirectX/DXILResourceAccess.cpp
@@ -40,10 +40,17 @@
using namespace llvm;
-[[noreturn]] static void
-diagnoseNonUniqueResourceAccess(Instruction *I,
- ArrayRef<IntrinsicInst *> Handles) {
+static void diagnoseNonUniqueResourceAccess(Instruction *I,
+ ArrayRef<IntrinsicInst *> Handles) {
LLVMContext &Context = I->getContext();
+ const char *Msg =
+ "Resource access is not guaranteed to map to a unique global resource";
+ if (I->getDebugLoc())
+ Context.diagnose(
+ DiagnosticInfoUnsupported(*I->getFunction(), Msg, I->getDebugLoc()));
+ else
+ Context.diagnose(DiagnosticInfoGeneric(Msg, DS_Error));
+
std::string InstStr;
raw_string_ostream InstOS(InstStr);
I->print(InstOS);
@@ -57,9 +64,6 @@ diagnoseNonUniqueResourceAccess(Instruction *I,
Context.diagnose(DiagnosticInfoGeneric(
"Uses resource handle:" + Twine(HandleStr), DS_Note));
}
- report_fatal_error(
- "Resource access is not guaranteed to map to a unique global resource",
- /*gen_crash_diag=*/false);
}
static Value *traverseGEPOffsets(const DataLayout &DL, IRBuilder<> &Builder,
diff --git a/llvm/test/CodeGen/DirectX/ResourceAccess/looped-phi-nodes-2.ll b/llvm/test/CodeGen/DirectX/ResourceAccess/looped-phi-nodes-2.ll
index fa5b7b8817fe3..0c051254d6a2b 100644
--- a/llvm/test/CodeGen/DirectX/ResourceAccess/looped-phi-nodes-2.ll
+++ b/llvm/test/CodeGen/DirectX/ResourceAccess/looped-phi-nodes-2.ll
@@ -6,10 +6,10 @@
; Ensure that a cyclic loop of resource ptrs reports a fatal error and exits
; compilation, rather than hanging or crashing later on the illegal access.
+; CHECK: error: Resource access is not guaranteed to map to a unique global resource
; CHECK: note: At resource access: %count = call i32 @llvm.dx.resource.updatecounter.tdx.RawBuffer_i32_1_0t(target("dx.RawBuffer", i32, 1, 0) %src, i8 1)
; CHECK-DAG: note: Uses resource handle: %handle0 = tail call target("dx.RawBuffer", i32, 1, 0) @llvm.dx.resource.handlefrombinding.tdx.RawBuffer_i32_1_0t(i32 0, i32 0, i32 1, i32 0, ptr nonnull @.str)
; CHECK-DAG: note: Uses resource handle: %handle1 = 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: LLVM ERROR: Resource access is not guaranteed to map to a unique global resource
define i32 @updatecounter_loop(i32 %n) {
entry:
diff --git a/llvm/test/CodeGen/DirectX/ResourceAccess/looped-phi-nodes.ll b/llvm/test/CodeGen/DirectX/ResourceAccess/looped-phi-nodes.ll
index 54b807e1e0d11..ebcfe0f4faf70 100644
--- a/llvm/test/CodeGen/DirectX/ResourceAccess/looped-phi-nodes.ll
+++ b/llvm/test/CodeGen/DirectX/ResourceAccess/looped-phi-nodes.ll
@@ -13,7 +13,7 @@
; RWBuffer<float> tmp = src; src = dst; dst = tmp;
; }
-; CHECK: LLVM ERROR: Resource access is not guaranteed to map to a unique global resource
+; CHECK: error: Resource access is not guaranteed to map to a unique global resource
@.str = private unnamed_addr constant [5 x i8] c"bufA\00", align 1
@.str.2 = private unnamed_addr constant [5 x i8] c"bufB\00", align 1
diff --git a/llvm/test/CodeGen/DirectX/ResourceAccess/non-unique-pointer-select.ll b/llvm/test/CodeGen/DirectX/ResourceAccess/non-unique-pointer-select.ll
index ce0fce38c96fe..4ebf334f2d8e5 100644
--- a/llvm/test/CodeGen/DirectX/ResourceAccess/non-unique-pointer-select.ll
+++ b/llvm/test/CodeGen/DirectX/ResourceAccess/non-unique-pointer-select.ll
@@ -4,10 +4,10 @@
; non-unique resource access, compilation must terminate before pointer
; lowering encounters the select.
+; CHECK: error: Resource access is not guaranteed to map to a unique global resource
; CHECK: note: At resource access: %value = load float, ptr %ptr, align 4
; CHECK-DAG: note: Uses resource handle: %handle0 = call target("dx.RawBuffer", float, 1, 0) @llvm.dx.resource.handlefrombinding.tdx.RawBuffer_f32_1_0t(i32 0, i32 0, i32 1, i32 0, ptr @BufferA.str)
; CHECK-DAG: note: Uses resource handle: %handle1 = call target("dx.RawBuffer", float, 1, 0) @llvm.dx.resource.handlefrombinding.tdx.RawBuffer_f32_1_0t(i32 0, i32 1, i32 1, i32 0, ptr @BufferB.str)
-; CHECK: LLVM ERROR: Resource access is not guaranteed to map to a unique global resource
; CHECK-NOT: Unhandled instruction - pointer escaped
@BufferA.str = internal unnamed_addr constant [8 x i8] c"BufferA\00"
diff --git a/llvm/test/CodeGen/DirectX/ResourceAccess/non-unique-updatecounter.ll b/llvm/test/CodeGen/DirectX/ResourceAccess/non-unique-updatecounter.ll
index d43cbc4efdc97..1dc0932fc6328 100644
--- a/llvm/test/CodeGen/DirectX/ResourceAccess/non-unique-updatecounter.ll
+++ b/llvm/test/CodeGen/DirectX/ResourceAccess/non-unique-updatecounter.ll
@@ -1,9 +1,9 @@
; RUN: not opt -S -dxil-resource-access -mtriple=dxil--shadermodel6.3-library %s 2>&1 | FileCheck %s
+; CHECK: error: Resource access is not guaranteed to map to a unique global resource
; CHECK: note: At resource access: %count = call i32 @llvm.dx.resource.updatecounter.tdx.RawBuffer_i32_1_0t(target("dx.RawBuffer", i32, 1, 0) %handle, i8 1)
; CHECK-DAG: note: Uses resource handle: %handle0 = 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-DAG: note: Uses resource handle: %handle1 = 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: LLVM ERROR: Resource access is not guaranteed to map to a unique global resource
@.str.2 = internal unnamed_addr constant [5 x i8] c"Out0\00", align 1
@.str.4 = internal unnamed_addr constant [5 x i8] c"Out1\00", align 1
diff --git a/llvm/test/CodeGen/DirectX/ResourceAccess/non-unique.ll b/llvm/test/CodeGen/DirectX/ResourceAccess/non-unique.ll
index 644f222a6e770..9201161e49407 100644
--- a/llvm/test/CodeGen/DirectX/ResourceAccess/non-unique.ll
+++ b/llvm/test/CodeGen/DirectX/ResourceAccess/non-unique.ll
@@ -3,10 +3,10 @@
; Test that a error message is generated for illegal resource accesses that do
; not access a unique global resource.
+; CHECK: error: Resource access is not guaranteed to map to a unique global resource
; 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: LLVM ERROR: Resource access is not guaranteed to map to a unique global resource
%__cblayout_c = type <{ i32 }>
More information about the cfe-commits
mailing list