Handling Multi-Versioning in LLVM: Code Tracking and Cloning...2011/09/16  · Interactive...

40
Handling Multi-Versioning in LLVM: Code Tracking and Cloning Alexandra Jimborean Vincent Loechner Philippe Clauss INRIA - CAMUS Team Université de Strasbourg London - September, 2011 A. Jimborean, V. Loechner, Ph. Clauss Handling Multi-Versioning in LLVM: Code Tracking and Cloning London - September, 2011 1 / 37

Transcript of Handling Multi-Versioning in LLVM: Code Tracking and Cloning...2011/09/16  · Interactive...

Page 1: Handling Multi-Versioning in LLVM: Code Tracking and Cloning...2011/09/16  · Interactive Compilation Interface Providing access to the internal functionalities of the compilers Generic

Handling Multi-Versioning in LLVM: Code Trackingand Cloning

Alexandra Jimborean Vincent Loechner Philippe Clauss

INRIA - CAMUS TeamUniversité de Strasbourg

London - September, 2011

A. Jimborean, V. Loechner, Ph. ClaussHandling Multi-Versioning in LLVM: Code

Tracking and Cloning London - September, 2011 1 / 37

Page 2: Handling Multi-Versioning in LLVM: Code Tracking and Cloning...2011/09/16  · Interactive Compilation Interface Providing access to the internal functionalities of the compilers Generic

Motivation

Outline

1 Motivation

2 State of the Art

3 Tracking code in LLVM IR using attached metadata

4 Interaction between high- and low-level IRs

5 Experiments

6 Conclusions

A. Jimborean, V. Loechner, Ph. ClaussHandling Multi-Versioning in LLVM: Code

Tracking and Cloning London - September, 2011 2 / 37

Page 3: Handling Multi-Versioning in LLVM: Code Tracking and Cloning...2011/09/16  · Interactive Compilation Interface Providing access to the internal functionalities of the compilers Generic

Motivation

Why do we need multi-versioning?

Multi-versioning

Sampling – Instrumentation

Adaptive computing – Runtime version selection

Dynamic optimization – Speculative parallelism

Multiple versions in different representations

Each version in the most suitable IR

Low-level IR for acquiring low-level information

Higher level IR for performing code transformations

Handled by a runtime system

A. Jimborean, V. Loechner, Ph. ClaussHandling Multi-Versioning in LLVM: Code

Tracking and Cloning London - September, 2011 3 / 37

Page 4: Handling Multi-Versioning in LLVM: Code Tracking and Cloning...2011/09/16  · Interactive Compilation Interface Providing access to the internal functionalities of the compilers Generic

Motivation

Why do we need multi-versioning?

Multi-versioning

Sampling – Instrumentation

Adaptive computing – Runtime version selection

Dynamic optimization – Speculative parallelism

Multiple versions in different representations

Each version in the most suitable IR

Low-level IR for acquiring low-level information

Higher level IR for performing code transformations

Handled by a runtime system

A. Jimborean, V. Loechner, Ph. ClaussHandling Multi-Versioning in LLVM: Code

Tracking and Cloning London - September, 2011 3 / 37

Page 5: Handling Multi-Versioning in LLVM: Code Tracking and Cloning...2011/09/16  · Interactive Compilation Interface Providing access to the internal functionalities of the compilers Generic

State of the Art

Outline

1 Motivation

2 State of the Art

3 Tracking code in LLVM IR using attached metadata

4 Interaction between high- and low-level IRs

5 Experiments

6 Conclusions

A. Jimborean, V. Loechner, Ph. ClaussHandling Multi-Versioning in LLVM: Code

Tracking and Cloning London - September, 2011 4 / 37

Page 6: Handling Multi-Versioning in LLVM: Code Tracking and Cloning...2011/09/16  · Interactive Compilation Interface Providing access to the internal functionalities of the compilers Generic

State of the Art

Related work

Tracking code through the optimization phase

Extend debugging info and create bi-directional maps [Brooks et al.]

Debug dynamically optimized code [Kumar et al.]

Interactive Compilation Interface

Providing access to the internal functionalities of the compilers

Generic cloning, instrumentation, control of individual optimizationpasses

Multi-versioning available only at function level

http://ctuning.org/ici

A. Jimborean, V. Loechner, Ph. ClaussHandling Multi-Versioning in LLVM: Code

Tracking and Cloning London - September, 2011 5 / 37

Page 7: Handling Multi-Versioning in LLVM: Code Tracking and Cloning...2011/09/16  · Interactive Compilation Interface Providing access to the internal functionalities of the compilers Generic

State of the Art

LLVM features

Embedding high-level information in the IR

Support for preserving the high-level information

Annotate the code using metadataNo influence on the optimization passes, unless designed for this

Cloning utilities

Copies of instructions, basic blocks or functions

No correlation between original and cloned values

Reserved only for some very specific situations

A. Jimborean, V. Loechner, Ph. ClaussHandling Multi-Versioning in LLVM: Code

Tracking and Cloning London - September, 2011 6 / 37

Page 8: Handling Multi-Versioning in LLVM: Code Tracking and Cloning...2011/09/16  · Interactive Compilation Interface Providing access to the internal functionalities of the compilers Generic

Tracking code in LLVM IR using attached metadata

Outline

1 Motivation

2 State of the Art

3 Tracking code in LLVM IR using attached metadata

4 Interaction between high- and low-level IRs

5 Experiments

6 Conclusions

A. Jimborean, V. Loechner, Ph. ClaussHandling Multi-Versioning in LLVM: Code

Tracking and Cloning London - September, 2011 7 / 37

Page 9: Handling Multi-Versioning in LLVM: Code Tracking and Cloning...2011/09/16  · Interactive Compilation Interface Providing access to the internal functionalities of the compilers Generic

Tracking code in LLVM IR using attached metadata

From C/C++ to LLVM IR with metadata

Code tracking in C/C++ source code

Source code : pragmaDefine new pragma todelimit the code regionsof interest

Focus on loop nests

A. Jimborean, V. Loechner, Ph. ClaussHandling Multi-Versioning in LLVM: Code

Tracking and Cloning London - September, 2011 8 / 37

Page 10: Handling Multi-Versioning in LLVM: Code Tracking and Cloning...2011/09/16  · Interactive Compilation Interface Providing access to the internal functionalities of the compilers Generic

Tracking code in LLVM IR using attached metadata

Extending the IR vs using annotations

A. Jimborean, V. Loechner, Ph. ClaussHandling Multi-Versioning in LLVM: Code

Tracking and Cloning London - September, 2011 9 / 37

Page 11: Handling Multi-Versioning in LLVM: Code Tracking and Cloning...2011/09/16  · Interactive Compilation Interface Providing access to the internal functionalities of the compilers Generic

Tracking code in LLVM IR using attached metadata

Identify the region after applying optimizations

Loop nest structure is significantly changed

Loop fusion, splitting, interchange etc.

Metadata information may not be preserved

Identify instructions that carry metadata information and considerthe whole enclosing loop nest

Additional code might be includedAll instructions marked for multiversioning are enclosed

A. Jimborean, V. Loechner, Ph. ClaussHandling Multi-Versioning in LLVM: Code

Tracking and Cloning London - September, 2011 10 / 37

Page 12: Handling Multi-Versioning in LLVM: Code Tracking and Cloning...2011/09/16  · Interactive Compilation Interface Providing access to the internal functionalities of the compilers Generic

Tracking code in LLVM IR using attached metadata

A. Cloning

BB1_clone:

x

x

x

BB1 ------> BB1_clone

BB2 ------> BB2_clone

BB3 ------> BB3_clone

BB4 ------> BB4_clone

BB2_clone:

BB3_clone: BB4_clone:

A. Jimborean, V. Loechner, Ph. ClaussHandling Multi-Versioning in LLVM: Code

Tracking and Cloning London - September, 2011 11 / 37

Page 13: Handling Multi-Versioning in LLVM: Code Tracking and Cloning...2011/09/16  · Interactive Compilation Interface Providing access to the internal functionalities of the compilers Generic

Tracking code in LLVM IR using attached metadata

B. Rebuild control-flow-graph between clones

A. Jimborean, V. Loechner, Ph. ClaussHandling Multi-Versioning in LLVM: Code

Tracking and Cloning London - September, 2011 12 / 37

Page 14: Handling Multi-Versioning in LLVM: Code Tracking and Cloning...2011/09/16  · Interactive Compilation Interface Providing access to the internal functionalities of the compilers Generic

Tracking code in LLVM IR using attached metadata

C. Extract versions in separate functions

Each version compiled independently into the most suitable IRA. Jimborean, V. Loechner, Ph. Clauss

Handling Multi-Versioning in LLVM: CodeTracking and Cloning London - September, 2011 13 / 37

Page 15: Handling Multi-Versioning in LLVM: Code Tracking and Cloning...2011/09/16  · Interactive Compilation Interface Providing access to the internal functionalities of the compilers Generic

Tracking code in LLVM IR using attached metadata

Challenges: Dominate all uses

Instruction does not dominate all uses!%tmp = add i32 %a, %b

%aux_clone = add i32 %c, %tmp

Clone, replace uses in clones, reinsert, reconstruct the loop structure

%tmp = add i32 %a, %b%aux = add i32 %c, %tmp

%tmp_clone = add i32 %a_clone, %b_clone%aux_clone = add i32 %c_clone, %tmp_clone

A. Jimborean, V. Loechner, Ph. ClaussHandling Multi-Versioning in LLVM: Code

Tracking and Cloning London - September, 2011 14 / 37

Page 16: Handling Multi-Versioning in LLVM: Code Tracking and Cloning...2011/09/16  · Interactive Compilation Interface Providing access to the internal functionalities of the compilers Generic

Interaction between high- and low-level IRs

Outline

1 Motivation

2 State of the Art

3 Tracking code in LLVM IR using attached metadata

4 Interaction between high- and low-level IRs

5 Experiments

6 Conclusions

A. Jimborean, V. Loechner, Ph. ClaussHandling Multi-Versioning in LLVM: Code

Tracking and Cloning London - September, 2011 15 / 37

Page 17: Handling Multi-Versioning in LLVM: Code Tracking and Cloning...2011/09/16  · Interactive Compilation Interface Providing access to the internal functionalities of the compilers Generic

Interaction between high- and low-level IRs

Interaction between high- and low-level IRs

Communication between code versions in distinct representations

Control flow cannot enter or exit lower level representationsInline assembly is expected to ‘fall through’ to the following code

Handle the control flow graph in the low-level IR

Minimally influence the behavior of the original code

A. Jimborean, V. Loechner, Ph. ClaussHandling Multi-Versioning in LLVM: Code

Tracking and Cloning London - September, 2011 16 / 37

Page 18: Handling Multi-Versioning in LLVM: Code Tracking and Cloning...2011/09/16  · Interactive Compilation Interface Providing access to the internal functionalities of the compilers Generic

Interaction between high- and low-level IRs

Handling jumps between LLVM IR and inline assembly

Generic callbacks - patched by the runtime system

mov $0x0,%rdi //address of the modulemov $0x0,%rsi //address of the function

LabelsIdentify the address of the code to be patchedTarget of the inline jumps

Jumps

Macro Hexadecimal form

asm_jge8 TARGET .byte 0X7D.byte \TARGET \()-.-1

asm_jge32 TARGET .byte 0X0F, 0X8D.long \TARGET \()-.-4

A. Jimborean, V. Loechner, Ph. ClaussHandling Multi-Versioning in LLVM: Code

Tracking and Cloning London - September, 2011 17 / 37

Page 19: Handling Multi-Versioning in LLVM: Code Tracking and Cloning...2011/09/16  · Interactive Compilation Interface Providing access to the internal functionalities of the compilers Generic

Interaction between high- and low-level IRs

Control flow graph rewritten in inline code

A. Jimborean, V. Loechner, Ph. ClaussHandling Multi-Versioning in LLVM: Code

Tracking and Cloning London - September, 2011 18 / 37

Page 20: Handling Multi-Versioning in LLVM: Code Tracking and Cloning...2011/09/16  · Interactive Compilation Interface Providing access to the internal functionalities of the compilers Generic

Interaction between high- and low-level IRs

Control flow graph rewritten in inline code

A. Jimborean, V. Loechner, Ph. ClaussHandling Multi-Versioning in LLVM: Code

Tracking and Cloning London - September, 2011 18 / 37

Page 21: Handling Multi-Versioning in LLVM: Code Tracking and Cloning...2011/09/16  · Interactive Compilation Interface Providing access to the internal functionalities of the compilers Generic

Interaction between high- and low-level IRs

Control flow graph rewritten in inline code

A. Jimborean, V. Loechner, Ph. ClaussHandling Multi-Versioning in LLVM: Code

Tracking and Cloning London - September, 2011 18 / 37

Page 22: Handling Multi-Versioning in LLVM: Code Tracking and Cloning...2011/09/16  · Interactive Compilation Interface Providing access to the internal functionalities of the compilers Generic

Interaction between high- and low-level IRs

Challenges: Phi nodes

Promote registers to memory

opt -reg2mem prg.bc

A. Jimborean, V. Loechner, Ph. ClaussHandling Multi-Versioning in LLVM: Code

Tracking and Cloning London - September, 2011 19 / 37

Page 23: Handling Multi-Versioning in LLVM: Code Tracking and Cloning...2011/09/16  · Interactive Compilation Interface Providing access to the internal functionalities of the compilers Generic

Interaction between high- and low-level IRs

Eliminate Phi nodes to hack into the CFG

A. Jimborean, V. Loechner, Ph. ClaussHandling Multi-Versioning in LLVM: Code

Tracking and Cloning London - September, 2011 20 / 37

Page 24: Handling Multi-Versioning in LLVM: Code Tracking and Cloning...2011/09/16  · Interactive Compilation Interface Providing access to the internal functionalities of the compilers Generic

Interaction between high- and low-level IRs

Toy example

A. Jimborean, V. Loechner, Ph. ClaussHandling Multi-Versioning in LLVM: Code

Tracking and Cloning London - September, 2011 21 / 37

Page 25: Handling Multi-Versioning in LLVM: Code Tracking and Cloning...2011/09/16  · Interactive Compilation Interface Providing access to the internal functionalities of the compilers Generic

Interaction between high- and low-level IRs

SPEC CPU 2006 bzip

CFG of a simple loop from bzip2 SPEC CPU 2006

A. Jimborean, V. Loechner, Ph. ClaussHandling Multi-Versioning in LLVM: Code

Tracking and Cloning London - September, 2011 22 / 37

Page 26: Handling Multi-Versioning in LLVM: Code Tracking and Cloning...2011/09/16  · Interactive Compilation Interface Providing access to the internal functionalities of the compilers Generic

Interaction between high- and low-level IRs

Promote registers to memory

Loop indices must be either defined or used outside the loop,otherwise they are not sent as parameters when extracting theloops in new functions

A. Jimborean, V. Loechner, Ph. ClaussHandling Multi-Versioning in LLVM: Code

Tracking and Cloning London - September, 2011 23 / 37

Page 27: Handling Multi-Versioning in LLVM: Code Tracking and Cloning...2011/09/16  · Interactive Compilation Interface Providing access to the internal functionalities of the compilers Generic

Interaction between high- and low-level IRs

Promote registers to memory

Inline assembly defining labels must come before the phiinstructions

A. Jimborean, V. Loechner, Ph. ClaussHandling Multi-Versioning in LLVM: Code

Tracking and Cloning London - September, 2011 24 / 37

Page 28: Handling Multi-Versioning in LLVM: Code Tracking and Cloning...2011/09/16  · Interactive Compilation Interface Providing access to the internal functionalities of the compilers Generic

Interaction between high- and low-level IRs

Promote registers to memory

More memory accesses

Restricted optimizations

Negative impact onperformance

A. Jimborean, V. Loechner, Ph. ClaussHandling Multi-Versioning in LLVM: Code

Tracking and Cloning London - September, 2011 25 / 37

Page 29: Handling Multi-Versioning in LLVM: Code Tracking and Cloning...2011/09/16  · Interactive Compilation Interface Providing access to the internal functionalities of the compilers Generic

Interaction between high- and low-level IRs

Challenges: Inline assembly

Prevent optimizations fromduplicating, reordering, deleting theinlined code

Create a new BasicBlockcontaining only the asm codeConnect it in the CFG usingindirect branchesInsert metadata to preventoptimizations

Minimally influence the optimizationpasses to maintain performance

A. Jimborean, V. Loechner, Ph. ClaussHandling Multi-Versioning in LLVM: Code

Tracking and Cloning London - September, 2011 26 / 37

Page 30: Handling Multi-Versioning in LLVM: Code Tracking and Cloning...2011/09/16  · Interactive Compilation Interface Providing access to the internal functionalities of the compilers Generic

Experiments

Outline

1 Motivation

2 State of the Art

3 Tracking code in LLVM IR using attached metadata

4 Interaction between high- and low-level IRs

5 Experiments

6 Conclusions

A. Jimborean, V. Loechner, Ph. ClaussHandling Multi-Versioning in LLVM: Code

Tracking and Cloning London - September, 2011 27 / 37

Page 31: Handling Multi-Versioning in LLVM: Code Tracking and Cloning...2011/09/16  · Interactive Compilation Interface Providing access to the internal functionalities of the compilers Generic

Experiments

Loop Instrumentation by sampling

Following code

No Yes

No

Yes Yes

No

Original

version

of the loop

Instrumented

version

of the loop

End of the loop

Decision block:

A. Jimborean, V. Loechner, Ph. ClaussHandling Multi-Versioning in LLVM: Code

Tracking and Cloning London - September, 2011 28 / 37

Page 32: Handling Multi-Versioning in LLVM: Code Tracking and Cloning...2011/09/16  · Interactive Compilation Interface Providing access to the internal functionalities of the compilers Generic

Experiments

Challenges: Multiple exit loops

Extract each loop in a new function

Unique exit: returning point of the function

A. Jimborean, V. Loechner, Ph. ClaussHandling Multi-Versioning in LLVM: Code

Tracking and Cloning London - September, 2011 29 / 37

Page 33: Handling Multi-Versioning in LLVM: Code Tracking and Cloning...2011/09/16  · Interactive Compilation Interface Providing access to the internal functionalities of the compilers Generic

Experiments

Challenges: Instrumentation instructions

In x86_64 assembly: after registerallocation

In LLVM IRRequires type conversionsInstrumenting all LLVM loads andstores –> negative impact on theperformance

A. Jimborean, V. Loechner, Ph. ClaussHandling Multi-Versioning in LLVM: Code

Tracking and Cloning London - September, 2011 30 / 37

Page 34: Handling Multi-Versioning in LLVM: Code Tracking and Cloning...2011/09/16  · Interactive Compilation Interface Providing access to the internal functionalities of the compilers Generic

Experiments

Results SPEC CPU 2006

Program Runtime Runtime # linear # instrumented Percentageoverhead overhead m.a. m.a. of linear

(-O0) (-O3) m.a.

bzip2 0.24% 12.31% 608 1,053 57.73%mcf 20.76% 17.23% 2,848,598 4,054,863 70.25%milc 0.081% 3.61% 1,988,256,000 1,988,256,195 99.99%

hmmer 0.062% 0.76% 845 0 0%sjeng 182% 11.13% 1,032,148,267 1,155,459,440 89.32%

libquantum 3.88% 2.76% 203,078 203,581 99.75%h264ref 0.49% 4.59% 30,707,102 32,452,013 94.62%

lbm 0% 0.93% 358 0 0%sphinx3 172% 27.62% 51,566,707 78,473,958 65.71%

A. Jimborean, V. Loechner, Ph. ClaussHandling Multi-Versioning in LLVM: Code

Tracking and Cloning London - September, 2011 31 / 37

Page 35: Handling Multi-Versioning in LLVM: Code Tracking and Cloning...2011/09/16  · Interactive Compilation Interface Providing access to the internal functionalities of the compilers Generic

Experiments

Measurements on SPEC CPU 2006: -O0 vs -O3

A. Jimborean, V. Loechner, Ph. ClaussHandling Multi-Versioning in LLVM: Code

Tracking and Cloning London - September, 2011 32 / 37

Page 36: Handling Multi-Versioning in LLVM: Code Tracking and Cloning...2011/09/16  · Interactive Compilation Interface Providing access to the internal functionalities of the compilers Generic

Experiments

Results

Pointer-Intensive benchmark suite

Program Runtime # linear # instrumented Percentageoverhead m.a. m.a. of linear m.a.

anagram -5.37% 134 159 84.27%bc 183% 243,785 302,034 80.71%ft -8.46% 22 36 61.11%ks 29.7% 29,524 42,298 69.79%

A. Jimborean, V. Loechner, Ph. ClaussHandling Multi-Versioning in LLVM: Code

Tracking and Cloning London - September, 2011 33 / 37

Page 37: Handling Multi-Versioning in LLVM: Code Tracking and Cloning...2011/09/16  · Interactive Compilation Interface Providing access to the internal functionalities of the compilers Generic

Conclusions

Outline

1 Motivation

2 State of the Art

3 Tracking code in LLVM IR using attached metadata

4 Interaction between high- and low-level IRs

5 Experiments

6 Conclusions

A. Jimborean, V. Loechner, Ph. ClaussHandling Multi-Versioning in LLVM: Code

Tracking and Cloning London - September, 2011 34 / 37

Page 38: Handling Multi-Versioning in LLVM: Code Tracking and Cloning...2011/09/16  · Interactive Compilation Interface Providing access to the internal functionalities of the compilers Generic

Conclusions

Open questions

Promoting registers to memory (Phi node elimination)

Maintain LLVM branches and jumps in inline assembly

Type conversions

A. Jimborean, V. Loechner, Ph. ClaussHandling Multi-Versioning in LLVM: Code

Tracking and Cloning London - September, 2011 35 / 37

Page 39: Handling Multi-Versioning in LLVM: Code Tracking and Cloning...2011/09/16  · Interactive Compilation Interface Providing access to the internal functionalities of the compilers Generic

Conclusions

Perspectives

Speculative code parallelization on the fly using multi-versioning

Develop an easy-to-use API to extend the framework

A. Jimborean, V. Loechner, Ph. ClaussHandling Multi-Versioning in LLVM: Code

Tracking and Cloning London - September, 2011 36 / 37

Page 40: Handling Multi-Versioning in LLVM: Code Tracking and Cloning...2011/09/16  · Interactive Compilation Interface Providing access to the internal functionalities of the compilers Generic

Conclusions

Thank you.

A. Jimborean, V. Loechner, Ph. ClaussHandling Multi-Versioning in LLVM: Code

Tracking and Cloning London - September, 2011 37 / 37