Using GCC as a Research Compiler

Similar documents
The GNU Compiler Collection

An Architectural Overview of GCC

GCC An Architectural Overview

GCC Internals Alias analysis

GCC Internals Control and data flow support

Design and Implementation of Tree SSA

A Propagation Engine for GCC

Compiler Passes. Optimization. The Role of the Optimizer. Optimizations. The Optimizer (or Middle End) Traditional Three-pass Compiler

Why Global Dataflow Analysis?

Extension of GCC with a fully manageable reverse engineering front end

6. Intermediate Representation!

6. Intermediate Representation

8. Static Single Assignment Form. Marcus Denker

High Performance Computing

The Design and Implementation of Gnu Compiler Collection

Intermediate Representation (IR)

Intermediate representation

CS 406/534 Compiler Construction Putting It All Together

Intermediate Representations

ECE 5775 (Fall 17) High-Level Digital Design Automation. Static Single Assignment

The Development of Static Single Assignment Form

Calvin Lin The University of Texas at Austin

An Overview of GCC Architecture (source: wikipedia) Control-Flow Analysis and Loop Detection

Compiler Theory. (GCC the GNU Compiler Collection) Sandro Spina 2009

9/5/17. The Design and Implementation of Programming Languages. Compilation. Interpretation. Compilation vs. Interpretation. Hybrid Implementation

Under the Compiler's Hood: Supercharge Your PLAYSTATION 3 (PS3 ) Code. Understanding your compiler is the key to success in the gaming world.

GCC Developers Summit Ottawa, Canada, June 2006

CSE P 501 Compilers. Intermediate Representations Hal Perkins Spring UW CSE P 501 Spring 2018 G-1

GENERIC and GIMPLE: A New Tree Representation for Entire Functions

CSE 501 Midterm Exam: Sketch of Some Plausible Solutions Winter 1997

Induction-variable Optimizations in GCC

Induction Variable Identification (cont)

Combining Analyses, Combining Optimizations - Summary

How to write a GCC plugin with MELT?

CSE P 501 Compilers. SSA Hal Perkins Spring UW CSE P 501 Spring 2018 V-1

Compiler Optimization

Control Flow Analysis & Def-Use. Hwansoo Han

Lecture 23 CIS 341: COMPILERS

CSE 401/M501 Compilers

5. Semantic Analysis!

5. Semantic Analysis. Mircea Lungu Oscar Nierstrasz

Intermediate Code Generation

Introduction to Machine-Independent Optimizations - 6

Topic I (d): Static Single Assignment Form (SSA)

SSA Construction. Daniel Grund & Sebastian Hack. CC Winter Term 09/10. Saarland University

INTRODUCTION TO LLVM Bo Wang SA 2016 Fall

Using Static Single Assignment Form

Usually, target code is semantically equivalent to source code, but not always!

15-411: LLVM. Jan Hoffmann. Substantial portions courtesy of Deby Katz

Intermediate Code & Local Optimizations

LLVM and IR Construction

Compiler Design. Fall Data-Flow Analysis. Sample Exercises and Solutions. Prof. Pedro C. Diniz

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

Principles of Compiler Design

Lecture 16: Static Semantics Overview 1

Programming for Engineers Structures, Unions

LECTURE 3. Compiler Phases

Intermediate Representations & Symbol Tables

The Design and Implementation of Gnu Compiler Collection

Flow-sensitive Alias Analysis

Reuse Optimization. LLVM Compiler Infrastructure. Local Value Numbering. Local Value Numbering (cont)

CS553 Lecture Dynamic Optimizations 2

ECE 5775 High-Level Digital Design Automation Fall More CFG Static Single Assignment

Lecture 3 Local Optimizations, Intro to SSA

CS691/SC791: Parallel & Distributed Computing

Scientific Programming in C X. More features & Fortran interface

Alternatives for semantic processing

HPE Security Data Security. HPE SecureData. Product Lifecycle Status. End of Support Dates. Date: April 20, 2017 Version:

Scaling on one node Hybrid engines with Multi-GPU on In-Memory database queries

Code Merge. Flow Analysis. bookkeeping

CS 4240: Compilers and Interpreters Project Phase 1: Scanner and Parser Due Date: October 4 th 2015 (11:59 pm) (via T-square)

Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan

CS5363 Final Review. cs5363 1

Lecture Compiler Middle-End

1 Lexical Considerations

Auto-Vectorization with GCC

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

TPTP 4.7 Release Review -- One Quick Foil What s new

Interprocedural Analysis. Motivation. Interprocedural Analysis. Function Calls and Pointers

Type Definition. C Types. Derived. Function Array Pointer Structure Union Enumerated. EE 1910 Winter 2017/18

Pointer Arithmetic and Lexical Scoping. CS449 Spring 2016

Principles of Programming Languages

COURSE LISTING. Courses Listed. Training for Cloud with SAP Cloud Platform in Development. 23 November 2017 (08:12 GMT) Beginner.

CS153: Compilers Lecture 23: Static Single Assignment Form

COURSE LISTING. Courses Listed. Training for Database & Technology with Development in SAP Cloud Platform. 1 December 2017 (22:41 GMT) Beginner

Control Center Release Notes

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

CS 314 Principles of Programming Languages

Why are there so many programming languages? Why do we have programming languages? What is a language for? What makes a language successful?

Static Single Assignment Form in the COINS Compiler Infrastructure

Calvin Lin The University of Texas at Austin

ELEC 876: Software Reengineering

Advanced C Programming

CJT^jL rafting Cm ompiler

GCC Internals: A Conceptual View Part II

Plugin Mechanisms in GCC

Compiling for HSA accelerators with GCC

The structure of a compiler

Intermediate Representations

Transcription:

Using GCC as a Research Compiler Diego Novillo dnovillo@redhat.com Computer Engineering Seminar Series North Carolina State University October 25, 2004

Introduction GCC is a popular compiler, freely available and with an open development model. However Code base large (2.1 MLOC) and aging (~15 years). Optimization framework based on a single IL (RTL). Monolithic middle-end difficult to maintain and extend. Recent architectural changes are dragging GCC kicking and screaming into the 90s. New Intermediate Representations (GENERIC and GIMPLE). New SSA-based global optimization framework. New API for implementing new passes. 2

GCC strengths One of the most popular compilers. Very wide user base lots of Standard compiler for Linux. test cases. Virtually all open/free source projects use it. Supports a wide variety of languages: C, C++, Java, Fortran, Ada, ObjC, ObjC++. Ported from deeply embedded to mainframes. Active and numerous development team. Free Software and open development process. So, what's wrong with it? 3

Problem 1 - Modularity New ports: straightforward Mostly driven by big embedded demand during 90s. Target description language flexible and well documented. Low-level optimizations: hard Too many target dependencies (some are to be expected). Little infrastructure support (no CFG until ~1999). New languages: very hard Front ends emit RTL almost directly. No clear separation between FE and BE. High-level optimizations: sigh RTL is the only IL available. No infrastructure to manipulate/analyse high-level constructs. 4

Problem 2 Lack of abstraction Single IL used for all optimization RTL not suited for high-level analyses/transformations. Original data type information mostly lost Addressing modes replace variable references C trees C expander C++ trees C++ expander RTL RTL Optimizer Java trees Java expander 5

Problem 3 Too much abstraction Parse trees contain complete control/data/type information. In principle, well suited for transformations closer to the source Scalar cleanups. Instrumentation. Loop transformations. However No common representation across all front ends. Side effects are allowed. Structurally complex. 6

Tree SSA Project started late 2000 as weekend hobby. Goal: SSA framework for high-level optimization. Approach: Evolution, not revolution immediate integration. Features Clear separation between FE and BE. FEs generate common high-level IL that is both language and target independent. Gradual lowering of IL. Common API for CFG, statements, operands, aliasing. Optimization framework: dom-tree walker, generic propagator, use-def chain walker, loop discovery, etc. 30+ passes implemented so far. 7

GENERIC and GIMPLE - 1 GENERIC is a common representation shared by all front ends Parsers may build their own representation for convenience. Once parsing is complete, they emit GENERIC. GIMPLE is a simplified version of GENERIC. 3-address representation. Restricted grammar to facilitate the job of optimizers. C trees C genericizer C++ trees C++ genericizer GENERIC gimplifier GIMPLE tree optimizers RTL Java trees Java genericizer Often, FEs just generate GIMPLE 8

GENERIC and GIMPLE - 2 GENERIC High GIMPLE Low GIMPLE if (foo (a + b, c)) c = b++ / a endif return c t1 = a + b t2 = foo (t1, c) if (t2!= 0) t3 = b b = b + 1 c = t3 / a endif return c t1 = a + b t2 = foo (t1, c) if (t2!= 0) <L1,L2> L1: t3 = b b = b + 1 c = t3 / a goto L3 L2: L3: return c 9

Properties of GIMPLE form No hidden/implicit side-effects. Simplified control flow Loops represented with if/goto. Lexical scopes removed (low-gimple). Locals of scalar types are treated as registers. Globals, aliased variables and non-scalar types treated as memory. At most one memory load/store operation per statement. Memory loads only on RHS of assignments. Stores only on LHS of assignments. Can be incrementally lowered (2 levels currently). 10

Statement Operands - 1 Real operands Non-aliased, scalar, local variables. Atomic references to the whole object. GIMPLE registers (may not fit in a physical register). double x, y, z; z = x + y; Virtual operands Globals, aliased, structures, arrays, pointer dereferences. Potential and/or partial references to the object. Distinction becomes important when building SSA form. int x[10]; struct A y; x[3] = y.f; 11

Statement Operands - 2 Types of virtual operands: Partial, potential and/or aliased stores (V_MAY_DEF) p = (cond)? &a : &b # a = V_MAY_DEF <a> # b = V_MAY_DEF <b> *p = x + 1 Partial, total and/or aliased loads (V_USE) # V_USE <s> y = s.f Killing definitions of aggregates and globals (V_MUST_DEF) # s = V_MUST_DEF <s> s = u # a = V_MAY_DEF <a> # b = V_MAY_DEF <b> foo (p) # s = V_MAY_DEF <s> s.f = y # V_USE <a> # V_USE <b> x = *p 12

Alias Analysis - 1 GIMPLE only has single level pointers. Pointer dereferences represented by artificial symbols memory tags (MT). If p points-to x p's tag is aliased with x. # MT = V_MAY_DEF <MT> *p =... Since MT is aliased with x: # x = V_MAY_DEF <x> *p =... 13

Alias Analysis - 2 Type Memory Tags (TMT) Used in type-based and flow-insensitive points-to analyses. Tags are associated with symbols. Name Memory Tags (NMT) Used in flow-sensitive points-to analysis. Tags are associated with SSA names. Compiler tries to use name tags first. 14

SSA form - 1 Static Single Assignment (SSA) Versioning representation to expose data flow explicitly. Assignments generate new versions of symbols. Convergence of multiple versions generates new one (Φ functions). Two kinds of SSA forms, one for real another for virtual operands. a_3 = a_1 2 b_4 = b_2 + a_3 a_1 = 3 b_2 = 9 if (i_3 > 20) a_5 = a_1 + 1 a_6 = Φ(a_3, a_5) b_7 = Φ(b_4, b_2) c_8 = a_6 + b_7 15

SSA Form - 2 Rewriting (or standard) SSA form Used for real operands. Different names for the same symbol are distinct objects. Optimizations may produce overlapping live ranges (OLR). x_3 = y_2 if (x_2 > 4) z_5 = x_3 1 Currently, program is taken out of SSA form for RTL generation (new symbols are created to fix OLR). Factored Use-Def Chains (FUD Chains) Used for virtual operands. All names refer to the same object. Optimizers may not produce OLR for virtual operands. 16

Implementing SSA passes - 1 17 To implement a new pass 1. Create an instance of struct tree_opt_pass 2. Declare it in tree pass.h 3. Sequence it in init_tree_optimization_passes APIs available for CFG: block/edge insertion, removal, dominance information, block iterators, dominance tree walker. Statements: insertion in block and edge, removal, iterators, replacement. Operands: iterators, replacement. Loop discovery and manipulation. Data dependency information (scalar evolutions framework).

Implementing SSA passes - 2 Other available infrastructure Debugging dumps (-fdump-tree-...) Timers for profiling passes (-ftime-report) CFG/GIMPLE/SSA verification (--enable-checking) Generic value propagation engine with callbacks for statement and Φ node visits. Generic use-def chain walker. Support in test harness for scanning dump files looking for specific transformations. Pass manager for scheduling passes and describing interdependencies, attributes required and attributes provided. 18

Implementation Status Infrastructure Pass manager. CFG, statement and operand iteration/manipulation. SSA renaming and verification. Alias analysis built in the representation. Pointer and array bound checking (mudflap). Generic value propagation support. Optimizations Most traditional scalar passes: DCE, CCP, DSE, SRA, tail call, etc. Some loop optimizations (loop invariant motion, loop unswitching, if-conversion, loop vectorization). 19

Future Work - 1 Short term Remove dominator-based optimizations. GVN PRE. Value range propagation. Conditional copy propagation. Copy and constant propagation of loads and stores. Medium term Stabilization and speedup (Bugzilla). Documentation. Tie into fledgling IPA framework. More loop optimizers (LNO branch). 20

Future Work - 2 Long term OpenMP Code factoring/hoisting for size Various type-based optimizations Devirtualization Redundant type checking elimination Escape analysis for Java 21

GCC Development Model - 1 Three main stages Stage 1 - Big disruptive changes. Stage 2 - Stabilization, minor features. Stage 3 - Bug fixes only (driven by bugzilla, mostly). At the end of stage 3, release branch is cut and stage 1 for next version begins. Major development that spans multiple releases is done in branches. Anyone with CVS access may create a development branch. Vendors create own branches from FSF release branches. 22

GCC Development Model - 2 All contributors must sign FSF copyright release. Even if only working on branches. Three levels of access Snapshots (weekly). Anonymous CVS. Read/write CVS. Major work on branches encouraged Design/implementation discussion on public lists. Frequent merges from mainline to avoid code drift. Final contribution into mainline only at stage 1 and approved by maintainers. 23

Project History - 1 Late 2000 Mar 2001 Jul 2001 Jan 2002 May 2002 Jun 2002 Project starts. CFG/Factored UD chains on C trees. Added to ast-optimizer-branch. Pretty printing and SIMPLE for C. SSA-PRE. Move to tree-ssa-20020619-branch. SIMPLE for C++. 24

Project History - 2 Jul 2002 SSA-CCP. Flow insensitive points-to analysis. Aug 2002 Oct 2002 Nov 2002 Jan 2003 Mudflap and SSA-DCE. GIMPLE and GENERIC. Tree browser. Replace FUD chains with rewriting SSA form. 25

Project History - 3 Feb 2003 Apr 2003 Jun 2003 Jul 2003 Sep 2003 Nov 2003 Statement iterators. Out of SSA pass. Dominator-based optimizations. GIMPLE for Java. Fortran 95 front end. EH lowering. Memory management for SSA names and PHI nodes. 26

Project History - 4 Nov 2003 Dec 2003 Scalar Replacement of Aggregates. Statement operands API. Pass manager. Jan 2004 Feb 2004 Complex numbers lowering. Flow-sensitive and escape analysis, PHI optimization, forward propagation, function unnesting, tree profiling, DSE, NRV. 27