llvm tutorial

Embed Size (px)

Citation preview

  • 7/25/2019 llvm tutorial

    1/61

    The LLVM CompilerThe LLVM Compiler

    Framework and InfrastructureFramework and Infrastructure

    Vikram AdveVikram [email protected]@cs.uiuc.e

    dudu

    Chris LattnerChris [email protected]@cs.uiuc.e

    dudu

    http://llvm.cs.uiuc.edu/http://llvm.cs.uiuc.edu/

    LCPC Tutorial: September 22, 2004LCPC Tutorial: September 22, 2004

  • 7/25/2019 llvm tutorial

    2/61

    Chris Lattner

    A!kno$led%ementsA!kno$led%ementsUIUC Contributors:UIUC Contributors:

    Tan&a 'rethourTan&a 'rethour

    (isha 'rukman(isha 'rukman

    Cameron 'us!hardtCameron 'us!hardt

    )ohn Cris$ell)ohn Cris$ell

    Alkis *vlo%imenosAlkis *vlo%imenos

    'rian +aeke'rian +aeke u!hira Sasankau!hira Sasanka

    Anand ShuklaAnand Shukla

    'ill -endlin%'ill -endlin%

    External Contributors:External Contributors:

    .enrik 'a!h.enrik 'a!h

    /ate 'e%eman/ate 'e%eman

    )e Cohen)e Cohen

    Paolo 1nverniiPaolo 1nvernii

    'rad )ones'rad )ones

    Vladimir (erliakovVladimir (erliakov Vladimir PrusVladimir Prus

    eid Spen!ereid Spen!er

    Funding:Funding:

    This $ork is sponsored b& the /S3 /et +eneration Sot$are pro%ram throu%h %rantsThis $ork is sponsored b& the /S3 /et +eneration Sot$are pro%ram throu%h %rants*1A50067428 9an /S3 CA** a$ard and *1A50;07peratin% S&stems and Compilers pro%ram 9%rant ?CC566@@4@2, theb& the /S3 >peratin% S&stems and Compilers pro%ram 9%rant ?CC566@@4@2, the

    /S3 *mbedded S&stems pro%ram 9%rant ?CC50206202, the (AC>#APA/S3 *mbedded S&stems pro%ram 9%rant ?CC50206202, the (AC>#APA

    +i%as!ale S&stems esear!h Center 9+SC, 1'( throu%h the APA5unded+i%as!ale S&stems esear!h Center 9+SC, 1'( throu%h the APA5unded

    P*CS proBe!t, and the (otorola niversit& Partnerships in esear!h pro%ram"P*CS proBe!t, and the (otorola niversit& Partnerships in esear!h pro%ram"

  • 7/25/2019 llvm tutorial

    3/61

    Chris Lattner

    LLV( Compiler S&stemLLV( Compiler S&stem

    The LLVM Compiler InfrastructureThe LLVM Compiler InfrastructureProvides reusable !omponents or buildin% !ompilersProvides reusable !omponents or buildin% !ompilers

    edu!e the time#!ost to build a ne$ !ompileredu!e the time#!ost to build a ne$ !ompiler

    'uild stati! !ompilers, )1Ts, tra!e5based optimiers, """'uild stati! !ompilers, )1Ts, tra!e5based optimiers, """

    The LLVM Compiler FrameworThe LLVM Compiler Framewor*nd5to5end !ompilers usin% the LLV( inrastru!ture*nd5to5end !ompilers usin% the LLV( inrastru!ture

    C and CDD are robust and a%%ressive:C and CDD are robust and a%%ressive: )ava, S!heme and others are in development)ava, S!heme and others are in development

    *mit C !ode or native !ode or E@8, Spar!, Po$erPC*mit C !ode or native !ode or E@8, Spar!, Po$erPC

  • 7/25/2019 llvm tutorial

    4/61

    Chris Lattner

    Three primar& LLV( !omponentsThree primar& LLV( !omponents

    The LLVMThe LLVM Virtual Instruction SetVirtual Instruction SetThe !ommon lan%ua%e5 and tar%et5independent 1The !ommon lan%ua%e5 and tar%et5independent 1

    1nternal 91 and eternal 9persistent representation1nternal 91 and eternal 9persistent representation

    ! collection of well"integrated libraries! collection of well"integrated librariesAnal&ses, optimiations, !ode %enerators, )1TAnal&ses, optimiations, !ode %enerators, )1T

    !ompiler, %arba%e !olle!tion support, proilin%, F!ompiler, %arba%e !olle!tion support, proilin%, F

    ! collection of tools built from the libraries! collection of tools built from the librariesAssemblers, automati! debu%%er, linker, !odeAssemblers, automati! debu%%er, linker, !ode

    %enerator, !ompiler driver, modular optimier, F%enerator, !ompiler driver, modular optimier, F

  • 7/25/2019 llvm tutorial

    5/61

    Chris Lattner

    Tutorial >vervie$Tutorial >vervie$

    Introduction to the running exampleIntroduction to the running example LLVM C#C$$ Compiler %&er&iewLLVM C#C$$ Compiler %&er&iew.i%h5level vie$ o an eample LLV( !ompiler.i%h5level vie$ o an eample LLV( !ompiler

    The LLVM Virtual Instruction 'etThe LLVM Virtual Instruction 'et 1 overvie$ and t&pe5s&stem1 overvie$ and t&pe5s&stem

    LLVM C$$ I( and important !)I*sLLVM C$$ I( and important !)I*s'asi!s, Pass(ana%er, datalo$, Ar%Promotion'asi!s, Pass(ana%er, datalo$, Ar%Promotion

    Important LLVM ToolsImportant LLVM Toolsopt, !ode %enerator, )1T, test suite, bu%pointopt, !ode %enerator, )1T, test suite, bu%point

    Example applications of LLVMExample applications of LLVM

  • 7/25/2019 llvm tutorial

    6/61

    Chris Lattner

    unnin% eample: ar% promotionunnin% eample: ar% promotion

    Consider use of b+"reference parameters:Consider use of b+"reference parameters:int callee,const int -./ 0int callee,const int -./ 0

    return .$12return .$12

    33

    int caller,/ 0int caller,/ 0

    return callee,4/2return callee,4/2

    33

    int callee,const int 5./ 0int callee,const int 5./ 0

    return 5.$12return 5.$12 // memory load// memory load

    33

    int caller,/ 0int caller,/ 0

    int tmp2int tmp2 // stack object// stack object

    tmp 6 42tmp 6 42 // memory store// memory store

    return callee,-tmp/2return callee,-tmp/2

    33

    compiles to

    Eliminated load in callee

    Eliminated store in caller

    Eliminated stac slot for 7tmp*

    int callee,int ./ 0int callee,int ./ 0 return .$12return .$12

    33

    int caller,/ 0int caller,/ 0

    return callee,4/2return callee,4/2

    33

    8e want:8e want:

  • 7/25/2019 llvm tutorial

    7/61Chris Lattner

    -h& is this hardG-h& is this hardG

    (e9uires interprocedural anal+sis:(e9uires interprocedural anal+sis:(ust !han%e the protot&pe o the !allee(ust !han%e the protot&pe o the !allee

    (ust update all !all sites(ust update all !all sites $e must$e must nownowall !allersall !allers

    -hat about !allers outside the translation unitG-hat about !allers outside the translation unitG

    (e9uires alias anal+sis:(e9uires alias anal+sis:eeren!e !ould alias other pointers in !alleeeeren!e !ould alias other pointers in !allee

    (ust kno$ that loaded value doesnHt !han%e rom(ust kno$ that loaded value doesnHt !han%e rom

    un!tion entr& to the loadun!tion entr& to the load(ust kno$ the pointer is not bein% stored throu%h(ust kno$ the pointer is not bein% stored throu%h

    (eference might not be to a stac obect;(eference might not be to a stac obect;

  • 7/25/2019 llvm tutorial

    8/61Chris Lattner

    Tutorial >vervie$Tutorial >vervie$

    Introduction to the running exampleIntroduction to the running example LLVM C#C$$ Compiler %&er&iewLLVM C#C$$ Compiler %&er&iew.i%h5level vie$ o an eample LLV( !ompiler.i%h5level vie$ o an eample LLV( !ompiler

    The LLVM Virtual Instruction 'etThe LLVM Virtual Instruction 'et 1 overvie$ and t&pe5s&stem1 overvie$ and t&pe5s&stem

    LLVM C$$ I( and important !)I*sLLVM C$$ I( and important !)I*s'asi!s, Pass(ana%er, datalo$, Ar%Promotion'asi!s, Pass(ana%er, datalo$, Ar%Promotion

    Important LLVM ToolsImportant LLVM Toolsopt, !ode %enerator, )1T, test suite, bu%pointopt, !ode %enerator, )1T, test suite, bu%point

    Example applications of LLVMExample applications of LLVM

  • 7/25/2019 llvm tutorial

    9/61Chris Lattner

    The LLV( C#CDD CompilerThe LLV( C#CDD Compiler

    From the high le&el< it is a standard compiler:From the high le&el< it is a standard compiler:Compatible $ith standard makeilesCompatible $ith standard makeiles

    ses +CC 7"4 C and CDD parserses +CC 7"4 C and CDD parser

    =istinguishing features:=istinguishing features:ses LLV( optimiers, not +CC optimiersses LLV( optimiers, not +CC optimiers

    "o iles !ontain LLV( 1#b&te!ode, not ma!hine !ode"o iles !ontain LLV( 1#b&te!ode, not ma!hine !ode

    *e!utable !an be b&te!ode 9)1THd or ma!hine !ode*e!utable !an be b&te!ode 9)1THd or ma!hine !ode

    llvm%DD

    llvm%!!C ile

    CDD ile

    "o ile

    "o ilellvm linker ee!utable

    Compile Time Link Time

  • 7/25/2019 llvm tutorial

    10/61Chris Lattner

    Lookin% into events at !ompile5timeLookin% into events at !ompile5time

    llvm%!!C ile "o ile llvm%DDCDD ile "o ile

    (odiied version o +DD

    *mits LLV( 1 as tet ile

    Lo$ers CDD AST to LLV(

    (odiied version o +CC

    *mits LLV( 1 as tet ile

    Lo$ers C AST to LLV(

    LLV( 1

    Parser

    LLV(

    Veriier

    40 LLV( Anal&sis I

    >ptimiation Passes

    LLV( "b!

    3ile -riter

    C to LLV(

    3rontend

    Compile5time

    >ptimier

    CDD to LLV(

    3rontend

    Compile5time

    >ptimier

    J!!;K J!!;plusK J%!!asKJ%!!asK

    ead +lobal *limination, 1P Constant Propa%ation, ead

    Ar%ument *limination, 1nlinin%, easso!iation, L1C(, Loop

    >pts, (emor& Promotion, ead Store *limination, AC*, F

  • 7/25/2019 llvm tutorial

    11/61

    Chris Lattner

    Lookin% into events at link5timeLookin% into events at link5time"o ile

    "o ilellvm linker ee!utable

    /ative Code

    'a!kend/ative ee!utable

    Jll!K

    C Code

    'a!kend

    C Compiler/ative

    ee!utableJll! mar!h!K

    J%!!K

    Link in native "o iles

    and libraries here

    LLV(

    Linker

    Link5time

    >ptimier

    "b! ile or LLV( )1T

    "o ile

    "o ile

    20 LLV( Anal&sis I

    >ptimiation Passes

    >ptionall& JinternaliesK:

    marks most un!tions as

    internal, to improve 1P>

    Pere!t pla!e or ar%ument

    promotion optimiationM

  • 7/25/2019 llvm tutorial

    12/61

    Chris Lattner

    +oals o the !ompiler desi%n+oals o the !ompiler desi%n

    !nal+>e and optimi>e as earl+ as possible:!nal+>e and optimi>e as earl+ as possible:Compile5time opts redu!e modi&5rebuild5ee!ute !&!leCompile5time opts redu!e modi&5rebuild5ee!ute !&!le

    Compile5time optimiations redu!e $ork at link5time 9b&Compile5time optimiations redu!e $ork at link5time 9b&

    shrinkin% the pro%ramshrinkin% the pro%ram

    !ll I)!#I)% mae an open"world assumption!ll I)!#I)% mae an open"world assumptionThus, the& all $ork on libraries and at !ompile5timeThus, the& all $ork on libraries and at !ompile5time

    JJ1nternalieK pass enables J$hole pro%ramK optn1nternalieK pass enables J$hole pro%ramK optn

    %ne I( ,without lowering/ for anal+sis - opt>n%ne I( ,without lowering/ for anal+sis - opt>nCompile5time optns !an be run at link5time tooMCompile5time optns !an be run at link5time tooMThe same 1 is used as input to the )1TThe same 1 is used as input to the )1T

    IR design is the key to these goals!IR design is the key to these goals!

  • 7/25/2019 llvm tutorial

    13/61

    Chris Lattner

    Tutorial >vervie$Tutorial >vervie$

    Introduction to the running exampleIntroduction to the running example LLVM C#C$$ Compiler %&er&iewLLVM C#C$$ Compiler %&er&iew.i%h5level vie$ o an eample LLV( !ompiler.i%h5level vie$ o an eample LLV( !ompiler

    The LLVM Virtual Instruction 'etThe LLVM Virtual Instruction 'et 1 overvie$ and t&pe5s&stem1 overvie$ and t&pe5s&stem

    LLVM C$$ I( and important !)I*sLLVM C$$ I( and important !)I*s'asi!s, Pass(ana%er, datalo$, Ar%Promotion'asi!s, Pass(ana%er, datalo$, Ar%Promotion

    Important LLVM ToolsImportant LLVM Toolsopt, !ode %enerator, )1T, test suite, bu%pointopt, !ode %enerator, )1T, test suite, bu%point

    Example applications of LLVMExample applications of LLVM

  • 7/25/2019 llvm tutorial

    14/61

    Chris Lattner

    +oals o LLV( 1+oals o LLV( 1

    Eas+ to produce< understand< and define;Eas+ to produce< understand< and define; Language" and Target"IndependentLanguage" and Target"IndependentAST5level 1 9e"%" A/3, /C>L is not ver& easibleAST5level 1 9e"%" A/3, /C>L is not ver& easible

    *ver& anal&sis#orm must kno$ about NallH lan%ua%es*ver& anal&sis#orm must kno$ about NallH lan%ua%es

    %ne I( for anal+sis and optimi>ation%ne I( for anal+sis and optimi>ation 1 must be able to support a%%ressive 1P>, loop opts,1 must be able to support a%%ressive 1P>, loop opts,

    s!alar opts, F hi%h5s!alar opts, F hi%h5 andandlo$5level optimiationMlo$5level optimiationM

    %ptimi>e as much as earl+ as possible%ptimi>e as much as earl+ as possibleCanHt postpone ever&thin% until link or runtimeCanHt postpone ever&thin% until link or runtime

    /o lo$erin% in the 1M/o lo$erin% in the 1M

  • 7/25/2019 llvm tutorial

    15/61

    Chris Lattner

    LLV( 1nstru!tion Set >vervie$ ?;LLV( 1nstru!tion Set >vervie$ ?;

    Low"le&el and target"independent semanticsLow"le&el and target"independent semantics1SC5like three address !ode1SC5like three address !ode

    1ninite virtual re%ister set in SSA orm1ninite virtual re%ister set in SSA orm

    Simple, lo$5level !ontrol lo$ !onstru!tsSimple, lo$5level !ontrol lo$ !onstru!ts

    Load#store instru!tions $ith t&ped5pointersLoad#store instru!tions $ith t&ped5pointers

    I( has text< binar+< and in"memor+ formsI( has text< binar+< and in"memor+ forms

    for (i = 0; i < N;for (i = 0; i < N;

    ++i)++i)

    Sum(&A[i], &P);Sum(&A[i], &P);

    loop:loop:

    %i.1 =%i.1 =phiphiintint[ 0, %0 ], [ %i.!, %loop ][ 0, %0 ], [ %i.!, %loop ] %AiA""r =%AiA""r = #$t$l$m$ntptr#$t$l$m$ntptrflotflot%A,%A, intint%i.1%i.1

    'll'lloi"oi"%Sum(%Sum(flotflot%AiA""r,%AiA""r, %pir%pir%P)%P)

    %i.! =%i.! = """"intint%i.1, 1%i.1, 1

    %tmp. =%tmp. = *$tlt*$tltintint%i.1, %N%i.1, %N

    rroolool%tmp.,%tmp., l$ll$l%loop,%loop, l$ll$l%outloop%outloop

  • 7/25/2019 llvm tutorial

    16/61

    Chris Lattner

    LLV( 1nstru!tion Set >vervie$ ?2LLV( 1nstru!tion Set >vervie$ ?2

    ?igh"le&el information exposed in the code?igh"le&el information exposed in the code*pli!it datalo$ throu%h SSA orm*pli!it datalo$ throu%h SSA orm

    *pli!it !ontrol5lo$ %raph 9even or e!eptions*pli!it !ontrol5lo$ %raph 9even or e!eptions

    *pli!it lan%ua%e5independent t&pe5inormation*pli!it lan%ua%e5independent t&pe5inormation

    *pli!it t&ped pointer arithmeti!*pli!it t&ped pointer arithmeti! Preserve arra& subs!ript and stru!ture indein%Preserve arra& subs!ript and stru!ture indein%

    for (i = 0; i < N;for (i = 0; i < N;

    ++i)++i)

    Sum(&A[i], &P);Sum(&A[i], &P);

    loop:loop:

    %i.1 =%i.1 =phiphiintint[ 0, %0 ], [ %i.!, %loop ][ 0, %0 ], [ %i.!, %loop ] %AiA""r =%AiA""r = #$t$l$m$ntptr#$t$l$m$ntptrflotflot%A,%A, intint%i.1%i.1

    'll'lloi"oi"%Sum(%Sum(flotflot%AiA""r,%AiA""r, %pir%pir%P)%P)

    %i.! =%i.! = """"intint%i.1, 1%i.1, 1

    %tmp. =%tmp. = *$tlt*$tltintint%i.1, %N%i.1, %N

    rroolool%tmp.,%tmp., l$ll$l%loop,%loop, l$ll$l%outloop%outloop

  • 7/25/2019 llvm tutorial

    17/61

    Chris Lattner

    LLV( T&pe S&stem etailsLLV( T&pe S&stem etails

    The entire t+pe s+stem consists of:The entire t+pe s+stem consists of:Primitives: void, bool, loat, ushort, opaOue, FPrimitives: void, bool, loat, ushort, opaOue, F

    erived: pointer, arra&, stru!ture, un!tionerived: pointer, arra&, stru!ture, un!tion

    /o hi%h5level t&pes: t&pe5s&stem is lan%ua%e neutralM/o hi%h5level t&pes: t&pe5s&stem is lan%ua%e neutralM

    T+pe s+stem allows arbitrar+ casts:T+pe s+stem allows arbitrar+ casts:Allo$s epressin% $eakl&5t&ped lan%ua%es, like CAllo$s epressin% $eakl&5t&ped lan%ua%es, like C

    Front-ends canFront-ends can implementimplementsafe languagessafe languagesAlso easy to define a type-safe subset of LLVMAlso easy to define a type-safe subset of LLVM

    'ee also:'ee also:"o'*n#-$f.html"o'*n#-$f.html

  • 7/25/2019 llvm tutorial

    18/61

    Chris Lattner

    Lo$erin% sour!e5level t&pes to LLV(Lo$erin% sour!e5level t&pes to LLV(

    'ource language t+pes are lowered:'ource language t+pes are lowered:i!h t&pe s&stems epanded to simple t&pe s&stemi!h t&pe s&stems epanded to simple t&pe s&stem

    1mpli!it I abstra!t t&pes are made epli!it I !on!rete1mpli!it I abstra!t t&pes are made epli!it I !on!rete

    Examples of lowering:Examples of lowering:eeren!es turn into pointers:eeren!es turn into pointers: && Comple numbers:Comple numbers: 'ompl$/ flot'ompl$/ flot flot, flot flot, flot

    'itields:'itields: *tru't 2 int 3:; int 4:!; *tru't 2 int 3:; int 4:!; int int

    1nheritan!e:1nheritan!e: 'l** : S int 2; 'l** : S int 2; S, int S, int

    (ethods:(ethods: 'l** oi" foo(); 'l** oi" foo(); oi" foo()oi" foo()

    'ame idea as lowering to machine code'ame idea as lowering to machine code

  • 7/25/2019 llvm tutorial

    19/61

    Chris Lattner

    LLV( Pro%ram Stru!tureLLV( Pro%ram Stru!ture

    Module contains Functions#@lobalVariablesModule contains Functions#@lobalVariables(odule is unit o !ompilation#anal&sis#optimiation(odule is unit o !ompilation#anal&sis#optimiation

    Function contains AasicAlocs#!rgumentsFunction contains AasicAlocs#!rguments

    3un!tions rou%hl& !orrespond to un!tions in C3un!tions rou%hl& !orrespond to un!tions in C AasicAloc contains list of instructionsAasicAloc contains list of instructions*a!h blo!k ends in a !ontrol lo$ instru!tion*a!h blo!k ends in a !ontrol lo$ instru!tion

    Instruction is opcode $ &ector of operandsInstruction is opcode $ &ector of operandsAll operands have t&pesAll operands have t&pes

    1nstru!tion result is t&ped1nstru!tion result is t&ped

  • 7/25/2019 llvm tutorial

    20/61

    Chris Lattner

    >ur eample, !ompiled to LLV(>ur eample, !ompiled to LLV(

    int callee,const int 5./ 0int callee,const int 5./ 0 return 5.$12return 5.$12 // load// load

    33

    int caller,/ 0int caller,/ 0

    int T2int T2 // on stack// on stack

    T 6 42T 6 42 // store// store return callee,-T/2return callee,-T/2

    33

    internal int Bcallee,int5 B./ 0internal int Bcallee,int5 B./ 0 Btmp1 6 load int5 B.Btmp1 6 load int5 B.

    BtmpD 6 add int Btmp1< 1BtmpD 6 add int Btmp1< 1

    ret int BtmpDret int BtmpD

    33

    int Bcaller,/ 0int Bcaller,/ 0 BT 6 alloca intBT 6 alloca int

    store int 4< int5 BTstore int 4< int5 BT

    Btmp 6 call int Bcallee,int5 BT/Btmp 6 call int Bcallee,int5 BT/

    ret int Btmpret int Btmp

    33

    Sta!k allo!ation is

    epli!it in LLV(

    All loads#stores are

    epli!it in the LLV(

    representation

    Linker JinternaliesK

    most un!tions in most

    !ases

  • 7/25/2019 llvm tutorial

    21/61

    Chris Lattner

    >ur eample, desired transormation>ur eample, desired transormation

    internal int Bcallee,int B.&al/ 0internal int Bcallee,int B.&al/ 0 BtmpD 6 add int B.&al< 1BtmpD 6 add int B.&al< 1

    ret int BtmpDret int BtmpD

    33

    int Bcaller,/ 0int Bcaller,/ 0

    BT 6 alloca intBT 6 alloca int store int 4< int5 BTstore int 4< int5 BT

    Btmp1 6 load int5 BTBtmp1 6 load int5 BT

    Btmp 6 call int Bcallee,Btmp1/Btmp 6 call int Bcallee,Btmp1/

    ret int Btmpret int Btmp

    33

    internal int Bcallee,int5 B./ 0internal int Bcallee,int5 B./ 0 Btmp1 6 load int5 B.Btmp1 6 load int5 B.

    BtmpD 6 add int Btmp1< 1BtmpD 6 add int Btmp1< 1

    ret int BtmpDret int BtmpD

    33

    int Bcaller,/ 0int Bcaller,/ 0 BT 6 alloca intBT 6 alloca int

    store int 4< int5 BTstore int 4< int5 BT

    Btmp 6 call int Bcallee,int5 BT/Btmp 6 call int Bcallee,int5 BT/

    ret int Btmpret int Btmp

    33

    Chan%e the protot&pe

    or the un!tion

    1nsert load instru!tions

    into all !allers

    pdate all !all sites o

    N!alleeH

    >ther transormation

    95mem2re% !leans up

    the rest

    int Bcaller,/ 0int Bcaller,/ 0

    Btmp 6 call int Bcallee,int 4/Btmp 6 call int Bcallee,int 4/

    ret int Btmpret int Btmp

    33

  • 7/25/2019 llvm tutorial

    22/61

    Chris Lattner

    Tutorial >vervie$Tutorial >vervie$

    Introduction to the running exampleIntroduction to the running example LLVM C#C$$ Compiler %&er&iewLLVM C#C$$ Compiler %&er&iew.i%h5level vie$ o an eample LLV( !ompiler.i%h5level vie$ o an eample LLV( !ompiler

    The LLVM Virtual Instruction 'etThe LLVM Virtual Instruction 'et 1 overvie$ and t&pe5s&stem1 overvie$ and t&pe5s&stem

    LLVM C$$ I( and important !)I*sLLVM C$$ I( and important !)I*s'asi!s, Pass(ana%er, datalo$, Ar%Promotion'asi!s, Pass(ana%er, datalo$, Ar%Promotion

    Important LLVM ToolsImportant LLVM Toolsopt, !ode %enerator, )1T, test suite, bu%pointopt, !ode %enerator, )1T, test suite, bu%point

    Example applications of LLVMExample applications of LLVM

  • 7/25/2019 llvm tutorial

    23/61

    Chris Lattner

    LLV( Codin% 'asi!sLLV( Codin% 'asi!s

    8ritten in modern C$$< uses the 'TL:8ritten in modern C$$< uses the 'TL:Parti!ularl& the ve!tor, set, and map !lassesParti!ularl& the ve!tor, set, and map !lasses

    LLVM I( is almost all doubl+"lined lists:LLVM I( is almost all doubl+"lined lists:

    (odule !ontains lists o 3un!tions I +lobalVariables(odule !ontains lists o 3un!tions I +lobalVariables3un!tion !ontains lists o 'asi!'lo!ks I Ar%uments3un!tion !ontains lists o 'asi!'lo!ks I Ar%uments

    'asi!'lo!k !ontains list o 1nstru!tions'asi!'lo!k !ontains list o 1nstru!tions

    Lined lists are tra&ersed with iterators:Lined lists are tra&ersed with iterators:5un'tion 6 = 75un'tion 6 = 7

    for (5un'tion::it$rtor 8 = 69$#in(); 8 = 69$n"(); ++8) for (5un'tion::it$rtor 8 = 69$#in(); 8 = 69$n"(); ++8)

    *i'lo' & = 8;*i'lo' & = 8;

    ......

    'ee also:'ee also:docs#)rogrammersManualhtmldocs#)rogrammersManualhtml

  • 7/25/2019 llvm tutorial

    24/61

    Chris Lattner

    LLV( Pass (ana%erLLV( Pass (ana%er

    Compiler is organi>ed as a series of 7passes*:Compiler is organi>ed as a series of 7passes*:*a!h pass is one anal&sis or transormation*a!h pass is one anal&sis or transormation

    Four t+pes of )ass:Four t+pes of )ass:

    (odulePass(odulePass

    : %eneral interpro!edural pass: %eneral interpro!edural pass

    Call+raphSCCPassCall+raphSCCPass: bottom5up on the !all %raph: bottom5up on the !all %raph

    3un!tionPass3un!tionPass: pro!ess a un!tion at a time: pro!ess a un!tion at a time

    'asi!'lo!kPass'asi!'lo!kPass: pro!ess a basi! blo!k at a time: pro!ess a basi! blo!k at a time

    Constraints imposed ,eg Function)ass/:Constraints imposed ,eg Function)ass/:3un!tionPass !an onl& look at J!urrent un!tionK3un!tionPass !an onl& look at J!urrent un!tionK

    Cannot maintain state a!ross un!tionsCannot maintain state a!ross un!tions

    'ee also:'ee also:"o'*>ritin#An?6P**.html"o'*>ritin#An?6P**.html

  • 7/25/2019 llvm tutorial

    25/61

    Chris Lattner

    Servi!es provided b& Pass(ana%erServi!es provided b& Pass(ana%er

    %ptimi>ation of pass execution:%ptimi>ation of pass execution:Pro!ess a un!tion at a time instead o a pass at a timePro!ess a un!tion at a time instead o a pass at a time

    *ample: 1 3, +, . are three un!tions in input p%m:*ample: 1 3, +, . are three un!tions in input p%m:

    J3333++++....K not J3+.3+.3+.3+.KJ3333++++....K not J3+.3+.3+.3+.K

    Pro!ess un!tions in parallel on an S(P 9uture $orkPro!ess un!tions in parallel on an S(P 9uture $ork

    =eclarati&e dependenc+ management:=eclarati&e dependenc+ management:Automati!all& ulill and mana%e anal&sis pass lietimesAutomati!all& ulill and mana%e anal&sis pass lietimes

    Share anal&ses bet$een passes $hen sae:Share anal&ses bet$een passes $hen sae: e"%" JominatorSet live unless pass modiies C3+Ke"%" JominatorSet live unless pass modiies C3+K

    !&oid boilerplate for tra&ersal of program!&oid boilerplate for tra&ersal of program

    'ee also:'ee also:"o'*>ritin#An?6P**.html"o'*>ritin#An?6P**.html

  • 7/25/2019 llvm tutorial

    26/61

    Chris Lattner

    Pass (ana%er D Ar% Promotion ?;#2Pass (ana%er D Ar% Promotion ?;#2

    !rg )romotion is a Call@raph'CC)ass:!rg )romotion is a Call@raph'CC)ass:/aturall& operates bottom5up on the Call+raph/aturall& operates bottom5up on the Call+raph

    'ubble pointers rom !allees out to !allers'ubble pointers rom !allees out to !allers

    !: @in'lu"$ llmBllCrphSBBP**.h

    D:*tru'tSimpl$Ar#Promotion : puli' BllCrphSBBP**

    !rg )romotion re9uires !lias!nal+sis info!rg )romotion re9uires !lias!nal+sis infoTo prove saet& o transormationTo prove saet& o transormation

    -orks $ith an& alias anal&sis al%orithm thou%h-orks $ith an& alias anal&sis al%orithm thou%hE:E: irtul oi" #$tAnlF*i*G*#$(AnlF*i*G*#$ &AG) 'on*t irtul oi" #$tAnlF*i*G*#$(AnlF*i*G*#$ &AG) 'on*t

    AG.""-$Huir$"

  • 7/25/2019 llvm tutorial

    27/61

    Chris Lattner

    Finall+< implementFinall+< implement runJnSBBrunJnSBB,line G/:,line G/:ool Simpl$Ar#Promotion::ool Simpl$Ar#Promotion::runJnSBB('on*t *t"::$'tor

  • 7/25/2019 llvm tutorial

    28/61

    Chris Lattner

    LLV( atalo$ Anal&sisLLV( atalo$ Anal&sis

    LLVM I( is in ''! form:LLVM I( is in ''! form:use5de and de5use !hains are al$a&s availableuse5de and de5use !hains are al$a&s available

    All obBe!ts have user#use ino, even un!tionsAll obBe!ts have user#use ino, even un!tions

    Control Flow @raph is alwa+s a&ailable:Control Flow @raph is alwa+s a&ailable:*posed as 'asi!'lo!k prede!essor#su!!essor lists*posed as 'asi!'lo!k prede!essor#su!!essor lists

    (an& %eneri! %raph al%orithms usable $ith the C3+(an& %eneri! %raph al%orithms usable $ith the C3+

    ?igher"le&el info implemented as passes:?igher"le&el info implemented as passes:ominators, Call+raph, indu!tion vars, aliasin%, +V/, Fominators, Call+raph, indu!tion vars, aliasin%, +V/, F

    'ee also:'ee also:"o'*Pro#rmm$r*6nul.html"o'*Pro#rmm$r*6nul.html

  • 7/25/2019 llvm tutorial

    29/61

    Chris Lattner

    Ar% Promotion: saet& !he!k ?;#4Ar% Promotion: saet& !he!k ?;#4

    1: Function must be JinternalK ,aa JstaticK/1: Function must be JinternalK ,aa JstaticK/ EE:EE:if (5 LL 59h*8nt$rnlin#$()) r$turn fl*$;if (5 LL 59h*8nt$rnlin#$()) r$turn fl*$;

    D: Mae sure address of F is not taenD: Mae sure address of F is not taen

    1n LLV(, !he!k that there are onl& dire!t !alls usin% 31n LLV(, !he!k that there are onl& dire!t !alls usin% 3

    ::for (?lu$::u*$Oit$rtor G8 = 59u*$O$#in();for (?lu$::u*$Oit$rtor G8 = 59u*$O$#in(); G8 = 59u*$O$n"(); ++G8) G8 = 59u*$O$n"(); ++G8) BllSit$ BS = BllSit$::#$t(G8);BllSit$ BS = BllSit$::#$t(G8); if (BS.#$t8n*tru'tion()) if (BS.#$t8n*tru'tion()) "#aking the address" of $."#aking the address" of $. r$turn fl*$;r$turn fl*$;

    : Chec to see if an+ args are promotable:: Chec to see if an+ args are promotable: 11:11:for (un*i#n$" i = 0; i = Point$rAr#*.*iK$(); ++i)for (un*i#n$" i = 0; i = Point$rAr#*.*iK$(); ++i) if (i*Sf$oPromot$Ar#um$nt(Point$rAr#*[i]))if (i*Sf$oPromot$Ar#um$nt(Point$rAr#*[i])) Point$rAr#*.$r*$(Point$rAr#*.$#in()+i);Point$rAr#*.$r*$(Point$rAr#*.$#in()+i); if (Point$rAr#*.$mptF()) r$turn fl*$; if (Point$rAr#*.$mptF()) r$turn fl*$; no args promotableno args promotable

  • 7/25/2019 llvm tutorial

    30/61

    Chris Lattner

    Ar% Promotion: saet& !he!k ?2#4Ar% Promotion: saet& !he!k ?2#4

    4: !rgument pointer can onl+ be loaded from:4: !rgument pointer can onl+ be loaded from:/o stores throu%h ar%ument pointer allo$edM/o stores throu%h ar%ument pointer allo$edM

    %oop o&er all uses of the argument 'use(def chains).%oop o&er all uses of the argument 'use(def chains).

    1E:1E:for (?lu$::u*$Oit$rtor G8 = Ar#9u*$O$#in();for (?lu$::u*$Oit$rtor G8 = Ar#9u*$O$#in();

    G8 = Ar#9u*$O$n"(); ++G8) G8 = Ar#9u*$O$n"(); ++G8)

    If the user is a load*If the user is a load*

    if (o"8n*t 8 = "FnO'*t

  • 7/25/2019 llvm tutorial

    31/61

    Chris Lattner

    G: Value of J5)K must not change in the AAG: Value of J5)K must not change in the AA-e move load out to the !aller, value !annot !han%eM-e move load out to the !aller, value !annot !han%eM

    Get AliasAnalysis implementation from the pass manager.Get AliasAnalysis implementation from the pass manager.

    1QR:1QR:Ali*AnlF*i* &AA = #$tAnlF*i*

  • 7/25/2019 llvm tutorial

    32/61

    Chris Lattner

    : J5)K cannot change from Fn entr+ to AA: J5)K cannot change from Fn entr+ to AA

    1DQ:1DQ:for (pr$"Oit$rtor P8 = pr$"O$#in(), = pr$"O$n"();for (pr$"Oit$rtor P8 = pr$"O$#in(), = pr$"O$n"();

    P8 = ; ++P8) P8 = ; ++P8) %oop o&er predecessors of .%oop o&er predecessors of .

    Check each block from to entry '+$ search on in&erse graph).Check each block from to entry '+$ search on in&erse graph).

    for (i"fOit$rtor

  • 7/25/2019 llvm tutorial

    33/61

    Chris Lattner

    Ar% Promotion: orm outline ?;#4Ar% Promotion: orm outline ?;#4

    1: Mae protot+pe with new arg t+pes: 11: Mae protot+pe with new arg t+pes: 1'asi!all& Bust repla!es NintH $ith NintH in protot&pe'asi!all& Bust repla!es NintH $ith NintH in protot&pe

    D: Create function with new protot+pe:D: Create function with new protot+pe:

    !1:!1:5un'tion N5 = n$M 5un'tion(N5F, 59#$tin#$(),5un'tion N5 = n$M 5un'tion(N5F, 59#$tin#$(), 59#$tNm$());59#$tNm$());

    59#$tPr$nt()9#$t5un'tioni*t().in*$rt(5, N5);59#$tPr$nt()9#$t5un'tioni*t().in*$rt(5, N5);

    : Change all callers of F to call NF:: Change all callers of F to call NF:

    If there are uses of $2 then calls to it remain.If there are uses of $2 then calls to it remain.

    !!1:!!1:Mhil$ (59u*$O$mptF()) Mhil$ (59u*$O$mptF())

    Get a caller of F.Get a caller of F.

    BllSit$ BS = BllSit$::#$t(59u*$O'());BllSit$ BS = BllSit$::#$t(59u*$O'());

  • 7/25/2019 llvm tutorial

    34/61

    Chris Lattner

    Ar% Promotion: orm outline ?2#4Ar% Promotion: orm outline ?2#4

    4: For each caller< add loads< determine args4: For each caller< add loads< determine argsLoop over the ar%s, insertin% the loads in the !allerLoop over the ar%s, insertin% the loads in the !aller

    !!0:!!0:*t"::$'tor

  • 7/25/2019 llvm tutorial

    35/61

    Chris Lattner

    Ar% Promotion: orm outline ?7#4Ar% Promotion: orm outline ?7#4

    G: (eplace the call site of F with call of NFG: (eplace the call site of F with call of NF Create the call to -$ with the adjusted arguments.Create the call to -$ with the adjusted arguments.!!:!!:8n*tru'tion N$M = n$M Bll8n*t(N5, Ar#*, , Bll);8n*tru'tion N$M = n$M Bll8n*t(N5, Ar#*, , Bll);

    If the return &alue of the old call was used2 use the ret&al of the new call.If the return &alue of the old call was used2 use the ret&al of the new call.

    if (Bll9u*$O$mptF())if (Bll9u*$O$mptF()) Bll9r$pl'$AllG*$*>ith(N$M);Bll9r$pl'$AllG*$*>ith(N$M);

    $inally2 remo&e the old call from the program2 reducing the use(count of $.$inally2 remo&e the old call from the program2 reducing the use(count of $. Bll9#$tPr$nt()9#$t8n*ti*t().$r*$(Bll);Bll9#$tPr$nt()9#$t8n*ti*t().$r*$(Bll);

    : Mo&e code from old function to new Fn: Mo&e code from old function to new Fn

    !Q:!Q:N59#$t*i'lo'i*t().*pli'$(N59$#in(),N59#$t*i'lo'i*t().*pli'$(N59$#in(), 59#$t*i'lo'i*t());59#$t*i'lo'i*t());

  • 7/25/2019 llvm tutorial

    36/61

    Chris Lattner

    Ar% Promotion: orm outline ?4#4Ar% Promotion: orm outline ?4#4

    : Change users of F*s arguments to use NF*s: Change users of F*s arguments to use NF*s

    !R:!R: for (5un'tion::it$rtor 8 = 59$#in(), 8! = N59$#in();for (5un'tion::it$rtor 8 = 59$#in(), 8! = N59$#in();

    8 = 59$n"(); ++8, ++8!)8 = 59$n"(); ++8, ++8!)

    if (Ar#*oPromot$.'ount(8)) if (Ar#*oPromot$.'ount(8)) -ot promoting this arg1-ot promoting this arg1

    89r$pl'$AllG*$*>ith(8!); 89r$pl'$AllG*$*>ith(8!); 3se new arg2 not old arg.3se new arg2 not old arg.

    $l*$ $l*$

    Mhil$ (89u*$O$mptF()) Mhil$ (89u*$O$mptF()) 4nly users can be loads.4nly users can be loads.

    o"8n*t 8 = '*tith(8!);

    89#$tPr$nt()9#$t8n*ti*t().$r*$(8);89#$tPr$nt()9#$t8n*ti*t().$r*$(8);

    O: =elete old function:O: =elete old function:

    !ER:!ER:59#$tPr$nt()9#$t5un'tioni*t().$r*$(5);59#$tPr$nt()9#$t5un'tioni*t().$r*$(5);

  • 7/25/2019 llvm tutorial

    37/61

    Chris Lattner

    Tutorial >vervie$Tutorial >vervie$

    Introduction to the running exampleIntroduction to the running example LLVM C#C$$ Compiler %&er&iewLLVM C#C$$ Compiler %&er&iew.i%h5level vie$ o an eample LLV( !ompiler.i%h5level vie$ o an eample LLV( !ompiler

    The LLVM Virtual Instruction 'etThe LLVM Virtual Instruction 'et 1 overvie$ and t&pe5s&stem1 overvie$ and t&pe5s&stem

    LLVM C$$ I( and important !)I*sLLVM C$$ I( and important !)I*s'asi!s, Pass(ana%er, datalo$, Ar%Promotion'asi!s, Pass(ana%er, datalo$, Ar%Promotion

    Important LLVM ToolsImportant LLVM Toolsopt, !ode %enerator, )1T, test suite, bu%pointopt, !ode %enerator, )1T, test suite, bu%point

    Example applications of LLVMExample applications of LLVM

  • 7/25/2019 llvm tutorial

    38/61

    Chris Lattner

    LLV( tools: t$o lavorsLLV( tools: t$o lavors

    JJ)rimiti&eK tools: do a single ob)rimiti&eK tools: do a single ob llvm5as: Convert rom "ll 9tet to "b! 9binar&llvm5as: Convert rom "ll 9tet to "b! 9binar&

    llvm5dis: Convert rom "b! 9binar& to "ll 9tetllvm5dis: Convert rom "b! 9binar& to "ll 9tet

    llvm5link: Link multiple "b! iles to%etherllvm5link: Link multiple "b! iles to%ether

    llvm5pro: Print proile output to human readersllvm5pro: Print proile output to human readers

    llvm!: Coni%urable !ompiler driverllvm!: Coni%urable !ompiler driver

    !ggregate tools: pull in multiple features!ggregate tools: pull in multiple features

    %!!as#%!!ld: Compile#link5time optimiers or C#CDD 3*%!!as#%!!ld: Compile#link5time optimiers or C#CDD 3*bu%point: automati! !ompiler debu%%erbu%point: automati! !ompiler debu%%er

    llvm5%!!#llvm5%DD: C#CDD !ompilersllvm5%!!#llvm5%DD: C#CDD !ompilers

    'ee also:'ee also:"o'*Bommn"Cui"$"o'*Bommn"Cui"$

  • 7/25/2019 llvm tutorial

    39/61

    Chris Lattner

    opt tool: LLV( modular optimieropt tool: LLV( modular optimier

    In&oe arbitrar+ se9uence of passes:In&oe arbitrar+ se9uence of passes:Completel& !ontrol Pass(ana%er rom !ommand lineCompletel& !ontrol Pass(ana%er rom !ommand line

    Supports loadin% passes as plu%ins rom "so ilesSupports loadin% passes as plu%ins rom "so iles

    opt "load fooso "pass1 "passD "pass xbc "o +bcopt "load fooso "pass1 "passD "pass xbc "o +bc

    )asses JregisterK themsel&es:)asses JregisterK themsel&es:R1:R1: -$#i*t$rJpt

  • 7/25/2019 llvm tutorial

    40/61

    Chris Lattner

    unnin% Ar% Promotion $ith optunnin% Ar% Promotion $ith opt

    Aasic execution with 7opt*:Aasic execution with 7opt*: opt 9*impl$r#promotion in.' 9o out.'opt 9*impl$r#promotion in.' 9o out.'

    Load "b! ile, run pass, $rite out resultsLoad "b! ile, run pass, $rite out results

    se J5load ilename"soK i !ompiled into a librar&se J5load ilename"soK i !ompiled into a librar&

    Pass(ana%er resolves all dependen!iesPass(ana%er resolves all dependen!ies

    %ptionall+ choose an alias anal+sis to use:%ptionall+ choose an alias anal+sis to use: opt U*i' U*impl$r#promotionopt U*i' U*impl$r#promotion ,default/ ,default/

    Alternativel&,Alternativel&, U*t$$n*9, Un"$r*9, U"*9, 7U*t$$n*9, Un"$r*9, U"*9, 7

    %ther useful options a&ailable:%ther useful options a&ailable: 9*tt*9*tt*: )rint statistics collected from the passes: )rint statistics collected from the passes

    9tim$9p**$*9tim$9p**$*: Time each pass being run< print output: Time each pass being run< print output

  • 7/25/2019 llvm tutorial

    41/61

    Chris Lattner

    *ample 5stats output 9%!!as ;

  • 7/25/2019 llvm tutorial

    42/61

    Chris Lattner

    *ample 5time5passes 9%!!as ;

  • 7/25/2019 llvm tutorial

    43/61

    Chris Lattner

    Anal&e tool: Visualie anal&sis resultsAnal&e tool: Visualie anal&sis results

    )rint most LLVM data structures)rint most LLVM data structuresominators, loops, alias sets, C3+, !all %raph, Fominators, loops, alias sets, C3+, !all %raph, F

    Converts most LLV( data stru!tures to NdotH %raphsConverts most LLV( data stru!tures to NdotH %raphs

  • 7/25/2019 llvm tutorial

    44/61

    Chris Lattner

    LLC Tool: Stati! !ode %eneratorLLC Tool: Stati! !ode %enerator

    Compiles LLVMCompiles LLVM nati&e assembl+ languagenati&e assembl+ languageCurrentl& or E@8, Spar!, Po$erPC 9others in alphaCurrentl& or E@8, Spar!, Po$erPC 9others in alpha ll' fil$.' 9o fil$.* 9mr'h=/ERll' fil$.' 9o fil$.* 9mr'h=/ER

    * fil$.* Uo fil$.o* fil$.* Uo fil$.o

    Compiles LLVMCompiles LLVM portable C codeportable C code ll' fil$.' 9o fil$.' 9mr'h='ll' fil$.' 9o fil$.' 9mr'h='

    #'' U' fil$.' Uo fil$.o#'' U' fil$.' Uo fil$.o

    Targets are modular - d+namicall+ loadable:Targets are modular - d+namicall+ loadable: ll' Ulo" lirm.*o fil$.' 9mr'h=rmll' Ulo" lirm.*o fil$.' 9mr'h=rm

  • 7/25/2019 llvm tutorial

    45/61

    Chris Lattner

    The LLV( Code +eneratorThe LLV( Code +enerator

    Target independent:Target independent:riven b& an al%orithm independent tar%et des!riptionriven b& an al%orithm independent tar%et des!ription

    ata la&out, e%ister, 1nstru!tion, S!hedulin%, Fata la&out, e%ister, 1nstru!tion, S!hedulin%, F

    Aasic code generator la+out:Aasic code generator la+out:

    !ll passes are replaceable!ll passes are replaceable

    e"%" Trivial to !han%e and add re%ister allo!atorse"%" Trivial to !han%e and add re%ister allo!ators

    Targets can add custom passesTargets can add custom passese"%" E@8 has spe!ial support or 3P sta!ke"%" E@8 has spe!ial support or 3P sta!k

    'ee also:'ee also:docs#Code@eneratorhtmldocs#Code@eneratorhtml

    1nstru!tion

    Sele!tionLLV( "s ile

    (a!hine

    SSA >pts

    e%ister

    Allo!ator

    1nstr

    S!hed

    Code

    *mission

    *poses all tar%et5spe!ii!

    details about a un!tion

    9!allin% !onventions, et!

    S!hedulin%, Peephole, G

    Tar%et 1ndependentTar%et Spe!ii!

    9%enerated

    Tar%et Spe!ii! 9b&

    hand or no$ 4 al%orithmsavailable toda&

    ll! 5re%allo!oo

  • 7/25/2019 llvm tutorial

    46/61

    Chris Lattner

    Portin% LLV( to a ne$ tar%etPortin% LLV( to a ne$ tar%et

    LLVM targets are &er+ eas+ to write:LLVM targets are &er+ eas+ to write:Ane!dotal eviden!e su%%ests ; $eek or a basi! portAne!dotal eviden!e su%%ests ; $eek or a basi! port

    FF or someone amiliar $ith the tar%et ma!hine andor someone amiliar $ith the tar%et ma!hine and

    !ompilers in %eneral, but not $ith LLV(!ompilers in %eneral, but not $ith LLV(

    LLVM targets are written with JtablegenK toolLLVM targets are written with JtablegenK toolSimple de!larative s&ntaSimple de!larative s&nta

    esi%ned to a!tor out redundan!& in tar%et des!esi%ned to a!tor out redundan!& in tar%et des!

    'ome C$$ code is still re9uired'ome C$$ code is still re9uiredPrimaril& in the instru!tion sele!torPrimaril& in the instru!tion sele!tor

    Continuin% $ork to improve thisContinuin% $ork to improve this

    'ee also:'ee also:docs#Table@enFundamentalshtmldocs#Table@enFundamentalshtmlandand 8riting!nLLVMAacendhtml8riting!nLLVMAacendhtml

  • 7/25/2019 llvm tutorial

    47/61

    Chris Lattner

    LLI allows direct execution of bc filesLLI allows direct execution of bc filesEg:Eg: lli #r$p.' 9i foo .'lli #r$p.' 9i foo .'

    LLI uses a Rust"In"Time compiler if a&ailable:LLI uses a Rust"In"Time compiler if a&ailable:ses same !ode %enerator as LLCses same !ode %enerator as LLC

    >ptionall& uses aster !omponents than LLC>ptionall& uses aster !omponents than LLC

    *mits ma!hine !ode to memor& instead o J"sK ile*mits ma!hine !ode to memor& instead o J"sK ile

    )1T is a librar& that !an be embedded in other tools)1T is a librar& that !an be embedded in other tools

    %therwise< it uses the LLVM interpreter:%therwise< it uses the LLVM interpreter: 1nterpreter is etremel& simple and ver& slo$1nterpreter is etremel& simple and ver& slo$

    1nterpreter is portable thou%hM1nterpreter is portable thou%hM

    LL1 Tool: LLV( *e!ution *n%ineLL1 Tool: LLV( *e!ution *n%ine

  • 7/25/2019 llvm tutorial

    48/61

    Chris Lattner

    C and CDD Pro%ram Test SuiteC and CDD Pro%ram Test Suite

    Large collection of programs and benchmars:Large collection of programs and benchmars:Standard suites 9e"%" SP*C 6=#2000, >lden, Ptrdist,Standard suites 9e"%" SP*C 6=#2000, >lden, Ptrdist,

    (!Cat, Stanord, 3reeben!h, ShootoutF(!Cat, Stanord, 3reeben!h, ShootoutF

    1ndividual pro%rams: s%ea, siod, sim, pi, povra&, F1ndividual pro%rams: s%ea, siod, sim, pi, povra&, F

    Proprietar& suites 9e"%" SP*C reOuire suite sour!eProprietar& suites 9e"%" SP*C reOuire suite sour!e

    Consistent build en&ironment:Consistent build en&ironment:*as& add hooks to build or proilin%#instrumentation*as& add hooks to build or proilin%#instrumentation

    *as& to %et perorman!e numbers rom entire test suite*as& to %et perorman!e numbers rom entire test suite Entire test suite is checed e&er+ night:Entire test suite is checed e&er+ night:.osted on Linu,Solaris,3ree'S on E@8,Spar! I PPC.osted on Linu,Solaris,3ree'S on E@8,Spar! I PPC

    'ee also:'ee also:"o'*$*tin#Cui"$.html"o'*$*tin#Cui"$.html

  • 7/25/2019 llvm tutorial

    49/61

    Chris Lattner

    1nte%rated ebu%%in% Tools1nte%rated ebu%%in% Tools

    Extensi&e assertions throughout codeExtensi&e assertions throughout code3ind problems as earl& as possible 9!lose to sour!e3ind problems as earl& as possible 9!lose to sour!e

    LLVM I( Verifier: Checs modules for &alidit+LLVM I( Verifier: Checs modules for &alidit+Che!ks t&pe properties, dominan!e properties, et!"Che!ks t&pe properties, dominan!e properties, et!"

    Automati!all& run b& optAutomati!all& run b& opt

    Problem oundG: print an error messa%e and abortProblem oundG: print an error messa%e and abort

    LLVM I( Lea =etectorLLVM I( Lea =etector

    *i!ient and simple J%arba%e !olle!torK or 1 obBe!ts*i!ient and simple J%arba%e !olle!torK or 1 obBe!ts*nsure 1 obBe!ts are deallo!ated appropriatel&*nsure 1 obBe!ts are deallo!ated appropriatel&

  • 7/25/2019 llvm tutorial

    50/61

    Chris Lattner

    The 'u%point automated bu% inderThe 'u%point automated bu% inder

    'imple idea: automate 7binar+* search for bug'imple idea: automate 7binar+* search for bug'u% isolation: $hi!h passes intera!t to produ!e bu%'u% isolation: $hi!h passes intera!t to produ!e bu%

    Test case reductionTest case reduction: redu!e input pro%ram: redu!e input pro%ram

    %ptimi>er#Codegen crashes:%ptimi>er#Codegen crashes:Thro$ portion o test !ase a$a&, !he!k or !rashThro$ portion o test !ase a$a&, !he!k or !rash

    1 so, keep %oin%1 so, keep %oin%

    >ther$ise, revert and tr& somethin% else>ther$ise, revert and tr& somethin% else

    *tremel& ee!tive in pra!ti!e*tremel& ee!tive in pra!ti!e 'imple greed+ algorithms for test reduction'imple greed+ algorithms for test reduction

    Completel+ blac"box approachCompletel+ blac"box approach

    'ee also:'ee also:"o'*u#point.html"o'*u#point.html

  • 7/25/2019 llvm tutorial

    51/61

    Chris Lattner

    ebu%%in% (is!ompilationsebu%%in% (is!ompilations

    %ptimi>er miscompilation:%ptimi>er miscompilation:Split test!ase in t$o, optimie one" Still brokenGSplit test!ase in t$o, optimie one" Still brokenG

    Qeep shrinkin% the portion bein% optimiedQeep shrinkin% the portion bein% optimied

    Codegen miscompilation:Codegen miscompilation:Split test!ase in t$o, !ompile one $ith C'*, brokenGSplit test!ase in t$o, !ompile one $ith C'*, brokenG

    Shrink portion bein% !ompiled $ith non C'* !ode%enShrink portion bein% !ompiled $ith non C'* !ode%en

    Code splitting granularities:Code splitting granularities:

    Take out $hole un!tionsTake out $hole un!tionsTake out loop nestsTake out loop nests

    Take out individual basi! blo!ksTake out individual basi! blo!ks

  • 7/25/2019 llvm tutorial

    52/61

    Chris Lattner

    .o$ $ell does this thin% $orkG.o$ $ell does this thin% $orkG

    Extremel+ effecti&e:Extremel+ effecti&e:Can oten redu!e a ;00Q L>C pro%ram and 80 passesCan oten redu!e a ;00Q L>C pro%ram and 80 passes

    to a e$ basi! blo!ks and ; pass in = minutesto a e$ basi! blo!ks and ; pass in = minutes

    Crashes are ound mu!h aster than mis!ompilationsCrashes are ound mu!h aster than mis!ompilations

    no need to run the pro%ram to test a redu!tionno need to run the pro%ram to test a redu!tion Interacts with integrated debugging toolsInteracts with integrated debugging toolsuntime errors are dete!ted asteruntime errors are dete!ted aster

    Limitations:Limitations:Pro%ram must be deterministi!Pro%ram must be deterministi!

    FF or modiied to be soor modiied to be so

    3inds JaK bu%, not JtheK bu%3inds JaK bu%, not JtheK bu%

  • 7/25/2019 llvm tutorial

    53/61

    Chris Lattner

    Tutorial >vervie$Tutorial >vervie$

    Introduction to the running exampleIntroduction to the running example LLVM C#C$$ Compiler %&er&iewLLVM C#C$$ Compiler %&er&iew.i%h5level vie$ o an eample LLV( !ompiler.i%h5level vie$ o an eample LLV( !ompiler

    The LLVM Virtual Instruction 'etThe LLVM Virtual Instruction 'et 1 overvie$ and t&pe5s&stem1 overvie$ and t&pe5s&stem

    LLVM C$$ I( and important !)I*sLLVM C$$ I( and important !)I*s'asi!s, Pass(ana%er, datalo$, Ar%Promotion'asi!s, Pass(ana%er, datalo$, Ar%Promotion

    Important LLVM ToolsImportant LLVM Toolsopt, !ode %enerator, )1T, test suite, bu%pointopt, !ode %enerator, )1T, test suite, bu%point

    Example applications of LLVMExample applications of LLVM

  • 7/25/2019 llvm tutorial

    54/61

    Chris Lattner

    se Case ;: *d%e or Path Proilin%se Case ;: *d%e or Path Proilin%

    5oal*5oal* Profiling Research or PGOProfiling Research or PGO Implementation:Implementation:FunctionPassFunctionPass: LLV(5to5LLV( transormation: LLV(5to5LLV( transormation

    1nstrumentation: se C3+, intervals, dominators1nstrumentation: se C3+, intervals, dominators

    Code %eneration: se C or an& native ba!k endCode %eneration: se C or an& native ba!k end

    Proile eedba!k: se proile Ouer& intera!eProile eedba!k: se proile Ouer& intera!e

    Core extensions needed:Core extensions needed: nonenone

    Maor LLVM AenefitsMaor LLVM AenefitsLan%ua%e5independen!e, C3+, ver& simple 1Lan%ua%e5independen!e, C3+, ver& simple 1

  • 7/25/2019 llvm tutorial

    55/61

    Chris Lattner

    se Case 2: Alias Anal&sisse Case 2: Alias Anal&sis

    5oal*5oal* Research on ne alias analysis algorithmsResearch on ne alias analysis algorithms Implementation:Implementation:ModulePassModulePass: -hole5pro%ram anal&sis pass on LLV(: -hole5pro%ram anal&sis pass on LLV(

    se t&pe inormationR SSAR heap#sta!k#%lobalsse t&pe inormationR SSAR heap#sta!k#%lobals

    CompareCompare !impleAA!impleAA,, !teensgard"s!teensgard"s,,Andersen"sAndersen"s,, #!A#!A

    *valuate man& !lients via*valuate man& !lients viaAliasAnalysisAliasAnalysis intera!eintera!e

    Core extensions needed:Core extensions needed: nonenone

    Maor LLVM AenefitsMaor LLVM AenefitsLan%ua%e5independen!e, t&pe ino, SSA, SA, 1P>Lan%ua%e5independen!e, t&pe ino, SSA, SA, 1P>

    AliasAnalysisAliasAnalysis intera!e $ith man& pre5eistin% !lientsintera!e $ith man& pre5eistin% !lients

  • 7/25/2019 llvm tutorial

    56/61

    Chris Lattner

    se Case 7: LS Preet!hin%se Case 7: LS Preet!hin%

    5oal*5oal* Prefetching lin$ed data structuresPrefetching lin$ed data structures Implementation:Implementation:ModulePassModulePass: Link5time LLV(5to5LLV( transormation: Link5time LLV(5to5LLV( transormation

    %ode transformations%ode transformations: use t&pe ino, loop anal&sis,: use t&pe ino, loop anal&sis,

    unrollin%, preet!h insertionunrollin%, preet!h insertion#ata transformations#ata transformations 9e"%," addin% histor& pointers:9e"%," addin% histor& pointers:

    useuse strongstrongt&pe ino rom SA, 1P>t&pe ino rom SA, 1P>

    Core extensions needed:Core extensions needed:

    Preet!h operation: add asPreet!h operation: add as intrinsi!intrinsi!9in pro%ress9in pro%ress Maor LLVM AenefitsMaor LLVM Aenefits

    Lan%ua%e5independen!e, t&pe ino, SA, 1P>Lan%ua%e5independen!e, t&pe ino, SA, 1P>

  • 7/25/2019 llvm tutorial

    57/61

    Chris Lattner

    se Case 4: Lan%ua%e 3ront endse Case 4: Lan%ua%e 3ront end

    5oal*5oal* &se LLVM to implement a ne language&se LLVM to implement a ne language Implementation:Implementation:Parser 9sa& to AST, Semanti! !he!kin%Parser 9sa& to AST, Semanti! !he!kin%

    AST5to5LLV( translatorAST5to5LLV( translator

    Core extensions needed:Core extensions needed: dependsdepends.i%h5level t&pe s&stem is.i%h5level t&pe s&stem is omitted by designomitted by design

    Maor LLVM AenefitsMaor LLVM AenefitsLo$5level, but po$erul t&pe s&stemLo$5level, but po$erul t&pe s&stem

    Ver& simple 1 to %enerate 9e"%", !ompare +CC TLVer& simple 1 to %enerate 9e"%", !ompare +CC TL

    *tensive %lobal and 1P optimiation rame$ork*tensive %lobal and 1P optimiation rame$ork

    )1T en%ine, native ba!k5ends, C ba!k5end)1T en%ine, native ba!k5ends, C ba!k5end

  • 7/25/2019 llvm tutorial

    58/61

    Chris Lattner

    se Case =: )1T Compilerse Case =: )1T Compiler

    5oal*5oal* 'rite ()T compiler for a bytecode language'rite ()T compiler for a bytecode language Implementation:Implementation:*tend the LLV( )1T rame$ork*tend the LLV( )1T rame$ork

    !imple ()T!imple ()T: 3ast translation rom b&te!ode to LLV(: 3ast translation rom b&te!ode to LLV(

    9then use LLV( )1T D +C9then use LLV( )1T D +COptimi*ing ()TOptimi*ing ()T: Lan%ua%e5spe!ii! optimiations D ast: Lan%ua%e5spe!ii! optimiations D ast

    translation 9then use LLV( optimiations, )1T, +Ctranslation 9then use LLV( optimiations, )1T, +C

    Core extensions needed:Core extensions needed: none in generalnone in general

    Maor LLVM AenefitsMaor LLVM AenefitsCompa!t, t&ped, lan%ua%e5independent 1Compa!t, t&ped, lan%ua%e5independent 1

    *istin% )1T rame$ork and +C*istin% )1T rame$ork and +C

  • 7/25/2019 llvm tutorial

    59/61

    Chris Lattner

    se Case 8: Ar!hite!ture esear!hse Case 8: Ar!hite!ture esear!h

    5oal*5oal* %ompiler support for ne architectures%ompiler support for ne architectures Implementation:Implementation:Add ne$ ma!hine des!ription 9or modi& oneAdd ne$ ma!hine des!ription 9or modi& one

    Add an& ne$ LLV(5to5LLV( transormationsAdd an& ne$ LLV(5to5LLV( transormations

    Core extensions needed:Core extensions needed: depends on goalsdepends on goals 1mminent eatures:1mminent eatures:modulo sched6 &ector opsmodulo sched6 &ector ops

    Maor LLVM AenefitsMaor LLVM AenefitsLo$5level, t&ped, ma!hine5independent 1Lo$5level, t&ped, ma!hine5independent 1

    *pli!it re%ister#memor& ar!hite!ture*pli!it re%ister#memor& ar!hite!ture

    A%%ressive mid5level and ba!k5end !ompiler rame$orkA%%ressive mid5level and ba!k5end !ompiler rame$ork

    Full-system e+aluation,Full-system e+aluation, appli!ations, libraries, even >Sappli!ations, libraries, even >S

  • 7/25/2019 llvm tutorial

    60/61

    Chris Lattner

    3ive point LLV( evie$3ive point LLV( evie$

    Extremel+ simple I( to learn and useExtremel+ simple I( to learn and use ;5to5; !orresponden!e bet$een "ll, "b!, and CDD 1;5to5; !orresponden!e bet$een "ll, "b!, and CDD 1

    Ver& positive user rea!tionsVer& positive user rea!tions

    )owerful and modular optimi>er)owerful and modular optimi>er

    *as& to etend, or Bust use $hat is alread& there*as& to etend, or Bust use $hat is alread& there Clean and modular code generatorClean and modular code generator*as& to retar%et, eas& to repla!e#t$eak !omponents*as& to retar%et, eas& to repla!e#t$eak !omponents

    Man+ Jproducti&it+ toolsK ,bugpoint< &erifier/Man+ Jproducti&it+ toolsK ,bugpoint< &erifier/

    +et more done, Oui!kerM+et more done, Oui!kerM !cti&e de& communit+< good documentation!cti&e de& communit+< good documentation(ailin% lists, 1C, do&%en, etensive do!s(ailin% lists, 1C, do&%en, etensive do!s

  • 7/25/2019 llvm tutorial

    61/61

    +et started $ith LLV(M+et started $ith LLV(M

    =ownload latest release or CV':=ownload latest release or CV':http://llvm.cs.uiuc.edu/releases/http://llvm.cs.uiuc.edu/releases/

    Follow the J@etting 'tarted @uideK:Follow the J@etting 'tarted @uideK:

    http://llvm.cs.uiuc.edu/docs/GettingStarted.hthttp://llvm.cs.uiuc.edu/docs/GettingStarted.ht

    mlml

    -alks &ou throu%h install and setup-alks &ou throu%h install and setup

    Lots o other do!s available in Jdo!sK dire!tor&Lots o other do!s available in Jdo!sK dire!tor&

    )oin us on mailin% lists and 1C)oin us on mailin% lists and 1C

    ?app+ hacing;?app+ hacing;