extending the gcc compiler with melt to suit your...

42
Extending the GCC compiler with MELT to suit your needs Basile STARYNKÉVITCH mail [email protected], web http://starynkevitch.net/Basile/ mobile +33 6 8501 2359 CEA, LIST DRT/DILS/Laboratoire de Sûreté du Logiciel, CEA/Saclay b.528 pt.cour.94, 91191 GIF/YVETTE CEDEX; France July 6, RMLL 2010 Basile STARYNKÉVITCH (CEA, LIST) Customizing GCC with MELT July 6, RMLL 2010 1 / 42

Upload: others

Post on 16-Apr-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Extending the GCC compiler with MELT to suit your needs2010.rmll.info/IMG/pdf/6-rmll-2010-07-06-GCC-MELT.pdf · 2010-07-06 · Introduction and context About this talk What this talk

Extending the GCC compiler with MELTto suit your needs

Basile STARYNKÉVITCHmail [email protected], web http://starynkevitch.net/Basile/

mobile +33 6 8501 2359

CEA, LISTDRT/DILS/Laboratoire de Sûreté du Logiciel, CEA/Saclay b.528 pt.cour.94, 91191 GIF/YVETTE CEDEX; France

July 6, RMLL 2010

Basile STARYNKÉVITCH (CEA, LIST) Customizing GCC with MELT July 6, RMLL 2010 1 / 42

Page 2: Extending the GCC compiler with MELT to suit your needs2010.rmll.info/IMG/pdf/6-rmll-2010-07-06-GCC-MELT.pdf · 2010-07-06 · Introduction and context About this talk What this talk

Contents

1 Introduction and context

2 Why extend GCC and for what?

3 Extending GCC with MELT

4 Conclusion and invitation

Basile STARYNKÉVITCH (CEA, LIST) Customizing GCC with MELT July 6, RMLL 2010 2 / 42

Page 3: Extending the GCC compiler with MELT to suit your needs2010.rmll.info/IMG/pdf/6-rmll-2010-07-06-GCC-MELT.pdf · 2010-07-06 · Introduction and context About this talk What this talk

Warning!

All opinions here are mine only, not those ofmy employer (CEA, LIST)1, APRIL2, the GCC community3, the FSF4

MELT development with public French funding (DGCIS)5 thru ITEA GlobalGCC(2006-2009) and FUI OpenGPU (2010-2011) 6 projects.

1http://www-list.cea.fr/2http://www.april.org/3http://gcc.gnu.org/4http://fsf.org/5http://www.industrie.gouv.fr/portail/une/dgcis.html6http://www.opengpu.net/

Basile STARYNKÉVITCH (CEA, LIST) Customizing GCC with MELT July 6, RMLL 2010 3 / 42

Page 4: Extending the GCC compiler with MELT to suit your needs2010.rmll.info/IMG/pdf/6-rmll-2010-07-06-GCC-MELT.pdf · 2010-07-06 · Introduction and context About this talk What this talk

Introduction and context

Introduction and context

Basile STARYNKÉVITCH (CEA, LIST) Customizing GCC with MELT July 6, RMLL 2010 4 / 42

Page 5: Extending the GCC compiler with MELT to suit your needs2010.rmll.info/IMG/pdf/6-rmll-2010-07-06-GCC-MELT.pdf · 2010-07-06 · Introduction and context About this talk What this talk

Introduction and context

Expected audience

power users of GCC compiler⇒ you are compiling C, C++ or Fortran, Ada, Objective C ... code every day!free software contributor (or user) and enthusiastworking on big (preferably free) software (≥ 500KLOC)Notably, contributors to major free projects like Gtk/Gnome, Qt/Kde, Linuxkernel, OpenOffice, Mozilla, Xorg, Apache, . . . or big GNU/Linux distributions

able to build (and enhance) big free software from source code⇒ Don’t wait for GCC 4.5 to be packaged, build it yourself

outside of source tree; but pay attention to prerequisites 7:autoconf 2.64, Parma Polyhedra Library 0.10.2, MPFR 2.4.2 etc. . .

senior software developer or architect, fluent with several programming languages

Question: who matches [most of] the above criteria?

N.B. Knowledge of GCC internals is not expected to attend this talk! Seehttp://gcc.gnu.org/onlinedocs/gccint/

7http://gcc.gnu.org/install/prerequisites.html

Basile STARYNKÉVITCH (CEA, LIST) Customizing GCC with MELT July 6, RMLL 2010 5 / 42

Page 6: Extending the GCC compiler with MELT to suit your needs2010.rmll.info/IMG/pdf/6-rmll-2010-07-06-GCC-MELT.pdf · 2010-07-06 · Introduction and context About this talk What this talk

Introduction and context

About GCC

Gnu Compiler Collection - http://gcc.gnu.org/

GCC exists since ≈ 1985 - it is a legacy software, part of GNUGCC is needed to build many free software projects, central to the free software eco-system

GCC is free software : (mostly) GPLv3+, c©FSF 8

current version is GCC 4.5.0 released in april 20101 huge: 4029 KLOC9 and still growing (> +30% in last 2.5 years),2 many (≈ 400) contributors (mostly full-time professionals from industry [e.g. AdaCore,

AMD, Apple, CodeSourcery, Google, IBM, Intel, Oracle, RedHat, ST, SuSE, . . . ] and academia)

no “benevolent dictator” : ⇒ every contribution is peer-reviewed3 compiles several source languages: C, C++, Objective C, Fortran, Ada, Java . . .4 targets dozens of machines (x86, x86-64, IA64, PowerPC*, Sparc*, s390*, MIPS* . . . )

and runs on many systems (Linux, *BSD, Solaris, . . . ), possible cross-compiler

recent GCC features: link-time optimizations [LTO] andplugins so users can extend & customize GCC to suit their specific needs

8All contributions to GCC belong to FSF ⇒ legal requirements before contributinghttp://gcc.gnu.org/contribute.html (copyright assignment to FSF or disclaimer)

9Measured with D.Wheeler’s sloccountBasile STARYNKÉVITCH (CEA, LIST) Customizing GCC with MELT July 6, RMLL 2010 6 / 42

Page 7: Extending the GCC compiler with MELT to suit your needs2010.rmll.info/IMG/pdf/6-rmll-2010-07-06-GCC-MELT.pdf · 2010-07-06 · Introduction and context About this talk What this talk

Introduction and context

About this talk

What this talk don’t provide :working or mature solutions to any specific needs of GCC extensionscrash-course on compilation10

complete overview of GCC internals

What this talk aims to give you :some few ideas on GCC extensions, and partial understanding of GCCinternals to code thema small overview of a bleeding-edge technology to extend GCC: MELT

the motivation to work more and experiment your GCC extensions withMELT for your specific needs (or those of major free projects)

10But this talk don’t pre-suppose specific knowledge about compilers or GCC

Basile STARYNKÉVITCH (CEA, LIST) Customizing GCC with MELT July 6, RMLL 2010 7 / 42

Page 8: Extending the GCC compiler with MELT to suit your needs2010.rmll.info/IMG/pdf/6-rmll-2010-07-06-GCC-MELT.pdf · 2010-07-06 · Introduction and context About this talk What this talk

Introduction and context

What is MELT?

MELT = [originally] Middle-End Lisp Translator ; see http://gcc.gnu.org/wiki/MELT11

MELT is a lispy domain specific language to easily code GCC extensions.MELT implementation is GPLv3+ c©FSF, available thru SubVersion

svn co svn://gcc.gnu.org/svn/gcc/branches/melt-branch \gcc-melt

MELT is available and usable1 as a self-contained GCC branch (close to the trunk), or2 as a melt.so plugin to an unmodified GCC 4.5

MELT is still work in progress: bugs are corrected, new features are added.But MELT is usable today (july 2010).

11And in september 2010 http://gcc-melt.org/

Basile STARYNKÉVITCH (CEA, LIST) Customizing GCC with MELT July 6, RMLL 2010 8 / 42

Page 9: Extending the GCC compiler with MELT to suit your needs2010.rmll.info/IMG/pdf/6-rmll-2010-07-06-GCC-MELT.pdf · 2010-07-06 · Introduction and context About this talk What this talk

Why extend GCC and for what?

Why extend GCC and for what?

Basile STARYNKÉVITCH (CEA, LIST) Customizing GCC with MELT July 6, RMLL 2010 9 / 42

Page 10: Extending the GCC compiler with MELT to suit your needs2010.rmll.info/IMG/pdf/6-rmll-2010-07-06-GCC-MELT.pdf · 2010-07-06 · Introduction and context About this talk What this talk

Why extend GCC and for what?

Why extend GCC?

GCC is a compiler, sofrom the input source files (or compilation unit) like foo.cc12, bar.f90, . . .using the powerful internal representations and processing inside GCC

GCC produces 13:object files containing machine instructions, data and other informations14

diagnostics for the human developer (e.g. warnings).

It is possible to extend/customize GCC to improve object code ordiagnostics, or take advantage of GCC processing.

Most GCC extensions will be specific e.g. to a free software community (e.g.TreeHydra GCC plugin for Mozilla)

12And all the #include-d files!13With the help of other programs, notably binutils = assembler as and linker ld or gold14Notably relocation directives for the linker, or even Gimple for LTO, consumed by GCC

Basile STARYNKÉVITCH (CEA, LIST) Customizing GCC with MELT July 6, RMLL 2010 10 / 42

Page 11: Extending the GCC compiler with MELT to suit your needs2010.rmll.info/IMG/pdf/6-rmll-2010-07-06-GCC-MELT.pdf · 2010-07-06 · Introduction and context About this talk What this talk

Why extend GCC and for what?

GCC layers

GCC is made of three layers and their intermediate representations (I.R.):source language specific front-ends; the main front-end representation isthe Generic/Tree (or just Tree) [AST, abstract syntax tree].See GCC 4.5 source files gcc/tree.h and gcc/tree.def

source and target independent15 middle-end Gimple representation.See source files gcc/gimple.h and gcc/gimple.def

target-specific RTL (Register Transfer Language) representation.

GCC compilation is not mostly a matter of parsing (building the Generic/TreeAST from source files) or generating (emitting the assembler code from I.R.).

The main work of GCC happens in the middle-end (optimizations like inlining, loopunrolling . . . ) on Gimple.

GCC contains nearly 250 passes organizing the transformations of the I.R.See files gcc/tree-pass.h and gcc/passes.c

15Except the few dependencies of the target visible from source languages, like sizeof

Basile STARYNKÉVITCH (CEA, LIST) Customizing GCC with MELT July 6, RMLL 2010 11 / 42

Page 12: Extending the GCC compiler with MELT to suit your needs2010.rmll.info/IMG/pdf/6-rmll-2010-07-06-GCC-MELT.pdf · 2010-07-06 · Introduction and context About this talk What this talk

Why extend GCC and for what?

GCC layers and I.R. = internal representations

Basile STARYNKÉVITCH (CEA, LIST) Customizing GCC with MELT July 6, RMLL 2010 12 / 42

Page 13: Extending the GCC compiler with MELT to suit your needs2010.rmll.info/IMG/pdf/6-rmll-2010-07-06-GCC-MELT.pdf · 2010-07-06 · Introduction and context About this talk What this talk

Why extend GCC and for what?

discovering GCC I.R. and passes

The gcc executable is just a driving program invoking cc1 (or cc1plus, orlto1 etc. and ld, as, . . . ). Run with flags gcc -v -time.

The executed set of passes depends upon the GCC flags (notably optimizations-O2 or -O3 or -Os . . . ) and may somehow change dynamically.

It is possible to dump incomplete textual views of the various I.R. e.g. byrunning gcc -O -fdump-tree-all or -fdump-rtl-all, but thisproduces a lot of files.

GCC contains a simple precise marking16 garbage collector Ggc which is runbetween passes. The gengtype utility generates marking routines using GTYannotations.

16But Ggc do not handle local pointers; passes have to store their GC-ed data in static orexternal GCC variables!

Basile STARYNKÉVITCH (CEA, LIST) Customizing GCC with MELT July 6, RMLL 2010 13 / 42

Page 14: Extending the GCC compiler with MELT to suit your needs2010.rmll.info/IMG/pdf/6-rmll-2010-07-06-GCC-MELT.pdf · 2010-07-06 · Introduction and context About this talk What this talk

Why extend GCC and for what?

GCC extensions thru plugins

GCC can be extended by plugins17, e.g. gcc-4.5 -fplugin=melt.so \-fplugin-arg-melt-mode=makegreen -O foo.c

GCC 4.5 provide a framework 18 and hooks for:1 checking the version of the compiler2 interacting with the pass manager (most plugins add their new passes)3 adding new __attribute__-s or #pragma-s to C or C++4 additional processing on C++ ASTs, types, or at end of compilation unit5 a few more things (e.g. hooks to Ggc)

GCC 4.5 don’t provide hooks for adding new front-ends or back-ends, andplugins cannot be run without input files (sometimes just empty.c) to GCC.

MELT uses the same hooks, so has the same extendability as plain GCC 4.5.

17GCC plugins should be free software [usually in C or C++], GPLv3+ compatible.Proprietary plugins are discouraged and almost disallowed.

18http://gcc.gnu.org/onlinedocs/gccint/Plugins.html

Basile STARYNKÉVITCH (CEA, LIST) Customizing GCC with MELT July 6, RMLL 2010 14 / 42

Page 15: Extending the GCC compiler with MELT to suit your needs2010.rmll.info/IMG/pdf/6-rmll-2010-07-06-GCC-MELT.pdf · 2010-07-06 · Introduction and context About this talk What this talk

Why extend GCC and for what?

Wish: a GCC extension for typing Gtk/Gnome

The Gtk library defines a generic object system and provides a polymorphicproperty setting function g_object_set, e.g. in gtk/gtkvolumebutton.c

GtkObject *adj;adj = gtk_adjustment_new (0., 0., 1., 0.02, 0.2, 0.);g_object_set (G_OBJECT (button),

"adjustment", adj,"size", GTK_ICON_SIZE_SMALL_TOOLBAR,"has-tooltip", TRUE,NULL);

A GCC extension19 specific to Gtk could check the property types (i.e. that"adjustment" property has a GtkObject*, "has-tooltip" has a gboolean, etc.) andissue useful Gtk-specific warnings, since GCC already knows the types!

Question: who is volunteering from the Gtk community?

19to be “easily” coded using MELT! Designing and making such an extension requires muchmore Gtk expertise than deep knowledge of GCC internals! The extension could even beenhanced to optimize the property settings into the actual setters.

Basile STARYNKÉVITCH (CEA, LIST) Customizing GCC with MELT July 6, RMLL 2010 15 / 42

Page 16: Extending the GCC compiler with MELT to suit your needs2010.rmll.info/IMG/pdf/6-rmll-2010-07-06-GCC-MELT.pdf · 2010-07-06 · Introduction and context About this talk What this talk

Why extend GCC and for what?

Wish: GCC extension[s] for the Linux Kernel

Caveat: I am not a kernel developer! So the insights may be wrong.

The Linux kernel is very complex, and has many rules, e.g. about

modes of memory allocation KFP_KERNEL vs KFP_ATOMIC

handling of locks or mutexes and synchronisation

symmetry of *_ops20

initialization style 21

etc . . . [error handling?]

GCC extensions specific to the kernel could be made to check such rules andhelp many kernel or driver programmers.See also http://coccinelle.lip6.fr/

who is volunteering? Kernel expertise is needed more than deep knowledge of GCC or MELT!

20E.g. every super_operations having a .alloc_inode should also have a.destroy_inode

21E.g. rwlock_init should appear only in initializing routinesBasile STARYNKÉVITCH (CEA, LIST) Customizing GCC with MELT July 6, RMLL 2010 16 / 42

Page 17: Extending the GCC compiler with MELT to suit your needs2010.rmll.info/IMG/pdf/6-rmll-2010-07-06-GCC-MELT.pdf · 2010-07-06 · Introduction and context About this talk What this talk

Why extend GCC and for what?

Wish: GCC extension[s] for navigation and metrics

GCC knows a lot about the source code it is compiling, so specific extensions(in MELT) could be designed to help navigation in big open source projects

GCC extensions could compute metrics on code (McCabe complexity, statisticson block sizes, local variables, call patterns, . . . )

The MELT pattern matching facilities could be a starting point for agrep-like 22 or awk-like utility on Gimple

GCC extensions can be complementary to sophisticated static analyzers likeFrama-C http://frama-c.com (autonomous static analyzer, LGPL license).

Once developed, GCC extensions are easier to use 23 than external tools!

22E.g. find calls to printf with a format which is not internationalized with gettext23Users just add a few flags to CFLAGS in their Makefile-s.

Basile STARYNKÉVITCH (CEA, LIST) Customizing GCC with MELT July 6, RMLL 2010 17 / 42

Page 18: Extending the GCC compiler with MELT to suit your needs2010.rmll.info/IMG/pdf/6-rmll-2010-07-06-GCC-MELT.pdf · 2010-07-06 · Introduction and context About this talk What this talk

Why extend GCC and for what?

Wish: GCC optimizing extension[s] for big libraries or software

A library (or a big software) could also provide GCC extensions doing specificoptimizations tailored to the library, e.g.

rewrite fprintf(stdout, ...) → printf(...)for Gtk, rewrite most calls to g_object_set to the appropriate setting routines

sometimes, replace malloc and free in the same routine by an alloca

a precise or copying garbage collector for C wants to know about localGC-ed pointers and mark/follow them, putting them in a local structuresimplify mutexes (e.g. recursive→ plain)clone some numerical code into OpenCL (run on GPU) = OpenGPU project

Such GCC extensions24 could be coded in MELT and transform Gimple!

But “premature optimization is evil”, and requires more expertise on the librarythan about GCC

24Perhaps driven by pragmas or attributes?

Basile STARYNKÉVITCH (CEA, LIST) Customizing GCC with MELT July 6, RMLL 2010 18 / 42

Page 19: Extending the GCC compiler with MELT to suit your needs2010.rmll.info/IMG/pdf/6-rmll-2010-07-06-GCC-MELT.pdf · 2010-07-06 · Introduction and context About this talk What this talk

Why extend GCC and for what?

Understanding Generic Tree -s

GCC [Generic] Tree -s are pointers25 (tree) to a sum type (discriminated union)of more than 180 cases (see gcc/tree.def) like:

/* Integer types in all languages, including char in C.Also used for sub-ranges of other discrete types.Has components TYPE_MIN_VALUE, TYPE_MAX_VALUE (expressions, inclusive)and TYPE_PRECISION (number of bits used by this type).In the case of a subrange type in Pascal, the TREE_TYPEof this will point at the supertype (another INTEGER_TYPE,or an ENUMERAL_TYPE or BOOLEAN_TYPE).Otherwise, the TREE_TYPE is zero. */

DEFTREECODE (INTEGER_TYPE, "integer_type", tcc_type, 0)

The Gimple representations partly use Tree for instruction arguments anddeclarations (of types, functions, arguments, . . . ).

25typedef-ed in gcc/coretypes.h and documented inhttp://gcc.gnu.org/onlinedocs/gccint/GENERIC.html

Basile STARYNKÉVITCH (CEA, LIST) Customizing GCC with MELT July 6, RMLL 2010 19 / 42

Page 20: Extending the GCC compiler with MELT to suit your needs2010.rmll.info/IMG/pdf/6-rmll-2010-07-06-GCC-MELT.pdf · 2010-07-06 · Introduction and context About this talk What this talk

Why extend GCC and for what?

Understanding Gimple -s

GCC Gimple -s are pointers 26 to a sum type (discriminated union) of 36 cases 27

(see gcc/gimple.def) like:

/* GIMPLE_CALL <FN, LHS, ARG1, ..., ARGN[, CHAIN]> represents functioncalls.

FN is the callee. It must be accepted by is_gimple_call_addr.

LHS is the operand where the return value from FN is stored. It maybe NULL.

ARG1 ... ARGN are the arguments. They must all be accepted byis_gimple_operand.

CHAIN is the optional static chain link for nested functions. */DEFGSCODE(GIMPLE_CALL, "gimple_call", GSS_WITH_MEM_OPS)

Most Gimple arguments are simple tree-s.26documented in http://gcc.gnu.org/onlinedocs/gccint/GIMPLE.html27But 14 out of 36 cases are for OpenMP support so only 22 cases are generally useful!

Basile STARYNKÉVITCH (CEA, LIST) Customizing GCC with MELT July 6, RMLL 2010 20 / 42

Page 21: Extending the GCC compiler with MELT to suit your needs2010.rmll.info/IMG/pdf/6-rmll-2010-07-06-GCC-MELT.pdf · 2010-07-06 · Introduction and context About this talk What this talk

Why extend GCC and for what?

textual example of Gimple

gcc-4.5 -fdump-tree-all -O -c sumarr.c gives 80 dump files!

/∗ f i l e sumarr . c ∗ /i n t sumposarr ( i n t n , i n t a r r [ ] ){

i n t i ;i n t sum = 0;for ( i =0; i <n ; i ++)

{i n t e l = a r r [ i ] ;i f ( e l >0)

sum += a r r [ i ] ∗ ( i +1 ) ;}

return sum;}

Basile STARYNKÉVITCH (CEA, LIST) Customizing GCC with MELT July 6, RMLL 2010 21 / 42

Page 22: Extending the GCC compiler with MELT to suit your needs2010.rmll.info/IMG/pdf/6-rmll-2010-07-06-GCC-MELT.pdf · 2010-07-06 · Introduction and context About this talk What this talk

Why extend GCC and for what?

As Gimple

// dump file sumarr.c.004t.gimplesumposarr (int n, int * arr) {

long unsigned int D.2724;long unsigned int D.2725;int * D.2726;int D.2729;int D.2730;int D.2731;int D.2732;int i;int sum;

sum = 0;i = 0;goto <D.1623>;<D.1622>:{int el;

D.2724 = (long unsigned int) i;D.2725 = D.2724 * 4;D.2726 = arr + D.2725;

el = *D.2726;if (el > 0) goto <D.2727>;

else goto <D.2728>;<D.2727>:D.2724 = (long unsigned int) i;D.2725 = D.2724 * 4;D.2726 = arr + D.2725;D.2729 = *D.2726;D.2730 = i + 1;D.2731 = D.2729 * D.2730;sum = D.2731 + sum;<D.2728>:

}i = i + 1;<D.1623>:if (i < n) goto <D.1622>;else goto <D.1624>;

<D.1624>:D.2732 = sum;return D.2732;

}

Basile STARYNKÉVITCH (CEA, LIST) Customizing GCC with MELT July 6, RMLL 2010 22 / 42

Page 23: Extending the GCC compiler with MELT to suit your needs2010.rmll.info/IMG/pdf/6-rmll-2010-07-06-GCC-MELT.pdf · 2010-07-06 · Introduction and context About this talk What this talk

Why extend GCC and for what?

Gimple SSA (static single assignment)Each SSA variable is assigned once! (simpler optimizations & transformations).

// dump file sumarr.c.023t.ssasumposarr (int n, int* arr) {int el; int sum; int i;int D.2732; int D.2731;int D.2730; int D.2729;int * D.2726;long unsigned int D.2725;long unsigned int D.2724;

<bb 2>:sum4 = 0;i5 = 0;goto <bb 6>;

<bb 3>:D.27247 = (long unsigned int) i1;D.27258 = D.27247 * 4;D.272610 = arr9(D) + D.27258;el11 = *D.272610;if (el11 > 0)

goto <bb 4>;else goto <bb 5>;

<bb 4>:D.272412 = (long unsigned int) i1;D.272513 = D.272412 * 4;D.272614 = arr9(D) + D.272513;D.272915 = *D.272614;D.273016 = i1 + 1;D.273117 = D.272915 * D.273016;sum18 = D.273117 + sum3;

<bb 5>:# sum2 = φ <sum3(3), sum18(4)>i19 = i1 + 1;

<bb 6>:# i1 = φ <i5(2), i19(5)># sum3 = φ <sum4(2), sum2(5)>if (i1 < n6(D))goto <bb 3>;

else goto <bb 7>;

<bb 7>:D.273220 = sum3;return D.273220; }

Basile STARYNKÉVITCH (CEA, LIST) Customizing GCC with MELT July 6, RMLL 2010 23 / 42

Page 24: Extending the GCC compiler with MELT to suit your needs2010.rmll.info/IMG/pdf/6-rmll-2010-07-06-GCC-MELT.pdf · 2010-07-06 · Introduction and context About this talk What this talk

Why extend GCC and for what?

The Control Flow Graph (CFG)

The CFG has extra simple data structures (edge-s, gimple_seq-s,basic_block-s). Basic blocks have one entry point and are not nested.They may be exited by conditional gimple-s.

simple intraprocedural gimple passes are running on each function.the cfun (current function) knows its entry and exit blocksbasic_block-s are chained (not nested)Each basic_block has edge-s to other basic blocks (notably its branchif true and branch if false edgesEach edge knows its origin and destination basic_block

Each basic_block has its gimple_seq and incoming or outgoingedge-sgimple_seq-s are vectors of gimple-sEach gimple belongs to its basic_blockThe gimple-s have usually fixed arity; their operands are tree-s

Basile STARYNKÉVITCH (CEA, LIST) Customizing GCC with MELT July 6, RMLL 2010 24 / 42

Page 25: Extending the GCC compiler with MELT to suit your needs2010.rmll.info/IMG/pdf/6-rmll-2010-07-06-GCC-MELT.pdf · 2010-07-06 · Introduction and context About this talk What this talk

Why extend GCC and for what?

Common difficulties when extending GCC

Understand:Generic Tree-s (front-ends and Gimple arguments)Gimple-s (middle-end representation[s])control flow graph representations : edge -s, basic_block -s,gimple_seq -s.the order of passes, and choosing where to add new passes.different kind of passes (Gimple or Inter-Procedural Analysis [IPA] or even RTL).major GCC global variables (cfun or cgraph)plugin hooks and eventsother representations (e.g. variables use-defs, dominators)

All these points are needed when coding GCC plugins in C or when codingGCC extensions in MELT. Re-read GCC internals manualhttp://gcc.gnu.org/onlinedocs/gccint/

MELT facilitate coding of GCC extensions once these points are mastered.

Basile STARYNKÉVITCH (CEA, LIST) Customizing GCC with MELT July 6, RMLL 2010 25 / 42

Page 26: Extending the GCC compiler with MELT to suit your needs2010.rmll.info/IMG/pdf/6-rmll-2010-07-06-GCC-MELT.pdf · 2010-07-06 · Introduction and context About this talk What this talk

Extending GCC with MELT

Extending GCC with MELT

Basile STARYNKÉVITCH (CEA, LIST) Customizing GCC with MELT July 6, RMLL 2010 26 / 42

Page 27: Extending the GCC compiler with MELT to suit your needs2010.rmll.info/IMG/pdf/6-rmll-2010-07-06-GCC-MELT.pdf · 2010-07-06 · Introduction and context About this talk What this talk

Extending GCC with MELT

Why not use a scripting language [Python, Ocaml, Guile, . . . ]?

N.B.: I worked a month on trying to embed Ocaml in GCC [practically infeasible].

The GCC internals API28 are not stable (from one GCC release to thenext)29, and will probably never be.The GCC API is huge and ill-defined.The API contains a lot of more or less simple functions and alsonon-functions (e.g. macros and even non-expression iterative macros likeFOREACH_FUNCTION_ARGS or FOR_EACH_BB_FN etc.). Interfacing them withscripting-language specific glue functions would be error-prone andinefficient. Glue generators (à la SWIG) don’t fit well.The Ggc (GCC garbage collector) is essential in GCC; mixing several GC-s(i.e. Ggc and Ocaml’s GC) is perilous and brittle.

28The Application Programming Interface, used when coding GCC plugins in C!29However, major I.R. like Gimple or Tree-s are too important to change often inside GCC. And

MELT went easily thru the major Gimple-tree 4.3 → Gimple-tuple 4.4 transition.

Basile STARYNKÉVITCH (CEA, LIST) Customizing GCC with MELT July 6, RMLL 2010 27 / 42

Page 28: Extending the GCC compiler with MELT to suit your needs2010.rmll.info/IMG/pdf/6-rmll-2010-07-06-GCC-MELT.pdf · 2010-07-06 · Introduction and context About this talk What this talk

Extending GCC with MELT

Basic requirements for MELT

⇒ a language for extending current GCC practically needs to

be translated to C (in a style compatible with GCC internals) with descriptivemechanisms for C code generation tuned to GCC API.have a GC compatible with Ggc!provide high-level programming constructs: functional or applicativeprogramming, object system, modularitygive pattern matching facilities to ease processing of GCC I.R. (notablyGimple and Tree)be reasonably sure (no frequent core dump-s) and somehow efficienthandle both dynamically typed30 first-class language values (closures,objects, lists, boxed integers, boxed GCC data, . . . ) and second-class GCC datastuff (raw GCC gimple-s or tree-s . . . )

MELT was progressively designed and implemented with the above features!30Making a statically-typed language à la Ocaml would require formalizing all GCC data and API

in a exceedingly complex type system.

Basile STARYNKÉVITCH (CEA, LIST) Customizing GCC with MELT July 6, RMLL 2010 28 / 42

Page 29: Extending the GCC compiler with MELT to suit your needs2010.rmll.info/IMG/pdf/6-rmll-2010-07-06-GCC-MELT.pdf · 2010-07-06 · Introduction and context About this talk What this talk

Extending GCC with MELT

MELT first-class values

boxed numbers and boxed GCC data (like gimple, tree,basic_block, edge, . . . )associative hash-tables to map homogenous GCC data to MELT values 31.lists (made of pairs, the list points to its first and last pairs).tuples (vectors of MELT values)MELT objects (single-inheritance)closures (functional values) and routineshash-tables keyed by objectsstrings, string-buffersspecial values like boxed files, boxed PPL data, etc. . . .

31It is not feasible to extend GCC data in plugins by adding extra fields in core GCC structureslike gimple

Basile STARYNKÉVITCH (CEA, LIST) Customizing GCC with MELT July 6, RMLL 2010 29 / 42

Page 30: Extending the GCC compiler with MELT to suit your needs2010.rmll.info/IMG/pdf/6-rmll-2010-07-06-GCC-MELT.pdf · 2010-07-06 · Introduction and context About this talk What this talk

Extending GCC with MELT

MELT and Lisp

MELT share many traits with Lisp and Schemelispy syntaxLispy keywords like defun if progn cond setq let letrec . . .lexical (single namespace) binding à la Scheme

but MELT have also pecularitieshandling of non-value stuff (like raw :gimple or :tree)many operations are not named like in Scheme (no car)lists are not simply pairsno tail recursion but several iterative constructslots of linguistic devices to fit well into GCC API and programming styleability to mix C code with MELT codethe let is sequential and can bind raw stuff (not only values)non-value raw stuff secondary arguments and results

Basile STARYNKÉVITCH (CEA, LIST) Customizing GCC with MELT July 6, RMLL 2010 30 / 42

Page 31: Extending the GCC compiler with MELT to suit your needs2010.rmll.info/IMG/pdf/6-rmll-2010-07-06-GCC-MELT.pdf · 2010-07-06 · Introduction and context About this talk What this talk

Extending GCC with MELT

Activating and defining a MELT mode

MELT is not activated by default; you should provide a mode:gcc-melt -fmelt-mode=generation -O foo.corgcc-4.5 -fplugin=melt.so-fplugin-arg-melt-mode=generation -O foo.c

Most MELT code examples by Jérémie Salvucci [email protected]

Defining and installing a mode in MELTafter having defined the generation_docmd function in MELT [next slide]

(definstance generation_mode class_melt_mode:named_name ’"generation" ;;; quoted string → boxed string value:meltmode_help ’" MELT ’generation’ : generate C code from Gimple":meltmode_fun generation_docmd)

(install_melt_mode generation_mode)

Basile STARYNKÉVITCH (CEA, LIST) Customizing GCC with MELT July 6, RMLL 2010 31 / 42

Page 32: Extending the GCC compiler with MELT to suit your needs2010.rmll.info/IMG/pdf/6-rmll-2010-07-06-GCC-MELT.pdf · 2010-07-06 · Introduction and context About this talk What this talk

Extending GCC with MELT

(defun generation_docmd (cmd moduldata)(let ((generation_pass

;; define a GCC gimple pass(instance class_gcc_gimple_pass

:named_name ’"melt_generation_pass":gccpass_gate generation_gate:gccpass_exec generation_exec:gccpass_data

(instance class_generation_code:code_prototypes_buffer

(make_strbuf discr_strbuf):code_buffer (make_strbuf discr_strbuf):code_structure (list))

:gccpass_properties_required ())));; install the pass after the existing cfg pass(install_melt_gcc_pass generation_pass "after" "cfg" 0);; register an anonymous function at unit translation finish(at_finish_unit_last

(lambda () ;; anonymous function(let ((datas (get_field :gccpass_data generation))

(buffer (get_field :code_prototypes_buffer datas)))(generation_prototypes datas)(output_sbuf_strconst buffer "/tmp/toto"))))

(return generation)))

Basile STARYNKÉVITCH (CEA, LIST) Customizing GCC with MELT July 6, RMLL 2010 32 / 42

Page 33: Extending the GCC compiler with MELT to suit your needs2010.rmll.info/IMG/pdf/6-rmll-2010-07-06-GCC-MELT.pdf · 2010-07-06 · Introduction and context About this talk What this talk

Extending GCC with MELT

defining a class in MELT

(defclass class_generation_code:super class_proped ;; super class providing a prop_table field:fields (;; the class’es own fieldscode_prototypes_buffercode_buffercode_structure))

The fields names should be globally unique, so that get_field 32 can betranslated to something safe, testing that the field is retrieved from a valuewhich is an object of the appropriate class (the class containing the field).

Methods can be dynamically installed & removed.

Classes, fields, method selectors, bindings, environments, . . . are themselvesobjects.

Every MELT value (including boxed gimple-s, closures, lists . . . ) has a discriminantand can be sent a message.

32A faster unsafe variant exist unsafe_get_field for gurus.

Basile STARYNKÉVITCH (CEA, LIST) Customizing GCC with MELT July 6, RMLL 2010 33 / 42

Page 34: Extending the GCC compiler with MELT to suit your needs2010.rmll.info/IMG/pdf/6-rmll-2010-07-06-GCC-MELT.pdf · 2010-07-06 · Introduction and context About this talk What this talk

Extending GCC with MELT

Pattern-matching GCC tree-s

A MELT function can accept as secondary formal arguments raw (unboxed!)data stuff like plain GCC tree-s and filter it by pattern-matching:

(defun write_prototype_return(buffer :tree type) ;; :tree keyword for raw stuff

(match type;;patterns start with a question mark and can be nested

(?(tree_of_type ?(tree_integer_type?(tree_type_declaration

?(tree_identifier ?name)) ?_ ?_ ?_))(debugcstring "Integer :" name)(add2sbuf_strconst buffer name))

(?(tree_of_type ?(tree_real_type?(tree_type_declaration

?(tree_identifier ?name)) ?_))(debugcstring "Real : " name)(add2sbuf_strconst buffer name))

(?_ ;;joker variable match anything)))

Pattern-matching is perhaps the best feature of MELT . Try to code above in C!33

33Or even in Guile or Python, assuming an imaginary GCC binding to Guile or Python!Basile STARYNKÉVITCH (CEA, LIST) Customizing GCC with MELT July 6, RMLL 2010 34 / 42

Page 35: Extending the GCC compiler with MELT to suit your needs2010.rmll.info/IMG/pdf/6-rmll-2010-07-06-GCC-MELT.pdf · 2010-07-06 · Introduction and context About this talk What this talk

Extending GCC with MELT

Matching GCC gimple-s in MELT

(let ( (:gimple g dsome code to get a gimplec) )ddisplay the gimple g for debuggingc(match g

(?(gimple_assign_cast ?lhs ?rhs)dprocess lhs and rhs for a casting assignmentc )

(?(gimple_assign_single?lhs?(and ?rhs

?(tree_var_decl ?(cstring_same "stdout"))))dprocess lhs and rhs as an assignment from stdout.c )

(?(gimple_call_2_more ?lhs?(and ?callfndcl

?(tree_function_decl ?(cstring_same "fprintf") ?_))?argfile ?argfmt ?nbargs)

dhandle the fprintf case to file argfile with format argfmtc)(?_ dotherwise...c))

Basile STARYNKÉVITCH (CEA, LIST) Customizing GCC with MELT July 6, RMLL 2010 35 / 42

Page 36: Extending the GCC compiler with MELT to suit your needs2010.rmll.info/IMG/pdf/6-rmll-2010-07-06-GCC-MELT.pdf · 2010-07-06 · Introduction and context About this talk What this talk

Extending GCC with MELT

Iterative MELT constructs

MELT provides a lot of iterative constructs34 !

For instance, to iterate on each argument of a Gimple which represents a callin the GCC compiled code:

;; assume gbox is a MELT value, usually a boxed Gimple(foreach_argument_of_gimple_call

( (gimple_content gbox) )(:tree targ)

dhandle the targ raw treec)

To print on stdout integers from 1 to 10 using a C code chunk(foreach_long_upto

(1 10)(:long i)(code_chunk print_i ;; a macro-string follows!

#{/*$print_i*/ printf("%d\n", $I);}# ))

The code_chunk syntax is to MELT what asm is to C!34Additional iterative constructs are easily defined thru defciterator

Basile STARYNKÉVITCH (CEA, LIST) Customizing GCC with MELT July 6, RMLL 2010 36 / 42

Page 37: Extending the GCC compiler with MELT to suit your needs2010.rmll.info/IMG/pdf/6-rmll-2010-07-06-GCC-MELT.pdf · 2010-07-06 · Introduction and context About this talk What this talk

Extending GCC with MELT

Interfacing MELT to GCC

MELT contains many linguistic devices describing how to generate C code.Iterators are defined like

(defciterator foreach_long_upto(:long imin imax) ;start formalseachlong ;state(:long ix) ;local formals;; documentation chunk35

:doc #{The $FOREACH_LONG_UPTO c-iterator provides the usualascending integer iterator. Start formals are $IMIN, the minimum startinteger, and $IMAX, le maximal ending integer. Local formal is $IX,the current index. The body is executed for each integer value $IXfrom $IMIN to $IMAX included.}#;;before code chunk expansion#{/*start $eachlong */

long $eachlong#_min = $imin;long $eachlong#_max = $imax;long $eachlong#_cur = 0;for ($eachlong#_cur = $eachlong#_min;

$eachlong#_cur <= $eachlong#_max;$eachlong#_cur ++) {

$ix = $eachlong#_cur; }#;after expansion#{ } /*end $eachlong */}#

)

35MELT has its own doxygen-like API documentation generator.

Basile STARYNKÉVITCH (CEA, LIST) Customizing GCC with MELT July 6, RMLL 2010 37 / 42

Page 38: Extending the GCC compiler with MELT to suit your needs2010.rmll.info/IMG/pdf/6-rmll-2010-07-06-GCC-MELT.pdf · 2010-07-06 · Introduction and context About this talk What this talk

Extending GCC with MELT

A C-matcher defines pattern matchers occurrence by their C expansion:(defcmatcher tree_type_declaration(:tree tr)(:tree declaration)titd#{ /* $titd ? */

(($tr) && TREE_CODE($tr) == TYPE_DECL)}#

#{ /* $titd ! */$declaration = DECL_NAME($tr);

}#)

So adding new pattern operators is easy36. Following the (slow) evolution ofcore GCC I.R. is fairly simple.

MELT pattern sub-language is very powerful (and has also conjunctive, disjunction,conditionals, . . . patterns) and flexible.

36Pattern operators can also be defined by a function in MELT using defunmatcher.

Basile STARYNKÉVITCH (CEA, LIST) Customizing GCC with MELT July 6, RMLL 2010 38 / 42

Page 39: Extending the GCC compiler with MELT to suit your needs2010.rmll.info/IMG/pdf/6-rmll-2010-07-06-GCC-MELT.pdf · 2010-07-06 · Introduction and context About this talk What this talk

Conclusion and invitation

Conclusion and invitation

Basile STARYNKÉVITCH (CEA, LIST) Customizing GCC with MELT July 6, RMLL 2010 39 / 42

Page 40: Extending the GCC compiler with MELT to suit your needs2010.rmll.info/IMG/pdf/6-rmll-2010-07-06-GCC-MELT.pdf · 2010-07-06 · Introduction and context About this talk What this talk

Conclusion and invitation

Present state of MELT (start of july 2010)

MELT is usable, but still work in progress37.MELT strengths are

an efficient copying MELT garbage collector, built abve GgcSo allocation of very temporary MELT values is quick and handling GCCdata is easy!The MELT translator is bootstrapped; it is coded in MELT and translated atMELT building time.MELT translation is quick38

MELT is usable as a GCC 4.5 plugin, or as a branchMELT is tightly integrated within GCC

MELT has strong pattern matching (and interesting other features)MELT has a french mailing list [email protected]

37Some bugs are being worked on38The bottleneck is the time required to compile the generated C code!

Basile STARYNKÉVITCH (CEA, LIST) Customizing GCC with MELT July 6, RMLL 2010 40 / 42

Page 41: Extending the GCC compiler with MELT to suit your needs2010.rmll.info/IMG/pdf/6-rmll-2010-07-06-GCC-MELT.pdf · 2010-07-06 · Introduction and context About this talk What this talk

Conclusion and invitation

Future work on MELT

bug correctionsgenerating OpenCL code in MELT from Gimple (OpenGPU)supporting and helping MELT usersmore powerful static analyzersbetter LTO supportetc . . .

Basile STARYNKÉVITCH (CEA, LIST) Customizing GCC with MELT July 6, RMLL 2010 41 / 42

Page 42: Extending the GCC compiler with MELT to suit your needs2010.rmll.info/IMG/pdf/6-rmll-2010-07-06-GCC-MELT.pdf · 2010-07-06 · Introduction and context About this talk What this talk

Conclusion and invitation

Invitation to use MELT in big free software projects

The biggest asset of any big39 free software project is their existing legacycode (compiled by GCC). Developping specific GCC extensions in MELT is oneof the best way to preserve and improve that asset.

Defining and experimenting such GCC extensions is the responsability of thefree software communities, because it requires expertise on these softwareprojects.

Question: who will use MELT? For what projects ?

Contact me [email protected]

39typically large projects of half a million lines of source code or more should benefit from GCCextensions in MELT

Basile STARYNKÉVITCH (CEA, LIST) Customizing GCC with MELT July 6, RMLL 2010 42 / 42