3131using namespace llvm ;
3232using namespace hlsl ;
3333
34- // Utility class for setting and restoring the diagnostic context so we may
35- // capture errors/warnings
36- struct DiagRestore {
37- LLVMContext &Ctx;
38- void *OrigDiagContext;
39- LLVMContext::DiagnosticHandlerTy OrigHandler;
40-
41- DiagRestore (llvm::LLVMContext &Ctx, void *DiagContext) : Ctx(Ctx) {
42- OrigHandler = Ctx.getDiagnosticHandler ();
43- OrigDiagContext = Ctx.getDiagnosticContext ();
44- Ctx.setDiagnosticHandler (PrintDiagnosticContext::PrintDiagnosticHandler,
45- DiagContext);
46- }
47- ~DiagRestore () { Ctx.setDiagnosticHandler (OrigHandler, OrigDiagContext); }
48- };
49-
5034static uint32_t runValidation (
5135 IDxcBlob *Shader,
5236 uint32_t Flags, // Validation flags.
53- llvm::Module *Module, // Module to validate, if available.
5437 llvm::Module *DebugModule, // Debug module to validate, if available
5538 AbstractMemoryStream *DiagMemStream) {
56-
5739 // Run validation may throw, but that indicates an inability to validate,
5840 // not that the validation failed (eg out of memory). That is indicated
5941 // by a failing HRESULT, and possibly error messages in the diagnostics
6042 // stream.
6143
6244 raw_stream_ostream DiagStream (DiagMemStream);
6345
64- if (Flags & DxcValidatorFlags_ModuleOnly) {
65- if (IsDxilContainerLike (Shader->GetBufferPointer (),
66- Shader->GetBufferSize ()))
67- return E_INVALIDARG;
68- } else {
69- if (!IsDxilContainerLike (Shader->GetBufferPointer (),
70- Shader->GetBufferSize ()))
71- return DXC_E_CONTAINER_INVALID;
72- }
73-
74- if (!Module) {
75- DXASSERT_NOMSG (DebugModule == nullptr );
76- if (Flags & DxcValidatorFlags_ModuleOnly) {
77- return ValidateDxilBitcode ((const char *)Shader->GetBufferPointer (),
78- (uint32_t )Shader->GetBufferSize (), DiagStream);
79- } else {
80- return ValidateDxilContainer (Shader->GetBufferPointer (),
81- Shader->GetBufferSize (), DiagStream);
82- }
83- }
84-
85- llvm::DiagnosticPrinterRawOStream DiagPrinter (DiagStream);
86- PrintDiagnosticContext DiagContext (DiagPrinter);
87- DiagRestore DR (Module->getContext (), &DiagContext);
88-
89- HRESULT hr = hlsl::ValidateDxilModule (Module, DebugModule);
90- if (FAILED (hr))
91- return hr;
92- if (!(Flags & DxcValidatorFlags_ModuleOnly)) {
93- hr = ValidateDxilContainerParts (
94- Module, DebugModule,
95- IsDxilContainerLike (Shader->GetBufferPointer (),
96- Shader->GetBufferSize ()),
97- (uint32_t )Shader->GetBufferSize ());
98- if (FAILED (hr))
99- return hr;
100- }
101-
102- if (DiagContext.HasErrors () || DiagContext.HasWarnings ()) {
103- return DXC_E_IR_VERIFICATION_FAILED;
104- }
105-
106- return S_OK;
46+ return ValidateDxilContainer (Shader->GetBufferPointer (),
47+ Shader->GetBufferSize (), DebugModule,
48+ DiagStream);
10749}
10850
10951static uint32_t
@@ -151,23 +93,14 @@ runRootSignatureValidation(IDxcBlob *Shader,
15193 return S_OK;
15294}
15395
154- // Compile a single entry point to the target shader model
155- uint32_t hlsl::validate (
156- IDxcBlob *Shader, // Shader to validate.
157- uint32_t Flags, // Validation flags.
158- IDxcOperationResult **Result // Validation output status, buffer, and errors
159- ) {
160- DxcThreadMalloc TM (DxcGetThreadMallocNoRef ());
161- if (Result == nullptr )
162- return false ;
163- *Result = nullptr ;
164- if (Shader == nullptr || Flags & ~DxcValidatorFlags_ValidMask)
165- return false ;
166- if ((Flags & DxcValidatorFlags_ModuleOnly) &&
167- (Flags &
168- (DxcValidatorFlags_InPlaceEdit | DxcValidatorFlags_RootSignatureOnly)))
169- return false ;
170- return validateWithOptModules (Shader, Flags, nullptr , nullptr , Result);
96+ static uint32_t runDxilModuleValidation (IDxcBlob *Shader, // Shader to validate.
97+ AbstractMemoryStream *DiagMemStream) {
98+ if (IsDxilContainerLike (Shader->GetBufferPointer (), Shader->GetBufferSize ()))
99+ return E_INVALIDARG;
100+
101+ raw_stream_ostream DiagStream (DiagMemStream);
102+ return ValidateDxilBitcode ((const char *)Shader->GetBufferPointer (),
103+ (uint32_t )Shader->GetBufferSize (), DiagStream);
171104}
172105
173106uint32_t hlsl::validateWithDebug (
@@ -212,17 +145,15 @@ uint32_t hlsl::validateWithDebug(
212145 if (FAILED (hr))
213146 throw hlsl::Exception (hr);
214147 }
215- return validateWithOptModules (Shader, Flags, nullptr , DebugModule.get (),
216- Result);
148+ return validateWithOptDebugModule (Shader, Flags, DebugModule.get (), Result);
217149 }
218150 CATCH_CPP_ASSIGN_HRESULT ();
219151 return hr;
220152}
221153
222- uint32_t hlsl::validateWithOptModules (
154+ uint32_t hlsl::validateWithOptDebugModule (
223155 IDxcBlob *Shader, // Shader to validate.
224156 uint32_t Flags, // Validation flags.
225- llvm::Module *Module, // Module to validate, if available.
226157 llvm::Module *DebugModule, // Debug module to validate, if available
227158 IDxcOperationResult **Result // Validation output status, buffer, and errors
228159) {
@@ -238,12 +169,12 @@ uint32_t hlsl::validateWithOptModules(
238169 throw hlsl::Exception (hr);
239170 // Run validation may throw, but that indicates an inability to validate,
240171 // not that the validation failed (eg out of memory).
241- if (Flags & DxcValidatorFlags_RootSignatureOnly) {
172+ if (Flags & DxcValidatorFlags_RootSignatureOnly)
242173 validationStatus = runRootSignatureValidation (Shader, DiagStream);
243- } else {
244- validationStatus =
245- runValidation (Shader, Flags, Module, DebugModule, DiagStream);
246- }
174+ else if (Flags & DxcValidatorFlags_ModuleOnly)
175+ validationStatus = runDxilModuleValidation (Shader, DiagStream);
176+ else
177+ validationStatus = runValidation (Shader, Flags, DebugModule, DiagStream);
247178 if (FAILED (validationStatus)) {
248179 std::string msg (" Validation failed.\n " );
249180 ULONG cbWritten;
0 commit comments