Skip to content

Commit bdbf47c

Browse files
committed
introduce a MetadataBuilder to handle the construction of nodes
1 parent 76e3b5e commit bdbf47c

File tree

4 files changed

+83
-18
lines changed

4 files changed

+83
-18
lines changed

clang/lib/CodeGen/CGHLSLRuntime.cpp

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,20 +73,14 @@ void addRootSignature(
7373
llvm::Function *Fn, llvm::Module &M) {
7474
auto &Ctx = M.getContext();
7575

76-
SmallVector<Metadata *> GeneratedMetadata;
77-
for (auto Element : Elements) {
78-
MDNode *ExampleRootElement = MDNode::get(Ctx, {});
79-
GeneratedMetadata.push_back(ExampleRootElement);
80-
}
81-
82-
MDNode *ExampleRootSignature = MDNode::get(Ctx, GeneratedMetadata);
83-
84-
MDNode *ExamplePairing = MDNode::get(Ctx, {ValueAsMetadata::get(Fn),
85-
ExampleRootSignature});
76+
llvm::hlsl::rootsig::MetadataBuilder Builder(Ctx, Elements);
77+
MDNode *RootSignature = Builder.BuildRootSignature();
78+
MDNode *FnPairing = MDNode::get(Ctx, {ValueAsMetadata::get(Fn),
79+
RootSignature});
8680

8781
StringRef RootSignatureValKey = "dx.rootsignatures";
8882
auto *RootSignatureValMD = M.getOrInsertNamedMetadata(RootSignatureValKey);
89-
RootSignatureValMD->addOperand(ExamplePairing);
83+
RootSignatureValMD->addOperand(FnPairing);
9084
}
9185

9286
} // namespace
Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
11
// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -emit-llvm -o - %s | FileCheck %s
22

33
// CHECK-DAG: ![[#EMPTY:]] = !{}
4-
// CHECK-DAG: ![[#SECOND_RS:]] = !{![[#EMPTY]]}
5-
// CHECK-DAG: ![[#SECOND_ENTRY:]] = !{ptr @SecondEntry, ![[#SECOND_RS]]}
6-
// CHECK-DAG: ![[#FIRST_ENTRY:]] = !{ptr @FirstEntry, ![[#EMPTY]]}
7-
// CHECK-DAG: !dx.rootsignatures = !{![[#FIRST_ENTRY]], ![[#SECOND_ENTRY]]}
8-
94
[shader("compute"), RootSignature("")]
105
[numthreads(1,1,1)]
116
void FirstEntry() {}
127

13-
[shader("compute"), RootSignature("DescriptorTable()")]
8+
// CHECK-DAG: ![[#TABLE:]] = !{!"DescriptorTable"}
9+
// CHECK-DAG: ![[#SECOND_RS:]] = !{![[#TABLE]]}
10+
11+
#define SampleDescriptorTable \
12+
"DescriptorTable( " \
13+
")"
14+
[shader("compute"), RootSignature(SampleDescriptorTable)]
1415
[numthreads(1,1,1)]
1516
void SecondEntry() {}
1617

1718
// Sanity test to ensure to root is added for this function
1819
[shader("compute")]
1920
[numthreads(1,1,1)]
2021
void ThirdEntry() {}
22+
23+
// CHECK-DAG: ![[#FIRST_ENTRY:]] = !{ptr @FirstEntry, ![[#EMPTY]]}
24+
// CHECK-DAG: ![[#SECOND_ENTRY:]] = !{ptr @SecondEntry, ![[#SECOND_RS]]}
25+
// CHECK-DAG: !dx.rootsignatures = !{![[#FIRST_ENTRY]], ![[#SECOND_ENTRY]]}

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,16 @@
1515
#define LLVM_FRONTEND_HLSL_HLSLROOTSIGNATURE_H
1616

1717
#include "llvm/ADT/ArrayRef.h"
18+
// #include "llvm/ADT/STLForwardCompat.h"
1819
#include "llvm/Support/DXILABI.h"
1920
#include "llvm/Support/raw_ostream.h"
2021
#include <variant>
2122

2223
namespace llvm {
24+
class LLVMContext;
25+
class MDNode;
26+
class Metadata;
27+
2328
namespace hlsl {
2429
namespace rootsig {
2530

@@ -125,6 +130,24 @@ using RootElement = std::variant<RootFlags, RootConstants, DescriptorTable,
125130

126131
void dumpRootElements(raw_ostream &OS, ArrayRef<RootElement> Elements);
127132

133+
class MetadataBuilder {
134+
public:
135+
MetadataBuilder(llvm::LLVMContext &Ctx, ArrayRef<RootElement> Elements)
136+
: Ctx(Ctx), Elements(Elements) {}
137+
138+
// Iterates through the elements and builds the respective nodes
139+
MDNode *BuildRootSignature();
140+
141+
private:
142+
// Define the various builders for the different metadata types
143+
MDNode *BuildDescriptorTable(const DescriptorTable &Table);
144+
MDNode *BuildDescriptorTableClause(const DescriptorTableClause &Clause);
145+
146+
llvm::LLVMContext &Ctx;
147+
ArrayRef<RootElement> Elements;
148+
SmallVector<Metadata *> GeneratedMetadata;
149+
};
150+
128151
} // namespace rootsig
129152
} // namespace hlsl
130153
} // namespace llvm

llvm/lib/Frontend/HLSL/HLSLRootSignature.cpp

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@
1010
///
1111
//===----------------------------------------------------------------------===//
1212

13-
#include "llvm/Frontend/HLSL/HLSLRootSignature.h"
1413
#include "llvm/ADT/bit.h"
14+
#include "llvm/Frontend/HLSL/HLSLRootSignature.h"
15+
#include "llvm/IR/IRBuilder.h"
16+
#include "llvm/IR/Metadata.h"
17+
#include "llvm/IR/Module.h"
1518

1619
namespace llvm {
1720
namespace hlsl {
@@ -160,6 +163,46 @@ void dumpRootElements(raw_ostream &OS, ArrayRef<RootElement> Elements) {
160163
OS << "}";
161164
}
162165

166+
static MDString *ClauseTypeToName(LLVMContext &Ctx, ClauseType Type) {
167+
StringRef Name;
168+
switch (Type) {
169+
case ClauseType::CBuffer:
170+
Name = "CBV";
171+
break;
172+
case ClauseType::SRV:
173+
Name = "SRV";
174+
break;
175+
case ClauseType::UAV:
176+
Name = "UAV";
177+
break;
178+
case ClauseType::Sampler:
179+
Name = "Sampler";
180+
break;
181+
}
182+
return MDString::get(Ctx, Name);
183+
}
184+
185+
MDNode *MetadataBuilder::BuildRootSignature() {
186+
for (const RootElement &Element : Elements) {
187+
MDNode *ElementMD = nullptr;
188+
if (const auto &Clause = std::get_if<DescriptorTableClause>(&Element))
189+
ElementMD = BuildDescriptorTableClause(*Clause);
190+
if (const auto &Table = std::get_if<DescriptorTable>(&Element))
191+
ElementMD = BuildDescriptorTable(*Table);
192+
GeneratedMetadata.push_back(ElementMD);
193+
}
194+
195+
return MDNode::get(Ctx, GeneratedMetadata);
196+
}
197+
198+
MDNode *MetadataBuilder::BuildDescriptorTable(const DescriptorTable &Table) {
199+
return MDNode::get(Ctx, {MDString::get(Ctx, "DescriptorTable")});
200+
}
201+
202+
MDNode *MetadataBuilder::BuildDescriptorTableClause(const DescriptorTableClause &Clause) {
203+
return MDNode::get(Ctx, {ClauseTypeToName(Ctx, Clause.Type)});
204+
}
205+
163206
} // namespace rootsig
164207
} // namespace hlsl
165208
} // namespace llvm

0 commit comments

Comments
 (0)