Nov 7, 2002 ... G. Agosta, G. Falauto. The SUIF Optimizing Compiler. Overview. • What is SUIF. •
How to compile with SUIF. • How to write a pass in SUIF.
The SUIF Optimizing Compiler Giovanni Agosta Gerlando Falauto November 7, 2002
0-0
G. Agosta, G. Falauto
The SUIF Optimizing Compiler
Overview • What is SUIF • How to compile with SUIF • How to write a pass in SUIF
ALaRI
November 7, 2002
1
G. Agosta, G. Falauto
The SUIF Optimizing Compiler
The Compilation Flow
ALaRI
November 7, 2002
2
G. Agosta, G. Falauto
The SUIF Optimizing Compiler
Optimization Phase
ALaRI
November 7, 2002
3
G. Agosta, G. Falauto
The SUIF Optimizing Compiler
Compiling with SUIF • The frontend accepts a C program file, and produces a SUIF IR file. It is invoked by the command c2suif • A SUIF file can then be passed through several stages of optimization, instrumentation, and analysis. All operations on the SUIF IR are performed through a driver, suifdriver • The driver can perform in both interactive (-i) and batch mode (-f )
ALaRI
November 7, 2002
4
G. Agosta, G. Falauto
The SUIF Optimizing Compiler
Compiling with SUIF .
ALaRI
/opt/suif2/nci/nci setup.sh
Sets enviroment variables for SUIF
suifdriver -i
Starts the driver in interactive mode
suif> require s2c
Loads SUIF to C backend pass
suif> load foo.suif
Loads program file in SUIF IR format
suif> s2c foo.out.c
Runs the backend and creates C output
November 7, 2002
5
G. Agosta, G. Falauto
The SUIF Optimizing Compiler
Writing a Pass in SUIF A SUIF pass is made up of three parts: • References to the SUIF infrastructure • Declaration of a class that wraps the pass procedures • Pass initialization The implementation of the pass procedures is usually kept in a different source file for clarity and readability. Lets have a look at a simple SUIF pass, which counts the number of statements within a procedure.
ALaRI
November 7, 2002
6
G. Agosta, G. Falauto
The SUIF Optimizing Compiler
Include directives #include #include #include #include #include #include #include
”suifnodes/suif.h” ”suifkernel/suif env.h” ”suifkernel/module subsystem.h” ”suifkernel/utilities.h” ”suifkernel/group walker.h” ”suifpasses/suifpasses.h” ”suifkernel/visitor map.h”
#include ”basicnodes/basic factory.h”
ALaRI
November 7, 2002
7
G. Agosta, G. Falauto
The SUIF Optimizing Compiler
The CounterPass class class CounterPass : public PipelinablePass { SuifEnv ∗ env; public: CounterPass(SuifEnv ∗env, const LString &name): PipelinablePass(env, name) { env = env; } virtual ∼CounterPass(void) { } Module ∗clone() const { return(Module∗)this;} void do procedure definition(ProcedureDefinition∗ proc def); }; ALaRI
November 7, 2002
8
G. Agosta, G. Falauto
The SUIF Optimizing Compiler
Initialization extern "C" void init counter(SuifEnv ∗suif env) { ModuleSubSystem ∗ms = suif env→get module subsystem(); ms→register module( new CounterPass (suif env, "solve counter")); }
ALaRI
November 7, 2002
9
G. Agosta, G. Falauto
The SUIF Optimizing Compiler
Implementation #include ”counter.hh” void CounterPass::do procedure definition(ProcedureDefinition∗ proc def) { printf("\nInstruction count for procedure %s \n\n", proc def→get procedure symbol()→get name().c str()); int statement count =0; //Iterator declaration and initialization Iter it = object iterator(proc def); printf("Iterator length ..........%d.\n", it.length());
ALaRI
November 7, 2002
10
G. Agosta, G. Falauto
The SUIF Optimizing Compiler
//Computation steps for ( ; it.is valid(); it.next() ) if ( is kind of(&it.current()) && !is a(&it.current()) ) statement count++; //Output to the terminal printf("There are %3d statements.\n\n", statement count); }
ALaRI
November 7, 2002
11