• Aucun résultat trouvé

Inferring functional properties of matrix manipulating programs by Abstract Interpretation

N/A
N/A
Protected

Academic year: 2022

Partager "Inferring functional properties of matrix manipulating programs by Abstract Interpretation"

Copied!
41
0
0

Texte intégral

(1)

(will be inserted by the editor)

Inferring functional properties of matrix manipulating programs by Abstract Interpretation

Matthieu Journault · Antoine Min´e

the date of receipt and acceptance should be inserted later

Abstract We present a new static analysis by abstract interpretation to prove automatically the functional correctness of algorithms implementing matrix oper- ations, such as matrix addition, multiplication, GEMM (general matrix multipli- cation), inversion, or more generally BLAS (Basic Linear Algebra Subprograms).

In order to do so, we introduce a family of abstract domains parameterized by a set of matrix predicates as well as a numerical domain. We show that our analysis is robust enough to prove the functional correctness of several versions of the same matrix operations, resulting from loop reordering, loop tiling, inverting the itera- tion order, line swapping, and expression decomposition. We extend our method to enable modular analysis on code fragments manipulating matrices by reference, and show that it results in a significant analysis speedup.

1 Introduction

Static analysis by abstract interpretation [7] allows discovering automatically prop- erties about program behaviors. In order to scale up, it employs abstractions, which induce approximations. However, the approximation is sound, as it con- siders a super-set of all program behaviors; hence, any property proved in the abstract (such as the absence of run-time error, or the validation of some speci- fication) also holds in all actual program executions. Static analysis by abstract interpretation has been applied with some success to the analysis of run-time errors [3]. More recently, it has been extended to proving functional properties, including array properties [6, 10, 1], such as proving that a sorting algorithm in- deed outputs a sorted array. In this work, we consider functional properties of a

This work is partially supported by the European Research Council under Consolidator Grant Agreement 681393 – MOPSA.

Matthieu Journault, Antoine Min´e

Sorbonnes Universit´es, UPMC Univ Paris 6, Laboratoire d’informatique de Paris 6 (LIP6), 4, place Jussieu, 75252 Paris Cedex 05, France,

E-mail: matthieu.journault@lip6.com E-mail: antoine.mine@lip6.com

(2)

1 /* N >= 5 */

2 for ( i =0; i < N ; i ++) 3 for ( j =0; j < N ; j ++) { 4 C [ i ][ j ] = 0;

5 for ( k =0; k < N ; k ++)

6 C [ i ][ j ] += A [ i ][ k ] * B [ k ][ j ];

Program 1: Matrix multiplicationC=A×B.

1 /* m >= 1 */

2 l =0;

3 w h i l e ( l < m ) {

4 m u l t i p l i c a t i o n ( A , A , B ) ; 5 i ++

6 }

Program 2: Computing the sequence A=Bm.

different kind, rarely tackled before (see related works in Sect. 8): properties on matrices. Consider as example Program 1 (see Subsection 4.4 for a presentation of the results of our analyzer on this program) starting with the assumptionN≥5.

Our analyzer will automatically infer the following postcondition for the program:

∀u, v ∈[0, N−1]2, C[u][v] = PN−1

w=0A[u][w]×B[w][v], i.e., the program indeed computes the product of matricesAandBintoC. Consider now Program 2 where multiplication(C,A,B)is Program 1. It computes the sequence A=Bm using function calls to a matrix manipulating function. Iterative constructions such as this one are useful in the field of matrix analysis (eigenvalues, eigenvectors, matrix inversion computation), therefore it is important to infer and prove automatically that such programs are indeed computing the expected sequence.

1.1 Introductory example

To explain how our method works in more details, we focus on a simpler intro- ductory example, the matrix addition from Program 3 (see Subsection 4.4 for a presentation of the results of our analyzer on this program). Note that we will show later that our method is also successful on the multiplication from Program 1, as well as more complex, optimized variants of these algorithms, such as the tiled addition in Program 12 on page 19.

We wish to design a sound analyzer capable of inferring that, at the end of line 10, the addition ofBandA has been stored intoC. Note that the program is parametric in the size N of the matrix, and we want our analysis to be able to prove that it is correct independently from the precise value of N. Therefore, we would like to infer that the formula φ= ∀a, ∀b, (0 ≤ a < N∧0 ≤ b < N)⇒ C[a][b] =A[a][b] +B[a][b] holds for all the memory states reachable at line 10.

In order to do so, the static analyzer needs to infer loop invariants for each of thewhile loops. For the outermost loop (line 3), we would infer:φi

= ∀a, ∀b, (0≤a < i∧0≤b < N)⇒C[a][b] =A[a][b] +B[a][b], and, for the innermost loop

(3)

1 /* N >= 5 */

2 i =0;

3 w h i l e ( i < N ) { 4 j =0;

5 w h i l e ( j < N ) {

6 C [ i ][ j ] = A [ i ][ j ] + B [ i ][ j ];

7 j ++;

8 };

9 i ++;

10 }

Program 3: Matrix additionC=A+B.

(line 5):φj

= (∀a, ∀b, (0≤a < i∧0≤b < N)⇒C[a][b] =A[a][b] +B[a][b])∧(∀b, 0≤b < j⇒C[i][b] =A[i][b] +B[i][b]). Note that the loop invariants are far more complex than the formula we expect at the end of the program. In particular, they depend on the local variablesiandj. They express the fact that the addition has been performed only for some lines (up to i) and, for φj, only on one part of the last line (up toj). Every formula we need can be seen as a conjunction of one or several sub-formulas, where each sub-formula expresses that the addition has been performed on some rectangular sub-part of the matrix. Therefore, we introduce the following formulaAdd(A, B, C, x, y, z, t) with which we will describe our matrices in the rest of the introductory example:

Add(A, B, C, x, y, z, t)=∀a, ∀b,(x≤a < z∧y≤b < t)

⇒C[a][b] =A[a][b] +B[a][b]

The formulas φij andφcan all be described as a conjunction of one or more instances of the fixed predicateAdd, as well as numeric constraints relating only scalar variables, including program variables (i,j) and predicate variables (x, y, z,t). Abstract interpretation provides numerical domains, such as polyhedra [11], to reason on numeric relations. We will design a family of abstract domains com- bining existing numerical domains with predicates such as Add and show how, ultimately, abstract operations modeling assignments, tests, joins, etc. can be ex- pressed as numeric operations to soundly reason about matrix contents. While the technique is similar to existing analyses of array operations [6, 10, 1], the applica- tion to matrices poses specific challenges: firstly, as matrices are bi-dimensional, it is less obvious how to update matrix predicates after assignments; secondly, matrix programs feature deep levels of loop nesting, which may pose scalability issues.

1.2 Contribution

The article presents new abstract domains for the static analysis of matrix-manipula- ting programs. Those abstract domains are based on a combination of parametric predicates and numerical domains and can be used on small programs (e.g. com- puting a matrix addition) as well as on more complicated programs calling matrix- manipulating functions. The analysis has been proved sound and implemented in a prototype (supporting a small pseudo-code language enabling matrix operations).

(4)

We provide experimental results proving the functional correctness of a few basic matrix-manipulating programs, including more complex variants obtained by the Plutosource to source loop optimizer [5, 4]. We show that our analysis is robust against code modifications that leave the semantic of the program unchanged, such as loop tiling performed by Pluto. In the context of full source to binary program certification, analyzing programs after optimization can free us from hav- ing to verify the soundness of the optimizer; therefore, our method could be used in combination with certified compilers, such as CompCert [19], while avoiding the need to certify optimization passes in Coq. Indeed, at the time of writing it is not yet possible to perform loop tiling optimizations using CompCert. The analysis we propose is modular: it is defined over matrix predicates that can be combined, and is furthermore parameterized by the choice of a numerical domain.

Moreover we added to our analyzer the ability to use some helper domains, mak- ing the process of defining new predicates and adding them to the analyzer more developer-friendly. Finally, the performance of our analyzer is quite encouraging.

The rest of the paper is organized as follows: Sect. 2 describes the programming language we aim to analyze; Sect. 3 formally defines the family of abstractions we are constructing. In Sect. 4, we introduce specific instances to handle matrix addi- tion and multiplication. In Sect. 5, we briefly present a modular inter-procedural version of our analysis and Sect. 6 exposes a small abstract domain that enables us to analyze iterative processes on matrices. This section concludes with Sect. 6.2 that presents the analysis of an example program that exploits the combination of all the abstract domains presented in this article. Sect. 7 presents our imple- mentation and our experimental results. Sect. 8 discusses related works, while Sect. 9 concludes. The appendix contains proofs of the soundness of our analyzer (Appendix B) and the source code of some programs mentioned in the article (Appendix C).

This article is an extended version of a paper published at SAS ’16 (see [18]).

Sects. 3 and 4 have been extended with examples and explanations. Sect. 6 presents new materials. The soundness proof (in Appendix B) is also a new addition.

2 Syntax and concrete semantics

2.1 Programming language syntax

We consider a small imperative language, in Fig. 1, based on variables X ∈ X, (bi-dimensional) array namesA ∈A, and size variables denoting the width and height of arrays S = {A.n | A ∈ A} ∪ {A.m | A ∈ A}, with arithmetic expres- sions E ∈E, boolean expressions B ∈ B, and commands C ∈C. The command ArrayAofE1 E2 denotes the definition of a matrixAof size E1×E2.

2.2 Concrete reachability

We define the concrete semantic of our programming language. It is the most pre- cise mathematical expression describing the possible executions of the program and it will be given in terms of postconditions for commands. This concrete se- mantic is not computable; therefore, in the rest of the document we will try to

(5)

E::= vR|XX|E1+E2|E1×E2|A[X1][X2]|A.n|A.m B::= E1< E2|E1E2|E1=E2| ¬B|B1B2|B1B2

C::= skip|X:=E|A[X1][X2]E|C1; C2 |

IfBthenC1 elseC2|whileBdoCdone|ArrayAofE1 E2

of a matrixAof sizeE1×E2.

Fig. 1: Syntax of the language.

∀vR,EJvKm=v EJXKm=mV(X)

EJE1+E2Km=EJE1Km+EJE2Km

EJE1×E2Km=EJE1Km× EJE2Km EJA.nKm=mV(A.n)

EJA.mKm=mV(A.m) EJA[X1][X2]Km=mA(A)(mV(X1), mV(X2)) whenmV(X1), mV(X2)N

Fig. 2: Evaluation of expressions,mdesignates (mV, mA).

BJE1< E2K

={m∈ M | EJE1Km <EJE2Km}

BJE1E2K

={m∈ M | EJE1Km≤ EJE2Km}

BJE1=E2K

={m∈ M | EJE1Km=EJE2Km}

BJ¬E1K

=M \ BJE1K

Fig. 3: Evaluation of booleans.

propose a computable approximation. The soundness property of Sect. 4.1 holds with respect to this concrete semantic.

2.2.1 Definitions.

Dis the domain of scalar values, and we assume from now on thatD=R.Mis the set of memory states, which are pairsM=MV×MAcontaining a scalar environ- mentMV

=V → Dand a matrix contents environmentMA

=A →(N×N)→ D (here we are not interested in proving that there is no out of bounds access, there- foreA[X1][X2] is defined as soon asX1, X2∈N), whereV(resp.A) is any subset of X∪S(resp.A).EJEK∈ M → Dis the semantic of an expressionE(it is well-defined only on memory states that bind the variables and array names appearing inE).

BJBK∈ ℘(M) defines the set of memory states in which B is true.PostJCK(S) denotes the set of states reachable from the set of statesS after a commandC. Finally var(E), var(B), var(C) denote the variables appearing respectively in an arithmetic expression, a boolean expression, a command. Figures 2, 3 and 4 describe the behavior of expressions, booleans and the state reachability of the program.

(6)

PostJCK(S)= match Cwith:

| skip S

| X:=E → {(mV[X:=EJEKm], mA)|mS}

| A[X1][X2]E → {(mV, mA[A:=λ(i, j).

EJEKm if (i, j) = (u, v) mA(A)(i, j) otherwise ])

|mS(mV(X1), mV(X2)) = (u, v)N2}

| IfBthenC1elseC2PostJC1K(S∩ BJBK)PostJC2K(S∩ BJ¬BK)

| WhileBdoC1 done lfp(λS0.SPostJC1K(S0∩ BJBK))∩ BJ¬BK

| ArrayAofE1E2 → {(mV[A.m:=EJE1Km, A.n:=EJE2Km], mA[A:=λ(i, j).0]) |mS}

| C1; C2 PostJC2K(PostJC1K(S))

Fig. 4: Reachability semantics,mdesignates the pair (mV, mA).

t::=A[x][y]|xY|t1+t2|t1×t2|vR|

z

X

x=y

t g::=t1=t2|t1t2|t1< t2

P::=tt|ff|g| ¬P1|P1P2|P1P2|P1P2| ∀xN, P1| ∃tA, P1

Fig. 5: Terms, atomic predicates, and predicates.

3 Generic abstract semantics

3.1 Predicates

As suggested in Sect. 1, we define predicates to specify relations between matrices.

The language of predicates is based on the expressions from our programming language, with added quantifiers. The predicate language is very general but we will only be using one fragment at a time to describe the behavior of our programs.

In order to do so we define terms t ∈ T, using dedicated predicate variables y ∈ Y (such that X∩Y= Y∩S = ∅), atomic predicates g ∈ G, and predicates P ∈ P, as shown in Fig. 5. Their interpretation will be respectively:ITJtK,IGJgK andIPJPK. Those interpretations are defined the same way as interpretations of expressions and booleans; therefore, they are not detailed here. var(P) denotes the free variables ofP that are inY∪X∪S. In the rest of the article, whenever we mention fresh variables, those will be fresh variables inY. ThePz

x=ytterm is not useful yet, however Subsection 4.2 will show how this construction enables us to link together multiple sub-predicates.

Example 1 Modulo some syntactic sugar, we can define a predicate such as:P+(A, B, C, x, y, z, t) = ∀u ∈ [x, z−1],∀v ∈[y, t−1], A[u][v] = B[u][v] +C[u][v]. The interpretation of such a predicate is the set of memory states in which the matrix Ais the sum of two matricesB andC on some rectangle.

(7)

Finally, we introduce the following relation P1 =S P2, meaning that ∃σ ∈ var(P1)→ var(P2) a bijection such that P1σ = P2, i.e., P1 andP2 are syntac- tically equivalent modulo a renaming of the variables (e.g., P+(A, B, C, x, y, z, t) =S P+(A, B, C, x0, y0, z0, t0)). This definition is extended to sets of predicates, P1=SP2, meaning that there is a one-to-one relation between the sets and every pair of elements in the relation are syntactically equivalent (e.g.,{Q(A, B, C, x, y, z, t), P(A, u, v)}=S{Q(A, B, C, x0, y0, z0, t0), P(A, u0, v0)}).

In the following we will only consider sets of predicatesPwich arewell-named in the sense that∀P 6=Q∈P,var(P)∩var(Q) =∅in that caseP=SQamounts to the existence of a renaming σ and a one-to-one relation b between Pand Q such that ∀P ∈P,∃Q∈Q, P =b(Q)σ. It is to be noted that there might exist more that one such one-to-one relation, this point will be detailed in the following Subsection 3.2.

3.2 Abstract states

We now assume we have an abstraction of the scalar environment MV (e.g. the interval or polyhedra [11] domain) that we callM]V, with concretization function γV, join ∪V, widening OV, meet with a boolean constraint ∩B,V or a family of boolean constraints uB,V, and a partial order relation @V. The set of variables bound by an abstract variable memory statea∈ M]V is notedvar(a)⊂X∪Y∪S. In the following, we define an abstraction for the analysis of our programming language, this abstraction being built upon a set of predicates describing relations between matrices and a numerical domain. To simplify, without loss of generality, we forbid predicates from referencing program variables or array dimensions, hence Y∩(X∪S) = ∅. We rely instead on the numerical domain to relate predicate variables with program variables, if needed.

Let us revisit the introductory example of Program 3. At program point 5 we would like to infer the invariant (∀a, ∀b, (0≤ a < i∧0≤ b < N)⇒C[a][b] = A[a][b] +B[a][b])∧(∀b, 0≤b < j⇒C[i][b] =A[i][b] +B[i][b]), using the predicate from Ex. 1 we can rewrite the invariant asP+(C, A, B, x, y, z, t)∧P+(C, A, B, x0, y0, z0, t0) with{x=y= 0∧z=i∧t=N∧x0=i∧y0= 0∧z0=i+ 1∧t0=j}. This last set of constraints can be represented by a numerical abstract domain. Such a set of predicates and the corresponding numerical abstract domain (that links predicate variables and program variables) will be called a monomial. In order to express disjunctions of states that require different numbers of predicates we will use as abstract state disjunctions of monomials.

Remark 1 The previous remarks amount to say that we want abstract states of the form:

(Mon1: numerical constraints 1 predicate 1 1 predicate 1 2 . . .)

(Mon2: numerical constraints 2 predicate 2 1 predicate 2 2 . . .)

...

(Monn: numerical constraints n predicate n 1 predicate n 2 . . .) where variables appearing inpredicate i jare bound by thenumerical constraints i

(8)

Hence, we will introduce a monomial as a conjunction of some predicates and a numerical abstract domain; an abstract state will be a disjunction of such monomi- als. Moreover, we want our state to be a set of monomials as small as possible (and in all cases, bounded, to ensure termination); therefore, we would rather use the disjunctive features provided by the numerical domain, if any, than predicate-level disjunctions. We enforce this rule through a notion of well-formedness, explained below.

Definition 1 (Monomial)We call awell-named monomial an element (P,a)∈ M=℘(P)× M]Vsuch thatS

P∈Pvar(P)⊂var(a) and∀P16=P2∈P,var(P1)∩ var(P2) =∅.

Definition 2 (Abstract memory state)We build an abstractionM]for mem- ory statesM that isM] =℘(M). We will say that an abstract memory state is well-named if every one of its monomials is well-named. The following concretiza- tion function is defined on every well-named abstract stateS]∈ M] as:

γ(S]) = [

(P,a)∈S]

{(mV|XS, mA)|mV∈γV(a)∧ ∀P ∈P,(mV, mA)∈ IP(P)}

wheremV|XSis the restriction ofmV toX∪S.

Example 2 (P+is the predicate defined in Example 1). Let us consider an abstract state{(P,a)}whereP={P+(A, B, C, x, y, z, t)}andais a polyhedron defined by the set of equations{x= 0, y= 0, z =n, t= 1, i= 1, j =n, A.n=n, B.n=A.n, C.n=A.n, C.m=B.m, B.m=A.m, A.m=n}. In this case:γ({(P,a)}) is the set of memory states in which the first column of the matrixA is the sum of the first columns of BandC.

Definition 3 (Well-formed)We will say that an abstract stateS]iswell-formed if ∀(P1,a1),(P2,a2)∈S],(P1=SP2)⇒(P1,a1) = (P2,a2), i.e., no two mono- mials inS] can be made equal through variable renaming.

The previous definition ensures that well-formed abstract states only have a bounded number of monomials. Indeed there is only a finite number of available predicates, and we want to keep unbounded numerical domains (so as to be ex- pressive enough), therefore we need to ensure that abstract elements are bounded.

Thewf operator ensures that a given set of predicate is only bound to a unique numerical domain.

Definition 4 (Shape)We will say that two abstract statesS]1, S2]have the same shape, notedS1]=F S2], when∃f:S1]→S2], a bijection,∀(P1,a1)∈S]1,(P2,a2) = f((P1,a1))⇒P1=SP2, we also say thatS]1andS2]have the sameshapeaccord- ing tof.

Some of our operations on abstract states may not output well-formed states.

We thus propose the following algorithm in order to construct a well-formed state.

This algorithm may lose some precision, but is always sound.

Definition 5 (Algorithm wf(S])) : while there is still a pair of monomials (P1,a1)6= (P2,a2)∈S]such thatP1=SP2:

(9)

t: S1]tS2]=wf(S]1S]2)

O: S1]OS]2

=

{(P1,a1OVa2σ)|m= (P1,a1)S1]} where (P2,a2) =f(m), P1=P2σ

ifS1], S2]have the same shape according tof

undefined otherwise

uB: S1]uBB=S

(P,a)∈S]1{(P,aB,VB)}

vB,V: a∈ M]VvB,VV

i∈IBiB=∀iI, γV(a)⊆ BJBiK

Fig. 6: Operations on abstract states.

– remove (P1,a1),(P2,a2) fromS] – find σsuch thatP1σ=P2 – add (P2,a1σ∪Va2) toS]

Remark 2 We can note that the algorithm from Definition 5 is non determinis- tic. The precision of the analysis was satisfactory on the examples we wanted to analyze, therefore no particular merging heuristic was needed.

Proposition 1 ∀S] well-named, γ(S])⊂γ(wf(S]))

The proof can be found in Appendix B. It is to be noted that computing wf on some S] does not require iterations until a fixpoint is reached, one can only iterate on every pair (P1,a1),(P2,a2)∈S]. Indeed (P2,a1σ∪Va2) has the same shape as (P1,a1) or (P2,a2).wf transforms any well-named abstract state into a well-named, well-formed abstract state. However, we only haveγ(S])⊂γ(wf(S])) in general and not the equality. Indeed, we transform a symbolic disjunction into a disjunction∪V on the numerical domain, therefore this transformation might not be exact.

Figure 6 gives the basic operations on the sets of abstract statesM]: a join (t), a widening (O),1a meet with booleans (uB), and an inclusion test with booleans (vB,V).

Remark 3 We note that both the widening and the join operators rely on an alignment of the predicates of two monomials, however there might be different such alignments as noted in Subsection 3.1. Indeed thewf operator transforms a disjunction of the form (A∧B)∨1(C∧D) (where (A∧B) and (C∧D) represent monomials and∨1 is a symbolic disjunction) into a monomial of the form (A∨2

C)∧(B∨2D) (where∨2 is a disjunction performed in the underlying numerical domain). The choice of matching predicates one-by-one to perform the disjunction ensures that the number of predicates in the monomial does not grow too large after the operation is performed. Different heuristics can be used to choose which predicates should be paired when the operation is performed. In our analyzer we decided to remember the order of apparition of predicates in each monomial, that way we could pair together predicates that appeared at the same time during the analysis. Figure 7 illustrates this choice.

1 Ocan not be used when the two abstract states do not have the same shape, in which case the analyzer will perform a join. However, ultimately, the shape will stabilize, (see proof in Appendix B) thus allowing the analyzer to perform a widening. This widening technique is similar to the one proposed by [24] on cofibred domains.

(10)

t = t t

= =

Fig. 7: theblue predicate is the first one to have appeared in the abstract state and thered predicateis the second one.

Post]JskipK(S1])=S1] Post]JX:=EK(S]1)=S

(P,a)∈S]1{P,Post]VJX:=EK(a)}

Post]JA[X1][X2]EK(S]1)=wf(S

(P,a)∈S]1{Assign]M(P,a, A, E, X1, X2)}) Post]JIfBthenC1 elseC2K(S1])=

Post]JC1K(S]1uBB)tPost]JC2K(S1]uB(¬B)) Post]JWhileBdoC doneK(S]1)=

Merge(fp(λS] let SP] =S]tPost]JCK(S]uBB) in if S]P =F S] then S]OSP] else SP])(S]1)uB¬B) Post]JC1 ;C2K(S]1)=Post]JC2K(Post]JC1K(S]1))

Fig. 8: Abstract postconditions.

3.3 Abstract transfer functions

We now describe how program commands are interpreted using the memory ab- stract domain to yield a computable over-approximation of the set of reachable states. We assume that the numeric domain provides the necessary transfer func- tions (Post]VJCK) for all instructions involving only scalar variables. In Fig. 8, the call to fp(f)(x) computes an over-approximation of the fixpoint off reached by successive iterations off starting atx(terminating in finite time through the use of a widening).

Two functions are yet to be defined:Assign]MandMerge. Indeed, these func- tions will depend on our choice of predicates. For instance, Sect. 4.1 will introduce one version ofAssign]M, namedAssign]+,M, able to handle expressions of the form A[X1][X2]←B[X1][X2] +C[X1][X2] to analyze matrix additions, while Sect. 4.2 will discuss multiplications. In practice, the developer will enrich the functions when adding new predicates to analyze new kinds of matrix algorithms in order to design a flexible, general-purpose analyzer. Depending onEand the predicates already existing in the abstract state,Assign]M(. . . , E, . . .) will either modify vari- ables appearing in predicates, produce new predicates, or remove predicates. In the worst case, when Assign]M cannot handle some expression E, it yields the approximation>, thus ensuring the correctness of the analyzer.

The functionMergegeometrically merges the abstract state: it coalesces pred- icates describing adjacent matrix parts into a single part, as long as their join can be exactly represented in our domain (over-approximating the join would be un- sound as our predicates employ universal quantifiers). An algorithm for Merge

(11)

for the case of the addition is proposed in Sect. 4.1. As expected, the problem of extending a bi-dimensional rectangle is slightly more complex than that of extend- ing a segment, as done in traditional array analysis [6, 10]. New rewriting rules are added toMergewhen new families of predicates are introduced.

Example 3 For example, we setAssign]MJA[0][0]←B[0][0] +C[0][0]K((∅,∅)) = ({P+(A, B, C, x, y, z, t)},{x=y= 0, z=t= 1}) andMerge({P+(A, B, C, x, y, z, t), P+(A, B, C, a, b, c, d)},{x= y = 0, z = 1, t= 10, a= 1, b= 0, c= 2, d = 10}) = ({P+(A, B, C, x, y, z, t)},{x= 0, y= 0, z= 2, t= 10}).

3.3.1 Maximum number of predicates.

As widenings are used on pairs of abstract states with the same shape, we need to ensure that the shape will stabilize; therefore, we need to bound the number of possible shapes an abstract state can have. In order to do so, we only authorize a certain amount of each kind of predicates in a given abstract state. This number will be denoted as Mpred. This bound is necessary to ensure the termination of the analysis, but it can be set to an arbitrary value by the user. Note, however, thatMergewill naturally reduce the number of predicates without loss of preci- sion, whenever possible. In practice,Mpreddepends on the complexity of the loop invariant, experimentally setting Mpred to the number of nested loops is enough to successfully analyze the programs proposed in this article.

4 Abstraction instances

4.1 Matrix addition

We now consider the analysis of the assignment E= A[X1][X2]←B[X1][X2] + C[X1][X2], as part of proving that a matrix addition is correct. As suggested by the introductory example in Sect. 1, let us define the following predicate:

P+(A, B, C, x, y, z, t)=∀a, b∈[x, z−1]×[y, t−1], A[a][b] =B[a][b] +C[a][b]

We define now versions Assign]+,M and Merge+ of Assign]M and Merge to compute postconditions and possible merges over P+. Even though the an- alyzer we implemented can handle predicates on arbitrary many matrices, we will make the description of the algorithms and the proof of correctness sim- pler by only allowing our analyzer to use predicates of the form P+ such that

∃A, B, C,∀P+(A0, B0, C0, . . .)∈P, A0 =A∧B0 = B∧C0 =C (i.e., the source and destination matrices are the same for all the addition predicates used in an abstract state).

Assign]+,M. The computation of Assign]+,M(P,a, A, B, C, X1, X2) is described in Algorithm 1. It starts by looking at whether one of the predicates stored inP (the variables of which are bound bya) can be geometrically extended using the fact that the cell (X1, X2) (also bound bya) now also contains the addition ofB andC (case (a) of Figure 9). This helper function is detailed in Algorithm 6: we only have to test a linear relation among variables in the numerical domain. In this

(12)

case, the variables inaare rebound to fit the new rectangle. If no such predicate is found andPalready containsMpredpredicates thenAssign]+,Mgives back (P,a) (case (c) of Figure 9). If no such predicate is found butPcontains less thanMpred

predicates, then a new predicate is added toP(case (b) of Figure 9), stating that the square (X1, X2, X1+ 1, X2+ 1) contains the addition of B andC. Finally, the other cases in the algorithm ensure that all predicates of the abstract state are describing the same matrices. The soundness ofAssign]+,M comes from the fact that we extend a predicate only in the cell where an addition has just been performed and from the test on line 15 in Algorithm 1 that ensures that if we store the sum of some newly encountered matrices in a matrix where some predicates held, then all the former predicates are removed.

Merge+. The functionMerge+ tries to merge two predicates when they corre- spond to two adjacent rectangles, the union of which can be exactly represented as a larger rectangle (the merge conditions are also given by linear relations). A description of a functionMerge+,Mcan be found in Algorithm 2, with the help of Algorithm 4, andMerge+is the application ofMerge+,Mto every monomial in the abstract state considered. The soundness of Merge+ comes from the sound- ness of the underlying numerical domain and the tests performed byfind merge. Figure 10 gives a visual representation of the behavior ofMerge+.

Algorithm 1: Assign]+,M, computes the image by the transfer functions (associated to the expressionE=A[X1][X2]←B[X1][X2] +C[X1][X2]).

Input :(P,a),A,B,C,i,j

Output:(P0,a0) the postcondition

1 if ∃P0P,find extension((P0,a),A,B,C,i,j) =res6=Nonethen

2 x0, y0, z0, t0fresh();

3 Iswitchresdo

4 caseSome(Right)do

5 (x=x0y=y0z+ 1 =z0t=t0)

6 caseSome(Down)do

7 (x=x0y=y0z=z0t+ 1 =t0)

8 caseSome(Left)do

9 (x=x0+ 1y=y0z=z0t=t0)

10 caseSome(Up)do

11 (x=x0y=y0+ 1z=z0t=t0)

12 end

13 return((P\P0)∪ {P+(A, B, C, x0, y0, z0, t0)},auB,VI)

14 else

15 if∀P+(A0, B0, C0, , , , )P, A0=AB0=BC0=Cthen

16 if ]P< Mpredthen

17 x0, y0, z0, t0fresh();

18 I(x0=iy0=jz0=i+ 1t0=j+ 1);

19 return(P∪ {P+(A, B, C, x0, y0, z0, t0)},auB,VI)

20 else

21 return(P,a)

22 end

23 else

24 return(∅,a)

25 end

26 end

(13)

Algorithm 2: Merge+,M, merges possible predicates of a monomial.

Input :(P,a)

Output:(P0,a0) merged state

1 (Po,ao)(P,a);

2 while∃(P0, P1)Po,find merge(P0, P1,ao)6=Nonedo

3 P+(A, B, C, x0, y0, z0, t0) =P0;

4 P+(A, B, C, x1, y1, z1, t1) =P1;

5 x0, y0, z0, t0fresh();

6 I((x0=x0)(y0=y0)(z0=z1)(t0=t1));

7 (Po,ao)((Po\ {P0, P1})P+(A, B, C, x0, y0, z0, t0),aouB,VI)

8 end

9 return(Po,ao)

Algorithm 3:find extension, finds possible extensions of a monomial.

Input :(P,a),A,B,C,i,j

Output:NonekSome(dir): the direction in which the rectangle can be extended

1 P+(A0, B0, C0, x, y, z, t) =P;

2 if A=A0B=B0C=C0then

3 ifavB,V((z=i)(y=j)(t=j+ 1))then

4 returnSome(Right)

5 else ifavB,V((x=i)(z=i+ 1)(t=j))then

6 returnSome(Down)

7 else ifavB,V((x=i+ 1)(y=j)(t=j+ 1))then

8 returnSome(Left)

9 else ifavB,V((x=i)(z=j)(y=j+ 1))then

10 returnSome(Up)

11 else

12 returnNone

13 else

14 returnNone

15 end

Theorem 1 The analyzer defined by the Post] function is sound, in the sense that it over-approximates the reachable states of the program:

∀C∈ C,∀S,∀S], S⊆γ(S])⇒PostJCK(S)⊆γ(Post]JCK(S]))

The proof can be found in Appendix B. The idea of the proof is to show that the proposed functionsAssign]+,M,Merge+,Mare sound, and to underline that the termination of the analysis of thewhile loop is ensured by a convergence of the shape of the abstract states.

4.2 Matrix multiplication

Consider Program 4 that implements a matrix multiplication. It employs two kinds of assignments E1 = A[X1][X2] ← c ∈ R and E2 = A[X1][X2] ← A[X1][X2]+

B[X1][X3]×C[X3][X2], the first one being used as an initialization, and the other one to accumulate partial products. To achieve a modular design, we naturally

(14)

Algorithm 4:find merge, finds possible merges among two predicates in a monomial.

Input :P0, P1,a

Output:NonekSome(dir): the direction in which the two rectangles can be merged

1 P+(A0, B0, C0, x0, y0, z0, t0) =P0;

2 P+(A00, B00, C00, x1, y1, z1, t1) =P1;

3 if A00=A0B00=B0C00=C0then

4 ifavB,V((x0=x1)(z0=z1)(t0=y1))then

5 returnSome(Right)

6 else

7 if avB,V((y0=y1)(t0=t1)(z0=x1))then

8 returnSome(Down)

9 else

10 returnNone

11 end

12 end

13 else

14 None

15 end

C=A[i][j]B[i][j] +C[i][j] C (a)

C (b)

C (c)

Fig. 9: Illustration of the behavior of theAssign]+,M function whenMpred = 2.

yellowandbluerectangles represent predicates, theredcircle represents the value (i, j).

Merge

Fig. 10: Illustration of the behavior of theMergefunction.Yellowandbluerect- angles represent predicates.

(15)

1 /* n >= 1; */

2 i := 0;

3 w h i l e ( i < n ) do 4 j := 0;

5 w h i l e ( j < n ) do 6 A [ i ][ j ] < - 0;

7 j := j +1

8 d o n e; 9 i := i +1 10 d o n e;

11 i := 0;

12 w h i l e ( i < n ) do 13 j := 0;

14 w h i l e ( j < n ) do

15 k := 0;

16 w h i l e ( k < n ) do 17 A [ i ][ j ] < - A [ i ][ j ]+

18 B [ i ][ k ]* C [ k ][ j ];

19 k := k + 1;

20 d o n e

21 j := j + 1;

22 d o n e

23 i := i + 1;

24 d o n e

Program 4: Multiplication with inner loop on k.

associate to each kind of assignments a kind of predicates, and show how these predicates interact. More precisely, in our case, we consider the following two predicates:

Ps(A, x, y, z, t, c)=∀i, j∈[x, z−1]×[y, t−1], A[i][j] =c P×(A, B, C, x, y, z, t, u, v)=

∀i, j ∈[x, z−1]×[y, t−1], A[i][j] =

v−1

X

k=u

B[i][k]C[k][j]

which state respectively that the matrixAhas been initialized to con some rect- angle, and that the matrixAreceived a partial product ofB andC.

Predicates can interact together in two ways. Firstly, in a non productive way, for example an addition is performed on a matrix A and then the matrix A is reset to 0. In order to ensure soundness, we need to remove the predicate stating thatAreceived an addition. Secondly, in a productive way, meaning that a matrix A is initialized to 0 as a prerequisite to receiving the product of two matrices, by summation over kof the partial productsB[i][k]C[k][j]. How to handle these different cases is explained in details in the following.

4.2.1 Removing predicates

The analysis suggested for the addition in terms of postconditions can be ex- tended to the initialization predicate the same way. Indeed, we add aAssign]s,M abstract operator that enlarges the predicates Ps(A, x, y, z, t, c) and a function Merges,M, that merges them when possible (it is done the same way as for the addition predicate). However, for the analyzer to be sound, we need to modify the Assign]×,MandAssign]s,Mfunctions to check whether the matrix that was mod- ified (A) by the evaluated command (resp A[X1][X2] ←B[X1][X2] +C[X1][X2] and A[X1][X2] ← c) appears in some other predicate P. If it is the case, then P is removed from the state, thus loosing information but ensuring soundness.

Assign]×,M and Assign]s,M can both be found in Appendix A, alongside some helper functions.

4.2.2 Splitting predicates

In Program 4, matrixAis initialized to 0 before the main loop. This is necessary if we want to compute the product ofBandC intoA. Therefore, we can only assert

Références

Documents relatifs

L’utilisation d’un matériel en titane (IRM compatible) simplifie le suivi postopératoire des patients. D’autres matériels utilisant des tiges sont

The class of liveness properties, on the other hand, is informally char- acterized as the class of properties stating that “something good eventually happens”, that is, a

Dans cette thèse, nous proposons un langage de formules permettant de spéci- fier des propriétés de traces de systèmes de transition probabilistes et non-déter- ministes,

Moreover, by means of piecewise-defined ranking function abstract domains [21,22,23], we design new static analysis methods to effectively infer sufficient preconditions for

© 2012 Jonathan Monk &amp; onestar press onestar press 49, rue Albert 75013 Paris France info@onestarpress.com www.onestarpress.com /250 jonathan monk. From the year I was born

sens très large, pour débusquer les men s onges e t les propagandes , il y a violence quand, direct e ment ou indirectement, d'un coup ou progressivement, un ou des-acteurs

After recalling the classic construction of static analyzers for sequential programs by abstraction of the concrete trace semantics, we introduce abstractions to derive thread-

The methods proposed in [4,19] consist instead in analyzing each thread independently as a sequential program, extract- ing from the analysis (an abstraction of) the set of values