@@ -201,97 +201,94 @@ Compile(const char *pszProgramSource, const char **pInputHeaders,
201
201
try {
202
202
std::unique_ptr<OCLFEBinaryResult> pResult (new OCLFEBinaryResult ());
203
203
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 ()) ;
207
207
208
- // Parse options
209
208
CompileOptionsParser optionsParser (pszOpenCLVer);
210
- optionsParser.processOptions (pszOptions, pszOptionsEx);
211
209
212
210
// Prepare error log
213
211
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 =
222
225
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));
229
228
230
- // Prepare output buffer
231
- std::unique_ptr<llvm::raw_pwrite_stream>
229
+ // Prepare output buffer
230
+ std::unique_ptr<llvm::raw_pwrite_stream>
232
231
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));
236
235
237
- compiler->setDiagnostics (&*Diags);
236
+ compiler->setDiagnostics (&*Diags);
238
237
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);
244
243
245
- compiler->createFileManager (OverlayFS);
246
- compiler->createSourceManager (compiler->getFileManager ());
244
+ compiler->createFileManager (OverlayFS);
245
+ compiler->createSourceManager (compiler->getFileManager ());
247
246
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);
254
253
255
- // Configure our handling of diagnostics.
256
- ProcessWarningOptions (*Diags, compiler->getDiagnosticOpts ());
254
+ // Configure our handling of diagnostics.
255
+ ProcessWarningOptions (*Diags, compiler->getDiagnosticOpts ());
257
256
258
- // Map memory buffers to a virtual file system
257
+ // Map memory buffers to a virtual file system
259
258
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 (
264
263
llvm::StringRef (pszProgramSource), optionsParser.getSourceName ()));
265
264
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
+ }
271
270
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 );
276
275
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
+ }
279
278
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
+ }
285
285
}
286
-
287
-
288
286
// Execute the frontend actions.
289
287
bool success = false ;
290
288
try {
291
289
success = clang::ExecuteCompilerInvocation (compiler.get ());
292
290
} catch (const std::exception &) {
293
291
}
294
-
295
292
pResult->setIRType (IR_TYPE_COMPILED_OBJECT);
296
293
pResult->setIRName (optionsParser.getSourceName ());
297
294
@@ -306,11 +303,11 @@ Compile(const char *pszProgramSource, const char **pInputHeaders,
306
303
if (success && optionsParser.hasEmitSPIRV ()) {
307
304
// Translate LLVM IR to SPIR-V.
308
305
llvm::StringRef LLVM_IR (static_cast <const char *>(pResult->GetIR ()),
309
- pResult->GetIRSize ());
306
+ pResult->GetIRSize ());
310
307
std::unique_ptr<llvm::MemoryBuffer> MB = llvm::MemoryBuffer::getMemBuffer (LLVM_IR, pResult->GetIRName (), false );
311
308
llvm::LLVMContext Context;
312
309
auto E = llvm::getOwningLazyBitcodeModule (std::move (MB), Context,
313
- /* ShouldLazyLoadMetadata=*/ true );
310
+ /* ShouldLazyLoadMetadata=*/ true );
314
311
llvm::logAllUnhandledErrors (E.takeError (), err_ostream, " error: " );
315
312
std::unique_ptr<llvm::Module> M = std::move (*E);
316
313
@@ -334,9 +331,11 @@ Compile(const char *pszProgramSource, const char **pInputHeaders,
334
331
err_ostream << Err.c_str ();
335
332
err_ostream.flush ();
336
333
}
337
-
338
- if (pBinaryResult) {
339
- *pBinaryResult = pResult.release ();
334
+ {
335
+ llvm::sys::SmartScopedLock<true > compileGuard {*compileMutex};
336
+ if (pBinaryResult) {
337
+ *pBinaryResult = pResult.release ();
338
+ }
340
339
}
341
340
return success ? CL_SUCCESS : CL_COMPILE_PROGRAM_FAILURE;
342
341
} catch (std::bad_alloc &) {
0 commit comments