diff options
| author | jsmall-nvidia <jsmall@nvidia.com> | 2020-11-18 18:12:43 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-11-18 18:12:43 -0500 |
| commit | e140c4950eb8c69606386ca57284c0655513b9e1 (patch) | |
| tree | 1361dcf8cfc1597b63960226006af301ac82bfc6 /source/slang/slang.cpp | |
| parent | d898d561e3c76ecf38db434ec7fbb4bbd0e25cb2 (diff) | |
Test for serializing out and reading back Stdlib (#1605)
* #include an absolute path didn't work - because paths were taken to always be relative.
* Mangling/module name extraction for GenericDecl
* Add comment on SerialFilter to explain re-enabling Stmt.
* Support setting up SyntaxDecl when reconstructed after deserialization.
* Improvements to setup SyntaxDecl.
* Fix typo so can read compressed SourceLocs.
* Fix issue with SourceManger.
* Simple test for serializing out stdlib and reading back in.
* Fix calling convention.
* Add override to StdLib impls.
* Fix typo.
* Apply testing to an actual compute test when using load-stdlib
Make -load/compile-stdlib processable by Slang
Move out testing into util into TestToolUtil so can be shared.
* Slightly more concise setup of session.
* Fix some errors introduced with session handling.
* Made setup for compile same across slangc and slangc-tool.
Diffstat (limited to 'source/slang/slang.cpp')
| -rw-r--r-- | source/slang/slang.cpp | 130 |
1 files changed, 85 insertions, 45 deletions
diff --git a/source/slang/slang.cpp b/source/slang/slang.cpp index 6f670167d..d61814136 100644 --- a/source/slang/slang.cpp +++ b/source/slang/slang.cpp @@ -152,7 +152,6 @@ void Session::init() // m_builtinLinkage->_stopRetainingParentSession(); - // Create scopes for various language builtins. // // TODO: load these on-demand to avoid parsing @@ -174,44 +173,6 @@ void Session::init() slangLanguageScope = new Scope(); slangLanguageScope->nextSibling = hlslLanguageScope; - if (false) - { - // Let's try loading serialized modules and adding them - _readBuiltinModule(coreLanguageScope, "core"); - _readBuiltinModule(hlslLanguageScope, "hlsl"); - } - else - { - addBuiltinSource(coreLanguageScope, "core", getCoreLibraryCode()); - addBuiltinSource(hlslLanguageScope, "hlsl", getHLSLLibraryCode()); - - // Write out - if (false) - { - for (auto& pair : m_builtinLinkage->mapNameToLoadedModules) - { - const Name* moduleName = pair.Key; - Module* module = pair.Value; - - // Set up options - SerialContainerUtil::WriteOptions options; - - options.optionFlags |= SerialOptionFlag::SourceLocation; - // TODO(JS): Should this be the Session::getBuiltinSourceManager() - options.sourceManager = m_builtinLinkage->getSourceManager(); - - StringBuilder builder; - builder << moduleName->text << ".slang-module"; - - FileStream stream(builder.ProduceString(), FileMode::Create, FileAccess::Write, FileShare::ReadWrite); - if (SLANG_FAILED(SerialContainerUtil::write(module, options, &stream))) - { - SLANG_UNEXPECTED("Unable to load stdlib"); - } - } - } - } - { for (Index i = 0; i < Index(SourceLanguage::CountOf); ++i) { @@ -228,6 +189,66 @@ void Session::init() m_languagePreludes[Index(SourceLanguage::HLSL)] = get_slang_hlsl_prelude(); } +SlangResult Session::compileStdLib() +{ + if (m_builtinLinkage->mapNameToLoadedModules.Count()) + { + // Already have a StdLib loaded + return SLANG_FAIL; + } + + // TODO(JS): Could make this return a SlangResult as opposed to exception + addBuiltinSource(coreLanguageScope, "core", getCoreLibraryCode()); + addBuiltinSource(hlslLanguageScope, "hlsl", getHLSLLibraryCode()); + return SLANG_OK; +} + +SlangResult Session::loadStdLib() +{ + if (m_builtinLinkage->mapNameToLoadedModules.Count()) + { + // Already have a StdLib loaded + return SLANG_FAIL; + } + + // Let's try loading serialized modules and adding them + SLANG_RETURN_ON_FAIL(_readBuiltinModule(coreLanguageScope, "core")); + SLANG_RETURN_ON_FAIL(_readBuiltinModule(hlslLanguageScope, "hlsl")); + return SLANG_OK; +} + +SlangResult Session::saveStdLib() +{ + if (m_builtinLinkage->mapNameToLoadedModules.Count() == 0) + { + // There is no standard lib loaded + return SLANG_FAIL; + } + + for (auto& pair : m_builtinLinkage->mapNameToLoadedModules) + { + const Name* moduleName = pair.Key; + Module* module = pair.Value; + + // Set up options + SerialContainerUtil::WriteOptions options; + + // Save with SourceLocation information + options.optionFlags |= SerialOptionFlag::SourceLocation; + + // TODO(JS): Should this be the Session::getBuiltinSourceManager()? + options.sourceManager = m_builtinLinkage->getSourceManager(); + + StringBuilder builder; + builder << moduleName->text << ".slang-module"; + + FileStream stream(builder.ProduceString(), FileMode::Create, FileAccess::Write, FileShare::ReadWrite); + SLANG_RETURN_ON_FAIL(SerialContainerUtil::write(module, options, &stream)); + } + + return SLANG_OK; +} + SlangResult Session::_readBuiltinModule(Scope* scope, String moduleName) { StringBuilder moduleFilename; @@ -3163,22 +3184,41 @@ Session::~Session() SLANG_API SlangSession* spCreateSession(const char*) { - Slang::RefPtr<Slang::Session> session(new Slang::Session()); - session->init(); + Slang::ComPtr<slang::IGlobalSession> globalSession; + if (SLANG_FAILED(slang_createGlobalSession(SLANG_API_VERSION, globalSession.writeRef()))) + { + return nullptr; + } // Will be returned with a refcount of 1 - return asExternal(session.detach()); + return globalSession.detach(); } SLANG_API SlangResult slang_createGlobalSession( SlangInt apiVersion, slang::IGlobalSession** outGlobalSession) { - if(apiVersion != 0) + Slang::ComPtr<slang::IGlobalSession> globalSession; + SLANG_RETURN_ON_FAIL(slang_createGlobalSessionWithoutStdLib(apiVersion, globalSession.writeRef())); + SLANG_RETURN_ON_FAIL(globalSession->compileStdLib()); + *outGlobalSession = globalSession.detach(); + return SLANG_OK; +} + +SLANG_API SlangResult slang_createGlobalSessionWithoutStdLib( + SlangInt apiVersion, + slang::IGlobalSession** outGlobalSession) +{ + if (apiVersion != 0) return SLANG_E_NOT_IMPLEMENTED; - Slang::RefPtr<Slang::Session> globalSession(new Slang::Session()); + // Create the session + Slang::Session* globalSession = new Slang::Session(); + // Put an interface ref on it + Slang::ComPtr<slang::IGlobalSession> result(globalSession); + + // Initialize it globalSession->init(); - Slang::ComPtr<slang::IGlobalSession> result(Slang::asExternal(globalSession)); + *outGlobalSession = result.detach(); return SLANG_OK; } |
