Compilation and Worst-Case Execution-Time Analysis Peter Puschner - - PowerPoint PPT Presentation

compilation and worst case execution time analysis
SMART_READER_LITE
LIVE PREVIEW

Compilation and Worst-Case Execution-Time Analysis Peter Puschner - - PowerPoint PPT Presentation

Compilation and Worst-Case Execution-Time Analysis Peter Puschner slides credits: P. Puschner, R. Kirner, B. Huber VU 2.0 182.101 SS 2015 Why Compiler-Support for WCET


slide-1
SLIDE 1

Compilation and Worst-Case Execution-Time Analysis

Peter Puschner

slides credits: P. Puschner, R. Kirner, B. Huber

VU 2.0 182.101 SS 2015

slide-2
SLIDE 2

Why Compiler-Support for WCET Analysis?

Compiler “knows” both code representations

  • Source code (the user representation)
  • Object code (the instructions determining timing)

Compiler “knows” the transformations it applies

  • optimizations could target the worst-case path

➭ Compiler can generate information that is relevant

for WCET analysis

➭ Compiler can generate time-predictable code

(cache locking, pre-fetching for scratch-pad, single- path code, avoidance of timing anomalies, etc.)

2

slide-3
SLIDE 3

A Related Problem – Source-level debugging

Source-level debugging faces similar challenges as timing analysis of optimized code:

  • Mapping between source code and object code

becomes blurred due to code transformations

  • In debugging, code locations and store (memory,

variables, etc.) content have to be mapped

  • In timing analysis, flow information has to be

mapped to object code and timing results need to be mapped to the source code.

3

slide-4
SLIDE 4

A Related Problem – Source-level debugging

int test (int a) { int b = a; int c = b + 4; return c; }

4

int test (int a) { int c = a + 4; return c; } What is the value

  • f variable

‘b’ ??

  • ptimization

à additional information required to reconstruct

  • riginal variable values.
slide-5
SLIDE 5

Contents

Provide program information to reduce manual code annotations WCET-aware code optimizations Transformation of flow information Preservation of code coverage

5

slide-6
SLIDE 6

Reducing Manual Code Annotations

Why is it hard to analyse code semantics for WCET analysis?

  • Platform-specific behavior (memory layout, ROM-areas,

memory-mapped IO, representation of data types, …)

  • Control-flow is well hidden within low-level constructs of
  • bject code (indirect jumps, …)
  • Formal limits of computability (loop bounds, correlation of

code predicates, type-inference in object-oriented languages, …)

6

slide-7
SLIDE 7

Workaround: Guide the Analysis by Means of Code Annotations (1)

Annotations to find jump-targets [aiT]: instruction <addr> calls <target-list>; instruction <addr> branches to <target-list>; Labeling assembly-instructions with high-level meaning [aiT]: instruction <addr> is a return;

7

slide-8
SLIDE 8

Workaround: Guide the Analysis by Means of Code Annotations (2)

Describing possible values of variables [aiT]: condition <addr> is always <bool value>; snippet <addr> is never executed; instruction <addr> is entered with <state>; Describe boundaries of memory access [aiT]: instruction <addr> accesses <addr-range>;

8

slide-9
SLIDE 9

Workaround: Guide the Analysis by Means of Code Annotations (3)

Describe iteration bounds of loops [Bound-T]: subprogram “<fn-name>” loop that uses <var-name> repeats <= 7 times; end loop; end “<fn-name>”; (information in external code annotation file)

9

slide-10
SLIDE 10

Reflection on Code Annotations

Code annotations may be erroneous Often code has to be annotated at object code level ð update/check annotations after changes and re-compilation

  • f code

Manually annotating the code is labor-intensive (especially at the object-code level !!!) Questions: Are there code annotations that can be avoided? Can we simplify the annotation process?

10

slide-11
SLIDE 11

Classification of Code Annotations (1)

Platform Property Annotations (PPA) CFG Reconstruction Annotations (CRA) Program Semantics Annotations (PSA) Auxiliary Annotations (AA)

11

slide-12
SLIDE 12

Classification of Code Annotations (2)

Platform Property Annotations (PPA) Description of special semantics behind the access of memory-mapped IO (local annotations). Description of platform properties that influence the execution time (mostly global annotations). Examples: description of memory layout, properties of target hardware, …

12

slide-13
SLIDE 13

Classification of Code Annotations (3)

CFG Reconstruction Annotations (CRA) Help to build basic structures of code analysis:

  • control-flow graph (CFG) and call-graph

Allow to reverse-engineer the high-level code structure from the low-level assembly statements. Examples: list of targets for branch instructions, type labeling of branch instructions, …

13

slide-14
SLIDE 14

Classification of Code Annotations (4)

Program Semantics Annotations (PSA) Local description of program behavior Support for

  • path analysis
  • value analysis
  • etc.

Examples: flow annotations like loop bounds, description of variable values, …

14

slide-15
SLIDE 15

Compiler-Support for WCET Analysis (1)

Information accessible by the compiler: Program representation at source and object code level. Code transformations performed during compilation. How the compiler can help: Describe CFG of object code (to avoid the need of CRA) Provide mapping of source-code annotations to object code (simplification by writing annotations at source code level instead of object-code level) [PPA,PSA,AA]

15

slide-16
SLIDE 16

Compiler-Support for WCET Analysis (2)

How the compiler can help (2): Emit information about a program’s execution behavior (reduce amount of code annotations [PSA])

  • in general, some PSA may remain mandatory
  • but in practice almost no PSA is mandatory!

Improve predictability of code

  • imitation of “WCET-oriented programming”
  • e.g., single-path conversion can reduce the execution-time

jitter of real-time programs

  • support of predictable HW mechanisms

(e.g., prefetching, scratchpad memory, cache locking),

16

slide-17
SLIDE 17

Final Remark on Code Annotations

Not all code annotations are artificial: application-context (operation mode) may be specified to refine the analysis [PSA] (e.g. range on initial variable values, CFG restriction) mapping of target-specific properties to code may be mandatory [PPA] (e.g. memory-mapped IO)

17

slide-18
SLIDE 18

Contents

Provide program information to reduce manual code annotations WCET-aware code optimizations Transformation of flow information Preservation of code coverage

18

slide-19
SLIDE 19

WCET-aware Code Optimizations

19

Compiler WCET Analyzer

  • localize worst-case path,
  • select appropriate optimizations,
  • identify code locations to

apply optimizations

slide-20
SLIDE 20

WCET-aware Code Optimizations (2)

Invariant Code Motion – a real-time optimization?

20

for (; i<10; i++) { a = 4; b = a+i; send (b); } a = 4; for (; i<10; i++) { b = a+i; send (b); }

  • ptimization

(invariant code motion) what if loop is never executed? à WCET is increased if loop is executed,

  • ptimized code has

decreased WCET

slide-21
SLIDE 21

WCET-aware Code Optimizations (3)

Superblock Formation:

21

1 2 3 4 5 6 7 8 9 1 2 3 4 5’ 6’ 8’ 9 5 6 7 8

superblock formation

…worst case path

increase of code size but reduction of WCET

superblock: …block with single entry point

slide-22
SLIDE 22

Worst-Case Path

Be aware: optimizations may change the worst-case path

22

input data input data execution time execution time real-time code optimization

different worst- case path!

slide-23
SLIDE 23

Worst-Case Path

Be aware: execution of new worst-case path may take longer than the original one

23

input data input data execution time execution time non real-time code optimization

increase along worst-case path!

slide-24
SLIDE 24

Contents

Provide program information to reduce manual code annotations WCET-aware code optimizations Transformation of flow information Preservation of code coverage

24

slide-25
SLIDE 25

Challenge: Keeping Flow Information Correct

25

source code

  • bject

code

flow facts (loop bounds, etc.)

compilation

Q: How can we guarantee that flow facts for compiled code are correct?

parallel transformation

  • f flow facts
slide-26
SLIDE 26

Challenge: Keeping Flow Information Correct (2)

for (i=0; i<N; i++) maximum 10 iterations { f(i); }

26

for (i=0; i<(N-2); i=i+3) { f(i); f(i+1); f(i+2); } for (;i<N; i++) { f(i); }

loop unrolling (unrolling factor 3)

Q: what flow facts are known

  • f the transformed code?

additional knowledge assumed: N≤10 (flow fact given as code annotation)

slide-27
SLIDE 27

Challenge: Keeping Flow Information Correct (3)

for (i=0; i<N; i++) maximum 10 iterations { f(i); }

27

scope { for (i=0; i<(N-2); i=i+3) maximum 3 iterations { marker m1; f(i); f(i+1); f(i+2); } for (;i<N; i++) maximum 2 iterations { marker m2; f(i); } restriction 3*m1 + m2 <= 10; } loop unrolling (unrolling factor 3)

Exercise: explain why the new flow facts follow from the original code and the transformation

additional knowledge assumed: N≤10 (flow fact given as code annotation) linear flow constraint flow variable flow variable loop bound loop bound

slide-28
SLIDE 28

Challenge: Keeping Flow Information Correct (4)

Flow information (flow facts) to be transformed:

  • Loop bound information (upper and lower bound of loop

iterations)

  • Marker bindings (link between flow variables and control-

flow edges of the program), e.g., “mAB” denotes the control flow from CFG node A to node B.

  • Flow constraints (linear relations between flow variables),

e.g., “4*mAB ≤ 1*mCD”

28

slide-29
SLIDE 29

Flow-Facts Transformation Functions Fff()

A flow facts transformation function Fff() has to be derived from the given code structure and the applied code

  • ptimization/transformation function Fcode()

Fcode() is composed of a sequence of low-level operations, like jump redirection; move, duplication, deletion, modification of code, etc. Deriving Fff() directly from the low-level operations of Fcode() is not possible, since low-level operations may leave the program in an inconsistent form – only after all low-level

  • perations has the program to be semantically equivalent

to the original program à Fff() has to be derived from the Fcode() as a whole

29

slide-30
SLIDE 30

Flow-Facts Transformation Functions Fff()

Flow facts transformation functions are composed of the following operations:

§ Update of loop bound information ( ):

(create, modify, or delete loop bound information)

§ Update of flow constraints ( ):

(transform terms of the form “const • flowvariable” of a linear flow constraint into a new term respectively a sum

  • f terms, creation of new flow constraints)

R

30

L

slide-31
SLIDE 31

Results of Flow Facts Transformation

31

  • Example: bubble sort

– algorithm: a pair of nested loops where the iteration count of the inner loop depends on the current iteration

  • f the outer loop (à “triangular loop”)
  • Compiler: gcc 2.7.2 (C167 port by HighTec), extended to

handle flow information (TU Vienna)

  • Static WCET tool: calc_wcet_167 (TU Vienna)
slide-32
SLIDE 32

Results of Flow Facts Transformation

WCET analysis method nonoptimized code

  • ptimized

code improvement [cycles] [cycles] [rel.] measured 104.007 30.486 70.7% static 1 (loop bounds only) 198.741 57.816 70.9% static 2 (detailed flow facts) 104.625 30.492 70.7%

32

à compatibility of WCET analysis framework with code

  • ptimizations provides significant WCET improvement
slide-33
SLIDE 33

Results of Flow Facts Transformation

WCET analysis method nonoptimized code

  • ptimized code

[cycles] [rel.] [cycles] [rel.] measured 104.007 100.0% 30.486 100.0% static 1 (loop bounds only) 198.741 191.1% 57.816 189.6% static 2 (detailed flow facts) 104.625 100.6% 30.492 100.1%

33

à support of detailed flow facts is important to avoid high overestimation of WCET

slide-34
SLIDE 34

Contents

Provide program information to reduce manual code annotations WCET-aware code optimizations Transformation of flow information Preservation of code coverage

34

slide-35
SLIDE 35

Preservation of Code Coverage

Assumption: Measurement-based WCET analysis that uses automatic test-data generation based on source code analysis

  • highly retargetable (source code semantics almost platform

independent)

Test data generation method: e.g., partition the source code into smaller units (so-called segments) and generate test data to cover each path within a segment

35

slide-36
SLIDE 36

Preservation of Code Coverage (2)

Typical structural code coverage metrics:

  • statement coverage (not line coverage!)
  • decision coverage (=branch coverage)
  • condition coverage
  • MC/DC coverage
  • path coverage

Challenge: How can we assure that the code coverage at source code is also valid at object code level?

  • compiler may introduce conditional control flow to
  • ptimize the frequent case

36

slide-37
SLIDE 37

Preservation of Code Coverage (3)

Solution: introduce compilation profiles into the compiler to select only optimizations that do preserve a given structural code coverage Command line switches used to specify which level of structural code coverage should be preserved: cc –sccp/dc … preserve decision coverage

37

slide-38
SLIDE 38

Summary

Compiler-support is very beneficial for WCET analysis:

  • reduce amount of code annotations to be specified

manually

  • optimizations can be chosen to optimize the worst-

case path

  • generate time-predictable code (next lectures)

Question: when is the hard real-time market strong enough to demand such solutions?

38

slide-39
SLIDE 39

Bibliography

Wankang Zhao, William Kreahling, David Whally, Christopher Healy, Frank Mueller, Improving WCET by Optimizing Worst-Case Paths,

  • Proc. 11th IEEE Real-Time Technology and Applications

Symposium, 2005. Heiko Falk, Paul Lokuciejewski, Henrik Theiling, Design of a WCET- Aware C Compiler, Proc. 18th Euromicro Conference on Real Time Systems, Dresden, Germany, July 2006. Raimund Kirner, Extending Optimising Compilation to Support Worst- Case Execution Time Analysis, Technische Universität Wien, PhD Thesis, Vienna, Austria, May 2003. Raimund Kirner, Peter Puschner, Classification of Code Annotations and Discussion of Compiler-Support for Worst-Case Execution Time Analysis, Proc. 5th International Workshop on Worst-Case Execution Time Analysis, Palma, Spain, July 2006.

39

slide-40
SLIDE 40

http://ti.tuwien.ac.at/rts/teaching/courses/wcet http://www.wcet.at

40