From 9a77087836ec47abed259c2bd1027041cdcb3754 Mon Sep 17 00:00:00 2001 From: Justin Bogner Date: Tue, 21 Oct 2025 10:23:44 -0700 Subject: [PATCH 1/2] [HLSL] Allow completely unused cbuffers We were checking for cbuffers where the global was removed, but if the buffer is completely unused the whole thing can be null. --- llvm/lib/Frontend/HLSL/CBuffer.cpp | 9 +++++++-- llvm/test/CodeGen/DirectX/CBufferAccess/unused.ll | 13 +++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 llvm/test/CodeGen/DirectX/CBufferAccess/unused.ll diff --git a/llvm/lib/Frontend/HLSL/CBuffer.cpp b/llvm/lib/Frontend/HLSL/CBuffer.cpp index 407b6ad6d5a7e..1e4a1a5020b0b 100644 --- a/llvm/lib/Frontend/HLSL/CBuffer.cpp +++ b/llvm/lib/Frontend/HLSL/CBuffer.cpp @@ -43,8 +43,13 @@ std::optional CBufferMetadata::get(Module &M) { for (const MDNode *MD : CBufMD->operands()) { assert(MD->getNumOperands() && "Invalid cbuffer metadata"); - auto *Handle = cast( - cast(MD->getOperand(0))->getValue()); + // For an unused cbuffer, the handle may have been optimizzd out + Metadata *OpMD = MD->getOperand(0); + if (!OpMD) + continue; + + auto *Handle = + cast(cast(OpMD)->getValue()); CBufferMapping &Mapping = Result->Mappings.emplace_back(Handle); for (int I = 1, E = MD->getNumOperands(); I < E; ++I) { diff --git a/llvm/test/CodeGen/DirectX/CBufferAccess/unused.ll b/llvm/test/CodeGen/DirectX/CBufferAccess/unused.ll new file mode 100644 index 0000000000000..8c0d82e43b4b1 --- /dev/null +++ b/llvm/test/CodeGen/DirectX/CBufferAccess/unused.ll @@ -0,0 +1,13 @@ +; RUN: opt -S -dxil-cbuffer-access -mtriple=dxil--shadermodel6.3-library %s | FileCheck %s +; Check that we correctly ignore cbuffers that were nulled out by optimizations. + +%__cblayout_CB = type <{ float }> +@CB.cb = local_unnamed_addr global target("dx.CBuffer", %__cblayout_CB) poison +@x = external local_unnamed_addr addrspace(2) global float, align 4 + +; CHECK-NOT: !hlsl.cbs = +!hlsl.cbs = !{!0, !1, !2} + +!0 = !{ptr @CB.cb, ptr addrspace(2) @x} +!1 = !{ptr @CB.cb, null} +!2 = !{null, null} From cca4a31a1966b9929c9c4f458b69d498c0fe2b30 Mon Sep 17 00:00:00 2001 From: Justin Bogner Date: Wed, 22 Oct 2025 13:22:04 -0700 Subject: [PATCH 2/2] Update llvm/lib/Frontend/HLSL/CBuffer.cpp Co-authored-by: Helena Kotas --- llvm/lib/Frontend/HLSL/CBuffer.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/llvm/lib/Frontend/HLSL/CBuffer.cpp b/llvm/lib/Frontend/HLSL/CBuffer.cpp index 1e4a1a5020b0b..1f53c87bb1683 100644 --- a/llvm/lib/Frontend/HLSL/CBuffer.cpp +++ b/llvm/lib/Frontend/HLSL/CBuffer.cpp @@ -43,7 +43,7 @@ std::optional CBufferMetadata::get(Module &M) { for (const MDNode *MD : CBufMD->operands()) { assert(MD->getNumOperands() && "Invalid cbuffer metadata"); - // For an unused cbuffer, the handle may have been optimizzd out + // For an unused cbuffer, the handle may have been optimized out Metadata *OpMD = MD->getOperand(0); if (!OpMD) continue;