Repository navigation
Description
@thomasspriggs @martin-cs Here is a summary of the properties of loop normal form for goto programs:
Viewing a GOTO program as both the sequence of its instructions as the control flow graph induced by the sequence structure and its GOTO statements: each instruction is a node, there's an edge n1->n2 iff n2 is the successor of n1 in the sequence, or if n1 is a GOTO instruction with n2 as jump target. The entry point of the graph is the node of the first instruction.
A loop in the CFG is a set of strongly connected nodes. The loop is natural iff there is a node in the loop, the header, that dominates the other nodes of the loop. An edge going from an instruction of the loop to the header node is called a back-edge. A node that has a back edge is called a latch node.
A node is an exiting node if it has at least one successor that is not in the loop. That successor outside of the loop is called an exit node of the loop.
The properties of a normalised natural loop are:
- The header node is the only node with predecessors that are not in the loop
- The header node has a unique predecessor called the pre-header node
- The predecessors of exit nodes are all in the loop (i.e. control can only reach an exit node without actually going through the loop instructions first)
- The loop has a single latch node
We say that a natural loop is densely packed in the goto program iff the sub-sequence of instructions starting at the loop header instruction and ending at the loop latch instruction only contains instructions of the loop and iff the preheader node is right before the header node in the sequence.
A goto-program is loop normal form iff all the loops it contains are natural, densely packed and if the only edges that jump to an instruction with a lower index in the sequence are back-edges of natural loops.
These notions are captured in two functions:
- a checker function that checks if the loop normal form properties holds on a goto program, and returns either success or an error value describing why it does not hold, e.g. "exit node 'n' of loop 'l' has a predecessor that is not in 'l'", "there is a backwards jump in the sequence that is not a natural loop back-edge")
- a normalisation function that rewrites a goto program's instruction sequence to loop normal form, and returns either success if the operation succeeds, or an error value describing the reason for failure.
- the normalisation function should be idempotent, i.e. should not modify a program that is already in loop normal form (it can use the checker function to decide wether there is work to do or not).
Activity
thomasspriggs commentedon Feb 6, 2023
The way the pre-header node was explained in the recent meeting appeared to require the use of a labelled skip instruction. It is worth noting that there is an existing normalisation pass in the cbmc code base which is called from tens of places called
remove_skip. As the name implies it will remove skip instructions from goto programs. It can move labels around in order to remove skip instructions. Therefore it could potentially combine the pre-header node from this initial suggested form with the header node. Therefore think that we should not make the notion of a mandatory pre-header node part of our definition.peterschrammel commentedon Feb 6, 2023
Are header nodes of nested loops allowed to coincide?
feliperodri commentedon Feb 6, 2023
#7506
#7517
remi-delmas-3000 commentedon Feb 6, 2023
Agreed, we can always add these nodes as needed afterwards.
I think the same observation could apply to exit nodes. We would like exit nodes to have only incoming edges coming from loop instructions so that the header of the loop dominates the exit nodes and that any instrumentation we may add on exit nodes is only executed when the loop has been entered once.
This may require the insertion of a skip node to separate incoming edges from the loop from incoming edges coming from somewhere else.
I am not sure however that reintroduce these skip nodes as needed after the fact is as easy as in the pre-header node case.
remi-delmas-3000 commentedon Feb 6, 2023
I'd like to rephrase the definition a bit to make it clearer that a natural loop is specific to a back edge:
A back-edge is an edge from a node
lto another nodehof the loop such thathdominateslfrom the entry node of the CFG. The dominance relationh dom limplies the existence of a path fromhtol, the back edge that of a connected component in the CFG that containslandh. The natural loop of the back-edgel->his the smallest subset of nodes the CFG that contains bothlandhand that is strongly connected.So by that definition it seems like two distinct natural loops (i.e that have different back edges and are each a minimum size SCC) can have the same header node.
It seems like the pre-header node requirement would reject sharing though.
It would be nice to have a checker function that can optionally check for the presence of pre-header and dominated exit nodes (since having these properties will really make loop contracts instrumentation easier).
thomasspriggs commentedon Feb 9, 2023
Is it worth considering some sort of requirement for basic blocks to be maximal as part of this definition? For example if we have 2 single entry single exit blocks of instructions A and B where block A ends with a GOTO which jumps to block B then the two blocks should be combined and the GOTO instruction removed, in order to reach normal form. I am thinking about resolving issues such as the one seen here - #7506
Cases like these, where code superficially looks like a loop due to the backwards GOTO, but no loop of any kind is actually present.
I'd also like to ask how closely does the existing code for detecting natural loops in
src/analyses/natural_loops.hfit with the natural loop definition, which we are aiming for in this issue?martin-cs commentedon Feb 11, 2023
Thanks @remi-delmas-3000, this is a good foundation.
I don't think any of this is wrong but here are a load of pedantic questions with the aim of tightening things up:
Good point. I was implicitly assuming loop normal form properties as an addition to the core GOTO fragment documented here : #7505
Could you provide an example ? The only case I can think of is that of
__CPROVER_preconditionsthat are pulled out of functions at the call site.Not necessarily, as I am not really certain what
src/analyses/natural_loops.hcomputes. A first step could be to give a spec tonatural_loops.h.We want this node to be unique.
Yes this depends on choosing the header. For a CFG that was successfully put in loop-normal-form, we would like to have natural loop back-edges be the only instruction-sequence back-edges.
Doesn't having (by definition) the header node dominate all nodes of the loop rule out multiple entrance nodes ?
Having a single entry (the header) and a pre-header node is important for loop contract transformation.
if (thing) {
while (other_thing) {
stuff();
}
}
I that case we would need to distinguish the exit node of the loop from the jump target of
if(thing) { .. }.I note that there are a couple of differences with this definition and LLVM's one. Your header is not required to be unique, or an entry node. Also I note that there is no maximality condition so:
A -> B
A -> C
B -> D
C -> D
D -> A
@martin-cs have you read this addendum (there's a minimality condition which I added to account for nested loops (if two natural loops are nested the minimality of each SCC should make nested loops emerge naturally).
thomasspriggs commentedon Feb 13, 2023
Yes. For now what we are asking for loop contracts is a function that will tell us if a program is in loop normal form or not, and a normalisation function that would succeed when goto programs from structured C programs programs that use if-then-else, while-loops, for-loops and do-while-loops with break and continue statements.
Yes. That's why this normalisation pass would be opt-in and invoked when needed for loop contracts, and optionally on programs coming from kani.
Yes that would be really good. So maybe the loop normal form checking function, instead of failing on awkward loops, could return a collection of loop descriptors and the "normal form/awkward" flag is just part of that loop descriptor. I dont' think tagging the actual loop instructions is a good idea, since normal form properties can be invalidated by program instrumentation/transformation passes.
Example input C program
Example labelled loop GOTO program
The latch node in that example seems to be instruction 7
GOTO 1. Instruction 5IF ¬(factorial::1::1::i ≤ factorial::number) THEN GOTO 2is an exiting node with corresponding exit node instruction 8. Instruction 7 is not an exiting node since its only successor is the header node. Multiple exit nodes start appearing when shortcutting conjunction or disjunction is used in loop guards or when break or continue statements are used in the loop body.remi-delmas-3000 commentedon Feb 13, 2023
@thomasspriggs
martin-cs commentedon Feb 13, 2023
martin-cs commentedon Feb 13, 2023
remi-delmas-3000 commentedon Feb 15, 2023
@thomasspriggs do you think the requirements sufficiently well defined now ?
thomasspriggs commentedon Feb 16, 2023
My team has re-prioritised some of the work. I now have other documentation to write first. So I am not actively working on this issue for the moment.
feliperodri commentedon Mar 9, 2023
This is not a blocker for our GOTO standardization anymore, so we can remove the high severity flag. We have also worked around corner cases in the GOTO instrumentation for loop contracts.