Bojian Zheng CSCD70 Spring 2018

Slides:



Advertisements
Similar presentations
CPSC 388 – Compiler Design and Construction
Advertisements

ANALYSIS OF PROG. LANG. PROGRAM ANALYSIS Instructors: Crista Lopes Copyright © Instructors. 1.
Control Flow Analysis (Chapter 7) Mooly Sagiv (with Contributions by Hanne Riis Nielson)
Week 8 Recap CSE 115 Spring Composite Revisited Once we create a composite object, it can itself refer to composites. Once we create a composite.
Semantic analysis Parsing only verifies that the program consists of tokens arranged in a syntactically-valid combination, we now move on to semantic analysis,
CPSC Compiler Tutorial 8 Code Generator (unoptimized)
ECE1724F Compiler Primer Sept. 18, 2002.
1 ES 314 Advanced Programming Lec 2 Sept 3 Goals: Complete the discussion of problem Review of C++ Object-oriented design Arrays and pointers.
Data Structures Using C++ 2E
Design Synopsys System Verilog API Donations to Accellera João Geada.
Copyright © 2003 ProsoftTraining. All rights reserved. Sun Certified Java Programmer Exam Preparation Guide.
COP4020 Programming Languages
LLVM Compiler (2 of 3) Jason Dangel. Lectures High-level overview of LLVM (Katie) Walkthrough of LLVM in context of our project (Jason) –Input requirements.
Functions in C. Consider #include main() { int i; for(i=1; i
1 Session 3: Flow Control & Functions iNET Academy Open Source Web Programming.
Assemblers.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 8: Semantic Analysis and Symbol Tables.
Unit-1 Introduction Prepared by: Prof. Harish I Rathod
Fundamentals of C and C++ Programming. EEL 3801 – Lotzi Bölöni Sub-Topics  Basic Program Structure  Variables - Types and Declarations  Basic Program.
L11-12: Design Patterns Definition Iterator (L4: Inheritance)‏ Factory (L4: Inheritance)‏ Strategy (L5: Multiple Inheritance)‏ Composite (L6: Implementation.
Compiler Principles Fall Compiler Principles Lecture 0: Local Optimizations Roman Manevich Ben-Gurion University.
The LLVM Compiler Framework and Infrastructure : Compiler Design Slides by David Koes Substantial portions courtesy Chris Lattner and Vikram Adve.
Executable Statements 1. Def. Executable statements are instructions to the computer to perform specific tasks. 2. Two examples: method calls and assignment.
C++ Programming Part 2 Michael Griffiths Corporate Information and Computing Services The University of Sheffield
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Programming in C++ Michal Brabec Petr Malý. Class / Struct Class / Struct consists of: data members function members constructors destructor copy constructor.
An Introduction to Programming with C++ Sixth Edition Chapter 10 Void Functions.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 11: Functions and stack frames.
Introduction to Object-Oriented Programming Lesson 2.
Chapter 12: Programming in the Large By: Suraya Alias 1-1.
CSE 332: C++ template examples Today: Using Class and Function Templates Two examples –Function template for printing different types –Class template for.
1 Structure of a Compiler Source Language Target Language Semantic Analyzer Syntax Analyzer Lexical Analyzer Front End Code Optimizer Target Code Generator.
The LLVM Compiler Framework and Infrastructure Program Analysis Original Slides by David Koes (CMU) Substantial portions courtesy Chris Lattner and Vikram.
1 Asstt. Prof Navjot Kaur Computer Dept PRESENTED BY.
Overview of Compilation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 2.
CS 342: C++ Overloading Copyright © 2004 Dept. of Computer Science and Engineering, Washington University Overview of C++ Overloading Overloading occurs.
LLVM Simone Campanoni
R Byte Code Optimization Compiler (1) March
Lecture 12 Intermediate Code Generation Translating Expressions
Unit 10 Code Reuse. Key Concepts Abstraction Header files Implementation files Storage classes Exit function Conditional compilation Command-line arguments.
Automating Fortran - C Interoperability Prepared by: Sisi Liu (Garnet) Mentor: Dan Nagle Co-Mentor: Davide del Vento 1.
Focused obfuscation for 1-day attack delaying
Simone Campanoni CFA Simone Campanoni
Simone Campanoni LLVM Simone Campanoni
Advanced Computer Systems
Basic Program Analysis
Objectives In this chapter, you will:
Constructing Precedence Table
Chapter 10: Void Functions
C++, OBJECT ORIENTED PROGRAMMING
Simone Campanoni LLVM Simone Campanoni
CS 536 / Fall 2017 Introduction to programming languages and compilers
CSCI 161: Introduction to Programming Function
LLVM Pass and Code Instrumentation
Conversions of the type of the value of an expression
Chapter 11 Introduction to Programming in C
Control Flow Analysis CS 4501 Baishakhi Ray.
Andy Wang Object Oriented Programming in C++ COP 3330
Variables and Their scope
Chapter 11 Introduction to Programming in C
Doubly Linked List Implementation
SystemVerilog for Verification
Functional interface.
Control Flow Analysis (Chapter 7)
Introduction to LLVM (II)
HW2 – Frequent Path Loop Invariant Code Motion
Andy Wang Object Oriented Programming in C++ COP 3330
ENERGY 211 / CME 211 Lecture 8 October 8, 2008.
14 – Sequential Containers
SPL – PS1 Introduction to C++.
Presentation transcript:

Bojian Zheng CSCD70 Spring 2018 bojian@cs.toronto.edu Introduction to LLVM Bojian Zheng CSCD70 Spring 2018 bojian@cs.toronto.edu

What you will need for Assignment 1 … LLVM: How to write a pass that analyzes and transforms (optimizes) Intermediate Representation (IR). C++ Fundamentals: Public Inheritance (Abstract Class, Dynamic Casting), Iterator, STL Data Structures Prerequisite

Three-Phase Design – From Source to Binary Source Code LLVM IR LLVM IR Front End Passes Back End Object Code

Three-Phase Design – From Source to Binary C/C++ Source LLVM IR int main() { return 0; } define i32 @main() … { ret i32 0 } clang

Example – IR Optimization Suppose that we are hoping to replace every 𝑥× 2 𝑁 statement in our code with 𝑥≪𝑁. How can we achieve this? Write a Pass that does the followings: Analyzes whether there are statements of the form %𝒑= 𝐦𝐮𝐥 %𝒒, 𝟐 𝑵 in our code or not, and where are those statements located. Transforms those instructions with %𝒑= 𝐬𝐡𝐥 %𝒒, 𝑵 .

IR Optimization The IR optimizations consist of many optimization passes. LLVM itself also has passes for analysis or transformations: https://llvm.org/docs/Passes.html In this assignment, we will be making use of the mem2reg pass. Please DON’T use the LLVM passes unless otherwise told to.

Questions? Keywords: Intermediate Representation (IR) Optimization Pass Analysis & Transformation

Analysis

How to write an analysis pass? We need to understand the following three things: Program Structure: How is our program represented in LLVM? Iterators: How to traverse through such structures? Downcasting: How to retrieve more information from iterators? LLVM Pass Interface: Implement LLVM interface.

Program Structure It is important that we understand how our programs are represented after being translated by the LLVM frontend clang:

Program Structure C/C++ Source LLVM IR Source File Function Code Block Statement Module contains Functions and Global Variables. Function contains Basic Blocks and Arguments. Basic Block contains a list of Instructions. Instruction is an Opcode plus vector of Operands.

Program Structure A Simplified View (for Understanding ONLY): typedef std::vector < Function > Module; typedef std::vector < BasicBlock > Function; typedef std::vector < Instruction > BasicBlock; typedef std::vector < Operand > Instruction;

How to iterate through the Structures? Iterators! Recall how you traverse through std::vector std::vector < unsigned > vec; for (auto iter = vec.begin(); iter != vec.end(); ++iter) {/* do something */}

How to iterate through the Structures? Similarly, … Module M; for (auto iter = M.begin(); iter != M.end(); ++iter) {/* do something */}

Downcasting – Getting More Details Suppose that we have an instruction, how can we know whether it is an unary instruction? a binary instruction? a call instruction? … Dynamic Casting! Consider the statement UnaryInstruction * unary_inst = dyn_cast < UnaryInstruction > (inst);

LLVM Pass Interface LLVM Interface Implementation class ModulePass { bool runOnModule (Module & M) = 0; }; class MyModulePass : public ModulePass { bool runOnModule (Module & M) for (iter = … } };

Questions? Keywords: Program Structure Iterators Downcasting LLVM Pass Interface

Transformations

Insert/Remove/Move/Replace Instructions Three Options Instruction class methods: insertBefore(), insertAfter(), moveBefore(), moveAfter(), eraseFromParent(), removeFromParent(), … Ask parent (BasicBlock) to do this: inst.getParent()->getInstList() .insert/erase/remove/…() Make use of BasicBlockUtils (defined in header llvm/Transforms/Utils/BasicBlockUtils.h): ReplaceInstWithValue(), ReplaceInstwithInst()