Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Validity Conditions

We associate borrow PCG edges with validity conditions to identify the control-flow conditions under which they are valid. Consider the following program:

fn main(){
    // BB0
    let mut x = 1;
    let mut y = 2;
    let mut z = 3;
    let mut r = &mut x;
    if rand() {
        // BB1
        r = &mut y;
    }
    // BB2
    if rand2() {
        // BB3
        r = &mut z;
    }
    // BB4
    *r = 5;
}

We represent control flow using a primitive called a branch choice of the form . A branch choice represents the flow of control from one basic block to another. In the above code there are two possible branch choices from bb0: and and one branch choice from bb1 (to bb2).

For any given basic block in the program, an execution path leading to is an ordered list of blocks ending in . For example, one path of execution to bb4 can be described by: .

Validity conditions conceptually define a predicate on execution paths. For example, the validity conditions describing the control flow where r borrows from z at bb3 require that the path contain .

We represent validity conditions as a partial function mapping relevant blocks to sets of allowed target blocks.

An execution path is valid for validity conditions iff, for each , either:

  • is not a relevant block in , or
  • is in the set of allowed target blocks of in

Formal Definition

The representation of validity conditions in our implementations corresponds closely to the following description:

Validity conditions is a map describing control-flow conditions. For each block , is a subset of the real successors of .

The join of validity conditions and is defined as:

Validity conditions are valid for a path iff:

Correctness Theorem (Sketch)

For every borrow edge in the PCG at location and execution path to , the corresponding borrow is live at iff satisfies the validity conditions of .

Proof

We prove this for each location in the MIR by induction on the basic blocks of the graph via a reverse-postorder traversal. The inductive hypothesis is that the property holds for each ancestor block.

Having proved for the 1st location in a basic block, it is trivial to show for the remaining locations in the block, therefore we only concern ourselves with the basic blocks.

The case of bb0 is trivial.

We now concern ourselves with the validity conditions of an arbitrary edge at block , assuming via IH that it holds for all where . Let be the set of incoming nodes. We define our as the repeated join: .

We first show that the if direction holds: For every borrow edge in the PCG at location and execution path to , if the corresponding borrow is live at , then satisfies the validity conditions of .

Every has a prefix and ends with , by our IH we have satisfies . We need to then show that satisfies . Our proof is by contradiction.

Suppose did not satisfy ; then there must be a pair where . We know that the pair cannot be the final pair in (i.e. ) by the definition of join we have . Therefore the edge must exist in some strict prefix of . Then, there must be a nonempty set where (otherwise, the joined would be either or contain ). But then, because must contain , it would not satisfy the validity conditions of for any .

We now show the reverse: For every borrow live at for an execution path , its corresponding edge satisfies the validity conditions of .

Our proof relies on the monotonicity of joins: if then for any .

The borrow is created at a unique block . Then is definitely in at some index and the borrow is not killed in any block for all .

Our proof works by building longer prefixes of . The base case is when the borrow is created (and therefore becomes live) at , where the property holds trivially. Then, when going from to , we add to the validity conditions and join other incoming blocks. Because the prefix satisfies the prior conditions by induction, satisfies the additional condition, and monotonicity of join, the longer prefix also satisfies conditions. QED.