diff options
| author | jsmall-nvidia <jsmall@nvidia.com> | 2021-10-20 10:32:50 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-10-20 10:32:50 -0400 |
| commit | deb638f446863ab38248a9568553a1eab47ca086 (patch) | |
| tree | 5a37ac31dce2b15b5079f8f67b268d7ef652e939 /source/slang/slang-compiler.cpp | |
| parent | 8406244e27c142ea56c7a934b09e0b1cdace6afd (diff) | |
Selecting downstream compiler on code gen transition (#1980)
* #include an absolute path didn't work - because paths were taken to always be relative.
* Add support for LLVM for host callable.
Added CodeGenTransitionMap.
* Remove hack to enable host callable for LLVM.
* Small improvements around transitions/downstream compiler.
* Fix typo in method name.
* Fix comment.
Diffstat (limited to 'source/slang/slang-compiler.cpp')
| -rw-r--r-- | source/slang/slang-compiler.cpp | 151 |
1 files changed, 65 insertions, 86 deletions
diff --git a/source/slang/slang-compiler.cpp b/source/slang/slang-compiler.cpp index a73289852..c1a768f02 100644 --- a/source/slang/slang-compiler.cpp +++ b/source/slang/slang-compiler.cpp @@ -7,6 +7,7 @@ #include "../core/slang-hex-dump-util.h" #include "../core/slang-riff.h" #include "../core/slang-type-text-util.h" +#include "../core/slang-type-convert-util.h" #include "slang-check.h" #include "slang-compiler.h" @@ -575,9 +576,7 @@ namespace Slang // And if pass-through isn't set, we don't need // access to the translation unit. - // - if(endToEndReq->m_passThrough == PassThroughMode::None) return false; - return true; + return endToEndReq->m_passThrough != PassThroughMode::None; } /// If there is a pass-through compile going on, find the translation unit for the given entry point. /// Assumes isPassThroughEnabled has already been called @@ -962,6 +961,26 @@ namespace Slang } } + static CodeGenTarget _getDefaultSourceForTarget(CodeGenTarget target) + { + switch (target) + { + case CodeGenTarget::HostCallable: + case CodeGenTarget::SharedLibrary: + case CodeGenTarget::Executable: + { + return CodeGenTarget::CPPSource; + } + case CodeGenTarget::PTX: return CodeGenTarget::CUDASource; + case CodeGenTarget::DXBytecode: return CodeGenTarget::HLSL; + case CodeGenTarget::DXIL: return CodeGenTarget::HLSL; + case CodeGenTarget::SPIRV: return CodeGenTarget::GLSL; + default: break; + } + return CodeGenTarget::Unknown; + } + + SlangResult emitWithDownstreamForEntryPoints( ComponentType* program, BackEndCompileRequest* slangRequest, @@ -977,80 +996,37 @@ namespace Slang auto session = slangRequest->getSession(); + CodeGenTarget sourceTarget = CodeGenTarget::None; SourceLanguage sourceLanguage = SourceLanguage::Unknown; - PassThroughMode downstreamCompiler = endToEndReq ? endToEndReq->m_passThrough : PassThroughMode::None; - RefPtr<ExtensionTracker> extensionTracker = _newExtensionTracker(target); + PassThroughMode compilerType = endToEndReq ? endToEndReq->m_passThrough : PassThroughMode::None; // If we are not in pass through, lookup the default compiler for the emitted source type - if (downstreamCompiler == PassThroughMode::None) + if (compilerType == PassThroughMode::None) { - switch (target) + // Get the default source codegen type for a given target + sourceTarget = _getDefaultSourceForTarget(target); + compilerType = (PassThroughMode)session->getDownstreamCompilerForTransition((SlangCompileTarget)sourceTarget, (SlangCompileTarget)target); + // We should have a downstream compiler set at this point + if (compilerType == PassThroughMode::None) { - case CodeGenTarget::HostCallable: - case CodeGenTarget::SharedLibrary: - case CodeGenTarget::Executable: - { - sourceTarget = CodeGenTarget::CPPSource; - sourceLanguage = SourceLanguage::CPP; - break; - } - case CodeGenTarget::PTX: - { - sourceTarget = CodeGenTarget::CUDASource; - sourceLanguage = SourceLanguage::CUDA; - break; - } - case CodeGenTarget::DXBytecode: - { - sourceTarget = CodeGenTarget::HLSL; - sourceLanguage = SourceLanguage::HLSL; - downstreamCompiler = PassThroughMode::Fxc; - break; - } - case CodeGenTarget::DXIL: - { - sourceTarget = CodeGenTarget::HLSL; - sourceLanguage = SourceLanguage::HLSL; - downstreamCompiler = PassThroughMode::Dxc; - break; - } - case CodeGenTarget::SPIRV: - { - sourceTarget = CodeGenTarget::GLSL; - sourceLanguage = SourceLanguage::GLSL; - downstreamCompiler = PassThroughMode::Glslang; - break; - } - default: break; - } + auto sourceName = TypeTextUtil::getCompileTargetName(SlangCompileTarget(sourceTarget)); + auto targetName = TypeTextUtil::getCompileTargetName(SlangCompileTarget(target)); - // Try looking up based on the language if one isn't set - if (downstreamCompiler == PassThroughMode::None) - { - downstreamCompiler = PassThroughMode(session->getDefaultDownstreamCompiler(SlangSourceLanguage(sourceLanguage))); - } - } - else - { - // If we are pass through, we may need to set extension tracker state. - if (GLSLExtensionTracker* glslTracker = as<GLSLExtensionTracker>(extensionTracker)) - { - trackGLSLTargetCaps(glslTracker, targetReq->getTargetCaps()); + sink->diagnose(SourceLoc(), Diagnostics::compilerNotDefinedForTransition, sourceName, targetName); + return SLANG_FAIL; } } - - // We should have a downstream compiler set at this point - SLANG_ASSERT(downstreamCompiler != PassThroughMode::None); + + SLANG_ASSERT(compilerType != PassThroughMode::None); // Get the required downstream compiler - DownstreamCompiler* compiler = session->getOrLoadDownstreamCompiler(downstreamCompiler, sink); - + DownstreamCompiler* compiler = session->getOrLoadDownstreamCompiler(compilerType, sink); if (!compiler) { - auto compilerName = TypeTextUtil::getPassThroughAsHumanText((SlangPassThrough)downstreamCompiler); + auto compilerName = TypeTextUtil::getPassThroughAsHumanText((SlangPassThrough)compilerType); sink->diagnose(SourceLoc(), Diagnostics::passThroughCompilerNotFound, compilerName); return SLANG_FAIL; } @@ -1061,13 +1037,11 @@ namespace Slang typedef DownstreamCompiler::CompileOptions CompileOptions; CompileOptions options; - /* Let's set the compiler specific options - - We can only do this if the endToEndReq is set. */ + // Set compiler specific args { auto linkage = targetReq->getLinkage(); - auto name = TypeTextUtil::getPassThroughName((SlangPassThrough)downstreamCompiler); + auto name = TypeTextUtil::getPassThroughName((SlangPassThrough)compilerType); const Index nameIndex = linkage->m_downstreamArgs.findName(name); if (nameIndex >= 0) { @@ -1079,15 +1053,30 @@ namespace Slang } } - /* This is more convoluted than the other scenarios, because when we invoke C/C++ compiler we would ideally like to use the original file. We want to do this because we want includes relative to the source file to work, and for that to work most easily we want to use the original file, if there is one */ if (isPassThroughEnabled(endToEndReq)) { + // If we are pass through, we may need to set extension tracker state. + if (GLSLExtensionTracker* glslTracker = as<GLSLExtensionTracker>(extensionTracker)) + { + trackGLSLTargetCaps(glslTracker, targetReq->getTargetCaps()); + } + // TODO(DG): Review this assertion later SLANG_ASSERT(entryPointIndices.getCount() == 1); auto translationUnit = getPassThroughTranslationUnit(endToEndReq, entryPointIndices[0]); + + // We are just passing thru, so it's whatever it originally was + sourceLanguage = translationUnit->sourceLanguage; + + // TODO(JS): This seems like a bit of a hack + // That if a pass-through is being performed and the source language is Slang + // no downstream compiler knows how to deal with that, so probably means 'HLSL' + sourceLanguage = (sourceLanguage == SourceLanguage::Slang) ? SourceLanguage::HLSL : sourceLanguage; + sourceTarget = CodeGenTarget(TypeConvertUtil::getCompileTargetFromSourceLanguage((SlangSourceLanguage)sourceLanguage)); + // If it's pass through we accumulate the preprocessor definitions. for (auto& define : translationUnit->compileRequest->preprocessorDefinitions) { @@ -1130,18 +1119,6 @@ namespace Slang } } - // We are just passing thru, so it's whatever it originally was - sourceLanguage = translationUnit->sourceLanguage; - - // TODO(JS): This seems like a bit of a hack - // That if a pass-through is being performed and the source language is Slang - // no downstream compiler knows how to deal with that, so probably means 'HLSL' - if (sourceLanguage == SourceLanguage::Slang) - { - sourceLanguage = SourceLanguage::HLSL; - } - - sourceTarget = CodeGenTarget(DownstreamCompiler::getCompileTarget(SlangSourceLanguage(sourceLanguage))); // If emitted source is required, emit and set the path if (_useEmittedSource(compiler, translationUnit)) @@ -1167,6 +1144,8 @@ namespace Slang { SLANG_RETURN_ON_FAIL(emitEntryPointsSource(slangRequest, entryPointIndices, targetReq, sourceTarget, endToEndReq, extensionTracker, options.sourceContents)); maybeDumpIntermediate(slangRequest, options.sourceContents.getBuffer(), sourceTarget); + + sourceLanguage = (SourceLanguage)TypeConvertUtil::getSourceLanguageFromTarget((SlangCompileTarget)sourceTarget); } // If we have an extension tracker, we may need to set options such as SPIR-V version @@ -1212,9 +1191,9 @@ namespace Slang // Disable exceptions and security checks options.flags &= ~(CompileOptions::Flag::EnableExceptionHandling | CompileOptions::Flag::EnableSecurityChecks); - if (downstreamCompiler == PassThroughMode::Fxc || - downstreamCompiler == PassThroughMode::Dxc || - downstreamCompiler == PassThroughMode::Glslang) + if (compilerType == PassThroughMode::Fxc || + compilerType == PassThroughMode::Dxc || + compilerType == PassThroughMode::Glslang) { if (entryPointIndices.getCount() != 1) { @@ -1233,7 +1212,7 @@ namespace Slang // Set the entry point name options.entryPointName = getText(entryPoint->getName()); - if (downstreamCompiler == PassThroughMode::Dxc) + if (compilerType == PassThroughMode::Dxc) { // We will enable the flag to generate proper code for 16 - bit types // by default, as long as the user is requesting a sufficiently @@ -1260,15 +1239,15 @@ namespace Slang // Set the matrix layout options.matrixLayout = targetReq->getDefaultMatrixLayoutMode(); } - else if (downstreamCompiler == PassThroughMode::Fxc) + else if (compilerType == PassThroughMode::Fxc) { // Set the profile options.profileName = GetHLSLProfileName(profile); } } - // For host callable we want downstream compile to produce a shared library - if (target == CodeGenTarget::HostCallable) + // If we aren't using LLVM 'host callable', we want downstream compile to produce a shared library + if (compilerType != PassThroughMode::LLVM && target == CodeGenTarget::HostCallable) { target = CodeGenTarget::SharedLibrary; } |
