Skip to content

Commit 51483dd

Browse files
committed
implement support for dumping descriptor table clauses
1 parent 6dd49d4 commit 51483dd

File tree

3 files changed

+166
-4
lines changed

3 files changed

+166
-4
lines changed

llvm/include/llvm/Frontend/HLSL/HLSLRootSignature.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ enum class RegisterType { BReg, TReg, UReg, SReg };
5353
struct Register {
5454
RegisterType ViewType;
5555
uint32_t Number;
56+
57+
void dump(raw_ostream &OS) const;
5658
};
5759

5860
// Models the end of a descriptor table and stores its visibility

llvm/lib/Frontend/HLSL/HLSLRootSignature.cpp

Lines changed: 93 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,105 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
#include "llvm/Frontend/HLSL/HLSLRootSignature.h"
14+
#include "llvm/ADT/bit.h"
1415

1516
namespace llvm {
1617
namespace hlsl {
1718
namespace rootsig {
1819

20+
static void dumpRegType(raw_ostream& OS, RegisterType Type) {
21+
switch (Type) {
22+
case RegisterType::BReg:
23+
OS << "b";
24+
break;
25+
case RegisterType::TReg:
26+
OS << "t";
27+
break;
28+
case RegisterType::UReg:
29+
OS << "u";
30+
break;
31+
case RegisterType::SReg:
32+
OS << "s";
33+
break;
34+
}
35+
}
36+
37+
void Register::dump(raw_ostream &OS) const {
38+
dumpRegType(OS, ViewType);
39+
OS << Number;
40+
}
41+
42+
static void dumpClauseType(raw_ostream& OS, ClauseType Type) {
43+
switch (Type) {
44+
case ClauseType::CBuffer:
45+
OS << "CBV";
46+
break;
47+
case ClauseType::SRV:
48+
OS << "SRV";
49+
break;
50+
case ClauseType::UAV:
51+
OS << "UAV";
52+
break;
53+
case ClauseType::Sampler:
54+
OS << "Sampler";
55+
break;
56+
}
57+
}
58+
59+
static void dumpDescriptorRangeFlag(raw_ostream &OS, unsigned Bit) {
60+
switch (static_cast<DescriptorRangeFlags>(Bit)) {
61+
case DescriptorRangeFlags::DescriptorsVolatile:
62+
OS << "DescriptorsVolatile";
63+
break;
64+
case DescriptorRangeFlags::DataVolatile:
65+
OS << "DataVolatile";
66+
break;
67+
case DescriptorRangeFlags::DataStaticWhileSetAtExecute:
68+
OS << "DataStaticWhileSetAtExecute";
69+
break;
70+
case DescriptorRangeFlags::DataStatic:
71+
OS << "DataStatic";
72+
break;
73+
case DescriptorRangeFlags::DescriptorsStaticKeepingBufferBoundsChecks:
74+
OS << "DescriptorsStaticKeepingBufferBoundsChecks";
75+
break;
76+
default:
77+
OS << "invalid: " << Bit;
78+
break;
79+
}
80+
}
81+
82+
static void dumpDescriptorRangeFlags(raw_ostream &OS, DescriptorRangeFlags Flags) {
83+
bool FlagSet = false;
84+
unsigned Remaining = llvm::to_underlying(Flags);
85+
while (Remaining) {
86+
unsigned Bit = 1u << llvm::countr_zero(Remaining);
87+
if (Remaining & Bit) {
88+
if (FlagSet)
89+
OS << " | ";
90+
dumpDescriptorRangeFlag(OS, Bit);
91+
FlagSet = true;
92+
}
93+
Remaining &= ~Bit;
94+
}
95+
if (!FlagSet)
96+
OS << "None";
97+
}
98+
1999
void DescriptorTableClause::dump(raw_ostream &OS) const {
20-
OS << "Clause!";
100+
dumpClauseType(OS, Type);
101+
OS << "(";
102+
Reg.dump(OS);
103+
OS << ", numDescriptors = " << NumDescriptors;
104+
OS << ", space = " << Space;
105+
OS << ", offset = ";
106+
if (Offset == DescriptorTableOffsetAppend)
107+
OS << "DESCRIPTOR_TABLE_OFFSET_APPEND";
108+
else
109+
OS << Offset;
110+
OS << ", flags = ";
111+
dumpDescriptorRangeFlags(OS, Flags);
112+
OS << ")";
21113
}
22114

23115
} // namespace rootsig

llvm/unittests/Frontend/HLSLRootSignatureDumpTest.cpp

Lines changed: 71 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,86 @@ using namespace llvm::hlsl::rootsig;
1313

1414
namespace {
1515

16-
TEST(HLSLRootSignatureTest, DescriptorTablesDump) {
17-
// Default clause
16+
TEST(HLSLRootSignatureTest, DescriptorCBVClauseDump) {
1817
DescriptorTableClause Clause;
1918
Clause.Type = ClauseType::CBuffer;
2019
Clause.Reg = { RegisterType::BReg, 0 };
20+
Clause.setDefaultFlags();
2121

2222
std::string Out;
2323
llvm::raw_string_ostream OS(Out);
2424
Clause.dump(OS);
2525
OS.flush();
2626

27-
EXPECT_EQ(Out, "Clause!");
27+
std::string Expected =
28+
"CBV(b0, numDescriptors = 1, space = 0, "
29+
"offset = DESCRIPTOR_TABLE_OFFSET_APPEND, "
30+
"flags = DataStaticWhileSetAtExecute)";
31+
EXPECT_EQ(Out, Expected);
32+
}
33+
34+
TEST(HLSLRootSignatureTest, DescriptorSRVClauseDump) {
35+
DescriptorTableClause Clause;
36+
Clause.Type = ClauseType::SRV;
37+
Clause.Reg = { RegisterType::TReg, 0 };
38+
Clause.NumDescriptors = 2;
39+
Clause.Space = 42;
40+
Clause.Offset = 3;
41+
Clause.Flags = DescriptorRangeFlags::None;
42+
43+
std::string Out;
44+
llvm::raw_string_ostream OS(Out);
45+
Clause.dump(OS);
46+
OS.flush();
47+
48+
std::string Expected =
49+
"SRV(t0, numDescriptors = 2, space = 42, offset = 3, flags = None)";
50+
EXPECT_EQ(Out, Expected);
51+
}
52+
53+
54+
TEST(HLSLRootSignatureTest, DescriptorUAVClauseDump) {
55+
DescriptorTableClause Clause;
56+
Clause.Type = ClauseType::UAV;
57+
Clause.Reg = { RegisterType::UReg, 0 };
58+
Clause.NumDescriptors = 2;
59+
Clause.Space = 42;
60+
Clause.Offset = 3;
61+
Clause.Flags = DescriptorRangeFlags::ValidFlags;
62+
63+
std::string Out;
64+
llvm::raw_string_ostream OS(Out);
65+
Clause.dump(OS);
66+
OS.flush();
67+
68+
std::string Expected =
69+
"UAV(u0, numDescriptors = 2, space = 42, offset = 3, flags = "
70+
"DescriptorsVolatile | "
71+
"DataVolatile | "
72+
"DataStaticWhileSetAtExecute | "
73+
"DataStatic | "
74+
"DescriptorsStaticKeepingBufferBoundsChecks)";
75+
EXPECT_EQ(Out, Expected);
76+
}
77+
78+
TEST(HLSLRootSignatureTest, DescriptorSamplerClauseDump) {
79+
DescriptorTableClause Clause;
80+
Clause.Type = ClauseType::Sampler;
81+
Clause.Reg = { RegisterType::SReg, 0 };
82+
Clause.NumDescriptors = 2;
83+
Clause.Space = 42;
84+
Clause.Offset = 3;
85+
Clause.Flags = DescriptorRangeFlags::ValidSamplerFlags;
86+
87+
std::string Out;
88+
llvm::raw_string_ostream OS(Out);
89+
Clause.dump(OS);
90+
OS.flush();
91+
92+
std::string Expected =
93+
"Sampler(s0, numDescriptors = 2, space = 42, offset = 3, "
94+
"flags = DescriptorsVolatile)";
95+
EXPECT_EQ(Out, Expected);
2896
}
2997

3098
} // namespace

0 commit comments

Comments
 (0)