Skip to content

Commit 749c762

Browse files
committed
[HLSL][RootSignature] Implement serialization of RootDescriptors
1 parent 21d74cc commit 749c762

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed

llvm/include/llvm/Frontend/HLSL/HLSLRootSignatureUtils.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ LLVM_ABI raw_ostream &operator<<(raw_ostream &OS, const RootFlags &Flags);
3232
LLVM_ABI raw_ostream &operator<<(raw_ostream &OS,
3333
const RootConstants &Constants);
3434

35+
LLVM_ABI raw_ostream &operator<<(raw_ostream &OS,
36+
const RootDescriptor &Descriptor);
37+
3538
LLVM_ABI raw_ostream &operator<<(raw_ostream &OS,
3639
const DescriptorTableClause &Clause);
3740

llvm/lib/Frontend/HLSL/HLSLRootSignatureUtils.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,42 @@ static raw_ostream &operator<<(raw_ostream &OS,
132132
return OS;
133133
}
134134

135+
static raw_ostream &operator<<(raw_ostream &OS,
136+
const RootDescriptorFlags &Flags) {
137+
bool FlagSet = false;
138+
unsigned Remaining = llvm::to_underlying(Flags);
139+
while (Remaining) {
140+
unsigned Bit = 1u << llvm::countr_zero(Remaining);
141+
if (Remaining & Bit) {
142+
if (FlagSet)
143+
OS << " | ";
144+
145+
switch (static_cast<RootDescriptorFlags>(Bit)) {
146+
case RootDescriptorFlags::DataVolatile:
147+
OS << "DataVolatile";
148+
break;
149+
case RootDescriptorFlags::DataStaticWhileSetAtExecute:
150+
OS << "DataStaticWhileSetAtExecute";
151+
break;
152+
case RootDescriptorFlags::DataStatic:
153+
OS << "DataStatic";
154+
break;
155+
default:
156+
OS << "invalid: " << Bit;
157+
break;
158+
}
159+
160+
FlagSet = true;
161+
}
162+
Remaining &= ~Bit;
163+
}
164+
165+
if (!FlagSet)
166+
OS << "None";
167+
168+
return OS;
169+
}
170+
135171
raw_ostream &operator<<(raw_ostream &OS, const RootFlags &Flags) {
136172
OS << "RootFlags(";
137173
bool FlagSet = false;
@@ -205,6 +241,16 @@ raw_ostream &operator<<(raw_ostream &OS, const RootConstants &Constants) {
205241
return OS;
206242
}
207243

244+
raw_ostream &operator<<(raw_ostream &OS, const RootDescriptor &Descriptor) {
245+
ClauseType Type = ClauseType(llvm::to_underlying(Descriptor.Type));
246+
OS << "Root" << Type << "(" << Descriptor.Reg
247+
<< ", space = " << Descriptor.Space
248+
<< ", visibility = " << Descriptor.Visibility
249+
<< ", flags = " << Descriptor.Flags << ")";
250+
251+
return OS;
252+
}
253+
208254
raw_ostream &operator<<(raw_ostream &OS, const DescriptorTable &Table) {
209255
OS << "DescriptorTable(numClauses = " << Table.NumClauses
210256
<< ", visibility = " << Table.Visibility << ")";

llvm/unittests/Frontend/HLSLRootSignatureDumpTest.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,4 +177,60 @@ TEST(HLSLRootSignatureTest, AllRootFlagsDump) {
177177
EXPECT_EQ(Out, Expected);
178178
}
179179

180+
TEST(HLSLRootSignatureTest, RootCBVDump) {
181+
RootDescriptor Descriptor;
182+
Descriptor.Type = DescriptorType::CBuffer;
183+
Descriptor.Reg = {RegisterType::BReg, 0};
184+
Descriptor.setDefaultFlags();
185+
186+
std::string Out;
187+
llvm::raw_string_ostream OS(Out);
188+
OS << Descriptor;
189+
OS.flush();
190+
191+
std::string Expected = "RootCBV(b0, space = 0, "
192+
"visibility = All, "
193+
"flags = DataStaticWhileSetAtExecute)";
194+
EXPECT_EQ(Out, Expected);
195+
}
196+
197+
TEST(HLSLRootSignatureTest, RootSRVDump) {
198+
RootDescriptor Descriptor;
199+
Descriptor.Type = DescriptorType::SRV;
200+
Descriptor.Reg = {RegisterType::TReg, 0};
201+
Descriptor.Space = 42;
202+
Descriptor.Visibility = ShaderVisibility::Geometry;
203+
Descriptor.Flags = RootDescriptorFlags::None;
204+
205+
std::string Out;
206+
llvm::raw_string_ostream OS(Out);
207+
OS << Descriptor;
208+
OS.flush();
209+
210+
std::string Expected =
211+
"RootSRV(t0, space = 42, visibility = Geometry, flags = None)";
212+
EXPECT_EQ(Out, Expected);
213+
}
214+
215+
TEST(HLSLRootSignatureTest, RootUAVDump) {
216+
RootDescriptor Descriptor;
217+
Descriptor.Type = DescriptorType::UAV;
218+
Descriptor.Reg = {RegisterType::UReg, 92374};
219+
Descriptor.Space = 932847;
220+
Descriptor.Visibility = ShaderVisibility::Hull;
221+
Descriptor.Flags = RootDescriptorFlags::ValidFlags;
222+
223+
std::string Out;
224+
llvm::raw_string_ostream OS(Out);
225+
OS << Descriptor;
226+
OS.flush();
227+
228+
std::string Expected =
229+
"RootUAV(u92374, space = 932847, visibility = Hull, flags = "
230+
"DataVolatile | "
231+
"DataStaticWhileSetAtExecute | "
232+
"DataStatic)";
233+
EXPECT_EQ(Out, Expected);
234+
}
235+
180236
} // namespace

0 commit comments

Comments
 (0)