Docsity

Optimizing Compiler Performance: Techniques and Limitations, Papers of Computer Science

Partial preview of the text

Download Optimizing Compiler Performance: Techniques and Limitations and more Papers Computer Science in PDF only on Docsity!

CS 201

Code Optimization,

Part 1

Gerson Robboy

Portland State University

There’s more to performance than asymptotic complexity

Constant factors matter too! Constant factors matter too!

 Factor of 10 improvement is possible depending on how

code is written

 Must optimize at multiple levels:

algorithm, data representations, procedures, and loops

Must understand system to optimize performance 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

Limitations of Optimizing Compilers

Fundamental Constraint: Fundamental Constraint:

 Must not cause any change in program behavior under any

possible condition

 Even pathological conditions.

Most analysis is performed only within procedures Most analysis is performed only within procedures

 whole-program analysis is too expensive

Most analysis is based only on Most analysis is based only on static static informationinformation

 The compiler does’t anticipate run-time inputs

 The programmer knows more about constraints on the data

than the compiler.

When in doubt, the compiler must be conservative When in doubt, the compiler must be conservative

Compiler-Generated Code Motion

 Most compilers do a good job with array code and simple

loop structures

Code Generated by GCC Code Generated by GCC

for (i = 0; i < n; i++) for (j = 0; j < n; j++) a[ni + j] = b[j]; imull %ebx,%eax # in movl 8(%ebp),%edi # a leal (%edi,%eax,4),%edx # p = a+i*n (scaled by 4)

Inner Loop

.L40: movl 12(%ebp),%edi # b movl (%edi,%ecx,4),%eax # b+j (scaled by 4) movl %eax,(%edx) # *p = b[j] addl $4,%edx # p++ (scaled by 4) incl %ecx # j++ jl .L40 # loop if j

Reduction in

Strength

int foo(int a[], int b[], int n) { int i, j; for (i = 0; i < n;i++) for (j = 0; j < n; j++) a[n*i + j]=b[j]; } movl 16(%ebp), %ebx xorl %esi, %esi cmpl %ebx, %esi jge .L movl $0, -16(%ebp) .L9: xorl %ecx, %ecx cmpl %ebx, %ecx jge .L movl -16(%ebp), %eax movl 8(%ebp), %edi leal (%edi,%eax,4), %edx .L8: movl 12(%ebp), %edi movl (%edi,%ecx,4), %eax incl %ecx movl %eax, (%edx) addl $4, %edx cmpl %ebx, %ecx jl .L .L13: incl %esi addl %ebx, -16(%ebp) cmpl %ebx, %esi jl .L .L11: # all done Write C code to show what the compiler generated.

Make Use of Registers

 Reading and writing registers is much faster than

reading/writing memory

Limitation Limitation

 Compiler not always able to determine whether variable can

be held in register

Possibility of Aliasing

 See example later

Another limitation in the case of Intel processors Another limitation in the case of Intel processors

 Almost no registers

 You have to make use of cache

Example

Data type: vector, illustrated above Data type: vector, illustrated above

Procedures 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 destReturn 0 if out of bounds, 1 if successful int *get_vec_start(vec_ptr v)  Return pointer to start of vector data

 Structured programming

Hide the implementation of the arrayAlways do bounds checking length data • • • 0 1 2 length– 1

Optimization Example

Procedure 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 Pentium II/III Performance: Clock Cycles / Element

 42.06 (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; } }

Exercise

Write a function Write a function combine2 combine2 that does the same thing asthat does the same thing as

combine1,combine1, without calling vec_length on eachwithout calling vec_length on each

iteration. iteration.

Move vec_length Call Out of Loop

Optimization Optimization

 Move call to vec_length out of inner loop

Value does not change from one iteration to nextCode motion

 CPE: 20.66 (Compiled -O2)

 vec_length requires only constant time, but significant overhead

Lower Case Conversion Performance

 Time quadruples when double string length

 Quadratic performance

lower 1

  1. 0001
  2. 001
  3. 01
  4. 1 1 10 100 1000 256 512 1024 2048 4096 8192 16384 32768 65536 131072 262144 String Length CPU Seconds

Exercise

 Why is the time proportional to the square of the string

length?

 How can you optimize the function to make it linear?

 Write the optimized code.

 Why can’t the compiler do that optimization?

Optimization Blocker: Procedure Calls

Compiler treats procedure call as a black box Compiler treats procedure call as a black box

 Weak optimizations in and around them

Why? Why?

Reduction in Strength

Optimization Optimization

 Avoid procedure call to retrieve each vector element

Get pointer to start of array before loopNot as clean in terms of data abstraction » Makes assumption about what a vector looks like internally

 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); *dest = 0; for (i = 0; i < length; i++) { *dest += data[i]; }