15
15
#define LLVM_FRONTEND_HLSL_ROOTSIGNATUREMETADATA_H
16
16
17
17
#include " llvm/Frontend/HLSL/HLSLRootSignature.h"
18
- #include " llvm/MC/DXContainerRootSignature .h"
18
+ #include " llvm/IR/Constants .h"
19
19
#include " llvm/IR/Function.h"
20
+ #include " llvm/MC/DXContainerRootSignature.h"
20
21
#include " llvm/Support/Error.h"
22
+ #include < cstdint>
21
23
#include < unordered_map>
22
24
23
25
namespace llvm {
@@ -28,6 +30,96 @@ class Metadata;
28
30
namespace hlsl {
29
31
namespace rootsig {
30
32
33
+ inline std::optional<uint32_t > extractMdIntValue (MDNode *Node,
34
+ unsigned int OpId) {
35
+ if (auto *CI =
36
+ mdconst::dyn_extract<ConstantInt>(Node->getOperand (OpId).get ()))
37
+ return CI->getZExtValue ();
38
+ return std::nullopt;
39
+ }
40
+
41
+ inline std::optional<float > extractMdFloatValue (MDNode *Node,
42
+ unsigned int OpId) {
43
+ if (auto *CI = mdconst::dyn_extract<ConstantFP>(Node->getOperand (OpId).get ()))
44
+ return CI->getValueAPF ().convertToFloat ();
45
+ return std::nullopt;
46
+ }
47
+
48
+ inline std::optional<StringRef> extractMdStringValue (MDNode *Node,
49
+ unsigned int OpId) {
50
+ MDString *NodeText = dyn_cast<MDString>(Node->getOperand (OpId));
51
+ if (NodeText == nullptr )
52
+ return std::nullopt;
53
+ return NodeText->getString ();
54
+ }
55
+
56
+ template <typename T>
57
+ class RootSignatureValidationError
58
+ : public ErrorInfo<RootSignatureValidationError<T>> {
59
+ public:
60
+ static char ID;
61
+ std::string ParamName;
62
+ T Value;
63
+
64
+ RootSignatureValidationError (StringRef ParamName, T Value)
65
+ : ParamName(ParamName.str()), Value(Value) {}
66
+
67
+ void log (raw_ostream &OS) const override {
68
+ OS << " Invalid value for " << ParamName << " : " << Value;
69
+ }
70
+
71
+ std::error_code convertToErrorCode () const override {
72
+ return llvm::inconvertibleErrorCode ();
73
+ }
74
+ };
75
+
76
+ class GenericRSMetadataError : public ErrorInfo <GenericRSMetadataError> {
77
+ public:
78
+ static char ID;
79
+ std::string Message;
80
+
81
+ GenericRSMetadataError (Twine Message) : Message(Message.str()) {}
82
+
83
+ void log (raw_ostream &OS) const override { OS << Message; }
84
+
85
+ std::error_code convertToErrorCode () const override {
86
+ return llvm::inconvertibleErrorCode ();
87
+ }
88
+ };
89
+
90
+ class InvalidRSMetadataFormat : public ErrorInfo <InvalidRSMetadataFormat> {
91
+ public:
92
+ static char ID;
93
+ std::string ElementName;
94
+
95
+ InvalidRSMetadataFormat (StringRef ElementName)
96
+ : ElementName(ElementName.str()) {}
97
+
98
+ void log (raw_ostream &OS) const override {
99
+ OS << " Invalid format for " << ElementName;
100
+ }
101
+
102
+ std::error_code convertToErrorCode () const override {
103
+ return llvm::inconvertibleErrorCode ();
104
+ }
105
+ };
106
+
107
+ class InvalidRSMetadataValue : public ErrorInfo <InvalidRSMetadataValue> {
108
+ public:
109
+ static char ID;
110
+ std::string ParamName;
111
+
112
+ InvalidRSMetadataValue (StringRef ParamName) : ParamName(ParamName.str()) {}
113
+
114
+ void log (raw_ostream &OS) const override {
115
+ OS << " Invalid value for " << ParamName;
116
+ }
117
+
118
+ std::error_code convertToErrorCode () const override {
119
+ return llvm::inconvertibleErrorCode ();
120
+ }
121
+ };
122
+
31
123
class MetadataBuilder {
32
124
public:
33
125
MetadataBuilder (llvm::LLVMContext &Ctx, ArrayRef<RootElement> Elements)
@@ -66,22 +158,32 @@ enum class RootSignatureElementKind {
66
158
67
159
class MetadataParser {
68
160
public:
69
- using MapT = SmallDenseMap<const Function *, llvm::mcdxbc::RootSignatureDesc>;
70
- MetadataParser (llvm::LLVMContext &Ctx, MDNode* Root): Ctx(Ctx), Root(Root) {}
161
+ MetadataParser (MDNode *Root) : Root(Root) {}
71
162
72
163
// / Iterates through root signature and converts them into MapT
73
- LLVM_ABI llvm::Expected<MapT*> ParseRootSignature ();
164
+ LLVM_ABI llvm::Expected<llvm::mcdxbc::RootSignatureDesc>
165
+ ParseRootSignature (uint32_t Version);
74
166
75
167
private:
76
- llvm::Error parseRootFlags (LLVMContext *Ctx, mcdxbc::RootSignatureDesc &RSD, MDNode *RootFlagNode);
77
- llvm::Error parseRootConstants (LLVMContext *Ctx, mcdxbc::RootSignatureDesc &RSD, MDNode *RootConstantNode);
78
- llvm::Error parseRootDescriptors (LLVMContext *Ctx, mcdxbc::RootSignatureDesc &RSD, MDNode *RootDescriptorNode, RootSignatureElementKind ElementKind);
79
- llvm::Error parseDescriptorRange (LLVMContext *Ctx, mcdxbc::DescriptorTable &Table, MDNode *RangeDescriptorNode);
80
- llvm::Error parseDescriptorTable (LLVMContext *Ctx, mcdxbc::RootSignatureDesc &RSD, MDNode *DescriptorTableNode);
81
- llvm::Error parseRootSignatureElement (LLVMContext *Ctx, mcdxbc::RootSignatureDesc &RSD, MDNode *Element);
82
- llvm::Error parseStaticSampler (LLVMContext *Ctx, mcdxbc::RootSignatureDesc &RSD, MDNode *StaticSamplerNode);
83
- llvm::LLVMContext &Ctx;
84
- MDNode* Root;
168
+ llvm::Error parseRootFlags (mcdxbc::RootSignatureDesc &RSD,
169
+ MDNode *RootFlagNode);
170
+ llvm::Error parseRootConstants (mcdxbc::RootSignatureDesc &RSD,
171
+ MDNode *RootConstantNode);
172
+ llvm::Error parseRootDescriptors (mcdxbc::RootSignatureDesc &RSD,
173
+ MDNode *RootDescriptorNode,
174
+ RootSignatureElementKind ElementKind);
175
+ llvm::Error parseDescriptorRange (mcdxbc::DescriptorTable &Table,
176
+ MDNode *RangeDescriptorNode);
177
+ llvm::Error parseDescriptorTable (mcdxbc::RootSignatureDesc &RSD,
178
+ MDNode *DescriptorTableNode);
179
+ llvm::Error parseRootSignatureElement (mcdxbc::RootSignatureDesc &RSD,
180
+ MDNode *Element);
181
+ llvm::Error parseStaticSampler (mcdxbc::RootSignatureDesc &RSD,
182
+ MDNode *StaticSamplerNode);
183
+
184
+ llvm::Error validateRootSignature (const llvm::mcdxbc::RootSignatureDesc &RSD);
185
+
186
+ MDNode *Root;
85
187
};
86
188
87
189
} // namespace rootsig
0 commit comments