llvm.org GIT mirror llvm / dc85724
Change the JIT to compile eagerly by default as agreed in http://llvm.org/PR5184, and beef up the comments to describe what both options do and the risks of lazy compilation in the presence of threads. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@85295 91177308-0d34-0410-b5e6-96231b3b80d8 Jeffrey Yasskin 9 years ago
8 changed file(s) with 62 addition(s) and 62 deletion(s). Raw diff Collapse all Expand all
387387
388388
389389
390

This illustrates that we can now call user code, but there is something a bit subtle

391 going on here. Note that we only invoke the JIT on the anonymous functions
392 that call testfunc, but we never invoked it on testfunc
393 itself.

394
395

What actually happened here is that the anonymous function was

396 JIT'd when requested. When the Kaleidoscope app calls through the function
397 pointer that is returned, the anonymous function starts executing. It ends up
398 making the call to the "testfunc" function, and ends up in a stub that invokes
399 the JIT, lazily, on testfunc. Once the JIT finishes lazily compiling testfunc,
400 it returns and the code re-executes the call.

401
402

In summary, the JIT will lazily JIT code, on the fly, as it is needed. The

403 JIT provides a number of other more advanced interfaces for things like freeing
404 allocated machine code, rejit'ing functions to update them, etc. However, even
405 with this simple code, we get some surprisingly powerful capabilities - check
406 this out (I removed the dump of the anonymous functions, you should get the idea
407 by now :) :

390

This illustrates that we can now call user code, but there is something a bit

391 subtle going on here. Note that we only invoke the JIT on the anonymous
392 functions that call testfunc, but we never invoked it
393 on testfunc itself. What actually happened here is that the JIT
394 scanned for all non-JIT'd functions transitively called from the anonymous
395 function and compiled all of them before returning
396 from getPointerToFunction().

397
398

The JIT provides a number of other more advanced interfaces for things like

399 freeing allocated machine code, rejit'ing functions to update them, etc.
400 However, even with this simple code, we get some surprisingly powerful
401 capabilities - check this out (I removed the dump of the anonymous functions,
402 you should get the idea by now :) :

408403
409404
410405

                  
                
452447 resolved. It allows you to establish explicit mappings between IR objects and
453448 addresses (useful for LLVM global variables that you want to map to static
454449 tables, for example), allows you to dynamically decide on the fly based on the
455 function name, and even allows you to have the JIT abort itself if any lazy
456 compilation is attempted.

450 function name, and even allows you to have the JIT compile functions lazily the
451 first time they're called.

457452
458453

One interesting application of this is that we can now extend the language

459454 by writing arbitrary C++ code to implement operations. For example, if we add:
405405
406406

This illustrates that we can now call user code, but there is something a bit

407407 subtle going on here. Note that we only invoke the JIT on the anonymous
408 functions that call testfunc, but we never invoked it on testfunc
409 itself.

410
411

What actually happened here is that the anonymous function was JIT'd when

412 requested. When the Kaleidoscope app calls through the function pointer that is
413 returned, the anonymous function starts executing. It ends up making the call
414 to the "testfunc" function, and ends up in a stub that invokes the JIT, lazily,
415 on testfunc. Once the JIT finishes lazily compiling testfunc,
416 it returns and the code re-executes the call.

417
418

In summary, the JIT will lazily JIT code, on the fly, as it is needed. The

419 JIT provides a number of other more advanced interfaces for things like freeing
420 allocated machine code, rejit'ing functions to update them, etc. However, even
421 with this simple code, we get some surprisingly powerful capabilities - check
422 this out (I removed the dump of the anonymous functions, you should get the idea
423 by now :) :

408 functions that call testfunc, but we never invoked it
409 on testfunc itself. What actually happened here is that the JIT
410 scanned for all non-JIT'd functions transitively called from the anonymous
411 function and compiled all of them before returning
412 from run_function.

413
414

The JIT provides a number of other more advanced interfaces for things like

415 freeing allocated machine code, rejit'ing functions to update them, etc.
416 However, even with this simple code, we get some surprisingly powerful
417 capabilities - check this out (I removed the dump of the anonymous functions,
418 you should get the idea by now :) :

424419
425420
426421

                  
                
466461 get resolved. It allows you to establish explicit mappings between IR objects
467462 and addresses (useful for LLVM global variables that you want to map to static
468463 tables, for example), allows you to dynamically decide on the fly based on the
469 function name, and even allows you to have the JIT abort itself if any lazy
470 compilation is attempted.

464 function name, and even allows you to have the JIT compile functions lazily the
465 first time they're called.

471466
472467

One interesting application of this is that we can now extend the language

473468 by writing arbitrary C code to implement operations. For example, if we add:
8787 class ExecutionEngine {
8888 const TargetData *TD;
8989 ExecutionEngineState EEState;
90 bool LazyCompilationDisabled;
90 bool CompilingLazily;
9191 bool GVCompilationDisabled;
9292 bool SymbolSearchingDisabled;
9393 bool DlsymStubsEnabled;
318318 virtual void RegisterJITEventListener(JITEventListener *) {}
319319 virtual void UnregisterJITEventListener(JITEventListener *) {}
320320
321 /// DisableLazyCompilation - If called, the JIT will abort if lazy compilation
322 /// is ever attempted.
323 void DisableLazyCompilation(bool Disabled = true) {
324 LazyCompilationDisabled = Disabled;
325 }
326 bool isLazyCompilationDisabled() const {
327 return LazyCompilationDisabled;
321 /// EnableLazyCompilation - When lazy compilation is off (the default), the
322 /// JIT will eagerly compile every function reachable from the argument to
323 /// getPointerToFunction. If lazy compilation is turned on, the JIT will only
324 /// compile the one function and emit stubs to compile the rest when they're
325 /// first called. If lazy compilation is turned off again while some lazy
326 /// stubs are still around, and one of those stubs is called, the program will
327 /// abort.
328 ///
329 /// In order to safely compile lazily in a threaded program, the user must
330 /// ensure that 1) only one thread at a time can call any particular lazy
331 /// stub, and 2) any thread modifying LLVM IR must hold the JIT's lock
332 /// (ExecutionEngine::lock) or otherwise ensure that no other thread calls a
333 /// lazy stub. See http://llvm.org/PR5184 for details.
334 void EnableLazyCompilation(bool Enabled = true) {
335 CompilingLazily = Enabled;
336 }
337 bool isCompilingLazily() const {
338 return CompilingLazily;
328339 }
329340
330341 /// DisableGVCompilation - If called, the JIT will abort if it's asked to
4848 ExecutionEngine::ExecutionEngine(ModuleProvider *P)
4949 : EEState(*this),
5050 LazyFunctionCreator(0) {
51 LazyCompilationDisabled = false;
51 CompilingLazily = false;
5252 GVCompilationDisabled = false;
5353 SymbolSearchingDisabled = false;
5454 DlsymStubsEnabled = false;
598598 isAlreadyCodeGenerating = false;
599599
600600 // If the function referred to another function that had not yet been
601 // read from bitcode, but we are jitting non-lazily, emit it now.
601 // read from bitcode, and we are jitting non-lazily, emit it now.
602602 while (!jitstate->getPendingFunctions(locked).empty()) {
603603 Function *PF = jitstate->getPendingFunctions(locked).back();
604604 jitstate->getPendingFunctions(locked).pop_back();
615615
616616 // If the JIT is configured to emit info so that dlsym can be used to
617617 // rewrite stubs to external globals, do so now.
618 if (areDlsymStubsEnabled() && isLazyCompilationDisabled())
618 if (areDlsymStubsEnabled() && !isCompilingLazily())
619619 updateDlsymStubTable();
620620 }
621621
294294 void *&Stub = state.getFunctionToStubMap(locked)[F];
295295 if (Stub) return Stub;
296296
297 // Call the lazy resolver function unless we are JIT'ing non-lazily, in which
298 // case we must resolve the symbol now.
299 void *Actual = TheJIT->isLazyCompilationDisabled()
300 ? (void *)0 : (void *)(intptr_t)LazyResolverFn;
301
297 // Call the lazy resolver function if we are JIT'ing lazily. Otherwise we
298 // must resolve the symbol now.
299 void *Actual = TheJIT->isCompilingLazily()
300 ? (void *)(intptr_t)LazyResolverFn : (void *)0;
301
302302 // If this is an external declaration, attempt to resolve the address now
303303 // to place in the stub.
304304 if (F->isDeclaration() && !F->hasNotBeenReadFromBitcode()) {
333333 // If we are JIT'ing non-lazily but need to call a function that does not
334334 // exist yet, add it to the JIT's work list so that we can fill in the stub
335335 // address later.
336 if (!Actual && TheJIT->isLazyCompilationDisabled())
336 if (!Actual && !TheJIT->isCompilingLazily())
337337 if (!F->isDeclaration() || F->hasNotBeenReadFromBitcode())
338338 TheJIT->addPendingFunction(F);
339339
470470 // Otherwise we don't have it, do lazy compilation now.
471471
472472 // If lazy compilation is disabled, emit a useful error message and abort.
473 if (TheJIT->isLazyCompilationDisabled()) {
473 if (!TheJIT->isCompilingLazily()) {
474474 llvm_report_error("LLVM JIT requested to do lazy compilation of function '"
475475 + F->getName() + "' when lazy compiles are disabled!");
476476 }
768768 // mechanism is capable of rewriting the instruction directly, prefer to do
769769 // that instead of emitting a stub. This uses the lazy resolver, so is not
770770 // legal if lazy compilation is disabled.
771 if (DoesntNeedStub && !TheJIT->isLazyCompilationDisabled())
771 if (DoesntNeedStub && TheJIT->isCompilingLazily())
772772 return Resolver.AddCallbackAtLocation(F, Reference);
773773
774774 // Otherwise, we have to emit a stub.
164164
165165 EE->RegisterJITEventListener(createOProfileJITEventListener());
166166
167 if (NoLazyCompilation)
168 EE->DisableLazyCompilation();
167 EE->EnableLazyCompilation(!NoLazyCompilation);
169168
170169 // If the user specifically requested an argv[0] to pass into the program,
171170 // do it now.
303303 Builder.CreateRet(result);
304304
305305 TheJIT->EnableDlsymStubs(false);
306 TheJIT->DisableLazyCompilation();
306 TheJIT->EnableLazyCompilation(false);
307307 int (*TestFunctionPtr)() = reinterpret_cast(
308308 (intptr_t)TheJIT->getPointerToFunction(TestFunction));
309309 // This used to crash in trying to call PlusOne().
313313 #if !defined(__arm__) && !defined(__powerpc__) && !defined(__ppc__)
314314 // Test a function C which calls A and B which call each other.
315315 TEST_F(JITTest, NonLazyCompilationStillNeedsStubs) {
316 TheJIT->DisableLazyCompilation();
316 TheJIT->EnableLazyCompilation(false);
317317
318318 const FunctionType *Func1Ty =
319319 cast(TypeBuilder::get(Context));
369369 // Regression test for PR5162. This used to trigger an AssertingVH inside the
370370 // JIT's Function to stub mapping.
371371 TEST_F(JITTest, NonLazyLeaksNoStubs) {
372 TheJIT->DisableLazyCompilation();
372 TheJIT->EnableLazyCompilation(false);
373373
374374 // Create two functions with a single basic block each.
375375 const FunctionType *FuncTy =