diff --git a/llvm/lib/Analysis/DXILResource.cpp b/llvm/lib/Analysis/DXILResource.cpp index 4ffc9dbebda8d..2f75b285356a3 100644 --- a/llvm/lib/Analysis/DXILResource.cpp +++ b/llvm/lib/Analysis/DXILResource.cpp @@ -749,6 +749,11 @@ void DXILBindingMap::populate(Module &M, DXILResourceTypeMap &DRTM) { NextID = 0; } + // We need to make sure the types of resource are ordered even if some are + // missing. + FirstCBuffer = std::min({FirstCBuffer, FirstSampler}); + FirstUAV = std::min({FirstUAV, FirstCBuffer}); + // Adjust the resource binding to use the next ID. RBI.setBindingID(NextID++); } diff --git a/llvm/test/CodeGen/DirectX/Metadata/cbuffer-only.ll b/llvm/test/CodeGen/DirectX/Metadata/cbuffer-only.ll new file mode 100644 index 0000000000000..c07e8d9a2b591 --- /dev/null +++ b/llvm/test/CodeGen/DirectX/Metadata/cbuffer-only.ll @@ -0,0 +1,19 @@ +; Regression test for https://github.com/llvm/llvm-project/issues/128560 - +; check that cbuffers are populated correctly when there aren't any other kinds +; of resource. + +; RUN: opt -S -passes=dxil-translate-metadata %s | FileCheck %s + +target triple = "dxil-pc-shadermodel6.6-compute" + +define void @cbuffer_is_only_binding() { + %cbuf = call target("dx.CBuffer", target("dx.Layout", {float}, 4, 0)) + @llvm.dx.resource.handlefrombinding(i32 1, i32 8, i32 1, i32 0, i1 false) + ; CHECK: %cbuffer = type + + ret void +} + +; CHECK: @[[CB0:.*]] = external constant %cbuffer + +; CHECK: !{i32 0, ptr @[[CB0]], !""