Techniqueadvanced
Burch-Dill flushing
Also called pipeline flushing abstraction, Burch-Dill commutative diagram.
Burch and Dill's check of a pipelined processor against its ISA that flushes the pipeline to get an abstraction function and proves a commuting diagram in EUF logic.
Burch-Dill flushing proves that a pipelined processor behaves like its instruction set architecture (ISA), which runs one instruction at a time. The hard part of such a proof is to say which ISA state an implementation state stands for, when several instructions are partly done. Burch and Dill (1994) take the answer from the implementation itself. Feed it stall cycles that fetch nothing until the pipeline is empty, then read the register file and the other programmer-visible state. This flushing gives an abstraction function from implementation states to ISA states. The check is a commuting diagram: one implementation step followed by must give the same result as followed by the ISA step of the instruction that was fetched, or by no step if none was. Burch and Dill write the two sides as terms in which the data values, the ALU and the memories are uninterpreted, and they prove the equation in the logic of equality with uninterpreted functions. The check covers control logic such as bypasses, stalls and squashes, and it does not depend on the width of the data or on what the ALU computes.
In plain words
A pipeline works on several instructions at once. One is fetched, one reads its registers, one computes, one writes back. To compare it with the simple one-at-a-time machine of the manual, you need to say what "the current state" of the pipeline means.
Flushing gives the answer. Stop fetching and let the pipeline run until every started instruction has finished. The register file then holds the state that the simple machine would have.
Now compare two routes from any pipeline state. Route one: run the pipeline for one cycle, then flush. Route two: flush, then run the simple machine on the instruction the pipeline took in that cycle. If the two routes always end in the same state, the pipeline is correct. The values in the registers stay symbols, so the proof covers every value at once.
When it applies
- There are two deterministic machines. The implementation has a state and a step per clock cycle, where an input offers an instruction or is a stall. The specification has a state and a step that runs one instruction .
- In every cycle the implementation either takes the offered instruction or does not, and says which. A stall input never fetches.
- The pipeline drains: from every reachable state, stall cycles complete all instructions in flight. Let , and let keep the programmer-visible part. Then .
- Each initial state has an empty pipeline, and is the matching initial ISA state .
- For the check in EUF, the design must be correct for every interpretation of the uninterpreted parts. A design that relies on an algebraic property of the ALU, such as commutativity of addition, needs that property as an extra axiom.
The criterion of correctness is the flushed one. For every run from an initial state, flushing at the end gives the ISA state after the fetched instructions, in the order fetched.
The template
BURCH-DILL-CHECK(δ_I, δ_S, FLUSH, proj, Q, inputs)- 1
for each implementation state q in Q do - 2
s ← proj(FLUSH(q)) - 3
for each input i do q′ ← δ_I(q, i) - 4
left ← proj(FLUSH(q′)) - 5
right ← δ_S(s, instr(i)) if fetched(q, i), else s - 6
if left ≠ right then return the counterexample (q, i) - 7
return the diagram commutes on Q
Burch and Dill run lines L1 to L6 once, symbolically. is all states, each latch holds a fresh symbol, and the control values are Boolean variables. Lines L2 to L5 are symbolic simulation of implementation cycles and one ISA step on terms. Line L6 asks whether the formula is valid in EUF. The holes of the template are the stall input and the depth of the flush, the function , and the decision procedure for line L6.
The commuting diagram is exact on reachable states. Let be the set of implementation states reachable from initial states. For a run , let be the sequence of fetched instructions, and let be the ISA state after running . The theorem: the diagram commutes at every and every input if and only if for every run.
Proof. If the diagram commutes on , use induction on . For , by assumption. If , then and the diagram give when the instruction is fetched, and when it is not. In both cases this is of and the fetched sequence . Conversely, let every run satisfy the equation. Take , reached by a run with fetched sequence , and an input . The run extended by ends in , so with if it is fetched, or if not. Since , this is at line L5, and the diagram commutes at .
The theorem says why flushing is a good choice. The abstraction comes from the stall behavior of the design, so nobody has to write it by hand. A correct design commutes with it on every reachable state, so on those states the check never gives a false alarm. A check over any that passes proves the design correct. A failure at a state outside proves nothing. Burch and Dill check all states. This is stronger than needed, and it fails for designs whose correctness depends on an invariant of the reachable states (see Pitfalls).
EUF makes it independent of the data. In the symbolic check, each data value is a term built from symbols by uninterpreted functions, such as . A formula of EUF is valid when it is true for every domain and every interpretation of the symbols and functions. The real ALU on 32-bit words is one interpretation, so a valid diagram holds for it. When the control values are fixed, as in the code of this page, the two sides are plain terms. Then validity is syntactic equality, because the free term algebra, in which every term denotes itself, is one interpretation in which different terms differ. In general the control depends on equalities between register numbers and on data, and line L6 needs an EUF decision procedure, such as congruence closure with case splits, Ackermann's reduction to propositional logic, or positive equality, which made large instances fast.
Example. The code models a two-stage pipeline with registers . Stage 1 reads the operands of an instruction into the pipeline register . Stage 2 applies the ALU and writes at the clock edge, and a bypass gives the result to a stage 1 instruction that reads in the same cycle. The flush is one stall cycle, so . Take the state with holding , the register file as symbols, and the input .
| route | register file at the end |
|---|---|
| flush, then ISA step (right) | , , |
| step, then flush, with the bypass (left) | the same terms |
| step, then flush, without the bypass (left) | , , |
Without the bypass, stage 1 reads the old , and the third register differs. The terms differ, so the check fails, and an interpretation with makes the values differ too. The breadth-first search of the tests then finds a real program that fails: write , then read it in the next cycle. A design that bypasses only the first operand fails in the same way on .
Cost. The two sides need symbolic implementation steps and one ISA step, so the formula has size for a design description of size , with shared terms. Validity in EUF is coNP-complete, because satisfiability of quantifier-free EUF is NP-complete. So the decision at line L6 is where the cost lies. The enumeration in the code is over control states and inputs for registers. That is 4,428 diagrams for , each of a few terms.
Tests. The term-level check passes for the correct pipeline with 1 to 3 registers and returns a counterexample for each of three bugs. An exhaustive check with 2-bit words and the real ALU agrees, and so does a check with five random ALU tables, as EUF validity predicts. On 1-bit words with 2 registers, a breadth-first search over pairs (implementation state, ISA state) confirms the theorem for all four designs: the diagram commutes on the reachable states exactly when every flushed run matches the ISA. Each failing program that the search returns is replayed. On 600 random programs with stalls, 4-bit words and up to 4 registers, the correct design matches the ISA after a flush, and the design without the bypass fails on more than 50 of them.
Classic examples
- In-order pipelines. Burch and Dill (1994) verified the control of a pipelined processor with bypasses and stalls, with data and memories left uninterpreted, using their own validity checker for EUF.
- Positive equality. Bryant, German and Velev (2001) showed that most equations in Burch-Dill formulas occur only positively. With positive equality, the terms can take fixed distinct values, and the formula becomes a much smaller propositional one. This made the check practical for much larger pipelines with SAT and BDD tools.
- Completion functions. Hosabettu, Srivas and Gopalakrishnan (1998) split the flush into one function per unfinished instruction, which says how that instruction would complete. The single large diagram becomes a chain of small lemmas.
- Simulation relations. The diagram is the condition for to be a functional simulation relation from the implementation to the specification, with the stall steps as stuttering steps.
Variations
- Several instructions per cycle. A superscalar design can fetch instructions in a cycle. The right side then runs ISA steps, where comes from the implementation state.
- Zero or one step, not known in advance. If is hard to state, check that the left side equals either or . This is weaker and needs a separate check of progress.
- Invariants. Replace by a set that contains the reachable states and is closed under , an inductive invariant. This removes false alarms from unreachable states at the cost of proving the invariant.
- Bit level. With small words and a real ALU, the check is a SAT or BDD problem, or a plain enumeration as in the tests. This needs no EUF and handles designs that rely on algebraic facts, but the cost grows with the width.
- Equivalence of two pipelines. Flushing both and comparing gives a check of one design against another, related to the product machine used for sequential equivalence.
Pitfalls
- A pipeline that never fetches. A design that stalls for ever makes the diagram commute with zero ISA steps in every cycle. The check proves safety only. Prove separately that the pipeline fetches within a bounded number of cycles.
- False alarms from unreachable states. The tests include a design whose bypass compares against a copy of stored in . In every reachable state the copy equals , and every program runs correctly. Over all states, a state where the copy differs makes the check fail. Add the invariant "copy equals " or check reachable states only.
- A flush that is itself wrong. The abstraction uses the stall behavior of the design. If stall cycles drop an instruction in flight, the check compares against the wrong state. The correctness criterion is about flushed runs, so such a bug shows only if stalls also occur in normal runs. Make the stall input of the flush the same input that normal operation uses.
- Algebraic facts. A design that swaps the operands of ADD is correct, but is not valid in EUF, so the check fails. Add the axiom, or leave that operation interpreted.
- The flush depth. If is less than the number of cycles that the longest instruction needs to finish, reads a register file with some writes missing, and the check fails on correct designs.
Code
Each implementation follows the pseudocode above. A comment @line links a line of code to a line of the pseudocode. Point at a line of the pseudocode to see the matching code. The build runs the native tests of each language.
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/burch-dill-flushing/. The authors are listed in prov/W76CN0.jsonl.
"""Burch and Dill's commuting-diagram check of a pipeline against its ISA.
The machine is a two-stage pipeline with a register file of R registers and
three-operand instructions (op, rd, rs1, rs2) meaning rd <- op(rs1, rs2).
Stage 1 decodes the instruction and reads its operands; the pipeline register P
then holds (valid, op, rd, tag, a, b). Stage 2 applies the ALU to a and b and
writes rd at the clock edge. An instruction in stage 1 that reads the register
written by the instruction in stage 2 must take the ALU result by a bypass.
The ISA state is the register file alone. The abstraction function flushes the
pipeline by one stall cycle (input None, no fetch) and keeps the register file.
The data values are a parameter of the machine. With alu = term (an
uninterpreted function) and atoms for the register contents, two values are
equal in every interpretation of the ALU exactly when they are the same term, so
the check over all control values is the EUF check of Burch and Dill. With
integers and a real ALU it is an exhaustive check of one interpretation.
bug selects a faulty design: "no-bypass", "bypass-rs1-only", or "tag", which
compares the bypass against a copy of rd that agrees with rd in every reachable
state but not in every state.
"""
from __future__ import annotations
from dataclasses import dataclass
from itertools import product
from typing import Callable, Hashable, Iterable, Optional
OPS = ("ADD", "AND", "XOR")
Instr = Optional[tuple[str, int, int, int]] # None is a stall cycle
@dataclass(frozen=True)
class Pipe:
valid: bool
op: str
rd: int
tag: int
a: Hashable
b: Hashable
State = tuple[tuple[Hashable, ...], Pipe] # (register file, pipeline register)
def term(op: str, a: Hashable, b: Hashable) -> Hashable:
"""The uninterpreted ALU: the result is the term op(a, b)."""
return (op, a, b)
def word_alu(width: int) -> Callable[[str, int, int], int]:
mask = (1 << width) - 1
def alu(op: str, a: int, b: int) -> int:
if op == "ADD":
return (a + b) & mask
if op == "AND":
return a & b
return a ^ b
return alu
class Machine:
def __init__(self, nregs: int, alu: Callable, zero: Hashable, bug: Optional[str] = None) -> None:
self.nregs, self.alu, self.zero, self.bug = nregs, alu, zero, bug
self.depth = 1 # stall cycles that empty the pipeline
def bubble(self) -> Pipe:
return Pipe(False, OPS[0], 0, 0, self.zero, self.zero)
def spec_step(self, rf: tuple, ins: Instr) -> tuple:
op, rd, rs1, rs2 = ins
out = list(rf)
out[rd] = self.alu(op, rf[rs1], rf[rs2])
return tuple(out)
def impl_step(self, q: State, ins: Instr) -> State:
rf, p = q
res = self.alu(p.op, p.a, p.b) if p.valid else None
if ins is None:
newp = self.bubble()
else:
op, rd, rs1, rs2 = ins
a, b = rf[rs1], rf[rs2]
if p.valid and self.bug != "no-bypass":
key = p.tag if self.bug == "tag" else p.rd
if key == rs1:
a = res
if key == rs2 and self.bug != "bypass-rs1-only":
b = res
newp = Pipe(True, op, rd, rd, a, b)
if p.valid:
rf = rf[:p.rd] + (res,) + rf[p.rd + 1:]
return (rf, newp)
def fetched(self, q: State, ins: Instr) -> bool:
"""Whether the implementation takes the instruction in this cycle (it never stalls itself)."""
return ins is not None
def flush(self, q: State) -> State:
for _ in range(self.depth):
q = self.impl_step(q, None)
return q
def abstraction(self, q: State) -> tuple:
return self.flush(q)[0]
def instructions(self) -> list[Instr]:
r = range(self.nregs)
return [(op, rd, s1, s2) for op in OPS for rd in r for s1 in r for s2 in r]
def commuting_check(m: Machine, states: Iterable[State], inputs: list[Instr]):
"""The first (q, i, left, right) where the diagram does not commute, or None."""
for q in states: # @line L1
s = m.abstraction(q) # @line L2
for i in inputs: # @line L3
q2 = m.impl_step(q, i)
left = m.abstraction(q2) # @line L4
right = m.spec_step(s, i) if m.fetched(q, i) else s # @line L5
if left != right: # @line L6
return (q, i, left, right)
return None # @line L7
def symbolic_states(m: Machine) -> Iterable[State]:
"""Every control value, with fresh atoms for all data: the states of the EUF check."""
rf = tuple(f"r{j}" for j in range(m.nregs))
r = range(m.nregs)
for valid, op, rd, tag in product((False, True), OPS, r, r):
yield (rf, Pipe(valid, op, rd, tag, "pa", "pb"))
def concrete_states(m: Machine, width: int) -> Iterable[State]:
"""Every state of the machine with width-bit words."""
words = range(1 << width)
r = range(m.nregs)
for rf in product(words, repeat=m.nregs):
for valid, op, rd, tag, a, b in product((False, True), OPS, r, r, words, words):
yield (rf, Pipe(valid, op, rd, tag, a, b))
def euf_check(nregs: int, bug: Optional[str] = None):
"""The Burch-Dill check over all states, with an uninterpreted ALU."""
m = Machine(nregs, term, "z", bug)
return commuting_check(m, symbolic_states(m), [None] + m.instructions())
test_burch_dill_flushing.py 149 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/burch-dill-flushing/. The authors are listed in prov/W76CN0.jsonl.
import random
from collections import deque
from itertools import product
from burch_dill_flushing import (OPS, Machine, Pipe, commuting_check, concrete_states, euf_check,
term, word_alu)
BUGS = (None, "no-bypass", "bypass-rs1-only", "tag")
def test_euf_check_passes_on_the_correct_pipeline():
for nregs in (1, 2, 3):
assert euf_check(nregs) is None
def test_euf_check_finds_each_bug():
q, i, left, right = euf_check(3, "no-bypass")
rf, p = q
assert p.valid and i is not None and p.rd in (i[2], i[3])
q, i, left, right = euf_check(3, "bypass-rs1-only")
assert q[1].valid and q[1].rd == i[3]
# The tag design fails only on a state where the copy of rd is wrong.
q, i, left, right = euf_check(3, "tag")
assert q[1].valid and q[1].tag != q[1].rd
def test_example_of_the_page():
# P holds ADD r1 <- pa + pb; the next instruction is ADD r2 <- r1 + r0.
m = Machine(3, term, "z", "no-bypass")
q = (("r0", "r1", "r2"), Pipe(True, "ADD", 1, 1, "pa", "pb"))
ins = ("ADD", 2, 1, 0)
left = m.abstraction(m.impl_step(q, ins))
right = m.spec_step(m.abstraction(q), ins)
assert right == ("r0", ("ADD", "pa", "pb"), ("ADD", ("ADD", "pa", "pb"), "r0"))
assert left == ("r0", ("ADD", "pa", "pb"), ("ADD", "r1", "r0"))
def test_concrete_exhaustive_with_a_real_alu():
for bug, ok in ((None, True), ("no-bypass", False), ("bypass-rs1-only", False), ("tag", False)):
m = Machine(2, word_alu(2), 0, bug)
res = commuting_check(m, concrete_states(m, 2), [None] + m.instructions())
assert (res is None) == ok
def test_euf_pass_covers_every_alu():
# A term-level pass holds for every interpretation of the ALU, here random tables.
rng = random.Random(31)
for _ in range(5):
table = {(op, a, b): rng.randrange(2) for op in OPS for a in range(2) for b in range(2)}
m = Machine(2, lambda op, a, b: table[(op, a, b)], 0)
assert commuting_check(m, concrete_states(m, 1), [None] + m.instructions()) is None
# ---------------------------------------------------------------- the theorem by brute force
def reachable(m, width):
"""All implementation states reachable from an empty pipeline, by breadth-first search."""
start = [(rf, m.bubble()) for rf in concrete_states_rf(m, width)]
seen = set(start)
todo = deque(start)
inputs = [None] + m.instructions()
while todo:
q = todo.popleft()
for i in inputs:
q2 = m.impl_step(q, i)
if q2 not in seen:
seen.add(q2)
todo.append(q2)
return seen
def concrete_states_rf(m, width):
return list(product(range(1 << width), repeat=m.nregs))
def flushed_runs_match(m, width):
"""Search the pairs (implementation state, ISA state) reachable together.
Returns None when every run, flushed, ends in the ISA state of the fetched
program, else a failing program.
"""
inputs = [None] + m.instructions()
start = [((rf, m.bubble()), rf) for rf in concrete_states_rf(m, width)]
parent = {pair: None for pair in start}
todo = deque(start)
while todo:
pair = todo.popleft()
q, s = pair
if m.abstraction(q) != s:
prog = []
while parent[pair] is not None:
pair, i = parent[pair]
prog.append(i)
return pair[1], prog[::-1]
for i in inputs:
nxt = (m.impl_step(q, i), m.spec_step(s, i) if m.fetched(q, i) else s)
if nxt not in parent:
parent[nxt] = (pair, i)
todo.append(nxt)
return None
def run_program(m, rf, prog):
q = (rf, m.bubble())
s = rf
for i in prog:
if m.fetched(q, i):
s = m.spec_step(s, i)
q = m.impl_step(q, i)
return m.flush(q)[0], s
def test_diagram_on_reachable_states_iff_all_flushed_runs_match():
for bug in BUGS:
m = Machine(2, word_alu(1), 0, bug)
diagram = commuting_check(m, reachable(m, 1), [None] + m.instructions()) is None
runs = flushed_runs_match(m, 1)
assert diagram == (runs is None), bug
if runs is not None:
rf, prog = runs
impl, isa = run_program(m, rf, prog)
assert impl != isa # a real failing program
# The tag design is correct on reachable states though the all-states check fails.
m = Machine(2, word_alu(1), 0, "tag")
assert flushed_runs_match(m, 1) is None
def test_random_programs_against_the_isa():
rng = random.Random(2026)
alu = word_alu(4)
bad = 0
for _ in range(600):
nregs = rng.randint(1, 4)
rf = tuple(rng.randrange(16) for _ in range(nregs))
prog = []
for _ in range(rng.randint(1, 8)):
if rng.random() < 0.2:
prog.append(None)
else:
prog.append((rng.choice(OPS), rng.randrange(nregs), rng.randrange(nregs), rng.randrange(nregs)))
for bug in (None, "tag"):
impl, isa = run_program(Machine(nregs, alu, 0, bug), rf, prog)
assert impl == isa
impl, isa = run_program(Machine(nregs, alu, 0, "no-bypass"), rf, prog)
bad += impl != isa
assert bad > 50
Exercises
- Add a stage to the example: fetch, read, execute, write. What is , and which bypasses does the check require?
- Give a design that fails the "zero or one step" check but passes the check with a known , or prove that none exists.
- Show that a design that always stalls passes the diagram, and write a bounded progress property that rules it out.
References
- J. R. Burch and D. L. Dill, Automatic verification of pipelined microprocessor control, Computer Aided Verification (CAV 1994), LNCS 818, 68 to 80, 1994, the abstraction function obtained by flushing the pipeline with stall inputs, the commuting diagram between one implementation step and zero or one specification steps, and its check by a validity checker for uninterpreted functions with equality
- R. E. Bryant, S. German and M. N. Velev, Processor verification using efficient reductions of the logic of uninterpreted functions to propositional logic, ACM Transactions on Computational Logic 2(1), 93 to 134, 2001, positive equality and the reduction of the Burch-Dill formulas of pipelined processors to propositional logic
- R. Hosabettu, M. Srivas and G. Gopalakrishnan, Decomposing the proof of correctness of pipelined microprocessors, Computer Aided Verification (CAV 1998), LNCS 1427, 122 to 134, 1998, completion functions, one per unfinished instruction, which split the flushing abstraction into smaller proof steps
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Code (python) | I0 not checked yet |
|
| Visual | V0 none |
|
| Writing | W3 standard |
|
Tag W76CN0 Edit this page History Report an error Markdown