Skip to content

Commit 6f9ed03

Browse files
committed
nfc: move MetadataBuilder to RootSignatureMetadata library
1 parent 7154ac7 commit 6f9ed03

File tree

6 files changed

+253
-175
lines changed

6 files changed

+253
-175
lines changed

clang/lib/CodeGen/CGHLSLRuntime.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
#include "clang/AST/Type.h"
2424
#include "clang/Basic/TargetOptions.h"
2525
#include "llvm/ADT/SmallVector.h"
26-
#include "llvm/Frontend/HLSL/HLSLRootSignatureUtils.h"
26+
#include "llvm/Frontend/HLSL/RootSignatureMetadata.h"
2727
#include "llvm/IR/Constants.h"
2828
#include "llvm/IR/DerivedTypes.h"
2929
#include "llvm/IR/GlobalVariable.h"

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

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -48,31 +48,6 @@ LLVM_ABI raw_ostream &operator<<(raw_ostream &OS, const RootElement &Element);
4848

4949
LLVM_ABI void dumpRootElements(raw_ostream &OS, ArrayRef<RootElement> Elements);
5050

51-
class MetadataBuilder {
52-
public:
53-
MetadataBuilder(llvm::LLVMContext &Ctx, ArrayRef<RootElement> Elements)
54-
: Ctx(Ctx), Elements(Elements) {}
55-
56-
/// Iterates through the elements and dispatches onto the correct Build method
57-
///
58-
/// Accumulates the root signature and returns the Metadata node that is just
59-
/// a list of all the elements
60-
LLVM_ABI MDNode *BuildRootSignature();
61-
62-
private:
63-
/// Define the various builders for the different metadata types
64-
MDNode *BuildRootFlags(const dxbc::RootFlags &Flags);
65-
MDNode *BuildRootConstants(const RootConstants &Constants);
66-
MDNode *BuildRootDescriptor(const RootDescriptor &Descriptor);
67-
MDNode *BuildDescriptorTable(const DescriptorTable &Table);
68-
MDNode *BuildDescriptorTableClause(const DescriptorTableClause &Clause);
69-
MDNode *BuildStaticSampler(const StaticSampler &Sampler);
70-
71-
llvm::LLVMContext &Ctx;
72-
ArrayRef<RootElement> Elements;
73-
SmallVector<Metadata *> GeneratedMetadata;
74-
};
75-
7651
} // namespace rootsig
7752
} // namespace hlsl
7853
} // namespace llvm
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//===- RootSignatureMetadata.h - HLSL Root Signature helpers --------------===//
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 a library for working with HLSL Root Signatures and
10+
/// their metadata representation.
11+
///
12+
//===----------------------------------------------------------------------===//
13+
14+
#ifndef LLVM_FRONTEND_HLSL_ROOTSIGNATUREMETADATA_H
15+
#define LLVM_FRONTEND_HLSL_ROOTSIGNATUREMETADATA_H
16+
17+
#include "llvm/Frontend/HLSL/HLSLRootSignature.h"
18+
19+
namespace llvm {
20+
class LLVMContext;
21+
class MDNode;
22+
class Metadata;
23+
24+
namespace hlsl {
25+
namespace rootsig {
26+
27+
class MetadataBuilder {
28+
public:
29+
MetadataBuilder(llvm::LLVMContext &Ctx, ArrayRef<RootElement> Elements)
30+
: Ctx(Ctx), Elements(Elements) {}
31+
32+
/// Iterates through the elements and dispatches onto the correct Build method
33+
///
34+
/// Accumulates the root signature and returns the Metadata node that is just
35+
/// a list of all the elements
36+
LLVM_ABI MDNode *BuildRootSignature();
37+
38+
private:
39+
/// Define the various builders for the different metadata types
40+
MDNode *BuildRootFlags(const dxbc::RootFlags &Flags);
41+
MDNode *BuildRootConstants(const RootConstants &Constants);
42+
MDNode *BuildRootDescriptor(const RootDescriptor &Descriptor);
43+
MDNode *BuildDescriptorTable(const DescriptorTable &Table);
44+
MDNode *BuildDescriptorTableClause(const DescriptorTableClause &Clause);
45+
MDNode *BuildStaticSampler(const StaticSampler &Sampler);
46+
47+
llvm::LLVMContext &Ctx;
48+
ArrayRef<RootElement> Elements;
49+
SmallVector<Metadata *> GeneratedMetadata;
50+
};
51+
52+
} // namespace rootsig
53+
} // namespace hlsl
54+
} // namespace llvm
55+
56+
#endif // LLVM_FRONTEND_HLSL_ROOTSIGNATUREMETADATA_H

llvm/lib/Frontend/HLSL/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ add_llvm_component_library(LLVMFrontendHLSL
33
HLSLResource.cpp
44
HLSLRootSignature.cpp
55
HLSLRootSignatureUtils.cpp
6+
RootSignatureMetadata.cpp
67

78
ADDITIONAL_HEADER_DIRS
89
${LLVM_MAIN_INCLUDE_DIR}/llvm/Frontend

llvm/lib/Frontend/HLSL/HLSLRootSignatureUtils.cpp

Lines changed: 1 addition & 149 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
#include "llvm/Frontend/HLSL/HLSLRootSignatureUtils.h"
1414
#include "llvm/ADT/SmallString.h"
1515
#include "llvm/ADT/bit.h"
16-
#include "llvm/IR/IRBuilder.h"
17-
#include "llvm/IR/Metadata.h"
1816
#include "llvm/Support/ScopedPrinter.h"
1917

2018
namespace llvm {
@@ -210,8 +208,7 @@ raw_ostream &operator<<(raw_ostream &OS, const StaticSampler &Sampler) {
210208
namespace {
211209

212210
// We use the OverloadVisit with std::visit to ensure the compiler catches if a
213-
// new RootElement variant type is added but it's operator<< or metadata
214-
// generation isn't handled.
211+
// new RootElement variant type is added but it's operator<< isn't handled.
215212
template <class... Ts> struct OverloadedVisit : Ts... {
216213
using Ts::operator()...;
217214
};
@@ -244,151 +241,6 @@ void dumpRootElements(raw_ostream &OS, ArrayRef<RootElement> Elements) {
244241
OS << "}";
245242
}
246243

247-
MDNode *MetadataBuilder::BuildRootSignature() {
248-
const auto Visitor = OverloadedVisit{
249-
[this](const dxbc::RootFlags &Flags) -> MDNode * {
250-
return BuildRootFlags(Flags);
251-
},
252-
[this](const RootConstants &Constants) -> MDNode * {
253-
return BuildRootConstants(Constants);
254-
},
255-
[this](const RootDescriptor &Descriptor) -> MDNode * {
256-
return BuildRootDescriptor(Descriptor);
257-
},
258-
[this](const DescriptorTableClause &Clause) -> MDNode * {
259-
return BuildDescriptorTableClause(Clause);
260-
},
261-
[this](const DescriptorTable &Table) -> MDNode * {
262-
return BuildDescriptorTable(Table);
263-
},
264-
[this](const StaticSampler &Sampler) -> MDNode * {
265-
return BuildStaticSampler(Sampler);
266-
},
267-
};
268-
269-
for (const RootElement &Element : Elements) {
270-
MDNode *ElementMD = std::visit(Visitor, Element);
271-
assert(ElementMD != nullptr &&
272-
"Root Element must be initialized and validated");
273-
GeneratedMetadata.push_back(ElementMD);
274-
}
275-
276-
return MDNode::get(Ctx, GeneratedMetadata);
277-
}
278-
279-
MDNode *MetadataBuilder::BuildRootFlags(const dxbc::RootFlags &Flags) {
280-
IRBuilder<> Builder(Ctx);
281-
Metadata *Operands[] = {
282-
MDString::get(Ctx, "RootFlags"),
283-
ConstantAsMetadata::get(Builder.getInt32(llvm::to_underlying(Flags))),
284-
};
285-
return MDNode::get(Ctx, Operands);
286-
}
287-
288-
MDNode *MetadataBuilder::BuildRootConstants(const RootConstants &Constants) {
289-
IRBuilder<> Builder(Ctx);
290-
Metadata *Operands[] = {
291-
MDString::get(Ctx, "RootConstants"),
292-
ConstantAsMetadata::get(
293-
Builder.getInt32(llvm::to_underlying(Constants.Visibility))),
294-
ConstantAsMetadata::get(Builder.getInt32(Constants.Reg.Number)),
295-
ConstantAsMetadata::get(Builder.getInt32(Constants.Space)),
296-
ConstantAsMetadata::get(Builder.getInt32(Constants.Num32BitConstants)),
297-
};
298-
return MDNode::get(Ctx, Operands);
299-
}
300-
301-
MDNode *MetadataBuilder::BuildRootDescriptor(const RootDescriptor &Descriptor) {
302-
IRBuilder<> Builder(Ctx);
303-
std::optional<StringRef> TypeName =
304-
getEnumName(dxil::ResourceClass(llvm::to_underlying(Descriptor.Type)),
305-
ArrayRef(ResourceClassNames));
306-
assert(TypeName && "Provided an invalid Resource Class");
307-
llvm::SmallString<7> Name({"Root", *TypeName});
308-
Metadata *Operands[] = {
309-
MDString::get(Ctx, Name),
310-
ConstantAsMetadata::get(
311-
Builder.getInt32(llvm::to_underlying(Descriptor.Visibility))),
312-
ConstantAsMetadata::get(Builder.getInt32(Descriptor.Reg.Number)),
313-
ConstantAsMetadata::get(Builder.getInt32(Descriptor.Space)),
314-
ConstantAsMetadata::get(
315-
Builder.getInt32(llvm::to_underlying(Descriptor.Flags))),
316-
};
317-
return MDNode::get(Ctx, Operands);
318-
}
319-
320-
MDNode *MetadataBuilder::BuildDescriptorTable(const DescriptorTable &Table) {
321-
IRBuilder<> Builder(Ctx);
322-
SmallVector<Metadata *> TableOperands;
323-
// Set the mandatory arguments
324-
TableOperands.push_back(MDString::get(Ctx, "DescriptorTable"));
325-
TableOperands.push_back(ConstantAsMetadata::get(
326-
Builder.getInt32(llvm::to_underlying(Table.Visibility))));
327-
328-
// Remaining operands are references to the table's clauses. The in-memory
329-
// representation of the Root Elements created from parsing will ensure that
330-
// the previous N elements are the clauses for this table.
331-
assert(Table.NumClauses <= GeneratedMetadata.size() &&
332-
"Table expected all owned clauses to be generated already");
333-
// So, add a refence to each clause to our operands
334-
TableOperands.append(GeneratedMetadata.end() - Table.NumClauses,
335-
GeneratedMetadata.end());
336-
// Then, remove those clauses from the general list of Root Elements
337-
GeneratedMetadata.pop_back_n(Table.NumClauses);
338-
339-
return MDNode::get(Ctx, TableOperands);
340-
}
341-
342-
MDNode *MetadataBuilder::BuildDescriptorTableClause(
343-
const DescriptorTableClause &Clause) {
344-
IRBuilder<> Builder(Ctx);
345-
std::optional<StringRef> Name =
346-
getEnumName(dxil::ResourceClass(llvm::to_underlying(Clause.Type)),
347-
ArrayRef(ResourceClassNames));
348-
assert(Name && "Provided an invalid Resource Class");
349-
Metadata *Operands[] = {
350-
MDString::get(Ctx, *Name),
351-
ConstantAsMetadata::get(Builder.getInt32(Clause.NumDescriptors)),
352-
ConstantAsMetadata::get(Builder.getInt32(Clause.Reg.Number)),
353-
ConstantAsMetadata::get(Builder.getInt32(Clause.Space)),
354-
ConstantAsMetadata::get(Builder.getInt32(Clause.Offset)),
355-
ConstantAsMetadata::get(
356-
Builder.getInt32(llvm::to_underlying(Clause.Flags))),
357-
};
358-
return MDNode::get(Ctx, Operands);
359-
}
360-
361-
MDNode *MetadataBuilder::BuildStaticSampler(const StaticSampler &Sampler) {
362-
IRBuilder<> Builder(Ctx);
363-
Metadata *Operands[] = {
364-
MDString::get(Ctx, "StaticSampler"),
365-
ConstantAsMetadata::get(
366-
Builder.getInt32(llvm::to_underlying(Sampler.Filter))),
367-
ConstantAsMetadata::get(
368-
Builder.getInt32(llvm::to_underlying(Sampler.AddressU))),
369-
ConstantAsMetadata::get(
370-
Builder.getInt32(llvm::to_underlying(Sampler.AddressV))),
371-
ConstantAsMetadata::get(
372-
Builder.getInt32(llvm::to_underlying(Sampler.AddressW))),
373-
ConstantAsMetadata::get(llvm::ConstantFP::get(llvm::Type::getFloatTy(Ctx),
374-
Sampler.MipLODBias)),
375-
ConstantAsMetadata::get(Builder.getInt32(Sampler.MaxAnisotropy)),
376-
ConstantAsMetadata::get(
377-
Builder.getInt32(llvm::to_underlying(Sampler.CompFunc))),
378-
ConstantAsMetadata::get(
379-
Builder.getInt32(llvm::to_underlying(Sampler.BorderColor))),
380-
ConstantAsMetadata::get(
381-
llvm::ConstantFP::get(llvm::Type::getFloatTy(Ctx), Sampler.MinLOD)),
382-
ConstantAsMetadata::get(
383-
llvm::ConstantFP::get(llvm::Type::getFloatTy(Ctx), Sampler.MaxLOD)),
384-
ConstantAsMetadata::get(Builder.getInt32(Sampler.Reg.Number)),
385-
ConstantAsMetadata::get(Builder.getInt32(Sampler.Space)),
386-
ConstantAsMetadata::get(
387-
Builder.getInt32(llvm::to_underlying(Sampler.Visibility))),
388-
};
389-
return MDNode::get(Ctx, Operands);
390-
}
391-
392244
} // namespace rootsig
393245
} // namespace hlsl
394246
} // namespace llvm

0 commit comments

Comments
 (0)