Skip to content

Commit 036d502

Browse files
committed
failing first draft
1 parent adffd31 commit 036d502

File tree

16 files changed

+106
-62
lines changed

16 files changed

+106
-62
lines changed
Binary file not shown.

include/dxc/Support/HLSLOptions.h

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,6 @@ struct RewriterOpts {
114114
bool DeclGlobalCB = false; // OPT_rw_decl_global_cb
115115
};
116116

117-
enum class ValidatorSelection : int {
118-
Auto, // Force internal validator (even if DXIL.dll is present)
119-
Internal, // Force internal validator (even if DXIL.dll is present)
120-
External, // Use DXIL.dll, failing compilation if not available
121-
Invalid = -1 // Invalid
122-
};
123-
124117
/// Use this class to capture all options.
125118
class DxcOpts {
126119
public:
@@ -225,8 +218,7 @@ class DxcOpts {
225218
bool ResMayAlias = false; // OPT_res_may_alias
226219
unsigned long ValVerMajor = UINT_MAX,
227220
ValVerMinor = UINT_MAX; // OPT_validator_version
228-
ValidatorSelection SelectValidator =
229-
ValidatorSelection::Auto; // OPT_select_validator
221+
std::string DxilDLLPath = ""; // OPT_dxil_dll_path
230222
unsigned ScanLimit = 0; // OPT_memdep_block_scan_limit
231223
bool ForceZeroStoreLifetimes = false; // OPT_force_zero_store_lifetimes
232224
bool EnableLifetimeMarkers = false; // OPT_enable_lifetime_markers

include/dxc/Support/HLSLOptions.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,8 @@ def print_before_all : Flag<["-", "/"], "print-before-all">, Group<hlslcomp_Grou
317317
HelpText<"Print LLVM IR before each pass.">;
318318
def print_before : Separate<["-", "/"], "print-before">, Group<hlslcomp_Group>, Flags<[CoreOption, HelpHidden]>,
319319
HelpText<"Print LLVM IR before a specific pass. May be specificied multiple times.">;
320+
def dxil_dll_path : Separate<["-", "/"], "dxil-dll-path">, Group<hlslcomp_Group>, Flags<[CoreOption, HelpHidden]>,
321+
HelpText<"Specify an absolute path to a DXIL.dll file to set the metadata version and validate the DXIL output.">;
320322
def print_after_all : Flag<["-", "/"], "print-after-all">, Group<hlslcomp_Group>, Flags<[CoreOption, HelpHidden]>,
321323
HelpText<"Print LLVM IR after each pass.">;
322324
def print_after : Separate<["-", "/"], "print-after">, Group<hlslcomp_Group>, Flags<[CoreOption, HelpHidden]>,

include/dxc/Support/dxcapi.use.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#define __DXCAPI_USE_H__
1414

1515
#include "dxc/dxcapi.h"
16+
#include <string>
1617

1718
namespace dxc {
1819

@@ -25,6 +26,7 @@ class DxcDllSupport {
2526
HMODULE m_dll;
2627
DxcCreateInstanceProc m_createFn;
2728
DxcCreateInstance2Proc m_createFn2;
29+
std::string DxilDLLPath = "";
2830

2931
HRESULT InitializeInternal(LPCSTR dllName, LPCSTR fnName) {
3032
if (m_dll != nullptr)
@@ -34,6 +36,12 @@ class DxcDllSupport {
3436
m_dll = LoadLibraryA(dllName);
3537
if (m_dll == nullptr)
3638
return HRESULT_FROM_WIN32(GetLastError());
39+
// load dxil.dll too
40+
HMODULE dxildllModule =
41+
LoadLibraryA(DxilDLLPath == "" ? "dxil.dll" : GetDxilDLLPath());
42+
if (dxildllModule == nullptr) {
43+
return HRESULT_FROM_WIN32(GetLastError());
44+
}
3745
m_createFn = (DxcCreateInstanceProc)GetProcAddress(m_dll, fnName);
3846

3947
if (m_createFn == nullptr) {
@@ -74,6 +82,10 @@ class DxcDllSupport {
7482
}
7583

7684
public:
85+
LPCSTR GetDxilDLLPath() {return DxilDLLPath.data();}
86+
void SetDxilDLLPath(LPCSTR p) {
87+
DxilDLLPath = p;
88+
}
7789
DxcDllSupport() : m_dll(nullptr), m_createFn(nullptr), m_createFn2(nullptr) {}
7890

7991
DxcDllSupport(DxcDllSupport &&other) {
@@ -88,7 +100,8 @@ class DxcDllSupport {
88100
~DxcDllSupport() { Cleanup(); }
89101

90102
HRESULT Initialize() {
91-
return InitializeInternal(kDxCompilerLib, "DxcCreateInstance");
103+
// load dxcompiler.dll
104+
return InitializeInternal(kDxCompilerLib, "DxcCreateInstance");
92105
}
93106

94107
HRESULT InitializeForDll(LPCSTR dll, LPCSTR entryPoint) {

lib/DxcSupport/HLSLOptions.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "llvm/Support/raw_ostream.h"
2727

2828
#include <optional>
29+
#include <filesystem>
2930

3031
using namespace llvm::opt;
3132
using namespace dxc;
@@ -409,6 +410,15 @@ LangStd parseHLSLVersion(llvm::StringRef Ver) {
409410
}
410411
namespace options {
411412

413+
bool AbsolutePathExists(const std::filesystem::path &p) {
414+
// First check if it’s an absolute path
415+
if (!p.is_absolute()) {
416+
return false;
417+
}
418+
// Then check if it exists
419+
return std::filesystem::exists(p);
420+
}
421+
412422
/// Reads all options from the given argument strings, populates opts, and
413423
/// validates reporting errors and warnings.
414424
int ReadDxcOpts(const OptTable *optionTable, unsigned flagsToInclude,
@@ -1033,6 +1043,20 @@ int ReadDxcOpts(const OptTable *optionTable, unsigned flagsToInclude,
10331043
opts.ValVerMinor = (unsigned long)minor64;
10341044
}
10351045

1046+
llvm::StringRef dxilDLLPathStr =
1047+
Args.getLastArgValue(OPT_dxil_dll_path);
1048+
std::filesystem::path p = dxilDLLPathStr.str();
1049+
1050+
// when the dxil_dll_path argument has an empty string,
1051+
// then the internal validator should be used
1052+
// otherwise, check that this path exists and is absolute
1053+
if (dxilDLLPathStr.str() == "" || AbsolutePathExists(p)) {
1054+
opts.DxilDLLPath = dxilDLLPathStr.str();
1055+
} else {
1056+
errors << "Absolute path \"" << dxilDLLPathStr << "\" does not exist.";
1057+
return 1;
1058+
}
1059+
10361060
if (opts.IsLibraryProfile() && Minor == 0xF) {
10371061
if (opts.ValVerMajor != UINT_MAX && opts.ValVerMajor != 0) {
10381062
errors << "Offline library profile cannot be used with non-zero "
@@ -1395,6 +1419,8 @@ int SetupDxcDllSupport(const DxcOpts &opts, dxc::DxcDllSupport &dxcSupport,
13951419
return 1;
13961420
}
13971421
}
1422+
1423+
dxcSupport.SetDxilDLLPath(opts.DxilDLLPath.data());
13981424
return 0;
13991425
}
14001426

lib/DxcSupport/dxcapi.use.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ void IFT_Data(HRESULT hr, LPCWSTR data) {
7575

7676
void EnsureEnabled(DxcDllSupport &dxcSupport) {
7777
if (!dxcSupport.IsEnabled()) {
78+
// first initialize Dxil.dll
7879
IFT(dxcSupport.Initialize());
7980
}
8081
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// RUN: %dxc -T cs_6_6 -E main -dxil-dll-path D:\DXC\dxc_releases\1.7.2212.github_release\x64\dxil.dll 2>&1 %s | FileCheck %s
2+
3+
[shader("compute")]
4+
[numthreads(2,2,1)]
5+
// CHECK: warning: External validator loaded at D:\DXC\dxc_releases\1.7.2212.github_release\x64\dxil.dll
6+
void main() {}

tools/clang/tools/dxcompiler/DXCompiler.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ static HRESULT InitMaybeFail() throw() {
6565
fsSetup = true;
6666
IFC(hlsl::SetupRegistryPassForHLSL());
6767
IFC(hlsl::SetupRegistryPassForPIX());
68-
IFC(DxilLibInitialize());
6968
if (hlsl::options::initHlslOptTable()) {
7069
hr = E_FAIL;
7170
goto Cleanup;

tools/clang/tools/dxcompiler/dxcompilerobj.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -850,11 +850,12 @@ class DxcCompiler : public IDxcCompiler3,
850850
compiler.getCodeGenOpts().HLSLValidatorMajorVer = opts.ValVerMajor;
851851
compiler.getCodeGenOpts().HLSLValidatorMinorVer = opts.ValVerMinor;
852852
} else {
853-
// Version from dxil.dll, or internal validator if unavailable
853+
// Version internal validator, or dxil dll if specified
854+
// with dxil_dll_path option
854855
dxcutil::GetValidatorVersion(
855856
&compiler.getCodeGenOpts().HLSLValidatorMajorVer,
856857
&compiler.getCodeGenOpts().HLSLValidatorMinorVer,
857-
opts.SelectValidator);
858+
opts.DxilDLLPath);
858859
}
859860

860861
// Root signature-only container validation is only supported on 1.5 and
@@ -934,7 +935,7 @@ class DxcCompiler : public IDxcCompiler3,
934935
CComPtr<IDxcBlobEncoding> pValErrors;
935936
// Validation failure communicated through diagnostic error
936937
dxcutil::ValidateRootSignatureInContainer(
937-
pOutputBlob, &compiler.getDiagnostics(), opts.SelectValidator);
938+
pOutputBlob, &compiler.getDiagnostics(), opts.DxilDLLPath);
938939
}
939940
}
940941
} else if (opts.VerifyDiagnostics) {
@@ -1055,7 +1056,7 @@ class DxcCompiler : public IDxcCompiler3,
10551056
SerializeFlags, pOutputStream, 0, opts.GetPDBName(),
10561057
&compiler.getDiagnostics(), &ShaderHashContent, pReflectionStream,
10571058
pRootSigStream, pRootSignatureBlob, pPrivateBlob,
1058-
opts.SelectValidator);
1059+
opts.DxilDLLPath);
10591060

10601061
inputs.pVersionInfo = static_cast<IDxcVersionInfo *>(this);
10611062

@@ -1109,7 +1110,7 @@ class DxcCompiler : public IDxcCompiler3,
11091110
// Validation failure communicated through diagnostic error
11101111
dxcutil::ValidateRootSignatureInContainer(
11111112
pRootSignature, &compiler.getDiagnostics(),
1112-
opts.SelectValidator);
1113+
opts.DxilDLLPath);
11131114
}
11141115
IFT(pResult->SetOutputObject(DXC_OUT_ROOT_SIGNATURE,
11151116
pRootSignature));

tools/clang/tools/dxcompiler/dxcutil.cpp

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -51,25 +51,20 @@ namespace {
5151

5252
// return true if the internal validator was used, false otherwise
5353
bool CreateValidator(CComPtr<IDxcValidator> &pValidator,
54-
hlsl::options::ValidatorSelection SelectValidator =
55-
hlsl::options::ValidatorSelection::Auto) {
56-
bool bInternal =
57-
SelectValidator == hlsl::options::ValidatorSelection::Internal;
58-
bool bExternal =
59-
SelectValidator == hlsl::options::ValidatorSelection::External;
60-
bool bAuto = SelectValidator == hlsl::options::ValidatorSelection::Auto;
61-
62-
// default behavior uses internal validator, as well as
63-
// explicitly specifying internal
64-
if (bInternal || bAuto) {
54+
std::string DxilDLLPath = "") {
55+
56+
// default behavior uses internal validator
57+
if (DxilDLLPath == "") {
6558
IFT(CreateDxcValidator(IID_PPV_ARGS(&pValidator)));
6659
return true;
6760
}
6861

69-
if (bExternal) {
70-
// if external was explicitly specified, but no
71-
// external validator could be found (no DXIL.dll), then error
72-
IFTBOOL(DxilLibIsEnabled(), DXC_E_VALIDATOR_MISSING);
62+
// otherwise, use the external validator provided by the dxil_dll_path
63+
// argument
64+
else {
65+
// if a valid absolute path was given, but the DXIL.dll
66+
// is invalid, then error
67+
IFTBOOL(DxilLibIsEnabled(DxilDLLPath), DXC_E_VALIDATOR_MISSING);
7368
IFT(DxilLibCreateInstance(CLSID_DxcValidator, &pValidator));
7469

7570
return false;
@@ -90,22 +85,22 @@ AssembleInputs::AssembleInputs(
9085
clang::DiagnosticsEngine *pDiag, hlsl::DxilShaderHash *pShaderHashOut,
9186
AbstractMemoryStream *pReflectionOut, AbstractMemoryStream *pRootSigOut,
9287
CComPtr<IDxcBlob> pRootSigBlob, CComPtr<IDxcBlob> pPrivateBlob,
93-
hlsl::options::ValidatorSelection SelectValidator)
88+
std::string DxilDLLPath)
9489
: pM(std::move(pM)), pOutputContainerBlob(pOutputContainerBlob),
9590
pMalloc(pMalloc), SerializeFlags(SerializeFlags),
9691
ValidationFlags(ValidationFlags), pModuleBitcode(pModuleBitcode),
9792
DebugName(DebugName), pDiag(pDiag), pShaderHashOut(pShaderHashOut),
9893
pReflectionOut(pReflectionOut), pRootSigOut(pRootSigOut),
9994
pRootSigBlob(pRootSigBlob), pPrivateBlob(pPrivateBlob),
100-
SelectValidator(SelectValidator) {}
95+
DxilDLLPath(DxilDLLPath) {}
10196

10297
void GetValidatorVersion(unsigned *pMajor, unsigned *pMinor,
103-
hlsl::options::ValidatorSelection SelectValidator) {
98+
std::string DxilDLLPath) {
10499
if (pMajor == nullptr || pMinor == nullptr)
105100
return;
106101

107102
CComPtr<IDxcValidator> pValidator;
108-
CreateValidator(pValidator, SelectValidator);
103+
CreateValidator(pValidator, DxilDLLPath);
109104

110105
CComPtr<IDxcVersionInfo> pVersionInfo;
111106
if (SUCCEEDED(pValidator.QueryInterface(&pVersionInfo))) {
@@ -115,6 +110,7 @@ void GetValidatorVersion(unsigned *pMajor, unsigned *pMinor,
115110
*pMajor = 1;
116111
*pMinor = 0;
117112
}
113+
return;
118114
}
119115

120116
void AssembleToContainer(AssembleInputs &inputs) {
@@ -177,8 +173,14 @@ HRESULT ValidateAndAssembleToContainer(AssembleInputs &inputs) {
177173
std::unique_ptr<llvm::Module> llvmModuleWithDebugInfo;
178174

179175
CComPtr<IDxcValidator> pValidator;
180-
bool bInternalValidator = CreateValidator(pValidator, inputs.SelectValidator);
181-
// Warning on internal Validator
176+
bool bInternalValidator = CreateValidator(pValidator, inputs.DxilDLLPath);
177+
// Warning on external Validator
178+
if (!bInternalValidator) {
179+
unsigned diagID = inputs.pDiag->getCustomDiagID(
180+
clang::DiagnosticsEngine::Level::Warning,
181+
"External validator loaded at %0");
182+
inputs.pDiag->Report(diagID) << inputs.DxilDLLPath;
183+
}
182184

183185
CComPtr<IDxcValidator2> pValidator2;
184186
if (!bInternalValidator) {
@@ -273,11 +275,11 @@ HRESULT ValidateAndAssembleToContainer(AssembleInputs &inputs) {
273275

274276
HRESULT ValidateRootSignatureInContainer(
275277
IDxcBlob *pRootSigContainer, clang::DiagnosticsEngine *pDiag,
276-
hlsl::options::ValidatorSelection SelectValidator) {
278+
std::string DxilDLLPath) {
277279
HRESULT valHR = S_OK;
278280
CComPtr<IDxcValidator> pValidator;
279281
CComPtr<IDxcOperationResult> pValResult;
280-
CreateValidator(pValidator);
282+
CreateValidator(pValidator, DxilDLLPath);
281283
IFT(pValidator->Validate(pRootSigContainer,
282284
DxcValidatorFlags_RootSignatureOnly |
283285
DxcValidatorFlags_InPlaceEdit,

0 commit comments

Comments
 (0)