From bf37b13de74829e47306830ff7e3c66418ccff25 Mon Sep 17 00:00:00 2001 From: Wenju He Date: Thu, 1 Jun 2023 13:48:40 +0800 Subject: [PATCH] revert db7696e and 4a88836 (#427) (#451) 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 5e4e5033..f33f0918 100644 --- a/common_clang.cpp +++ b/common_clang.cpp @@ -87,7 +87,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(); @@ -217,6 +217,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 @@ -228,8 +229,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); @@ -358,7 +357,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 4866fc51..9f2a38f5 100644 --- a/options_compile.cpp +++ b/options_compile.cpp @@ -38,7 +38,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) @@ -410,7 +410,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");