Skip to content

Commit 861820a

Browse files
joaosaffranaokblast
authored andcommitted
[HLSL] Adding DXIL Storage type into TypedInfo (llvm#164887)
In DXIL, some 64bit types are actually represented with their 32bit counterpart. This was already being address in the codegen, however the metadata generation was lacking this information. This PR is fixing this issue. Closes: [llvm#146735](llvm#146735)
1 parent a5f1e21 commit 861820a

File tree

6 files changed

+54
-23
lines changed

6 files changed

+54
-23
lines changed

llvm/include/llvm/Analysis/DXILResource.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,7 @@ class ResourceTypeInfo {
293293

294294
struct TypedInfo {
295295
dxil::ElementType ElementTy;
296+
dxil::ElementType DXILStorageTy;
296297
uint32_t ElementCount;
297298

298299
bool operator==(const TypedInfo &RHS) const {

llvm/lib/Analysis/DXILResource.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,14 @@ static dxil::ElementType toDXILElementType(Type *Ty, bool IsSigned) {
206206
return ElementType::Invalid;
207207
}
208208

209+
static dxil::ElementType toDXILStorageType(dxil::ElementType ET) {
210+
if (ET == dxil::ElementType::U64 || ET == dxil::ElementType::F64 ||
211+
ET == dxil::ElementType::I64 || ET == dxil::ElementType::SNormF64 ||
212+
ET == dxil::ElementType::UNormF64)
213+
return dxil::ElementType::U32;
214+
return ET;
215+
}
216+
209217
ResourceTypeInfo::ResourceTypeInfo(TargetExtType *HandleTy,
210218
const dxil::ResourceClass RC_,
211219
const dxil::ResourceKind Kind_)
@@ -569,10 +577,11 @@ ResourceTypeInfo::TypedInfo ResourceTypeInfo::getTyped() const {
569577

570578
auto [ElTy, IsSigned] = getTypedElementType(Kind, HandleTy);
571579
dxil::ElementType ET = toDXILElementType(ElTy, IsSigned);
580+
dxil::ElementType DXILStorageTy = toDXILStorageType(ET);
572581
uint32_t Count = 1;
573582
if (auto *VTy = dyn_cast<FixedVectorType>(ElTy))
574583
Count = VTy->getNumElements();
575-
return {ET, Count};
584+
return {ET, DXILStorageTy, Count};
576585
}
577586

578587
dxil::SamplerFeedbackType ResourceTypeInfo::getFeedbackType() const {
@@ -636,7 +645,10 @@ void ResourceTypeInfo::print(raw_ostream &OS, const DataLayout &DL) const {
636645
OS << " Alignment: " << Struct.AlignLog2 << "\n";
637646
} else if (isTyped()) {
638647
TypedInfo Typed = getTyped();
639-
OS << " Element Type: " << getElementTypeName(Typed.ElementTy) << "\n"
648+
OS << " Element Type: " << getElementTypeName(Typed.ElementTy);
649+
if (Typed.ElementTy != Typed.DXILStorageTy)
650+
OS << " (stored as " << getElementTypeName(Typed.DXILStorageTy) << ")";
651+
OS << "\n"
640652
<< " Element Count: " << Typed.ElementCount << "\n";
641653
} else if (isFeedback())
642654
OS << " Feedback Type: " << getSamplerFeedbackTypeName(getFeedbackType())
@@ -714,7 +726,8 @@ MDTuple *ResourceInfo::getAsMetadata(Module &M,
714726
Tags.push_back(getIntMD(RTI.getStruct(DL).Stride));
715727
} else if (RTI.isTyped()) {
716728
Tags.push_back(getIntMD(llvm::to_underlying(ExtPropTags::ElementType)));
717-
Tags.push_back(getIntMD(llvm::to_underlying(RTI.getTyped().ElementTy)));
729+
Tags.push_back(
730+
getIntMD(llvm::to_underlying(RTI.getTyped().DXILStorageTy)));
718731
} else if (RTI.isFeedback()) {
719732
Tags.push_back(
720733
getIntMD(llvm::to_underlying(ExtPropTags::SamplerFeedbackKind)));

llvm/lib/Target/DirectX/DXILPrettyPrinter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ static StringRef getRCPrefix(dxil::ResourceClass RC) {
4949

5050
static StringRef getFormatName(const dxil::ResourceTypeInfo &RI) {
5151
if (RI.isTyped()) {
52-
switch (RI.getTyped().ElementTy) {
52+
switch (RI.getTyped().DXILStorageTy) {
5353
case dxil::ElementType::I1:
5454
return "i1";
5555
case dxil::ElementType::I16:

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
@Four.str = private unnamed_addr constant [5 x i8] c"Four\00", align 1
1010
@Array.str = private unnamed_addr constant [6 x i8] c"Array\00", align 1
1111
@Five.str = private unnamed_addr constant [5 x i8] c"Five\00", align 1
12+
@Six.str = private unnamed_addr constant [4 x i8] c"Six\00", align 1
1213
@CB.str = private unnamed_addr constant [3 x i8] c"CB\00", align 1
1314
@Constants.str = private unnamed_addr constant [10 x i8] c"Constants\00", align 1
1415

@@ -137,6 +138,23 @@ define void @test_typedbuffer() {
137138
; CHECK: Element Type: f32
138139
; CHECK: Element Count: 4
139140

141+
%uav4 = call target("dx.TypedBuffer", double, 1, 0, 0)
142+
@llvm.dx.resource.handlefrombinding(i32 5, i32 0, i32 1, i32 0, ptr @Six.str)
143+
; CHECK: Resource [[UAV4:[0-9]+]]:
144+
; CHECK: Name: Six
145+
; CHECK: Binding:
146+
; CHECK: Record ID: 4
147+
; CHECK: Space: 5
148+
; CHECK: Lower Bound: 0
149+
; CHECK: Size: 1
150+
; CHECK: Globally Coherent: 0
151+
; CHECK: Counter Direction: Unknown
152+
; CHECK: Class: UAV
153+
; CHECK: Kind: Buffer
154+
; CHECK: IsROV: 0
155+
; CHECK: Element Type: f64 (stored as u32)
156+
; CHECK: Element Count: 1
157+
140158
%cb0 = call target("dx.CBuffer", {float})
141159
@llvm.dx.resource.handlefrombinding(i32 1, i32 0, i32 1, i32 0, ptr @CB.str)
142160
; CHECK: Resource [[CB0:[0-9]+]]:
@@ -175,6 +193,7 @@ define void @test_typedbuffer() {
175193
; CHECK-DAG: Call bound to [[UAV1]]: %uav1 =
176194
; CHECK-DAG: Call bound to [[UAV2]]: %uav2_1 =
177195
; CHECK-DAG: Call bound to [[UAV2]]: %uav2_2 =
196+
; CHECK-DAG: Call bound to [[UAV4]]: %uav4 =
178197
; CHECK-DAG: Call bound to [[CB0]]: %cb0 =
179198
; CHECK-DAG: Call bound to [[CB1]]: %cb1 =
180199

llvm/test/CodeGen/DirectX/Metadata/srv_metadata.ll

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ target triple = "dxil-pc-shadermodel6.6-compute"
2222
; PRINT-NEXT:; ------------------------------ ---------- ------- ----------- ------- -------------- ---------
2323
; PRINT-NEXT:; Zero texture f16 buf T0 t0 1
2424
; PRINT-NEXT:; One texture f32 buf T1 t1 1
25-
; PRINT-NEXT:; Two texture f64 buf T2 t2 1
25+
; PRINT-NEXT:; Two texture u32 buf T2 t2 1
2626
; PRINT-NEXT:; Three texture i32 buf T3 t3 1
2727
; PRINT-NEXT:; Four texture byte r/o T4 t5 1
2828
; PRINT-NEXT:; Five texture struct r/o T5 t6 1
29-
; PRINT-NEXT:; Six texture u64 buf T6 t10,space2 1
29+
; PRINT-NEXT:; Six texture u32 buf T6 t10,space2 1
3030
; PRINT-NEXT:; Array texture f32 buf T7 t4,space3 100
31-
; PRINT-NEXT:; Array2 texture f64 buf T8 t2,space4 unbounded
32-
; PRINT-NEXT:; Seven texture u64 buf T9 t20,space5 1
31+
; PRINT-NEXT:; Array2 texture u32 buf T8 t2,space4 unbounded
32+
; PRINT-NEXT:; Seven texture u32 buf T9 t20,space5 1
3333
;
3434

3535
define void @test() #0 {
@@ -120,15 +120,14 @@ attributes #0 = { noinline nounwind "hlsl.shader"="compute" }
120120
; CHECK: ![[Half]] = !{i32 0, i32 8}
121121
; CHECK: ![[One]] = !{i32 1, ptr @One, !"One", i32 0, i32 1, i32 1, i32 10, i32 0, ![[Float:[0-9]+]]}
122122
; CHECK: ![[Float]] = !{i32 0, i32 9}
123-
; CHECK: ![[Two]] = !{i32 2, ptr @Two, !"Two", i32 0, i32 2, i32 1, i32 10, i32 0, ![[Double:[0-9]+]]}
124-
; CHECK: ![[Double]] = !{i32 0, i32 10}
123+
; CHECK: ![[Two]] = !{i32 2, ptr @Two, !"Two", i32 0, i32 2, i32 1, i32 10, i32 0, ![[U32:[0-9]+]]}
124+
; CHECK: ![[U32]] = !{i32 0, i32 5}
125125
; CHECK: ![[Three]] = !{i32 3, ptr @Three, !"Three", i32 0, i32 3, i32 1, i32 10, i32 0, ![[I32:[0-9]+]]}
126126
; CHECK: ![[I32]] = !{i32 0, i32 4}
127127
; CHECK: ![[Four]] = !{i32 4, ptr @Four, !"Four", i32 0, i32 5, i32 1, i32 11, i32 0, null}
128128
; CHECK: ![[Five]] = !{i32 5, ptr @Five, !"Five", i32 0, i32 6, i32 1, i32 12, i32 0, ![[FiveStride:[0-9]+]]}
129129
; CHECK: ![[FiveStride]] = !{i32 1, i32 2}
130-
; CHECK: ![[Six]] = !{i32 6, ptr @Six, !"Six", i32 2, i32 10, i32 1, i32 10, i32 0, ![[U64:[0-9]+]]}
131-
; CHECK: ![[U64]] = !{i32 0, i32 7}
130+
; CHECK: ![[Six]] = !{i32 6, ptr @Six, !"Six", i32 2, i32 10, i32 1, i32 10, i32 0, ![[U32:[0-9]+]]}
132131
; CHECK: ![[Array]] = !{i32 7, ptr @Array, !"Array", i32 3, i32 4, i32 100, i32 10, i32 0, ![[Float]]}
133-
; CHECK: ![[Array2]] = !{i32 8, ptr @Array2, !"Array2", i32 4, i32 2, i32 -1, i32 10, i32 0, ![[Double]]}
134-
; CHECK: ![[Seven]] = !{i32 9, ptr @Seven, !"Seven", i32 5, i32 20, i32 1, i32 10, i32 0, ![[U64]]}
132+
; CHECK: ![[Array2]] = !{i32 8, ptr @Array2, !"Array2", i32 4, i32 2, i32 -1, i32 10, i32 0, ![[U32]]}
133+
; CHECK: ![[Seven]] = !{i32 9, ptr @Seven, !"Seven", i32 5, i32 20, i32 1, i32 10, i32 0, ![[U32]]}

llvm/test/CodeGen/DirectX/Metadata/uav_metadata.ll

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,17 @@ target triple = "dxil-pc-shadermodel6.6-compute"
2525
; PRINT-NEXT:; ------------------------------ ---------- ------- ----------- ------- -------------- ---------
2626
; PRINT-NEXT:; Zero UAV f16 buf U0 u0 1
2727
; PRINT-NEXT:; One UAV f32 buf U1 u1 1
28-
; PRINT-NEXT:; Two UAV f64 buf U2 u2 1
28+
; PRINT-NEXT:; Two UAV u32 buf U2 u2 1
2929
; PRINT-NEXT:; Three UAV i32 buf U3 u3 1
3030
; PRINT-NEXT:; Four UAV byte r/w U4 u5 1
3131
; PRINT-NEXT:; Five UAV struct r/w U5 u6 1
3232
; PRINT-NEXT:; Six UAV i32 buf U6 u7 1
3333
; PRINT-NEXT:; Seven UAV struct r/w U7 u8 1
3434
; PRINT-NEXT:; Eight UAV byte r/w U8 u9 1
35-
; PRINT-NEXT:; Nine UAV u64 buf U9 u10,space2 1
35+
; PRINT-NEXT:; Nine UAV u32 buf U9 u10,space2 1
3636
; PRINT-NEXT:; Array UAV f32 buf U10 u4,space3 100
37-
; PRINT-NEXT:; Array2 UAV f64 buf U11 u2,space4 unbounded
38-
; PRINT-NEXT:; Ten UAV u64 buf U12 u22,space5 1
37+
; PRINT-NEXT:; Array2 UAV u32 buf U11 u2,space4 unbounded
38+
; PRINT-NEXT:; Ten UAV u32 buf U12 u22,space5 1
3939

4040
define void @test() #0 {
4141
; RWBuffer<half4> Zero : register(u0)
@@ -144,8 +144,8 @@ attributes #0 = { noinline nounwind "hlsl.shader"="compute" }
144144
; CHECK: ![[Half]] = !{i32 0, i32 8}
145145
; CHECK: ![[One]] = !{i32 1, ptr @One, !"One", i32 0, i32 1, i32 1, i32 10, i1 false, i1 false, i1 false, ![[Float:[0-9]+]]}
146146
; CHECK: ![[Float]] = !{i32 0, i32 9}
147-
; CHECK: ![[Two]] = !{i32 2, ptr @Two, !"Two", i32 0, i32 2, i32 1, i32 10, i1 false, i1 false, i1 false, ![[Double:[0-9]+]]}
148-
; CHECK: ![[Double]] = !{i32 0, i32 10}
147+
; CHECK: ![[Two]] = !{i32 2, ptr @Two, !"Two", i32 0, i32 2, i32 1, i32 10, i1 false, i1 false, i1 false, ![[U32:[0-9]+]]}
148+
; CHECK: ![[U32]] = !{i32 0, i32 5}
149149
; CHECK: ![[Three]] = !{i32 3, ptr @Three, !"Three", i32 0, i32 3, i32 1, i32 10, i1 false, i1 false, i1 false, ![[I32:[0-9]+]]}
150150
; CHECK: ![[I32]] = !{i32 0, i32 4}
151151
; CHECK: ![[Four]] = !{i32 4, ptr @Four, !"Four", i32 0, i32 5, i32 1, i32 11, i1 false, i1 false, i1 false, null}
@@ -155,8 +155,7 @@ attributes #0 = { noinline nounwind "hlsl.shader"="compute" }
155155
; CHECK: ![[Seven]] = !{i32 7, ptr @Seven, !"Seven", i32 0, i32 8, i32 1, i32 12, i1 false, i1 false, i1 true, ![[SevenStride:[0-9]+]]}
156156
; CHECK: ![[SevenStride]] = !{i32 1, i32 16}
157157
; CHECK: ![[Eight]] = !{i32 8, ptr @Eight, !"Eight", i32 0, i32 9, i32 1, i32 11, i1 false, i1 false, i1 true, null}
158-
; CHECK: ![[Nine]] = !{i32 9, ptr @Nine, !"Nine", i32 2, i32 10, i32 1, i32 10, i1 false, i1 false, i1 false, ![[U64:[0-9]+]]}
159-
; CHECK: ![[U64]] = !{i32 0, i32 7}
158+
; CHECK: ![[Nine]] = !{i32 9, ptr @Nine, !"Nine", i32 2, i32 10, i32 1, i32 10, i1 false, i1 false, i1 false, ![[U32]]}
160159
; CHECK: ![[Array]] = !{i32 10, ptr @Array, !"Array", i32 3, i32 4, i32 100, i32 10, i1 false, i1 false, i1 false, ![[Float]]}
161-
; CHECK: ![[Array2]] = !{i32 11, ptr @Array2, !"Array2", i32 4, i32 2, i32 -1, i32 10, i1 false, i1 false, i1 false, ![[Double]]}
160+
; CHECK: ![[Array2]] = !{i32 11, ptr @Array2, !"Array2", i32 4, i32 2, i32 -1, i32 10, i1 false, i1 false, i1 false, ![[U32]]}
162161
; CHECK: ![[Ten]] = !{i32 12, ptr @Ten, !"Ten", i32 5, i32 22, i32 1, i32 10, i1 false, i1 false, i1 false, ![[U64:[0-9]+]]}

0 commit comments

Comments
 (0)