1515#include " llvm/ADT/StringSwitch.h"
1616#include " llvm/ADT/Twine.h"
1717#include " llvm/Analysis/DXILMetadataAnalysis.h"
18+ #include " llvm/BinaryFormat/DXContainer.h"
1819#include " llvm/IR/Constants.h"
20+ #include " llvm/IR/DiagnosticInfo.h"
1921#include " llvm/IR/Function.h"
22+ #include " llvm/IR/LLVMContext.h"
2023#include " llvm/IR/Module.h"
2124#include " llvm/InitializePasses.h"
2225#include " llvm/Pass.h"
26+ #include " llvm/Support/Error.h"
27+ #include < optional>
2328
2429using namespace llvm ;
2530using namespace llvm ::dxil;
2631
27- static bool reportError (Twine Message) {
28- report_fatal_error (Message, false );
32+ bool ModuleRootSignature::reportError (Twine Message,
33+ DiagnosticSeverity Severity) {
34+ Ctx->diagnose (DiagnosticInfoGeneric (Message, Severity));
2935 return true ;
3036}
3137
@@ -130,43 +136,33 @@ bool ModuleRootSignature::parse(NamedMDNode *Root, const Function *EF) {
130136 return HasError;
131137}
132138
133- bool ModuleRootSignature::validateRootFlag () {
134- // Root Element validation, as specified:
135- // https://github.com/llvm/wg-hlsl/blob/main/proposals/0002-root-signature-in-clang.md#validations-during-dxil-generation
136- if ((Flags & ~0x80000fff ) != 0 )
137- return reportError (" Invalid flag value for RootFlag" );
138-
139- return false ;
140- }
141-
142139bool ModuleRootSignature::validate () {
143- if (validateRootFlag ())
140+ if (dxbc::RootSignatureValidations:: validateRootFlag (Flags)) {
144141 return reportError (" Invalid flag value for RootFlag" );
145-
142+ }
146143 return false ;
147144}
148145
149- ModuleRootSignature ModuleRootSignature::analyzeModule (Module &M,
150- const Function *F) {
151- ModuleRootSignature MRS;
146+ OptionalRootSignature ModuleRootSignature::analyzeModule (Module &M,
147+ const Function *F) {
148+ ModuleRootSignature MRS (&M. getContext ()) ;
152149
153150 NamedMDNode *RootSignatureNode = M.getNamedMetadata (" dx.rootsignatures" );
154- if (RootSignatureNode) {
155- if (MRS.parse (RootSignatureNode, F) || MRS.validate ())
156- llvm_unreachable (" Invalid Root Signature Metadata." );
157- }
151+ if (RootSignatureNode == nullptr || MRS.parse (RootSignatureNode, F) ||
152+ MRS.validate ())
153+ return std::nullopt ;
158154
159155 return MRS;
160156}
161157
162158AnalysisKey RootSignatureAnalysis::Key;
163159
164- ModuleRootSignature RootSignatureAnalysis::run (Module &M,
165- ModuleAnalysisManager &AM) {
160+ OptionalRootSignature RootSignatureAnalysis::run (Module &M,
161+ ModuleAnalysisManager &AM) {
166162 auto MMI = AM.getResult <DXILMetadataAnalysis>(M);
167163
168164 if (MMI.ShaderProfile == Triple::Library)
169- return ModuleRootSignature () ;
165+ return std:: nullopt ;
170166
171167 assert (MMI.EntryPropertyVec .size () == 1 );
172168
@@ -186,7 +182,6 @@ bool RootSignatureAnalysisWrapper::runOnModule(Module &M) {
186182
187183 const Function *EntryFunction = MMI.EntryPropertyVec [0 ].Entry ;
188184 MRS = ModuleRootSignature::analyzeModule (M, EntryFunction);
189-
190185 return false ;
191186}
192187
0 commit comments