Skip to content

Commit 74ed334

Browse files
authored
[HLSL][RootSignature] Implement serialized dump of Descriptor Tables (#138326)
- defines the `dump` method for in-memory descriptor table data structs in `Frontend/HLSLRootSignature` - creates unit test infrastructure to support unit tests of the dump methods Resolves #138189
1 parent b3963d3 commit 74ed334

File tree

5 files changed

+268
-0
lines changed

5 files changed

+268
-0
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#define LLVM_FRONTEND_HLSL_HLSLROOTSIGNATURE_H
1616

1717
#include "llvm/Support/DXILABI.h"
18+
#include "llvm/Support/raw_ostream.h"
1819
#include <variant>
1920

2021
namespace llvm {
@@ -83,6 +84,8 @@ struct RootConstants {
8384
struct DescriptorTable {
8485
ShaderVisibility Visibility = ShaderVisibility::All;
8586
uint32_t NumClauses = 0; // The number of clauses in the table
87+
88+
void dump(raw_ostream &OS) const;
8689
};
8790

8891
static const uint32_t NumDescriptorsUnbounded = 0xffffffff;
@@ -111,6 +114,8 @@ struct DescriptorTableClause {
111114
break;
112115
}
113116
}
117+
118+
void dump(raw_ostream &OS) const;
114119
};
115120

116121
// Models RootElement : RootConstants | DescriptorTable | DescriptorTableClause

llvm/lib/Frontend/HLSL/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
add_llvm_component_library(LLVMFrontendHLSL
22
CBuffer.cpp
33
HLSLResource.cpp
4+
HLSLRootSignature.cpp
45

56
ADDITIONAL_HEADER_DIRS
67
${LLVM_MAIN_INCLUDE_DIR}/llvm/Frontend
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
//===- HLSLRootSignature.cpp - HLSL Root Signature helper objects ---------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
///
9+
/// \file This file contains helpers for working with HLSL Root Signatures.
10+
///
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "llvm/Frontend/HLSL/HLSLRootSignature.h"
14+
#include "llvm/ADT/bit.h"
15+
16+
namespace llvm {
17+
namespace hlsl {
18+
namespace rootsig {
19+
20+
static raw_ostream &operator<<(raw_ostream &OS, const Register &Reg) {
21+
switch (Reg.ViewType) {
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+
OS << Reg.Number;
36+
return OS;
37+
}
38+
39+
static raw_ostream &operator<<(raw_ostream &OS,
40+
const ShaderVisibility &Visibility) {
41+
switch (Visibility) {
42+
case ShaderVisibility::All:
43+
OS << "All";
44+
break;
45+
case ShaderVisibility::Vertex:
46+
OS << "Vertex";
47+
break;
48+
case ShaderVisibility::Hull:
49+
OS << "Hull";
50+
break;
51+
case ShaderVisibility::Domain:
52+
OS << "Domain";
53+
break;
54+
case ShaderVisibility::Geometry:
55+
OS << "Geometry";
56+
break;
57+
case ShaderVisibility::Pixel:
58+
OS << "Pixel";
59+
break;
60+
case ShaderVisibility::Amplification:
61+
OS << "Amplification";
62+
break;
63+
case ShaderVisibility::Mesh:
64+
OS << "Mesh";
65+
break;
66+
}
67+
68+
return OS;
69+
}
70+
71+
void DescriptorTable::dump(raw_ostream &OS) const {
72+
OS << "DescriptorTable(numClauses = " << NumClauses
73+
<< ", visibility = " << Visibility << ")";
74+
}
75+
76+
static raw_ostream &operator<<(raw_ostream &OS, const ClauseType &Type) {
77+
switch (Type) {
78+
case ClauseType::CBuffer:
79+
OS << "CBV";
80+
break;
81+
case ClauseType::SRV:
82+
OS << "SRV";
83+
break;
84+
case ClauseType::UAV:
85+
OS << "UAV";
86+
break;
87+
case ClauseType::Sampler:
88+
OS << "Sampler";
89+
break;
90+
}
91+
92+
return OS;
93+
}
94+
95+
static raw_ostream &operator<<(raw_ostream &OS,
96+
const DescriptorRangeFlags &Flags) {
97+
bool FlagSet = false;
98+
unsigned Remaining = llvm::to_underlying(Flags);
99+
while (Remaining) {
100+
unsigned Bit = 1u << llvm::countr_zero(Remaining);
101+
if (Remaining & Bit) {
102+
if (FlagSet)
103+
OS << " | ";
104+
105+
switch (static_cast<DescriptorRangeFlags>(Bit)) {
106+
case DescriptorRangeFlags::DescriptorsVolatile:
107+
OS << "DescriptorsVolatile";
108+
break;
109+
case DescriptorRangeFlags::DataVolatile:
110+
OS << "DataVolatile";
111+
break;
112+
case DescriptorRangeFlags::DataStaticWhileSetAtExecute:
113+
OS << "DataStaticWhileSetAtExecute";
114+
break;
115+
case DescriptorRangeFlags::DataStatic:
116+
OS << "DataStatic";
117+
break;
118+
case DescriptorRangeFlags::DescriptorsStaticKeepingBufferBoundsChecks:
119+
OS << "DescriptorsStaticKeepingBufferBoundsChecks";
120+
break;
121+
default:
122+
OS << "invalid: " << Bit;
123+
break;
124+
}
125+
126+
FlagSet = true;
127+
}
128+
Remaining &= ~Bit;
129+
}
130+
131+
if (!FlagSet)
132+
OS << "None";
133+
134+
return OS;
135+
}
136+
137+
void DescriptorTableClause::dump(raw_ostream &OS) const {
138+
OS << Type << "(" << Reg << ", numDescriptors = " << NumDescriptors
139+
<< ", space = " << Space << ", offset = ";
140+
if (Offset == DescriptorTableOffsetAppend)
141+
OS << "DescriptorTableOffsetAppend";
142+
else
143+
OS << Offset;
144+
OS << ", flags = " << Flags << ")";
145+
}
146+
147+
} // namespace rootsig
148+
} // namespace hlsl
149+
} // namespace llvm

llvm/unittests/Frontend/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
set(LLVM_LINK_COMPONENTS
22
Analysis
33
Core
4+
FrontendHLSL
45
FrontendOpenACC
56
FrontendOpenMP
67
Passes
@@ -10,6 +11,7 @@ set(LLVM_LINK_COMPONENTS
1011
)
1112

1213
add_llvm_unittest(LLVMFrontendTests
14+
HLSLRootSignatureDumpTest.cpp
1315
OpenACCTest.cpp
1416
OpenMPContextTest.cpp
1517
OpenMPIRBuilderTest.cpp
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
//===-------- HLSLRootSignatureDumpTest.cpp - RootSignature dump tests ----===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "llvm/Frontend/HLSL/HLSLRootSignature.h"
10+
#include "gtest/gtest.h"
11+
12+
using namespace llvm::hlsl::rootsig;
13+
14+
namespace {
15+
16+
TEST(HLSLRootSignatureTest, DescriptorCBVClauseDump) {
17+
DescriptorTableClause Clause;
18+
Clause.Type = ClauseType::CBuffer;
19+
Clause.Reg = {RegisterType::BReg, 0};
20+
Clause.setDefaultFlags();
21+
22+
std::string Out;
23+
llvm::raw_string_ostream OS(Out);
24+
Clause.dump(OS);
25+
OS.flush();
26+
27+
std::string Expected = "CBV(b0, numDescriptors = 1, space = 0, "
28+
"offset = DescriptorTableOffsetAppend, "
29+
"flags = DataStaticWhileSetAtExecute)";
30+
EXPECT_EQ(Out, Expected);
31+
}
32+
33+
TEST(HLSLRootSignatureTest, DescriptorSRVClauseDump) {
34+
DescriptorTableClause Clause;
35+
Clause.Type = ClauseType::SRV;
36+
Clause.Reg = {RegisterType::TReg, 0};
37+
Clause.NumDescriptors = 2;
38+
Clause.Space = 42;
39+
Clause.Offset = 3;
40+
Clause.Flags = DescriptorRangeFlags::None;
41+
42+
std::string Out;
43+
llvm::raw_string_ostream OS(Out);
44+
Clause.dump(OS);
45+
OS.flush();
46+
47+
std::string Expected =
48+
"SRV(t0, numDescriptors = 2, space = 42, offset = 3, flags = None)";
49+
EXPECT_EQ(Out, Expected);
50+
}
51+
52+
TEST(HLSLRootSignatureTest, DescriptorUAVClauseDump) {
53+
DescriptorTableClause Clause;
54+
Clause.Type = ClauseType::UAV;
55+
Clause.Reg = {RegisterType::UReg, 92374};
56+
Clause.NumDescriptors = 3298;
57+
Clause.Space = 932847;
58+
Clause.Offset = 1;
59+
Clause.Flags = DescriptorRangeFlags::ValidFlags;
60+
61+
std::string Out;
62+
llvm::raw_string_ostream OS(Out);
63+
Clause.dump(OS);
64+
OS.flush();
65+
66+
std::string Expected =
67+
"UAV(u92374, numDescriptors = 3298, space = 932847, offset = 1, flags = "
68+
"DescriptorsVolatile | "
69+
"DataVolatile | "
70+
"DataStaticWhileSetAtExecute | "
71+
"DataStatic | "
72+
"DescriptorsStaticKeepingBufferBoundsChecks)";
73+
EXPECT_EQ(Out, Expected);
74+
}
75+
76+
TEST(HLSLRootSignatureTest, DescriptorSamplerClauseDump) {
77+
DescriptorTableClause Clause;
78+
Clause.Type = ClauseType::Sampler;
79+
Clause.Reg = {RegisterType::SReg, 0};
80+
Clause.NumDescriptors = 2;
81+
Clause.Space = 42;
82+
Clause.Offset = DescriptorTableOffsetAppend;
83+
Clause.Flags = DescriptorRangeFlags::ValidSamplerFlags;
84+
85+
std::string Out;
86+
llvm::raw_string_ostream OS(Out);
87+
Clause.dump(OS);
88+
OS.flush();
89+
90+
std::string Expected = "Sampler(s0, numDescriptors = 2, space = 42, offset = "
91+
"DescriptorTableOffsetAppend, "
92+
"flags = DescriptorsVolatile)";
93+
EXPECT_EQ(Out, Expected);
94+
}
95+
96+
TEST(HLSLRootSignatureTest, DescriptorTableDump) {
97+
DescriptorTable Table;
98+
Table.NumClauses = 4;
99+
Table.Visibility = ShaderVisibility::Geometry;
100+
101+
std::string Out;
102+
llvm::raw_string_ostream OS(Out);
103+
Table.dump(OS);
104+
OS.flush();
105+
106+
std::string Expected =
107+
"DescriptorTable(numClauses = 4, visibility = Geometry)";
108+
EXPECT_EQ(Out, Expected);
109+
}
110+
111+
} // namespace

0 commit comments

Comments
 (0)