Course Staff Administrivia Overview of the Material
CO444H Administrivia Overview of the Material Ben Livshits Two - - PowerPoint PPT Presentation
CO444H Administrivia Overview of the Material Ben Livshits Two - - PowerPoint PPT Presentation
Course Staff CO444H Administrivia Overview of the Material Ben Livshits Two Primary Goals We Pursue Optimisations Reliability Make programs run faster Find bugs before they make it into production We will see some traditional
Two Primary Goals We Pursue
Optimisations
- Make programs run faster
- We will see some
traditional “old-school”
- ptimisations
- We will see some that are
based on parallelism
- We will see some runtime
- ptimisations
Reliability
- Find bugs before they
make it into production
- Find security flaws
before they are exploited by hackers
- Help developers
produce better code
2
Aren’t Compilers a Solved Problem?
“Optimization for scalar machines is a problem that was solved ten years ago.”
- -David Kuck, 1990, Professor at UIUC
- Architectures keep changing (ARM and Qualcomm
Snapdragon are popular processors/architectures)
- Languages keep changing (think about compiling
JavaScript)
- Applications keep changing, (i.e. Android apps)
- When to compile keeps changing, i.e. JIT (just-in-time
compilation)
3
What You Will Learn
- Methodology of
compiler development
- Theoretical framework
- Key algorithms
- Hands-on experience
- Practical trade-offs
- Compiler optimisations
- Reliability tools
- Extracting parallelism
- Inner working of
runtimes
4
Nongoal: build a complete optimizing compiler
Why Is This Useful and Important
- Software developers
interact with compilers
- daily. Understanding
how the process of transforming a (static) program into (runtime) execution simply makes you a better developer
- Knowing how to build such
tools gives you an extra edge in development
- Knowing how optimisations
work gives you the ability to harness the compiler to its full extent
- Unlocking parallelism leads
to significant performance improvements on today’s architectures
5
Studying Compilers Changes Your Mindset
- Excellent software-
engineering example – theory meets practice: need to understand the math and also create real systems
- You start thinking about
software/hardware interactions a lot more
- Compilers are
everywhere: An essential way of thinking about software
- development. For
example, consider DSLs for boosting developer productivity for databases, graphics, networking, bio- computation, IoT, etc.
6
Finding Bugs with FB Infer
7
Requires Pretty Deep Code Analysis
- Can be shallow, i.e.
syntactic matching such as grep
- Can be intraprocedural
– perform analysis within a single procedure
- Can be a little deeper,
interprocedural
- What are some of the
concerns in building such an analysis?
8
Surprising Applications
- f Compilers: PrePose
- Programming games is painful
- Part of this is because games require gestures to
be defined
- Today, gesture definition is a pain process, like the
punch gesture shown on the right in C++
// Punch Gesture if ( vHandPos.z – vShoulderPos.z > fThreshold1 && fVelocityOfHand > fThreshold2 || fVelocityOfElbow > fThreshold3 && DotProduct( vUpperArm, vLowerArm) > fThreshold4 ) { bDetect = TRUE; }
9
PrePose – a Language for gesture-based Programming
10
Ballet Gestures in PrePose
11
Encoding Tai Chi Gestures
12
IKEA Furniture Assembly Programming
- Provide instructions
for the furniture assembler in a specialized, domain- specific language
- Generate manuals
containing pictures and text in multiple languages
- Generate an
assembly plan for a specialized robot
13
Compilers are everywhere: An essential way of thinking about software development
14
Administrative Matters
15
Course Staff
- Professor:
- Dr. Ben Livshits (Reader in Computing)
- TA’s:
- Anastasios Andronidis
- Course page – everything will be here:
https://www.doc.ic.ac.uk/~livshits/classes/CO444H
16
Course Requirements
Requirements
- It is generally assumed that
you have taken beginner Compilers CO221
- Come talk to me if you have
questions
- You should be quite
comfortable programming in Java and ideally Python
- r C++
Workload
- Two (programming)
assignments
- Some tutorials
- Exam
17
Course Resources: Slides + …
18
COMPILERS: PRINCIPLES TECHNIQUES AND TOOLS, second edition, 2013
Dragon Book (Ch. 1 — 5) (previous class)
- Chapter 1 contains
motivational materials
- Chapter 2 develops a
miniature compiler
- Chapter 3 covers lexical
analysis, regular expressions, finite-state machines, and scanner- generator tools
- Chapter 4 covers the major
parsing methods, top-down (recursive-descent, LL) and bottom-up (LR and its variants).
- Chapter 5 introduces the
principal ideas in syntax- directed definitions and syntax-directed translations.
19
Dragon Book (Ch. 6 — 9)
- Chapter 6 takes the theory
- f Chapter 5 and shows
how to use it to generate intermediate code for a typical programming language.
- Chapter 7 covers run-time
environments, especially management of the run- time stack and garbage collection.
- Chapter 8 is on object-code
- generation. It covers
construction of basic blocks, generation of code from expressions and basic blocks, and register- allocation techniques.
- Chapter 9 covers flow
graphs, data-flow frameworks, and iterative algorithms
20
Dragon Book (Ch. 10 — 12)
- Chapter 10 covers
instruction-level
- ptimization. The emphasis
is on the extraction of parallelism from small sequences of instructions and scheduling them on single processors that can do more than one thing at
- nce.
- Chapter 11 talks about
larger-scale parallelism detection and exploitation.
- Chapter 12 is on
interprocedural analysis. It covers pointer analysis, aliasing, and call graph construction, etc.
21
Compiler Organisation
22
Language 1 source Language 2 source
Lexical analyzer (scanner) Syntax/semantic analyzer (parser) Intermediate code generator Lexical analyzer (scanner) Syntax/semantic analyzer (parser) Intermediate code generator Intermediate code
- ptimizer and analyzer
Target 1 code generator Target 2 code generator
Program Machine code
Structural inlining loop unrolling loop perm Scalar CSE constants expressions Memory scalar repl ptrs Register allocation Scheduling peephole
Analysis Flavours
Scope of program analysis
- within a basic block (local) – start here
- within a method (global)
- across methods (interprocedural)
Analysis types we will consider…
23
control flow graph dominators, loops, etc. dataflow analysis flow of values static-single-assignment transform programs such that each variable has a unique definition alias analysis pointer memory usage dependence analysis parallelism
Break…
24
Some of the Basics
Flow Graphs Constant Folding Global Common Subexpressions Induction Variables/Reduction in Strength
25
Intermediate Code
for (i=0; i<n; i++) { A[i] = 1; }
- Intermediate code
exposes optimizable constructs we cannot see at the source-code level
- General idea
- Lowering passes
- Start with a high-level
program
- Translate it into levels of
intermediate representation (IR)
- Often, the next level of
IR will be lower level and will enable new analysis opportunities
26
Basic Blocks in a Control Flow Graph (CFG)
- Make program control
flow explicit by breaking into basic blocks
- What are basic blocks?
- Sequences of
instructions with entry at beginning, exit at end.
27
i = 0 if i>=n goto … t1 = 8*i A[t1] = 1 i = i+1
28
Induction Variables
- x is an induction variable in a loop if it takes on a linear
sequence of values each time through the loop.
- Common case: loop index like i and computed array
index like t1
- Eliminate “superfluous” induction variables
- Replace multiplication by addition (reduction in
strength)
29
Example of Induction Variables
i = 0 if i>=n goto … t1 = 8*i A[t1] = 1 i = i+1 t1 = 0 n1 = 8*n if t1>=n1 goto … A[t1] = 1 t1 = t1+8
30
Loop-Invariant Code Motion
- Sometimes, a computation is done each time
around a loop
- Move it before the loop to save n-1 computations.
- Be careful: could n=0?
- i.e., the loop is typically executed 0 times
31
Example of Loop-Invariant Code Motion
i = 0 if i>=n goto … t1 = y+z x = x+t1 i = i+1 i = 0 t1 = y+z if i>=n goto … x = x+t1 i = i+1
32
Constant Folding
- Sometimes a variable has a known
constant value at a point
- If so, replacing the variable by the
constant simplifies and speeds-up the code.
- Easy within a basic block; harder across
blocks
33
Example of Constant Folding
i = 0 n = 100 if i>=n goto … t1 = 8*i A[t1] = 1 i = i+1 t1 = 0 if t1>=800 goto … A[t1] = 1 t1 = t1+8
Global Common Subexpressions
- Suppose basic block B has a computation of x+y
- Suppose we are sure that when we reach this
computation, we are sure to have:
1. Computed x+y, and 2. Not subsequently reassigned x or y
- Then we can hold the value of x+y and use it in B
34
35
Example of Global Common Subexpression Elimination
a = x+y b = x+y c = x+y t = x+y a = t t = x+y b = t c = t
36
Example of CSE – Even Better
t = x+y a = t t = x+y b = t c = t t = x+y a = t b = t c = t t = x+y b = t
Q&A on the Optimisations Above
- Can we sometimes lose optimisation opportunities as a
result of lowering IR?
- Is it trivial to decide which basic blocks to connect to each
- ther?
- Can the optimisations we described above reduce the
performance?
- Can optimisations take more time to perform than they
save?
37