An Overview of the SUIF2 System Monica Lam Stanford University http://suif.stanford.edu/ The SUIF System PGI Fortran Interprocedural Analysis Parallelization Locality Opt C EDG C++ SUIF2 Alpha Java Inst. Scheduling Register Allocation x86 Research Infrastructure: Support at 3 Levels I. Compose compiler with existing passes Front ends + passes Easy expression of compiler compositions (dynamically) II. Develop new passes User concentrates on algorithmic issues Infrastructure takes care of common functionalities Standard intermediate representation and associate tools Common utilities and data structures III. Develop new IR (new language constructs or analyses) Easy definition of IR nodes Old code works with new IR without recompilation I: Compose Compiler with Existing Passes Previous system Each pass is an independent program that reads/writes SUIF in a file Advantages: modular, visibility of intermediate results Disadvantages: expensive disk access New system write code once, can run on program on disk or in memory simple development, efficient deployment How? A pass = a common driver + module A compound pass • a common driver + series of modules loaded dynamically Modular Compiler System COMPILER A series of stand-alone programs Suif-file1 driver+module1 Suif-file2 driver+module2 Suif-file3 A driver that imports & applies modules to program in memory Suif-file1 Suifdriver imports/executes module1 module2 module3 driver+module3 Suif-file4 Suif-file4 SUIF Driver > suifdriver suif> import basicnodes suifnodes suif> import mylibrary suif> load test.suif suif> mylibrary_pass1 suif> print test.out suif> save test.tsuif Suifdriver: accepts a simple scripting language pre-registered modules (import, load, print, save) loads libraries dynamically which can register new modules (new commands) The SUIF Architecture Executable suifdriver MODULES: Passes analyses optimizations Kernel suifkernel iokernel IR suifnodes basicnodes Components Kernel: provides all basic functionality iokernel: implements I/O suifkernel: hides iokernel and provides modules support, cloning, command line parsing, list of factories, etc. Modules passes: provides a pass framework IR: basic program representations Suifdriver provides execution control over modules and passes using a scripting language II. Develop a New Pass Kernel: provides all basic functionality iokernel: implements I/O suifkernel: hides iokernel and provides modules support, cloning, command line parsing, list of factories, etc. Modules passes: provides a pass framework <-- Derive from this framework IR: basic program representations Suifdriver provides execution control over modules and passes using a scripting language Compilation System = SuifEnv + Modules SuifEnv: Keeps the states of the compilation Holds onto the representation (FileSetBlock) and the loaded modules A module is a C++ class that implements either a pass or a set of nodes in IR must have a unique module_name no global variables one or more modules make up a dll each library includes a function (init_<dllname>) to register all modules dynamically Registration is based on a prototype object Example Pass: Count Statements in Procedures class mypass: public Pass { public: mypass(SuifEnv *env, const Lstring &name): Pass(env, name) {} virtual ~mypass() {} Module *clone() const {return(Module*) this:} void do_procedure_definition (ProcedureDefinition* proc_def) { cout << proc_def->get_procedure_symbol() ->get_name() cout << object_iterator<ExecutionObject>(proc_def).length(); } } extern “C” void init_mypass (SuifEnv *suif_env) { suif_env->get_module_subsystem()->register_module (new mypass (suif_env, “mypass”)); } Tools for writing passes Traversing and manipulating data structures Visitors: dispatch method according to type Iterators: simple iteration of certain objects Walkers: user-controllable traversal of data structures Data structures Infinite precision integers strings lists, sets, hash maps assertion/error handling Program Representation Multiple representations for semantics at different abstraction levels e.g. FOR loops or statement list with branches and jumps High-level representation useful for high-level transformations => Superset representation: mixture of high-level and low-level constructs Dismantlers lower representation Allow analysis to operate at different levels of abstraction e.g. analysis may/may not care about specific arithmetic operators => Uses object-oriented class hierarchy SUIF IR Design Root: Object Uniform functionality Fields accessed with get_<field>, and set_<field> printing I/O to disk cloning iterators walkers Memory management aid: object creation via factories owner edges embed a tree into program representation Object SUIFObject AnnotableObject SymbolTable ScopedObject ExecutionObject Statement Expression Supports Code with Abstraction ExecutionObject Statement get_child_statements IfStatement WhileStatement get_then_part get_else_part get_body Maximize reuse by abstract fields allow uniform access to different child components III. Extending the IR Goals Support extension by different research groups Code written for a high-level IR should work on refined IR (without recompilation!) Easy specification of new extensions Grammar-based specification How? Meta-class system Objects contain information describing representation If environment does not include definitions of refinements • information retained even if methods are not available • written out when representation saved Insulating the User from the Implementation Object Definition (.hoof) SUIF Macro Generator a general grammar-based tool Interface for user (.h) Implementation in Meta-Class System (.cpp) Meta-Class System reading & writing to file in machine-independent format • Easy for the programmer • Easy for the implementor to develop the system Example of a Hoof Definition concrete New { int x; } class New : public SuifObject { public: int get_x(); void set_x(int the_value); ~example(); void print(…); static const Lstring &get_class_name(); … } Uniform data access functions (get_ & set_) Automatic generation of meta class information etc. Examples of Suif Nodes abstract Statement : ExecutionObject { virtual list<Statement* owner> child_statements; ... } concrete IfStatement : Statement { Expression * condition in source_ops; Statement * owner then_part in child_statements; Statement * owner else_part in child_statements; } Documentation The SUIF2 Infrastructure Guide (an index to all documentation) Overview of the SUIF Infrastructure The SUIF Representation Guide The Basic SUIF Programmer’s Guide Web page: http://suif.stanford.edu/suif2