A Summary of Essential Abstractions Uday Khedker - - PowerPoint PPT Presentation

a summary of essential abstractions
SMART_READER_LITE
LIVE PREVIEW

A Summary of Essential Abstractions Uday Khedker - - PowerPoint PPT Presentation

A Summary of Essential Abstractions Uday Khedker (www.cse.iitb.ac.in/uday) GCC Resource Center, Department of Computer Science and Engineering, Indian Institute of Technology, Bombay 13 June 2014 Part 2 Methodology EAGCC-PLDI-14


slide-1
SLIDE 1

A Summary of Essential Abstractions

Uday Khedker

(www.cse.iitb.ac.in/˜uday) GCC Resource Center, Department of Computer Science and Engineering, Indian Institute of Technology, Bombay 13 June 2014
slide-2
SLIDE 2

Part 2

Methodology

slide-3
SLIDE 3 EAGCC-PLDI-14 Essential Abstractions: Methodology 1/31

Our Padagogy

Compiler Specifications Compiler Generator Generated Compiler External View Internal View Machine descriptions Front end hooks Configuration and building Retargetability mechanism Gray box probing Pass structure and IR Parallelization, Vectorization Pass structure Control flow Static and dynamic plugin mechanisms

Uday Khedker GRC, IIT Bombay
slide-4
SLIDE 4 EAGCC-PLDI-14 Essential Abstractions: Methodology 2/31

Gray Box Probing

Phase 1 Phase 2 . . . Phase n Black Box Probing Observe Observe

Uday Khedker GRC, IIT Bombay
slide-5
SLIDE 5 EAGCC-PLDI-14 Essential Abstractions: Methodology 2/31

Gray Box Probing

Phase 1 Phase 2 . . . Phase n White Box Probing Observe Observe

Uday Khedker GRC, IIT Bombay
slide-6
SLIDE 6 EAGCC-PLDI-14 Essential Abstractions: Methodology 2/31

Gray Box Probing

Phase 1 Phase 2 . . . Phase n Observe Observe Gray Box Probing Observe Observe

Uday Khedker GRC, IIT Bombay
slide-7
SLIDE 7 EAGCC-PLDI-14 Essential Abstractions: Methodology 3/31

Systematic Development of Machine Descriptions

Other data types Conditional control transfers Function Calls Arithmetic Expressions Sequence of Simple Assignments involving integers MD Level 1 MD Level 2 MD Level 3 MD Level 4 MD Level 5 Uday Khedker GRC, IIT Bombay
slide-8
SLIDE 8

Part 3

The Framework

slide-9
SLIDE 9 EAGCC-PLDI-14 Essential Abstractions: The Framework 4/31

The GNU Tool Chain for C

gcc Source Program Target Program cc1 cpp cc1 cpp as ld glibc/newlib

Uday Khedker GRC, IIT Bombay
slide-10
SLIDE 10 EAGCC-PLDI-14 Essential Abstractions: The Framework 5/31

The Architecture of GCC

Language Specific Code Language and Machine Independent Generic Code Machine Dependent Generator Code Machine Descriptions Compiler Generation Framework Parser Gimplifier Tree SSA Optimizer Expander Optimizer Recognizer Generated Compiler (cc1) Source Program Assembly Program Input Language Target Name Selected Copied Copied Generated Generated Development Time Build Time Use Time Uday Khedker GRC, IIT Bombay
slide-11
SLIDE 11

Part 4

The Generated Compiler

slide-12
SLIDE 12 EAGCC-PLDI-14 Essential Abstractions: The Generated Compiler 6/31

Compilation Models

Aho Ullman Model Davidson Fraser Model Front End AST Optimizer Target Indep. IR Code Generator Target Program Front End AST Expander Register Transfers Optimizer Register Transfers Recognizer Target Program Aho Ullman: Instruction selection

  • over optimized IR using
  • cost based tree pattern matching

Davidson Fraser: Instruction selection

  • over AST using
  • structural tree pattern matching
  • naive code which is
◮ target dependent, and is ◮ optimized subsequently Uday Khedker GRC, IIT Bombay
slide-13
SLIDE 13 EAGCC-PLDI-14 Essential Abstractions: The Generated Compiler 7/31

Basic Transformations in GCC

Tranformation from a language to a different language

Target Independent Target Dependent

Parse Gimplify Tree SSA Optimize Generate RTL Optimize RTL Generate ASM

GIMPLE → RTL RTL → ASM RTL Passes GIMPLE Passes

Uday Khedker GRC, IIT Bombay
slide-14
SLIDE 14 EAGCC-PLDI-14 Essential Abstractions: The Generated Compiler 8/31

Plugin Structure in cc1

toplev main front end pass manager For simplicity, we have included all passes in a single list. Actually passes are organized into five lists and are invoked as five different sequences pass 1 pass 2

. . .

pass expand

. . .

pass n code for pass 2 code for pass 1 recognizer code expander code
  • ptab table
langhook . . . code for language 1 code for language 2 code for language n insn data generated code for machine 1 MD 1 MD 2 MD n Uday Khedker GRC, IIT Bombay
slide-15
SLIDE 15 EAGCC-PLDI-14 Essential Abstractions: The Generated Compiler 9/31

The Mechanism of Dynamic Plugin

pass manager

. . . . . .

code for pass code for pass recognizer code for expander code
  • ptab table
code for dynamic plugin Runtime initialization
  • f the appropriate
linked list of passes Made possible by dynamic linking Uday Khedker GRC, IIT Bombay
slide-16
SLIDE 16 EAGCC-PLDI-14 Essential Abstractions: The Generated Compiler 10/31

Execution Order in Intraprocedural Passes

Function 1 Function 2 Function 3 Function 4 Function 5 Pass 1 Pass 2 Pass 3 Pass 4 Pass 5

Uday Khedker GRC, IIT Bombay
slide-17
SLIDE 17 EAGCC-PLDI-14 Essential Abstractions: The Generated Compiler 11/31

Execution Order in Interprocedural Passes

Function 1 Function 2 Function 3 Function 4 Function 5 Pass 1 Pass 2 Pass 3 Pass 4 Pass 5

Uday Khedker GRC, IIT Bombay
slide-18
SLIDE 18

Part 5

LTO

slide-19
SLIDE 19 EAGCC-PLDI-14 Essential Abstractions: LTO 12/31

Partitioned and Non-Partitioned LTO

Analysis Sequential Analysis Transformation Load complete call graph Load function summaries but not bodies Load all function bodies Load all function bodies Load function bodies

  • ne by one

Load groups

  • f function

bodies All function bodies already loaded

××

No need to load the entire program in memory IPA possible (multiple function bodies) Parallel transformations possible Analysis and transformations in independent processes Partitioned Mode

Uday Khedker GRC, IIT Bombay
slide-20
SLIDE 20 EAGCC-PLDI-14 Essential Abstractions: LTO 12/31

Partitioned and Non-Partitioned LTO

Analysis Sequential Analysis Transformation Load complete call graph Load function summaries but not bodies Load all function bodies Load all function bodies Load function bodies

  • ne by one

Load groups

  • f function

bodies All function bodies already loaded

××

Partitioned Mode Balanced partitions -flto -flto-partitions=balanced One Partition per file -flto -flto-partitions=1to1 Partitions by number -flto --params lto-partitions=n Partitions by size -flto --params lto-min-partition=s

Uday Khedker GRC, IIT Bombay
slide-21
SLIDE 21 EAGCC-PLDI-14 Essential Abstractions: LTO 12/31

Partitioned and Non-Partitioned LTO

Analysis Sequential Analysis Transformation Load complete call graph Load function summaries but not bodies Load all function bodies Load all function bodies Load function bodies

  • ne by one

Load groups

  • f function

bodies All function bodies already loaded

××

Non-Partitioned Mode Entire program needs to be loaded in memory No partitions -flto -flto-partitions=none Strictly sequential transformations Analysis and transformations in the same processes

Uday Khedker GRC, IIT Bombay
slide-22
SLIDE 22 EAGCC-PLDI-14 Essential Abstractions: LTO 13/31

cc1 and Single Process lto1

toplev main ... compile file ... cgraph analyze function cgraph optimize ... ipa passes ... cgraph expand all functions ... tree rest of compilation cc1

Uday Khedker GRC, IIT Bombay
slide-23
SLIDE 23 EAGCC-PLDI-14 Essential Abstractions: LTO 13/31

cc1 and Single Process lto1

toplev main ... compile file ... cgraph analyze function lto main ... read cgraph and symbols ... materialize cgraph cgraph optimize ... ipa passes ... cgraph expand all functions ... tree rest of compilation lto1

Uday Khedker GRC, IIT Bombay
slide-24
SLIDE 24 EAGCC-PLDI-14 Essential Abstractions: LTO 14/31

The GNU Tool Chain for Single Process LTO Support

gcc cc1′ lto1′ common cc1 “Fat” .s files as as “Fat” .o files collect2 cc1′ lto1′ common lto1 Single .s file as as Single .o file collect2 + glibc/newlib ld ld a.out file

Uday Khedker GRC, IIT Bombay
slide-25
SLIDE 25 EAGCC-PLDI-14 Essential Abstractions: LTO 14/31

The GNU Tool Chain for Single Process LTO Support

gcc cc1′ lto1′ common cc1 “Fat” .s files as as “Fat” .o files collect2 cc1′ lto1′ common lto1 Single .s file as as Single .o file collect2 + glibc/newlib ld ld a.out file Common Code (executed twice for each function in the input program for single process LTO. Once during LGEN and then during WPA + LTRANS) cgraph optimize ipa passes execute ipa pass list(all small ipa passes)/*!in lto*/ execute ipa summary passes(all regular ipa passes) execute ipa summary passes(all lto gen passes) ipa write summaries execute ipa pass list(all late ipa passes) cgraph expand all functions cgraph expand function /* Intraprocedural passes on GIMPLE, */ /* expansion pass, and passes on RTL. */

Uday Khedker GRC, IIT Bombay
slide-26
SLIDE 26 EAGCC-PLDI-14 Essential Abstractions: LTO 15/31

Partitioned LTO (aka WHOPR LTO)

f1.c

cc1′ lto1′ common

f1.o Option -flto -c f2.c

cc1′ lto1′ common

f2.o f3.c

cc1′ lto1′ common

f3.o

cc1′ lto1′ common

Option

  • flto -o out
  • ut

large call graph without procedure bodies (Interproc. analysis: √ Tranformation: ×) /tmp/ccdKEyVB.ltrans0.o (possibly multiple files)

cc1′ lto1′ common

(possibly multiple files) LGEN WPA LTRANS

Uday Khedker GRC, IIT Bombay
slide-27
SLIDE 27 EAGCC-PLDI-14 Essential Abstractions: LTO 16/31

(Non-Partitioned LTO)

f1.c

cc1′ lto1′ common

f1.o Option -flto -c f2.c

cc1′ lto1′ common

f2.o f3.c

cc1′ lto1′ common

f3.o

cc1′ lto1′ common

Option

  • flto -o out
  • flto-partition=none
  • ut

large call graph with procedure bodies (Interproc. analysis: √ Transformation: √) LGEN IPA + Transformations This IPA can examine function bodies also

Uday Khedker GRC, IIT Bombay
slide-28
SLIDE 28

Part 6

The Build Process

slide-29
SLIDE 29 EAGCC-PLDI-14 Essential Abstractions: The Build Process 17/31

Configuring GCC

configure config.guess configure.in config/* config.sub config.log config.cache config.status config.h.in Makefile.in Makefile config.h

Uday Khedker GRC, IIT Bombay
slide-30
SLIDE 30 EAGCC-PLDI-14 Essential Abstractions: The Build Process 18/31

Bootstrapping: The Conventional View

Cn−1 Cn−2 m/c Cn Cn−1 m/c input language

  • utput language

implementation language Level n C

Uday Khedker GRC, IIT Bombay
slide-31
SLIDE 31 EAGCC-PLDI-14 Essential Abstractions: The Build Process 19/31

A Native Build on i386

Requirement: BS = HS = TS = i386

  • Stage 1 build compiled using cc
  • Stage 2 build compiled using gcc
  • Stage 3 build compiled using gcc
  • Stage 2 and Stage 3 Builds must be

identical for a successful native build GCC Source C i386 i386 cc C i386 C i386 i386 gcc Stage 1 Build C i386 i386 gcc Stage 2 Build C i386 i386 gcc Stage 3 Build

Uday Khedker GRC, IIT Bombay
slide-32
SLIDE 32 EAGCC-PLDI-14 Essential Abstractions: The Build Process 20/31

Build for a Given Machine

This is what actually happens!

  • Generation
◮ Generator sources

($(SOURCE D)/gcc/gen*.c) are read and generator executables are created in $(BUILD)/gcc/build

◮ MD files are read by the generator

executables and back end source code is generated in $(BUILD)/gcc

  • Compilation

Other source files are read from $(SOURCE D) and executables created in corresponding subdirectories of $(BUILD)

  • Installation

Created executables and libraries are copied in $(INSTALL) genattr gencheck genconditions genconstants genflags genopinit genpreds genattrtab genchecksum gencondmd genemit gengenrtl genmddeps genoutput genrecog genautomata gencodes genconfig genextract gengtype genmodes genpeep

Uday Khedker GRC, IIT Bombay
slide-33
SLIDE 33 EAGCC-PLDI-14 Essential Abstractions: The Build Process 21/31

More Details of an Actual Stage 1 Build for C

native cc, binutils, libraries GCC sources libraries libiberty fixincl gen* cc1 cpp xgcc libgcc target binutils, libraries cc, binutils, libraries for stage 2

Uday Khedker GRC, IIT Bombay
slide-34
SLIDE 34 EAGCC-PLDI-14 Essential Abstractions: The Build Process 22/31

Building a MIPS Cross Compiler on i386: A Closer Look

GCC Source C i386 i386 cc C mips C i386 mips.a cc1 Stage 1 Build mips assembly C mips mips gcc Stage 2 Build

×

Requirement: BS = HS = i386, TS = mips

  • Stage 1 cannot build gcc but can build only cc1
  • Stage 1 build cannot create executables
  • Library sources cannot be compiled for mips using

stage 1 build

  • Stage 2 build is not possible

Stage 2 build is infeasible for cross build we have not built libraries for mips

Uday Khedker GRC, IIT Bombay
slide-35
SLIDE 35 EAGCC-PLDI-14 Essential Abstractions: The Build Process 23/31

Difficulty in Building a Cross Compiler

gcc for target libgcc requires target libraries

uses require

Uday Khedker GRC, IIT Bombay
slide-36
SLIDE 36 EAGCC-PLDI-14 Essential Abstractions: The Build Process 24/31

Generated Compiler Executable for All Languages

  • Main driver

$BUILD/gcc/xgcc

  • C compiler

$BUILD/gcc/cc1

  • C++ compiler

$BUILD/gcc/cc1plus

  • Fortran compiler

$BUILD/gcc/f951

  • Ada compiler

$BUILD/gcc/gnat1

  • Java compiler

$BUILD/gcc/jcl

  • Java compiler for generating main class

$BUILD/gcc/jvgenmain

  • LTO driver

$BUILD/gcc/lto1

  • Objective C

$BUILD/gcc/cc1obj

  • Objective C++

$BUILD/gcc/cc1objplus

Uday Khedker GRC, IIT Bombay
slide-37
SLIDE 37

Part 7

Retargetability

slide-38
SLIDE 38 EAGCC-PLDI-14 Essential Abstractions: Retargetability 25/31

Examples of Influences on the Machine Descriptions

Machine Description Source Language
  • INT TYPE SIZE
  • Activation Record
<target>.h GCC Architecture
  • Generation of nop
  • tree covers for
instruction selection
  • define predicate
<target>.h Build System Host System hwint.h Target System
  • Instruction Set
Architecture
  • Assembly and
executable formats
  • <target>.md
<target>.h <target>.h
  • ther headers
Uday Khedker GRC, IIT Bombay
slide-39
SLIDE 39 EAGCC-PLDI-14 Essential Abstractions: Retargetability 26/31

Redundancy in MIPS Machine Descriptions: Example 3

[(set (match_operand:m 0 "register_operand" "c0") (plus:m (mult:m (match_operand:m 1 "register_operand" "c1") (match_operand:m 2 "register_operand" "c2")))] (match_operand:m 3 "register_operand" "c3")))] RTL Template = + ∗ Structure Details Pattern name m c0 c1 c2 c3 *mul acc si SI =l*?*?,d? d,d d,d 0,d *mul acc si r3900 SI =l*?*?,d*?,d? d,d,d d,d,d 0,1,d *macc SI =l,d d,d d,d 0,1 *madd4<mode> ANYF =f f f f *madd3<mode> ANYF =f f f

Uday Khedker GRC, IIT Bombay
slide-40
SLIDE 40 EAGCC-PLDI-14 Essential Abstractions: Retargetability 27/31

Instruction Specification and Translation: A Recap

Target Independent Target Dependent Parse Gimplify Tree SSA Optimize Generate RTL Optimize RTL Generate ASM GIMPLE → RTL RTL → ASM
  • GIMPLE: target independent
  • RTL: target dependent
  • Need: associate the semantics
⇒GCC Solution: Standard Pattern Names GIMPLE ASSIGN

RTL Template ASM

(define_insn "movsi" [(set (match_operand 0 "register_operand" "r") (match_operand 1 "const_int_operand" "k"))] "" /* C boolean expression, if required */ "li %0, %1" )

Uday Khedker GRC, IIT Bombay
slide-41
SLIDE 41 EAGCC-PLDI-14 Essential Abstractions: Retargetability 28/31

Translation Sequence in GCC

(define_insn "movsi" [(set (match_operand 0 "register_operand" "r") (match_operand 1 "const_int_operand" "k") )] "" /* C boolean expression, if required */ "li %0, %1" ) D.1283 = 10; (set (reg:SI 58 [D.1283]) (const int 10: [0xa]) ) li $t0, 10 Development Use

Uday Khedker GRC, IIT Bombay
slide-42
SLIDE 42 EAGCC-PLDI-14 Essential Abstractions: Retargetability 29/31

Retargetability Mechanism of GCC

Language Specific Code Language and Machine Independent Generic Code Machine Dependent Generator Code Machine Descriptions Compiler Generation Framework Input Language Target Name Parser Gimplifier Tree SSA Optimizer RTL Generator Optimizer Code Generator Selected Copied Copied Generated Generated Generated Compiler Development Time Build Time Use Time GIMPLE → PN + PN → IR-RTL + IR-RTL → ASM GIMPLE → IR-RTL + IR-RTL → ASM Uday Khedker GRC, IIT Bombay
slide-43
SLIDE 43 EAGCC-PLDI-14 Essential Abstractions: Retargetability 30/31

Hooking up Back End Details

  • ptab table

. . . . . . OTI mov mov optab handler SI insn code CODE FOR movsi SF insn code CODE FOR nothing $(SOURCE)/gcc/optabs.h $(SOURCE)/gcc/optabs.c $(BUILD)/gcc/insn-output.c insn data . . . . . . 1280 "movsi" . . . gen movsi . . . $BUILD/gcc/insn-codes.h CODE FOR movsi=1280 CODE FOR movsf=CODE FOR nothing $BUILD/gcc/insn-opinit.c ... Runtime initialization of data structure in cc1 through function init all optabs

Uday Khedker GRC, IIT Bombay
slide-44
SLIDE 44 EAGCC-PLDI-14 Essential Abstractions: Retargetability 31/31

The Process of Expansion

gimple expand cfg GIMPLE RTL Match SPN Search

  • ptab table[]

and extract CODE FOR <SPN> Search insn data[] and extract gen <SPN> invoke gen <SPN> Generated

Uday Khedker GRC, IIT Bombay