@@ -201,142 +201,142 @@ 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 }
292+ {
293+ llvm::sys::SmartScopedLock<true > compileGuard {*compileMutex};
294+ pResult->setIRType (IR_TYPE_COMPILED_OBJECT);
295+ pResult->setIRName (optionsParser.getSourceName ());
296+
297+ // Our error handler depends on the Diagnostics object, which we're
298+ // potentially about to delete. Uninstall the handler now so that any
299+ // later errors use the default handling behavior instead.
300+ // (currently commented out since setting the llvm error handling in
301+ // multi-threaded environment is unsupported)
302+ // llvm::remove_fatal_error_handler();
303+ err_ostream.flush ();
294304
295- pResult->setIRType (IR_TYPE_COMPILED_OBJECT);
296- pResult->setIRName (optionsParser.getSourceName ());
297-
298- // Our error handler depends on the Diagnostics object, which we're
299- // potentially about to delete. Uninstall the handler now so that any
300- // later errors use the default handling behavior instead.
301- // (currently commented out since setting the llvm error handling in
302- // multi-threaded environment is unsupported)
303- // llvm::remove_fatal_error_handler();
304- err_ostream.flush ();
305-
306- if (success && optionsParser.hasEmitSPIRV ()) {
307- // Translate LLVM IR to SPIR-V.
308- llvm::StringRef LLVM_IR (static_cast <const char *>(pResult->GetIR ()),
309- pResult->GetIRSize ());
310- std::unique_ptr<llvm::MemoryBuffer> MB = llvm::MemoryBuffer::getMemBuffer (LLVM_IR, pResult->GetIRName (), false );
311- llvm::LLVMContext Context;
312- auto E = llvm::getOwningLazyBitcodeModule (std::move (MB), Context,
313- /* ShouldLazyLoadMetadata=*/ true );
314- llvm::logAllUnhandledErrors (E.takeError (), err_ostream, " error: " );
315- std::unique_ptr<llvm::Module> M = std::move (*E);
316-
317- if (M->materializeAll ()) {
318- if (pBinaryResult) {
319- *pBinaryResult = nullptr ;
305+ if (success && optionsParser.hasEmitSPIRV ()) {
306+ // Translate LLVM IR to SPIR-V.
307+ llvm::StringRef LLVM_IR (static_cast <const char *>(pResult->GetIR ()),
308+ pResult->GetIRSize ());
309+ std::unique_ptr<llvm::MemoryBuffer> MB = llvm::MemoryBuffer::getMemBuffer (LLVM_IR, pResult->GetIRName (), false );
310+ llvm::LLVMContext Context;
311+ auto E = llvm::getOwningLazyBitcodeModule (std::move (MB), Context,
312+ /* ShouldLazyLoadMetadata=*/ true );
313+ llvm::logAllUnhandledErrors (E.takeError (), err_ostream, " error: " );
314+ std::unique_ptr<llvm::Module> M = std::move (*E);
315+
316+ if (M->materializeAll ()) {
317+ if (pBinaryResult) {
318+ *pBinaryResult = nullptr ;
319+ }
320+ assert (!" Failed to read just compiled LLVM IR!" );
321+ return CL_COMPILE_PROGRAM_FAILURE;
320322 }
321- assert (!" Failed to read just compiled LLVM IR!" );
322- return CL_COMPILE_PROGRAM_FAILURE;
323- }
324- pResult->getIRBufferRef ().clear ();
325- SmallVectorBuffer StreamBuf (pResult->getIRBufferRef ());
326- std::ostream OS (&StreamBuf);
327- std::string Err;
328- SPIRV::TranslatorOpts SPIRVOpts;
329- SPIRVOpts.enableAllExtensions ();
330- if (!optionsParser.hasOptDisable ()) {
331- SPIRVOpts.setMemToRegEnabled (true );
323+ pResult->getIRBufferRef ().clear ();
324+ SmallVectorBuffer StreamBuf (pResult->getIRBufferRef ());
325+ std::ostream OS (&StreamBuf);
326+ std::string Err;
327+ SPIRV::TranslatorOpts SPIRVOpts;
328+ SPIRVOpts.enableAllExtensions ();
329+ if (!optionsParser.hasOptDisable ()) {
330+ SPIRVOpts.setMemToRegEnabled (true );
331+ }
332+ success = llvm::writeSpirv (M.get (), SPIRVOpts, OS, Err);
333+ err_ostream << Err.c_str ();
334+ err_ostream.flush ();
332335 }
333- success = llvm::writeSpirv (M.get (), SPIRVOpts, OS, Err);
334- err_ostream << Err.c_str ();
335- err_ostream.flush ();
336- }
337336
338- if (pBinaryResult) {
339- *pBinaryResult = pResult.release ();
337+ if (pBinaryResult) {
338+ *pBinaryResult = pResult.release ();
339+ }
340340 }
341341 return success ? CL_SUCCESS : CL_COMPILE_PROGRAM_FAILURE;
342342 } catch (std::bad_alloc &) {
0 commit comments