Computer Science 313 – Advanced Programming Topics.

Slides:



Advertisements
Similar presentations
compilers and interpreters
Advertisements

Code Optimization and Performance Chapter 5 CS 105 Tour of the Black Holes of Computing.
Computer Science 313 – Advanced Programming Topics.
ECE 454 Computer Systems Programming Compiler and Optimization (I) Ding Yuan ECE Dept., University of Toronto
The Assembly Language Level
Program Representations. Representing programs Goals.
Programming Logic and Design, Third Edition Comprehensive
COEN Expressions and Assignment
Programming Concept #2 Iteration. Is just a fancy way of saying that you would like something to repeat more than one time. It is used in any modern programming.
Example in SSA X := Y op Z in out F X := Y op Z (in) = in [ { X ! Y op Z } X :=  (Y,Z) in 0 out F X :=   (in 0, in 1 ) = (in 0 Å in 1 ) [ { X ! E |
the fourth iteration of this loop is shown here
Faculty of Sciences and Social Sciences HOPE Java: Loops within loops Stewart Blakeway FML 213
Recap from last time We were trying to do Common Subexpression Elimination Compute expressions that are available at each program point.
1 Intermediate representation Goals: –encode knowledge about the program –facilitate analysis –facilitate retargeting –facilitate optimization scanning.
1 Parts of a Loop (reminder) Every loop will always contain three main elements: –Priming: initialize your variables. –Testing: test against some known.
Loop invariant detection using SSA An expression is invariant in a loop L iff: (base cases) –it’s a constant –it’s a variable use, all of whose single.
CS 412/413 Spring 2007Introduction to Compilers1 Lecture 29: Control Flow Analysis 9 Apr 07 CS412/413 Introduction to Compilers Tim Teitelbaum.
Class canceled next Tuesday. Recap: Components of IR Control dependencies: sequencing of operations –evaluation of if & then –side-effects of statements.
U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE Emery Berger University of Massachusetts, Amherst Advanced Compilers CMPSCI 710.
1 Copy Propagation What does it mean? – Given an assignment x = y, replace later uses of x with uses of y, provided there are no intervening assignments.
Common Sub-expression Elim Want to compute when an expression is available in a var Domain:
Recap from last time: live variables x := 5 y := x + 2 x := x + 1 y := x y...
Miscellaneous topicsCS-2301 B-term Miscellaneous Topics CS-2301, System Programming for Non-majors (Slides include materials from The C Programming.
An Object-Oriented Approach to Programming Logic and Design Chapter 6 Looping.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
CSC 313 – Advanced Programming Topics. Why Recurse?  Recursion is useful, powerful technique  Often yields compact, easy-to-read code  Highlights all.
Compiler Code Optimizations. Introduction Introduction Optimized codeOptimized code Executes faster Executes faster efficient memory usage efficient memory.
Types for Programs and Proofs Lecture 1. What are types? int, float, char, …, arrays types of procedures, functions, references, records, objects,...
What’s in an optimizing compiler?
CPS120 Introduction to Computer Science Iteration (Looping)
Question of the Day  On a game show you’re given the choice of three doors: Behind one door is a car; behind the others, goats. After you pick a door,
CS 320 Assignment 1 Rewriting the MISC Osystem class to support loading machine language programs at addresses other than 0 1.
 2007 Pearson Education, Inc. All rights reserved C Functions -Continue…-
Computer Science 313 – Advanced Programming Topics.
CPSC 388 – Compiler Design and Construction Optimization.
CPS120: Introduction to Computer Science Lecture 14 Functions.
U NIVERSITY OF D ELAWARE C OMPUTER & I NFORMATION S CIENCES D EPARTMENT Optimizing Compilers CISC 673 Spring 2011 Dependence Analysis and Loop Transformations.
Problem of the Day  I am thinking of a question and propose 3 possible answers. Exactly one of the following is the solution. Which is it? A. Answer 1.
High-Level Transformations for Embedded Computing
Computer Science 313 – Advanced Programming Topics.
8-1 Compilers Compiler A program that translates a high-level language program into machine code High-level languages provide a richer set of instructions.
FOUNDATION IN INFORMATION TECHNOLOGY (CS-T-101) TOPIC : INFORMATION SYSTEM – SOFTWARE.
CMSC 104, Version 9/011 More Loops Topics Counter-Controlled (Definite) Repetition Event-Controlled (Indefinite) Repetition for Loops do-while Loops Choosing.
12/18/2015© Hal Perkins & UW CSET-1 CSE P 501 – Compilers Loops Hal Perkins Autumn 2009.
CPS120 Introduction to Computer Science Iteration (Looping)
Compiler Optimizations ECE 454 Computer Systems Programming Topics: The Role of the Compiler Common Compiler (Automatic) Code Optimizations Cristiana Amza.
1 09/27/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Georgia Institute of Technology Speed part 4 Barb Ericson Georgia Institute of Technology May 2006.
CSC 213 – Large Scale Programming. Today’s Goal  Understand why testing code is important  Result of poor or no testing & embarrassment caused  Learn.
Computer Science 313 – Advanced Programming Topics.
3/2/2016© Hal Perkins & UW CSES-1 CSE P 501 – Compilers Optimizing Transformations Hal Perkins Autumn 2009.
CSC 212 – Data Structures Lecture 15: Big-Oh Notation.
LECTURE 22: BIG-OH COMPLEXITY CSC 212 – Data Structures.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
©SoftMoore ConsultingSlide 1 Code Optimization. ©SoftMoore ConsultingSlide 2 Code Optimization Code generation techniques and transformations that result.
CS 412/413 Spring 2005Introduction to Compilers1 CS412/CS413 Introduction to Compilers Tim Teitelbaum Lecture 30: Loop Optimizations and Pointer Analysis.
Computing & Information Sciences Kansas State University Lecture 42 of 42 CIS 732 Machine Learning & Pattern Recognition Lecture 42 of 42 Genetic Programming.
Introduction to Analysis of Algorithms
Introduction to complexity
Simone Campanoni Loop transformations Simone Campanoni
ACOE301: Computer Architecture II Labs
Princeton University Spring 2016
Repeating Instructions And Advance collection
Optimizing Transformations Hal Perkins Autumn 2011
Compiler Code Optimizations
Chapter 8: More on the Repetition Structure
Copyright 2003, Keith D. Cooper & Linda Torczon, all rights reserved.
Tonga Institute of Higher Education IT 141: Information Systems
Tonga Institute of Higher Education IT 141: Information Systems
Introduction to Optimization
Presentation transcript:

Computer Science 313 – Advanced Programming Topics

Loops Matter  Computers often used for repetitive analyses  Machines speed & memory advantageous for this  Not hurt by its lack of common sense  “Repetitive analyses” means loops  Loops occur everywhere in computer science  Performing statistical analyses on data  Database processing for result analyses  Evaluating results of large system simulation  Redrawing detailed pictures from WoW

Programs without Loops  What do we write that does NOT use loops?

Optimizing Loops Useful  Big differences from small change to loop  Size unimportant; time spent in execution important  Repeated execute the code in the loop  Even small changes become greatly magnified  Compiler limited in how it optimizes loops  Often lacks precise knowledge of how things work  Languages prevent optimizations across iterations  Non-inlined method calls cannot be optimize  Cannot optimize uses of an object or field

Simple Example  Consider following loop  Calls size() at start of each iteration  Calls get() within body of the loop int i; int retVal = 0; for (i = 0; i < list.size(); i++){ retVal += list.get(i); }

Minor Change  Make following changes which have little impact  Calls size() at start of loop, but not within iteration  Calls get() within body of the loop int i = list.size() - 1; int retVal = 0; for (; i >= 0; i--){ retVal += list.get(i); }

Another Small Change  Loop counts up, not down, in this version  Calls size() at start of loop, but not within iteration  Calls get() within body of the loop int end = list.size(); int retVal = 0; for (int i = 0; i < end; i++){ retVal += list.get(i); }

Little Odder Change  Limit iterations needed to complete loop  Calls size() at start of loop, but not within iteration  Calls get() within body of the loop int end = list.size(); int retVal, tmp1 = 0, tmp2 = 0; for (int i = 0; i < end; i+=2){ tmp1 += list.get(i); tmp2 += list.get(i + 1); } retVal = tmp1 + tmp2;

Execution Time of Each Loop

Reason for the 2 Big Drops  Biggest change when code moved out of loop loop hoisting loop invariant code-motion  Called loop hoisting or loop invariant code-motion  Loop hoisting done automatically by compiler  But only when it can determine optimization is safe  Need to understand how this works  Try to write code to enable optimization  Don’t write hard-to-read code duplicating optimization

Another Use of SSA Form  Method must be converted into SSA form  Find definition for each use of a variable  Instruction is loop-invariant when: t = x n  y n  x n & y n are both constant –or–  Definitions of x n & y n are outside the loop –or–  Loop-invariant instructions define x n & y n

Loop Hoisting Actions  Need location to place hoisted instructions  Where instructions moved when they get hoisted  Could try and remember where loop starts  Prepend instructions just before where loop start  Need to make sure is not included in loop  Much easier to add pre-loop header  Blank header included with all loops  Purpose is only to hold hoisted instructions

Loop-Invariant Example  Create loop header for loop  Any instruction should be mark as loop-invariant if:  Constant operands used  Operands def’d outside loop  Operands from other loop-invariant instructions x 1 = a y 1 = b 1 + x 1 z 1 = foo() if (z 1 < 45) a 1 = … b 1 = …

Loop-Invariant Example  Create loop header for loop  Any instruction should be mark as loop-invariant if:  Constant operands used  Operands def’d outside loop  Operands from other loop-invariant instructions a 1 = … b 1 = … x 1 = a y 1 = b 1 + x 1 z 1 = foo() if (z 1 < 45)

Loop-Invariant Example  Create loop header for loop  Any instruction should be mark as loop-invariant if:  Constant operands used  Operands def’d outside loop  Operands from other loop-invariant instructions a 1 = … b 1 = … y 1 = b 1 + x 1 z 1 = foo() x 1 = a if (z 1 < 45)

Loop-Invariant Example a 1 = … b 1 = … z 1 = foo() x 1 = a y 1 = b 1 + x 1 if (z 1 < 45)  Create loop header for loop  Any instruction should be mark as loop-invariant if:  Constant operands used  Operands def’d outside loop  Operands from other loop-invariant instructions

Loop-Invariant Example a 1 = … b 1 = … x 1 = a y 1 = b 1 + x 1 if (z 1 < 45) z 1 = foo() c 1 = field

Must Also Meet Conditions  Must meet conditions  Pre-header dominates hoisted instruction  Loop contains exactly one variable definition do { i = i + 1 t = a * b M[i] = t } while (i < t); x = t

Must Also Meet Conditions  Must meet conditions  Pre-header dominates hoisted instruction  Loop contains exactly one variable definition do { if (i >= 45) t = a * b i = i + 1 M[i] = t } while (i < t); x = t

Must Also Meet Conditions  Must meet conditions  Pre-header dominates hoisted instruction  Loop contains exactly one variable definition do { if (i >= 45) t = a * b else t = a + b M[i] = t } while (i < t);

Great For Nested Loops  Fairly common to work with sets of loop  Databases & scientific data especially so  Can hoist instructions:  From outer loop to outside both loops  From inner loop to outside both loops  From inner loop to only in the outer loop  Normally nested loops use arrays or objects  Use scalar replacement to solve this

Nested Loop Example  Exposes reuse of a value  Array & object uses cannot be optimized  Use of local variable can be optimized  For really impressive sounding name:  Use dependence analysis to help rewrite loops

Loop Unrolling  Unroll to reduce overhead of the loop Advantages: + Fewer instructions executed + More optimizations possible + Great for consecutive accesses Disadvantages: - Code gets bloated - Still using objects

For Next Class  Lab available on the web  Lab will be due 1 week from Friday  Read pages 385 – 399 for this Friday  Begin looking at the State pattern  Closely related to what 2 patterns already discussed?  When and where would we want to use State pattern?