Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
156 changes: 72 additions & 84 deletions common_clang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ Copyright (c) Intel Corporation (2009-2017).
#include "llvm/Support/Path.h"
#include "llvm/Support/Threading.h"
#include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/Mutex.h"
#include "llvm/Support/VirtualFileSystem.h"
#include "clang/Basic/LangOptions.h"
#include "clang/Basic/Diagnostic.h"
Expand Down Expand Up @@ -81,8 +80,6 @@ Copyright (c) Intel Corporation (2009-2017).

using namespace Intel::OpenCL::ClangFE;

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 @@ -198,9 +195,6 @@ Compile(const char *pszProgramSource, const char **pInputHeaders,
CommonClangInitialize();

try {
#ifdef _WIN32
llvm::sys::SmartScopedLock<true> compileGuard{*compileMutex};
#endif
std::unique_ptr<OCLFEBinaryResult> pResult(new OCLFEBinaryResult());

// Create the clang compiler
Expand All @@ -211,82 +205,79 @@ Compile(const char *pszProgramSource, const char **pInputHeaders,

// Prepare error log
llvm::raw_string_ostream err_ostream(pResult->getLogRef());
{
#ifndef _WIN32
llvm::sys::SmartScopedLock<true> compileGuard{*compileMutex};
#endif
// Parse options
if (optionsParser.processOptions(pszOptions, pszOptionsEx) != 0) {
if (pBinaryResult)
*pBinaryResult = nullptr;
return CL_INVALID_BUILD_OPTIONS;
}

// Prepare our diagnostic client.
llvm::IntrusiveRefCntPtr<clang::DiagnosticIDs> DiagID(
new clang::DiagnosticIDs());
llvm::IntrusiveRefCntPtr<clang::DiagnosticOptions> DiagOpts(
new clang::DiagnosticOptions());
DiagOpts->ShowPresumedLoc = true;
clang::TextDiagnosticPrinter *DiagsPrinter =
new clang::TextDiagnosticPrinter(err_ostream, &*DiagOpts);
llvm::IntrusiveRefCntPtr<clang::DiagnosticsEngine> Diags(
new clang::DiagnosticsEngine(DiagID, &*DiagOpts, DiagsPrinter));

// Prepare output buffer
std::unique_ptr<llvm::raw_pwrite_stream>
ir_ostream(new llvm::raw_svector_ostream(pResult->getIRBufferRef()));
// Set buffers
// CompilerInstance takes ownership over output stream
compiler->setOutputStream(std::move(ir_ostream));

compiler->setDiagnostics(&*Diags);

llvm::IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> OverlayFS(
new llvm::vfs::OverlayFileSystem(llvm::vfs::getRealFileSystem()));
llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> MemFS(
new llvm::vfs::InMemoryFileSystem);
OverlayFS->pushOverlay(MemFS);

compiler->createFileManager(OverlayFS);
compiler->createSourceManager(compiler->getFileManager());

// Create compiler invocation from user args before trickering with it
clang::CompilerInvocation::CreateFromArgs(compiler->getInvocation(),
optionsParser.args(), *Diags);

// Configure our handling of diagnostics.
ProcessWarningOptions(*Diags, compiler->getDiagnosticOpts());

// Map memory buffers to a virtual file system

// Source file
MemFS->addFile(
optionsParser.getSourceName(), (time_t)0,
llvm::MemoryBuffer::getMemBuffer(
llvm::StringRef(pszProgramSource), optionsParser.getSourceName()));

// Input header with OpenCL defines.
std::vector<Resource> vHeaderWithDefs;
if (!GetHeaders(vHeaderWithDefs)) {
return CL_COMPILE_PROGRAM_FAILURE;
}
// Parse options
if (optionsParser.processOptions(pszOptions, pszOptionsEx) != 0) {
if (pBinaryResult)
*pBinaryResult = nullptr;
return CL_INVALID_BUILD_OPTIONS;
}

// Prepare our diagnostic client.
llvm::IntrusiveRefCntPtr<clang::DiagnosticIDs> DiagID(
new clang::DiagnosticIDs());
llvm::IntrusiveRefCntPtr<clang::DiagnosticOptions> DiagOpts(
new clang::DiagnosticOptions());
DiagOpts->ShowPresumedLoc = true;
clang::TextDiagnosticPrinter *DiagsPrinter =
new clang::TextDiagnosticPrinter(err_ostream, &*DiagOpts);
llvm::IntrusiveRefCntPtr<clang::DiagnosticsEngine> Diags(
new clang::DiagnosticsEngine(DiagID, &*DiagOpts, DiagsPrinter));

// Prepare output buffer
std::unique_ptr<llvm::raw_pwrite_stream>
ir_ostream(new llvm::raw_svector_ostream(pResult->getIRBufferRef()));
// Set buffers
// CompilerInstance takes ownership over output stream
compiler->setOutputStream(std::move(ir_ostream));

compiler->setDiagnostics(&*Diags);

llvm::IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> OverlayFS(
new llvm::vfs::OverlayFileSystem(llvm::vfs::getRealFileSystem()));
llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> MemFS(
new llvm::vfs::InMemoryFileSystem);
OverlayFS->pushOverlay(MemFS);

compiler->createFileManager(OverlayFS);
compiler->createSourceManager(compiler->getFileManager());

// Create compiler invocation from user args before trickering with it
clang::CompilerInvocation::CreateFromArgs(compiler->getInvocation(),
optionsParser.args(), *Diags);

// Configure our handling of diagnostics.
ProcessWarningOptions(*Diags, compiler->getDiagnosticOpts());

// Map memory buffers to a virtual file system

// Source file
MemFS->addFile(
optionsParser.getSourceName(), (time_t)0,
llvm::MemoryBuffer::getMemBuffer(
llvm::StringRef(pszProgramSource), optionsParser.getSourceName()));

// Input header with OpenCL defines.
std::vector<Resource> vHeaderWithDefs;
if (!GetHeaders(vHeaderWithDefs)) {
return CL_COMPILE_PROGRAM_FAILURE;
}

for (const auto &Header:vHeaderWithDefs) {
auto Buf = llvm::MemoryBuffer::getMemBuffer(
llvm::StringRef(Header.m_data, Header.m_size),
Header.m_name);
for (const auto &Header:vHeaderWithDefs) {
auto Buf = llvm::MemoryBuffer::getMemBuffer(
llvm::StringRef(Header.m_data, Header.m_size),
Header.m_name);

MemFS->addFile(Header.m_name,(time_t)0, std::move(Buf));
}
MemFS->addFile(Header.m_name,(time_t)0, std::move(Buf));
}

// Input Headers
for (unsigned int i = 0; i < uiNumInputHeaders; ++i) {
auto Header = llvm::MemoryBuffer::getMemBuffer(
pInputHeaders[i], pInputHeadersNames[i]);
MemFS->addFile(pInputHeadersNames[i], (time_t)0, std::move(Header));
}
// Input Headers
for (unsigned int i = 0; i < uiNumInputHeaders; ++i) {
auto Header = llvm::MemoryBuffer::getMemBuffer(
pInputHeaders[i], pInputHeadersNames[i]);
MemFS->addFile(pInputHeadersNames[i], (time_t)0, std::move(Header));
}

// Execute the frontend actions.
bool success = false;
try {
Expand Down Expand Up @@ -338,14 +329,11 @@ Compile(const char *pszProgramSource, const char **pInputHeaders,
err_ostream << Err.c_str();
err_ostream.flush();
}
{
#ifndef _WIN32
llvm::sys::SmartScopedLock<true> compileGuard{*compileMutex};
#endif
if (pBinaryResult) {
*pBinaryResult = pResult.release();
}

if (pBinaryResult) {
*pBinaryResult = pResult.release();
}

return success ? CL_SUCCESS : CL_COMPILE_PROGRAM_FAILURE;
} catch (std::bad_alloc &) {
if (pBinaryResult) {
Expand Down
6 changes: 0 additions & 6 deletions options_compile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ Copyright (c) Intel Corporation (2009-2017).

using namespace llvm::opt;

extern llvm::ManagedStatic<llvm::sys::SmartMutex<true>> compileMutex;

static const OptTable::Info ClangOptionsInfoTable[] = {
#define PREFIX(NAME, VALUE)
#define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
Expand Down Expand Up @@ -522,10 +520,6 @@ 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{*compileMutex};

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