99// //
1010// /////////////////////////////////////////////////////////////////////////////
1111
12- #include " llvm/Bitcode/ReaderWriter.h"
13- #include " llvm/IR/DiagnosticPrinter.h"
14- #include " llvm/IR/LLVMContext.h"
12+ #include " dxcvalidator.h"
1513
16- #include " dxc/DxilContainer/DxilContainer.h"
17- #include " dxc/HLSL/DxilValidation.h"
1814#include " dxc/Support/WinIncludes.h"
1915
20- #include " dxc/DxilRootSignature/DxilRootSignature.h"
21- #include " dxc/Support/FileIOHelper.h"
2216#include " dxc/Support/Global.h"
23- #include " dxc/Support/dxcapi.impl.h"
2417#include " dxc/Support/microcom.h"
25- #include " llvm/Support/FileSystem.h"
26- #include " llvm/Support/MSFileSystem.h"
27- #include " llvm/Support/MemoryBuffer.h"
28-
29- #ifdef _WIN32
30- #include " dxcetw.h"
31- #endif
18+ #include " dxc/dxcapi.h"
3219
3320#ifdef SUPPORT_QUERY_GIT_COMMIT_INFO
3421#include " clang/Basic/Version.h"
3724using namespace llvm ;
3825using namespace hlsl ;
3926
40- // Utility class for setting and restoring the diagnostic context so we may
41- // capture errors/warnings
42- struct DiagRestore {
43- LLVMContext &Ctx;
44- void *OrigDiagContext;
45- LLVMContext::DiagnosticHandlerTy OrigHandler;
46-
47- DiagRestore (llvm::LLVMContext &Ctx, void *DiagContext) : Ctx(Ctx) {
48- OrigHandler = Ctx.getDiagnosticHandler ();
49- OrigDiagContext = Ctx.getDiagnosticContext ();
50- Ctx.setDiagnosticHandler (PrintDiagnosticContext::PrintDiagnosticHandler,
51- DiagContext);
52- }
53- ~DiagRestore () { Ctx.setDiagnosticHandler (OrigHandler, OrigDiagContext); }
54- };
55-
5627class DxcValidator : public IDxcValidator2 ,
5728#ifdef SUPPORT_QUERY_GIT_COMMIT_INFO
5829 public IDxcVersionInfo2
@@ -63,16 +34,6 @@ class DxcValidator : public IDxcValidator2,
6334private:
6435 DXC_MICROCOM_TM_REF_FIELDS ()
6536
66- HRESULT RunValidation (
67- IDxcBlob *pShader, // Shader to validate.
68- UINT32 Flags, // Validation flags.
69- llvm::Module *pModule, // Module to validate, if available.
70- llvm::Module *pDebugModule, // Debug module to validate, if available
71- AbstractMemoryStream *pDiagStream);
72-
73- HRESULT RunRootSignatureValidation (IDxcBlob *pShader, // Shader to validate.
74- AbstractMemoryStream *pDiagStream);
75-
7637public:
7738 DXC_MICROCOM_TM_ADDREF_RELEASE_IMPL ()
7839 DXC_MICROCOM_TM_CTOR (DxcValidator)
@@ -129,17 +90,7 @@ HRESULT STDMETHODCALLTYPE DxcValidator::Validate(
12990 IDxcOperationResult *
13091 *ppResult // Validation output status, buffer, and errors
13192) {
132- DxcThreadMalloc TM (m_pMalloc);
133- if (ppResult == nullptr )
134- return E_INVALIDARG;
135- *ppResult = nullptr ;
136- if (pShader == nullptr || Flags & ~DxcValidatorFlags_ValidMask)
137- return E_INVALIDARG;
138- if ((Flags & DxcValidatorFlags_ModuleOnly) &&
139- (Flags &
140- (DxcValidatorFlags_InPlaceEdit | DxcValidatorFlags_RootSignatureOnly)))
141- return E_INVALIDARG;
142- return ValidateWithOptModules (pShader, Flags, nullptr , nullptr , ppResult);
93+ return hlsl::validate (pShader, Flags, ppResult);
14394}
14495
14596HRESULT STDMETHODCALLTYPE DxcValidator::ValidateWithDebug (
@@ -150,42 +101,7 @@ HRESULT STDMETHODCALLTYPE DxcValidator::ValidateWithDebug(
150101 IDxcOperationResult *
151102 *ppResult // Validation output status, buffer, and errors
152103) {
153- if (ppResult == nullptr )
154- return E_INVALIDARG;
155- *ppResult = nullptr ;
156- if (pShader == nullptr || Flags & ~DxcValidatorFlags_ValidMask)
157- return E_INVALIDARG;
158- if ((Flags & DxcValidatorFlags_ModuleOnly) &&
159- (Flags &
160- (DxcValidatorFlags_InPlaceEdit | DxcValidatorFlags_RootSignatureOnly)))
161- return E_INVALIDARG;
162- if (pOptDebugBitcode &&
163- (pOptDebugBitcode->Ptr == nullptr || pOptDebugBitcode->Size == 0 ||
164- pOptDebugBitcode->Size >= UINT32_MAX))
165- return E_INVALIDARG;
166-
167- HRESULT hr = S_OK;
168- DxcThreadMalloc TM (m_pMalloc);
169- try {
170- LLVMContext Ctx;
171- CComPtr<AbstractMemoryStream> pDiagStream;
172- IFT (CreateMemoryStream (m_pMalloc, &pDiagStream));
173- raw_stream_ostream DiagStream (pDiagStream);
174- llvm::DiagnosticPrinterRawOStream DiagPrinter (DiagStream);
175- PrintDiagnosticContext DiagContext (DiagPrinter);
176- Ctx.setDiagnosticHandler (PrintDiagnosticContext::PrintDiagnosticHandler,
177- &DiagContext, true );
178- std::unique_ptr<llvm::Module> pDebugModule;
179- if (pOptDebugBitcode) {
180- IFT (ValidateLoadModule ((const char *)pOptDebugBitcode->Ptr ,
181- (uint32_t )pOptDebugBitcode->Size , pDebugModule,
182- Ctx, DiagStream, /* bLazyLoad*/ false ));
183- }
184- return ValidateWithOptModules (pShader, Flags, nullptr , pDebugModule.get (),
185- ppResult);
186- }
187- CATCH_CPP_ASSIGN_HRESULT ();
188- return hr;
104+ return hlsl::validateWithDebug (pShader, Flags, pOptDebugBitcode, ppResult);
189105}
190106
191107HRESULT DxcValidator::ValidateWithOptModules (
@@ -196,51 +112,13 @@ HRESULT DxcValidator::ValidateWithOptModules(
196112 IDxcOperationResult *
197113 *ppResult // Validation output status, buffer, and errors
198114) {
199- *ppResult = nullptr ;
200- HRESULT hr = S_OK;
201- HRESULT validationStatus = S_OK;
202- DxcEtw_DxcValidation_Start ();
203- DxcThreadMalloc TM (m_pMalloc);
204- try {
205- CComPtr<AbstractMemoryStream> pDiagStream;
206- IFT (CreateMemoryStream (m_pMalloc, &pDiagStream));
207-
208- // Run validation may throw, but that indicates an inability to validate,
209- // not that the validation failed (eg out of memory).
210- if (Flags & DxcValidatorFlags_RootSignatureOnly) {
211- validationStatus = RunRootSignatureValidation (pShader, pDiagStream);
212- } else {
213- validationStatus =
214- RunValidation (pShader, Flags, pModule, pDebugModule, pDiagStream);
215- }
216- if (FAILED (validationStatus)) {
217- std::string msg (" Validation failed.\n " );
218- ULONG cbWritten;
219- pDiagStream->Write (msg.c_str (), msg.size (), &cbWritten);
220- }
221- // Assemble the result object.
222- CComPtr<IDxcBlob> pDiagBlob;
223- hr = pDiagStream.QueryInterface (&pDiagBlob);
224- DXASSERT_NOMSG (SUCCEEDED (hr));
225- IFT (DxcResult::Create (
226- validationStatus, DXC_OUT_NONE,
227- {DxcOutputObject::ErrorOutput (
228- CP_UTF8, // TODO Support DefaultTextCodePage
229- (LPCSTR)pDiagBlob->GetBufferPointer (), pDiagBlob->GetBufferSize ())},
230- ppResult));
231- }
232- CATCH_CPP_ASSIGN_HRESULT ();
233-
234- DxcEtw_DxcValidation_Stop (SUCCEEDED (hr) ? validationStatus : hr);
235- return hr;
115+ return hlsl::validateWithOptModules (pShader, Flags, pModule, pDebugModule,
116+ ppResult);
236117}
237118
238119HRESULT STDMETHODCALLTYPE DxcValidator::GetVersion (UINT32 *pMajor,
239120 UINT32 *pMinor) {
240- if (pMajor == nullptr || pMinor == nullptr )
241- return E_INVALIDARG;
242- GetValidationVersion (pMajor, pMinor);
243- return S_OK;
121+ return hlsl::getValidationVersion (pMajor, pMinor);
244122}
245123
246124#ifdef SUPPORT_QUERY_GIT_COMMIT_INFO
@@ -273,106 +151,6 @@ HRESULT STDMETHODCALLTYPE DxcValidator::GetFlags(UINT32 *pFlags) {
273151 return S_OK;
274152}
275153
276- HRESULT DxcValidator::RunValidation (
277- IDxcBlob *pShader,
278- UINT32 Flags, // Validation flags.
279- llvm::Module *pModule, // Module to validate, if available.
280- llvm::Module *pDebugModule, // Debug module to validate, if available
281- AbstractMemoryStream *pDiagStream) {
282-
283- // Run validation may throw, but that indicates an inability to validate,
284- // not that the validation failed (eg out of memory). That is indicated
285- // by a failing HRESULT, and possibly error messages in the diagnostics
286- // stream.
287-
288- raw_stream_ostream DiagStream (pDiagStream);
289-
290- if (Flags & DxcValidatorFlags_ModuleOnly) {
291- IFRBOOL (!IsDxilContainerLike (pShader->GetBufferPointer (),
292- pShader->GetBufferSize ()),
293- E_INVALIDARG);
294- } else {
295- IFRBOOL (IsDxilContainerLike (pShader->GetBufferPointer (),
296- pShader->GetBufferSize ()),
297- DXC_E_CONTAINER_INVALID);
298- }
299-
300- if (!pModule) {
301- DXASSERT_NOMSG (pDebugModule == nullptr );
302- if (Flags & DxcValidatorFlags_ModuleOnly) {
303- return ValidateDxilBitcode ((const char *)pShader->GetBufferPointer (),
304- (uint32_t )pShader->GetBufferSize (),
305- DiagStream);
306- } else {
307- return ValidateDxilContainer (pShader->GetBufferPointer (),
308- pShader->GetBufferSize (), DiagStream);
309- }
310- }
311-
312- llvm::DiagnosticPrinterRawOStream DiagPrinter (DiagStream);
313- PrintDiagnosticContext DiagContext (DiagPrinter);
314- DiagRestore DR (pModule->getContext (), &DiagContext);
315-
316- IFR (hlsl::ValidateDxilModule (pModule, pDebugModule));
317- if (!(Flags & DxcValidatorFlags_ModuleOnly)) {
318- IFR (ValidateDxilContainerParts (
319- pModule, pDebugModule,
320- IsDxilContainerLike (pShader->GetBufferPointer (),
321- pShader->GetBufferSize ()),
322- (uint32_t )pShader->GetBufferSize ()));
323- }
324-
325- if (DiagContext.HasErrors () || DiagContext.HasWarnings ()) {
326- return DXC_E_IR_VERIFICATION_FAILED;
327- }
328-
329- return S_OK;
330- }
331-
332- HRESULT
333- DxcValidator::RunRootSignatureValidation (IDxcBlob *pShader,
334- AbstractMemoryStream *pDiagStream) {
335-
336- const DxilContainerHeader *pDxilContainer = IsDxilContainerLike (
337- pShader->GetBufferPointer (), pShader->GetBufferSize ());
338- if (!pDxilContainer) {
339- return DXC_E_IR_VERIFICATION_FAILED;
340- }
341-
342- const DxilProgramHeader *pProgramHeader =
343- GetDxilProgramHeader (pDxilContainer, DFCC_DXIL);
344- const DxilPartHeader *pPSVPart =
345- GetDxilPartByType (pDxilContainer, DFCC_PipelineStateValidation);
346- const DxilPartHeader *pRSPart =
347- GetDxilPartByType (pDxilContainer, DFCC_RootSignature);
348- IFRBOOL (pRSPart, DXC_E_MISSING_PART);
349- if (pProgramHeader) {
350- // Container has shader part, make sure we have PSV.
351- IFRBOOL (pPSVPart, DXC_E_MISSING_PART);
352- }
353- try {
354- RootSignatureHandle RSH;
355- RSH.LoadSerialized ((const uint8_t *)GetDxilPartData (pRSPart),
356- pRSPart->PartSize );
357- RSH.Deserialize ();
358- raw_stream_ostream DiagStream (pDiagStream);
359- if (pProgramHeader) {
360- IFRBOOL (VerifyRootSignatureWithShaderPSV (
361- RSH.GetDesc (),
362- GetVersionShaderType (pProgramHeader->ProgramVersion ),
363- GetDxilPartData (pPSVPart), pPSVPart->PartSize , DiagStream),
364- DXC_E_INCORRECT_ROOT_SIGNATURE);
365- } else {
366- IFRBOOL (VerifyRootSignature (RSH.GetDesc (), DiagStream, false ),
367- DXC_E_INCORRECT_ROOT_SIGNATURE);
368- }
369- } catch (...) {
370- return DXC_E_IR_VERIFICATION_FAILED;
371- }
372-
373- return S_OK;
374- }
375-
376154// /////////////////////////////////////////////////////////////////////////////
377155
378156HRESULT RunInternalValidator (IDxcValidator *pValidator, llvm::Module *pModule,
0 commit comments