@@ -201,97 +201,94 @@ Compile(const char *pszProgramSource, const char **pInputHeaders,
201201 try {
202202 std::unique_ptr<OCLFEBinaryResult> pResult (new OCLFEBinaryResult ());
203203
204- // LLVM doesn't guarantee thread safety,
205- // therefore we serialize execution of LLVM code.
206- llvm::sys::SmartScopedLock< true > compileGuard {*compileMutex} ;
204+ // Create the clang compiler
205+ std::unique_ptr<clang::CompilerInstance> compiler (
206+ new clang::CompilerInstance ()) ;
207207
208- // Parse options
209208 CompileOptionsParser optionsParser (pszOpenCLVer);
210- optionsParser.processOptions (pszOptions, pszOptionsEx);
211209
212210 // Prepare error log
213211 llvm::raw_string_ostream err_ostream (pResult->getLogRef ());
214-
215- // Prepare our diagnostic client.
216- llvm::IntrusiveRefCntPtr<clang::DiagnosticIDs> DiagID (
217- new clang::DiagnosticIDs ());
218- llvm::IntrusiveRefCntPtr<clang::DiagnosticOptions> DiagOpts (
219- new clang::DiagnosticOptions ());
220- DiagOpts->ShowPresumedLoc = true ;
221- clang::TextDiagnosticPrinter *DiagsPrinter =
212+ {
213+ llvm::sys::SmartScopedLock<true > compileGuard {*compileMutex};
214+
215+ // Parse options
216+ optionsParser.processOptions (pszOptions, pszOptionsEx);
217+
218+ // Prepare our diagnostic client.
219+ llvm::IntrusiveRefCntPtr<clang::DiagnosticIDs> DiagID (
220+ new clang::DiagnosticIDs ());
221+ llvm::IntrusiveRefCntPtr<clang::DiagnosticOptions> DiagOpts (
222+ new clang::DiagnosticOptions ());
223+ DiagOpts->ShowPresumedLoc = true ;
224+ clang::TextDiagnosticPrinter *DiagsPrinter =
222225 new clang::TextDiagnosticPrinter (err_ostream, &*DiagOpts);
223- llvm::IntrusiveRefCntPtr<clang::DiagnosticsEngine> Diags (
224- new clang::DiagnosticsEngine (DiagID, &*DiagOpts, DiagsPrinter));
225-
226- // Create the clang compiler
227- std::unique_ptr<clang::CompilerInstance> compiler (
228- new clang::CompilerInstance ());
226+ llvm::IntrusiveRefCntPtr<clang::DiagnosticsEngine> Diags (
227+ new clang::DiagnosticsEngine (DiagID, &*DiagOpts, DiagsPrinter));
229228
230- // Prepare output buffer
231- std::unique_ptr<llvm::raw_pwrite_stream>
229+ // Prepare output buffer
230+ std::unique_ptr<llvm::raw_pwrite_stream>
232231 ir_ostream (new llvm::raw_svector_ostream (pResult->getIRBufferRef ()));
233- // Set buffers
234- // CompilerInstance takes ownership over output stream
235- compiler->setOutputStream (std::move (ir_ostream));
232+ // Set buffers
233+ // CompilerInstance takes ownership over output stream
234+ compiler->setOutputStream (std::move (ir_ostream));
236235
237- compiler->setDiagnostics (&*Diags);
236+ compiler->setDiagnostics (&*Diags);
238237
239- llvm::IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> OverlayFS (
240- new llvm::vfs::OverlayFileSystem (llvm::vfs::getRealFileSystem ()));
241- llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> MemFS (
242- new llvm::vfs::InMemoryFileSystem);
243- OverlayFS->pushOverlay (MemFS);
238+ llvm::IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> OverlayFS (
239+ new llvm::vfs::OverlayFileSystem (llvm::vfs::getRealFileSystem ()));
240+ llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> MemFS (
241+ new llvm::vfs::InMemoryFileSystem);
242+ OverlayFS->pushOverlay (MemFS);
244243
245- compiler->createFileManager (OverlayFS);
246- compiler->createSourceManager (compiler->getFileManager ());
244+ compiler->createFileManager (OverlayFS);
245+ compiler->createSourceManager (compiler->getFileManager ());
247246
248- // Calling ResetAllOptionOccurrences as WA for issue from here:
249- // https://reviews.llvm.org/D66324?id=219733#1680231
250- llvm::cl::ResetAllOptionOccurrences ();
251- // Create compiler invocation from user args before trickering with it
252- clang::CompilerInvocation::CreateFromArgs (compiler->getInvocation (),
253- optionsParser.args (), *Diags);
247+ // Calling ResetAllOptionOccurrences as WA for issue from here:
248+ // https://reviews.llvm.org/D66324?id=219733#1680231
249+ llvm::cl::ResetAllOptionOccurrences ();
250+ // Create compiler invocation from user args before trickering with it
251+ clang::CompilerInvocation::CreateFromArgs (compiler->getInvocation (),
252+ optionsParser.args (), *Diags);
254253
255- // Configure our handling of diagnostics.
256- ProcessWarningOptions (*Diags, compiler->getDiagnosticOpts ());
254+ // Configure our handling of diagnostics.
255+ ProcessWarningOptions (*Diags, compiler->getDiagnosticOpts ());
257256
258- // Map memory buffers to a virtual file system
257+ // Map memory buffers to a virtual file system
259258
260- // Source file
261- MemFS->addFile (
262- optionsParser.getSourceName (), (time_t )0 ,
263- llvm::MemoryBuffer::getMemBuffer (
259+ // Source file
260+ MemFS->addFile (
261+ optionsParser.getSourceName (), (time_t )0 ,
262+ llvm::MemoryBuffer::getMemBuffer (
264263 llvm::StringRef (pszProgramSource), optionsParser.getSourceName ()));
265264
266- // Input header with OpenCL defines.
267- std::vector<Resource> vHeaderWithDefs;
268- if (!GetHeaders (vHeaderWithDefs)) {
269- return CL_COMPILE_PROGRAM_FAILURE;
270- }
265+ // Input header with OpenCL defines.
266+ std::vector<Resource> vHeaderWithDefs;
267+ if (!GetHeaders (vHeaderWithDefs)) {
268+ return CL_COMPILE_PROGRAM_FAILURE;
269+ }
271270
272- for (const auto &Header:vHeaderWithDefs) {
273- auto Buf = llvm::MemoryBuffer::getMemBuffer (
274- llvm::StringRef (Header.m_data , Header.m_size ),
275- Header.m_name );
271+ for (const auto &Header:vHeaderWithDefs) {
272+ auto Buf = llvm::MemoryBuffer::getMemBuffer (
273+ llvm::StringRef (Header.m_data , Header.m_size ),
274+ Header.m_name );
276275
277- MemFS->addFile (Header.m_name ,(time_t )0 , std::move (Buf));
278- }
276+ MemFS->addFile (Header.m_name ,(time_t )0 , std::move (Buf));
277+ }
279278
280- // Input Headers
281- for (unsigned int i = 0 ; i < uiNumInputHeaders; ++i) {
282- auto Header = llvm::MemoryBuffer::getMemBuffer (
283- pInputHeaders[i], pInputHeadersNames[i]);
284- MemFS->addFile (pInputHeadersNames[i], (time_t )0 , std::move (Header));
279+ // Input Headers
280+ for (unsigned int i = 0 ; i < uiNumInputHeaders; ++i) {
281+ auto Header = llvm::MemoryBuffer::getMemBuffer (
282+ pInputHeaders[i], pInputHeadersNames[i]);
283+ MemFS->addFile (pInputHeadersNames[i], (time_t )0 , std::move (Header));
284+ }
285285 }
286-
287-
288286 // Execute the frontend actions.
289287 bool success = false ;
290288 try {
291289 success = clang::ExecuteCompilerInvocation (compiler.get ());
292290 } catch (const std::exception &) {
293291 }
294-
295292 pResult->setIRType (IR_TYPE_COMPILED_OBJECT);
296293 pResult->setIRName (optionsParser.getSourceName ());
297294
@@ -306,11 +303,11 @@ Compile(const char *pszProgramSource, const char **pInputHeaders,
306303 if (success && optionsParser.hasEmitSPIRV ()) {
307304 // Translate LLVM IR to SPIR-V.
308305 llvm::StringRef LLVM_IR (static_cast <const char *>(pResult->GetIR ()),
309- pResult->GetIRSize ());
306+ pResult->GetIRSize ());
310307 std::unique_ptr<llvm::MemoryBuffer> MB = llvm::MemoryBuffer::getMemBuffer (LLVM_IR, pResult->GetIRName (), false );
311308 llvm::LLVMContext Context;
312309 auto E = llvm::getOwningLazyBitcodeModule (std::move (MB), Context,
313- /* ShouldLazyLoadMetadata=*/ true );
310+ /* ShouldLazyLoadMetadata=*/ true );
314311 llvm::logAllUnhandledErrors (E.takeError (), err_ostream, " error: " );
315312 std::unique_ptr<llvm::Module> M = std::move (*E);
316313
@@ -334,9 +331,11 @@ Compile(const char *pszProgramSource, const char **pInputHeaders,
334331 err_ostream << Err.c_str ();
335332 err_ostream.flush ();
336333 }
337-
338- if (pBinaryResult) {
339- *pBinaryResult = pResult.release ();
334+ {
335+ llvm::sys::SmartScopedLock<true > compileGuard {*compileMutex};
336+ if (pBinaryResult) {
337+ *pBinaryResult = pResult.release ();
338+ }
340339 }
341340 return success ? CL_SUCCESS : CL_COMPILE_PROGRAM_FAILURE;
342341 } catch (std::bad_alloc &) {
0 commit comments