| Stefan Gränitz @ LLVM Social Berlin Thursday, September 29th 2016 | 
|  stefan.graenitz@gmail.com  github.com/weliveindetail  @weliveindetail | 
Whenever a program, while running, creates and runs some new executable code which was not part of the program when it was stored on disk, it’s a JIT. Eli Bendersky
              Static Compilation
              Compile → Link → Load → Run
              
              Runtime Compilation
              Compile → Load → Link → Run
            
| Runtime Compiler | Static Compiler | |
|---|---|---|
| Compile | emit machine code | emit assembly | 
| Link | resolve symbols & relocations | use system tools for static & dynamic linking | 
| Load | provide & manage memory | use system loader (implicitly) | 
| Run | lifetime management for heap allocations | nothing to do as long as DLLs aren't unlinked | 
It's pretty simple actually:
Example: RelocationStubsPlayground (VC++ on Win64)
              What's that?
              A compiler infrastructure.
            
              So...?
              Well it defines an intermediate representation.
              And it provides a set of tools to manipulate it.
            
What do we gain?
Basically it takes away all the really heavy stuff!
The 20000ft overview
Engines
MCJIT (Machine Code JIT)
ORC (On-Request Compilation)
; ModuleID = 'test'
target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
; Declare the string constant as a global constant.
@.str = private unnamed_addr constant [13 x i8] c"hello world\0A\00"
; External declaration of the puts function
declare i32 @puts(i8* nocapture) nounwind
; Definition of main function
define i32 @main() {   ; i32()*
entry:
  ; Convert [13 x i8]* to i8  *...
  %cast210 = getelementptr [13 x i8], [13 x i8]* @.str, i64 0, i64 0
  ; Call puts function to write out the string to stdout.
  call i32 @puts(i8* %cast210)
  ret i32 0
}
; Named metadata
!0 = !{i32 42, null, !"string"}
!foo = !{!0}
            Properties
Representations
Restrictions
Setup LLVM
Command Line
clang++ `llvm-config --libs --system-libs`
                          
                          -D__STDC_CONSTANT_MACROS
                          
                          -D__STDC_LIMIT_MACROS
                          
                          -lz -Wall -std=c++14
                          
                          -o myExec mySource.cpp
          CMake Tips
find_package(LLVM)llvm_map_components_to_libnames(LLVM_LIBS 
                                        
                                  core orcjit x86asmparser x86codegen)Checkout the tools
See the IR that clang generates from C++$ clang++ -S -emit-llvm foo.cpp
              $ lli foo.ll
              
              Explore code generation
              
                                                   http://github.com/weliveindetail/JitFromScratch
              
Keep in mind
llvm_shutdown() and reinitCompileOnDemandLayer for lazy jittingabs → _absLoadLibraryPermanently(nullptr)getelementptr
                customIntAllocator →__Z18customIntAllocatori
                  customIntAllocator → 
                    ?customIntAllocator@@YAPEAHH@Z
                  IRTransformLayer to invoke optimizerPassManagerBuilder to populate passesBBVectorize → Basic-Block vectorizationSLPVectorize → 
                      Superword-Level Parallelism
                  llvm::ObjectCacheIRCompileLayer