From 7b54f43fb1b123f451460edb0add218a0428fe95 Mon Sep 17 00:00:00 2001 From: Tim Foley Date: Mon, 7 Aug 2017 14:54:55 -0700 Subject: 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. --- source/slang/type-defs.h | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) (limited to 'source/slang/type-defs.h') diff --git a/source/slang/type-defs.h b/source/slang/type-defs.h index 138b0a849..c1818eae0 100644 --- a/source/slang/type-defs.h +++ b/source/slang/type-defs.h @@ -49,11 +49,14 @@ RAW( virtual String ToString() override; virtual RefPtr SubstituteImpl(Substitutions* subst, int* ioDiff) override; - static DeclRefType* Create(DeclRef declRef); + static DeclRefType* Create( + Session* session, + DeclRef declRef); DeclRefType() {} - DeclRefType(DeclRef declRef) + DeclRefType( + DeclRef declRef) : declRef(declRef) {} protected: @@ -66,6 +69,7 @@ END_SYNTAX_CLASS() // Base class for types that can be used in arithmetic expressions ABSTRACT_SYNTAX_CLASS(ArithmeticExpressionType, DeclRefType) RAW( +public: virtual BasicExpressionType* GetScalarType() = 0; ) END_SYNTAX_CLASS() @@ -75,11 +79,9 @@ SYNTAX_CLASS(BasicExpressionType, ArithmeticExpressionType) FIELD(BaseType, BaseType) RAW( - BasicExpressionType() - { - BaseType = Slang::BaseType::Int; - } - BasicExpressionType(Slang::BaseType baseType) + BasicExpressionType() {} + BasicExpressionType( + Slang::BaseType baseType) { BaseType = baseType; } @@ -211,7 +213,9 @@ SYNTAX_CLASS(SamplerStateType, DeclRefType) { SamplerState, SamplerComparisonState, - };) + }; + + ) FIELD(Flavor, flavor) END_SYNTAX_CLASS() @@ -284,6 +288,7 @@ SYNTAX_CLASS(ArrayExpressionType, ExpressionType) RAW( virtual Slang::String ToString() override; + protected: virtual bool EqualsImpl(ExpressionType * type) override; virtual ExpressionType* CreateCanonicalType() override; @@ -336,6 +341,7 @@ END_SYNTAX_CLASS() // A matrix type, e.g., `matrix` SYNTAX_CLASS(MatrixExpressionType, ArithmeticExpressionType) RAW( + ExpressionType* getElementType(); IntVal* getRowCount(); IntVal* getColumnCount(); @@ -355,7 +361,8 @@ SYNTAX_CLASS(NamedExpressionType, ExpressionType) RAW( NamedExpressionType() {} - NamedExpressionType(DeclRef declRef) + NamedExpressionType( + DeclRef declRef) : declRef(declRef) {} @@ -377,6 +384,9 @@ SYNTAX_CLASS(FuncType, ExpressionType) DECL_FIELD(DeclRef, declRef) RAW( + FuncType() + {} + virtual String ToString() override; protected: virtual bool EqualsImpl(ExpressionType * type) override; @@ -393,7 +403,8 @@ SYNTAX_CLASS(GenericDeclRefType, ExpressionType) RAW( GenericDeclRefType() {} - GenericDeclRefType(DeclRef declRef) + GenericDeclRefType( + DeclRef declRef) : declRef(declRef) {} -- cgit v1.2.3