diff options
| author | Tim Foley <tfoley@nvidia.com> | 2017-08-07 14:54:55 -0700 |
|---|---|---|
| committer | Tim Foley <tfoley@nvidia.com> | 2017-08-07 15:16:54 -0700 |
| commit | 7b54f43fb1b123f451460edb0add218a0428fe95 (patch) | |
| tree | e492a82b13334955c1c56f6e3f9d25e8165de82c /source/slang/compiler.h | |
| parent | ca8eea98c89c632dd7b5a6a8b84d379d1e9e59cf (diff) | |
Remove uses of global variables
There were two main places where global variables were used in the Slang implementation:
1. The "standard library" code was generated as a string at run-time, and stored in a global variable so that it could be amortized across compiles.
2. The representation of types uses some globals (well, class `static` members) to store common types (e.g., `void`) and to deal with memory lifetime for things like canonicalized types.
In each case the "simple" fix is to move the relevant state into the `Session` type that controlled their lifetime already (the `Session` destructor was already cleaning up these globals to avoid leaks).
For the standard library stuff this really was easy, but for the types it required threading through the `Session` a bit carefully.
One more case that I found: there was a function-`static` variable used to generate a unique ID for files output when dumping of intermediates is enabled (this is almost strictly a debugging option).
Rather than make this counter per-session (which would lead to different sessions on different threads clobbering the same few files), I went ahead and used an atomic in this case.
Note that the remaining case I had been worried about was any function-`static` counter that might be used in generating unique names.
It turns out that right now the parser doesn't use such a counter (even in cases where it probably should), and the lowering pass already uses a counter local to the pass (again, whether or not this is a good idea).
This change should be a major step toward allowing an application to use Slang in multiple threads, so long as each thread uses a distinct `SlangSession`. The case of using a single session across multiple threads is harder to support, and will require more careful implementation work.
Diffstat (limited to 'source/slang/compiler.h')
| -rw-r--r-- | source/slang/compiler.h | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/source/slang/compiler.h b/source/slang/compiler.h index 180fb027a..8fcbd444d 100644 --- a/source/slang/compiler.h +++ b/source/slang/compiler.h @@ -309,6 +309,69 @@ namespace Slang CompileRequest* compileRequest, char const* text, CodeGenTarget target); + + // + + class Session + { + public: + // + + RefPtr<Scope> coreLanguageScope; + RefPtr<Scope> hlslLanguageScope; + RefPtr<Scope> slangLanguageScope; + RefPtr<Scope> glslLanguageScope; + + List<RefPtr<ProgramSyntaxNode>> loadedModuleCode; + + + // + + // Generated code for stdlib, etc. + String stdlibPath; + String coreLibraryCode; + String slangLibraryCode; + String hlslLibraryCode; + String glslLibraryCode; + + String getStdlibPath(); + String getCoreLibraryCode(); + String getHLSLLibraryCode(); + String getGLSLLibraryCode(); + + // Basic types that we don't want to re-create all the time + RefPtr<ExpressionType> errorType; + RefPtr<ExpressionType> initializerListType; + RefPtr<ExpressionType> overloadedType; + + Dictionary<int, RefPtr<ExpressionType>> builtinTypes; + Dictionary<String, Decl*> magicDecls; + List<RefPtr<ExpressionType>> canonicalTypes; + + void initializeTypes(); + + ExpressionType* getBoolType(); + ExpressionType* getFloatType(); + ExpressionType* getDoubleType(); + ExpressionType* getIntType(); + ExpressionType* getUIntType(); + ExpressionType* getVoidType(); + ExpressionType* getBuiltinType(BaseType flavor); + + ExpressionType* getInitializerListType(); + ExpressionType* getOverloadedType(); + ExpressionType* getErrorType(); + + // + + Session(); + + void addBuiltinSource( + RefPtr<Scope> const& scope, + String const& path, + String const& source); + }; + } #endif
\ No newline at end of file |
