Skip to content

Commit 1d3408c

Browse files
committed
self-review: misc clean up
1 parent 9bdf703 commit 1d3408c

File tree

4 files changed

+52
-36
lines changed

4 files changed

+52
-36
lines changed

clang/lib/CodeGen/CGHLSLRuntime.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@ void addRootSignature(
126126

127127
llvm::hlsl::root_signature::MetadataBuilder Builder(Ctx, Elements);
128128
MDNode *RootSignature = Builder.BuildRootSignature();
129-
MDNode *FnPairing = MDNode::get(Ctx, {ValueAsMetadata::get(Fn),
130-
RootSignature});
129+
MDNode *FnPairing =
130+
MDNode::get(Ctx, {ValueAsMetadata::get(Fn), RootSignature});
131131

132132
StringRef RootSignatureValKey = "dx.rootsignatures";
133133
auto *RootSignatureValMD = M.getOrInsertNamedMetadata(RootSignatureValKey);
@@ -471,10 +471,9 @@ void CGHLSLRuntime::emitEntryFunction(const FunctionDecl *FD,
471471

472472
// Add and identify root signature to function, if applicable
473473
const AttrVec &Attrs = FD->getAttrs();
474-
for (const Attr *Attr : Attrs) {
474+
for (const Attr *Attr : Attrs)
475475
if (const auto *RSAttr = dyn_cast<HLSLRootSignatureAttr>(Attr))
476476
addRootSignature(RSAttr->getElements(), EntryFn, M);
477-
}
478477
}
479478

480479
void CGHLSLRuntime::setHLSLFunctionAttributes(const FunctionDecl *FD,

clang/test/CodeGenHLSL/RootSignature.hlsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ void FirstEntry() {}
1919
[numthreads(1,1,1)]
2020
void SecondEntry() {}
2121

22-
// Sanity test to ensure to root is added for this function
22+
// Sanity test to ensure no root is added for this function
2323
[shader("compute")]
2424
[numthreads(1,1,1)]
2525
void ThirdEntry() {}

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,15 +129,18 @@ using ParamType = std::variant<uint32_t *, DescriptorRangeOffset *,
129129
DescriptorRangeFlags *, ShaderVisibility *>;
130130

131131
class MetadataBuilder {
132-
public:
132+
public:
133133
MetadataBuilder(llvm::LLVMContext &Ctx, ArrayRef<RootElement> Elements)
134-
: Ctx(Ctx), Elements(Elements) {}
134+
: Ctx(Ctx), Elements(Elements) {}
135135

136-
// Iterates through the elements and builds the respective nodes
136+
/// Iterates through the elements and dispatches onto the correct Build method
137+
///
138+
/// Accumulates the root signature and returns the Metadata node that is just
139+
/// a list of all the elements
137140
MDNode *BuildRootSignature();
138141

139-
private:
140-
// Define the various builders for the different metadata types
142+
private:
143+
/// Define the various builders for the different metadata types
141144
MDNode *BuildDescriptorTable(const DescriptorTable &Table);
142145
MDNode *BuildDescriptorTableClause(const DescriptorTableClause &Clause);
143146

llvm/lib/Frontend/HLSL/HLSLRootSignature.cpp

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
//===- HLSLRootSignature.cpp - HLSL Root Signature helper objects ----------===//
1+
//===- HLSLRootSignature.cpp - HLSL Root Signature helper objects
2+
//----------===//
23
//
34
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45
// See https://llvm.org/LICENSE.txt for license information.
@@ -17,7 +18,9 @@
1718

1819
namespace llvm {
1920
namespace hlsl {
20-
namespace root_signature {
21+
namespace rootsig {
22+
23+
// Static helper functions
2124

2225
static MDString *ClauseTypeToName(LLVMContext &Ctx, ClauseType Type) {
2326
StringRef Name;
@@ -47,16 +50,15 @@ template <class... Ts> OverloadBuilds(Ts...) -> OverloadBuilds<Ts...>;
4750
MDNode *MetadataBuilder::BuildRootSignature() {
4851
for (const RootElement &Element : Elements) {
4952
MDNode *ElementMD =
50-
std::visit(
51-
OverloadBuilds{
52-
[&](DescriptorTable Table) -> MDNode * {
53-
return BuildDescriptorTable(Table);
54-
},
55-
[&](DescriptorTableClause Clause) -> MDNode * {
56-
return BuildDescriptorTableClause(Clause);
57-
},
58-
},
59-
Element);
53+
std::visit(OverloadBuilds{
54+
[&](DescriptorTable Table) -> MDNode * {
55+
return BuildDescriptorTable(Table);
56+
},
57+
[&](DescriptorTableClause Clause) -> MDNode * {
58+
return BuildDescriptorTableClause(Clause);
59+
},
60+
},
61+
Element);
6062
GeneratedMetadata.push_back(ElementMD);
6163
}
6264

@@ -66,29 +68,41 @@ MDNode *MetadataBuilder::BuildRootSignature() {
6668
MDNode *MetadataBuilder::BuildDescriptorTable(const DescriptorTable &Table) {
6769
IRBuilder<> B(Ctx);
6870
SmallVector<Metadata *> TableOperands;
71+
// Set the mandatory arguments
6972
TableOperands.push_back(MDString::get(Ctx, "DescriptorTable"));
70-
TableOperands.push_back(ConstantAsMetadata::get(B.getInt32(llvm::to_underlying(Table.Visibility))));
73+
TableOperands.push_back(ConstantAsMetadata::get(
74+
B.getInt32(llvm::to_underlying(Table.Visibility))));
7175

72-
assert(Table.NumClauses <= GeneratedMetadata.size() && "Table expected all owned clauses to be generated already");
73-
TableOperands.append(GeneratedMetadata.end() - Table.NumClauses, GeneratedMetadata.end());
76+
// Remaining operands are references to the table's clauses. The in-memory
77+
// representation of the Root Elements created from parsing will ensure that
78+
// the previous N elements are the clauses for this table.
79+
assert(Table.NumClauses <= GeneratedMetadata.size() &&
80+
"Table expected all owned clauses to be generated already");
81+
// So, add a refence to each clause to our operands
82+
TableOperands.append(GeneratedMetadata.end() - Table.NumClauses,
83+
GeneratedMetadata.end());
84+
// Then, remove those clauses from the general list of Root Elements
7485
GeneratedMetadata.pop_back_n(Table.NumClauses);
7586

7687
return MDNode::get(Ctx, TableOperands);
7788
}
7889

79-
MDNode *MetadataBuilder::BuildDescriptorTableClause(const DescriptorTableClause &Clause) {
90+
MDNode *MetadataBuilder::BuildDescriptorTableClause(
91+
const DescriptorTableClause &Clause) {
8092
IRBuilder<> B(Ctx);
81-
return MDNode::get(Ctx, {
82-
ClauseTypeToName(Ctx, Clause.Type),
83-
ConstantAsMetadata::get(B.getInt32(Clause.NumDescriptors)),
84-
ConstantAsMetadata::get(B.getInt32(Clause.Register.Number)),
85-
ConstantAsMetadata::get(B.getInt32(Clause.Space)),
86-
ConstantAsMetadata::get(B.getInt32(llvm::to_underlying(Clause.Offset))),
87-
ConstantAsMetadata::get(B.getInt32(llvm::to_underlying(Clause.Flags))),
88-
});
93+
return MDNode::get(
94+
Ctx, {
95+
ClauseTypeToName(Ctx, Clause.Type),
96+
ConstantAsMetadata::get(B.getInt32(Clause.NumDescriptors)),
97+
ConstantAsMetadata::get(B.getInt32(Clause.Register.Number)),
98+
ConstantAsMetadata::get(B.getInt32(Clause.Space)),
99+
ConstantAsMetadata::get(
100+
B.getInt32(llvm::to_underlying(Clause.Offset))),
101+
ConstantAsMetadata::get(
102+
B.getInt32(llvm::to_underlying(Clause.Flags))),
103+
});
89104
}
90105

91-
} // namespace root_signature
106+
} // namespace rootsig
92107
} // namespace hlsl
93108
} // namespace llvm
94-

0 commit comments

Comments
 (0)