Skip to content

Commit 19bd6de

Browse files
authored
[ocl-open-210] Remove compileMutex to enable parallel compilation (#692) (#703)
The issue observed in 76bc7f9 were fixed in upstream. (cherry picked from commit 18ec882) Fixes #680
1 parent e5d7d20 commit 19bd6de

2 files changed

Lines changed: 70 additions & 88 deletions

File tree

opencl_clang.cpp

Lines changed: 70 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ Copyright (c) Intel Corporation (2009-2017).
3737
#include "llvm/Support/Path.h"
3838
#include "llvm/Support/Threading.h"
3939
#include "llvm/Support/ManagedStatic.h"
40-
#include "llvm/Support/Mutex.h"
4140
#include "clang/Basic/LangOptions.h"
4241
#include "clang/Basic/Diagnostic.h"
4342
#include "clang/Basic/DiagnosticIDs.h"
@@ -74,8 +73,6 @@ Copyright (c) Intel Corporation (2009-2017).
7473

7574
using namespace Intel::OpenCL::ClangFE;
7675

77-
llvm::ManagedStatic<llvm::sys::SmartMutex<true>> compileMutex;
78-
7976
void OpenCLClangTerminate() { llvm::llvm_shutdown(); }
8077

8178
// This function mustn't be invoked from a static object constructor,
@@ -191,9 +188,6 @@ Compile(const char *pszProgramSource, const char **pInputHeaders,
191188
OpenCLClangInitialize();
192189

193190
try {
194-
#ifdef _WIN32
195-
llvm::sys::SmartScopedLock<true> compileGuard{*compileMutex};
196-
#endif
197191
std::unique_ptr<OCLFEBinaryResult> pResult(new OCLFEBinaryResult());
198192

199193
// Create the clang compiler
@@ -204,80 +198,77 @@ Compile(const char *pszProgramSource, const char **pInputHeaders,
204198

205199
// Prepare error log
206200
llvm::raw_string_ostream err_ostream(pResult->getLogRef());
207-
{
208-
#ifndef _WIN32
209-
llvm::sys::SmartScopedLock<true> compileGuard{*compileMutex};
210-
#endif
211-
// Parse options
212-
if (optionsParser.processOptions(pszOptions, pszOptionsEx) != 0) {
213-
if (pBinaryResult)
214-
*pBinaryResult = nullptr;
215-
return CL_INVALID_BUILD_OPTIONS;
216-
}
217201

218-
// Prepare our diagnostic client.
219-
llvm::IntrusiveRefCntPtr<clang::DiagnosticIDs> DiagID(
220-
new clang::DiagnosticIDs());
221-
clang::DiagnosticOptions DiagOpts;
222-
DiagOpts.ShowPresumedLoc = true;
223-
clang::TextDiagnosticPrinter *DiagsPrinter =
224-
new clang::TextDiagnosticPrinter(err_ostream, DiagOpts);
225-
llvm::IntrusiveRefCntPtr<clang::DiagnosticsEngine> Diags(
226-
new clang::DiagnosticsEngine(DiagID, DiagOpts, DiagsPrinter));
227-
228-
// Prepare output buffer
229-
std::unique_ptr<llvm::raw_pwrite_stream>
230-
ir_ostream(new llvm::raw_svector_ostream(pResult->getIRBufferRef()));
231-
// Set buffers
232-
// CompilerInstance takes ownership over output stream
233-
compiler->setOutputStream(std::move(ir_ostream));
234-
235-
compiler->setDiagnostics(&*Diags);
236-
237-
llvm::IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> OverlayFS(
238-
new llvm::vfs::OverlayFileSystem(llvm::vfs::getRealFileSystem()));
239-
llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> MemFS(
240-
new llvm::vfs::InMemoryFileSystem);
241-
OverlayFS->pushOverlay(MemFS);
242-
243-
compiler->createFileManager(std::move(OverlayFS));
244-
compiler->createSourceManager(compiler->getFileManager());
245-
246-
// Create compiler invocation from user args before trickering with it
247-
clang::CompilerInvocation::CreateFromArgs(compiler->getInvocation(),
248-
optionsParser.args(), *Diags);
249-
250-
// Configure our handling of diagnostics.
251-
ProcessWarningOptions(*Diags, compiler->getDiagnosticOpts(),
252-
compiler->getFileManager().getVirtualFileSystem());
253-
254-
// Map memory buffers to a virtual file system
255-
MemFS->addFile(
256-
optionsParser.getSourceName(), (time_t)0,
257-
llvm::MemoryBuffer::getMemBuffer(
258-
llvm::StringRef(pszProgramSource), optionsParser.getSourceName()));
259-
260-
// Input header with OpenCL defines.
261-
std::vector<Resource> vHeaderWithDefs;
262-
if (!GetHeaders(vHeaderWithDefs)) {
263-
return CL_COMPILE_PROGRAM_FAILURE;
264-
}
202+
// Parse options
203+
if (optionsParser.processOptions(pszOptions, pszOptionsEx) != 0) {
204+
if (pBinaryResult)
205+
*pBinaryResult = nullptr;
206+
return CL_INVALID_BUILD_OPTIONS;
207+
}
208+
209+
// Prepare our diagnostic client.
210+
llvm::IntrusiveRefCntPtr<clang::DiagnosticIDs> DiagID(
211+
new clang::DiagnosticIDs());
212+
clang::DiagnosticOptions DiagOpts;
213+
DiagOpts.ShowPresumedLoc = true;
214+
clang::TextDiagnosticPrinter *DiagsPrinter =
215+
new clang::TextDiagnosticPrinter(err_ostream, DiagOpts);
216+
llvm::IntrusiveRefCntPtr<clang::DiagnosticsEngine> Diags(
217+
new clang::DiagnosticsEngine(DiagID, DiagOpts, DiagsPrinter));
218+
219+
// Prepare output buffer
220+
std::unique_ptr<llvm::raw_pwrite_stream>
221+
ir_ostream(new llvm::raw_svector_ostream(pResult->getIRBufferRef()));
222+
// Set buffers
223+
// CompilerInstance takes ownership over output stream
224+
compiler->setOutputStream(std::move(ir_ostream));
225+
226+
compiler->setDiagnostics(&*Diags);
227+
228+
llvm::IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> OverlayFS(
229+
new llvm::vfs::OverlayFileSystem(llvm::vfs::getRealFileSystem()));
230+
llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> MemFS(
231+
new llvm::vfs::InMemoryFileSystem);
232+
OverlayFS->pushOverlay(MemFS);
233+
234+
compiler->createFileManager(std::move(OverlayFS));
235+
compiler->createSourceManager(compiler->getFileManager());
236+
237+
// Create compiler invocation from user args before trickering with it
238+
clang::CompilerInvocation::CreateFromArgs(compiler->getInvocation(),
239+
optionsParser.args(), *Diags);
240+
241+
// Configure our handling of diagnostics.
242+
ProcessWarningOptions(*Diags, compiler->getDiagnosticOpts(),
243+
compiler->getFileManager().getVirtualFileSystem());
244+
245+
// Map memory buffers to a virtual file system
246+
MemFS->addFile(
247+
optionsParser.getSourceName(), (time_t)0,
248+
llvm::MemoryBuffer::getMemBuffer(
249+
llvm::StringRef(pszProgramSource), optionsParser.getSourceName()));
250+
251+
// Input header with OpenCL defines.
252+
std::vector<Resource> vHeaderWithDefs;
253+
if (!GetHeaders(vHeaderWithDefs)) {
254+
return CL_COMPILE_PROGRAM_FAILURE;
255+
}
265256

266-
for (const auto &Header:vHeaderWithDefs) {
267-
auto Buf = llvm::MemoryBuffer::getMemBuffer(
268-
llvm::StringRef(Header.m_data, Header.m_size),
269-
Header.m_name);
257+
for (const auto &Header:vHeaderWithDefs) {
258+
auto Buf = llvm::MemoryBuffer::getMemBuffer(
259+
llvm::StringRef(Header.m_data, Header.m_size),
260+
Header.m_name);
270261

271-
MemFS->addFile(Header.m_name,(time_t)0, std::move(Buf));
272-
}
262+
MemFS->addFile(Header.m_name,(time_t)0, std::move(Buf));
263+
}
273264

274-
// Input Headers
275-
for (unsigned int i = 0; i < uiNumInputHeaders; ++i) {
276-
auto Header = llvm::MemoryBuffer::getMemBuffer(
277-
pInputHeaders[i], pInputHeadersNames[i]);
278-
MemFS->addFile(pInputHeadersNames[i], (time_t)0, std::move(Header));
279-
}
265+
// Input Headers
266+
for (unsigned int i = 0; i < uiNumInputHeaders; ++i) {
267+
auto Header = llvm::MemoryBuffer::getMemBuffer(
268+
pInputHeaders[i], pInputHeadersNames[i]);
269+
MemFS->addFile(pInputHeadersNames[i], (time_t)0, std::move(Header));
280270
}
271+
281272
// Execute the frontend actions.
282273
bool success = false;
283274
try {
@@ -329,14 +320,11 @@ Compile(const char *pszProgramSource, const char **pInputHeaders,
329320
err_ostream << Err.c_str();
330321
err_ostream.flush();
331322
}
332-
{
333-
#ifndef _WIN32
334-
llvm::sys::SmartScopedLock<true> compileGuard{*compileMutex};
335-
#endif
336-
if (pBinaryResult) {
337-
*pBinaryResult = pResult.release();
338-
}
323+
324+
if (pBinaryResult) {
325+
*pBinaryResult = pResult.release();
339326
}
327+
340328
return success ? CL_SUCCESS : CL_COMPILE_PROGRAM_FAILURE;
341329
} catch (std::bad_alloc &) {
342330
if (pBinaryResult) {

options_compile.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ Copyright (c) Intel Corporation (2009-2017).
3333

3434
using namespace llvm::opt;
3535

36-
extern llvm::ManagedStatic<llvm::sys::SmartMutex<true>> compileMutex;
37-
3836
static constexpr OptTable::Info ClangOptionsInfoTable[] = {
3937
#define PREFIX(NAME, VALUE)
4038
#define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, \
@@ -552,10 +550,6 @@ std::string CompileOptionsParser::getEffectiveOptionsAsString() const {
552550
extern "C" CC_DLL_EXPORT bool CheckCompileOptions(const char *pszOptions,
553551
char *pszUnknownOptions,
554552
size_t uiUnknownOptionsSize) {
555-
// LLVM doesn't guarantee thread safety,
556-
// therefore we serialize execution of LLVM code.
557-
llvm::sys::SmartScopedLock<true> compileOptionsGuard{*compileMutex};
558-
559553
try {
560554
CompileOptionsParser optionsParser("200");
561555
return optionsParser.checkOptions(pszOptions, pszUnknownOptions,

0 commit comments

Comments
 (0)