Plugin Mechanisms in GCC

Similar documents
GCC Control Flow and Plugins

Motivation. GCC Control Flow and Plugins. Outline. Essential Abstractions in GCC. Module Binding Mechanisms. Part 1. GCC Resource Center.

The Design and Implementation of Gnu Compiler Collection

About These Slides. GDFA: Generic Data Flow Analyser for GCC. Part 1. Copyright. Outline. Uday Khedker

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

The Design and Implementation of Gnu Compiler Collection

First Level Gray Box Probing

Chapter 11 Introduction to Programming in C

Chapter 11 Introduction to Programming in C

GCC Internals: A Conceptual View Part II

Chapter 11 Introduction to Programming in C

2 Compiling a C program

Advanced Programming & C++ Language

The C Preprocessor (and more)!

Chapter 11 Introduction to Programming in C

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

Chapter 11 Introduction to Programming in C

Lecture 2. Xiaoguang Wang. January 16th, 2014 STAT 598W. (STAT 598W) Lecture 2 1 / 41

Lectures 5-6: Introduction to C

Makefiles SE 2XA3. Term I, 2018/19

CS 345. Functions. Vitaly Shmatikov. slide 1

Final CSE 131B Spring 2004

Chapter 11 Introduction to Programming in C

Introduction to Machine Descriptions

Link 8.A Dynamic Linking

Quick Reference for shmdefine

Miscellaneous C-programming Issues

Short Notes of CS201

Using GCC as a Research Compiler

Programs. Function main. C Refresher. CSCI 4061 Introduction to Operating Systems

CS201 - Introduction to Programming Glossary By

An Architectural Overview of GCC

Lectures 5-6: Introduction to C

Modifiers. int foo(int x) { static int y=0; /* value of y is saved */ y = x + y + 7; /* across invocations of foo */ return y; }

Organization of Programming Languages CS 3200/5200N. Lecture 09

CS3215. Outline: 1. Introduction 2. C++ language features 3. C++ program organization

COP4020 Programming Languages. Compilers and Interpreters Robert van Engelen & Chris Lacher

CS 247: Software Engineering Principles. Modules

G Programming Languages Spring 2010 Lecture 6. Robert Grimm, New York University

Chapter 11 Introduction to Programming in C

Announcements. My office hours are today in Gates 160 from 1PM-3PM. Programming Project 3 checkpoint due tomorrow night at 11:59PM.

An Overview of Compilation

C Programming Review CSC 4320/6320

A Fast Review of C Essentials Part I

Generic Programming in C

CS 110 Computer Architecture. Lecture 2: Introduction to C, Part I. Instructor: Sören Schwertfeger.

Incremental Machine Descriptions for GCC

PRINCIPLES OF OPERATING SYSTEMS

Intermediate Programming, Spring 2017*

multiple variables having the same value multiple variables having the same identifier multiple uses of the same variable

The Phasewise File Groups of GCC

Appendix. Grammar. A.1 Introduction. A.2 Keywords. There is no worse danger for a teacher than to teach words instead of things.

Link 7. Dynamic Linking

independent compilation and Make

Where We Are. Lexical Analysis. Syntax Analysis. IR Generation. IR Optimization. Code Generation. Machine Code. Optimization.

Separate Compilation Model

P.G.TRB - COMPUTER SCIENCE. c) data processing language d) none of the above

GNU make. Michal Koutný. Software development and monitoring tools (NSWI126)

Retargeting GCC to Spim: A Recap

CMSC 216 Introduction to Computer Systems Lecture 23 Libraries

Linking and Loading. ICS312 - Spring 2010 Machine-Level and Systems Programming. Henri Casanova

Stream Computing using Brook+

Page 1. Agenda. Programming Languages. C Compilation Process

C: Program Structure. Department of Computer Science College of Engineering Boise State University. September 11, /13

Topic 6: A Quick Intro To C

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

CS Programming In C

LECTURE 3. Compiler Phases

A Fast Review of C Essentials Part II

Binding and Variables

The Make Utility. Independent compilation. Large programs are difficult to maintain. Problem solved by breaking the program into separate files

specrtl: A Language for GCC Machine Descriptions

6. Pointers, Structs, and Arrays. 1. Juli 2011

Multimedia-Programmierung Übung 3

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

The SGI Pro64 Compiler Infrastructure - A Tutorial

A simple tutorial on generating PTX assembler out of Ada source code using LLVM NVPTX backend

CSE 307: Principles of Programming Languages

Motivation was to facilitate development of systems software, especially OS development.

CSE 307: Principles of Programming Languages

cc is really a front-end program to a number of passes or phases of the whole activity of "converting" our C source files to executable programs:

82V391x / 8V893xx WAN PLL Device Families Device Driver User s Guide

Chapter 5. Names, Bindings, and Scopes

Kakadu and Java. David Taubman, UNSW June 3, 2003

CST-402(T): Language Processors

INTERMEDIATE SOFTWARE DESIGN SPRING 2011 ACCESS SPECIFIER: SOURCE FILE

Instantiation of Template class

Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah. Lecturer Department of Computer Science & IT University of Balochistan

COMP26912: Algorithms and Imperative Programming

Type Checking. Outline. General properties of type systems. Types in programming languages. Notation for type rules.

Agenda. CS 61C: Great Ideas in Computer Architecture. Lecture 2: Numbers & C Language 8/29/17. Recap: Binary Number Conversion

Outline. General properties of type systems. Types in programming languages. Notation for type rules. Common type rules. Logical rules of inference

How to write a GCC plugin with MELT?

CS2141 Software Development using C/C++ Compiling a C++ Program

CS 61C: Great Ideas in Computer Architecture. Lecture 2: Numbers & C Language. Krste Asanović & Randy Katz

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

EL6483: Brief Overview of C Programming Language

Computers Programming Course 5. Iulian Năstac

엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2017 EOM, HYEONSANG ALL RIGHTS RESERVED

TMS470 ARM ABI Migration

Transcription:

Plugin Mechanisms in GCC (www.cse.iitb.ac.in/ uday) GCC Resource Center, Department of Computer Science and Engineering, Indian Institute of Technology, Bombay 13 June 2014

EAGCC-PLDI-14 Plugins: Outline 1/27 Outline Motivation Plugins in GCC

Part 1 Motivation

EAGCC-PLDI-14 Plugins: Motivation 2/27 Module Binding Mechanisms The need for adding, removing, and maintaining modules relatively independently The mechanism for supporting this is called by many names: Plugin, hook, callback, Sometimes it remains unnamed (eg. compilers in gcc driver) It may involve Minor changes in the main source Requires static linking No changes in the main source Requires dynamic linking

EAGCC-PLDI-14 Plugins: Motivation 2/27 Module Binding Mechanisms The need for adding, removing, and maintaining modules relatively independently The mechanism for supporting this is called by many names: Plugin, hook, callback, Sometimes it remains unnamed (eg. compilers in gcc driver) It may involve Minor changes in the main source Requires static linking We call this a static plugin No changes in the main source Requires dynamic linking We call this a dynamic plugin

EAGCC-PLDI-14 Plugins: Motivation 3/27 Plugin as a Module Binding Mechanisms We view plugin at a more general level than the conventional view Adjectives static and dynamic create a good contrast Most often a plugin in a C based software is a data structure containing function pointers and other related information

EAGCC-PLDI-14 Plugins: Motivation 4/27 Static Vs. Dynamic Plugins Static plugin requires static linking Changes required in gcc/makefile.in, some header and source files At least cc1 may have to be rebuild All files that include the changed headers will have to be recompiled Dynamic plugin uses dynamic linking Supported on platforms that support -ldl -rdynamic Loaded using dlopen and invoked at pre-determined locations in the compilation process Command line option -fplugin=/path/to/name.so Arguments required can be supplied as name-value pairs

EAGCC-PLDI-14 Plugins: Motivation 5/27 Static Plugins in the GCC Driver Source Program cc1/cc1plus cpp gcc/g++ as glibc/newlib ld Target Program

EAGCC-PLDI-14 Plugins: Motivation 5/27 Static Plugins in the GCC Driver Source Program cc1/cc1plus cpp gcc/g++ as Plugin for a translator in the driver gcc glibc/newlib ld Target Program

EAGCC-PLDI-14 Plugins: Motivation 6/27 Static Plugins in the Generated Compiler Input Language Compiler Generation Framework Target Name Language Specific Code Language and Machine Independent Generic Code Machine Dependent Generator Code Machine Descriptions Selected Copied Copied Generated Generated Parser Gimplifier Tree SSA Optimizer Expander Optimizer Recognizer Generated Compiler (cc1/cc1plus) Source Program Assembly Program

EAGCC-PLDI-14 Plugins: Motivation 6/27 Static Plugins in the Generated Compiler Input Language Compiler Generation Framework Target Name Language Specific Code Language and Machine Independent Generic Code Machine Dependent Generator Code Machine Descriptions Selected Plugin for a Copied language front end in cc1/cc1plus Copied Generated Generated Parser Gimplifier Tree SSA Optimizer Expander Optimizer Recognizer Generated Compiler (cc1/cc1plus) Source Program Assembly Program

EAGCC-PLDI-14 Plugins: Motivation 6/27 Static Plugins in the Generated Compiler Input Language Compiler Generation Framework Target Name Language Specific Code Language and Machine Independent Generic Code Machine Dependent Generator Code Machine Descriptions Selected Plugin for a Copied language front end in cc1/cc1plus Plugin for adding passes in Copied cc1/cc1plus Generated Generated Parser Gimplifier Tree SSA Optimizer Expander Optimizer Recognizer Generated Compiler (cc1/cc1plus) Source Program Assembly Program

EAGCC-PLDI-14 Plugins: Motivation 6/27 Static Plugins in the Generated Compiler Input Language Compiler Generation Framework Target Name Language Specific Code Language and Machine Independent Generic Code Machine Dependent Generator Code Machine Descriptions Selected Plugin for a Copied language front end in cc1/cc1plus Plugin for adding passes in Copied cc1/cc1plus Generated Plugin for Generated code generator in cc1/cc1plus Parser Gimplifier Tree SSA Optimizer Expander Optimizer Recognizer Generated Compiler (cc1/cc1plus) Source Program Assembly Program

Part 2 Static Plugins in GCC

EAGCC-PLDI-14 Plugins: Static Plugins in GCC 7/27 GCC s Solution Implementation Plugin Data Structure Initialization Translator in gcc/g++ Array of C structures Development time Front end in cc1/cc1plus C structure Build time Passes in cc1/cc1plus Linked list of C structures Development time Back end in cc1/cc1plus Arrays of structures Build time

EAGCC-PLDI-14 Plugins: Static Plugins in GCC 8/27 Plugin Data Structure in the GCC Driver struct compiler { const char *suffix; /* Use this compiler for input files whose names end in this suffix. */ const char *spec; /* To use this compiler, run this spec. * const char *cpp_spec; /* If non-null, substitute this spec for %C, rather than the usual cpp_spec. */ const int combinable; /* If nonzero, compiler can deal with multiple source files at once (IMA). const int needs_preprocessing; /* If nonzero, source files need to be run through a preprocessor. */ };

EAGCC-PLDI-14 Plugins: Static Plugins in GCC 9/27 Default Specs in the Plugin Data Structure in gcc.c All entries of Objective C/C++ and some entries of Fortran removed. static const struct compiler default_compilers[] = { {".cc", "#C++", 0, 0, 0}, {".cxx", "#C++", 0, 0, 0}, {".cpp", "#C++", 0, 0, 0}, {".cp", "#C++", 0, 0, 0}, {".c++", "#C++", 0, 0, 0}, {".C", "#C++", 0, 0, 0}, {".CPP", "#C++", 0, 0, 0}, {".ii", "#C++", 0, 0, 0}, {".ads", "#Ada", 0, 0, 0}, {".adb", "#Ada", 0, 0, 0}, {".f", "#Fortran", 0, 0, 0}, {".F", "#Fortran", 0, 0, 0}, {".for", "#Fortran", 0, 0, 0}, {".FOR", "#Fortran", 0, 0, 0}, {".f90", "#Fortran", 0, 0, 0}, {".F90", "#Fortran", 0, 0, 0}, {".p", "#Pascal", 0, 0, 0}, {".pas", "#Pascal", 0, 0, 0}, {".java", "#Java", 0, 0, 0}, {".class", "#Java", 0, 0, 0}, {".c", "@c", 0, 1, 1}, {".h", "@c-header", 0, 0, 0}, {".i", "@cpp-output", 0, 1, 0}, {".s", "@assembler", 0, 1, 0} }

EAGCC-PLDI-14 Plugins: Static Plugins in GCC 9/27 Default Specs in the Plugin Data Structure in gcc.c All entries of Objective C/C++ and some entries of Fortran removed. static const struct compiler default_compilers[] = { {".cc", "#C++", 0, 0, 0}, {".cxx", "#C++", 0, 0, 0}, {".cpp", "#C++", 0, 0, 0}, {".cp", "#C++", 0, 0, 0}, {".c++", "#C++", 0, 0, 0}, {".C", "#C++", 0, 0, 0}, {".CPP", "#C++", 0, 0, 0}, {".ii", "#C++", 0, 0, 0}, {".ads", "#Ada", 0, 0, 0}, {".adb", "#Ada", 0, 0, 0}, {".f", "#Fortran", 0, 0, 0}, {".F", "#Fortran", 0, 0, 0}, {".for", "#Fortran", 0, 0, 0}, {".FOR", "#Fortran", 0, 0, 0}, {".f90", "#Fortran", 0, 0, 0}, {".F90", "#Fortran", 0, 0, 0}, {".p", "#Pascal", 0, 0, 0}, {".pas", "#Pascal", 0, 0, 0}, {".java", "#Java", 0, 0, 0}, {".class", "#Java", 0, 0, 0}, {".c", "@c", 0, 1, 1}, @: Aliased entry {".h", "@c-header", 0, 0, 0}, {".i", "@cpp-output", 0, 1, 0}, {".s", "@assembler", 0, 1, 0} }

EAGCC-PLDI-14 Plugins: Static Plugins in GCC 9/27 Default Specs in the Plugin Data Structure in gcc.c All entries of Objective C/C++ and some entries of Fortran removed. static const struct compiler default_compilers[] = { {".cc", "#C++", 0, 0, 0}, {".cxx", "#C++", 0, 0, 0}, {".cpp", "#C++", 0, 0, 0}, {".cp", "#C++", 0, 0, 0}, {".c++", "#C++", 0, 0, 0}, {".C", "#C++", 0, 0, 0}, {".CPP", "#C++", 0, 0, 0}, {".ii", "#C++", 0, 0, 0}, {".ads", "#Ada", 0, 0, 0}, {".adb", "#Ada", 0, 0, 0}, {".f", "#Fortran", 0, 0, 0}, {".F", "#Fortran", 0, 0, 0}, {".for", "#Fortran", 0, 0, 0}, {".FOR", "#Fortran", 0, 0, 0}, {".f90", "#Fortran", 0, 0, 0}, {".F90", "#Fortran", 0, 0, 0}, {".p", "#Pascal", 0, 0, 0}, {".pas", "#Pascal", 0, 0, 0}, {".java", "#Java", 0, 0, 0}, {".class", "#Java", 0, 0, 0}, {".c", "@c", 0, 1, 1}, @: Aliased entry {".h", "@c-header", 0, 0, 0}, {".i", "@cpp-output", 0, 1, 0}, #: Default specs not available {".s", "@assembler", 0, 1, 0} }

EAGCC-PLDI-14 Plugins: Static Plugins in GCC 10/27 Complete Entry for C in gcc.c {"@c", /* cc1 has an integrated ISO C preprocessor. We should invoke the external preprocessor if -save-temps is given. */ "%{E M MM:%(trad_capable_cpp) %(cpp_options) %(cpp_debug_options)}\ %{!E:%{!M:%{!MM:\ %{traditional ftraditional:\ %egnu C no longer supports -traditional without -E}\ %{!combine:\ %{save-temps traditional-cpp no-integrated-cpp:%(trad_capable_cpp) \ %(cpp_options) -o %{save-temps:%b.i} %{!save-temps:%g.i} \n\ cc1 -fpreprocessed %{save-temps:%b.i} %{!save-temps:%g.i} \ %(cc1_options)}\ %{!save-temps:%{!traditional-cpp:%{!no-integrated-cpp:\ cc1 %(cpp_unique_options) %(cc1_options)}}}\ %{!fsyntax-only:%(invoke_as)}} \ %{combine:\ %{save-temps traditional-cpp no-integrated-cpp:%(trad_capable_cpp) \ %(cpp_options) -o %{save-temps:%b.i} %{!save-temps:%g.i}}\ %{!save-temps:%{!traditional-cpp:%{!no-integrated-cpp:\ cc1 %(cpp_unique_options) %(cc1_options)}}\ %{!fsyntax-only:%(invoke_as)}}}}}}", 0, 1, 1},

EAGCC-PLDI-14 Plugins: Static Plugins in GCC 11/27 Populated Plugin Data Structure for C++: gcc/cp/lang-specs.h {".cc", "@c++", 0, 0, 0}, {".cp", "@c++", 0, 0, 0}, {".cxx", "@c++", 0, 0, 0}, {".cpp", "@c++", 0, 0, 0}, {".c++", "@c++", 0, 0, 0}, {".C", "@c++", 0, 0, 0}, {".CPP", "@c++", 0, 0, 0}, {".H", "@c++-header", 0, 0, 0}, {".hpp", "@c++-header", 0, 0, 0}, {".hp", "@c++-header", 0, 0, 0}, {".hxx", "@c++-header", 0, 0, 0}, {".h++", "@c++-header", 0, 0, 0}, {".HPP", "@c++-header", 0, 0, 0}, {".tcc", "@c++-header", 0, 0, 0}, {".hh", "@c++-header", 0, 0, 0},

EAGCC-PLDI-14 Plugins: Static Plugins in GCC 11/27 Populated Plugin Data Structure for C++: gcc/cp/lang-specs.h {"@c++-header", "%{E M MM:cc1plus -E %(cpp_options) %2 %(cpp_debug_options)}\ %{!E:%{!M:%{!MM:\ %{save-temps no-integrated-cpp:cc1plus -E\ %(cpp_options) %2 -o %{save-temps:%b.ii} %{!save-temps:%g.ii} \n}\ cc1plus %{save-temps no-integrated-cpp:-fpreprocessed %{save-temps:% %{!save-temps:%{!no-integrated-cpp:%(cpp_unique_options)}}\ %(cc1_options) %2\ %{!fsyntax-only:%{!fdump-ada-spec*:-o %g.s %{!o*:--output-pch=%i.gch}\ %W{o*:--output-pch=%*}}%V}}}}", CPLUSPLUS_CPP_SPEC, 0, 0},

EAGCC-PLDI-14 Plugins: Static Plugins in GCC 11/27 Populated Plugin Data Structure for C++: gcc/cp/lang-specs.h {"@c++", "%{E M MM:cc1plus -E %(cpp_options) %2 %(cpp_debug_options)}\ %{!E:%{!M:%{!MM:\ %{save-temps no-integrated-cpp:cc1plus -E\ %(cpp_options) %2 -o %{save-temps:%b.ii} %{!save-temps:%g.ii} \n}\ cc1plus %{save-temps no-integrated-cpp:-fpreprocessed %{save-temps:% %{!save-temps:%{!no-integrated-cpp:%(cpp_unique_options)}}\ %(cc1_options) %2\ %{!fsyntax-only:%(invoke_as)}}}}", CPLUSPLUS_CPP_SPEC, 0, 0}, {".ii", "@c++-cpp-output", 0, 0, 0}, {"@c++-cpp-output", "%{!M:%{!MM:%{!E:\ cc1plus -fpreprocessed %i %(cc1_options) %2\ %{!fsyntax-only:%(invoke_as)}}}}", 0, 0, 0},

EAGCC-PLDI-14 Plugins: Static Plugins in GCC 12/27 Populated Plugin Data Structure for LTO: gcc/lto/lang-specs.h /* LTO contributions to the "compilers" array in gcc.c. */ {"@lto", "lto1 %(cc1_options) %i %{!fsyntax-only:%(invoke_as)}", /*cpp_spec=*/null, /*combinable=*/1, /*needs_preprocessing=*/0},

EAGCC-PLDI-14 Plugins: Static Plugins in GCC 13/27 What about the Files to be Procecced by the Linker? Linking is the last step Every file is passed on to linker unless it is suppressed If a translator is not found, input file is assumed to be a file for linker

EAGCC-PLDI-14 Plugins: Static Plugins in GCC 14/27 Plugin Structure in cc1 toplev main front end pass manager

EAGCC-PLDI-14 Plugins: Static Plugins in GCC 14/27 Plugin Structure in cc1 toplev main front end pass manager Double arrow represents control flow whereas single arrow represents pointer or index pass 1 pass 2 pass expand 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 n

EAGCC-PLDI-14 Plugins: Static Plugins in GCC 14/27 Plugin Structure in cc1 toplev main front end pass manager pass 1 pass 2 pass expand pass n pass 1 pass 2 expander code optab table recognizer code

EAGCC-PLDI-14 Plugins: Static Plugins in GCC 14/27 Plugin Structure in cc1 toplev main front end pass manager langhook pass 1 pass 1 language 1 language 2 language n pass 2 pass expand pass n pass 2 expander code optab table recognizer code

EAGCC-PLDI-14 Plugins: Static Plugins in GCC 14/27 Plugin Structure in cc1 toplev main front end pass manager langhook pass 1 pass 1 language 1 language 2 language n pass 2 pass expand pass n pass 2 expander code optab table recognizer code

EAGCC-PLDI-14 Plugins: Static Plugins in GCC 14/27 Plugin Structure in cc1 toplev main front end pass manager langhook pass 1 pass 1 language 1 language 2 language n pass 2 pass expand pass n pass 2 expander code optab table recognizer code

EAGCC-PLDI-14 Plugins: Static Plugins in GCC 14/27 Plugin Structure in cc1 toplev main langhook language 1 language 2 language n front end pass 1 pass 2 pass expand pass n pass manager pass 1 pass 2 expander code optab table recognizer code insn data generated machine 1 MD n MD 2 MD 1

EAGCC-PLDI-14 Plugins: Static Plugins in GCC 14/27 Plugin Structure in cc1 toplev main langhook language 1 language 2 language n front end pass 1 pass 2 pass expand pass n pass manager pass 1 pass 2 expander code optab table recognizer code insn data generated machine 2 MD n MD 2 MD 1

EAGCC-PLDI-14 Plugins: Static Plugins in GCC 14/27 Plugin Structure in cc1 toplev main langhook language 1 language 2 language n front end pass 1 pass 2 pass expand pass n pass manager pass 1 pass 2 expander code optab table recognizer code insn data generated machine n MD n MD 2 MD 1

EAGCC-PLDI-14 Plugins: Static Plugins in GCC 15/27 Front End Plugin Important fields of struct lang hooks instantiated for C #define LANG_HOOKS_FINISH c_common_finish #define LANG_HOOKS_PARSE_FILE c_common_parse_file #define LANG_HOOKS_WRITE_GLOBALS c_write_global_declarations

EAGCC-PLDI-14 Plugins: Static Plugins in GCC 16/27 Plugins for Pass Specifications Intraprocedural Passes Interprocedural Passes struct gimple opt pass struct opt pass struct simple ipa opt pass struct opt pass struct opt pass struct rtl opt pass struct opt pass struct ipa opt pass d struct opt pass /* Additional fields */

EAGCC-PLDI-14 Plugins: Static Plugins in GCC 17/27 Plugins for Intraprocedural Passes struct opt_pass { enum opt_pass_type type; const char *name; bool (*gate) (void); unsigned int (*execute) (void); struct opt_pass *sub; struct opt_pass *next; int static_pass_number; timevar_id_t tv_id; unsigned int properties_required; unsigned int properties_provided; unsigned int properties_destroyed; unsigned int todo_flags_start; unsigned int todo_flags_finish; }; struct gimple_opt_pass { struct opt_pass pass; }; struct rtl_opt_pass { struct opt_pass pass; };

EAGCC-PLDI-14 Plugins: Static Plugins in GCC 18/27 Plugins for Interprocedural Passes on a Translation Unit Pass variable: all simple ipa passes struct simple_ipa_opt_pass { struct opt_pass pass; };

EAGCC-PLDI-14 Plugins: Static Plugins in GCC 19/27 Plugins for Interprocedural Passes across a Translation Unit Pass variable: all regular ipa passes struct ipa_opt_pass_d { struct opt_pass pass; void (*generate_summary) (void); void (*read_summary) (void); void (*write_summary) (struct cgraph_node_set_def *, struct varpool_node_set_def *); void (*write_optimization_summary)(struct cgraph_node_set_def *, struct varpool_node_set_def *); void (*read_optimization_summary) (void); void (*stmt_fixup) (struct cgraph_node *, gimple *); unsigned int function_transform_todo_flags_start; unsigned int (*function_transform) (struct cgraph_node *); void (*variable_transform) (struct varpool_node *); };

EAGCC-PLDI-14 Plugins: Static Plugins in GCC 20/27 Predefined Pass Lists Pass List all lowering passes all small ipa passes all regular ipa passes all late ipa passes all lto gen passes all passes Purpose AST to CFG translation Interprocedural passes restricted to a single translation unit Interprocedural passes on a translation unit as well as across translation units (during WPA/IPA of LTO) Interprocedural passes on partitions created by LTO (after WPA/IPA) Passes to encode program for LTO Intraprocedural passes on GIMPLE and RTL

EAGCC-PLDI-14 Plugins: Static Plugins in GCC 21/27 Registering a Pass as a Static Plugin 1. Write the driver function in your file 2. Declare your pass in file tree-pass.h: extern struct gimple opt pass your pass name; 3. Add your pass to the appropriate pass list in init optimization passes() using the macro NEXT PASS 4. Add your file details to $SOURCE/gcc/Makefile.in 5. Configure and build gcc (For simplicity, you can make cc1 only) 6. Debug cc1 using ddd/gdb if need arises (For debuging cc1 from within gcc, see: http://gcc.gnu.org/ml/gcc/2004-03/msg01195.html)

Part 3 Dynamic Plugins in GCC

EAGCC-PLDI-14 Plugins: Dynamic Plugins in GCC 22/27 Dynamic Plugins Supported on platforms that support -ldl -rdynamic Loaded using dlopen and invoked at pre-determined locations in the compilation process Command line option -fplugin=/path/to/name.so Arguments required can be supplied as name-value pairs

EAGCC-PLDI-14 Plugins: Dynamic Plugins in GCC 23/27 The Mechanism of Dynamic Plugin pass manager pass dynamic plugin pass expander code optab table recognizer

EAGCC-PLDI-14 Plugins: Dynamic Plugins in GCC 23/27 The Mechanism of Dynamic Plugin pass manager pass pass expander code optab table recognizer dynamic plugin Runtime initialization of the appropriate linked list of passes Made possible by dynamic linking

EAGCC-PLDI-14 Plugins: Dynamic Plugins in GCC 24/27 Specifying an Example Pass struct simple_ipa_opt_pass pass_plugin = { { SIMPLE_IPA_PASS, "dynamic_plug", /* name */ 0, /* gate */ execute_pass_plugin, /* execute */ NULL, /* sub */ NULL, /* next */ 0, /* static pass number */ TV_INTEGRATION, /* tv_id */ 0, /* properties required */ 0, /* properties provided */ 0, /* properties destroyed */ 0, /* todo_flags start */ 0 /* todo_flags end */ } };

EAGCC-PLDI-14 Plugins: Dynamic Plugins in GCC 25/27 Registering Our Pass as a Dynamic Plugin struct register_pass_info pass_info = { &(pass_plugin.pass), /* Address of new pass, here, the struct opt_pass field of simple_ipa_opt_pass defined above */ "pta", /* Name of the reference pass (string in the structure specification) for hooking up the new pass. */ 0, /* Insert the pass at the specified instance number of the reference pass. Do it for every instance if it is 0. */ PASS_POS_INSERT_AFTER /* how to insert the new pass: before, after, or replace. Here we are inserting our pass the pass named pta */ };

EAGCC-PLDI-14 Plugins: Dynamic Plugins in GCC 26/27 Registering Callback for Our Pass for a Dynamic Plugins int plugin_init(struct plugin_name_args *plugin_info, struct plugin_gcc_version *version) { /* Plugins are activiated using this callback */ register_callback ( plugin_info->base_name, /* char *name: Plugin name, could be any name. plugin_info->base_name gives this filename */ PLUGIN_PASS_MANAGER_SETUP, /* int event: The event code. Here, setting up a new pass */ NULL, /* The function that handles the event */ &pass_info); /* plugin specific data */ } return 0;

EAGCC-PLDI-14 Plugins: Dynamic Plugins in GCC 27/27 Makefile for Creating and Using a Dynamic Plugin CC = $(INSTALL_D)/bin/g++ PLUGIN_SOURCES = new-pass.c PLUGIN_OBJECTS = $(patsubst %.c,%.o,$(plugin_sources )) GCCPLUGINS_DIR = $(shell $(CC) -print-file-name=plugin) CFLAGS+= -fpic -O2 INCLUDE = -Iplugin/include %.o : %.c $(CC) $(CFLAGS) $(INCLUDE) -c $< new-pass.so: $(PLUGIN_OBJECTS) $(CC) $(CFLAGS) $(INCLUDE) -shared $^ -o $@ test_plugin: test.c $(CC) -fplugin=./new-pass.so $^ -o $@ -fdump-tree-all