diff --git a/sycl-jit/jit-compiler/lib/rtc/DeviceCompilation.cpp b/sycl-jit/jit-compiler/lib/rtc/DeviceCompilation.cpp index 95180b10e1e9c..648a541c242ba 100644 --- a/sycl-jit/jit-compiler/lib/rtc/DeviceCompilation.cpp +++ b/sycl-jit/jit-compiler/lib/rtc/DeviceCompilation.cpp @@ -69,20 +69,30 @@ using namespace jit_compiler; namespace { class SYCLToolchain { - // TODO: For some reason, moving this to a data member of the single instance - // of SYCLToolchain results in some data races leading to memory corruption - // (e.g., ::free() report errors). - static auto getToolchainFS() { - llvm::IntrusiveRefCntPtr ToolchainFS = - llvm::makeIntrusiveRefCnt(); - using namespace jit_compiler::resource; - - for (size_t i = 0; i < NumToolchainFiles; ++i) { - resource_file RF = ToolchainFiles[i]; - std::string_view Path{RF.Path.S, RF.Path.Size}; - std::string_view Content{RF.Content.S, RF.Content.Size}; - ToolchainFS->addFile(Path, 0, llvm::MemoryBuffer::getMemBuffer(Content)); - } + static auto &getToolchainFS() { + // TODO: For some reason, removing `thread_local` results in data races + // leading to memory corruption (e.g., ::free() report errors). I'm not sure + // if that's a bug somewhere in clang tooling/LLVMSupport or intentional + // limitation (or maybe a bug in this file, but I can't imagine how could + // that be). + // + // For single thread compilation this gives us [almost] the same performance + // as if there was no `thread_local` so performing very time-consuming + // investigation wouldn't give a justifiable ROI at this moment. + static thread_local const auto ToolchainFS = []() { + llvm::IntrusiveRefCntPtr ToolchainFS = + llvm::makeIntrusiveRefCnt(); + using namespace jit_compiler::resource; + + for (size_t i = 0; i < NumToolchainFiles; ++i) { + resource_file RF = ToolchainFiles[i]; + std::string_view Path{RF.Path.S, RF.Path.Size}; + std::string_view Content{RF.Content.S, RF.Content.Size}; + ToolchainFS->addFile(Path, 0, + llvm::MemoryBuffer::getMemBuffer(Content)); + } + return ToolchainFS; + }(); return ToolchainFS; }