From 94d3f2bd9c5557658751f73bc5fc443b41230d2c Mon Sep 17 00:00:00 2001 From: Yong He Date: Sat, 26 Sep 2020 20:09:50 -0700 Subject: Add API for whole program compilation. (#1562) * Add API for whole program compilation. This change exposes a new target flag: `SLANG_TARGET_FLAG_GENERATE_WHOLE_PROGRAM` that can be set on a target with `spSetTargetFlags`. When this flag is set, `spCompile` function generates target code for the entire input module instead of just the specified entrypoints. The resulting code will include all the entrypoints defined in the input source. The resulting whole program code can be retrieved with two new functions: `spGetTargetCodeBlob` and `spGetTargetHostCallable`. This change also cleans up the unnecessary `entryPointIndices` parameter of `TargetProgram::getOrCreateWholeProgramResult`, and modifies the `cpu-hello-world` example to make use of the new whole-program compilation API to simplify its logic. * Update comments. --- source/slang/slang.cpp | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) (limited to 'source/slang/slang.cpp') diff --git a/source/slang/slang.cpp b/source/slang/slang.cpp index 572c9c3a3..376376f24 100644 --- a/source/slang/slang.cpp +++ b/source/slang/slang.cpp @@ -3613,6 +3613,33 @@ static SlangResult _getEntryPointResult( return SLANG_OK; } +static SlangResult _getWholeProgramResult( + SlangCompileRequest* request, + int targetIndex, + Slang::CompileResult** outCompileResult) +{ + using namespace Slang; + if (!request) + return SLANG_ERROR_INVALID_PARAMETER; + + auto req = Slang::asInternal(request); + auto linkage = req->getLinkage(); + auto program = req->getSpecializedGlobalAndEntryPointsComponentType(); + + Index targetCount = linkage->targets.getCount(); + if ((targetIndex < 0) || (targetIndex >= targetCount)) + { + return SLANG_ERROR_INVALID_PARAMETER; + } + auto targetReq = linkage->targets[targetIndex]; + + auto targetProgram = program->getTargetProgram(targetReq); + if (!targetProgram) + return SLANG_FAIL; + *outCompileResult = &targetProgram->getExistingWholeProgramResult(); + return SLANG_OK; +} + SLANG_API SlangResult spGetEntryPointCodeBlob( SlangCompileRequest* request, int entryPointIndex, @@ -3648,6 +3675,43 @@ SLANG_API SlangResult spGetEntryPointHostCallable( return SLANG_OK; } +SLANG_API SlangResult spGetTargetCodeBlob( + SlangCompileRequest* request, + int targetIndex, + ISlangBlob** outBlob) +{ + using namespace Slang; + if (!outBlob) + return SLANG_ERROR_INVALID_PARAMETER; + Slang::CompileResult* compileResult = nullptr; + SLANG_RETURN_ON_FAIL( + _getWholeProgramResult(request, targetIndex, &compileResult)); + + ComPtr blob; + SLANG_RETURN_ON_FAIL(compileResult->getBlob(blob)); + *outBlob = blob.detach(); + return SLANG_OK; +} + +SLANG_API SlangResult spGetTargetHostCallable( + SlangCompileRequest* request, + int targetIndex, + ISlangSharedLibrary** outSharedLibrary) +{ + using namespace Slang; + if (!outSharedLibrary) + return SLANG_ERROR_INVALID_PARAMETER; + + Slang::CompileResult* compileResult = nullptr; + SLANG_RETURN_ON_FAIL( + _getWholeProgramResult(request, targetIndex, &compileResult)); + + ComPtr sharedLibrary; + SLANG_RETURN_ON_FAIL(compileResult->getSharedLibrary(sharedLibrary)); + *outSharedLibrary = sharedLibrary.detach(); + return SLANG_OK; +} + SLANG_API char const* spGetEntryPointSource( SlangCompileRequest* request, int entryPointIndex) -- cgit v1.2.3