Skip to content

Commit

Permalink
[DXIL][Analysis] Make sure resource accessors are contiguous
Browse files Browse the repository at this point in the history
When some resource types were present, but not all of them, we were
ending up in a situation where we would fail to initialize the `FirstX`
variables and get incorrect iterators.

Fixes llvm#128560.
  • Loading branch information
bogner committed Feb 25, 2025
1 parent f404047 commit 499bf9b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
5 changes: 5 additions & 0 deletions llvm/lib/Analysis/DXILResource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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++);
}
Expand Down
12 changes: 12 additions & 0 deletions llvm/test/Analysis/DXILResource/buffer-frombinding.ll
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,17 @@ define void @test_typedbuffer() {
; CHECK: Element Type: f32
; CHECK: Element Count: 4

%cb0 = 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: Binding [[CB0:[0-9]+]]:
; CHECK: Binding:
; CHECK: Record ID: 0
; CHECK: Space: 1
; CHECK: Lower Bound: 8
; CHECK: Size: 1
; CHECK: Class: CBuffer
; CHECK: Kind: CBuffer

; CHECK-NOT: Binding {{[0-9]+}}:

ret void
Expand All @@ -118,5 +129,6 @@ define void @test_typedbuffer() {
; CHECK-DAG: Call bound to [[UAV1]]: %uav1 =
; CHECK-DAG: Call bound to [[UAV2]]: %uav2_1 =
; CHECK-DAG: Call bound to [[UAV2]]: %uav2_2 =
; CHECK-DAG: Call bound to [[CB0]]: %cb0 =

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(none) }
19 changes: 19 additions & 0 deletions llvm/test/CodeGen/DirectX/Metadata/cbuffer-only.ll
Original file line number Diff line number Diff line change
@@ -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]], !""

0 comments on commit 499bf9b

Please sign in to comment.