the llvm compiler framework and sasanka vanand shukla ... llvm compiler system nthe llvm compiler...

Download The LLVM Compiler Framework and   Sasanka vAnand Shukla ... LLVM Compiler System nThe LLVM Compiler Infrastructure ... nThe LLVM Compiler Framework

Post on 06-Feb-2018

228 views

Category:

Documents

11 download

Embed Size (px)

TRANSCRIPT

  • The LLVM Compiler The LLVM Compiler Framework and InfrastructureFramework and Infrastructure

    VikramVikram AdveAdvevadve@cs.uiuc.eduvadve@cs.uiuc.edu

    Chris Chris LattnerLattnerlattner@cs.uiuc.edulattner@cs.uiuc.edu

    http://http://llvm.cs.uiuc.edullvm.cs.uiuc.edu//LCPC Tutorial: September 22, 2004LCPC Tutorial: September 22, 2004

  • Chris Lattner lattner@cs.uiuc.eduhttp://llvm.cs.uiuc.edu/

    AcknowledgementsAcknowledgementsUIUC Contributors:UIUC Contributors:

    vv Tanya Tanya BrethourBrethourvv MishaMisha BrukmanBrukmanvv Cameron Cameron BuschardtBuschardtvv John CriswellJohn Criswellvv AlkisAlkis EvlogimenosEvlogimenosvv Brian Brian GaekeGaekevv RuchiraRuchira SasankaSasankavv AnandAnand ShuklaShuklavv Bill Bill WendlingWendling

    External Contributors:External Contributors:vv HenrikHenrik BachBachvv NateNate BegemanBegemanvv Jeff CohenJeff Cohenvv Paolo Paolo InvernizziInvernizzivv Brad JonesBrad Jonesvv Vladimir Vladimir MerzliakovMerzliakovvv Vladimir Vladimir PrusPrusvv Reid SpencerReid Spencer

    Funding: Funding: This work is sponsored by the NSF Next Generation Software progrThis work is sponsored by the NSF Next Generation Software program through am through grants EIAgrants EIA--0093426 (an NSF CAREER award) and EIA0093426 (an NSF CAREER award) and EIA--0103756. It is also supported 0103756. It is also supported in part by the NSF Operating Systems and Compilers program (granin part by the NSF Operating Systems and Compilers program (grant #CCRt #CCR--9988482), the NSF Embedded Systems program (grant #CCR9988482), the NSF Embedded Systems program (grant #CCR--0209202), the 0209202), the MARCO/DARPA MARCO/DARPA GigascaleGigascale Systems Research Center (GSRC), IBM through the Systems Research Center (GSRC), IBM through the DARPADARPA--funded PERCS project, and the Motorola University Partnerships ifunded PERCS project, and the Motorola University Partnerships in n Research program.Research program.

  • Chris Lattner lattner@cs.uiuc.eduhttp://llvm.cs.uiuc.edu/

    LLVM Compiler SystemLLVM Compiler System

    nn The LLVM Compiler InfrastructureThe LLVM Compiler InfrastructurevvProvides reusable components for building compilersProvides reusable components for building compilersvvReduce the time/cost to build a new compilerReduce the time/cost to build a new compilervvBuild static compilers, Build static compilers, JITsJITs, trace, trace--based optimizers, ...based optimizers, ...

    nn The LLVM Compiler FrameworkThe LLVM Compiler FrameworkvvEndEnd--toto--end compilers using the LLVM infrastructureend compilers using the LLVM infrastructurevvC and C++ are robust and aggressive:C and C++ are robust and aggressive:

    nn Java, Scheme and others are in developmentJava, Scheme and others are in development

    vvEmit C code or native code for X86, Emit C code or native code for X86, SparcSparc, PowerPC, PowerPC

  • Chris Lattner lattner@cs.uiuc.eduhttp://llvm.cs.uiuc.edu/

    Three primary LLVM componentsThree primary LLVM components

    nn The LLVM The LLVM Virtual Instruction SetVirtual Instruction SetvvThe common languageThe common language-- and targetand target--independent IRindependent IRvv Internal (IR) and external (persistent) representationInternal (IR) and external (persistent) representation

    nn A collection of wellA collection of well--integrated librariesintegrated librariesvvAnalyses, optimizations, code generators, JIT Analyses, optimizations, code generators, JIT

    compiler, garbage collection support, profiling, compiler, garbage collection support, profiling,

    nn A collection of tools built from the librariesA collection of tools built from the librariesvvAssemblers, automatic debugger, linker, code Assemblers, automatic debugger, linker, code

    generator, compiler driver, modular optimizer, generator, compiler driver, modular optimizer,

  • Chris Lattner lattner@cs.uiuc.eduhttp://llvm.cs.uiuc.edu/

    Tutorial OverviewTutorial Overview

    nn Introduction to the running exampleIntroduction to the running examplenn LLVM C/C++ Compiler OverviewLLVM C/C++ Compiler OverviewvvHighHigh--level view of an example LLVM compilerlevel view of an example LLVM compiler

    nn The LLVM Virtual Instruction SetThe LLVM Virtual Instruction Setvv IR overview and typeIR overview and type--systemsystem

    nn LLVM C++ IR and important APILLVM C++ IR and important APIssvvBasics, Basics, PassManagerPassManager, dataflow, , dataflow, ArgPromotionArgPromotion

    nn Important LLVM ToolsImportant LLVM Toolsvvopt, code generator, JIT, test suite, opt, code generator, JIT, test suite, bugpointbugpoint

    nn Example applications of LLVMExample applications of LLVM

  • Chris Lattner lattner@cs.uiuc.eduhttp://llvm.cs.uiuc.edu/

    Running example: Running example: argarg promotionpromotion

    Consider use of byConsider use of by--reference parameters:reference parameters:int int callee(constcallee(const int &X) {int &X) {

    return X+1;return X+1;}}int caller() {int caller() {

    return callee(4);return callee(4);}}

    int int callee(constcallee(const int *X) {int *X) {return *X+1; return *X+1; // memory load// memory load

    }}int caller() {int caller() {

    int int tmptmp; ; // stack object// stack objecttmptmp = 4; = 4; // memory store// memory storereturn return callee(&tmpcallee(&tmp););

    }}

    compiles to

    Eliminated load in callee

    Eliminated store in caller

    Eliminated stack slot for tmp

    int int callee(intcallee(int X) {X) {return X+1;return X+1;

    }}int caller() {int caller() {

    return callee(4);return callee(4);}}

    We want:We want:

  • Chris Lattner lattner@cs.uiuc.eduhttp://llvm.cs.uiuc.edu/

    Why is this hard?Why is this hard?

    nn Requires interprocedural analysis:Requires interprocedural analysis:vvMust change the prototype of the Must change the prototype of the calleecalleevvMust update all call sites Must update all call sites we must we must knowknow all callersall callersvvWhat about callers outside the translation unit?What about callers outside the translation unit?

    nn Requires alias analysis:Requires alias analysis:vvReference could alias other pointers in Reference could alias other pointers in calleecalleevvMust know that loaded value doesnMust know that loaded value doesnt change from t change from

    function entry to the loadfunction entry to the loadvvMust know the pointer is not being stored throughMust know the pointer is not being stored through

    nn Reference might not be to a stack object!Reference might not be to a stack object!

  • Chris Lattner lattner@cs.uiuc.eduhttp://llvm.cs.uiuc.edu/

    Tutorial OverviewTutorial Overview

    nn Introduction to the running exampleIntroduction to the running examplenn LLVM C/C++ Compiler OverviewLLVM C/C++ Compiler OverviewvvHighHigh--level view of an example LLVM compilerlevel view of an example LLVM compiler

    nn The LLVM Virtual Instruction SetThe LLVM Virtual Instruction Setvv IR overview and typeIR overview and type--systemsystem

    nn LLVM C++ IR and important APILLVM C++ IR and important APIssvvBasics, Basics, PassManagerPassManager, dataflow, , dataflow, ArgPromotionArgPromotion

    nn Important LLVM ToolsImportant LLVM Toolsvvopt, code generator, JIT, test suite, opt, code generator, JIT, test suite, bugpointbugpoint

    nn Example applications of LLVMExample applications of LLVM

  • Chris Lattner lattner@cs.uiuc.eduhttp://llvm.cs.uiuc.edu/

    The LLVM C/C++ CompilerThe LLVM C/C++ Compiler

    nn From the high level, it is a standard compiler:From the high level, it is a standard compiler:vvCompatible with standard Compatible with standard makefilesmakefilesvvUses GCC 3.4 C and C++ parserUses GCC 3.4 C and C++ parser

    nn Distinguishing features:Distinguishing features:vvUses LLVM optimizers, not GCC optimizersUses LLVM optimizers, not GCC optimizersvv .o files contain LLVM IR/.o files contain LLVM IR/bytecodebytecode, not machine code, not machine codevvExecutable can be Executable can be bytecodebytecode ((JITJITdd) or machine code) or machine code

    llvmg++

    llvmgccC file

    C++ file

    .o file

    .o filellvm linker executable

    Compile Time Link Time

  • Chris Lattner lattner@cs.uiuc.eduhttp://llvm.cs.uiuc.edu/

    Looking into events at compileLooking into events at compile--timetime

    llvmgccC file .o file llvmg++C++ file .o file

    Modified version of G++Emits LLVM IR as text fileLowers C++ AST to LLVM

    Modified version of GCCEmits LLVM IR as text fileLowers C AST to LLVM

    LLVM IR Parser

    LLVM Verifier

    40 LLVM Analysis & Optimization Passes

    LLVM .bcFile Writer

    C to LLVM Frontend

    Compile-time Optimizer

    C++ to LLVM Frontend

    Compile-time Optimizer

    cc1 cc1plus gccasgccas

    Dead Global Elimination, IP Constant Propagation, Dead Argument Elimination, Inlining, Reassociation, LICM, Loop

    Opts, Memory Promotion, Dead Store Elimination, ADCE,

  • Chris Lattner lattner@cs.uiuc.eduhttp://llvm.cs.uiuc.edu/

    Looking into events at linkLooking into events at link--timetime.o file

    .o filellvm linker executable

    Native Code Backend Native executable

    llc

    C Code Backend

    C Compiler Native executable

    llc march=cgcc

    Link in native .o files and libraries here

    LLVM Linker

    Link-time Optimizer .bc file for LLVM JIT

    .o file

    .o file

    20 LLVM Analysis & Optimization Passes

    Optionally internalizes: marks most functions as internal, to improve IPO

    Perfect place for argument promotion optimization!

  • Chris Lattner lattner@cs.uiuc.eduhttp://llvm.cs.uiuc.edu/

    Goals of the compiler des

Recommended

View more >