Skip to content

Commit 1b3ee58

Browse files
committed
implement stage 1, remove dxil_dll_path and selectValidator
1 parent 131c43b commit 1b3ee58

File tree

15 files changed

+110
-122
lines changed

15 files changed

+110
-122
lines changed

include/dxc/Support/HLSLOptions.td

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,8 +317,6 @@ 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.">;
322320
def print_after_all : Flag<["-", "/"], "print-after-all">, Group<hlslcomp_Group>, Flags<[CoreOption, HelpHidden]>,
323321
HelpText<"Print LLVM IR after each pass.">;
324322
def print_after : Separate<["-", "/"], "print-after">, Group<hlslcomp_Group>, Flags<[CoreOption, HelpHidden]>,

include/dxc/Support/dxcapi.use.h

Lines changed: 74 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313
#define __DXCAPI_USE_H__
1414

1515
#include "dxc/dxcapi.h"
16+
#include <cstdlib> // for getenv
1617
#include <string>
18+
#include <filesystem> // C++17 and later
19+
#include <dxc/Support/Global.h> // for hresult handling with DXC_FAILED
1720

1821
namespace dxc {
1922

@@ -26,17 +29,15 @@ class DxcDllSupport {
2629
HMODULE m_dll;
2730
DxcCreateInstanceProc m_createFn;
2831
DxcCreateInstance2Proc m_createFn2;
29-
std::string DxilDLLPath = "";
3032

31-
HRESULT InitializeInternal(LPCSTR dllName, LPCSTR fnName) {
33+
HRESULT virtual InitializeInternal(LPCSTR dllName, LPCSTR fnName) {
3234
if (m_dll != nullptr)
3335
return S_OK;
3436

3537
#ifdef _WIN32
3638
m_dll = LoadLibraryA(dllName);
3739
if (m_dll == nullptr)
3840
return HRESULT_FROM_WIN32(GetLastError());
39-
4041
m_createFn = (DxcCreateInstanceProc)GetProcAddress(m_dll, fnName);
4142

4243
if (m_createFn == nullptr) {
@@ -77,8 +78,6 @@ class DxcDllSupport {
7778
}
7879

7980
public:
80-
LPCSTR GetDxilDLLPath() { return DxilDLLPath.data(); }
81-
void SetDxilDLLPath(LPCSTR p) { DxilDLLPath = p; }
8281
DxcDllSupport() : m_dll(nullptr), m_createFn(nullptr), m_createFn2(nullptr) {}
8382

8483
DxcDllSupport(DxcDllSupport &&other) {
@@ -90,14 +89,13 @@ class DxcDllSupport {
9089
other.m_createFn2 = nullptr;
9190
}
9291

93-
~DxcDllSupport() { Cleanup(); }
92+
virtual ~DxcDllSupport() { Cleanup(); }
9493

95-
HRESULT Initialize() {
96-
// load dxcompiler.dll
94+
HRESULT virtual Initialize() {
9795
return InitializeInternal(kDxCompilerLib, "DxcCreateInstance");
9896
}
9997

100-
HRESULT InitializeForDll(LPCSTR dll, LPCSTR entryPoint) {
98+
HRESULT virtual InitializeForDll(LPCSTR dll, LPCSTR entryPoint) {
10199
return InitializeInternal(dll, entryPoint);
102100
}
103101

@@ -106,7 +104,8 @@ class DxcDllSupport {
106104
return CreateInstance(clsid, __uuidof(TInterface), (IUnknown **)pResult);
107105
}
108106

109-
HRESULT CreateInstance(REFCLSID clsid, REFIID riid, IUnknown **pResult) {
107+
HRESULT virtual CreateInstance(REFCLSID clsid, REFIID riid,
108+
IUnknown **pResult) {
110109
if (pResult == nullptr)
111110
return E_POINTER;
112111
if (m_dll == nullptr)
@@ -122,7 +121,7 @@ class DxcDllSupport {
122121
(IUnknown **)pResult);
123122
}
124123

125-
HRESULT CreateInstance2(IMalloc *pMalloc, REFCLSID clsid, REFIID riid,
124+
HRESULT virtual CreateInstance2(IMalloc *pMalloc, REFCLSID clsid, REFIID riid,
126125
IUnknown **pResult) {
127126
if (pResult == nullptr)
128127
return E_POINTER;
@@ -134,11 +133,21 @@ class DxcDllSupport {
134133
return hr;
135134
}
136135

137-
bool HasCreateWithMalloc() const { return m_createFn2 != nullptr; }
136+
bool virtual HasCreateWithMalloc() const { return m_createFn2 != nullptr; }
137+
138+
bool virtual IsEnabled() const { return m_dll != nullptr; }
138139

139-
bool IsEnabled() const { return m_dll != nullptr; }
140+
bool virtual GetCreateInstanceProcs(DxcCreateInstanceProc *pCreateFn,
141+
DxcCreateInstance2Proc *pCreateFn2) const {
142+
if (pCreateFn == nullptr || pCreateFn2 == nullptr ||
143+
m_createFn == nullptr)
144+
return false;
145+
*pCreateFn = m_createFn;
146+
*pCreateFn2 = m_createFn2;
147+
return true;
148+
}
140149

141-
void Cleanup() {
150+
void virtual Cleanup() {
142151
if (m_dll != nullptr) {
143152
m_createFn = nullptr;
144153
m_createFn2 = nullptr;
@@ -151,7 +160,7 @@ class DxcDllSupport {
151160
}
152161
}
153162

154-
HMODULE Detach() {
163+
HMODULE virtual Detach() {
155164
HMODULE hModule = m_dll;
156165
m_dll = nullptr;
157166
return hModule;
@@ -184,6 +193,56 @@ void WriteOperationErrorsToConsole(IDxcOperationResult *pResult,
184193
void WriteOperationResultToConsole(IDxcOperationResult *pRewriteResult,
185194
bool outputWarnings);
186195

196+
class DxcDllExtValidationSupport : public DxcDllSupport {
197+
// this instance of DxcDllSupport manages the lifetime of
198+
// dxil.dll
199+
DxcDllSupport *m_DxilSupport = nullptr;
200+
201+
std::string DxilDLLPathExt = "";
202+
bool InitializationSuccess = false;
203+
// override DxcDllSupport's implementation of InitializeInternal,
204+
// adding the environment variable value check for a path to a dxil.dll
205+
// for external validation
206+
HRESULT InitializeInternal(LPCSTR dllName, LPCSTR fnName){
207+
208+
// Load dxcompiler.dll
209+
HRESULT result = m_DxilSupport->InitializeForDll(dllName, fnName);
210+
InitializationSuccess = DXC_FAILED(result) ? false : true;
211+
if (!InitializationSuccess){
212+
return result;
213+
}
214+
215+
// now handle internal or external dxil.dll
216+
const char *envVal = std::getenv("DXC_DXIL_DLL_PATH");
217+
bool ValidateInternally = false;
218+
if (!envVal || std::string(envVal).empty()) {
219+
ValidateInternally = true;
220+
}
221+
222+
if (!ValidateInternally){
223+
std::string DllPathStr(envVal);
224+
DxilDLLPathExt = DllPathStr;
225+
std::filesystem::path DllPath(DllPathStr);
226+
227+
// Check if path is absolute and exists
228+
if (!DllPath.is_absolute() || !std::filesystem::exists(DllPath)) {
229+
InitializationSuccess = false;
230+
// TODO: Ideally emit some diagnostic that the given absolute path doesn't exist
231+
return HRESULT_FROM_WIN32(GetLastError());
232+
}
233+
result = m_DxilSupport->InitializeForDll(DllPathStr.data(), fnName);
234+
if (DXC_FAILED(result)) {
235+
InitializationSuccess = false;
236+
}
237+
}
238+
// nothing to do if we are validating internally, dxcompiler.dll
239+
// is loaded and it'll take care of validation.
240+
return InitializationSuccess;
241+
}
242+
243+
std::string GetDxilDLLPathExt() { return DxilDLLPathExt; }
244+
245+
};
187246
} // namespace dxc
188247

189248
#endif

lib/DxcSupport/HLSLOptions.cpp

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

28-
#include <filesystem>
2928
#include <optional>
3029

3130
using namespace llvm::opt;
@@ -410,15 +409,6 @@ LangStd parseHLSLVersion(llvm::StringRef Ver) {
410409
}
411410
namespace options {
412411

413-
bool AbsolutePathExists(const std::filesystem::path &p) {
414-
// First check if it is 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-
422412
/// Reads all options from the given argument strings, populates opts, and
423413
/// validates reporting errors and warnings.
424414
int ReadDxcOpts(const OptTable *optionTable, unsigned flagsToInclude,
@@ -1043,19 +1033,6 @@ int ReadDxcOpts(const OptTable *optionTable, unsigned flagsToInclude,
10431033
opts.ValVerMinor = (unsigned long)minor64;
10441034
}
10451035

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

tools/clang/test/DXC/Inputs/dxil_dll_path.hlsl

Lines changed: 0 additions & 6 deletions
This file was deleted.

tools/clang/tools/dxclib/dxc.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1448,7 +1448,6 @@ int dxc::main(int argc, const char **argv_) {
14481448
SetUnhandledExceptionFilter(ExceptionFilter);
14491449
#endif
14501450

1451-
// Setup dxcompiler DLL.
14521451
{
14531452
std::string dllErrorString;
14541453
llvm::raw_string_ostream dllErrorStream(dllErrorString);
@@ -1462,7 +1461,6 @@ int dxc::main(int argc, const char **argv_) {
14621461
}
14631462

14641463
EnsureEnabled(dxcSupport);
1465-
14661464
DxcContext context(dxcOpts, dxcSupport);
14671465
// Handle help request, which overrides any other processing.
14681466
if (dxcOpts.ShowHelp) {

tools/clang/tools/dxcompiler/DXCompiler.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,6 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD Reason, LPVOID reserved) {
109109
::hlsl::options::cleanupHlslOptTable();
110110
::llvm::sys::fs::CleanupPerThreadFileSystem();
111111
::llvm::llvm_shutdown();
112-
if (reserved ==
113-
NULL) { // FreeLibrary has been called or the DLL load failed
114-
DxilLibCleanup(DxilLibCleanUpType::UnloadLibrary);
115-
} else { // Process termination. We should not call FreeLibrary()
116-
DxilLibCleanup(DxilLibCleanUpType::ProcessTermination);
117-
}
118112
DxcClearThreadMalloc();
119113
DxcCleanupThreadMalloc();
120114
DxcEtw_DXCompilerShutdown_Stop(S_OK);

tools/clang/tools/dxcompiler/dxcapi.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,7 @@ HRESULT CreateDxcContainerBuilder(REFIID riid, _Out_ LPVOID *ppv) {
6060
// Call dxil.dll's containerbuilder
6161
*ppv = nullptr;
6262
const char *warning;
63-
HRESULT hr = DxilLibInitialize();
64-
if (FAILED(hr)) {
65-
warning = "Unable to load dxil.dll.\n";
66-
}
67-
hr = DxilLibCreateInstance(CLSID_DxcContainerBuilder,
63+
HRESULT hr = DxilLibCreateInstance(CLSID_DxcContainerBuilder,
6864
(IDxcContainerBuilder **)ppv);
6965
if (FAILED(hr)) {
7066
warning = "Unable to create container builder from dxil.dll. Resulting "

tools/clang/tools/dxcompiler/dxcompilerobj.cpp

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -850,12 +850,9 @@ class DxcCompiler : public IDxcCompiler3,
850850
compiler.getCodeGenOpts().HLSLValidatorMajorVer = opts.ValVerMajor;
851851
compiler.getCodeGenOpts().HLSLValidatorMinorVer = opts.ValVerMinor;
852852
} else {
853-
// Version internal validator, or dxil dll if specified
854-
// with dxil_dll_path option
855853
dxcutil::GetValidatorVersion(
856854
&compiler.getCodeGenOpts().HLSLValidatorMajorVer,
857-
&compiler.getCodeGenOpts().HLSLValidatorMinorVer,
858-
opts.DxilDLLPath);
855+
&compiler.getCodeGenOpts().HLSLValidatorMinorVer);
859856
}
860857

861858
// Root signature-only container validation is only supported on 1.5 and
@@ -935,7 +932,7 @@ class DxcCompiler : public IDxcCompiler3,
935932
CComPtr<IDxcBlobEncoding> pValErrors;
936933
// Validation failure communicated through diagnostic error
937934
dxcutil::ValidateRootSignatureInContainer(
938-
pOutputBlob, &compiler.getDiagnostics(), opts.DxilDLLPath);
935+
pOutputBlob, &compiler.getDiagnostics());
939936
}
940937
}
941938
} else if (opts.VerifyDiagnostics) {
@@ -1055,8 +1052,7 @@ class DxcCompiler : public IDxcCompiler3,
10551052
std::move(serializeModule), pOutputBlob, m_pMalloc,
10561053
SerializeFlags, pOutputStream, 0, opts.GetPDBName(),
10571054
&compiler.getDiagnostics(), &ShaderHashContent, pReflectionStream,
1058-
pRootSigStream, pRootSignatureBlob, pPrivateBlob,
1059-
opts.DxilDLLPath);
1055+
pRootSigStream, pRootSignatureBlob, pPrivateBlob);
10601056

10611057
inputs.pVersionInfo = static_cast<IDxcVersionInfo *>(this);
10621058

@@ -1109,8 +1105,7 @@ class DxcCompiler : public IDxcCompiler3,
11091105
CComPtr<IDxcBlobEncoding> pValErrors;
11101106
// Validation failure communicated through diagnostic error
11111107
dxcutil::ValidateRootSignatureInContainer(
1112-
pRootSignature, &compiler.getDiagnostics(),
1113-
opts.DxilDLLPath);
1108+
pRootSignature, &compiler.getDiagnostics());
11141109
}
11151110
IFT(pResult->SetOutputObject(DXC_OUT_ROOT_SIGNATURE,
11161111
pRootSignature));

0 commit comments

Comments
 (0)