diff --git a/source/slang/slang-ast-base.cpp b/source/slang/slang-ast-base.cpp index a1624fcba5..ac18da4044 100644 --- a/source/slang/slang-ast-base.cpp +++ b/source/slang/slang-ast-base.cpp @@ -23,15 +23,7 @@ void NodeBase::_initDebug(ASTNodeType inAstNodeType, ASTBuilder* inAstBuilder) } DeclRefBase* Decl::getDefaultDeclRef() { - if (auto astBuilder = getCurrentASTBuilder()) - { - const Index currentEpoch = astBuilder->getEpoch(); - if (currentEpoch != m_defaultDeclRefEpoch || !m_defaultDeclRef) - { - m_defaultDeclRef = astBuilder->getOrCreate(this); - m_defaultDeclRefEpoch = currentEpoch; - } - } + SLANG_ASSERT(m_defaultDeclRef); return m_defaultDeclRef; } diff --git a/source/slang/slang-ast-base.h b/source/slang/slang-ast-base.h index 2b5de61e88..8c2f7afa73 100644 --- a/source/slang/slang-ast-base.h +++ b/source/slang/slang-ast-base.h @@ -793,7 +793,6 @@ class Decl : public DeclBase private: SLANG_UNREFLECTED DeclRefBase* m_defaultDeclRef = nullptr; - SLANG_UNREFLECTED Index m_defaultDeclRefEpoch = -1; }; class Expr : public SyntaxNode diff --git a/source/slang/slang-ast-builder.h b/source/slang/slang-ast-builder.h index bd5c9b4e39..cae380e40c 100644 --- a/source/slang/slang-ast-builder.h +++ b/source/slang/slang-ast-builder.h @@ -693,7 +693,10 @@ class ASTBuilder : public RefObject auto val = (Val*)(node); val->m_resolvedValEpoch = getEpoch(); } - + else if (node->getClassInfo().isSubClassOf(*ASTClassInfo::getInfo(Decl::kType))) + { + ((Decl*)node)->m_defaultDeclRef = getOrCreate((Decl*)node); + } return node; } diff --git a/source/slang/slang.cpp b/source/slang/slang.cpp index 38988ca466..5ec1996581 100644 --- a/source/slang/slang.cpp +++ b/source/slang/slang.cpp @@ -4598,7 +4598,7 @@ void Module::_processFindDeclsExportSymbolsRec(Decl* decl) if (_canExportDeclSymbol(decl->astNodeType)) { // It's a reference to a declaration in another module, so first get the symbol name. - String mangledName = getMangledName(getASTBuilder(), decl); + String mangledName = getMangledName(getCurrentASTBuilder(), decl); Index index = Index(m_mangledExportPool.add(mangledName)); diff --git a/source/slang/slang.natvis b/source/slang/slang.natvis index 70b57e3397..341a390f2a 100644 --- a/source/slang/slang.natvis +++ b/source/slang/slang.natvis @@ -426,7 +426,8 @@ DeclRefType#{_debugUID} {*(Val*)(((Slang::DeclRefType*)this)->m_operands.m_buffer[0].values.nodeOperand)} DeclRefType {*(Val*)(((Slang::DeclRefType*)this)->m_operands.m_buffer[0].values.nodeOperand)} - DirectRef {*(Decl*)m_operands.m_buffer[0].values.nodeOperand} + DirectRef#{_debugUID} {*(Decl*)m_operands.m_buffer[0].values.nodeOperand} + DirectRef {*(Decl*)m_operands.m_buffer[0].values.nodeOperand} {astNodeType,en} #{_debugUID} {astNodeType,en} diff --git a/tools/slang-unit-test/unit-test-module-ptr.cpp b/tools/slang-unit-test/unit-test-module-ptr.cpp new file mode 100644 index 0000000000..1c11b69fb5 --- /dev/null +++ b/tools/slang-unit-test/unit-test-module-ptr.cpp @@ -0,0 +1,92 @@ +// unit-test-module-ptr.cpp + +#include "core/slang-memory-file-system.h" +#include "slang-com-ptr.h" +#include "slang.h" +#include "unit-test/slang-unit-test.h" + +#include +#include + +using namespace Slang; + +SLANG_UNIT_TEST(modulePtr) +{ + const char* testModuleSource = R"( + module test_module; + + public void atomicFunc(__ref Atomic ptr) { + ptr.add(1); + } + )"; + + const char* testSource = R"( + import "test_module"; + + RWStructuredBuffer> input0; + + [shader("compute")] + [numthreads(1,1,1)] + void computeMain(uint3 workGroup : SV_GroupID) + { + atomicFunc(input0[0]); + } + )"; + ComPtr memoryFileSystem = + ComPtr(new Slang::MemoryFileSystem()); + + ComPtr globalSession; + SLANG_CHECK(slang_createGlobalSession(SLANG_API_VERSION, globalSession.writeRef()) == SLANG_OK); + slang::TargetDesc targetDesc = {}; + targetDesc.format = SLANG_SPIRV; + targetDesc.profile = globalSession->findProfile("spirv_1_5"); + slang::SessionDesc sessionDesc = {}; + sessionDesc.targetCount = 1; + sessionDesc.targets = &targetDesc; + sessionDesc.compilerOptionEntryCount = 0; + sessionDesc.fileSystem = memoryFileSystem; + + // Precompile test_module to file. + { + ComPtr session; + SLANG_CHECK(globalSession->createSession(sessionDesc, session.writeRef()) == SLANG_OK); + + ComPtr diagnosticBlob; + auto module = session->loadModuleFromSourceString( + "test_module", + "test_module.slang", + testModuleSource, + diagnosticBlob.writeRef()); + SLANG_CHECK(module != nullptr); + + ComPtr moduleBlob; + module->serialize(moduleBlob.writeRef()); + memoryFileSystem->saveFile( + "test_module.slang-module", + moduleBlob->getBufferPointer(), + moduleBlob->getBufferSize()); + } + + // compile test. + { + ComPtr session; + SLANG_CHECK(globalSession->createSession(sessionDesc, session.writeRef()) == SLANG_OK); + + ComPtr diagnosticBlob; + auto module = session->loadModuleFromSourceString( + "test", + "test.slang", + testSource, + diagnosticBlob.writeRef()); + SLANG_CHECK(module != nullptr); + + ComPtr linkedProgram; + module->link(linkedProgram.writeRef()); + + ComPtr code; + + linkedProgram->getTargetCode(0, code.writeRef(), diagnosticBlob.writeRef()); + + SLANG_CHECK(code->getBufferSize() > 0); + } +}