@@ -208,95 +208,92 @@ Compile(const char *pszProgramSource, const char **pInputHeaders,
208208 try {
209209 std::unique_ptr<OCLFEBinaryResult> pResult (new OCLFEBinaryResult ());
210210
211- // LLVM doesn't guarantee thread safety,
212- // therefore we serialize execution of LLVM code.
213- llvm::sys::SmartScopedLock< true > compileGuard {*compileMutex} ;
211+ // Create the clang compiler
212+ std::unique_ptr<clang::CompilerInstance> compiler (
213+ new clang::CompilerInstance ()) ;
214214
215- // Parse options
216215 CompileOptionsParser optionsParser (pszOpenCLVer);
217- optionsParser.processOptions (pszOptions, pszOptionsEx);
218-
219216 // Prepare error log
220217 llvm::raw_string_ostream err_ostream (pResult->getLogRef ());
221-
222- // Prepare our diagnostic client.
223- llvm::IntrusiveRefCntPtr<clang::DiagnosticIDs> DiagID (
224- new clang::DiagnosticIDs ());
225- llvm::IntrusiveRefCntPtr<clang::DiagnosticOptions> DiagOpts (
226- new clang::DiagnosticOptions ());
227- DiagOpts->ShowPresumedLoc = true ;
228- clang::TextDiagnosticPrinter *DiagsPrinter =
218+ {
219+ llvm::sys::SmartScopedLock<true > compileGuard {*compileMutex};
220+
221+ // Parse options
222+ optionsParser.processOptions (pszOptions, pszOptionsEx);
223+
224+ // Prepare our diagnostic client.
225+ llvm::IntrusiveRefCntPtr<clang::DiagnosticIDs> DiagID (
226+ new clang::DiagnosticIDs ());
227+ llvm::IntrusiveRefCntPtr<clang::DiagnosticOptions> DiagOpts (
228+ new clang::DiagnosticOptions ());
229+ DiagOpts->ShowPresumedLoc = true ;
230+ clang::TextDiagnosticPrinter *DiagsPrinter =
229231 new clang::TextDiagnosticPrinter (err_ostream, &*DiagOpts);
230- llvm::IntrusiveRefCntPtr<clang::DiagnosticsEngine> Diags (
231- new clang::DiagnosticsEngine (DiagID, &*DiagOpts, DiagsPrinter));
232+ llvm::IntrusiveRefCntPtr<clang::DiagnosticsEngine> Diags (
233+ new clang::DiagnosticsEngine (DiagID, &*DiagOpts, DiagsPrinter));
232234
233- // Create the clang compiler
234- std::unique_ptr<clang::CompilerInstance> compiler (
235- new clang::CompilerInstance ());
236-
237- // Prepare output buffer
238- std::unique_ptr<llvm::raw_pwrite_stream>
235+ // Prepare output buffer
236+ std::unique_ptr<llvm::raw_pwrite_stream>
239237 ir_ostream (new llvm::raw_svector_ostream (pResult->getIRBufferRef ()));
240- // Set buffers
241- // CompilerInstance takes ownership over output stream
242- compiler->setOutputStream (std::move (ir_ostream));
238+ // Set buffers
239+ // CompilerInstance takes ownership over output stream
240+ compiler->setOutputStream (std::move (ir_ostream));
243241
244- compiler->setDiagnostics (&*Diags);
242+ compiler->setDiagnostics (&*Diags);
245243
246- llvm::IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> OverlayFS (
247- new llvm::vfs::OverlayFileSystem (llvm::vfs::getRealFileSystem ()));
248- llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> MemFS (
249- new llvm::vfs::InMemoryFileSystem);
250- OverlayFS->pushOverlay (MemFS);
244+ llvm::IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> OverlayFS (
245+ new llvm::vfs::OverlayFileSystem (llvm::vfs::getRealFileSystem ()));
246+ llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> MemFS (
247+ new llvm::vfs::InMemoryFileSystem);
248+ OverlayFS->pushOverlay (MemFS);
251249
252- compiler->createFileManager (OverlayFS);
253- compiler->createSourceManager (compiler->getFileManager ());
250+ compiler->createFileManager (OverlayFS);
251+ compiler->createSourceManager (compiler->getFileManager ());
254252
255- // Create compiler invocation from user args before trickering with it
256- clang::CompilerInvocation::CreateFromArgs (compiler->getInvocation (),
257- optionsParser.beginArgs (),
258- optionsParser.endArgs (), *Diags);
253+ // Create compiler invocation from user args before trickering with it
254+ clang::CompilerInvocation::CreateFromArgs (compiler->getInvocation (),
255+ optionsParser.beginArgs (),
256+ optionsParser.endArgs (), *Diags);
259257
260- // Configure our handling of diagnostics.
261- ProcessWarningOptions (*Diags, compiler->getDiagnosticOpts ());
258+ // Configure our handling of diagnostics.
259+ ProcessWarningOptions (*Diags, compiler->getDiagnosticOpts ());
262260
263- // Map memory buffers to a virtual file system
261+ // Map memory buffers to a virtual file system
264262
265- // Source file
266- MemFS->addFile (
267- optionsParser.getSourceName (), (time_t )0 ,
268- llvm::MemoryBuffer::getMemBuffer (
263+ // Source file
264+ MemFS->addFile (
265+ optionsParser.getSourceName (), (time_t )0 ,
266+ llvm::MemoryBuffer::getMemBuffer (
269267 llvm::StringRef (pszProgramSource), optionsParser.getSourceName ()));
270268
271- // Input header with OpenCL defines.
272- std::vector<Resource> vHeaderWithDefs;
273- if (!GetHeaders (vHeaderWithDefs)) {
274- return CL_COMPILE_PROGRAM_FAILURE;
275- }
269+ // Input header with OpenCL defines.
270+ std::vector<Resource> vHeaderWithDefs;
271+ if (!GetHeaders (vHeaderWithDefs)) {
272+ return CL_COMPILE_PROGRAM_FAILURE;
273+ }
276274
277- for (const auto &Header:vHeaderWithDefs) {
278- auto Buf = llvm::MemoryBuffer::getMemBuffer (
279- llvm::StringRef (Header.m_data , Header.m_size ),
280- Header.m_name );
275+ for (const auto &Header:vHeaderWithDefs) {
276+ auto Buf = llvm::MemoryBuffer::getMemBuffer (
277+ llvm::StringRef (Header.m_data , Header.m_size ),
278+ Header.m_name );
281279
282- MemFS->addFile (Header.m_name ,(time_t )0 , std::move (Buf));
283- }
280+ MemFS->addFile (Header.m_name ,(time_t )0 , std::move (Buf));
281+ }
284282
285- // Input Headers
286- for (unsigned int i = 0 ; i < uiNumInputHeaders; ++i) {
287- auto Header = llvm::MemoryBuffer::getMemBuffer (
288- pInputHeaders[i], pInputHeadersNames[i]);
289- MemFS->addFile (pInputHeadersNames[i], (time_t )0 , std::move (Header));
283+ // Input Headers
284+ for (unsigned int i = 0 ; i < uiNumInputHeaders; ++i) {
285+ auto Header = llvm::MemoryBuffer::getMemBuffer (
286+ pInputHeaders[i], pInputHeadersNames[i]);
287+ MemFS->addFile (pInputHeadersNames[i], (time_t )0 , std::move (Header));
288+ }
290289 }
291290
292-
293291 // Execute the frontend actions.
294292 bool success = false ;
295293 try {
296294 success = clang::ExecuteCompilerInvocation (compiler.get ());
297295 } catch (const std::exception &) {
298296 }
299-
300297 pResult->setIRType (IR_TYPE_COMPILED_OBJECT);
301298 pResult->setIRName (optionsParser.getSourceName ());
302299
@@ -313,11 +310,11 @@ Compile(const char *pszProgramSource, const char **pInputHeaders,
313310 if (optionsParser.hasEmitSPIRVText ())
314311 SPIRV::SPIRVUseTextFormat = true ;
315312 llvm::StringRef LLVM_IR (static_cast <const char *>(pResult->GetIR ()),
316- pResult->GetIRSize ());
313+ pResult->GetIRSize ());
317314 std::unique_ptr<llvm::MemoryBuffer> MB = llvm::MemoryBuffer::getMemBuffer (LLVM_IR, pResult->GetIRName (), false );
318315 llvm::LLVMContext Context;
319316 auto E = llvm::getOwningLazyBitcodeModule (std::move (MB), Context,
320- /* ShouldLazyLoadMetadata=*/ true );
317+ /* ShouldLazyLoadMetadata=*/ true );
321318 llvm::logAllUnhandledErrors (E.takeError (), err_ostream, " error: " );
322319 std::unique_ptr<llvm::Module> M = std::move (*E);
323320
@@ -341,9 +338,11 @@ Compile(const char *pszProgramSource, const char **pInputHeaders,
341338 err_ostream << Err.c_str ();
342339 err_ostream.flush ();
343340 }
344-
345- if (pBinaryResult) {
346- *pBinaryResult = pResult.release ();
341+ {
342+ llvm::sys::SmartScopedLock<true > compileGuard {*compileMutex};
343+ if (pBinaryResult) {
344+ *pBinaryResult = pResult.release ();
345+ }
347346 }
348347 return success ? CL_SUCCESS : CL_COMPILE_PROGRAM_FAILURE;
349348 } catch (std::bad_alloc &) {
0 commit comments