From cf77b1947a1c886f88ce068304ff7da83d86ae2a Mon Sep 17 00:00:00 2001 From: Wenju He Date: Thu, 1 Jun 2023 13:48:22 +0800 Subject: [PATCH] revert db7696e and 4a88836 (#427) (#447) llvm-related variable initialization, clang::ExecuteCompilerInvocation and llvm-spirv aren't thread safe, at least for 90 branch. (cherry picked from commit 76bc7f93d59190d0ffbedab9045b3a1b6effaa58) --- common_clang.cpp | 6 ++---- options_compile.cpp | 4 ++-- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/common_clang.cpp b/common_clang.cpp index 4d4432a8..d5f0362b 100644 --- a/common_clang.cpp +++ b/common_clang.cpp @@ -85,7 +85,7 @@ static volatile bool lazyCCInit = true; // the flag must be 'volatile' to prevent caching in a CPU register static llvm::sys::Mutex lazyCCInitMutex; -static llvm::ManagedStatic > compileMutex; +llvm::ManagedStatic> compileMutex; void CommonClangTerminate() { llvm::llvm_shutdown(); } @@ -199,6 +199,7 @@ Compile(const char *pszProgramSource, const char **pInputHeaders, CommonClangInitialize(); try { + llvm::sys::SmartScopedLock compileGuard{*compileMutex}; std::unique_ptr pResult(new OCLFEBinaryResult()); // Create the clang compiler @@ -210,8 +211,6 @@ Compile(const char *pszProgramSource, const char **pInputHeaders, // Prepare error log llvm::raw_string_ostream err_ostream(pResult->getLogRef()); { - llvm::sys::SmartScopedLock compileGuard {*compileMutex}; - // Parse options optionsParser.processOptions(pszOptions, pszOptionsEx); @@ -330,7 +329,6 @@ Compile(const char *pszProgramSource, const char **pInputHeaders, err_ostream.flush(); } { - llvm::sys::SmartScopedLock compileGuard {*compileMutex}; if (pBinaryResult) { *pBinaryResult = pResult.release(); } diff --git a/options_compile.cpp b/options_compile.cpp index 84997da3..913671d7 100644 --- a/options_compile.cpp +++ b/options_compile.cpp @@ -36,7 +36,7 @@ Copyright (c) Intel Corporation (2009-2017). using namespace llvm::opt; -static llvm::ManagedStatic > compileOptionsMutex; +extern llvm::ManagedStatic> compileMutex; static const OptTable::Info ClangOptionsInfoTable[] = { #define PREFIX(NAME, VALUE) @@ -310,7 +310,7 @@ extern "C" CC_DLL_EXPORT bool CheckCompileOptions(const char *pszOptions, size_t uiUnknownOptionsSize) { // LLVM doesn't guarantee thread safety, // therefore we serialize execution of LLVM code. - llvm::sys::SmartScopedLock compileOptionsGuard {*compileOptionsMutex}; + llvm::sys::SmartScopedLock compileOptionsGuard{*compileMutex}; try { CompileOptionsParser optionsParser("200");