+ All Categories
Home > Documents > ScalarEvolution and Loop Optimization - LLVMTripcount Analysis •Induction Variable analysis using...

ScalarEvolution and Loop Optimization - LLVMTripcount Analysis •Induction Variable analysis using...

Date post: 24-Sep-2020
Category:
Upload: others
View: 2 times
Download: 0 times
Share this document with a friend
48
ScalarEvolution and Loop Optimization 1
Transcript
Page 1: ScalarEvolution and Loop Optimization - LLVMTripcount Analysis •Induction Variable analysis using SSA •“Backedge-Taken Count” •may be an arbitrary expression •New tools:

ScalarEvolution and Loop Optimization

1

Page 2: ScalarEvolution and Loop Optimization - LLVMTripcount Analysis •Induction Variable analysis using SSA •“Backedge-Taken Count” •may be an arbitrary expression •New tools:

Loop Optimization in LLVM

• Relatively young

• Canonicalization

• Make optimizations simpler

• Make optimizations more general

2

Page 3: ScalarEvolution and Loop Optimization - LLVMTripcount Analysis •Induction Variable analysis using SSA •“Backedge-Taken Count” •may be an arbitrary expression •New tools:

Loop Canonicalizations

• Natural Loops

• Loop Rotation

• LoopSimplify form

• LCSSA form

• “Loop-Closed SSA”

• Identifies loop exit values

3

Page 4: ScalarEvolution and Loop Optimization - LLVMTripcount Analysis •Induction Variable analysis using SSA •“Backedge-Taken Count” •may be an arbitrary expression •New tools:

Loop Rotationfor (i = 0; i < n; ++i)C code:

4

Page 5: ScalarEvolution and Loop Optimization - LLVMTripcount Analysis •Induction Variable analysis using SSA •“Backedge-Taken Count” •may be an arbitrary expression •New tools:

Loop Rotationfor (i = 0; i < n; ++i)

i = 0;while (true) { if (i >= n) break; ... ++i;}

C code:

Typical Lowering:

4

Page 6: ScalarEvolution and Loop Optimization - LLVMTripcount Analysis •Induction Variable analysis using SSA •“Backedge-Taken Count” •may be an arbitrary expression •New tools:

Loop Rotationfor (i = 0; i < n; ++i)

i = 0;while (true) { if (i >= n) break; ... ++i;}

C code:

Typical Lowering:

Branch

Branch

Trip Count = n + 1

4

Page 7: ScalarEvolution and Loop Optimization - LLVMTripcount Analysis •Induction Variable analysis using SSA •“Backedge-Taken Count” •may be an arbitrary expression •New tools:

Loop Rotationfor (i = 0; i < n; ++i)

i = 0;while (true) { if (i >= n) break; ... ++i;}

C code:

Typical Lowering:

i = n;if (i > 0) { do { ... ++i; } while (i < n);}

After Rotation:

Branch

Branch

Branch

Branch

Trip Count = n + 1 Trip Count = n

4

Page 8: ScalarEvolution and Loop Optimization - LLVMTripcount Analysis •Induction Variable analysis using SSA •“Backedge-Taken Count” •may be an arbitrary expression •New tools:

Header

Latch

Latch

Backedge

Backedge

Exit

Exit

5

Page 9: ScalarEvolution and Loop Optimization - LLVMTripcount Analysis •Induction Variable analysis using SSA •“Backedge-Taken Count” •may be an arbitrary expression •New tools:

Header

Latch

Latch

Backedge

Backedge

Exit

Exit

Natural Loop

5

Page 10: ScalarEvolution and Loop Optimization - LLVMTripcount Analysis •Induction Variable analysis using SSA •“Backedge-Taken Count” •may be an arbitrary expression •New tools:

Header

Latch

Latch

Backedge

Backedge

Preheader

Exit

Exit

Natural Loop

5

Page 11: ScalarEvolution and Loop Optimization - LLVMTripcount Analysis •Induction Variable analysis using SSA •“Backedge-Taken Count” •may be an arbitrary expression •New tools:

Header

Latch

Latch

Backedge

Backedge

Preheader

Exit

Exit

Natural Loop

5

Page 12: ScalarEvolution and Loop Optimization - LLVMTripcount Analysis •Induction Variable analysis using SSA •“Backedge-Taken Count” •may be an arbitrary expression •New tools:

Header

Latch

Latch

Backedge

Preheader

Exit

Exit

Header

Single

Natural LoopLoopSimplify Form

5

Page 13: ScalarEvolution and Loop Optimization - LLVMTripcount Analysis •Induction Variable analysis using SSA •“Backedge-Taken Count” •may be an arbitrary expression •New tools:

Header

Latch

Latch

Backedge

Preheader

Exit

Exit

Header

Single

Natural LoopLoopSimplify Form

5

Page 14: ScalarEvolution and Loop Optimization - LLVMTripcount Analysis •Induction Variable analysis using SSA •“Backedge-Taken Count” •may be an arbitrary expression •New tools:

Header

Latch

Latch

Backedge

Preheader

Exit

Exit

Header

Single

Phi

Natural LoopLoopSimplify Form

LCSSA Form

Phi

5

Page 15: ScalarEvolution and Loop Optimization - LLVMTripcount Analysis •Induction Variable analysis using SSA •“Backedge-Taken Count” •may be an arbitrary expression •New tools:

Loops in LLVM IR

header: %i = phi i64 [ 0, %preheader ], [ %next, %backedge ] ... %p = getelementptr @A, 0, %i %a = load float* %p ...latch: %next = add i64 %i, 1 %cmp = icmp slt %next, %N br i1 %cmp, label %header, label %exit

6

Page 16: ScalarEvolution and Loop Optimization - LLVMTripcount Analysis •Induction Variable analysis using SSA •“Backedge-Taken Count” •may be an arbitrary expression •New tools:

Loops in LLVM IR

header: %i = phi i64 [ 0, %preheader ], [ %next, %backedge ] ... %p = getelementptr @A, 0, %i %a = load float* %p ...latch: %next = add i64 %i, 1 %cmp = icmp slt %next, %N br i1 %cmp, label %header, label %exit

?

6

Page 17: ScalarEvolution and Loop Optimization - LLVMTripcount Analysis •Induction Variable analysis using SSA •“Backedge-Taken Count” •may be an arbitrary expression •New tools:

{@A,+,sizeof(float)}<%header>

Loops in LLVM IR

header: %i = phi i64 [ 0, %preheader ], [ %next, %backedge ] ... %p = getelementptr @A, 0, %i %a = load float* %p ...latch: %next = add i64 %i, 1 %cmp = icmp slt %next, %N br i1 %cmp, label %header, label %exit

Start Stride Loop

6

Page 18: ScalarEvolution and Loop Optimization - LLVMTripcount Analysis •Induction Variable analysis using SSA •“Backedge-Taken Count” •may be an arbitrary expression •New tools:

ScalarEvolution

• An analysis Pass

• Understand loop-oriented expressions, “scalars” whose values may evolve as loops iterate

• Map from Value to SCEV

• Loop trip-count analysis

7

Page 19: ScalarEvolution and Loop Optimization - LLVMTripcount Analysis •Induction Variable analysis using SSA •“Backedge-Taken Count” •may be an arbitrary expression •New tools:

How does LLVM use ScalarEvolution today?• IndVarSimplify (IndVars)

• prepare loops for advanced optimizations

• expose trip counts

• promote induction variables

• rewrite exit values

• LoopStrengthReduce (LSR)

• prepare loops for efficient execution

8

Page 20: ScalarEvolution and Loop Optimization - LLVMTripcount Analysis •Induction Variable analysis using SSA •“Backedge-Taken Count” •may be an arbitrary expression •New tools:

IndVars vs. LSRfor (i = 0; i < n; i += 2) ... = p[i];

9

Page 21: ScalarEvolution and Loop Optimization - LLVMTripcount Analysis •Induction Variable analysis using SSA •“Backedge-Taken Count” •may be an arbitrary expression •New tools:

IndVars vs. LSRfor (i = 0; i < n; i += 2) ... = p[i];

Indvars sets up a canonical induction variable:

for (i = 0; i != n; ++i) ... = p[i*2];

9

Page 22: ScalarEvolution and Loop Optimization - LLVMTripcount Analysis •Induction Variable analysis using SSA •“Backedge-Taken Count” •may be an arbitrary expression •New tools:

IndVars vs. LSRfor (i = 0; i < n; i += 2) ... = p[i];

Indvars sets up a canonical induction variable:

for (i = 0; i != n; ++i) ... = p[i*2];

for (i = 0; i != n; i += 2) ... = p[i];

LSR eliminates the multiplication in the loop:

9

Page 23: ScalarEvolution and Loop Optimization - LLVMTripcount Analysis •Induction Variable analysis using SSA •“Backedge-Taken Count” •may be an arbitrary expression •New tools:

Tripcount Analysis

• Induction Variable analysis using SSA

• “Backedge-Taken Count”

• may be an arbitrary expression

• New tools: nsw, nuw, inbounds

•for (i = a; i < b; i += c)

• (b-a)/c ?

• what if i is an “int” on a 64-bit target?10

Page 24: ScalarEvolution and Loop Optimization - LLVMTripcount Analysis •Induction Variable analysis using SSA •“Backedge-Taken Count” •may be an arbitrary expression •New tools:

What’s a SCEV?

• “SCalar EVolution” expression

• + * / sext zext trunc smax umax

• Constant, Sizeof, Alignof

• Unknown Value

• Add Recurrences (AddRecs)

11

Page 25: ScalarEvolution and Loop Optimization - LLVMTripcount Analysis •Induction Variable analysis using SSA •“Backedge-Taken Count” •may be an arbitrary expression •New tools:

What’s a SCEV?

• “SCalar EVolution” expression

• + * / sext zext trunc smax umax

• Constant, Sizeof, Alignof

• Unknown Value

• Add Recurrences (AddRecs)

11

Page 26: ScalarEvolution and Loop Optimization - LLVMTripcount Analysis •Induction Variable analysis using SSA •“Backedge-Taken Count” •may be an arbitrary expression •New tools:

A simple example

define void @foo(i64 %a, i64 %b, i64 %c) { %t0 = add i64 %b, %a %t1 = add i64 %t0, 7 %t2 = add i64 %t1, %c ret i64 %t2}

12

Page 27: ScalarEvolution and Loop Optimization - LLVMTripcount Analysis •Induction Variable analysis using SSA •“Backedge-Taken Count” •may be an arbitrary expression •New tools:

A simple example

define void @foo(i64 %a, i64 %b, i64 %c) { %t0 = add i64 %b, %a %t1 = add i64 %t0, 7 %t2 = add i64 %t1, %c ret i64 %t2}

(7 + %a + %b +%c)

12

Page 28: ScalarEvolution and Loop Optimization - LLVMTripcount Analysis •Induction Variable analysis using SSA •“Backedge-Taken Count” •may be an arbitrary expression •New tools:

define double*@bar([10 x double]* %a, i64 %b, i64 %c) { %bx3 = mul i64 %b, 3 %bx3a7 = add i64 %bx3, 7 %ca5 = add i64 %c, 5 %z = getelementptr [10 x double]* %a, i64 %bx3a7, i64 %ca5 ret double* %z }

double *bar(double a[10][10], long b, long c) { return &a[b * 3 + 7][c + 5];}

13

Page 29: ScalarEvolution and Loop Optimization - LLVMTripcount Analysis •Induction Variable analysis using SSA •“Backedge-Taken Count” •may be an arbitrary expression •New tools:

define double*@bar([10 x double]* %a, i64 %b, i64 %c) { %bx3 = mul i64 %b, 3 %bx3a7 = add i64 %bx3, 7 %ca5 = add i64 %c, 5 %z = getelementptr [10 x double]* %a, i64 %bx3a7, i64 %ca5 ret double* %z }

double *bar(double a[10][10], long b, long c) { return &a[b * 3 + 7][c + 5];}

(600 + (8 * %c) + (240 * %b) + %a)13

Page 30: ScalarEvolution and Loop Optimization - LLVMTripcount Analysis •Induction Variable analysis using SSA •“Backedge-Taken Count” •may be an arbitrary expression •New tools:

Add Recurrences

{@A,+,sizeof(float)}<%loop>

• Based on Bachmann, Wang, and Zima’s “Chains of Recurrences” (“chrecs”)

• Lots of room for exploration

14

Page 31: ScalarEvolution and Loop Optimization - LLVMTripcount Analysis •Induction Variable analysis using SSA •“Backedge-Taken Count” •may be an arbitrary expression •New tools:

Add Recurrences

Start Stride Loop

{@A,+,sizeof(float)}<%loop>

• Based on Bachmann, Wang, and Zima’s “Chains of Recurrences” (“chrecs”)

• Lots of room for exploration

14

Page 32: ScalarEvolution and Loop Optimization - LLVMTripcount Analysis •Induction Variable analysis using SSA •“Backedge-Taken Count” •may be an arbitrary expression •New tools:

void foo(long n, double *p) { for (long i = 0; i < n; ++i)

p[i] = 0.0;}

15

Page 33: ScalarEvolution and Loop Optimization - LLVMTripcount Analysis •Induction Variable analysis using SSA •“Backedge-Taken Count” •may be an arbitrary expression •New tools:

void foo(long n, double *p) { for (long i = 0; i < n; ++i)

p[i] = 0.0;}

{%p,+,8}<%for.body>

{%p,+,sizeof(double)}<%for.body>

As a SCEV:

Optionally, without TargetData:

15

Page 34: ScalarEvolution and Loop Optimization - LLVMTripcount Analysis •Induction Variable analysis using SSA •“Backedge-Taken Count” •may be an arbitrary expression •New tools:

void foo(long n, long j, char *p) { for (long i = 0; i < n; ++i)

p[i + j + bar()] = 0.0;

}

16

Page 35: ScalarEvolution and Loop Optimization - LLVMTripcount Analysis •Induction Variable analysis using SSA •“Backedge-Taken Count” •may be an arbitrary expression •New tools:

void foo(long n, long j, char *p) { for (long i = 0; i < n; ++i)

p[i + j + bar()] = 0.0;

}

({(%j + %p),+,1}<%for.body> + %call)

16

Page 36: ScalarEvolution and Loop Optimization - LLVMTripcount Analysis •Induction Variable analysis using SSA •“Backedge-Taken Count” •may be an arbitrary expression •New tools:

void foo(long n, long j, char *p) { for (long i = 0; i < n; ++i)

p[i + j + bar()] = 0.0;

}

({(%j + %p),+,1}<%for.body> + %call)

• AddRec operands are always loop-invariant

16

Page 37: ScalarEvolution and Loop Optimization - LLVMTripcount Analysis •Induction Variable analysis using SSA •“Backedge-Taken Count” •may be an arbitrary expression •New tools:

{

%a = getelementptr [3 x [3 x double]]* %p, %i, %j, %k

{ {%p,+,72}<%L0>,+,24}<%L1>,+,8}<%L2>

Nested AddRecs

17

Page 38: ScalarEvolution and Loop Optimization - LLVMTripcount Analysis •Induction Variable analysis using SSA •“Backedge-Taken Count” •may be an arbitrary expression •New tools:

{

%a = getelementptr [3 x [3 x double]]* %p, %i, %j, %k

{ {%p,+,72}<%L0> ,+,24}<%L1> ,+,8}<%L2>Outer Loop Inner Loop

Nested AddRecs

Middle Loop

17

Page 39: ScalarEvolution and Loop Optimization - LLVMTripcount Analysis •Induction Variable analysis using SSA •“Backedge-Taken Count” •may be an arbitrary expression •New tools:

Expression Canonicalization

• Goals:

• Uniquify

• Simplify

• Put Add Recurrences on the outside

• Subtract by adding -1*x

18

Page 40: ScalarEvolution and Loop Optimization - LLVMTripcount Analysis •Induction Variable analysis using SSA •“Backedge-Taken Count” •may be an arbitrary expression •New tools:

Future uses for ScalarEvolution

• SCEV AliasAnalysis

• Loop dependence analysis

• Software prefetch insertion

• Array bounds-check elimination

• ...

19

Page 41: ScalarEvolution and Loop Optimization - LLVMTripcount Analysis •Induction Variable analysis using SSA •“Backedge-Taken Count” •may be an arbitrary expression •New tools:

Dependence Analysis on SCEVs

• What’s missing before this can start?

• shape analysis

• given a nest of AddRecs, break out a base and indices for each array dimension

• Why?

• GEPs are abstracted away

• multidimensional VLAs and hand-linearized code “just work”

20

Page 42: ScalarEvolution and Loop Optimization - LLVMTripcount Analysis •Induction Variable analysis using SSA •“Backedge-Taken Count” •may be an arbitrary expression •New tools:

Non-linear recurrencesfor (i=0; i<n; ++i)

j += ifor (i=0; i<n; ++i)

j = i*i

21

Page 43: ScalarEvolution and Loop Optimization - LLVMTripcount Analysis •Induction Variable analysis using SSA •“Backedge-Taken Count” •may be an arbitrary expression •New tools:

Non-linear recurrences

{0,+,0,+,1}<L>=

{0,+, {0,+,1}<L> }<L>

for (i=0; i<n; ++i)j += i

for (i=0; i<n; ++i)j = i*i

21

Page 44: ScalarEvolution and Loop Optimization - LLVMTripcount Analysis •Induction Variable analysis using SSA •“Backedge-Taken Count” •may be an arbitrary expression •New tools:

Non-linear recurrences

{0,+,0,+,1}<L>=

{0,+, {0,+,1}<L> }<L>

for (i=0; i<n; ++i)j += i

for (i=0; i<n; ++i)j = i*i

{0,+,1,+,2}<L>=

{0,+, {1,+,2}<L> }<L>

21

Page 45: ScalarEvolution and Loop Optimization - LLVMTripcount Analysis •Induction Variable analysis using SSA •“Backedge-Taken Count” •may be an arbitrary expression •New tools:

Non-linear recurrences

• ScalarEvolution can solve polynomial recurrences in some cases

• There’s lots more to explore here

{0,+,0,+,1}<L>=

{0,+, {0,+,1}<L> }<L>

for (i=0; i<n; ++i)j += i

for (i=0; i<n; ++i)j = i*i

{0,+,1,+,2}<L>=

{0,+, {1,+,2}<L> }<L>

21

Page 46: ScalarEvolution and Loop Optimization - LLVMTripcount Analysis •Induction Variable analysis using SSA •“Backedge-Taken Count” •may be an arbitrary expression •New tools:

Design questions

• ScalarEvolution is essentially a value-constraints analysis.

• Should it grow to be able to analyze floating-point values too? Vector values?

• Or should there be a separate value constraints analysis Pass instead?

22

Page 47: ScalarEvolution and Loop Optimization - LLVMTripcount Analysis •Induction Variable analysis using SSA •“Backedge-Taken Count” •may be an arbitrary expression •New tools:

CallbackVH fun

• ScalarEvolution is a FunctionPass today

• Keep the map<Value *, SCEV *> current

• Automatic notification for Value deletion.

• Automatic notification for Value modifications?

• Could it be an ImmutablePass?

• Other passes could use this too

23

Page 48: ScalarEvolution and Loop Optimization - LLVMTripcount Analysis •Induction Variable analysis using SSA •“Backedge-Taken Count” •may be an arbitrary expression •New tools:

the end.

24


Recommended