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/lookup.cpp | 64 ++++++++++++++++++++++++++++++++++++------------- 1 file changed, 48 insertions(+), 16 deletions(-) (limited to 'source/slang/lookup.cpp') diff --git a/source/slang/lookup.cpp b/source/slang/lookup.cpp index f5e472ec5..b0e9ce971 100644 --- a/source/slang/lookup.cpp +++ b/source/slang/lookup.cpp @@ -22,8 +22,9 @@ struct BreadcrumbInfo }; void DoLocalLookupImpl( + Session* session, String const& name, - DeclRef containerDeclRef, + DeclRef containerDeclRef, LookupRequest const& request, LookupResult& result, BreadcrumbInfo* inBreadcrumbs); @@ -151,6 +152,7 @@ LookupResultItem CreateLookupResultItem( } void DoMemberLookupImpl( + Session* session, String const& name, RefPtr baseType, LookupRequest const& request, @@ -168,7 +170,9 @@ void DoMemberLookupImpl( derefBreacrumb.prev = breadcrumbs; // Recursively perform lookup on the result of deref - return DoMemberLookupImpl(name, pointerLikeType->elementType, request, ioResult, &derefBreacrumb); + return DoMemberLookupImpl( + session, + name, pointerLikeType->elementType, request, ioResult, &derefBreacrumb); } // Default case: no dereference needed @@ -177,7 +181,9 @@ void DoMemberLookupImpl( { if (auto baseAggTypeDeclRef = baseDeclRefType->declRef.As()) { - DoLocalLookupImpl(name, baseAggTypeDeclRef, request, ioResult, breadcrumbs); + DoLocalLookupImpl( + session, + name, baseAggTypeDeclRef, request, ioResult, breadcrumbs); } } @@ -185,18 +191,24 @@ void DoMemberLookupImpl( } void DoMemberLookupImpl( - String const& name, - DeclRef baseDeclRef, + Session* session, + String const& name, + DeclRef baseDeclRef, LookupRequest const& request, LookupResult& ioResult, BreadcrumbInfo* breadcrumbs) { - auto baseType = getTypeForDeclRef(baseDeclRef); - return DoMemberLookupImpl(name, baseType, request, ioResult, breadcrumbs); + auto baseType = getTypeForDeclRef( + session, + baseDeclRef); + return DoMemberLookupImpl( + session, + name, baseType, request, ioResult, breadcrumbs); } // Look for members of the given name in the given container for declarations void DoLocalLookupImpl( + Session* session, String const& name, DeclRef containerDeclRef, LookupRequest const& request, @@ -246,13 +258,21 @@ void DoLocalLookupImpl( memberRefBreadcrumb.declRef = transparentMemberDeclRef; memberRefBreadcrumb.prev = inBreadcrumbs; - DoMemberLookupImpl(name, transparentMemberDeclRef, request, result, &memberRefBreadcrumb); + DoMemberLookupImpl( + session, + name, + transparentMemberDeclRef, + request, + result, + &memberRefBreadcrumb); } // Consider lookup via extension if( auto aggTypeDeclRef = containerDeclRef.As() ) { - RefPtr type = DeclRefType::Create(aggTypeDeclRef); + RefPtr type = DeclRefType::Create( + session, + aggTypeDeclRef); for (auto ext = GetCandidateExtensions(aggTypeDeclRef); ext; ext = ext->nextCandidateExtension) { @@ -264,12 +284,15 @@ void DoLocalLookupImpl( // the constructed result can somehow indicate that a member // was found through an extension. - DoLocalLookupImpl(name, extDeclRef, request, result, inBreadcrumbs); + DoLocalLookupImpl( + session, + name, extDeclRef, request, result, inBreadcrumbs); } } } void DoLookupImpl( + Session* session, String const& name, LookupRequest const& request, LookupResult& result) @@ -301,7 +324,9 @@ void DoLookupImpl( { if( auto genericTypeParam = pp.As() ) { - subst->args.Add(DeclRefType::Create(DeclRef(genericTypeParam.Ptr(), nullptr))); + subst->args.Add(DeclRefType::Create( + session, + DeclRef(genericTypeParam.Ptr(), nullptr))); } else if( auto genericValParam = pp.As() ) { @@ -311,7 +336,9 @@ void DoLookupImpl( } DeclRef containerRef = DeclRef(containerDecl, subst).As(); - DoLocalLookupImpl(name, containerRef, request, result, nullptr); + DoLocalLookupImpl( + session, + name, containerRef, request, result, nullptr); } if (result.isValid()) @@ -325,14 +352,18 @@ void DoLookupImpl( // If we run out of scopes, then we are done. } -LookupResult DoLookup(String const& name, LookupRequest const& request) +LookupResult DoLookup( + Session* session, + String const& name, + LookupRequest const& request) { LookupResult result; - DoLookupImpl(name, request, result); + DoLookupImpl(session, name, request, result); return result; } LookupResult LookUp( + Session* session, SemanticsVisitor* semantics, String const& name, RefPtr scope) @@ -340,12 +371,13 @@ LookupResult LookUp( LookupRequest request; request.semantics = semantics; request.scope = scope; - return DoLookup(name, request); + return DoLookup(session, name, request); } // perform lookup within the context of a particular container declaration, // and do *not* look further up the chain LookupResult LookUpLocal( + Session* session, SemanticsVisitor* semantics, String const& name, DeclRef containerDeclRef) @@ -354,7 +386,7 @@ LookupResult LookUpLocal( request.semantics = semantics; LookupResult result; - DoLocalLookupImpl(name, containerDeclRef, request, result, nullptr); + DoLocalLookupImpl(session, name, containerDeclRef, request, result, nullptr); return result; } -- cgit v1.2.3