Machine Independent Optimizations Topics Code motion Reduction in strength Common subexpression sharing.

Slides:



Advertisements
Similar presentations
Code Optimization and Performance Chapter 5 CS 105 Tour of the Black Holes of Computing.
Advertisements

Carnegie Mellon Today Program optimization  Optimization blocker: Memory aliasing  Out of order processing: Instruction level parallelism  Understanding.
7. Optimization Prof. O. Nierstrasz Lecture notes by Marcus Denker.
Program Optimization (Chapter 5)
Programming and Data Structure
1 Code Optimization Code produced by compilation algorithms can often be improved (ideally optimized) in terms of run-time speed and the amount of memory.
1 Code Optimization(II). 2 Outline Understanding Modern Processor –Super-scalar –Out-of –order execution Suggested reading –5.14,5.7.
Winter 2015 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University Program Optimization.
Fall 2011SYSC 5704: Elements of Computer Systems 1 SYSC 5704 Elements of Computer Systems Optimization to take advantage of hardware.
Carnegie Mellon 1 Program Optimization : Introduction to Computer Systems 25 th Lecture, Nov. 23, 2010 Instructors: Randy Bryant and Dave O’Hallaron.
Inline Assembly Section 1: Recitation 7. In the early days of computing, most programs were written in assembly code. –Unmanageable because No type checking,
Code Optimization I September 24, 2007 Topics Machine-Independent Optimizations Basic optimizations Optimization blockers class08.ppt F’07.
9. Optimization Marcus Denker. 2 © Marcus Denker Optimization Roadmap  Introduction  Optimizations in the Back-end  The Optimizer  SSA Optimizations.
Code Optimization: Machine Independent Optimizations Feb 12, 2004 Topics Machine-Independent Optimizations Machine-Dependent Opts Understanding Processor.
Code Optimization I: Machine Independent Optimizations Sept. 26, 2002 Topics Machine-Independent Optimizations Code motion Reduction in strength Common.
1 Lecture 7: Computer Arithmetic Today’s topics:  Chapter 2 wrap-up  Numerical representations  Addition and subtraction Reminder: Assignment 3 will.
Architecture Basics ECE 454 Computer Systems Programming
Code Optimization 1. Outline Machine-Independent Optimization –Code motion –Memory optimization Suggested reading –5.1 ~
Topic #10: Optimization EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
IT253: Computer Organization Lecture 4: Instruction Set Architecture Tonga Institute of Higher Education.
Cache Performance Metrics
1 Code Optimization. 2 Outline Machine-Independent Optimization –Code motion –Memory optimization Suggested reading –5.2 ~ 5.6.
Recitation 7: 10/21/02 Outline Program Optimization –Machine Independent –Machine Dependent Loop Unrolling Blocking Annie Luo
University of Amsterdam Computer Systems – optimizing program performance Arnoud Visser 1 Computer Systems Optimizing program performance.
1 Code optimization “Code optimization refers to the techniques used by the compiler to improve the execution efficiency of the generated object code”
Introduction to Computer Systems Topics: Theme Five great realities of computer systems (continued) “The class that bytes”
University of Amsterdam Computer Systems – optimizing program performance Arnoud Visser 1 Computer Systems Optimizing program performance.
Code Optimization and Performance Chapters 5 and 9 perf01.ppt CS 105 “Tour of the Black Holes of Computing”
Chapter 10 Code Optimization Zhang Jing, Wang HaiLing College of Computer Science & Technology Harbin Engineering University.
Computer Organization II Topics: Theme Five great realities of computer systems How this fits within CS curriculum CS 2733.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Code Optimization II: Machine Dependent Optimization Topics Machine-Dependent Optimizations Unrolling Enabling instruction level parallelism.
Code Optimization and Performance CS 105 “Tour of the Black Holes of Computing”
Programming for Performance CS 740 Oct. 4, 2000 Topics How architecture impacts your programs How (and how not) to tune your code.
Machine-Dependent Optimization CS 105 “Tour of the Black Holes of Computing”
1 Code Optimization. 2 Outline Machine-Independent Optimization –Code motion –Memory optimization Suggested reading –5.2 ~ 5.6.
Instruction Sets. Instruction set It is a list of all instructions that a processor can execute. It is a list of all instructions that a processor can.
Code Optimization and Performance I Chapter 5 perf01.ppt CS 105 “Tour of the Black Holes of Computing”
©SoftMoore ConsultingSlide 1 Code Optimization. ©SoftMoore ConsultingSlide 2 Code Optimization Code generation techniques and transformations that result.
Code Optimization Code produced by compilation algorithms can often be improved (ideally optimized) in terms of run-time speed and the amount of memory.
Code Optimization Overview and Examples
Computer Organization and Design Pointers, Arrays and Strings in C
Code Optimization.
Today’s Instructor: Phil Gibbons
Optimization Code Optimization ©SoftMoore Consulting.
CS 3114 Many of the following slides are taken with permission from
Code Optimization I: Machine Independent Optimizations
Instructors: Dave O’Hallaron, Greg Ganger, and Greg Kesden
Program Optimization CENG331 - Computer Organization
Instructors: Pelin Angin and Erol Sahin
Program Optimization CSCE 312
Code Optimization II: Machine Dependent Optimizations
Machine-Dependent Optimization
Code Optimization /18-213/14-513/15-513: Introduction to Computer Systems 10th Lecture, September 27, 2018.
Introduction to Computer Systems
Code Optimization and Performance
Code Optimization I: Machine Independent Optimizations Feb 11, 2003
Code Optimization(II)
Code Optimization April 6, 2000
Code Optimization I Nov. 25, 2008
COMP 2130 Intro Computer Systems Thompson Rivers University
Optimizing program performance
Cache Memories Lecture, Oct. 30, 2018
Program Optimization CSE 238/2038/2138: Systems Programming
CS 3114 Many of the following slides are taken with permission from
Machine-Independent Optimization
Lecture 11: Machine-Dependent Optimization
Code Optimization and Performance
Code Optimization II October 2, 2001
Presentation transcript:

Machine Independent Optimizations Topics Code motion Reduction in strength Common subexpression sharing

– 2 – Great Reality There’s more to performance than asymptotic complexity Constant factors matter too! Easily see 10:1 performance range depending on how code is written Must optimize at multiple levels: algorithm, data representations, procedures, and loops Must understand system to optimize performance How programs are compiled and executed How to measure program performance and identify bottlenecks How to improve performance without destroying code modularity and generality

– 3 – Machine-Independent Optimizations Optimizations you should do regardless of processor / compiler Code Motion Reduce frequency with which computation performed If it will always produce same result Especially moving code out of loop for (i = 0; i < n; i++) for (j = 0; j < n; j++) a[n*i + j] = b[j];

– 4 – Machine-Independent Optimizations Optimizations you should do regardless of processor / compiler Code Motion Reduce frequency with which computation performed If it will always produce same result Especially moving code out of loop for (i = 0; i < n; i++) for (j = 0; j < n; j++) a[n*i + j] = b[j];

– 5 – Reduction in Strength Replace costly operation with simpler one Shift, add instead of multiply or divide 16*x-->x << 4 Utility machine dependent Depends on cost of multiply or divide instruction On Pentium II or III, integer multiply only requires 4 CPU cycles Recognize sequence of products for (i = 0; i < n; i++) for (j = 0; j < n; j++) a[n*i + j] = b[j];

– 6 – Reduction in Strength Replace costly operation with simpler one Shift, add instead of multiply or divide 16*x-->x << 4 Utility machine dependent Depends on cost of multiply or divide instruction On Pentium II or III, integer multiply only requires 4 CPU cycles Recognize sequence of products for (i = 0; i < n; i++) for (j = 0; j < n; j++) a[n*i + j] = b[j];

– 7 – Make Use of Registers Reading and writing registers much faster than reading/writing memoryLimitation Compiler not always able to determine whether variable can be held in register Possibility of Aliasing

– 8 – Machine-Independent Opts. (Cont.) Share Common Subexpressions Reuse portions of expressions Compilers often not very sophisticated in exploiting arithmetic properties /* Sum neighbors of i,j */ up = val[(i-1)*n + j]; down = val[(i+1)*n + j]; left = val[i*n + j-1]; right = val[i*n + j+1]; sum = up + down + left + right; 3 multiplications: i*n, (i–1)*n, (i+1)*n1 multiplication:

– 9 – Machine-Independent Opts. (Cont.) Share Common Subexpressions Reuse portions of expressions Compilers often not very sophisticated in exploiting arithmetic properties /* Sum neighbors of i,j */ up = val[(i-1)*n + j]; down = val[(i+1)*n + j]; left = val[i*n + j-1]; right = val[i*n + j+1]; sum = up + down + left + right; 3 multiplications: i*n, (i–1)*n, (i+1)*n1 multiplication:

– 10 – Assume Vector ADT Procedures vec_ptr new_vec(int len) Create vector of specified length int get_vec_element(vec_ptr v, int index, int *dest) Retrieve vector element, store at *dest Return 0 if out of bounds, 1 if successful int *get_vec_start(vec_ptr v) Return pointer to start of vector data Similar to array implementations in Java E.g., always do bounds checking length data      012length–1

– 11 – Optimization Example Procedure Compute sum of all elements of vector Store result at destination location void combine1(vec_ptr v, int *dest) { int i; *dest = 0; for (i = 0; i < vec_length(v); i++) { int val; get_vec_element(v, i, &val); *dest += val; }

– 12 – Reminder: Cycles Per Element Convenient way to express performance of program that operators on vectors or lists Length = n T = CPE*n + Overhead vsum1 Slope = 4.0 vsum2 Slope = 3.5

– 13 – Optimization Example Procedure Compute sum of all elements of integer vector Store result at destination location Vector data structure and operations defined via abstract data type Pentium II/III Performance: Clock Cycles / Element (Compiled -g)31.25 (Compiled -O2) void combine1(vec_ptr v, int *dest) { int i; *dest = 0; for (i = 0; i < vec_length(v); i++) { int val; get_vec_element(v, i, &val); *dest += val; }

– 14 – Loop Invariant Code Motion Optimization CPE: (Compiled -O2) vec_length requires only constant time, but significant overhead void combine2(vec_ptr v, int *dest) { int i; *dest = 0; for (i = 0; i < vec_length(v); i++) { int val; get_vec_element(v, i, &val); *dest += val; }

– 15 – void lower(char *s) { int i; for (i = 0; i < strlen(s); i++) if (s[i] >= 'A' && s[i] <= 'Z') s[i] -= ('A' - 'a'); } Code Motion Example #2 Procedure to Convert String to Lower Case

– 16 – Lower Case Conversion Performance Time quadruples when double string length Quadratic performance

– 17 – Improving Performance Move call to strlen outside of loop Since result does not change from one iteration to another Form of code motion void lower(char *s) { int i; int len = strlen(s); for (i = 0; i < len; i++) if (s[i] >= 'A' && s[i] <= 'Z') s[i] -= ('A' - 'a'); }

– 18 – Lower Case Conversion Performance Time doubles when double string length Linear performance

– 19 – Optimization Blocker: Procedure Calls Why couldn’t the compiler move vec_len or strlen out of the inner loop? Procedure may have side effects Alters global state each time called Function may not return same value for given arguments Depends on other parts of global state Why doesn’t compiler look at code for vec_len or strlen ? Linker may overload with different version Unless declared static Interprocedural optimization is not used extensively due to costWarning: Compiler treats procedure call as a black box Weak optimizations in and around them

– 20 – Replace func call wt Direct Access accumulatorOptimization CPE: 6.00 (Compiled -O2) Procedure calls are expensive! Bounds checking is expensive void combine3(vec_ptr v, int *dest) { int i; int length = vec_length(v); int *data = get_vec_start(v); }

– 21 – Direct Access Optimization CPE: 6.00 (Compiled -O2) Procedure calls are expensive! Bounds checking is expensive void combine3(vec_ptr v, int *dest) { int i; int length = vec_length(v); int *data = get_vec_start(v); }

– 22 – Eliminate Unneeded Memory Refs Optimization Don’t need to store in destination until end Local variable sum, called “accumulator var”, held in register Avoids 1 memory read, 1 memory write per cycle CPE: 2.00 (Compiled -O2) Memory references are expensive! void combine4(vec_ptr v, int *dest) { int i; int length = vec_length(v); int *data = get_vec_start(v); }

– 23 – Detecting Unneeded Memory Refs..L18: movl (%ecx,%edx,4),%eax addl %eax,(%edi) incl %edx cmpl %esi,%edx jl.L18 Combine3.L24: addl (%eax,%edx,4),%ecx incl %edx cmpl %esi,%edx jl.L24 Combine4

– 24 – Optimization Blocker: Memory Aliasing Aliasing Two different memory references specify single locationExampleObservations Easy to have happen in C Since allowed to do address arithmetic Direct access to storage structures Get in habit of introducing local variables Accumulating within loops Your way of telling compiler not to check for aliasing

– 25 – Machine-Independent Opt. Summary Code Motion Compilers are good at this for simple loop/array structures Don’t do well in presence of procedure calls and memory aliasing Reduction in Strength Shift, add instead of multiply or divide compilers are (generally) good at this Exact trade-offs machine-dependent Keep data in registers rather than memory compilers are not good at this, since concerned with aliasing Share Common Subexpressions compilers have limited algebraic reasoning capabilities

– 26 –

– 27 –

– 28 –

– 29 –

– 30 –