Skip to content

Commit 11a24d6

Browse files
bognerhekota
andauthored
[HLSL] Allow completely unused cbuffers (#164557)
We were checking for cbuffers where the global was removed, but if the buffer is completely unused the whole thing can be null. --------- Co-authored-by: Helena Kotas <[email protected]>
1 parent 152f3e7 commit 11a24d6

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

llvm/lib/Frontend/HLSL/CBuffer.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,13 @@ std::optional<CBufferMetadata> CBufferMetadata::get(Module &M) {
4343
for (const MDNode *MD : CBufMD->operands()) {
4444
assert(MD->getNumOperands() && "Invalid cbuffer metadata");
4545

46-
auto *Handle = cast<GlobalVariable>(
47-
cast<ValueAsMetadata>(MD->getOperand(0))->getValue());
46+
// For an unused cbuffer, the handle may have been optimized out
47+
Metadata *OpMD = MD->getOperand(0);
48+
if (!OpMD)
49+
continue;
50+
51+
auto *Handle =
52+
cast<GlobalVariable>(cast<ValueAsMetadata>(OpMD)->getValue());
4853
CBufferMapping &Mapping = Result->Mappings.emplace_back(Handle);
4954

5055
for (int I = 1, E = MD->getNumOperands(); I < E; ++I) {
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
; RUN: opt -S -dxil-cbuffer-access -mtriple=dxil--shadermodel6.3-library %s | FileCheck %s
2+
; Check that we correctly ignore cbuffers that were nulled out by optimizations.
3+
4+
%__cblayout_CB = type <{ float }>
5+
@CB.cb = local_unnamed_addr global target("dx.CBuffer", %__cblayout_CB) poison
6+
@x = external local_unnamed_addr addrspace(2) global float, align 4
7+
8+
; CHECK-NOT: !hlsl.cbs =
9+
!hlsl.cbs = !{!0, !1, !2}
10+
11+
!0 = !{ptr @CB.cb, ptr addrspace(2) @x}
12+
!1 = !{ptr @CB.cb, null}
13+
!2 = !{null, null}

0 commit comments

Comments
 (0)