Skip to content

Commit e19863e

Browse files
bognerjph-13
authored andcommitted
[DirectX] Update CBuffer to refer to a dx.Layout type (llvm#128697)
This adds support cbuffers based on llvm/wg-hlsl#171 - the type argument of the CBuffer TargetExtType is either a `dx.Layout` type which reports its own size, or it's a normal type and we can simply refer to DataLayout.
1 parent 1f10887 commit e19863e

File tree

3 files changed

+55
-2
lines changed

3 files changed

+55
-2
lines changed

llvm/include/llvm/Analysis/DXILResource.h

+21-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,6 @@ class CBufferExtType : public TargetExtType {
166166
CBufferExtType &operator=(const CBufferExtType &) = delete;
167167

168168
Type *getResourceType() const { return getTypeParameter(0); }
169-
uint32_t getCBufferSize() const { return getIntParameter(0); }
170169

171170
static bool classof(const TargetExtType *T) {
172171
return T->getName() == "dx.CBuffer";
@@ -197,6 +196,27 @@ class SamplerExtType : public TargetExtType {
197196
}
198197
};
199198

199+
/// The dx.Layout target extension type
200+
///
201+
/// `target("dx.Layout", <Type>, <size>, [offsets...])`
202+
class LayoutExtType : public TargetExtType {
203+
public:
204+
LayoutExtType() = delete;
205+
LayoutExtType(const LayoutExtType &) = delete;
206+
LayoutExtType &operator=(const LayoutExtType &) = delete;
207+
208+
Type *getWrappedType() const { return getTypeParameter(0); }
209+
uint32_t getSize() const { return getIntParameter(0); }
210+
uint32_t getOffsetOfElement(int I) const { return getIntParameter(I + 1); }
211+
212+
static bool classof(const TargetExtType *T) {
213+
return T->getName() == "dx.Layout";
214+
}
215+
static bool classof(const Type *T) {
216+
return isa<TargetExtType>(T) && classof(cast<TargetExtType>(T));
217+
}
218+
};
219+
200220
//===----------------------------------------------------------------------===//
201221

202222
class ResourceTypeInfo {

llvm/lib/Analysis/DXILResource.cpp

+8-1
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,14 @@ ResourceTypeInfo::UAVInfo ResourceTypeInfo::getUAV() const {
382382

383383
uint32_t ResourceTypeInfo::getCBufferSize(const DataLayout &DL) const {
384384
assert(isCBuffer() && "Not a CBuffer");
385-
return cast<CBufferExtType>(HandleTy)->getCBufferSize();
385+
386+
Type *ElTy = cast<CBufferExtType>(HandleTy)->getResourceType();
387+
388+
if (auto *LayoutTy = dyn_cast<LayoutExtType>(ElTy))
389+
return LayoutTy->getSize();
390+
391+
// TODO: What should we do with unannotated arrays?
392+
return DL.getTypeAllocSize(ElTy);
386393
}
387394

388395
dxil::SamplerType ResourceTypeInfo::getSamplerType() const {

llvm/test/Analysis/DXILResource/buffer-frombinding.ll

+26
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,30 @@ define void @test_typedbuffer() {
106106
; CHECK: Element Type: f32
107107
; CHECK: Element Count: 4
108108

109+
%cb0 = call target("dx.CBuffer", {float})
110+
@llvm.dx.resource.handlefrombinding(i32 1, i32 0, i32 1, i32 0, i1 false)
111+
; CHECK: Binding [[CB0:[0-9]+]]:
112+
; CHECK: Binding:
113+
; CHECK: Record ID: 0
114+
; CHECK: Space: 1
115+
; CHECK: Lower Bound: 0
116+
; CHECK: Size: 1
117+
; CHECK: Class: CBuffer
118+
; CHECK: Kind: CBuffer
119+
; CHECK: CBuffer size: 4
120+
121+
%cb1 = call target("dx.CBuffer", target("dx.Layout", {float}, 4, 0))
122+
@llvm.dx.resource.handlefrombinding(i32 1, i32 8, i32 1, i32 0, i1 false)
123+
; CHECK: Binding [[CB1:[0-9]+]]:
124+
; CHECK: Binding:
125+
; CHECK: Record ID: 1
126+
; CHECK: Space: 1
127+
; CHECK: Lower Bound: 8
128+
; CHECK: Size: 1
129+
; CHECK: Class: CBuffer
130+
; CHECK: Kind: CBuffer
131+
; CHECK: CBuffer size: 4
132+
109133
; CHECK-NOT: Binding {{[0-9]+}}:
110134

111135
ret void
@@ -118,5 +142,7 @@ define void @test_typedbuffer() {
118142
; CHECK-DAG: Call bound to [[UAV1]]: %uav1 =
119143
; CHECK-DAG: Call bound to [[UAV2]]: %uav2_1 =
120144
; CHECK-DAG: Call bound to [[UAV2]]: %uav2_2 =
145+
; CHECK-DAG: Call bound to [[CB0]]: %cb0 =
146+
; CHECK-DAG: Call bound to [[CB1]]: %cb1 =
121147

122148
attributes #0 = { nocallback nofree nosync nounwind willreturn memory(none) }

0 commit comments

Comments
 (0)