Skip to content

Commit 9e465c7

Browse files
authored
Fix TypeCheckingCache concurrency and candidate lifetime. (#6444)
* Fix TypeCheckingCache concurrency. * Fix.
1 parent edcb2f0 commit 9e465c7

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

source/slang/slang-check-overload.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -2565,7 +2565,7 @@ Expr* SemanticsVisitor::ResolveInvoke(InvokeExpr* expr)
25652565
// We should only use the cached candidate if it is persistent direct declref
25662566
// created from GlobalSession's ASTBuilder, or it is created in the current Linkage.
25672567
if (candidate.cacheVersion == typeCheckingCache->version ||
2568-
as<DirectDeclRef>(candidate.candidate.item.declRef.declRefBase))
2568+
findNextOuterGeneric(candidate.decl) == nullptr)
25692569
{
25702570
context.bestCandidateStorage = candidate.candidate;
25712571
context.bestCandidate = &context.bestCandidateStorage;

source/slang/slang-compiler.h

+1
Original file line numberDiff line numberDiff line change
@@ -3591,6 +3591,7 @@ class Session : public RefObject, public slang::IGlobalSession
35913591

35923592
RefPtr<RefObject> m_typeCheckingCache;
35933593
TypeCheckingCache* getTypeCheckingCache();
3594+
std::mutex m_typeCheckingCacheMutex;
35943595

35953596
private:
35963597
struct BuiltinModuleInfo

source/slang/slang.cpp

+9-4
Original file line numberDiff line numberDiff line change
@@ -809,9 +809,12 @@ Session::createSession(slang::SessionDesc const& inDesc, slang::ISession** outSe
809809

810810
RefPtr<Linkage> linkage = new Linkage(this, astBuilder, getBuiltinLinkage());
811811

812-
if (m_typeCheckingCache)
813-
linkage->m_typeCheckingCache =
814-
new TypeCheckingCache(*static_cast<TypeCheckingCache*>(m_typeCheckingCache.get()));
812+
{
813+
std::lock_guard<std::mutex> lock(m_typeCheckingCacheMutex);
814+
if (m_typeCheckingCache)
815+
linkage->m_typeCheckingCache =
816+
new TypeCheckingCache(*static_cast<TypeCheckingCache*>(m_typeCheckingCache.get()));
817+
}
815818

816819
linkage->setMatrixLayoutMode(desc.defaultMatrixLayoutMode);
817820

@@ -1308,14 +1311,16 @@ Linkage::~Linkage()
13081311
if (m_typeCheckingCache)
13091312
{
13101313
auto globalSession = getSessionImpl();
1314+
std::lock_guard<std::mutex> lock(globalSession->m_typeCheckingCacheMutex);
13111315
if (!globalSession->m_typeCheckingCache ||
13121316
globalSession->getTypeCheckingCache()->resolvedOperatorOverloadCache.getCount() <
13131317
getTypeCheckingCache()->resolvedOperatorOverloadCache.getCount())
13141318
{
13151319
globalSession->m_typeCheckingCache = m_typeCheckingCache;
1320+
getTypeCheckingCache()->version++;
13161321
}
1322+
destroyTypeCheckingCache();
13171323
}
1318-
destroyTypeCheckingCache();
13191324
}
13201325

13211326
SearchDirectoryList& Linkage::getSearchDirectories()

0 commit comments

Comments
 (0)