diff options
| author | jsmall-nvidia <jsmall@nvidia.com> | 2019-12-12 14:53:44 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-12-12 14:53:44 -0500 |
| commit | 15335549340c54fd7b89b28104ddc907e9c64638 (patch) | |
| tree | 02c4705fc7784c3003d14fc661894b5f88c05875 /source/slang/slang-check.cpp | |
| parent | 6e6a876a6b5ad3d2ef402757d2e20641f5a2b49b (diff) | |
Use DownstreamCompiler for all downstream compilers (#1152)
* CPPCompiler -> DownstreamCompiler
* Added DownstreamCompileResult to start abstraction such that we don't need files.
* * Split out slang-blob.cpp
* Made CompileResult hold a DownstreamCompileResult - for access to binary or ISlangSharedLibrary
* Keep temporary files in scope.
* Add a hash to the hex dump stream.
* Move all file tracking into DownstreamCompiler.
* WIP support for nvrtc.
* WIP: Adding support for nvrtc compiler.
Adding enum types, wiring up the nvrtc into slang.
* Fix remaining CPPCompiler references.
* Fix order issue on target string matching.
* Use ISlangSharedLibrary for nvrtc.
* Use DownstreamCompiler for nvrtc.
* WIP first pass at compilation win nvrtc.
* Added testing if file is on file system into CommandLineDownstreamCompiler.
Added sourceContentsPath.
* Make test cuda-compile.cu work by just compiling not comparing output.
* Genearlize DownstreamCompiler usage.
* Fix warning on clang.
* Remove CompilerType from DownstreamCompiler.
* Use DownstreamCompiler interface for all compilers.
NOTE for FXC, DXC and GLSLANG this doesn't mean using 'compile' - it's still extracting functions from shared library.
* Fix compiling on gcc/clang for DownstreamCompiler.
* Fix problem on non-vc builds with not having return on locateCompilers for VS.
* Change so no warning for code not reachable on locateCompilers for vs.
Diffstat (limited to 'source/slang/slang-check.cpp')
| -rw-r--r-- | source/slang/slang-check.cpp | 199 |
1 files changed, 124 insertions, 75 deletions
diff --git a/source/slang/slang-check.cpp b/source/slang/slang-check.cpp index db69a3155..50de73f07 100644 --- a/source/slang/slang-check.cpp +++ b/source/slang/slang-check.cpp @@ -13,86 +13,151 @@ namespace Slang struct FunctionInfo { const char* name; - SharedLibraryType libraryType; + PassThroughMode compilerType; }; + + const Guid IID_ISlangSharedLibraryLoader = SLANG_UUID_ISlangSharedLibraryLoader; + const Guid IID_ISlangUnknown = SLANG_UUID_ISlangUnknown; + + class SinkSharedLibraryLoader : public RefObject, public ISlangSharedLibraryLoader + { + public: + SLANG_REF_OBJECT_IUNKNOWN_ALL + + virtual SLANG_NO_THROW SlangResult SLANG_MCALL loadSharedLibrary( + const char* path, + ISlangSharedLibrary** outSharedLibrary) SLANG_OVERRIDE + { + SlangResult res = m_loader->loadSharedLibrary(path, outSharedLibrary); + + // Special handling for failure... + if (SLANG_FAILED(res) && m_sink) + { + String filename = Path::getFileNameWithoutExt(path); + if (filename == "dxil") + { + m_sink->diagnose(SourceLoc(), Diagnostics::dxilNotFound); + } + else + { + m_sink->diagnose(SourceLoc(), Diagnostics::failedToLoadDynamicLibrary, path); + } + } + return res; + } + + SinkSharedLibraryLoader(ISlangSharedLibraryLoader* loader, DiagnosticSink* sink) : + m_loader(loader), + m_sink(sink) + { + } + + protected: + ISlangUnknown* getInterface(const Guid& guid) + { + return (guid == IID_ISlangUnknown || guid == IID_ISlangSharedLibraryLoader) ? static_cast<ISlangSharedLibraryLoader*>(this) : nullptr; + } + ISlangSharedLibraryLoader* m_loader; + DiagnosticSink* m_sink; + }; + } // anonymous static FunctionInfo _getFunctionInfo(Session::SharedLibraryFuncType funcType) { typedef Session::SharedLibraryFuncType FuncType; - typedef SharedLibraryType LibType; - + switch (funcType) { - case FuncType::Glslang_Compile: return { "glslang_compile", LibType::Glslang } ; - case FuncType::Fxc_D3DCompile: return { "D3DCompile", LibType::Fxc }; - case FuncType::Fxc_D3DDisassemble: return { "D3DDisassemble", LibType::Fxc }; - case FuncType::Dxc_DxcCreateInstance: return { "DxcCreateInstance", LibType::Dxc }; - default: return { nullptr, LibType::Unknown }; + case FuncType::Glslang_Compile: return { "glslang_compile", PassThroughMode::Glslang} ; + case FuncType::Fxc_D3DCompile: return { "D3DCompile", PassThroughMode::Fxc}; + case FuncType::Fxc_D3DDisassemble: return { "D3DDisassemble", PassThroughMode::Fxc }; + case FuncType::Dxc_DxcCreateInstance: return { "DxcCreateInstance", PassThroughMode::Dxc }; + default: return { nullptr, PassThroughMode::None }; } } - static PassThroughMode _toPassThroughMode(SharedLibraryType type) + void Session::setSharedLibraryLoader(ISlangSharedLibraryLoader* loader) { - switch (type) + if (m_sharedLibraryLoader != loader) { - case SharedLibraryType::Dxil: - case SharedLibraryType::Dxc: + // Need to clear all of the libraries + m_downstreamCompilerSet->clear(); + m_downstreamCompilerInitialized = 0; + + for (Index i = 0; i < Index(SLANG_PASS_THROUGH_COUNT_OF); ++i) { - return PassThroughMode::Dxc; + m_downstreamCompilers[i].setNull(); } - case SharedLibraryType::Fxc: return PassThroughMode::Fxc; - case SharedLibraryType::Glslang: return PassThroughMode::Glslang; - default: break; - } - return PassThroughMode::None; + // Clear all of the functions + ::memset(m_sharedLibraryFunctions, 0, sizeof(m_sharedLibraryFunctions)); + + // Set the loader + m_sharedLibraryLoader = loader; + } } - void Session::setSharedLibrary(SharedLibraryType type, ISlangSharedLibrary* library) + void Session::resetDownstreamCompiler(PassThroughMode type) { - sharedLibraries[int(type)] = library; + // Mark as initialized + m_downstreamCompilerInitialized &= ~(1 << int(type)); + m_downstreamCompilers[int(type)].setNull(); } - ISlangSharedLibrary* Session::getOrLoadSharedLibrary(SharedLibraryType type, DiagnosticSink* sink) + DownstreamCompiler* Session::getOrLoadDownstreamCompiler(PassThroughMode type, DiagnosticSink* sink) { - // If not loaded, try loading it - if (!sharedLibraries[int(type)]) + if (m_downstreamCompilerInitialized & (1 << int(type))) { - // Try to preload dxil first, if loading dxc - if (type == SharedLibraryType::Dxc) - { - // Pass nullptr as the sink, because if it fails we don't want to report as error - getOrLoadSharedLibrary(SharedLibraryType::Dxil, nullptr); - } + return m_downstreamCompilers[int(type)]; + } + + if (type == PassThroughMode::GenericCCpp) + { + // try loading all C/C++ compilers + getOrLoadDownstreamCompiler(PassThroughMode::Clang, sink); + getOrLoadDownstreamCompiler(PassThroughMode::Gcc, sink); + getOrLoadDownstreamCompiler(PassThroughMode::VisualStudio, sink); + } - const char* libName = DefaultSharedLibraryLoader::getSharedLibraryNameFromType(type); + // Mark that we have tried to load it + m_downstreamCompilerInitialized |= (1 << int(type)); + m_downstreamCompilers[int(type)].setNull(); - StringBuilder builder; - PassThroughMode passThrough = _toPassThroughMode(type); - if (passThrough != PassThroughMode::None && m_downstreamCompilerPaths[int(passThrough)].getLength() > 0) - { - Path::combineIntoBuilder(m_downstreamCompilerPaths[int(passThrough)].getUnownedSlice(), UnownedStringSlice(libName), builder); - libName = builder.getBuffer(); - } + // Do we have a locator + auto locator = m_downstreamCompilerLocators[int(type)]; + if (!locator) + { + return nullptr; + } - if (SLANG_FAILED(sharedLibraryLoader->loadSharedLibrary(libName, sharedLibraries[int(type)].writeRef()))) - { - if (sink) - { - sink->diagnose(SourceLoc(), Diagnostics::failedToLoadDynamicLibrary, libName); - } - return nullptr; - } + m_downstreamCompilerSet->remove(SlangPassThrough(type)); + + SinkSharedLibraryLoader loader(m_sharedLibraryLoader, sink); + locator(m_downstreamCompilerPaths[int(type)], &loader, m_downstreamCompilerSet); + + DownstreamCompilerUtil::updateDefaults(m_downstreamCompilerSet); + + if (type == PassThroughMode::GenericCCpp) + { + m_downstreamCompilers[int(type)] = m_downstreamCompilerSet->getDefaultCompiler(DownstreamCompiler::SourceType::CPP); } - return sharedLibraries[int(type)]; + else + { + DownstreamCompiler::Desc desc; + desc.type = SlangPassThrough(type); + + m_downstreamCompilers[int(type)] = DownstreamCompilerUtil::findCompiler(m_downstreamCompilerSet, DownstreamCompilerUtil::MatchType::Newest, desc); + } + + return m_downstreamCompilers[int(type)]; } SlangFuncPtr Session::getSharedLibraryFunc(SharedLibraryFuncType type, DiagnosticSink* sink) { - if (sharedLibraryFunctions[int(type)]) + if (m_sharedLibraryFunctions[int(type)]) { - return sharedLibraryFunctions[int(type)]; + return m_sharedLibraryFunctions[int(type)]; } // do we have the library FunctionInfo info = _getFunctionInfo(type); @@ -101,47 +166,31 @@ namespace Slang return nullptr; } // Try loading the library - ISlangSharedLibrary* sharedLib = getOrLoadSharedLibrary(info.libraryType, sink); - if (!sharedLib) + DownstreamCompiler* compiler = getOrLoadDownstreamCompiler(info.compilerType, sink); + if (!compiler) + { + return nullptr; + } + ISlangSharedLibrary* sharedLibrary = compiler->getSharedLibrary(); + if (!sharedLibrary) { return nullptr; } // Okay now access the func - SlangFuncPtr func = sharedLib->findFuncByName(info.name); + SlangFuncPtr func = sharedLibrary->findFuncByName(info.name); if (!func) { - const char* libName = DefaultSharedLibraryLoader::getSharedLibraryNameFromType(info.libraryType); - sink->diagnose(SourceLoc(), Diagnostics::failedToFindFunctionInSharedLibrary, info.name, libName); + UnownedStringSlice compilerName = DownstreamCompiler::getCompilerTypeAsText(SlangPassThrough(info.compilerType)); + sink->diagnose(SourceLoc(), Diagnostics::failedToFindFunctionForCompiler, info.name, compilerName); return nullptr; } // Store in the function cache - sharedLibraryFunctions[int(type)] = func; + m_sharedLibraryFunctions[int(type)] = func; return func; } - DownstreamCompilerSet* Session::requireDownstreamCompilerSet() - { - if (downstreamCompilerSet == nullptr) - { - downstreamCompilerSet = new DownstreamCompilerSet; - - typedef DownstreamCompiler::CompilerType CompilerType; - DownstreamCompilerUtil::InitializeSetDesc desc; - - desc.paths[int(CompilerType::GCC)] = m_downstreamCompilerPaths[int(PassThroughMode::Gcc)]; - desc.paths[int(CompilerType::Clang)] = m_downstreamCompilerPaths[int(PassThroughMode::Clang)]; - desc.paths[int(CompilerType::VisualStudio)] = m_downstreamCompilerPaths[int(PassThroughMode::VisualStudio)]; - - desc.sharedLibraries[int(CompilerType::NVRTC)] = getOrLoadSharedLibrary(SharedLibraryType::NVRTC, nullptr); - - DownstreamCompilerUtil::initializeSet(desc, downstreamCompilerSet); - } - SLANG_ASSERT(downstreamCompilerSet); - return downstreamCompilerSet; - } - TypeCheckingCache* Session::getTypeCheckingCache() { if (!typeCheckingCache) |
