summaryrefslogtreecommitdiff
path: root/source/slang/compiler.h
diff options
context:
space:
mode:
authorTim Foley <tfoley@nvidia.com>2017-06-12 12:26:12 -0700
committerTim Foley <tfoley@nvidia.com>2017-06-13 13:53:47 -0700
commitd81c347e0edcbbf181885baf2b13978c28dfc9a8 (patch)
treeed48ba12c7db141d517b29343b9127692e72ad8c /source/slang/compiler.h
parenta14f3c50ef2cf7d3e34e729dfe10ce1cec880955 (diff)
First pass at support for cross-compilation
This is a large change that contains many pieces: - Update the `cross-compile0` test to actually make use of cross compilation. Now the `cross-compile0.hlsl` file contains both HLSL and GLSL source code, and then imports code from `cross-compile0.slang`, which provides a "library" (one function) that can be shared between both the HLSL and GLSL version of things. - Fixed a bug in the support for backslash-escaped newlines. - Added a new `__import` declaration type (replaces the `using` directive that was still around in a vestigial form) An `__import` causes the compiler to look for a Slang source file (currently using the ordinary `#include` lookup logic), and then parse/check the found file as an additional module ("translation unit"), before making its declarations visible in the current scope. - Refactored the main compilation flow to be simpler. There were the `ShaderCompiler` and `ShaderCompilerImpl` classes that weren't relaly doing anything, but added complexity to the whole workflow. - The `render-test` application has been heavily modified to better support testing cross-compilation workflows. At the most basic level we are starting to distinguish pass-through vs. rewriter workflows, and are passing various `#define`s down to the compiler(s) to let the source code be customized as needed for each case. Several annoying corner cases are caused here by having to support the GLSL compilation model, which really wants each entry point in its own specific translation unit, whereas we really want to keep things nicely contained in single files. - Added support for `__intrinsic` operations to have target-specific behavior. This allows a function to be given a different name for some specific target (so a call gets emitted as a call to that other operation). More generally, the library writer can put together an arbitrary format string that will be used in place of expressions that call the given function, e.g.: __intrinsic(hlsl, "$1 - $0") __intrinsic int foo(int a, int b); Given this declaration, a call like `foo(x,y)` will code generate as `x - y` for HLSL, and as `foo(x,y)` for all other targets. Annoying things still to be dealt with: - The way that I'm filtering the user-provided options when passing things down to the compilation of dynamically loaded modules is a bit ad hoc. It would be good to have a systematic notion of which options will be inherited and which won't. There is also more code duplication than I'd like, so we risk having the compiler behave differently when compiling a file at the top level, vs. because of `__import`. - Adding target-specific behavior to intrinsics is all well and good, but the current approach means we can only add this to the original declaration, which limits the ability to easily extend the set of targets. A better approach long-term would be to add a more robust notion of target-based overload resolution (which would happen after semantic checking). Then one mechanism would be used to find the right target-specific overload to use for an operation, and then each (target-specific) definition could use a simpler attribute to intercept code-generation behavior. Note that we might eventually need a similar notion to deal with stage- or profile-specific functions and the overloading behavior around them, so using this for intrinsics doesn't seem like a bad idea.
Diffstat (limited to 'source/slang/compiler.h')
-rw-r--r--source/slang/compiler.h56
1 files changed, 46 insertions, 10 deletions
diff --git a/source/slang/compiler.h b/source/slang/compiler.h
index f35d6603c..01db7874f 100644
--- a/source/slang/compiler.h
+++ b/source/slang/compiler.h
@@ -17,6 +17,7 @@ namespace Slang
{
class ILConstOperand;
struct IncludeHandler;
+ struct CompileRequest;
enum class CompilerMode
{
@@ -87,28 +88,31 @@ namespace Slang
// The source file(s) that will be compiled to form this translation unit
List<RefPtr<SourceFile> > sourceFiles;
+
+ // Preprocessor definitions to use for this translation unit only
+ // (whereas the ones on `CompileOptions` will be shared)
+ Dictionary<String, String> preprocessorDefinitions;
};
class CompileOptions
{
public:
- CompilerMode Mode = CompilerMode::ProduceShader;
+ // What target language are we compiling to?
CodeGenTarget Target = CodeGenTarget::Unknown;
- StageTarget stage = StageTarget::Unknown;
- EnumerableDictionary<String, String> BackendArguments;
- String SymbolToCompile;
- String outputName;
- List<String> TemplateShaderArguments;
+ // Directories to search for `#include` files or `import`ed modules
List<String> SearchDirectories;
- Dictionary<String, String> PreprocessorDefinitions;
+ // Definitions to provide during preprocessing
+ Dictionary<String, String> preprocessorDefinitions;
+
+ // Translation units we are being asked to compile
List<TranslationUnitOptions> translationUnits;
- // the code generation profile we've been asked to use
+ // The code generation profile we've been asked to use.
Profile profile;
- // should we just pass the input to another compiler?
+ // Should we just pass the input to another compiler?
PassThroughMode passThrough = PassThroughMode::None;
// Flags supplied through the API
@@ -133,13 +137,34 @@ namespace Slang
RefPtr<ProgramLayout> layout;
};
+ // Context information for code generation
+ struct ExtraContext
+ {
+ CompileOptions const* options = nullptr;
+ TranslationUnitOptions const* translationUnitOptions = nullptr;
+
+ CompileResult* compileResult = nullptr;
+
+ RefPtr<ProgramSyntaxNode> programSyntax;
+ ProgramLayout* programLayout;
+
+ String sourceText;
+ String sourcePath;
+
+ CompileOptions const& getOptions() { return *options; }
+ TranslationUnitOptions const& getTranslationUnitOptions() { return *translationUnitOptions; }
+ };
+
+#if 0
+
class ShaderCompiler : public CoreLib::Basic::Object
{
public:
virtual void Compile(
CompileResult& result,
CollectionOfTranslationUnits* collectionOfTranslationUnits,
- const CompileOptions& options) = 0;
+ const CompileOptions& options,
+ CompileRequest* request) = 0;
virtual TranslationUnitResult PassThrough(
String const& sourceText,
@@ -150,6 +175,17 @@ namespace Slang
};
ShaderCompiler * CreateShaderCompiler();
+#endif
+
+ TranslationUnitResult passThrough(
+ String const& sourceText,
+ String const& sourcePath,
+ const CompileOptions & options,
+ TranslationUnitOptions const& translationUnitOptions);
+
+ void generateOutput(
+ ExtraContext& context,
+ CollectionOfTranslationUnits* collectionOfTranslationUnits);
}
}