Docsity

Translation Validation of Optimizing Compilers, Thesis of Compiler Design

Partial preview of the text

Download Translation Validation of Optimizing Compilers and more Thesis Compiler Design in PDF only on Docsity!

Translation Validation of Optimizing

Compilers

by

Yi Fang

A dissertation submitted in partial fulfillment of the requirements for the degree of Doctor of Philosophy Department of Computer Science Courant Institute of Mathematical Sciences New York University December, 2005

Advisor: Amir Pnueli

Abstract

There is a growing awareness, both in industry and academia, of the crucial role of formally verifying the translation from high-level source-code into low- level object code that is typically performed by an optimizing compiler. Formally verifying an optimizing compiler, as one would verify any other large program, is not feasible due to its size, ongoing evolution and modification, and possibly, proprietary considerations. Translation validation is a novel approach that offers an alternative to the verification of translator in general and compilers in partic- ular: Rather than verifying the compiler itself, one constructs a validation tool which, after every run of the compiler, formally confirms that the target code pro- duced in the run is a correct translation of the source program. This thesis work takes an important step towards ensuring an extremely high level of confidence in compilers targeted at EPIC architectures. The dissertation focuses on the translation validation of structure-preserving optimizations, i.e., transformations that do not modify programs’ structure in a major way, which include most of the global optimizations performed by compil- ers. The first part of the dissertation develops the theory of a correct translation, which provides a precise definition of the notion of a target program being a cor- rect translation of a source program, and the method that formally establishes the correctness of structure preserving transformations based on computational induc- tion. The second part of the dissertation describes a tool that applies the theory

Contents

List of Tables

6.1 Translation from IR to Transition Relation............. 90

vi

List of Figures

1.1 Refinement Completes the Picture................. 5 1.2 The architecture of TVOC...................... 6 2.1 The syntax of the IR intermediate language............ 12 2.2 A C Program and its Intermediate Code.............. 12 2.3 Example 1 represented as Control-Flow Graph........... 15 2.4 Example 1 represented in SSA Form................ 16 3.1 The Proof Rule VALIDATE..................... 30 3.2 Example : Source and Annotated Target Programs......... 31 4.1 An example with a condition that is a common subexpression... 57 5.1 Example: a source program and its target transformed by a series of optimizations: Constant folding, copy propagation, dead code elimination, control flow graph optimization (loop inversion), and strength reduction, both in SSA form................ 66 5.2 Procedure to Compute Invariants.................. 74

vii

Chapter 1

Introduction

1.1 Motivation

There is a growing awareness, both in industry and academia, of the crucial role of formally proving the correctness of safety-critical portions of systems. Most verification methods focus on verification of specification with respect to require- ments, and high-level code with respect to specification. However, if one is to prove that the high-level specification is correctly implemented in low-level code, one needs to verify the compiler which performs the translations. Verifying the correctness of modern optimizing compilers is challenging because of the com- plexity and reconfigurability of the target architectures, as well as the sophisticated analysis and optimization algorithms used in the compilers. Formally verifying a full-fledged optimizing compiler, as one would verify any other large program, is not feasible, due to its size, evolution over time, and,

possibly, proprietary considerations. Translation Validation is a novel approach that offers an alternative to the verification of translators in general and of com- pilers in particular. Using the translation validation approach, rather than verify the compiler itself one constructs a validating tool which, after every run of the compiler, formally confirms that the target code produced is a correct translation of the source program. The introduction of new families of microprocessor architectures, such as the EPIC family exemplified by the Intel IA-64 architecture, places an even heavier responsibility on optimizing compilers. Compile-time dependence analysis and instruction scheduling is required to exploit instruction-level parallelism in order to compete with other architectures, such as the super-scalar class of machines where the hardware determines dependences and reorders instructions at run-time. As a result, a new family of sophisticated optimizations have been developed and incorporated into compilers targeted at EPIC architectures. At first glance, the approach of translation validation is designed to ensure the correct functioning of compilers. But it also have impact on the testing process of compiler development. Since the output of compilers is unreadable, people can recognize a incorrect compilation only by observing an incorrect program execution. As compiler testers have observed, many times an “incorrect output” is caused not by the compiler itself, but by the errors in the program it compiles. As a result, much of the time and efforts in compiler testing is spent in isolating the bugs in the testing programs from those in the compiler. Since the translation validation approach checks the output of compilers directly, the error it detects

tant contribution is the ability to handle pointers in the source program. However, the method proposed there assumes full instrumentation of the compiler, which is not assumed here or in [Nec00]. The work in [LJWF04] presents a framework for describing global optimiza- tions by rewrite rules with CTL formulae as side conditions, which allow for gen- eration of correct optimizations, but not for verification of (possibly incorrect) optimizations. The work in [GGB02] proposes a method for deploying optimiz- ing code generation while correct translation between input program and code. They focus on code selection and instruction scheduling for SIMD machines.

1.3 Overview of the Compiler Validation Project

The ultimate goal of our compiler validation project is to develop a methodology for the translation validation of advanced optimizing compilers, with an empha- sis on EPIC-targeted compilers and the aggressive optimizations characteristic to such compilers. Our methods will handle an extensive set of optimizations and can be used to implement fully automatic certifiers for a wide range of compilers, ensuring an extremely high level of confidence in the compiler in areas, such as safety-critical systems and compilation into silicon, where correctness is of para- mount concern. The approach of translation validation is based on the theory of correct trans- lation. In general terms, we first give common semantics to the source and target languages using the formalism of Transition Systems ( TS ’s). The notion of a target

code T being a correct implementation of a source code S is then defined in terms of refinement , stating that every computation of T corresponds to some computa- tion of S with matching values of the corresponding variables. In Figure 1.1 we present the process of refinement as completion of a mapping diagram.

Compiler Semantics Mapping

Semantics Mapping Optimizing Refinement

S: Source Sem(S)

T : Target Sem(T)

Figure 1.1: Refinement Completes the Picture

To apply the general approach of translation validation to verifying compiler optimizations, we distinguish between structure preserving optimizations, which admit a clear mapping of control points in the target program to corresponding control points in the source program, and structure modifying optimizations that admit no such mapping. Structure Preserving optimizations cover most high-level optimizations and many low-level control-flow optimizations. Our approach for dealing with these optimizations is to establish a correspondence between the target and source code, based on refinement , and to prove it by simulation. According to this approach, we establish a refinement mapping correlating the control points the source and target, and indicating how the relevant source variables correspond to the target variables or expressions at each control point. The proof is then broken into a set of verification conditions (also called proof obligations ), each claiming that a

and target program T. These are provided in the WHIRL intermediate representa- tion, a format used by the ORC compiler among others. Just as compilers perform optimizations in multiple passes, it is reasonable to break the validation into mul- tiple phases, each using a different proof rule and focusing on a different set of optimizations. Currently, TVOC uses two phases to validate optimizations per- formed by the compiler, with two components TVOC-LOOP and TVOC-SP. In the first phase, TVOC-LOOP attempts to detect loop reordering transformations and generates an intermediate program S′^ which is obtained by transforming S so that it’s loop structure corresponds to that found in T. The equivalence of S and S′ is verified using rule PERMUTE [ZPG+02, ZPFG03]. Verification conditions are submitted to and checked by the automatic theorem prover CVC Lite [BB04]. In the second phase, TVOC-SP compares the program S′^ generated by phase 1 to the target program T. Since the transformation from S′^ to T is structure-preserving, the equivalence of these two programs is verified using rule VALIDATE (as pre- sented in Chapter 3) Again, verification conditions are submitted to CVC Lite. If all verification conditions from both phases succeed, then the source and target are equivalent and TVOC outputs the result “VALID”. Otherwise, TVOC outputs “INVALID” with a message indicating the stage at which the verification failed.

1.4 Contribution and Thesis Outline

The main goal of this thesis research is to develop theory and tools for validating structure preserving optimizations. The contribution of my thesis work includes:

  • participating in desiging the current version of a proof rule, called VALIDATE , for validating structure preserving optimizations;
  • developing TVOC-SP, a tool that automatically generate proofs of VALIDATE rule for structure preserving optimizations of an EPIC compiler;
  • modeling memory operations and aliasing information to accomodate pro- grams with arrays and pointers.

Chapter 2 describes a formal model and theory of correct translation. It also presents definitions and terms commonly used in the research of compiler opti- mizations that is later refered in this thesis. Chapter 3 first describes a general proof rule, called VALIDATE , for translation validation of structure preserving optimizations. The proof rule presented in this thesis succeeds a series of proof rules that were sound but failed to handle some common optimizations. The current version has the versitility to handle minor structure changes in transformation, as long as the structure of loops is preserved. Then I define the well-formedness of a VALIDATE proof, and show that, given well-formed proofs P 1 and P 2 that establish the refinement between S and T 1 , and the refinement between T 1 and T 2 , respectively, we can compose a VALIDATE proof from P 1 and P 2 that establishes the refinement between S and T 2 directly. Chapter 4 studies a series of individual compiler optimizations that are candi- dates for validation with rule VALIDATE. The study of individual optimizations not only shows the scope of rule VALIDATE , but also gives valuable insights of what type of the refinement mapping and auxiliary invariants are required to construct

Chapter 2

Preliminaries

2.1 Program Representation

The programs we consider are written in WHIRL [CJW01], the intermediate lan- guage ( IR ) for the ORC compiler. As compilation can be viewed as a process of gradual transition from the high level language constructs to the low level ma- chine instructions, there are different levels of IR possible. The closer an IR is to the source language, the higher is its level. The more an IR resembles the machine instructions, the lower is its level. The WHIRL IR was designed to be capable of representing any level of IR from Very High to Very Low except the level that corresponds to the machine instructions. The compiler optimizations we focus on are the global optimizations, which are performed on High and Mid WHIRL , and the control-flow optimziations, which are perfomed on Low WHIRL. During the compilation from High WHIRL to Low WHIRL , high level control

flow constructs such as the operator DO LOOP , DO WHILE , WHILD DO , IF ,etc. are translated to be uniformly represented via TRUEBR , FALSEBR or GOTO in Low WHIRL. In addition, the form of array subscription preserved in High WHIRL via ARRAY is lowered to address expressions in Mid and Low WHIRL. For exposition purposes we assume that programs are written in the IR inter- mediate language, which is a subset of Mid and Low WHIRL , whose syntax is shown in Fig. 2.1. A function body is a sequence of IR statements. Among state- ments, we have direct assignments to symbols, denoted by id ← expr; function entries, denoted by FUNC ENTRY f, id 1 ,... , idm, which represents a function f with an array of formal parameters id 1 ,... , idm; function returns RETURN id; la- bels LABEL L :; unconditional jumps GOTO ; and conditional jumps TRUEBR and FALSEBR. The expressions in the IR language contain symbol identities id, con- stants c, and composite expressions using a variety of operators expr 1 op expr 2. We assume that the evaluation of expression does not have side-effect, i.e. none of the program variables have their values modified in the process of evaluation. With the set of statements in IR as represented in Fig. 2.1, the high level control constructs such as loops and conditional branches preserved by the High WHIRL operators DO LOOP , DO WHILE , WHILD DO , IF , etc. are translated to be uni- formly represented via TRUEBR , FALSEBR or GOTO in Low WHIRL.

Example 1 Consider the function f oo written in C language in Fig. 2.2 and its translation, by Intel ORC compiler into the intermediate code IR_._