2626#include " llvm/Pass.h"
2727#include " llvm/Support/Error.h"
2828#include " llvm/Support/ErrorHandling.h"
29+ #include " llvm/Support/raw_ostream.h"
2930#include < cstdint>
3031#include < optional>
3132#include < utility>
@@ -39,20 +40,20 @@ static bool reportError(LLVMContext *Ctx, Twine Message,
3940 return true ;
4041}
4142
42- static bool parseRootFlags (LLVMContext *Ctx, ModuleRootSignature &MRS ,
43+ static bool parseRootFlags (LLVMContext *Ctx, mcdxbc::RootSignatureDesc &RSD ,
4344 MDNode *RootFlagNode) {
4445
4546 if (RootFlagNode->getNumOperands () != 2 )
4647 return reportError (Ctx, " Invalid format for RootFlag Element" );
4748
4849 auto *Flag = mdconst::extract<ConstantInt>(RootFlagNode->getOperand (1 ));
49- MRS .Flags = Flag->getZExtValue ();
50+ RSD .Flags = Flag->getZExtValue ();
5051
5152 return false ;
5253}
5354
5455static bool parseRootSignatureElement (LLVMContext *Ctx,
55- ModuleRootSignature &MRS ,
56+ mcdxbc::RootSignatureDesc &RSD ,
5657 MDNode *Element) {
5758 MDString *ElementText = cast<MDString>(Element->getOperand (0 ));
5859 if (ElementText == nullptr )
@@ -61,21 +62,22 @@ static bool parseRootSignatureElement(LLVMContext *Ctx,
6162 RootSignatureElementKind ElementKind =
6263 StringSwitch<RootSignatureElementKind>(ElementText->getString ())
6364 .Case (" RootFlags" , RootSignatureElementKind::RootFlags)
64- .Default (RootSignatureElementKind::None );
65+ .Default (RootSignatureElementKind::Error );
6566
6667 switch (ElementKind) {
6768
6869 case RootSignatureElementKind::RootFlags:
69- return parseRootFlags (Ctx, MRS , Element);
70- case RootSignatureElementKind::None :
70+ return parseRootFlags (Ctx, RSD , Element);
71+ case RootSignatureElementKind::Error :
7172 return reportError (Ctx, " Invalid Root Signature Element: " +
7273 ElementText->getString ());
7374 }
7475
75- llvm_unreachable (" Root signature element kind not expected ." );
76+ llvm_unreachable (" Unhandled RootSignatureElementKind enum ." );
7677}
7778
78- static bool parse (LLVMContext *Ctx, ModuleRootSignature &MRS, MDNode *Node) {
79+ static bool parse (LLVMContext *Ctx, mcdxbc::RootSignatureDesc &RSD,
80+ MDNode *Node) {
7981 bool HasError = false ;
8082
8183 // Loop through the Root Elements of the root signature.
@@ -84,20 +86,20 @@ static bool parse(LLVMContext *Ctx, ModuleRootSignature &MRS, MDNode *Node) {
8486 if (Element == nullptr )
8587 return reportError (Ctx, " Missing Root Element Metadata Node." );
8688
87- HasError = HasError || parseRootSignatureElement (Ctx, MRS , Element);
89+ HasError = HasError || parseRootSignatureElement (Ctx, RSD , Element);
8890 }
8991
9092 return HasError;
9193}
9294
93- static bool validate (LLVMContext *Ctx, const ModuleRootSignature &MRS ) {
94- if (!dxbc::RootSignatureValidations::isValidRootFlag (MRS .Flags )) {
95+ static bool validate (LLVMContext *Ctx, const mcdxbc::RootSignatureDesc &RSD ) {
96+ if (!dxbc::RootSignatureValidations::isValidRootFlag (RSD .Flags )) {
9597 return reportError (Ctx, " Invalid Root Signature flag value" );
9698 }
9799 return false ;
98100}
99101
100- static SmallDenseMap<const Function *, ModuleRootSignature >
102+ static SmallDenseMap<const Function *, mcdxbc::RootSignatureDesc >
101103analyzeModule (Module &M) {
102104
103105 /* * Root Signature are specified as following in the metadata:
@@ -114,11 +116,11 @@ analyzeModule(Module &M) {
114116
115117 LLVMContext *Ctx = &M.getContext ();
116118
117- SmallDenseMap<const Function *, ModuleRootSignature> MRSMap ;
119+ SmallDenseMap<const Function *, mcdxbc::RootSignatureDesc> RSDMap ;
118120
119121 NamedMDNode *RootSignatureNode = M.getNamedMetadata (" dx.rootsignatures" );
120122 if (RootSignatureNode == nullptr )
121- return MRSMap ;
123+ return RSDMap ;
122124
123125 for (const auto &RSDefNode : RootSignatureNode->operands ()) {
124126 if (RSDefNode->getNumOperands () != 2 ) {
@@ -153,49 +155,50 @@ analyzeModule(Module &M) {
153155 reportError (Ctx, " Missing Root Element List Metadata node." );
154156 }
155157
156- ModuleRootSignature MRS ;
158+ mcdxbc::RootSignatureDesc RSD ;
157159
158- if (parse (Ctx, MRS , RootElementListNode) || validate (Ctx, MRS )) {
159- return MRSMap ;
160+ if (parse (Ctx, RSD , RootElementListNode) || validate (Ctx, RSD )) {
161+ return RSDMap ;
160162 }
161163
162- MRSMap .insert (std::make_pair (F, MRS ));
164+ RSDMap .insert (std::make_pair (F, RSD ));
163165 }
164166
165- return MRSMap ;
167+ return RSDMap ;
166168}
167169
168170AnalysisKey RootSignatureAnalysis::Key;
169171
170- SmallDenseMap<const Function *, ModuleRootSignature >
172+ SmallDenseMap<const Function *, mcdxbc::RootSignatureDesc >
171173RootSignatureAnalysis::run (Module &M, ModuleAnalysisManager &AM) {
172174 return analyzeModule (M);
173175}
174176
175177// ===----------------------------------------------------------------------===//
176178
177- static void printSpaces (raw_ostream &Stream, unsigned int Count) {
178- for (unsigned int I = 0 ; I < Count; ++I) {
179- Stream << ' ' ;
180- }
181- }
182-
183179PreservedAnalyses RootSignatureAnalysisPrinter::run (Module &M,
184180 ModuleAnalysisManager &AM) {
185181
186- SmallDenseMap<const Function *, ModuleRootSignature > &MRSMap =
182+ SmallDenseMap<const Function *, mcdxbc::RootSignatureDesc > &RSDMap =
187183 AM.getResult <RootSignatureAnalysis>(M);
188184 OS << " Root Signature Definitions"
189185 << " \n " ;
190186 uint8_t Space = 0 ;
191- for (const auto &P : MRSMap ) {
192- const auto &[Function, MRS ] = P;
187+ for (const auto &P : RSDMap ) {
188+ const auto &[Function, RSD ] = P;
193189 OS << " Definition for '" << Function->getName () << " ':\n " ;
194190
195191 // start root signature header
196192 Space++;
197- printSpaces (OS, Space);
198- OS << " Flags: " << format_hex (MRS.Flags , 8 ) << " :\n " ;
193+ OS << indent (Space) << " Flags: " << format_hex (RSD.Flags , 8 ) << " :\n " ;
194+ OS << indent (Space) << " Version: " << RSD.Version << " :\n " ;
195+ OS << indent (Space) << " NumParameters: " << RSD.NumParameters << " :\n " ;
196+ OS << indent (Space) << " RootParametersOffset: " << RSD.RootParametersOffset
197+ << " :\n " ;
198+ OS << indent (Space) << " NumStaticSamplers: " << RSD.NumStaticSamplers
199+ << " :\n " ;
200+ OS << indent (Space) << " StaticSamplersOffset: " << RSD.StaticSamplersOffset
201+ << " :\n " ;
199202 Space--;
200203 // end root signature header
201204 }
@@ -205,7 +208,7 @@ PreservedAnalyses RootSignatureAnalysisPrinter::run(Module &M,
205208
206209// ===----------------------------------------------------------------------===//
207210bool RootSignatureAnalysisWrapper::runOnModule (Module &M) {
208- MRS = analyzeModule (M);
211+ FuncToRsMap = analyzeModule (M);
209212 return false ;
210213}
211214
0 commit comments