From 5c7fac4f45290d7d102a035579ae4c4a75d6e496 Mon Sep 17 00:00:00 2001 From: Wenju He Date: Wed, 17 May 2023 11:12:26 +0800 Subject: [PATCH] revert db7696e and 4a88836 (#427) 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 79abbd79..426235ba 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(); } @@ -212,6 +212,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 @@ -223,8 +224,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); @@ -343,7 +342,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 1c734ef9..db48db1c 100644 --- a/options_compile.cpp +++ b/options_compile.cpp @@ -39,7 +39,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) @@ -430,7 +430,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");