2020#include " llvm/IR/DiagnosticInfo.h"
2121#include " llvm/IR/Function.h"
2222#include " llvm/IR/LLVMContext.h"
23+ #include " llvm/IR/Metadata.h"
2324#include " llvm/IR/Module.h"
2425#include " llvm/InitializePasses.h"
2526#include " llvm/Pass.h"
@@ -63,9 +64,6 @@ static bool parseRootSignatureElement(LLVMContext *Ctx,
6364
6465 case RootSignatureElementKind::RootFlags:
6566 return parseRootFlags (Ctx, MRS, Element);
66- default :
67- return reportError (Ctx,
68- " Invalid Root Element: " + ElementText->getString ());
6967 }
7068
7169 return true ;
@@ -95,8 +93,14 @@ static bool parse(LLVMContext *Ctx, ModuleRootSignature *MRS, NamedMDNode *Root,
9593 continue ;
9694 }
9795
96+ const MDOperand &FunctionPointerMdNode = Node->getOperand (0 );
97+ if (FunctionPointerMdNode == nullptr ) {
98+ // Function was pruned during compilation.
99+ continue ;
100+ }
101+
98102 ValueAsMetadata *VAM =
99- llvm::dyn_cast<ValueAsMetadata>(Node-> getOperand ( 0 ) .get ());
103+ llvm::dyn_cast<ValueAsMetadata>(FunctionPointerMdNode .get ());
100104 if (VAM == nullptr ) {
101105 HasError =
102106 reportError (Ctx, " First element of root signature is not a value" );
@@ -140,24 +144,26 @@ static bool validate(LLVMContext *Ctx, ModuleRootSignature *MRS) {
140144 return false ;
141145}
142146
143- std::optional<ModuleRootSignature>
144- ModuleRootSignature::analyzeModule (Module &M, ModuleMetadataInfo MMI) {
145- if (MMI.ShaderProfile == Triple::Library)
146- return std::nullopt ;
147+ static const Function *getEntryFunction (Module &M, ModuleMetadataInfo MMI) {
147148
148149 LLVMContext *Ctx = &M.getContext ();
149-
150150 if (MMI.EntryPropertyVec .size () != 1 ) {
151151 reportError (Ctx, " More than one entry function defined." );
152- return std:: nullopt ;
152+ return nullptr ;
153153 }
154+ return MMI.EntryPropertyVec [0 ].Entry ;
155+ }
156+
157+ std::optional<ModuleRootSignature>
158+ ModuleRootSignature::analyzeModule (Module &M, const Function *F) {
159+
160+ LLVMContext *Ctx = &M.getContext ();
154161
155162 ModuleRootSignature MRS;
156- const Function *EntryFunction = MMI.EntryPropertyVec [0 ].Entry ;
157163
158164 NamedMDNode *RootSignatureNode = M.getNamedMetadata (" dx.rootsignatures" );
159- if (RootSignatureNode == nullptr ||
160- parse (Ctx, &MRS, RootSignatureNode, EntryFunction) || validate (Ctx, &MRS))
165+ if (RootSignatureNode == nullptr || parse (Ctx, &MRS, RootSignatureNode, F) ||
166+ validate (Ctx, &MRS))
161167 return std::nullopt ;
162168
163169 return MRS;
@@ -168,15 +174,18 @@ AnalysisKey RootSignatureAnalysis::Key;
168174std::optional<ModuleRootSignature>
169175RootSignatureAnalysis::run (Module &M, ModuleAnalysisManager &AM) {
170176 ModuleMetadataInfo MMI = AM.getResult <DXILMetadataAnalysis>(M);
171- return ModuleRootSignature::analyzeModule (M, MMI);
177+ if (MMI.ShaderProfile == Triple::Library)
178+ return std::nullopt ;
179+ return ModuleRootSignature::analyzeModule (M, getEntryFunction (M, MMI));
172180}
173181
174182// ===----------------------------------------------------------------------===//
175183bool RootSignatureAnalysisWrapper::runOnModule (Module &M) {
176-
177184 dxil::ModuleMetadataInfo &MMI =
178185 getAnalysis<DXILMetadataAnalysisWrapperPass>().getModuleMetadata ();
179- MRS = ModuleRootSignature::analyzeModule (M, MMI);
186+ if (MMI.ShaderProfile == Triple::Library)
187+ return false ;
188+ MRS = ModuleRootSignature::analyzeModule (M, getEntryFunction (M, MMI));
180189 return false ;
181190}
182191
0 commit comments