Explainer 7 of 15

Sweeps: a fixpoint with a budget that says when it ran out

Read first: Visiting a graph, Fixpoints and worklists

A whole-corpus fixpoint is as slow as its slowest program: on a corpus of about 3,800 programs, an earlier version of the walk spent 1,404 s, 2,324 s and 4,434 s on the last three programs. A worklist cannot say how far along a program is, and capping it is only safe if a capped program says so instead of quietly returning less.

PROGA's loop writes WS-NEXT; a LINK after the loop reads it. A budget of 10 passes lets the walk go round the program at most 10 times; it finds both targets and settles on pass 2. Stopped after one pass, the LINK gets only PROGB, marked incomplete-walk.

The walk is the worklist from fixpoints and worklists: a queue of nodes whose entry state changed, taken in reverse postorder. A pass (cobble's flag and log lines call it a sweep) is one trip of the walk's place in that order, its cursor, from the start of the order to the end.

The example

Cobble's control-flow graph for PROGA has 13 reachable nodes, numbered by their place in reverse postorder. The dashed, highlighted edge is the one back edge: an edge to a node earlier in the order.

flowchart TD
    L["MAIN-PARA#35;label · 1"] --> T["MAIN-PARA/0#35;loop#35;test · 2"]
    T -->|FALSE| M0["MAIN-PARA/0 PERFORM · 9"]
    T -->|TRUE| M1["MAIN-PARA/1 LINK · 3"]
    M0 -->|PERFORM| PL["PICK-PARA#35;label · 10"]
    PL --> P0["PICK-PARA/0 MOVE 'PROGC' · 11"]
    P0 --> P1["PICK-PARA/1 ADD · 12"]
    P1 --> PE["PICK-PARA#35;end · 13"]
    PE -.->|"RETURN · back edge"| T
    M1 --> M1r["MAIN-PARA/1#35;raise · 4"]
    M1r -->|TRUE| M1d["MAIN-PARA/1#35;dispatch · 8"]
    M1r -->|FALSE| M2["MAIN-PARA/2 RETURN · 5"]
    M2 --> M2r["MAIN-PARA/2#35;raise · 6"]
    M2r -->|TRUE| M2d["MAIN-PARA/2#35;dispatch · 7"]
    M0 ==>|resume| T
    linkStyle 7 stroke:#f5b041,stroke-width:3px

The answer: the LINK's targets under each budget of passes, as cobble reports them. Two passes or more settle it, and the mark comes off.

--sweeps 1: PROGB · incomplete-walk--sweeps 2 or more: PROGB, PROGC · settled

The lines that matter:

05  WS-NEXT  PIC X(8) VALUE 'PROGB'.
05  WS-I     PIC 9(2) VALUE 0.
PERFORM PICK-PARA UNTIL WS-I > 3.
EXEC CICS LINK PROGRAM(WS-NEXT) END-EXEC.
PICK-PARA.
    MOVE 'PROGC' TO WS-NEXT.
    ADD 1 TO WS-I.
Full listing (PROGA)
       IDENTIFICATION DIVISION.
       PROGRAM-ID. PROGA.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  WS-REC.
           05  WS-NEXT          PIC X(8) VALUE 'PROGB'.
           05  WS-I             PIC 9(2) VALUE 0.
       PROCEDURE DIVISION.
       MAIN-PARA.
           PERFORM PICK-PARA UNTIL WS-I > 3.
           EXEC CICS LINK PROGRAM(WS-NEXT) END-EXEC.
           EXEC CICS RETURN END-EXEC.
       PICK-PARA.
           MOVE 'PROGC' TO WS-NEXT.
           ADD 1 TO WS-I.

Besides one node per statement, the graph has a label at the start of each paragraph, a separate node for the loop's test, and for each EXEC CICS a #raise node (did the command raise a handled condition?) and a #dispatch node (where a handler would take it). The test's FALSE arm goes to the PERFORM (MAIN-PARA/0); its TRUE arm goes to the LINK (MAIN-PARA/1). The graph also has a PERFORM edge into PICK-PARA and a RETURN edge out of its end, but the walk follows neither. It walks PICK-PARA on its own, a range walk, from the state the PERFORM is visited with, and hands what leaves PICK-PARA#end to the PERFORM's resume, the node control comes back to: here the test (the thick edge). A RETURN edge leads to every caller of the paragraph at once; the resume belongs to one call.

The reverse postorder cobble computes for it — the numbers in the graph — from depth-first search over all of these edges, taking the test's FALSE arm first:

MAIN-PARA#labelMAIN-PARA/0#loop#testMAIN-PARA/1MAIN-PARA/1#raiseMAIN-PARA/2MAIN-PARA/2#raiseMAIN-PARA/2#dispatchMAIN-PARA/1#dispatchMAIN-PARA/0PICK-PARA#labelPICK-PARA/0PICK-PARA/1PICK-PARA#end

In the chips, the code after the loop (chips 3 to 8) comes ahead of the PERFORM (chip 9). The loop on the traversal page put the body first, because its search took the body's arm first. Either way the test, chip 2, comes before both, and the PERFORM resumes at it, behind the cursor: that is what decides how many passes the loop needs. Only the nine MAIN-PARA nodes are ever on the program's own queue; chips 10 to 13 are visited inside the range walks.

The tables in the walk below show, for each node in the graph, the values WS-NEXT can hold on entry. PROGB is the VALUE clause; PROGC is written by PICK-PARA/0, node 11. WS-I is tracked too, and "settled" needs its states to stop changing as well. ADD 1 TO WS-I (node 12) is not a literal, so it is not evaluated: the write gives WS-I no values, only the reason not-a-literal (see the reasons taxonomy). Joined with the VALUE at the test, that is {00} plus not-a-literal after the first trip round the loop, and it does not grow after that; the walk does not count the loop to 4.

Step by step

The walk on PROGA, with a budget of 10 passes (2 passes)
  1. Only the entry node has a state: the program's starting image, in which WS-NEXT holds its VALUE. The queue holds only the entry. The order above is fixed before the walk starts.

    Entry state of MAIN-PARA#label

    fieldcan hold
    WS-NEXT{PROGB}
  2. Pass 1, dequeues 1 to 13. The test, taken second, pushes {PROGB} to both arms, so the LINK (MAIN-PARA/1) gets {PROGB}. The cursor runs on through the LINK's chain and takes the PERFORM ninth. Its range walk takes PICK-PARA's four nodes, from the PERFORM's {PROGB}: PICK-PARA/0 moves 'PROGC' in, and the {PROGC} leaving PICK-PARA#end goes to the resume, the test, whose entry state grows to {PROGB, PROGC}. The test is behind the cursor, so taking it again starts a new pass. The program's own walk took 9 nodes and the range walk 4.

    Entry states after pass 1: what WS-NEXT can hold (the PICK-PARA rows are the range walk's)

    nodestatementWS-NEXT
    MAIN-PARA#labelparagraph start{PROGB}
    MAIN-PARA/0#loop#testUNTIL WS-I > 3{PROGB, PROGC}
    MAIN-PARA/1LINK PROGRAM(WS-NEXT){PROGB}
    MAIN-PARA/1#raiseLINK raised?{PROGB}
    MAIN-PARA/2RETURN{PROGB}
    MAIN-PARA/2#raiseRETURN raised?{PROGB}
    MAIN-PARA/2#dispatchhandler{PROGB}
    MAIN-PARA/1#dispatchhandler{PROGB}
    MAIN-PARA/0PERFORM PICK-PARA{PROGB}
    PICK-PARA#labelparagraph start{PROGB}
    PICK-PARA/0MOVE 'PROGC' TO WS-NEXT{PROGB}
    PICK-PARA/1ADD 1 TO WS-I{PROGC}
    PICK-PARA#endend of the range{PROGC}

    If the budget were 1, the walk would stop here, with the test still queued. The LINK's target row

    candidates: PROGBreasons: incomplete-walk
  3. Pass 2, dequeues 14 to 24. The test is taken again with {PROGB, PROGC} and pushes it to both arms. The LINK sees PROGC for the first time, and so does everything after the test. The PERFORM, taken last, walks PICK-PARA again from its larger state, picking up from the first walk's states: PICK-PARA#label, PICK-PARA/0 and PICK-PARA/1 grow, PICK-PARA/1's entry also gaining the body's not-a-literal write to WS-I, and PICK-PARA#end's does not, so the state handed back to the test is the one it already has. Nothing is queued any more. The walk stops with 8 passes of the budget unused: the member is settled, and the doubt comes off.

    Entry states after pass 2 (amber: grew in this pass)

    nodestatementWS-NEXT
    MAIN-PARA#labelparagraph start{PROGB}
    MAIN-PARA/0#loop#testUNTIL WS-I > 3{PROGB, PROGC}
    MAIN-PARA/1LINK PROGRAM(WS-NEXT){PROGB, PROGC}
    MAIN-PARA/1#raiseLINK raised?{PROGB, PROGC}
    MAIN-PARA/2RETURN{PROGB, PROGC}
    MAIN-PARA/2#raiseRETURN raised?{PROGB, PROGC}
    MAIN-PARA/2#dispatchhandler{PROGB, PROGC}
    MAIN-PARA/1#dispatchhandler{PROGB, PROGC}
    MAIN-PARA/0PERFORM PICK-PARA{PROGB, PROGC}
    PICK-PARA#labelparagraph start{PROGB, PROGC}
    PICK-PARA/0MOVE 'PROGC' TO WS-NEXT{PROGB, PROGC}
    PICK-PARA/1ADD 1 TO WS-I{PROGC}
    PICK-PARA#endend of the range{PROGC}

    The LINK's target row

    candidates: PROGB, PROGCreasons: none

The same program under each budget, as cobble reports it. "Dequeues" counts every node the walk took, the range walks' included, counted by stepping cobble's walk one dequeue at a time.

walkpasses runsettleddequeuesLINK candidatesreasons
--sweeps 11no13PROGBincomplete-walk
--sweeps 22yes24PROGB, PROGCnone
--sweeps 32yes24PROGB, PROGCnone
--sweeps 102yes24PROGB, PROGCnone
no budget of passes—yes24PROGB, PROGCnone

A settled row is the unbounded walk: the same 24 dequeues and the same answer. The budget changes only where the walk may stop, and how far along it is becomes a fraction of a known whole — the "sweep 2 of 10" in the log lines under why a pass budget can stop early, rather than the queue lengths in the table under why not the worklist's budget.

Why a pass budget can stop early

One pass moves the cursor once along reverse postorder, taking each queued node it comes to. At each node it applies the statement to the node's entry state and pushes the result into each successor's entry state, joining it with whatever is there; a successor whose state grew is queued. A node that is not queued is skipped: its entry state has not changed, so visiting it would change nothing. When nothing queued lies ahead of the cursor, the pass ends and the cursor wraps to the earliest queued node.

A loop needs a second pass. Passes 1 and 2 of the walk, cut down to the nodes of the loop that matter, in the order the program's walk takes them:

dequeue in the passnodepass 1pass 2
1st (2nd in pass 1)the testpushes {PROGB}pushes {PROGB, PROGC}
nextthe LINK, after the loopsees {PROGB}sees {PROGB, PROGC}
lastthe PERFORM: its range walk's MOVE 'PROGC'writes PROGCwrites PROGC
lastthe PERFORM: its resumehands {PROGC} back to the testhands back the same

The test comes before the body in reverse postorder, because the test is how the body is reached. In pass 1 the LINK in row 2 sees only what was true before the loop ran: the write in row 3 reaches the test only at row 4, through the PERFORM's resume, after the test has had its turn. In pass 2 the test starts with the body's write in its state and hands it on. A value has to go back round the loop once before it reaches the code after the loop. That is why two passes is the useful floor.

A forward chain is carried in one pass, however long. Reverse postorder puts every node after all the nodes that lead into it, except along back edges. Take PROGX, a chain of copies through two PERFORMs (its four fields are PIC X(4)):

       MAIN-PARA.
           MOVE 'X' TO WS-A.
           PERFORM P1.
           MOVE WS-C TO WS-D.
           GOBACK.
       P1.
           MOVE WS-A TO WS-B.
           PERFORM P2.
       P2.
           MOVE WS-B TO WS-C.

Cobble's reverse postorder for it, 12 nodes

MAIN-PARA#labelMAIN-PARA/0MAIN-PARA/1P1#labelP1/0P1/1P2#labelP2/0P2#endP1#endMAIN-PARA/2MAIN-PARA/3

Each read and the definer the first pass found for it

read atreadsdefiner
P1/0 (5th)WS-AMAIN-PARA/0 (2nd)
P2/0 (8th)WS-BP1/0 (5th)
MAIN-PARA/2 (11th)WS-CP2/0 (8th)

Every definer sits earlier in the chips row than its read, and the walk takes them in that order: P1 is walked when MAIN-PARA/1 is taken, P2 inside that walk when P1/1 is, and MAIN-PARA/2 after both have returned. So each write has been pushed forward by the time its read is visited. This graph has no back edge, and the queue empties in the first pass — its log line is PROGX sweep 1 of 10 done, 5 nodes reached, settled. The 5 nodes are the MAIN-PARA nodes on the program's own queue; P1's and P2's are visited inside range walks and not counted there.

An empty queue proves the fixpoint. When nothing is queued, every node's entry state already holds what its predecessors last sent, so another visit anywhere would change nothing. That is the end of pass 2 of the walk. The member is settled: the walk ran to the end, and its answer is exactly the unbounded walk's, because it is the same walk. If the budget runs out with nodes still queued, it is unsettled. Cobble logs which one each pass was:

PROGA sweep 1 of 10 done, 9 nodes reached, still growing
PROGA sweep 2 of 10 done, 9 nodes reached, settled

No pass is spent only on proof: the pass that carries PROGC to the LINK is the one whose queue empties. The 9 nodes are the MAIN-PARA nodes, as in the chain.

Unsettled means missing, never wrong. Every transfer and the join are monotone (see the rule), so a state stopped early is a subset of the fixpoint's. Pass 1's table against the settled one:

WS-NEXT on entry toafter 1 passfixpoint
MAIN-PARA/1 (LINK){PROGB}{PROGB, PROGC}
PICK-PARA/0{PROGB}{PROGB, PROGC}
PICK-PARA#end{PROGC}{PROGC}

Every value in the middle column is also in the right-hand one, so every flow read off an early state is real; what may be absent is a value that needed more passes, like PROGC in row 1. Cobble says so: every dynamic call target and every jump in an unsettled member carries the reason incomplete-walk, as in pass 1's target row (see the reasons taxonomy for the full list of reason codes), and the run's summary counts and names the member as unsettled. A walk in the combine that passes through that member's jump facts carries incomplete-walk on into the targets it feeds in other programs.

Why not the worklist's budget

Without --sweeps, the walk's budget is --iteration-factor: that many dequeues per CFG node (200 by default), counting the range walks' dequeues, after which it publishes what it has, unsettled. The example's graph has 14 nodes (MAIN-PARA#end unreachable): a cap of 200 × 14 = 2,800 dequeues; it finishes in 24. On the large corpus the worklist finished every program, but the run spent over an hour waiting on one.

A worklist is also hard to watch. Its queue on the example, after each of the program's own 17 dequeues, as cobble runs it; the range walks run inside dequeues 9 and 17 and take 4 and 3 more:

dequeuesnodes takenqueue length after each
1–9all 9 MAIN-PARA nodes, MAIN-PARA#label to MAIN-PARA/01 2 2 3 3 3 2 1 1
10–178 again, MAIN-PARA/0#loop#test to MAIN-PARA/02 2 3 3 3 2 1 0

Nothing in it counts down. After dequeue 9 the queue holds one node, exactly as after dequeue 8, and yet 8 more follow; after dequeue 16 it holds one again, and only one follows. A progress line can only estimate from how fast the queue drains. A budget of passes is known before the walk starts, still gives the exact answer when it has time to, and marks its results, like the LINK's row in pass 1, when it does not:

walkcap, known before it startsused
--iteration-factor 200200 × 14 = 2,800 dequeues24
--sweeps 1010 passes2 passes (24 dequeues)

Both caps are known up front, but only the second says how far along the walk is: after pass 1 it is one pass into at most 10, and its log line says sweep 1 of 10.

Where this shows up

When the goal is one answer — which programs each CALL WS-PGM or EXEC CICS XCTL/LINK PROGRAM(WS-...) can reach — a few tangled programs should not hold the rest back, nor quietly return less than they found.

--sweeps 1:
{"caller": "PROGA", "node": "MAIN-PARA/1", "kind": "program",
 "transfer": "CICS_LINK", "expression": "WS-NEXT",
 "candidates": ["PROGB"],
 "reasons": ["incomplete-walk"], "dropped": []}
--sweeps 2:
{"caller": "PROGA", "node": "MAIN-PARA/1", "kind": "program",
 "transfer": "CICS_LINK", "expression": "WS-NEXT",
 "candidates": ["PROGB", "PROGC"],
 "reasons": [], "dropped": []}

The first row's "reasons": ["incomplete-walk"] is what tells a reviewer that "candidates": ["PROGB"] may be short, and the second row adds PROGC. Any whole-program analysis run under a time limit makes the same trade: let every program finish, or cap the work and mark what the cap touched. The marking is what makes the cap safe to use.

The rule

in(n) is node n's entry state; transfer_n applies n's statement to a state; ⊔ is the join, which keeps every value either side holds; N is the --sweeps budget.

order = reverse postorder of the nodes the entry reaches; queue = {entry}
one step: take the first queued n at or after the cursor, else wrap to the earliest;
          after = transfer_n(in(n)); for each successor s: in(s) = in(s) ⊔ after, queue s if it grew
a PERFORM: walk its range from after, by this same rule and budget;
          in(resume) = in(resume) ⊔ what leaves the range's end; RETURN edges are never followed
a pass ends at each wrap; stop when the queue is empty, or when pass N + 1 would start
settled = this walk's queue and every range walk's queue emptied
unsettled ⇒ in(n) ⊆ fixpoint in(n) for every n; every target and jump gets incomplete-walk

Each line is a step of the walk above. order is the 13-node chips row. The PERFORM line is dequeue 9, whose range walk hands {PROGC} back to the test and grows its {PROGB}. The wrap is the start of pass 2, and the empty queue is its end. The unsettled ⇒ line is the budget-1 chips with incomplete-walk.

A value written in a loop body reaches a reader after the loop on pass 2, as the LINK's row in the walk does. Each further step it has to take backward against the order — round the loop again, or to a copy earlier in the same body, as in the second item below — costs one more pass. Every program on this page, as cobble ran it:

programLINK complete fromsettled from
the chain (no back edge)—--sweeps 1
the example--sweeps 2--sweeps 2
the copy-the-other-way variant--sweeps 3--sweeps 3
the PERFORM-twice program--sweeps 1--sweeps 2

The two columns agree in the loops. The first can run ahead of the second, as in row 4, which the third item below explains.

What goes wrong

In cobble

The fixpoint (cobble-dataflow-fixpoint) runs the worklist capped at --iteration-factor × nodes dequeues by default (factor 200) — the 2,800 for the example under why not the worklist's budget. --sweeps N bounds the same walk by N passes instead, in every PERFORM range walk as well as the program's own. The two flags cannot be combined, and --sweeps 0 is rejected:

cobble-dataflow-fixpoint: error: argument --iteration-factor: not allowed with argument --sweeps
cobble-dataflow-fixpoint: error: argument --sweeps: needs at least one sweep, got 0

A member with no back edge empties its queue in one pass, as the chain program's log line in why a pass budget can stop early shows. For each member the log says settled in N sweeps or NOT SETTLED after N sweeps; the closing warning names every unsettled member, and the summary counts them under unsettled. For the example with --sweeps 1:

PROGA 2/2 writes covered, 1 unhandled, 0 unmodelled, 3 edges, 2 of 2 extents tracked, 0.0s, NOT SETTLED after 1 sweeps -- edges are a subset
...
!! 1 members still growing when their walk's budget ran out (PROGA); their edges are a subset of the flows those programs have
{"analysed": 1, ..., "unsettled": 1, ...}

A walk that stops before settling adds the incomplete-walk reason to the targets and jumps it found, as in the targets.jsonl rows under where this shows up. The flags are documented in the field dataflow reference. Next: answering every walk once.