summaryrefslogtreecommitdiffstats
path: root/source/slang/compiler.cpp
diff options
context:
space:
mode:
authorTim Foley <tfoley@nvidia.com>2017-06-28 13:34:38 -0700
committerTim Foley <tfoley@nvidia.com>2017-07-06 09:17:04 -0700
commitf145e09a6dcbcf326f782b3e6a76dbf291c792cf (patch)
tree88a04619ceaaa37b87199dd82334cc9d102c156d /source/slang/compiler.cpp
parentc0d2c17bc73bc2a8863e086af3ea395ad09465ee (diff)
Start to support cross-compilation via "lowering" pass
- The big change here is the introduction of a "lowering" pass that takes an input AST from the semantic checker, and produces an output AST suitable for emitting. The intention is that he lowering pass is responsible for: - Stripping out unused code (when we have enough information to do so), by only outputting declarations that are transitively references from an entry point - When cross-compiling to GLSL, generating a suitable `void main()` entry point to wrap the user-written entry-point function - (Eventually) legalizing types in the program, by scalarizing aggregate types that mix uniform and resource types - (Eventually) instantiating generic declarations so that the resulting code only deals with fully specialized declarations - (Eventually) de-sugaring OOP constructs into basic "structs and functions" form - (Eventually) instantiating code that depends on interface types at the concrete types chosen - It is clear that there is still a lot of work to be done there, to this change is really about getting infrastructure in place without breaking the existing test cases. - One cleanup here is that we get rid of the idea of whole-translation-unit output, since that was specific to HLSL output, and there is really no strong reason for keeping it. Users should now just ask for the output for each entry point that they wanted to generate. - The biggest source of complexity for the lowering process is that it needs to produce the same AST structure as the input, to deal with the complexity of the rewriter case. That is, we need the output to be able to reproduce the input exactly in the case where we are rewriting and nothing needs to change, so the output format needs at least the degrees of freedom of the input. - As a result, we end up having to distinguish "rewriter" and "full" modes in both lowering and code-emit steps, so that we can react appropriately. - Generating a GLSL `main()` also adds a lot of complexity. Right now I'm using the simplest approach, where we always output the Slang/HLSL entry point as an ordinary function (as written) and then emit a simple GLSL `main()` to call it. I generate globals for all the shader inputs/outputs (these need to be scalarized and have explicit `location`s attached), and then collect these into the `struct` types of the original parameters as needed. - This approach will start to have some major down-sides once we have to deal with "arrayed" input/output - A long-term question here is how to replace entry-point parameter types with scalarized and/or "transposed" versions, while still letting the original code work as written (including copying those inputs to temporary arrays) - Split `BlockStatementSyntaxNode` into: - `BlockStmt` which just provides a scope around a `body` statement - `SeqStmt` which just allows multiple statements to be treated as one - Change how we emit `for` loops, to deal with the case where the initialization part might expand into multiple statements - Basically `for(A;B;C) {D}` becomes `{A; for(;B;C) {D}}`, so we can handle arbitrary statements for `A` - As an additional wrinkle, when we are rewriting HLSL, we just generate `A; for(;B;C) {D}` to deal with the broken scoping there - This change is needed because the lowering pass was sometimes expanding the original initialization statement `A` into a block `{A}`. Certainly if it declared multiple variables we'd need to handle it, and this seemed the easiest way - A more significant challenge for lowering would come if/when we ever wanted to support true short-circuiting behavior for `&&` and `||` - For right now I'm not changing the behavior of the "rewriter" mode, so we still have `UnparsedStmt` instances being generated, but it is clear that eventually we need to parse *all* input, even if we can't type-check 100% of it. This is required so that we can rewrite user code that might refer to a shader input with interface type.
Diffstat (limited to 'source/slang/compiler.cpp')
-rw-r--r--source/slang/compiler.cpp73
1 files changed, 18 insertions, 55 deletions
diff --git a/source/slang/compiler.cpp b/source/slang/compiler.cpp
index 9132624f9..19623a7aa 100644
--- a/source/slang/compiler.cpp
+++ b/source/slang/compiler.cpp
@@ -55,10 +55,11 @@ namespace Slang
//
- String emitHLSLForTranslationUnit(
- TranslationUnitRequest* translationUnit)
+ String emitHLSLForEntryPoint(
+ EntryPointRequest* entryPoint)
{
- auto compileRequest = translationUnit->compileRequest;
+ auto compileRequest = entryPoint->compileRequest;
+ auto translationUnit = entryPoint->getTranslationUnit();
if (compileRequest->passThrough != PassThroughMode::None)
{
// Generate a string that includes the content of
@@ -92,8 +93,8 @@ namespace Slang
}
else
{
- return emitProgram(
- translationUnit->SyntaxNode.Ptr(),
+ return emitEntryPoint(
+ entryPoint,
compileRequest->layout.Ptr(),
CodeGenTarget::HLSL);
}
@@ -137,8 +138,8 @@ namespace Slang
{
// TODO(tfoley): need to pass along the entry point
// so that we properly emit it as the `main` function.
- return emitProgram(
- translationUnit->SyntaxNode.Ptr(),
+ return emitEntryPoint(
+ entryPoint,
compileRequest->layout.Ptr(),
CodeGenTarget::GLSL);
}
@@ -180,15 +181,7 @@ namespace Slang
assert(D3DCompile_);
}
- // The HLSL compiler will try to "canonicalize" our input file path,
- // and we don't want it to do that, because they it won't report
- // the same locations on error messages that we would.
- //
- // To work around that, we prepend a custom `#line` directive.
-
- auto translationUnit = entryPoint->getTranslationUnit();
-
- auto hlslCode = emitHLSLForTranslationUnit(translationUnit);
+ auto hlslCode = emitHLSLForEntryPoint(entryPoint);
ID3DBlob* codeBlob;
ID3DBlob* diagnosticsBlob;
@@ -223,6 +216,7 @@ namespace Slang
if (FAILED(hr))
{
// TODO(tfoley): What to do on failure?
+ exit(1);
}
return data;
}
@@ -400,6 +394,13 @@ namespace Slang
switch (compileRequest->Target)
{
+ case CodeGenTarget::HLSL:
+ {
+ String code = emitHLSLForEntryPoint(entryPoint);
+ result.outputSource = code;
+ }
+ break;
+
case CodeGenTarget::GLSL:
{
String code = emitGLSLForEntryPoint(entryPoint);
@@ -505,45 +506,7 @@ namespace Slang
TranslationUnitResult emitTranslationUnit(
TranslationUnitRequest* translationUnit)
{
- auto compileRequest = translationUnit->compileRequest;
-
- // Most of our code generation targets will require us
- // to proceed through one entry point at a time, but
- // in some cases we can emit an entire translation unit
- // in one go.
-
- switch (compileRequest->Target)
- {
- default:
- // The default behavior is going to loop over all the entry
- // points, and then collect an aggregate result.
- return emitTranslationUnitEntryPoints(translationUnit);
-
- case CodeGenTarget::HLSL:
- // When targetting HLSL, we can emit the entire translation unit
- // as a single HLSL program, and include all the entry points.
- {
-
- String hlsl = emitHLSLForTranslationUnit(translationUnit);
-
- TranslationUnitResult result;
- result.outputSource = hlsl;
-
- // Because the user might ask for per-entry-point source,
- // we will just attach the same string as the result for
- // each entry point.
- for( auto& entryPoint : translationUnit->entryPoints )
- {
- EntryPointResult entryPointResult;
- entryPointResult.outputSource = hlsl;
-
- entryPoint->result = entryPointResult;
- }
-
- return result;
- }
- break;
- }
+ return emitTranslationUnitEntryPoints(translationUnit);
}
#if 0