https://github.com/danbrown-amd updated https://github.com/llvm/llvm-project/pull/206396
>From 9a4b4e4026041962890f002e47cf5f61084cb7a7 Mon Sep 17 00:00:00 2001 From: danbrown-amd <[email protected]> Date: Thu, 12 Nov 2026 22:04:46 -0600 Subject: [PATCH 1/2] [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 00000000000000..1de30d5ea35e6a --- /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 c1923caa5e283f..b2f3f2323379ad 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 fa5b7b8817fe32..0c051254d6a2b9 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 54b807e1e0d11e..ebcfe0f4faf708 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 ce0fce38c96fe8..4ebf334f2d8e56 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 d43cbc4efdc97f..1dc0932fc6328a 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 644f222a6e770f..9201161e49407f 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 }> >From 310dfdcd9356de5d00f629594402f52f62268285 Mon Sep 17 00:00:00 2001 From: danbrown-amd <[email protected]> Date: Mon, 16 Nov 2026 16:49:21 -0600 Subject: [PATCH 2/2] Consistently Use range size 0 for unbounded resource arrays Commit a12b612c0db0a049ca9050a43b1e03ca36c65695 changed the representation, but some places still used UINT32_MAX, leading to an assertion failure. Assisted-by: Claude Sonnet 5 --- llvm/lib/Target/DirectX/DXILResourceAccess.cpp | 5 ++++- llvm/lib/Target/DirectX/DXILResourceImplicitBinding.cpp | 4 ++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/llvm/lib/Target/DirectX/DXILResourceAccess.cpp b/llvm/lib/Target/DirectX/DXILResourceAccess.cpp index b2f3f2323379ad..277700f0efd21d 100644 --- a/llvm/lib/Target/DirectX/DXILResourceAccess.cpp +++ b/llvm/lib/Target/DirectX/DXILResourceAccess.cpp @@ -1108,7 +1108,7 @@ static hlsl::Binding getHandleIntrinsicBinding(IntrinsicInst *Handle, uint32_t LowerBound = cast<ConstantInt>(Handle->getArgOperand(1))->getZExtValue(); uint32_t Size = cast<ConstantInt>(Handle->getArgOperand(2))->getZExtValue(); - uint32_t UpperBound = Size == UINT32_MAX ? UINT32_MAX : LowerBound + Size - 1; + uint32_t UpperBound = Size == 0 ? UINT32_MAX : LowerBound + Size - 1; return hlsl::Binding(Class, Space, LowerBound, UpperBound, nullptr); } @@ -1417,6 +1417,9 @@ static bool legalizeResourceHandles(Function &F, DXILResourceTypeMap &DRTM) { for (auto *I : llvm::reverse(DeadInsts)) if (I->hasNUses(0)) { // Handle can still be used outside of replaced path + // Debug users of a resource handle would be left as poison of a target + // extension type, which can't be emitted in DXIL. + dropDebugUsers(*I); I->eraseFromParent(); MadeChanges = true; } diff --git a/llvm/lib/Target/DirectX/DXILResourceImplicitBinding.cpp b/llvm/lib/Target/DirectX/DXILResourceImplicitBinding.cpp index 1af01a366d30f9..eb76adad6739ec 100644 --- a/llvm/lib/Target/DirectX/DXILResourceImplicitBinding.cpp +++ b/llvm/lib/Target/DirectX/DXILResourceImplicitBinding.cpp @@ -90,6 +90,10 @@ static bool assignBindings(Module &M, DXILResourceBindingInfo &DRBI, cast<ConstantInt>(IB.Call->getArgOperand(1))->getZExtValue(); int32_t Size = cast<ConstantInt>(IB.Call->getArgOperand(2))->getZExtValue(); + // A range size of 0 denotes an unbounded array, which + // findAvailableBinding expects as -1. + if (Size == 0) + Size = -1; std::optional<uint32_t> RegSlot = DRBI.findAvailableBinding(RTI.getResourceClass(), Space, Size); _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
