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 “old - school” • Find security flaws optimisations before they are • We will see some that are exploited by hackers based on parallelism • Help developers • We will see some runtime produce better code optimisations 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 optimisations compiler development • Reliability tools • Theoretical framework • Extracting parallelism • Key algorithms • Inner working of • Hands-on experience runtimes • Practical trade-offs Nongoal: build a complete optimizing compiler 4
Why Is This Useful and Important • Knowing how to build such • Software developers tools gives you an extra interact with compilers edge in development • Knowing how optimisations daily. Understanding work gives you the ability to how the process of harness the compiler to its full extent transforming a (static) • Unlocking parallelism leads program into (runtime) to significant performance execution simply makes improvements on today’s architectures you a better developer 5
Studying Compilers Changes Your Mindset • Excellent software- • Compilers are engineering example – everywhere: An theory meets practice: essential way of need to understand the thinking about software math and also create development. For real systems example, consider DSLs for boosting developer • You start thinking about productivity for software/hardware databases, graphics, interactions a lot more networking, bio- computation, IoT, etc. 6
Finding Bugs with FB Infer 7
Requires Pretty Deep Code Analysis • Can be shallow, i.e. • What are some of the syntactic matching such concerns in building as grep such an analysis? • Can be intraprocedural – perform analysis within a single procedure • Can be a little deeper, interprocedural 8
Surprising Applications of 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 Workload • It is generally assumed that • Two (programming) you have taken beginner assignments Compilers CO221 • Some tutorials • Come talk to me if you have • Exam questions • You should be quite comfortable programming in Java and ideally Python or C++ 17
Course Resources: Slides + … COMPILERS: PRINCIPLES TECHNIQUES AND TOOLS, second edition, 2013 18
Dragon Book (Ch. 1 — 5) (previous class) • Chapter 1 contains • Chapter 4 covers the major motivational materials parsing methods, top-down (recursive-descent, LL) and • Chapter 2 develops a bottom-up (LR and its miniature compiler variants). • Chapter 3 covers lexical • Chapter 5 introduces the analysis, regular principal ideas in syntax- expressions, finite-state directed definitions and machines, and scanner- syntax-directed generator tools translations. 19
Dragon Book (Ch. 6 — 9) • Chapter 6 takes the theory • Chapter 8 is on object-code of Chapter 5 and shows generation. It covers how to use it to generate construction of basic intermediate code for a blocks, generation of code typical programming from expressions and basic language. blocks, and register- allocation techniques. • Chapter 7 covers run-time • Chapter 9 covers flow environments, especially management of the run- graphs, data-flow time stack and garbage frameworks, and iterative collection. algorithms 20
Dragon Book (Ch. 10 — 12) • Chapter 10 covers • Chapter 11 talks about instruction-level larger-scale parallelism optimization. The emphasis detection and exploitation. is on the extraction of • Chapter 12 is on parallelism from small interprocedural analysis. It sequences of instructions covers pointer analysis, and scheduling them on aliasing, and call graph single processors that can construction, etc. do more than one thing at once. 21
Compiler Language 1 Language 2 Organisation source source Lexical analyzer Lexical analyzer (scanner) (scanner) Syntax/semantic Syntax/semantic analyzer (parser) analyzer (parser) Intermediate code Intermediate code generator generator Machine Program code Intermediate code Structural Scalar Memory Register Scheduling optimizer and analyzer inlining CSE scalar repl allocation peephole loop unrolling constants ptrs loop perm expressions Target 1 code Target 2 code generator generator 22
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… 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 23 dependence analysis parallelism
Break… 24
Some of the Basics Flow Graphs Constant Folding Global Common Subexpressions Induction Variables/Reduction in Strength 25
Intermediate Code • General idea for (i=0; i<n; i++) • Lowering passes { • Start with a high-level A[i] = 1; program } • Translate it into levels of intermediate representation (IR) • Intermediate code • Often, the next level of exposes optimizable IR will be lower level constructs we cannot see and will enable new at the source-code level analysis opportunities 26
Basic Blocks in a Control Flow Graph (CFG) • Make program control i = 0 flow explicit by breaking into basic blocks if i>=n goto … • What are basic blocks? • Sequences of t1 = 8*i instructions with entry A[t1] = 1 at beginning, exit at i = i+1 end. 27
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) 28
Example of Induction Variables t1 = 0 i = 0 n1 = 8*n if i>=n goto … if t1>=n1 goto … t1 = 8*i A[t1] = 1 A[t1] = 1 t1 = t1+8 i = i+1 29
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 30
Example of Loop-Invariant Code Motion i = 0 i = 0 t1 = y+z if i>=n goto … if i>=n goto … t1 = y+z x = x+t1 x = x+t1 i = i+1 i = i+1 31
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 32
Example of Constant Folding i = 0 t1 = 0 n = 100 if i>=n goto … if t1>=800 goto … t1 = 8*i A[t1] = 1 A[t1] = 1 t1 = t1+8 i = i+1 33
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
Example of Global Common Subexpression Elimination t = x+y a = x+y a = t t = x+y b = x+y b = t c = x+y c = t 35
Example of CSE – Even Better t = x+y t = x+y a = t a = t t = x+y t = x+y b = t b = t b = t c = t c = t 36
Recommend
More recommend