AUTOMATIC GENERATION OF CODE OPTIMIZERS FROM FORMAL SPECIFICATIONS Vineeth Kumar Paleri Regional Engineering College, calicut Kerala, India. , Visiting Assistant Professor, Dept. Computer and Information Scien
Jan 17, 2016
AUTOMATIC GENERATION OF CODE OPTIMIZERSFROM
FORMAL SPECIFICATIONS
Vineeth Kumar Paleri
Regional Engineering College, calicutKerala, India.
(Currently, Visiting Assistant Professor, Dept. Computer and Information Science, IUPUI).
Code Optimization ( Transformation ): Introduction
To reduce the space or running of the code. To reduce the space or running of the code.
Scalar and Parallel TransformationsScalar and Parallel Transformations
Scalar Transformations:Scalar Transformations: Reduce the number of instructions executed.Reduce the number of instructions executed. Eg. Useless code elimination, Constant propagation.Eg. Useless code elimination, Constant propagation.
Parallel Transformations:Parallel Transformations: Maximize parallelism and Memory localityMaximize parallelism and Memory locality Eg. Loop Interchange, Loop fusionEg. Loop Interchange, Loop fusion..
Code Transformation: StagesCode Transformation: Stages Data flow analysisData flow analysis
Collects information regarding ‘definitions’ and ‘uses’ of Collects information regarding ‘definitions’ and ‘uses’ of variablesvariables..
Modification of codeModification of code An exampleAn example::
constant propagationconstant propagation• • •
X:= 5;X:= 5;• • Z:X+YZ:X+Y• •
Automatic Generation of code OptimizersAutomatic Generation of code Optimizers::
Formal specification of code transformations.Formal specification of code transformations.
• Framework for the specification.Framework for the specification.
Design of a specification language.Design of a specification language.
Development of a generator to produce code from Development of a generator to produce code from specifications.specifications.
Framework for Specification of code TransformationsFramework for Specification of code Transformations ::
Dependence RelationDependence Relation
Control dependenceControl dependence Data dependenceData dependence
Flow dependenceFlow dependence Anti dependenceAnti dependence Output dependenceOutput dependence
Control DependenceControl Dependence::
SSii : if ( x = c) : if ( x = c)
SSjj : y :=1 : y :=1
SSjj is control dependent on S is control dependent on Sii; ;
SSii c c S Sjj
Data DependenceData Dependence::
Flow dependenceFlow dependence
SSii : x := a + b : x := a + b
SSjj : y := x + c : y := x + c
SSjj is flow dependent on S is flow dependent on Sii; S; Sii S Sjj
Anti dependenceAnti dependence
SSii : x := y + a : x := y + a
SSjj : y := a + b : y := a + b
SSjj is anti dependent on S is anti dependent on Sii; S; Sii S Sjj
__
Output dependenceOutput dependence
SSii : z := x + a : z := x + a
SSjj : z := y + b : z := y + b
SSjj is Output dependent on S is Output dependent on Sii; S; Sii oo
S Sjj
Intermediate RepresentationIntermediate Representation High-level intermediate representation
• Abstract syntax trees
Low-level intermediate representation
• Three – address code
d s t : = scr1 op scr2
Specification FormatSpecification Format
PreconditionCombination of some basic conditionsBasic conditions
•Conditions to identify interested program elements(E.g.. Statements and loops)•Checks for dependence relations between pairs of statements
ActionSequence of primitive actions to perform the transformationPrimitive actions
•delete-stmt(Si)•insert-stmt(Si,position)•replace-operand(opr1,opr2)
Formal Specification:An ExampleFormal Specification:An Example
Useless code elimination
Si: definition-stmt(Si) : (∄Sj : Sj Si : Si Sj)
{delete-stmt(Si);}
Specification LanguageSpecification Language
Syntax
transformation-name:[ action-quantifier : conditional-expropt
: conditional-expropt
{action-list}]
An Example:Useless code elimination
useless-code-elimination:[
foreach stmt-i: : useless-stmt(stmt-i)
{ delete-stmt(stmt-i); }]
Specification Language :FeaturesSpecification Language :Features
Powerful enough to express almost all traditional scalar Powerful enough to express almost all traditional scalar transformationstransformations
Permits modularity in expressing complex specificationsPermits modularity in expressing complex specifications Permits extensions to the languagePermits extensions to the language
Automatic Generation of Code OptimizersAutomatic Generation of Code Optimizers
Stanford University Intermediate Format - SUIFStanford University Intermediate Format - SUIF
• SUIF consists of
A Kernel
• Defines the intermediate representation
•Provides functions to access and manipulate the
intermediate representation. A toolkit
• C and Fortran front-ends
•MIPS back-end
•A set of compiler passes
•Each pass typically performs a single analysis or
transformation and then writes the results on to the file
Optimizer GeneratorOptimizer Generator
• Input : Specification for code transformations in the
specification language
• Output : C language code performing those transformations
• Lexical analysis
•Using LEX
• Syntax analysis
•Using YACC
• C language code generation
• By syntax-directed translation approach
Optimized GeneratorOptimized Generator
•Semantic actions are associated with productions of the grammar
to emit code
•Precondition part is used to generate code
•For identifying the program elements of interest
•To check the conditions to be satisfied for the transformation
•Action part is used to generate code
•To perform the corresponding transformations
Syntax-directed translation approachSyntax-directed translation approach
Optimizer Generator : Syntax-directed Translation ApproachOptimizer Generator : Syntax-directed Translation ApproachTransformation-name :
[
foreach program-element {semantic actions to generate a loop construct
for iteration over the instances of the program }
: conditional-expr1
{semantic actions to generate code for checking
the conditions to select the program elements of interest }
: conditional-expr2
{semantic actions to generate code to check the
conditions that have to be satisfied for the transformation}
: action-list
{semantic actions to generate code for performing the
corresponding transformations}
Optimizer Generator : An ExampleOptimizer Generator : An Example
Useless code elimination
Specification
useless-code-elimination :
[ foreach stmt-i :
: useless-stmt (stmt-i)
{delete-stmt(stmt-i);}
Generated code outline
Void useless-code-elimination (…..)
{ ……
while ( ! end-of-instruction-list )
{ ……
stmt-i = next-instruction ;
if ( useless-stmt (stmt-i))
{delete-stmt (stmt-i);}
}
}
Dependence Analysis
Computation of Control Dependence
•Algorithm
begin
Construct the control flow graph, CFG;
Construct the augment control flow graph, ACFG;
Construct the reverse augmented control flow graph, RACFG;
Compute the dominators of each node in RACFG;
Compute the dominance frontier of each node in RACFG;
Compute control dependencies,from the dominance frontier information;
end
Dependence AnalysisDependence Analysis
Computation of Data Dependence
Iterative data-flow analysis
Data dependence computation are formulated as data-flow
analysis problems by setting up corresponding data-flow
equations
Dependence Analysis : Computation of Data DependenceDependence Analysis : Computation of Data DependenceExample : Flow Dependence Computation
• Flow dependence computation is essentially the identification of all
reachable uses of each definition in a program, with the following
data-flow equations:
in [B] = gen [B] U ( out [B] – kill [B] )
out [B] = US ε succ (B) in [S]
Where,
in [B] is the set of uses reachable from the beginning of basic block B,
out [B], the set of uses reachable from the end of B,
gen [B], the set of upward exposed uses in B, and
kill [B], the set of uses reachable from the end of B, of all variables
defined in B
The Transformation System : ExperimentsThe Transformation System : Experiments
Generated optimizers were applied on LINPACK Generated optimizers were applied on LINPACK
benchmark routines benchmark routines Correctness of the generated optimizers verified by Correctness of the generated optimizers verified by
comparing the output of the optimized code with the comparing the output of the optimized code with the output from the original unoptimized codeoutput from the original unoptimized code
ConclusionConclusion
Generated code for all traditional scalar transformations Generated code for all traditional scalar transformations from formal specificationsfrom formal specifications
Verified correctness of the generated codeVerified correctness of the generated code System can be extended to include parallel transformationsSystem can be extended to include parallel transformations LimitationsLimitations
• Could not specify some of the transformations in the chosen
framework
• The transformation system is not efficient
ReferenceReference
Vineeth Paleri . An environment for automatic generation of code optimizers . PhD Thesis , Department of Computer science and Automation , Indian Institute of Science , Bangalore, 1999.
Also,
(Technical Report : TR No. IISC-CSA-2001-2 , http://www.csa.iisc.ernet.in )