The LLVM Compiler Framework and Infrastructure
|
|
- Everett Singleton
- 5 years ago
- Views:
Transcription
1 The LLVM Compiler Framework and Infrastructure Program Analysis Original Slides by David Koes (CMU) Substantial portions courtesy Chris Lattner and Vikram Adve
2 LLVM Compiler System The LLVM Compiler Infrastructure Provides reusable components for building compilers Reduces the time/cost to build a new compiler Build static compilers, JITs, trace-based optimizers,... The LLVM Compiler Framework End-to-end compilers using the LLVM infrastructure C and C++ gcc frontend Backends for C, X86, Sparc, PowerPC, Alpha, Arm, Thumb, IA-64 2
3 Three primary LLVM components The LLVM Virtual Instruction Set The common language- and target-independent IR Internal (IR) and external (persistent) representation A collection of well-integrated libraries Analyses, optimizations, code generators, JIT compiler, garbage collection support, profiling, A collection of tools built from the libraries Assemblers, automatic debugger, linker, code generator, compiler driver, modular optimizer, 3
4 Tutorial Overview Introduction to the running example LLVM C/C++ Compiler Overview High-level view of an example LLVM compiler The LLVM Virtual Instruction Set IR overview and type-system LLVM C++ IR and important API s Basics, PassManager, dataflow, ArgPromotion Alias Analysis in LLVM 4
5 Running example: arg promotion Consider use of by-reference parameters: int callee(const int &X) { return X+1; int caller() { return callee(4); We want: int callee(int X) { return X+1; int caller() { return callee(4); compiles to int callee(const int *X) { return *X+1; // memory load int caller() { int tmp; // stack object tmp = 4; // memory store return callee(&tmp); Eliminated load in callee Eliminated store in caller Eliminated stack slot for tmp 5
6 Why is this hard? Requires interprocedural analysis: Must change the prototype of the callee Must update all call sites we must know all callers What about callers outside the translation unit? Requires alias analysis: Reference could alias other pointers in callee Must know that loaded value doesn t change from function entry to the load Must know the pointer is not being stored through Reference might not be to a stack object! 6
7 Tutorial Overview Introduction to the running example LLVM C/C++ Compiler Overview High-level view of an example LLVM compiler The LLVM Virtual Instruction Set IR overview and type-system LLVM C++ IR and important API s Basics, PassManager, dataflow, ArgPromotion Alias Analysis in LLVM 7
8 The LLVM C/C++ Compiler From the high level, it is a standard compiler: Compatible with standard makefiles Uses GCC 4.2 C and C++ parser Generates native executables/object files/assembly Distinguishing features: Uses LLVM optimizers, not GCC optimizers Pass -emit-llvm to output LLVM IR -S: human readable assembly -c: efficient bitcode binary 8
9 Looking into events at compile-time C/C++ file llvm-gcc/llvm-g++ -O -S assembly IR GENERIC GIMPLE (tree-ssa) LLVM IR Machine Code IR -emit-llvm LLVM asm >50 LLVM Analysis & Optimization Passes: Dead Global Elimination, IP Constant Propagation, Dead Argument Elimination, Inlining, Reassociation, LICM, Loop Opts, Memory Promotion, Dead Store Elimination, ADCE, 9
10 Looking into events at link-time LLVM bitcode.o file LLVM bitcode.o file llvm-ld executable LLVM Linker >30 LLVM Analysis & Optimization Passes Optionally internalizes : marks most functions as internal, to improve IPO Link-time Optimizer -native.bc file for LLVM JIT Native executable Perfect place for argument promotion optimization! 10
11 Goals of the compiler design Analyze and optimize as early as possible: Compile-time opts reduce modify-rebuild-execute cycle Compile-time optimizations reduce work at link-time (by shrinking the program) All IPA/IPO make an open-world assumption Thus, they all work on libraries and at compile-time Internalize pass enables whole program optzn One IR (without lowering) for analysis & optzn Compile-time optzns can be run at link-time too! The same IR is used as input to the JIT IR design is the key to these goals! 11
12 Tutorial Overview Introduction to the running example LLVM C/C++ Compiler Overview High-level view of an example LLVM compiler The LLVM Virtual Instruction Set IR overview and type-system LLVM C++ IR and important API s Basics, PassManager, dataflow, ArgPromotion Alias Analysis in LLVM 12
13 Goals of LLVM IR Easy to produce, understand, and define! Language- and Target-Independent AST-level IR (e.g. ANDF, UNCOL) is not very feasible Every analysis/xform must know about all languages One IR for analysis and optimization IR must be able to support aggressive IPO, loop opts, scalar opts, high- and low-level optimization! Optimize as much as early as possible Can t postpone everything until link or runtime No lowering in the IR! 13
14 LLVM Instruction Set Overview #1 Low-level and target-independent semantics RISC-like three address code Infinite virtual register set in SSA form Simple, low-level control flow constructs Load/store instructions with typed-pointers IR has text, binary, and in-memory forms bb: ; preds = %bb, %entry %i.1 = phi i32 [ 0, %entry ], [ %i.2, %bb ] %AiAddr = getelementptr float* %A, i32 %i.1 for (i = 0; i < N; call float* %AiAddr, %pair* %P ) %i.2 = add i32 %i.1, 1 ++i) %exitcond = icmp eq i32 %i.2, %N Sum(&A[i], &P); br i1 %exitcond, label %return, label %bb 14
15 LLVM Instruction Set Overview #2 High-level information exposed in the code Explicit dataflow through SSA form Explicit control-flow graph (even for exceptions) Explicit language-independent type-information Explicit typed pointer arithmetic Preserve array subscript and structure indexing bb: ; preds = %bb, %entry %i.1 = phi i32 [ 0, %entry ], [ %i.2, %bb ] %AiAddr = getelementptr float* %A, i32 %i.1 for (i = 0; i < N; call float* %AiAddr, %pair* %P ) %i.2 = add i32 %i.1, 1 ++i) %exitcond = icmp eq i32 %i.2, %N Sum(&A[i], &P); br i1 %exitcond, label %return, label %bb 15
16 LLVM Type System Details The entire type system consists of: Primitives: integer, floating point, label, void no signed integer types arbitrary bitwidth integers (i32, i64, i1) Derived: pointer, array, structure, function, vector, No high-level types: type-system is language neutral! Type system allows arbitrary casts: Allows expressing weakly-typed languages, like C Front-ends can implement safe languages Also easy to define a type-safe subset of LLVM See also: docs/langref.html 16
17 Lowering source-level types to LLVM Source language types are lowered: Rich type systems expanded to simple type system Implicit & abstract types are made explicit & concrete Examples of lowering: References turn into pointers: T& T* Complex numbers: complex float { float, float Bitfields: struct X { int Y:4; int Z:2; { i32 Inheritance: class T : S { int X; { S, i32 Methods: class T { void foo(); void foo(t*) Same idea as lowering to machine code 17
18 LLVM Program Structure Module contains Functions/GlobalVariables Module is unit of compilation/analysis/optimization Function contains BasicBlocks/Arguments Functions roughly correspond to functions in C BasicBlock contains list of instructions Each block ends in a control flow instruction Instruction is opcode + vector of operands All operands have types Instruction result is typed 18
19 Our example, compiled to LLVM int callee(const int *X) { return *X+1; // load int caller() { int T; // on stack T = 4; // store return callee(&t); All loads/stores are Stack allocation is explicit in the LLVM explicit in LLVM representation define internal %X) { entry: %tmp2 = load i32* %X %tmp3 = add i32 %tmp2, 1 ret i32 %tmp3 define internal { entry: %T = alloca i32 store i32 4, i32* %T %tmp1 = call i32* %T ) ret i32 %tmp1 19
20 Our example, desired transformation define %X) { %tmp2 = load i32* %X %tmp3 = add i32 %tmp2, 1 ret i32 %tmp3 define { %T = alloca i32 store i32 4, i32* %T %tmp1 = call i32* %T ) ret i32 %tmp1 define internal %X.val) { %tmp3 = add i32 %X.val, 1 ret i32 %tmp3 define internal { %T = alloca i32 store i32 4, i32* %T %Tval = load i32* %T %tmp1 = call i32 %Tval ) ret i32 %tmp1 Other transformation Insert Change Update load all the instructions call prototype sites of (-mem2reg) cleans up for into the callee function callers the rest define internal { %tmp1 = call i32 4 ) ret i32 %tmp1 20
21 Tutorial Overview Introduction to the running example LLVM C/C++ Compiler Overview High-level view of an example LLVM compiler The LLVM Virtual Instruction Set IR overview and type-system LLVM C++ IR and important API s Basics, PassManager, dataflow, ArgPromotion Alias Analysis in LLVM 21
22 LLVM Coding Basics Written in modern C++, uses the STL: Particularly the vector, set, and map classes LLVM IR is almost all doubly-linked lists: Module contains lists of Functions & GlobalVariables Function contains lists of BasicBlocks & Arguments BasicBlock contains list of Instructions Linked lists are traversed with iterators: Function *M = for (Function::iterator I = M->begin(); I!= M->end(); ++I) { BasicBlock &BB = *I;... See also: docs/programmersmanual.html 22
23 LLVM Coding Basics cont. BasicBlock doesn t provide a reverse iterator Highly obnoxious when doing the assignment for(basicblock::iterator I = bb->end(); I!= bb->begin(); ) { --I; Instruction *insn = I; Traversing successors of a BasicBlock: for (succ_iterator SI = succ_begin(bb), E = succ_end(bb); SI!= E; ++SI) { BasicBlock *Succ = *SI; C++ is not Java primitive class variable not automatically initialized you must manage memory virtual vs. non-virtual functions and much much more 23
24 LLVM Pass Manager Compiler is organized as a series of passes : Each pass is one analysis or transformation Types of Pass: ModulePass: general interprocedural pass CallGraphSCCPass: bottom-up on the call graph FunctionPass: process a function at a time LoopPass: process a natural loop at a time BasicBlockPass: process a basic block at a time Constraints imposed (e.g. FunctionPass): FunctionPass can only look at current function Cannot maintain state across functions See also: docs/writinganllvmpass.html 24
25 Services provided by PassManager Optimization of pass execution: Process a function at a time instead of a pass at a time Example: If F, G, H are three functions in input pgm: FFFFGGGGHHHH not FGHFGHFGHFGH Process functions in parallel on an SMP (future work) Declarative dependency management: Automatically fulfill and manage analysis pass lifetimes Share analyses between passes when safe: e.g. DominatorSet live unless pass modifies CFG Avoid boilerplate for traversal of program See also: docs/writinganllvmpass.html 25
26 Pass Manager + Arg Promotion #1/2 Arg Promotion is a CallGraphSCCPass: Naturally operates bottom-up on the CallGraph Bubble pointers from callees out to callers 24: #include "llvm/callgraphsccpass.h" 47: struct SimpleArgPromotion : public CallGraphSCCPass { Arg Promotion requires AliasAnalysis info To prove safety of transformation Works with any alias analysis algorithm though 48: virtual void getanalysisusage(analysisusage &AU) const { AU.addRequired<AliasAnalysis>(); // Get aliases AU.addRequired<TargetData>(); // Get data layout CallGraphSCCPass::getAnalysisUsage(AU); // Get CallGraph 26
27 Pass Manager + Arg Promotion #2/2 Finally, implement runonscc (line 65): bool SimpleArgPromotion:: runonscc(const std::vector<callgraphnode*> &SCC) { bool Changed = false, LocalChange; do { // Iterate until we stop promoting from this SCC. LocalChange = false; // Attempt to promote arguments from all functions in this SCC. for (unsigned i = 0, e = SCC.size(); i!= e; ++i) LocalChange = PromoteArguments(SCC[i]); Changed = LocalChange; // Remember that we changed something. while (LocalChange); return Changed; // Passes return true if something changed. static int foo(int ***P) { return ***P; static int foo(int P_val_val_val) { return P_val_val_val; 27
28 Constant Propagation with DefUse Chains 28
29 LLVM Dataflow Analysis LLVM IR is in SSA form: use-def and def-use chains are always available All objects have user/use info, even functions Control Flow Graph is always available: Exposed as BasicBlock predecessor/successor lists Many generic graph algorithms usable with the CFG Higher-level info implemented as passes: Dominators, CallGraph, induction vars, aliasing, GVN, See also: docs/programmersmanual.html 29
30 Arg Promotion: safety check #1/4 #1: Function must be internal (aka static ) 88: if (!F!F->hasInternalLinkage()) return false; #2: Make sure address of F is not taken In LLVM, check that there are only direct calls using F 99: for (Value::use_iterator UI = F->use_begin(); UI!= F->use_end(); ++UI) { CallSite CS = CallSite::get(*UI); if (!CS.getInstruction()) // "Taking the address" of F. return false; #3: Check to see if any args are promotable: 114: for (unsigned i = 0; i!= PointerArgs.size(); ++i) if (!issafetopromoteargument(pointerargs[i])) PointerArgs.erase(PointerArgs.begin()+i); if (PointerArgs.empty()) return false; // no args promotable 30
31 Arg Promotion: safety check #2/4 #4: Argument pointer can only be loaded from: No stores through argument pointer allowed! // Loop over all uses of the argument (use-def chains). 138: for (Value::use_iterator UI = Arg->use_begin(); UI!= Arg->use_end(); ++UI) { // If the user is a load: if (LoadInst *LI = dyn_cast<loadinst>(*ui)) { // Don't modify volatile loads. if (LI->isVolatile()) return false; Loads.push_back(LI); else { return false; // Not a load. 31
32 Alias Analysis The AliasAnalysis class defines the interface that all alias analysis support Computed by the basicaa pass Can be changed Simple example int i; char C[2]; char A[10]; /*... */ for (i = 0; i!= 10; ++i) { C[0] = A[i]; /* One byte store */ C[1] = A[9-i]; /* One byte store */ 32
33 Arg Promotion: safety check #3/4 #5: Value of *P must not change in the BB We move load out to the caller, value cannot change! load P Modifie s *P? // Get AliasAnalysis implementation from the pass manager. 156: AliasAnalysis &AA = getanalysis<aliasanalysis>(); // Ensure *P is not modified from start of block to load 169: if (AA.canInstructionRangeModify(BB->front(), *Load, Arg, LoadSize)) return false; // Pointer is invalidated! See also: docs/aliasanalysis.html 33
34 Arg Promotion: safety check #4/4 #6: *P cannot change from Fn entry to BB Entry Modifie s *P? Modifie s *P? Entry load P load P 175: for (pred_iterator PI = pred_begin(bb), E = pred_end(bb); PI!= E; ++PI) // Loop over predecessors of BB. // Check each block from BB to entry (DF search on inverse graph). for (idf_iterator<basicblock*> I = idf_begin(*pi); I!= idf_end(*pi); ++I) // Might *P be modified in this basic block? if (AA.canBasicBlockModify(**I, Arg, LoadSize)) return false; 34
35 Arg Promotion: xform outline #1/4 #1: Make prototype with new arg types: #197 Basically just replaces int* with int in prototype #2: Create function with new prototype: 214: Function *NF = new Function(NFTy, F->getLinkage(), F->getName()); F->getParent()->getFunctionList().insert(F, NF); #3: Change all callers of F to call NF: // If there are uses of F, then calls to it remain. 221: while (!F->use_empty()) { // Get a caller of F. CallSite CS = CallSite::get(F->use_back()); 35
36 Arg Promotion: xform outline #2/4 #4: For each caller, add loads, determine args Loop over the args, inserting the loads in the caller 220: std::vector<value*> Args; 226: CallSite::arg_iterator AI = CS.arg_begin(); for (Function::aiterator I = F->abegin(); I!= F->aend(); ++I, ++AI) if (!ArgsToPromote.count(I)) // Unmodified argument. Args.push_back(*AI); else { // Insert the load before the call. LoadInst *LI = new LoadInst(*AI, (*AI)->getName()+".val", Call); // Insertion point Args.push_back(LI); 36
37 Arg Promotion: xform outline #3/4 #5: Replace the call site of F with call of NF // Create the call to NF with the adjusted arguments. 242: Instruction *New = new CallInst(NF, Args, "", Call); // If the return value of the old call was used, use the retval of the new call. if (!Call->use_empty()) Call->replaceAllUsesWith(New); // Finally, remove the old call from the program, reducing the use-count of F. Call->getParent()->getInstList().erase(Call); #6: Move code from old function to new Fn 259: NF->getBasicBlockList().splice(NF->begin(), F->getBasicBlockList()); 37
38 Arg Promotion: xform outline #4/4 #7: Change users of F s arguments to use NF s 264: for (Function::aiterator I = F->abegin(), I2 = NF->abegin(); I!= F->aend(); ++I, ++I2) if (!ArgsToPromote.count(I)) { // Not promoting this arg? I->replaceAllUsesWith(I2); // Use new arg, not old arg. else { while (!I->use_empty()) { // Only users can be loads. LoadInst *LI = cast<loadinst>(i->use_back()); LI->replaceAllUsesWith(I2); LI->getParent()->getInstList().erase(LI); #8: Delete old function: 286: F->getParent()->getFunctionList().erase(F); 38
39 Tutorial Overview Introduction to the running example LLVM C/C++ Compiler Overview High-level view of an example LLVM compiler The LLVM Virtual Instruction Set IR overview and type-system LLVM C++ IR and important API s Basics, PassManager, dataflow, ArgPromotion Alias Analysis in LLVM 39
40 Alias Analysis The AliasAnalysis class defines the interface that all alias analysis support Computed by the basicaa pass Can be changed Simple example int i; char C[2]; char A[10]; /*... */ for (i = 0; i!= 10; ++i) { C[0] = A[i]; /* One byte store */ C[1] = A[9-i]; /* One byte store */ 40
41 Project 1 Improve Pointer Analysis LLVM Study the precision of LLVM on some examples Suggest improvements by: Flow sensitivity Destructive updates 41
42 Project 2 Numeric analyzer in LLVM Integrate LLVM and Apron in a reasonable way Intera-procedural only Bonus interprocedural 42
43 Project 3 Shape Analysis in LLVM Integrate LLVM and TVLA in a reasonable way Intera-procedural only Generate TVP 43
LLVM Compiler System. The LLVM Compiler Framework and Infrastructure. Tutorial Overview. Three primary LLVM components
The LLVM Compiler Framework and Infrastructure 15-745: Optimizing Compilers David Koes 1/22/2008 Substantial portions courtesy Chris Lattner and Vikram Adve LLVM Compiler System The LLVM Compiler Infrastructure
More informationThe LLVM Compiler Framework and Infrastructure
The LLVM Compiler Framework and Infrastructure 15-411: Compiler Design Slides by David Koes Substantial portions courtesy Chris Lattner and Vikram Adve LLVM Compiler System The LLVM Compiler Infrastructure
More informationLecture 3 Overview of the LLVM Compiler
LLVM Compiler System Lecture 3 Overview of the LLVM Compiler The LLVM Compiler Infrastructure - Provides reusable components for building compilers - Reduce the time/cost to build a new compiler - Build
More informationLecture 3 Overview of the LLVM Compiler
Lecture 3 Overview of the LLVM Compiler Jonathan Burket Special thanks to Deby Katz, Gennady Pekhimenko, Olatunji Ruwase, Chris Lattner, Vikram Adve, and David Koes for their slides The LLVM Compiler Infrastructure
More informationECE 5775 (Fall 17) High-Level Digital Design Automation. Static Single Assignment
ECE 5775 (Fall 17) High-Level Digital Design Automation Static Single Assignment Announcements HW 1 released (due Friday) Student-led discussions on Tuesday 9/26 Sign up on Piazza: 3 students / group Meet
More informationLecture 2 Overview of the LLVM Compiler
Lecture 2 Overview of the LLVM Compiler Abhilasha Jain Thanks to: VikramAdve, Jonathan Burket, DebyKatz, David Koes, Chris Lattner, Gennady Pekhimenko, and Olatunji Ruwase, for their slides The LLVM Compiler
More information15-411: LLVM. Jan Hoffmann. Substantial portions courtesy of Deby Katz
15-411: LLVM Jan Hoffmann Substantial portions courtesy of Deby Katz and Gennady Pekhimenko, Olatunji Ruwase,Chris Lattner, Vikram Adve, and David Koes Carnegie What is LLVM? A collection of modular and
More informationINTRODUCTION TO LLVM Bo Wang SA 2016 Fall
INTRODUCTION TO LLVM Bo Wang SA 2016 Fall LLVM Basic LLVM IR LLVM Pass OUTLINE What is LLVM? LLVM is a compiler infrastructure designed as a set of reusable libraries with well-defined interfaces. Implemented
More informationIntroduction to LLVM compiler framework
Introduction to LLVM compiler framework Michele Scandale Politecnico di Milano April 8, 2015 This material is strongly based on Ettore Speziale s material for the previous year course. Michele Scandale
More informationLLVM Tutorial. John Criswell University of Rochester
LLVM Tutorial John Criswell University of Rochester 1 Overview 2 History of LLVM Developed by Chris Lattner and Vikram Adve at the University of Illinois at Urbana-Champaign Released open-source in October
More informationPlan for Today. Concepts. Next Time. Some slides are from Calvin Lin s grad compiler slides. CS553 Lecture 2 Optimizations and LLVM 1
Plan for Today Quiz 2 How to automate the process of performance optimization LLVM: Intro to Intermediate Representation Loops as iteration spaces Data-flow Analysis Intro Control-flow graph terminology
More informationA Brief Introduction to Using LLVM. Nick Sumner
A Brief Introduction to Using LLVM Nick Sumner What is LLVM? A compiler? (clang) What is LLVM? A compiler? (clang) A set of formats, libraries, and tools. What is LLVM? A compiler? (clang) A set of formats,
More informationIntroduction to LLVM compiler framework
Introduction to LLVM compiler framework Stefano Cherubin Politecnico di Milano 12-04-2017 This material is strongly based on material produced by Michele Scandale and Ettore Speziale for the course `Code
More informationLecture 6 More on the LLVM Compiler
Lecture 6 More on the LLVM Compiler Jonathan Burket Special thanks to Deby Katz, Luke Zarko, and Gabe Weisz for their slides Visualizing the LLVM Compiler System C C++ Java Source Code Clang (Front End)
More informationLecture 4 Overview of the LLVM Compiler
Lecture 4 Overview of the LLVM Compiler Pratik Fegade Thanks to: Vikram Adve, Jonathan Burket, Deby Katz, David Koes, Chris Lattner, Gennady Pekhimenko, and Olatunji Ruwase, for their slides Visualizing
More informationLecture 4 More on the LLVM Compiler
Lecture 4 More on the LLVM Compiler Abhilasha Jain Thanks to: Jonathan Burket, Deby Katz, Gabe Weisz, Luke Zarko, and Dominic Chen for their slides Visualizing the LLVM Compiler System C C++ Java Source
More informationLLVM & LLVM Bitcode Introduction
LLVM & LLVM Bitcode Introduction What is LLVM? (1/2) LLVM (Low Level Virtual Machine) is a compiler infrastructure Written by C++ & STL History The LLVM project started in 2000 at the University of Illinois
More informationCS 426 Fall Machine Problem 4. Machine Problem 4. CS 426 Compiler Construction Fall Semester 2017
CS 426 Fall 2017 1 Machine Problem 4 Machine Problem 4 CS 426 Compiler Construction Fall Semester 2017 Handed out: November 16, 2017. Due: December 7, 2017, 5:00 p.m. This assignment deals with implementing
More informationLLVM: A Compilation Framework. Transformation
Systems and Internet Infrastructure Security Network and Security Research Center Department of Computer Science and Engineering Pennsylvania State University, University Park PA LLVM: A Compilation Framework
More informationLLVM and IR Construction
LLVM and IR Construction Fabian Ritter based on slides by Christoph Mallon and Johannes Doerfert http://compilers.cs.uni-saarland.de Compiler Design Lab Saarland University 1 Project Progress source code
More informationLecture Compiler Middle-End
Lecture 16-18 18 Compiler Middle-End Jianwen Zhu Electrical and Computer Engineering University of Toronto Jianwen Zhu 2009 - P. 1 What We Have Done A lot! Compiler Frontend Defining language Generating
More informationUniversidade Federal de Minas Gerais Department of Computer Science Programming Languages Laboratory WRITING AN LLVM PASS DCC 888
Universidade Federal de Minas Gerais Department of Computer Science Programming Languages Laboratory WRITING AN LLVM PASS DCC 888 Passes LLVM applies a chain of analyses and transformaaons on the target
More informationIntermediate Representation (IR)
Intermediate Representation (IR) Components and Design Goals for an IR IR encodes all knowledge the compiler has derived about source program. Simple compiler structure source code More typical compiler
More informationTutorial: Building a backend in 24 hours. Anton Korobeynikov
Tutorial: Building a backend in 24 hours Anton Korobeynikov anton@korobeynikov.info Outline 1. From IR to assembler: codegen pipeline 2. MC 3. Parts of a backend 4. Example step-by-step The Pipeline LLVM
More informationAnnouncements. My office hours are today in Gates 160 from 1PM-3PM. Programming Project 3 checkpoint due tomorrow night at 11:59PM.
IR Generation Announcements My office hours are today in Gates 160 from 1PM-3PM. Programming Project 3 checkpoint due tomorrow night at 11:59PM. This is a hard deadline and no late submissions will be
More information4/1/15 LLVM AND SSA. Low-Level Virtual Machine (LLVM) LLVM Compiler Infrastructure. LL: A Subset of LLVM. Basic Blocks
4//5 Low-Level Virtual Machine (LLVM) LLVM AND SSA Slides adapted from those prepared by Steve Zdancewic at Penn Open-Source Compiler Infrastructure see llvm.org for full documntation Created by Chris
More informationLecture 23 CIS 341: COMPILERS
Lecture 23 CIS 341: COMPILERS Announcements HW6: Analysis & Optimizations Alias analysis, constant propagation, dead code elimination, register allocation Due: Wednesday, April 25 th Zdancewic CIS 341:
More informationTargeting LLVM IR. LLVM IR, code emission, assignment 4
Targeting LLVM IR LLVM IR, code emission, assignment 4 LLVM Overview Common set of tools & optimizations for compiling many languages to many architectures (x86, ARM, PPC, ASM.js). Integrates AOT & JIT
More informationAccelerating Ruby with LLVM
Accelerating Ruby with LLVM Evan Phoenix Oct 2, 2009 RUBY RUBY Strongly, dynamically typed RUBY Unified Model RUBY Everything is an object RUBY 3.class # => Fixnum RUBY Every code context is equal RUBY
More informationCIS 341 Final Examination 4 May 2017
CIS 341 Final Examination 4 May 2017 1 /14 2 /15 3 /12 4 /14 5 /34 6 /21 7 /10 Total /120 Do not begin the exam until you are told to do so. You have 120 minutes to complete the exam. There are 14 pages
More informationCompiler Construction: LLVMlite
Compiler Construction: LLVMlite Direct compilation Expressions X86lite Input Output Compile directly from expression language to x86 Syntax-directed compilation scheme Special cases can improve generated
More informationAn example of optimization in LLVM. Compiler construction Step 1: Naive translation to LLVM. Step 2: Translating to SSA form (opt -mem2reg)
Compiler construction 2014 An example of optimization in LLVM Lecture 8 More on code optimization SSA form Constant propagation Common subexpression elimination Loop optimizations int f () { int i, j,
More informationShort Notes of CS201
#includes: Short Notes of CS201 The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with < and > if the file is a system
More informationWhere We Are. Lexical Analysis. Syntax Analysis. IR Generation. IR Optimization. Code Generation. Machine Code. Optimization.
Where We Are Source Code Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Machine Code Where We Are Source Code Lexical Analysis Syntax Analysis
More informationCS201 - Introduction to Programming Glossary By
CS201 - Introduction to Programming Glossary By #include : The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with
More informationExpressing high level optimizations within LLVM. Artur Pilipenko
Expressing high level optimizations within LLVM Artur Pilipenko artur.pilipenko@azul.com This presentation describes advanced development work at Azul Systems and is for informational purposes only. Any
More informationLLVM code generation and implementation of nested functions for the SimpliC language
LLVM code generation and implementation of nested functions for the SimpliC language Oscar Legetth Lunds University dat12ole@student.lth.se Gustav Svensson Lunds University dat12gs1@student.lth.se Abstract
More informationFinal CSE 131B Spring 2004
Login name Signature Name Student ID Final CSE 131B Spring 2004 Page 1 Page 2 Page 3 Page 4 Page 5 Page 6 Page 7 Page 8 (25 points) (24 points) (32 points) (24 points) (28 points) (26 points) (22 points)
More informationAdvanced C Programming
Advanced C Programming Compilers Sebastian Hack hack@cs.uni-sb.de Christoph Weidenbach weidenbach@mpi-inf.mpg.de 20.01.2009 saarland university computer science 1 Contents Overview Optimizations Program
More informationUsually, target code is semantically equivalent to source code, but not always!
What is a Compiler? Compiler A program that translates code in one language (source code) to code in another language (target code). Usually, target code is semantically equivalent to source code, but
More informationLLVM - the early days. Where did it come from, and how?
LLVM - the early days Where did it come from, and how? Before LLVM September 1999 Tiger native compiler Directed study in compilers @ UofP: with Dr. Steven Vegdahl, Nick Forrette http://www.nondot.org/sabre/projects/compilers/
More informationGCC Internals Control and data flow support
GCC Internals Control and data flow support Diego Novillo dnovillo@google.com November 2007 Control/Data Flow support Call Graph (cgraph) Control Flow Graph (CFG) Static Single Assignment in GIMPLE (SSA)
More informationFinal CSE 131B Winter 2003
Login name Signature Name Student ID Final CSE 131B Winter 2003 Page 1 Page 2 Page 3 Page 4 Page 5 Page 6 Page 7 Page 8 _ (20 points) _ (25 points) _ (21 points) _ (40 points) _ (30 points) _ (25 points)
More informationTutorial: Building a backend in 24 hours. Anton Korobeynikov
Tutorial: Building a backend in 24 hours Anton Korobeynikov anton@korobeynikov.info Outline 1. Codegen phases and parts 2. The Target 3. First steps 4. Custom lowering 5. Next steps Codegen Phases Preparation
More informationGetting started. Roel Jordans
Getting started Roel Jordans Goal Translate a program from a high level language into something the processor can execute efficiently So before we start we need to know how this processor executes a program
More informationInterprocedural Type Specialization of JavaScript Programs Without Type Analysis
Interprocedural Type Specialization of JavaScript Programs Without Type Analysis Maxime Chevalier-Boisvert joint work with Marc Feeley ECOOP - July 20th, 2016 Overview Previous work: Lazy Basic Block Versioning
More informationLLVM, Clang and Embedded Linux Systems. Bruno Cardoso Lopes University of Campinas
LLVM, Clang and Embedded Linux Systems Bruno Cardoso Lopes University of Campinas What s LLVM? What s LLVM Compiler infrastructure Frontend (clang) IR Optimizer Backends JIT Tools Assembler Disassembler
More informationNames, Scope, and Bindings
Names, Scope, and Bindings COMS W4115 Prof. Stephen A. Edwards Spring 2007 Columbia University Department of Computer Science What s In a Name? Name: way to refer to something else variables, functions,
More informationA Propagation Engine for GCC
A Propagation Engine for GCC Diego Novillo Red Hat Canada dnovillo@redhat.com May 1, 2005 Abstract Several analyses and transformations work by propagating known values and attributes throughout the program.
More informationImplementing Subprograms
1 Implementing Subprograms CS 315 Programming Languages Pinar Duygulu Bilkent University CS315 Programming Languages Pinar Duygulu The General Semantics of Calls and Returns 2 The subprogram call and return
More informationIntermediate Code Generation (ICG)
Intermediate Code Generation (ICG) Transform AST to lower-level intermediate representation Basic Goals: Separation of Concerns Generate efficient code sequences for individual operations Keep it fast
More informationCreating Formally Verified Components for Layered Assurance with an LLVM-to-ACL2 Translator
Creating Formally Verified Components for Layered Assurance with an LLVM-to-ACL2 Translator Jennifer Davis, David Hardin, Jedidiah McClurg December 2013 Introduction Research objectives: Reduce need to
More informationDesign Issues. Subroutines and Control Abstraction. Subroutines and Control Abstraction. CSC 4101: Programming Languages 1. Textbook, Chapter 8
Subroutines and Control Abstraction Textbook, Chapter 8 1 Subroutines and Control Abstraction Mechanisms for process abstraction Single entry (except FORTRAN, PL/I) Caller is suspended Control returns
More informationCOS 320. Compiling Techniques
Topic 5: Types COS 320 Compiling Techniques Princeton University Spring 2016 Lennart Beringer 1 Types: potential benefits (I) 2 For programmers: help to eliminate common programming mistakes, particularly
More informationAdvanced Compiler Construction
CS 526 Advanced Compiler Construction http://misailo.cs.illinois.edu/courses/cs526 INTERPROCEDURAL ANALYSIS The slides adapted from Vikram Adve So Far Control Flow Analysis Data Flow Analysis Dependence
More informationIntroduction to LLVM. UG3 Compiling Techniques Autumn 2018
Introduction to LLVM UG3 Compiling Techniques Autumn 2018 Contact Information Instructor: Aaron Smith Email: aaron.l.smith@ed.ac.uk Office: IF 1.29 TA for LLVM: Andrej Ivanis Email: andrej.ivanis@ed.ac.uk
More informationHomework #3: CMPT-379
Only submit answers for questions marked with. Homework #3: CMPT-379 Download the files for this homework: wget http://www.cs.sfu.ca/ msiahban/personal/teaching/cmpt-379-spring-2016/hw3.tgz Put your solution
More informationRegister Allocation. CS 502 Lecture 14 11/25/08
Register Allocation CS 502 Lecture 14 11/25/08 Where we are... Reasonably low-level intermediate representation: sequence of simple instructions followed by a transfer of control. a representation of static
More informationUnder the Compiler's Hood: Supercharge Your PLAYSTATION 3 (PS3 ) Code. Understanding your compiler is the key to success in the gaming world.
Under the Compiler's Hood: Supercharge Your PLAYSTATION 3 (PS3 ) Code. Understanding your compiler is the key to success in the gaming world. Supercharge your PS3 game code Part 1: Compiler internals.
More informationTypes. Type checking. Why Do We Need Type Systems? Types and Operations. What is a type? Consensus
Types Type checking What is a type? The notion varies from language to language Consensus A set of values A set of operations on those values Classes are one instantiation of the modern notion of type
More informationThe structure of a compiler
The structure of a compiler O(n) A compiler is a lot of fast stuff followed by hard problems scanning parsing intermediate code generation Polynomial time Code optimization: local dataflow, global dataflow,
More informationSSA Construction. Daniel Grund & Sebastian Hack. CC Winter Term 09/10. Saarland University
SSA Construction Daniel Grund & Sebastian Hack Saarland University CC Winter Term 09/10 Outline Overview Intermediate Representations Why? How? IR Concepts Static Single Assignment Form Introduction Theory
More informationSystem Software Assignment 1 Runtime Support for Procedures
System Software Assignment 1 Runtime Support for Procedures Exercise 1: Nested procedures Some programming languages like Oberon and Pascal support nested procedures. 1. Find a run-time structure for such
More informationSimone Campanoni Alias Analysis
Simone Campanoni simonec@eecs.northwestern.edu Alias Analysis Memory alias analysis: the problem Does j depend on i? i: (*p) = vara + 1 j: varb = (*q) * 2 i: obj1.f = vara + 1 j: varb= obj2.f * 2 Do p
More informationLLVM for a Managed Language What we've learned
LLVM for a Managed Language What we've learned Sanjoy Das, Philip Reames {sanjoy,preames}@azulsystems.com LLVM Developers Meeting Oct 30, 2015 This presentation describes advanced development work at Azul
More informationAdvanced Compiler Construction
CS 526 Advanced Compiler Construction http://misailo.cs.illinois.edu/courses/cs526 Goals of the Course Develop a fundamental understanding of the major approaches to program analysis and optimization Understand
More informationSE352b: Roadmap. SE352b Software Engineering Design Tools. W3: Programming Paradigms
SE352b Software Engineering Design Tools W3: Programming Paradigms Feb. 3, 2005 SE352b, ECE,UWO, Hamada Ghenniwa SE352b: Roadmap CASE Tools: Introduction System Programming Tools Programming Paradigms
More informationCCured. One-Slide Summary. Lecture Outline. Type-Safe Retrofitting of C Programs
CCured Type-Safe Retrofitting of C Programs [Necula, McPeak,, Weimer, Condit, Harren] #1 One-Slide Summary CCured enforces memory safety and type safety in legacy C programs. CCured analyzes how you use
More informationWriting LLVM Backends Construindo Backends para o LLVM. Bruno Cardoso Lopes
Writing LLVM Backends Construindo Backends para o LLVM Bruno Cardoso Lopes Agenda What s LLVM? Why LLVM? Who is using? Infrastructure. Internals. The Backend. Writing LLVM backends. Work. What's LLVM?
More informationEECS Homework 2
EECS 583 - Homework 2 Assigned: Wednesday, September 28th, 2016 Due: Monday, October 17th, 2016 (11:00:00 AM) 1 Summary The goal of this homework is to extend the LLVM loop invariant code motion (LICM)
More informationSSA Based Mobile Code: Construction and Empirical Evaluation
SSA Based Mobile Code: Construction and Empirical Evaluation Wolfram Amme Friedrich Schiller University Jena, Germany Michael Franz Universit of California, Irvine, USA Jeffery von Ronne Universtity of
More informationOperating Systems CMPSCI 377, Lec 2 Intro to C/C++ Prashant Shenoy University of Massachusetts Amherst
Operating Systems CMPSCI 377, Lec 2 Intro to C/C++ Prashant Shenoy University of Massachusetts Amherst Department of Computer Science Why C? Low-level Direct access to memory WYSIWYG (more or less) Effectively
More informationTranslating JVM Code to MIPS Code 1 / 43
Translating JVM Code to MIPS Code 1 / 43 Outline 1 Introduction 2 SPIM and the MIPS Architecture 3 Our Translator 2 / 43 Introduction Compilation is not necessarily done after the class file is constructed
More informationDevelopment of a Translator from LLVM to ACL2
Development of a Translator from LLVM to ACL2 David Hardin, Jennifer Davis, David Greve, and Jedidiah McClurg July 2014 Introduction Research objectives: Reason about machine code generated from high-level
More informationOptiCode: Machine Code Deobfuscation for Malware Analysis
OptiCode: Machine Code Deobfuscation for Malware Analysis NGUYEN Anh Quynh, COSEINC CONFidence, Krakow - Poland 2013, May 28th 1 / 47 Agenda 1 Obfuscation problem in malware analysis
More informationCSE 501: Compiler Construction. Course outline. Goals for language implementation. Why study compilers? Models of compilation
CSE 501: Compiler Construction Course outline Main focus: program analysis and transformation how to represent programs? how to analyze programs? what to analyze? how to transform programs? what transformations
More informationReuse Optimization. LLVM Compiler Infrastructure. Local Value Numbering. Local Value Numbering (cont)
LLVM Compiler Infrastructure Source: LLVM: A Compilation Framework for Lifelong Program Analysis & Transformation by Lattner and Adve Reuse Optimization Eliminate redundant operations in the dynamic execution
More informationCHANDLER CARRUTH LLVM DEVMTG 2014 THE LLVM PASS MANAGER PART 2
CHANDLER CARRUTH LLVM DEVMTG 2014 THE LLVM PASS MANAGER PART 2 FROM THE PREVIOUS TALK: A pass operates on some unit of IR (Function, Module, ) Generally to transform it from one form to another Alternatively
More informationCIS 341 Final Examination 4 May 2018
CIS 341 Final Examination 4 May 2018 1 /21 2 /25 3 /30 4 /16 5 /15 6 /13 Total /120 Do not begin the exam until you are told to do so. You have 120 minutes to complete the exam. There are 16 pages in this
More informationCompiler Passes. Optimization. The Role of the Optimizer. Optimizations. The Optimizer (or Middle End) Traditional Three-pass Compiler
Compiler Passes Analysis of input program (front-end) character stream Lexical Analysis Synthesis of output program (back-end) Intermediate Code Generation Optimization Before and after generating machine
More informationCSCI-1200 Data Structures Fall 2017 Lecture 5 Pointers, Arrays, & Pointer Arithmetic
CSCI-1200 Data Structures Fall 2017 Lecture 5 Pointers, Arrays, & Pointer Arithmetic Review from Letctures 3 & 4 C++ class syntax, designing classes, classes vs. structs; Passing comparison functions to
More informationGCC Internals Alias analysis
GCC Internals Alias analysis Diego Novillo dnovillo@google.com November 2007 Overview GIMPLE represents alias information explicitly Alias analysis is just another pass Artificial symbols represent memory
More informationAgenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures
The main body and cout Agenda 1 Fundamental data types Declarations and definitions Control structures References, pass-by-value vs pass-by-references The main body and cout 2 C++ IS AN OO EXTENSION OF
More informationMcSema: Static Translation of X86 Instructions to LLVM
McSema: Static Translation of X86 Instructions to LLVM ARTEM DINABURG, ARTEM@TRAILOFBITS.COM ANDREW RUEF, ANDREW@TRAILOFBITS.COM About Us Artem Security Researcher blog.dinaburg.org Andrew PhD Student,
More informationLLVM IR Code Generations Inside YACC. Li-Wei Kuo
LLVM IR Code Generations Inside YACC Li-Wei Kuo LLVM IR LLVM code representation In memory compiler IR (Intermediate Representation) On-disk bitcode representation (*.bc) Human readable assembly language
More informationBEAMJIT, a Maze of Twisty Little Traces
BEAMJIT, a Maze of Twisty Little Traces A walk-through of the prototype just-in-time (JIT) compiler for Erlang. Frej Drejhammar 130613 Who am I? Senior researcher at the Swedish Institute
More informationELEC 377 C Programming Tutorial. ELEC Operating Systems
ELE 377 Programming Tutorial Outline! Short Introduction! History & Memory Model of! ommon Errors I have seen over the years! Work through a linked list example on the board! - uses everything I talk about
More informationCode Generation. Lecture 12
Code Generation Lecture 12 1 Lecture Outline Topic 1: Basic Code Generation The MIPS assembly language A simple source language Stack-machine implementation of the simple language Topic 2: Code Generation
More informationNames, Scope, and Bindings
Names, Scope, and Bindings COMS W4115 Prof. Stephen A. Edwards Fall 2007 Columbia University Department of Computer Science What s In a Name? Name: way to refer to something else variables, functions,
More informationMore Dataflow Analysis
More Dataflow Analysis Steps to building analysis Step 1: Choose lattice Step 2: Choose direction of dataflow (forward or backward) Step 3: Create transfer function Step 4: Choose confluence operator (i.e.,
More informationC Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee
C Language Part 1 (Minor modifications by the instructor) References C for Python Programmers, by Carl Burch, 2011. http://www.toves.org/books/cpy/ The C Programming Language. 2nd ed., Kernighan, Brian,
More informationLecture #16: Introduction to Runtime Organization. Last modified: Fri Mar 19 00:17: CS164: Lecture #16 1
Lecture #16: Introduction to Runtime Organization Last modified: Fri Mar 19 00:17:19 2010 CS164: Lecture #16 1 Status Lexical analysis Produces tokens Detects & eliminates illegal tokens Parsing Produces
More informationA simple tutorial on generating PTX assembler out of Ada source code using LLVM NVPTX backend
Institute of Computational Science A simple tutorial on generating PTX assembler out of Ada source code using LLVM NVPTX backend Dmitry Mikushin dmitrymikushin@usich September 14, 2012 Dmitry Mikushin
More informationProject Compiler. CS031 TA Help Session November 28, 2011
Project Compiler CS031 TA Help Session November 28, 2011 Motivation Generally, it s easier to program in higher-level languages than in assembly. Our goal is to automate the conversion from a higher-level
More informationInterprocedural Analysis. Dealing with Procedures. Course so far: Terminology
Interprocedural Analysis Course so far: Control Flow Representation Dataflow Representation SSA form Classic DefUse and UseDef Chains Optimizations Scheduling Register Allocation Just-In-Time Compilation
More informationOutline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring
Java Outline Java Models for variables Types and type checking, type safety Interpretation vs. compilation Reasoning about code CSCI 2600 Spring 2017 2 Java Java is a successor to a number of languages,
More information1.3b Type Conversion
1.3b Type Conversion Type Conversion When we write expressions involved data that involves two different data types, such as multiplying an integer and floating point number, we need to perform a type
More informationPointer Analysis for C/C++ with cclyzer. George Balatsouras University of Athens
Pointer Analysis for C/C++ with cclyzer George Balatsouras University of Athens Analyzes C/C++ programs translated to LLVM Bitcode Declarative framework Analyzes written in Datalog
More informationKurt Schmidt. October 30, 2018
to Structs Dept. of Computer Science, Drexel University October 30, 2018 Array Objectives to Structs Intended audience: Student who has working knowledge of Python To gain some experience with a statically-typed
More informationIntroduction to Programming (Java) 2/12
Introduction to Programming (Java) 2/12 Michal Krátký Department of Computer Science Technical University of Ostrava Introduction to Programming (Java) 2008/2009 c 2006 2008 Michal Krátký Introduction
More information