Docsity

Dynamic Optimization in Algorithmic Language Compilers | CS 553, Study notes of Computer Science

Toggle sidebar
bg1
1
CS553 Lecture Dynamic Optimizations 2
Dynamic Optimizations
Last time
– Profiling
Today
– Dynamic compilation
CS553 Lecture Dynamic Optimizations 3
Limitations of static analysis
– Programs can have values and invariants that are known at runtime but
unknown at compile time. Static compilers cannot exploit such values or
invariants
– Many of the motivations for profile-guided optimizations apply here
Basic idea
– Perform translation at runtime when more information is known
– Traditionally, two types of translations are done
– Runtime code generation (JIT compilers)
– Partial evaluation (Staged compilation)
Motivation
CS553 Lecture Dynamic Optimizations 4
Basic idea
– Take a general program and partially evaluate it, producing a specialized
program that’s more efficient
e.g., f(a,b,c)→f’(a,b), where the result has its third parameter hard-
coded into the implementation. f’ is typically more efficient than f
– Exploit runtime constants, which are variables whose value does not
change during program execution, e.g., write-once variables
Partial Evaluation
Exploiting runtime constants
−Perform constant propagation
−Eliminate memory ops
−Remove branches
−Unroll loops
Improves performance by moving
computation from runtime to compile
time
CS553 Lecture Dynamic Optimizations 5
Applications with Runtime Constants
Interpreters:
Simulators:
Graphics renderers:
Scientific simulations:
Extensible OS kernels:
Examples
– A cache simulator might take the line size as a parameter
– A partially evaluated simulator might produce a faster simulator for the
special case where the line size is 16
Program being interpreted is runtime constant
Subject of simulation (circuit, cache, network) is
runtime constant
The scene to render is runtime constant
Matrices can be runtime constants
Extensions to the kernel can be runtime constant
pf3
pf4
pf5

Partial preview of the text

Download Dynamic Optimization in Algorithmic Language Compilers | CS 553 and more Study notes Computer Science in PDF only on Docsity!

CS553 Lecture Dynamic Optimizations 2

Dynamic Optimizations

Last time

  • Profiling Today
  • Dynamic compilation CS553 Lecture Dynamic Optimizations 3 Limitations of static analysis
  • Programs can have values and invariants that are known at runtime but unknown at compile time. Static compilers cannot exploit such values or invariants
  • Many of the motivations for profile-guided optimizations apply here Basic idea
  • Perform translation at runtime when more information is known
  • Traditionally, two types of translations are done
  • Runtime code generation (JIT compilers)
  • Partial evaluation (Staged compilation)

Motivation

CS553 Lecture Dynamic Optimizations 4 Basic idea

  • Take a general program and partially evaluate it, producing a specialized program that’s more efficient e.g., f(a,b,c)→f’(a,b), where the result has its third parameter hard- coded into the implementation. f’ is typically more efficient than f
  • Exploit runtime constants, which are variables whose value does not change during program execution, e.g ., write-once variables

Partial Evaluation

Exploiting runtime constants − Perform constant propagation − Eliminate memory ops − Remove branches − Unroll loops Improves performance by moving computation from runtime to compile time CS553 Lecture Dynamic Optimizations 5

Applications with Runtime Constants

Interpreters: Simulators: Graphics renderers: Scientific simulations: Extensible OS kernels: Examples

  • A cache simulator might take the line size as a parameter
  • A partially evaluated simulator might produce a faster simulator for the special case where the line size is 16 Program being interpreted is runtime constant Subject of simulation (circuit, cache, network) is runtime constant The scene to render is runtime constant Matrices can be runtime constants Extensions to the kernel can be runtime constant

CS553 Lecture Dynamic Optimizations 6

Partial Evaluation (cont)

Active research area

  • Interesting theoretical results
    • Can partially evaluate an interpreter with respect to a program ( i.e., compile the program) [1st^ Futamura projection]
    • Can partially evaluate a partial evaluator with respect to an interpreter ( i.e , generate a compiler) [2nd^ Futamura projection]
    • Can partially evaluate a partial evaluator with respect to a partial evaluator (i.e., generate a compiler generator) [3rd^ Futamura projection]
  • Most PE research focuses on functional languages
  • Key issue
    • When do we stop partially evaluating the code when there is iteration or recursion? CS553 Lecture Dynamic Optimizations 7

Dynamic Compilation with DyC

DyC [Auslander, et al 1996]

  • Staged compilation
  • Apply ideas of Partial Evaluation
  • Perform some of the Partial Evaluation at runtime
    • Can handle more runtime constants than Partial Evaluation
  • Reminiscent of link-time register allocation in the sense that the compilation is performed in stages Tradeoffs
  • Must overcome the run-time cost of the dynamic compiler
  • Fast dynamic compilation: low overhead
  • High quality dynamically generated code: high benefit
  • Ideal: dynamically translate code once, execute this code many times
  • Implication: don’t dynamically translate everything
  • Only perform dynamic translation where it will be profitable CS553 Lecture Dynamic Optimizations 8

Applying Dynamic Compilation

System goal

  • Both fast dynamic compilation and high quality compiled code How do we know what will be profitable?
  • Let user annotations guide the dynamic compilation process System design
  • Dynamic compilation for the C language
  • Declarative annotations:
  • Identify pieces of code to dynamically compile: dynamic regions
  • Identify source code variables that will be constant during the execution of dynamic regions CS553 Lecture Dynamic Optimizations 9

Staged Compilation in DyC

annotated C code (^) static compiler template setup code directives dynamic compiler (stitcher) executable program runtime values static compile time dynamic compile time − Make the static compiler do as much work as possible − Give the dynamic compiler as little work as possible

CS553 Lecture Dynamic Optimizations 14 cacheResult cacheLookup (void *addr, Cache cache) { dynamicRegion (cache) { / cache is a runtime constant */ int blockSize = cache->blockSize; int numLines = cache->numLines; int tag = addr / (blockSize * numLines); int line = (add / blockSize) % numLines; setStructure *setArray = cache->lines[line]->sets; int assoc = cache->associativity; int set; unrolled for (set=0; set<assoc; set++) { if (setArray[set] dynamic ->tag == tag) return CacheHit; } return CacheMiss; } / end of dynamic region */ }

The Annotations

dynamic − Any type of data can be considered constant − In particular, contents of arrays and pointer-based structures are assumed to be runtime constant whenever they are accessed by runtime constant pointers − To ensure that this assumption is correct, users must insert the dynamic annotation to mark pointer refs that are not constant CS553 Lecture Dynamic Optimizations 15 cacheResult cacheLookup (void *addr, Cache cache) { dynamicRegion (cache) { / cache is a runtime constant */ int blockSize = cache->blockSize; int numLines = cache->numLines; int tag = addr / (blockSize * numLines); int line = (add / blockSize) % numLines; setStructure *setArray = cache->lines[line]->sets; int assoc = cache->associativity; int set; unrolled for (set=0; set<assoc; set++) { if (setArray[set] dynamic ->tag == tag) return CacheHit; } return CacheMiss; } / end of dynamic region */ }

The Annotations

unrolled − Directs the compiler to completely unroll a loop − Loop termination must be governed by runtime constants − The static compiler can check whether this annotation is legal − Complete unrolling is a critical optimization − Allows induction variables to become runtime constants CS553 Lecture Dynamic Optimizations 16 cacheResult cacheLookup (void *addr, Cache *cache) { dynamicRegion key (cache, foo) { int blockSize = cache->blockSize; int numLines = cache->numLines; int tag = addr / (blockSize * numLines); int line = (add / blockSize) % numLines; setStructure *setArray = cache->lines[line]->sets; int assoc = cache->associativity; int set; unrolled for (set=0; set<assoc; set++) { if (setArray[set] dynamic ->tag == tag) return CacheHit; } return CacheMiss; } / end of dynamic region */ }

The Annotations

key − Allows the creation of multiple versions of a dynamic region, each using different runtime constants − Separate code is dynamically generated for each distinct combination of values of the runtime constants CS553 Lecture Dynamic Optimizations 17

The Need for Annotations

Annotation errors

  • Lead to incorrect dynamic compilation
    • e.g ., Incorrect code if a value is not really a runtime constant Automatic dynamic compilation is difficult
  • Which variables are runtime constant over which pieces of code?
  • Complicated by aliases, side effects, pointers that can modify memory
  • Which loops are profitable to unroll?
  • Estimating profitability is the difficult part

CS553 Lecture Dynamic Optimizations 18

The Static Compiler

Operates on low-level IR

  • CFG + three address code in SSA form Tasks
  • Identifies runtime constants inside of dynamic regions
  • Splits each dynamic region subgraph into set-up and template code subgraphs
  • Optimizes the control flow for each procedure
  • Generates machine code, including templates
  • In most cases, table space for runtime constants can be statically allocated
  • What do we do about unrolled loops?
  • Generates stitcher directives CS553 Lecture Dynamic Optimizations 19

Detecting Runtime Constants

Simple data-flow analysis

  • Propagates initial runtime constants through the dynamic region using the following transfer functions − x = y x is a constant iff y is a constant − x = y op z x is a const iff y and z are consts and op is an idempotent, side-effect free, non-trapping op − x = f(y 1 , … yn) x is a const iff the yi are consts and f is an idempotent, side-effect free, non-trapping function − x = *p x is a constant iff p is constant − x = dynamic *p x is not constant CS553 Lecture Dynamic Optimizations 20

Detecting Runtime Constants (cont)

Merging control flow

  • If a variable has the same runtime constant reaching definition along all predecessors, it’s considered a constant after the merge x 2 = 2 t 1 = test t 1? x 1 = 1 x 3 = φ (x 1 ,x 2 ) − If test is a runtime constant, then we’ll always take one branch or the other − If test is constant, φ is idempotent so the result is constant − If test is not constant, φ is not idempotent, so the result is not constant CS553 Lecture Dynamic Optimizations 21

Optimizations

Integrated optimizations

  • For best quality code, optimizations should be performed across dynamic region boundaries, e.g ., global CSE, global register allocation
  • Optimizations can be performed both before and after the dynamic region has been split into setup and template codes Restrictions on optimizing split code
  • Instructions with holes cannot be moved outside of their dynamic region
  • Holes cannot be treated as legal values outside of the dynamic region. ( e.g ., Copy propagation cannot propagate values of holes outside of dynamic regions)
  • Holes are typically viewed as constants throughout the dynamic region, but induction variables become constant for only a given iteration of an unrolled loop

CS553 Lecture Dynamic Optimizations 26

Evaluation

Today’s discussion

  • Simple caching scheme
    • Setup once, reuse thereafter
  • More sophisticated schemes are possible
    • Can cache multiple versions of code
    • Can provide eager, or speculative, specialization
    • Can allow different dynamic regions for different variables Recent progress on DyC
  • More sophisticated language and compiler [Grant, et al 1999]
  • More complexity is needed
  • Extremely difficult to annotate the applications
  • Automated insertion of annotations [Mock, et al 2000]
  • Use profiling to obtain value and frequency information CS553 Lecture Dynamic Optimizations 27

Lessons

Is dynamic compilation worthwhile?

  • For optimization, need to be careful because of dynamic compilation costs
  • Important for Java (Just in Time compilers) Dynamo: HP Labs [Bala, et al 2000]
  • Dynamically translate binaries (no annotations)
  • Only modest performance improvements
  • But many other interesting uses (DELI system)
  • Emulation of novel architectures
  • Software sandboxing
  • Software verification CS553 Lecture Dynamic Optimizations 28

Next Time

Reading

  • Ch 14 Lecture
  • Compiling OOP