llvm.org GIT mirror llvm / 14e4008
lli: avoid global variables, use a local unique_ptr instead There was issue with order of destruction in some cases. From: Mehdi Amini <mehdi.amini@apple.com> git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@266652 91177308-0d34-0410-b5e6-96231b3b80d8 Mehdi Amini 4 years ago
1 changed file(s) with 11 addition(s) and 24 deletion(s). Raw diff Collapse all Expand all
311311 }
312312 };
313313
314 static LLVMContext Context;
315 static ExecutionEngine *EE = nullptr;
316 static LLIObjectCache *CacheManager = nullptr;
317
318 static void do_shutdown() {
319 // Cygwin-1.5 invokes DLL's dtors before atexit handler.
320 #ifndef DO_NOTHING_ATEXIT
321 delete EE;
322 if (CacheManager)
323 delete CacheManager;
324 llvm_shutdown();
325 #endif
326 }
327
328314 // On Mingw and Cygwin, an external symbol named '__main' is called from the
329315 // generated 'main' function to allow static intialization. To avoid linking
330316 // problems with remote targets (because lli's remote target support does not
331317 // currently handle external linking) we add a secondary module which defines
332318 // an empty '__main' function.
333 static void addCygMingExtraModule(ExecutionEngine *EE,
334 LLVMContext &Context,
319 static void addCygMingExtraModule(ExecutionEngine &EE, LLVMContext &Context,
335320 StringRef TargetTripleStr) {
336321 IRBuilder<> Builder(Context);
337322 Triple TargetTriple(TargetTripleStr);
361346 Builder.CreateRet(ReturnVal);
362347
363348 // Add this new module to the ExecutionEngine.
364 EE->addModule(std::move(M));
349 EE.addModule(std::move(M));
365350 }
366351
367352 CodeGenOpt::Level getOptLevel() {
385370 sys::PrintStackTraceOnErrorSignal();
386371 PrettyStackTraceProgram X(argc, argv);
387372
388 atexit(do_shutdown); // Call llvm_shutdown() on exit.
373 atexit(llvm_shutdown); // Call llvm_shutdown() on exit.
389374
390375 // If we have a native target, initialize it to ensure it is linked in and
391376 // usable by the JIT.
399384 // If the user doesn't want core files, disable them.
400385 if (DisableCoreFiles)
401386 sys::Process::PreventCoreFiles();
387
388 LLVMContext Context;
402389
403390 // Load the bitcode...
404391 SMDiagnostic Err;
470457
471458 builder.setTargetOptions(Options);
472459
473 EE = builder.create();
460 std::unique_ptr EE(builder.create());
474461 if (!EE) {
475462 if (!ErrorMsg.empty())
476463 errs() << argv[0] << ": error creating EE: " << ErrorMsg << "\n";
479466 exit(1);
480467 }
481468
469 std::unique_ptr CacheManager;
482470 if (EnableCacheManager) {
483 CacheManager = new LLIObjectCache(ObjectCacheDir);
484 EE->setObjectCache(CacheManager);
471 CacheManager.reset(new LLIObjectCache(ObjectCacheDir));
472 EE->setObjectCache(CacheManager.get());
485473 }
486474
487475 // Load any additional modules specified on the command line.
537525 // If the target is Cygwin/MingW and we are generating remote code, we
538526 // need an extra module to help out with linking.
539527 if (RemoteMCJIT && Triple(Mod->getTargetTriple()).isOSCygMing()) {
540 addCygMingExtraModule(EE, Context, Mod->getTargetTriple());
528 addCygMingExtraModule(*EE, Context, Mod->getTargetTriple());
541529 }
542530
543531 // The following functions have no effect if their respective profiling
706694 // Delete the EE - we need to tear it down *before* we terminate the session
707695 // with the remote, otherwise it'll crash when it tries to release resources
708696 // on a remote that has already been disconnected.
709 delete EE;
710 EE = nullptr;
697 EE.reset();
711698
712699 // Signal the remote target that we're done JITing.
713700 R->terminateSession();