Skip to content

Commit

Permalink
Serialize execution of LLVM code in CommonClang
Browse files Browse the repository at this point in the history
Change-Id: Iacd0f6e81206a28eaa0cfd8ca84d6b08d977dcb5
  • Loading branch information
AlexeySotkin committed Feb 24, 2018
1 parent b77e1bc commit 9d17e0b
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
6 changes: 6 additions & 0 deletions cclang/common_clang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ 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<llvm::sys::SmartMutex<true> > compileMutex;

void CommonClangTerminate() { llvm::llvm_shutdown(); }

// This function mustn't be invoked from a static object constructor,
Expand Down Expand Up @@ -183,6 +185,10 @@ Compile(const char *pszProgramSource, const char **pInputHeaders,
try {
std::unique_ptr<OCLFEBinaryResult> pResult(new OCLFEBinaryResult());

// LLVM doesn't guarantee thread safety,
// therefore we serialize execution of LLVM code.
llvm::sys::SmartScopedLock<true> compileGuard {*compileMutex};

// Parse options
CompileOptionsParser optionsParser(pszOpenCLVer);
optionsParser.processOptions(pszOptions, pszOptionsEx);
Expand Down
8 changes: 8 additions & 0 deletions cclang/getkernelarginfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ Copyright (c) Intel Corporation (2009-2017).
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Metadata.h"
#include "llvm/IR/Module.h"
#include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/Mutex.h"
#include "llvm/Support/MemoryBuffer.h"

#include <string>
Expand All @@ -40,6 +42,8 @@ struct CachedArgInfo {
cl_kernel_arg_type_qualifier typeQualifier;
};

static llvm::ManagedStatic<llvm::sys::SmartMutex<true> > kernelArgInfoMutex;

class OCLFEKernelArgInfo : public IOCLFEKernelArgInfo {
public:
unsigned int getNumArgs() const { return m_argsInfo.size(); }
Expand Down Expand Up @@ -79,6 +83,10 @@ extern "C" CC_DLL_EXPORT int GetKernelArgInfo(const void *pBin,
// Lazy initialization
CommonClangInitialize();

// LLVM doesn't guarantee thread safety,
// therefore we serialize execution of LLVM code.
llvm::sys::SmartScopedLock<true> kernelArgInfoGuard {*kernelArgInfoMutex};

try {
std::unique_ptr<llvm::MemoryBuffer> pBinBuff(
llvm::MemoryBuffer::getMemBuffer(
Expand Down
13 changes: 13 additions & 0 deletions cclang/link.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,16 @@ Copyright (c) Intel Corporation (2009-2017).
#include "llvm/IR/Module.h"
#include "llvm/IR/Constants.h"
#include "llvm/Linker/Linker.h"
#include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/Mutex.h"
#include "llvm/Support/raw_ostream.h"

using namespace Intel::OpenCL::ClangFE;
using namespace llvm;

static llvm::ManagedStatic<llvm::sys::SmartMutex<true> > linkMutex;
static llvm::ManagedStatic<llvm::sys::SmartMutex<true> > linkOptionsMutex;

void CommonClangInitialize();

///
Expand Down Expand Up @@ -291,6 +296,10 @@ OCLFEBinaryResult *LinkInternal(const void **pInputBinaries,
return pResult.release();
}

// LLVM doesn't guarantee thread safety,
// therefore we serialize execution of LLVM code.
llvm::sys::SmartScopedLock<true> linkGuard {*linkMutex};

// Parse options
ClangLinkOptions optionsParser;
optionsParser.processOptions(pszOptions);
Expand Down Expand Up @@ -387,6 +396,10 @@ extern "C" CC_DLL_EXPORT int Link(const void **pInputBinaries,
extern "C" CC_DLL_EXPORT bool CheckLinkOptions(const char *pszOptions,
char *pszUnknownOptions,
size_t uiUnknownOptionsSize) {
// LLVM doesn't guarantee thread safety,
// therefore we serialize execution of LLVM code.
llvm::sys::SmartScopedLock<true> linkOptionsGuard {*linkOptionsMutex};

try {
ClangLinkOptions optionsParser;
return optionsParser.checkOptions(pszOptions, pszUnknownOptions,
Expand Down
10 changes: 9 additions & 1 deletion cclang/options_compile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ Copyright (c) Intel Corporation (2009-2017).
#include "common_clang.h"
#include "options.h"

#include "clang/Driver/Options.h"
#include "llvm/Option/Arg.h"
#include "llvm/Option/ArgList.h"
#include "clang/Driver/Options.h"
#include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/Mutex.h"

#include <sstream>

Expand All @@ -34,6 +36,8 @@ Copyright (c) Intel Corporation (2009-2017).

using namespace llvm::opt;

static llvm::ManagedStatic<llvm::sys::SmartMutex<true> > compileOptionsMutex;

static const OptTable::Info ClangOptionsInfoTable[] = {
#define PREFIX(NAME, VALUE)
#define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
Expand Down Expand Up @@ -370,6 +374,10 @@ std::string CompileOptionsParser::getEffectiveOptionsAsString() const {
extern "C" CC_DLL_EXPORT bool CheckCompileOptions(const char *pszOptions,
char *pszUnknownOptions,
size_t uiUnknownOptionsSize) {
// LLVM doesn't guarantee thread safety,
// therefore we serialize execution of LLVM code.
llvm::sys::SmartScopedLock<true> compileOptionsGuard {*compileOptionsMutex};

try {
CompileOptionsParser optionsParser("200");
return optionsParser.checkOptions(pszOptions, pszUnknownOptions,
Expand Down

0 comments on commit 9d17e0b

Please sign in to comment.