diff options
Diffstat (limited to 'source')
| -rw-r--r-- | source/core/slang-test-tool-util.cpp | 13 | ||||
| -rw-r--r-- | source/core/slang-test-tool-util.h | 3 | ||||
| -rwxr-xr-x | source/slang/slang-compiler.h | 6 | ||||
| -rw-r--r-- | source/slang/slang-options.cpp | 12 | ||||
| -rw-r--r-- | source/slang/slang.cpp | 130 | ||||
| -rw-r--r-- | source/slangc/main.cpp | 32 |
6 files changed, 139 insertions, 57 deletions
diff --git a/source/core/slang-test-tool-util.cpp b/source/core/slang-test-tool-util.cpp index f06095ada..68673a29f 100644 --- a/source/core/slang-test-tool-util.cpp +++ b/source/core/slang-test-tool-util.cpp @@ -37,6 +37,19 @@ namespace Slang } } +/* static */bool TestToolUtil::hasDeferredStdLib(Index argc, const char*const* argv) +{ + for (Index i = 0; i < argc; ++i) + { + UnownedStringSlice option(argv[i]); + if (option == "-load-stdlib" || option == "-compile-stdlib") + { + return true; + } + } + return false; +} + /* static */SlangResult TestToolUtil::getIncludePath(const String& parentPath, const char* path, String& outIncludePath) { String includePath; diff --git a/source/core/slang-test-tool-util.h b/source/core/slang-test-tool-util.h index 07532682b..186003a62 100644 --- a/source/core/slang-test-tool-util.h +++ b/source/core/slang-test-tool-util.h @@ -63,6 +63,9 @@ struct TestToolUtil /// Sets the default preludes on the session based on the executable path static SlangResult setSessionDefaultPreludeFromExePath(const char* exePath, slang::IGlobalSession* session); + + /// Returns true if the StdLib should not be initialized immediately (eg when doing a -load-stdlib). + static bool hasDeferredStdLib(Index numArgs, const char*const* args); }; } // namespace Slang diff --git a/source/slang/slang-compiler.h b/source/slang/slang-compiler.h index 649283f35..45ebc2909 100755 --- a/source/slang/slang-compiler.h +++ b/source/slang/slang-compiler.h @@ -2077,6 +2077,10 @@ namespace Slang SLANG_NO_THROW void SLANG_MCALL setLanguagePrelude(SlangSourceLanguage inSourceLanguage, char const* prelude) override; SLANG_NO_THROW void SLANG_MCALL getLanguagePrelude(SlangSourceLanguage inSourceLanguage, ISlangBlob** outPrelude) override; + SLANG_NO_THROW SlangResult SLANG_MCALL compileStdLib() override; + SLANG_NO_THROW SlangResult SLANG_MCALL loadStdLib() override; + SLANG_NO_THROW SlangResult SLANG_MCALL saveStdLib() override; + /// Get the default compiler for a language DownstreamCompiler* getDefaultDownstreamCompiler(SourceLanguage sourceLanguage); @@ -2132,7 +2136,7 @@ namespace Slang String getCoreLibraryCode(); String getHLSLLibraryCode(); - + RefPtr<SharedASTBuilder> m_sharedASTBuilder; diff --git a/source/slang/slang-options.cpp b/source/slang/slang-options.cpp index 8897a6549..6a5b43876 100644 --- a/source/slang/slang-options.cpp +++ b/source/slang/slang-options.cpp @@ -447,6 +447,18 @@ struct OptionsParser { flags |= SLANG_COMPILE_FLAG_NO_MANGLING; } + else if (argStr == "-load-stdlib") + { + SLANG_RETURN_ON_FAIL(session->loadStdLib()); + } + else if (argStr == "-compile-stdlib") + { + SLANG_RETURN_ON_FAIL(session->compileStdLib()); + } + else if (argStr == "-save-stdlib") + { + SLANG_RETURN_ON_FAIL(session->saveStdLib()); + } else if (argStr == "-no-codegen") { flags |= SLANG_COMPILE_FLAG_NO_CODEGEN; 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; } diff --git a/source/slangc/main.cpp b/source/slangc/main.cpp index 9f4ffcaff..887eca77a 100644 --- a/source/slangc/main.cpp +++ b/source/slangc/main.cpp @@ -66,10 +66,28 @@ static SlangResult _compile(SlangCompileRequest* compileRequest, int argc, const return res; } -SLANG_TEST_TOOL_API SlangResult innerMain(StdWriters* stdWriters, SlangSession* session, int argc, const char*const* argv) +SLANG_TEST_TOOL_API SlangResult innerMain(StdWriters* stdWriters, slang::IGlobalSession* sharedSession, int argc, const char*const* argv) { StdWriters::setSingleton(stdWriters); + // Assume we will used the shared session + ComPtr<slang::IGlobalSession> session(sharedSession); + + // The sharedSession always has a pre-loaded stdlib, is sharedSession is not nullptr. + // This differed test checks if the command line has an option to setup the stdlib. + // If so we *don't* use the sharedSession, and create a new stdlib-less session just for this compilation. + if (TestToolUtil::hasDeferredStdLib(Index(argc - 1), argv + 1)) + { + SLANG_RETURN_ON_FAIL(slang_createGlobalSessionWithoutStdLib(SLANG_API_VERSION, session.writeRef())); + TestToolUtil::setSessionDefaultPreludeFromExePath(argv[0], session); + } + else if (!session) + { + // Just create the global session in the regular way if there isn't one set + SLANG_RETURN_ON_FAIL(slang_createGlobalSession(SLANG_API_VERSION, session.writeRef())); + TestToolUtil::setSessionDefaultPreludeFromExePath(argv[0], session); + } + SlangCompileRequest* compileRequest = spCreateCompileRequest(session); SlangResult res = _compile(compileRequest, argc, argv); // Now that we are done, clean up after ourselves @@ -80,16 +98,8 @@ SLANG_TEST_TOOL_API SlangResult innerMain(StdWriters* stdWriters, SlangSession* int MAIN(int argc, char** argv) { - SlangResult res; - { - SlangSession* session = spCreateSession(nullptr); - TestToolUtil::setSessionDefaultPreludeFromExePath(argv[0], session); - - auto stdWriters = StdWriters::initDefaultSingleton(); - - res = innerMain(stdWriters, session, argc, argv); - spDestroySession(session); - } + auto stdWriters = StdWriters::initDefaultSingleton(); + SlangResult res = innerMain(stdWriters, nullptr, argc, argv); return (int)TestToolUtil::getReturnCode(res); } |
