From 4a888364c4169b12fd870e01c7b9b3cac0a4dd70 Mon Sep 17 00:00:00 2001 From: Taylor Lee Date: Wed, 17 Mar 2021 13:01:18 +0900 Subject: [PATCH] Released writeSpirv from locking, too. --- common_clang.cpp | 89 ++++++++++++++++++++++++------------------------ 1 file changed, 44 insertions(+), 45 deletions(-) diff --git a/common_clang.cpp b/common_clang.cpp index d085cd2c..2a70d170 100644 --- a/common_clang.cpp +++ b/common_clang.cpp @@ -294,53 +294,52 @@ Compile(const char *pszProgramSource, const char **pInputHeaders, success = clang::ExecuteCompilerInvocation(compiler.get()); } catch (const std::exception &) { } - { - llvm::sys::SmartScopedLock compileGuard {*compileMutex}; - pResult->setIRType(IR_TYPE_COMPILED_OBJECT); - pResult->setIRName(optionsParser.getSourceName()); - - // Our error handler depends on the Diagnostics object, which we're - // potentially about to delete. Uninstall the handler now so that any - // later errors use the default handling behavior instead. - // (currently commented out since setting the llvm error handling in - // multi-threaded environment is unsupported) - // llvm::remove_fatal_error_handler(); - err_ostream.flush(); - - if (success && (optionsParser.hasEmitSPIRV() || optionsParser.hasEmitSPIRVText())) { - // Translate LLVM IR to SPIR-V. - if (optionsParser.hasEmitSPIRVText()) - SPIRV::SPIRVUseTextFormat = true; - llvm::StringRef LLVM_IR(static_cast(pResult->GetIR()), - pResult->GetIRSize()); - std::unique_ptr MB = llvm::MemoryBuffer::getMemBuffer(LLVM_IR, pResult->GetIRName(), false); - llvm::LLVMContext Context; - auto E = llvm::getOwningLazyBitcodeModule(std::move(MB), Context, - /*ShouldLazyLoadMetadata=*/true); - llvm::logAllUnhandledErrors(E.takeError(), err_ostream, "error: "); - std::unique_ptr M = std::move(*E); - - if (M->materializeAll()) { - if (pBinaryResult) { - *pBinaryResult = nullptr; - } - assert(!"Failed to read just compiled LLVM IR!"); - return CL_COMPILE_PROGRAM_FAILURE; - } - pResult->getIRBufferRef().clear(); - SmallVectorBuffer StreamBuf(pResult->getIRBufferRef()); - std::ostream OS(&StreamBuf); - std::string Err; - SPIRV::TranslatorOpts SPIRVOpts; - SPIRVOpts.enableAllExtensions(); - if (!optionsParser.hasOptDisable()) { - SPIRVOpts.setMemToRegEnabled(true); + pResult->setIRType(IR_TYPE_COMPILED_OBJECT); + pResult->setIRName(optionsParser.getSourceName()); + + // Our error handler depends on the Diagnostics object, which we're + // potentially about to delete. Uninstall the handler now so that any + // later errors use the default handling behavior instead. + // (currently commented out since setting the llvm error handling in + // multi-threaded environment is unsupported) + // llvm::remove_fatal_error_handler(); + err_ostream.flush(); + + if (success && (optionsParser.hasEmitSPIRV() || optionsParser.hasEmitSPIRVText())) { + // Translate LLVM IR to SPIR-V. + if (optionsParser.hasEmitSPIRVText()) + SPIRV::SPIRVUseTextFormat = true; + llvm::StringRef LLVM_IR(static_cast(pResult->GetIR()), + pResult->GetIRSize()); + std::unique_ptr MB = llvm::MemoryBuffer::getMemBuffer(LLVM_IR, pResult->GetIRName(), false); + llvm::LLVMContext Context; + auto E = llvm::getOwningLazyBitcodeModule(std::move(MB), Context, + /*ShouldLazyLoadMetadata=*/true); + llvm::logAllUnhandledErrors(E.takeError(), err_ostream, "error: "); + std::unique_ptr M = std::move(*E); + + if (M->materializeAll()) { + if (pBinaryResult) { + *pBinaryResult = nullptr; } - success = llvm::writeSpirv(M.get(), SPIRVOpts, OS, Err); - err_ostream << Err.c_str(); - err_ostream.flush(); + assert(!"Failed to read just compiled LLVM IR!"); + return CL_COMPILE_PROGRAM_FAILURE; } - + pResult->getIRBufferRef().clear(); + SmallVectorBuffer StreamBuf(pResult->getIRBufferRef()); + std::ostream OS(&StreamBuf); + std::string Err; + SPIRV::TranslatorOpts SPIRVOpts; + SPIRVOpts.enableAllExtensions(); + if (!optionsParser.hasOptDisable()) { + SPIRVOpts.setMemToRegEnabled(true); + } + success = llvm::writeSpirv(M.get(), SPIRVOpts, OS, Err); + err_ostream << Err.c_str(); + err_ostream.flush(); + } + { + llvm::sys::SmartScopedLock compileGuard {*compileMutex}; if (pBinaryResult) { *pBinaryResult = pResult.release(); }