summaryrefslogtreecommitdiff
path: root/source/slang/slang-compiler.cpp
diff options
context:
space:
mode:
authorjsmall-nvidia <jsmall@nvidia.com>2019-12-19 11:23:14 -0500
committerGitHub <noreply@github.com>2019-12-19 11:23:14 -0500
commite3fe0319467546bae070137c58dcf8f9fbe93c79 (patch)
tree6cc26ccda33725e98c4a9a0408cf31a1348db268 /source/slang/slang-compiler.cpp
parent60934d98fbc20d83b5e149e72a197ec4f5c61580 (diff)
WIP CUDA source emit (#1157)
* 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. * Replace DownstreamCompiler::SourceType -> SlangSourceLanguage * Replace _canCompile with something data driven. * Fix compiling on gcc/clang for DownstreamCompiler. * Moved some text conversions into 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. * WIP: CUDA code generation - currently just using CPU layout and HLSL. * emitXXXForEntryPoint -> emitEntryPointSource emitSourceForEntryPoint -> emitEntryPointSourceFromIR Fix up generating cuda to get PTX. * WIP emitting cuda for IR. * Small improvements to CUDA ouput. * Disable the CUDA emit test, as output not currently compilable.
Diffstat (limited to 'source/slang/slang-compiler.cpp')
-rw-r--r--source/slang/slang-compiler.cpp186
1 files changed, 60 insertions, 126 deletions
diff --git a/source/slang/slang-compiler.cpp b/source/slang/slang-compiler.cpp
index 07bfaacc9..d0fca1ab5 100644
--- a/source/slang/slang-compiler.cpp
+++ b/source/slang/slang-compiler.cpp
@@ -478,8 +478,6 @@ namespace Slang
return PassThroughMode::None;
}
case CodeGenTarget::GLSL:
- case CodeGenTarget::GLSL_Vulkan:
- case CodeGenTarget::GLSL_Vulkan_OneDesc:
{
// Can always output GLSL
return PassThroughMode::None;
@@ -489,6 +487,11 @@ namespace Slang
// Can always output HLSL
return PassThroughMode::None;
}
+ case CodeGenTarget::CUDASource:
+ {
+ // Can always output CUDA
+ return PassThroughMode::None;
+ }
case CodeGenTarget::SPIRVAssembly:
case CodeGenTarget::SPIRV:
{
@@ -504,6 +507,11 @@ namespace Slang
{
return PassThroughMode::Dxc;
}
+ case CodeGenTarget::GLSL_Vulkan:
+ case CodeGenTarget::GLSL_Vulkan_OneDesc:
+ {
+ return PassThroughMode::Glslang;
+ }
case CodeGenTarget::CPPSource:
case CodeGenTarget::CSource:
{
@@ -581,10 +589,11 @@ namespace Slang
outCodeBuilder << fileContent << "\n";
}
- String emitHLSLForEntryPoint(
+ String emitEntryPointSource(
BackEndCompileRequest* compileRequest,
Int entryPointIndex,
TargetRequest* targetReq,
+ CodeGenTarget target,
EndToEndCompileRequest* endToEndReq)
{
if(auto translationUnit = findPassThroughTranslationUnit(endToEndReq, entryPointIndex))
@@ -596,91 +605,41 @@ namespace Slang
// mode.
StringBuilder codeBuilder;
- for(auto sourceFile : translationUnit->getSourceFiles())
- {
- _appendCodeWithPath(sourceFile->getPathInfo().foundPath.getUnownedSlice(), sourceFile->getContent(), codeBuilder);
- }
-
- return codeBuilder.ProduceString();
- }
- else
- {
- return emitEntryPointSource(
- compileRequest,
- entryPointIndex,
- CodeGenTarget::HLSL,
- targetReq);
- }
- }
-
- String emitCPPForEntryPoint(
- BackEndCompileRequest* compileRequest,
- Int entryPointIndex,
- TargetRequest* targetReq,
- EndToEndCompileRequest* endToEndReq)
- {
- if (auto translationUnit = findPassThroughTranslationUnit(endToEndReq, entryPointIndex))
- {
- // Generate a string that includes the content of
- // the source file(s), along with a line directive
- // to ensure that we get reasonable messages
- // from the downstream compiler when in pass-through
- // mode.
-
- StringBuilder codeBuilder;
- for (auto sourceFile : translationUnit->getSourceFiles())
+ if (target == CodeGenTarget::GLSL)
{
- _appendCodeWithPath(sourceFile->getPathInfo().foundPath.getUnownedSlice(), sourceFile->getContent(), codeBuilder);
+ // Special case GLSL
+ int translationUnitCounter = 0;
+ for (auto sourceFile : translationUnit->getSourceFiles())
+ {
+ int translationUnitIndex = translationUnitCounter++;
+
+ // We want to output `#line` directives, but we need
+ // to skip this for the first file, since otherwise
+ // some GLSL implementations will get tripped up by
+ // not having the `#version` directive be the first
+ // thing in the file.
+ if (translationUnitIndex != 0)
+ {
+ codeBuilder << "#line 1 " << translationUnitIndex << "\n";
+ }
+ codeBuilder << sourceFile->getContent() << "\n";
+ }
}
-
- return codeBuilder.ProduceString();
- }
- else
- {
- return emitEntryPointSource(compileRequest, entryPointIndex, CodeGenTarget::CPPSource, targetReq);
- }
- }
-
- String emitGLSLForEntryPoint(
- BackEndCompileRequest* compileRequest,
- Int entryPointIndex,
- TargetRequest* targetReq,
- EndToEndCompileRequest* endToEndReq)
- {
- if(auto translationUnit = findPassThroughTranslationUnit(endToEndReq, entryPointIndex))
- {
- // Generate a string that includes the content of
- // the source file(s), along with a line directive
- // to ensure that we get reasonable messages
- // from the downstream compiler when in pass-through
- // mode.
-
- StringBuilder codeBuilder;
- int translationUnitCounter = 0;
- for(auto sourceFile : translationUnit->getSourceFiles())
+ else
{
- int translationUnitIndex = translationUnitCounter++;
-
- // We want to output `#line` directives, but we need
- // to skip this for the first file, since otherwise
- // some GLSL implementations will get tripped up by
- // not having the `#version` directive be the first
- // thing in the file.
- if(translationUnitIndex != 0)
+ for(auto sourceFile : translationUnit->getSourceFiles())
{
- codeBuilder << "#line 1 " << translationUnitIndex << "\n";
+ _appendCodeWithPath(sourceFile->getPathInfo().foundPath.getUnownedSlice(), sourceFile->getContent(), codeBuilder);
}
- codeBuilder << sourceFile->getContent() << "\n";
}
-
return codeBuilder.ProduceString();
}
else
{
- return emitEntryPointSource(
+ return emitEntryPointSourceFromIR(
compileRequest,
entryPointIndex,
- CodeGenTarget::GLSL,
+ target,
targetReq);
}
}
@@ -964,7 +923,7 @@ namespace Slang
return SLANG_FAIL;
}
- auto hlslCode = emitHLSLForEntryPoint(compileRequest, entryPointIndex, targetReq, endToEndReq);
+ auto hlslCode = emitEntryPointSource(compileRequest, entryPointIndex, targetReq, CodeGenTarget::HLSL, endToEndReq);
maybeDumpIntermediate(compileRequest, hlslCode.getBuffer(), CodeGenTarget::HLSL);
auto profile = getEffectiveProfile(entryPoint, targetReq);
@@ -1251,13 +1210,16 @@ SlangResult dissassembleDXILUsingDXC(
EndToEndCompileRequest* endToEndReq,
RefPtr<DownstreamCompileResult>& outResult)
{
+ outResult.setNull();
+
auto sink = slangRequest->getSink();
auto session = slangRequest->getSession();
const String originalSourcePath = calcSourcePathForEntryPoint(endToEndReq, entryPointIndex);
- outResult.setNull();
+ CodeGenTarget sourceTarget = CodeGenTarget::None;
+ SourceLanguage sourceLanguage = SourceLanguage::Unknown;
PassThroughMode downstreamCompiler = endToEndReq->passThrough;
@@ -1265,23 +1227,26 @@ SlangResult dissassembleDXILUsingDXC(
if (downstreamCompiler == PassThroughMode::None)
{
auto target = targetReq->target;
-
switch (target)
{
case CodeGenTarget::PTX:
{
- downstreamCompiler = PassThroughMode(session->getDefaultDownstreamCompiler(SLANG_SOURCE_LANGUAGE_CUDA));
+ sourceTarget = CodeGenTarget::CUDASource;
+ sourceLanguage = SourceLanguage::CUDA;
break;
}
case CodeGenTarget::HostCallable:
case CodeGenTarget::SharedLibrary:
case CodeGenTarget::Executable:
{
- downstreamCompiler = PassThroughMode(session->getDefaultDownstreamCompiler(SLANG_SOURCE_LANGUAGE_CPP));
+ sourceTarget = CodeGenTarget::CPPSource;
+ sourceLanguage = SourceLanguage::CPP;
break;
}
default: break;
}
+
+ downstreamCompiler = PassThroughMode(session->getDefaultDownstreamCompiler(SlangSourceLanguage(sourceLanguage)));
}
// Get the required downstream compiler
@@ -1301,8 +1266,6 @@ SlangResult dissassembleDXILUsingDXC(
return SLANG_FAIL;
}
- SourceLanguage rawSourceLanguage = SourceLanguage::Unknown;
-
Dictionary<String, String> preprocessorDefinitions;
List<String> includePaths;
@@ -1357,8 +1320,10 @@ SlangResult dissassembleDXILUsingDXC(
}
// We are just passing thru, so it's whatever it originally was
- rawSourceLanguage = translationUnit->sourceLanguage;
+ sourceLanguage = translationUnit->sourceLanguage;
+ sourceTarget = CodeGenTarget(DownstreamCompiler::getCompileTarget(SlangSourceLanguage(sourceLanguage)));
+ // Special case if we have a single file, so that we pass the path, and the contents
const auto& sourceFiles = translationUnit->getSourceFiles();
if (sourceFiles.getCount() == 1)
{
@@ -1372,30 +1337,18 @@ SlangResult dissassembleDXILUsingDXC(
}
else
{
- // If can't just use file, concat together and make
- StringBuilder codeBuilder;
- for (auto sourceFile : translationUnit->getSourceFiles())
- {
- _appendCodeWithPath(sourceFile->getPathInfo().foundPath.getUnownedSlice(), sourceFile->getContent(), codeBuilder);
- }
- options.sourceContents = codeBuilder.ProduceString();
+ options.sourceContents = emitEntryPointSource(slangRequest, entryPointIndex, targetReq, sourceTarget, endToEndReq);
}
}
else
{
- options.sourceContents = emitCPPForEntryPoint(
- slangRequest,
- entryPointIndex,
- targetReq,
- endToEndReq);
-
- maybeDumpIntermediate(slangRequest, options.sourceContents.getBuffer(), CodeGenTarget::CPPSource);
+ options.sourceContents = emitEntryPointSource(slangRequest, entryPointIndex, targetReq, sourceTarget, endToEndReq);
- rawSourceLanguage = SourceLanguage::CPP;
+ maybeDumpIntermediate(slangRequest, options.sourceContents.getBuffer(), sourceTarget);
}
// Set the source type
- options.sourceLanguage = SlangSourceLanguage(rawSourceLanguage);
+ options.sourceLanguage = SlangSourceLanguage(sourceLanguage);
// Disable exceptions and security checks
options.flags &= ~(CompileOptions::Flag::EnableExceptionHandling | CompileOptions::Flag::EnableSecurityChecks);
@@ -1556,10 +1509,11 @@ SlangResult dissassembleDXILUsingDXC(
{
spirvOut.clear();
- String rawGLSL = emitGLSLForEntryPoint(
+ String rawGLSL = emitEntryPointSource(
slangRequest,
entryPointIndex,
targetReq,
+ CodeGenTarget::GLSL,
endToEndReq);
maybeDumpIntermediate(slangRequest, rawGLSL.getBuffer(), CodeGenTarget::GLSL);
@@ -1671,38 +1625,18 @@ SlangResult dissassembleDXILUsingDXC(
}
}
break;
- case CodeGenTarget::HLSL:
- {
- String code = emitHLSLForEntryPoint(
- compileRequest,
- entryPointIndex,
- targetReq,
- endToEndReq);
- maybeDumpIntermediate(compileRequest, code.getBuffer(), target);
- result = CompileResult(code);
- }
- break;
-
case CodeGenTarget::GLSL:
- {
- String code = emitGLSLForEntryPoint(
- compileRequest,
- entryPointIndex,
- targetReq,
- endToEndReq);
- maybeDumpIntermediate(compileRequest, code.getBuffer(), target);
- result = CompileResult(code);
- }
- break;
-
+ case CodeGenTarget::HLSL:
+ case CodeGenTarget::CUDASource:
case CodeGenTarget::CPPSource:
case CodeGenTarget::CSource:
{
String code = emitEntryPointSource(
compileRequest,
entryPointIndex,
- target,
- targetReq);
+ targetReq,
+ target,
+ endToEndReq);
maybeDumpIntermediate(compileRequest, code.getBuffer(), target);
result = CompileResult(code);
}