Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions llvm/lib/Frontend/HLSL/CBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,13 @@ std::optional<CBufferMetadata> CBufferMetadata::get(Module &M) {
for (const MDNode *MD : CBufMD->operands()) {
assert(MD->getNumOperands() && "Invalid cbuffer metadata");

auto *Handle = cast<GlobalVariable>(
cast<ValueAsMetadata>(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<GlobalVariable>(cast<ValueAsMetadata>(OpMD)->getValue());
CBufferMapping &Mapping = Result->Mappings.emplace_back(Handle);

for (int I = 1, E = MD->getNumOperands(); I < E; ++I) {
Expand Down
13 changes: 13 additions & 0 deletions llvm/test/CodeGen/DirectX/CBufferAccess/unused.ll
Original file line number Diff line number Diff line change
@@ -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}