Schedule independence by ownership (T2′, Grid.confluent_owned)
Lean: conclusions 1 and 2 proved, for every program, kernel, launch, arguments, address choice,
oracle and discipline, in MojoProof/Sem/ScheduleOwned.lean (Grid.confluent_owned,
Grid.owned_clean, Grid.owned_race, Grid.owned_bad), by the route of section "Route in Lean"
below: T2 (Grid.confluent, schedule.md) with its bad set replaced by the faults (GPUProof.Faults
at sig)
(Confluent.transfer along the identity), which conclusion 2 allows. Conclusion 3 is proved in a
stronger form, for every program, kernel, launch, arguments, address choice, oracle and discipline
(Grid.owned_sound, with the trace lemma racy_kept_of_local and Grid.Done.bad_iff,
MojoProof/Sem/Determinism.lean): its premises are on the trace of the done grid of the one run,
not on every reachable grid (section "Conclusion 3 in Lean"). This document's own route, below the
statement, is not formalized; it is kept because it needs no commutation of atomics.
It uses that an alloca's block number is Mem.fresh, from its thread and step count, and that no
step changes Mem.next. That an alloca reads its addresses from the skeleton choices
(Ctx.addr) and that the oracle holds the float choices only are not needed: this proof fixes
both. The lemmas it uses (section "Lemmas used") are proved, for the Lean route, in schedule.md's
setting: L1a (step_pending, Grid.advance_pending, Grid.fire_trace, and exec_retrace,
step_retrace, Grid.advance_retrace, Grid.fire_retrace for the trace no step or fire reads),
MojoProof/Sem/Schedule.lean, and with it bisim and the fault part of bad_eqv
(Grid.Same.step, Grid.Same.faults); L1c for the memory (Thread.pending_shape) and W5's block
part (Grid.reach_keeps: every grid reachable from g keeps every block of g, and, C1's part
about memory, its next and every number below next with no block free); C1's other part, the
closed-grid invariant (Grid.reach_closed and Grid.pending_reach, MojoProof/Sem/Closed.lean:
every step and fire keeps Grid.Closed, and a running thread of a closed grid has the same
pending accesses in every grid reachable from it; from the start, on a caller's memory whose
pointer bytes name known numbers, Grid.start_closed, Grid.start_reach_closed and
Grid.start_pending); W5's other part (Grid.start_reach_atCalls: in every grid reachable from
the start, every waiting thread is at its collective call, Thread.AtCall); and L1b
(step_changes, and in every grid reachable from the start Grid.start_reach_changes,
MojoProof/Sem/Writes.lean; Grid.fire_blocks, Schedule.lean). MojoProofTest/Launch.lean checks
the start's invariants on a launch that allocates, stores, waits at a barrier and returns.
The start makes the invariants hold. Grid.start first checks the caller's memory
(Mem.checkClosed, MojoProof/Sem/Grid.lean): a memory with next = 0 or a block 0 is unsupported,
and so is one in which an argument, or a pointer byte of a block below next, names a number from
next on. The launch's own blocks (shared memory, byval copies) come at next and above. An
alloca whose number is in use is unsupported, and so is an allocation of the start
(Mem.allocNext) at a number that has a block, so no block is ever overwritten, and the start
deletes no block of the caller's memory (Grid.start_blocks, proved for every program, launch,
memory and choice). Mem.checkClosed checks the fields of Mem.Closed except above (no block
from next on), which no function can check, and except the pointer bytes of blocks from next
on, which above says do not exist; so T2 takes the hypothesis that the caller's memory is closed
for the arguments. The invariant that every block number a thread holds is allocated (W1) is
replaced by the closed-grid invariant Grid.Closed, which holds at the start of a closed memory
and which every step keeps (section "The closed-grid invariant"). Without the check the statement
is false: a pointer argument naming the number that another thread's alloca takes later faults
in one order and runs in the other (the counterexample in section "The closed-grid invariant").
lifetime.start makes the bytes of a thread's own block undefined; L1b, L2, the disjointness
argument and bad_step include those bytes.
Scope. Every program, launch, start memory, skeleton choice and oracle, under the hypotheses
below. The hypotheses hold for kernels whose memory accesses follow one tiling for the whole run
(vector_addition). They fail for kernels whose shared-memory tiles change hands at a barrier
(tiled_matmul, which needs race freedom per barrier interval, barrier-owned.md) and for kernels
with atomics on one location (reduction, which needs L10: two atomicrmws that
Access.conflict exempts, of one commuting operation on the same bytes, commute). The hypotheses constrain
where accesses go, not whether steps fault. Faults are the bad states. An out-of-bounds access lies
in no tile, so a kernel with one is not covered.
Statement
Data. hs : Grid.start prog fn L args m ch oracle = .ok (g₀, c) and hm : m.Closed args, for
every start memory m and argument values args. A closed memory passes the start's check
(Mem.checkClosed_of_closed), and the start keeps every block of m (Grid.start_blocks). Let
N := g₀.threads.size, the number of the start grid's threads. The Lean statement takes T2's
hypothesis on the caller's memory instead of hm: every pointer byte of its blocks names a number
below next or with a block (hm : m.BytesIn m.Known, schedule.md).
The states are γ := { g // ReflTransGen (Grid.Step L c) g₀ g }. The relation is rc x y := Grid.Step L c x.1 y.1.
A discipline is a tiling map T : Nat → Option (Tiling ι) and a tile map own : Nat → ι (Tiling, Owned:
MojoProof/Sem/Trace.lean).
A definition the proof adds (part of L1). Thread.pending (c : Ctx) (t : Thread) (m : Mem) : List Event
lists the accesses that the step of t from m makes: for a load, store or atomicrmw at the thread's
position whose pointer operand evaluates, its access (two for atomicrmw, read then write), and nothing
otherwise. This holds whether the step then succeeds or faults. On success the step logs exactly these
accesses (step_pending). (Proved in MojoProof/Sem/Schedule.lean, as a list of events, newest first, so
an atomicrmw's write is in front of its read; step_pending for every context, thread and memory.)
Hypotheses.
hH(owned accesses): for every reachableg, every threadnand every accessaamongGrid.pending c g n(Thread.pending (c n) t g.memifg.threads[n]? = some (.run t), none otherwise), eitherOwned T own a, orais thread-local:g.mem.Owned n a.ptr.block(blocka.ptr.blockofg.memhas owner.thread n).hX(exclusive tiles):ownis injective on thread numbers belowN.hI(invariant loads): for every reachableg, threadnand accessaamongGrid.pending c g nwithinvariant = true,T a.ptr.block = noneandais not thread-local (¬ g.mem.Owned n a.ptr.block).
Equivalence. Grid.Same g g': g.threads = g'.threads, g.mem.blocks = g'.mem.blocks,
g.mem.next = g'.mem.next, and g.mem.trace is a permutation of g'.mem.trace (List.Perm). No renaming
of blocks is needed: a block's number (Mem.fresh) does not depend on the schedule. T2 (schedule.md)
replaces List.Perm with Trace.Same, which also keeps each access's barrier phase, and proves T2′ as
its corollary; the Lean proof follows schedule.md, and this document's own route is kept as the one
that needs no atomic commutation.
Conclusions.
Confluent rc (fun x y => Grid.Same x.1 y.1) (fun x => GPUProof.Faults sig (x.1, L, c)).- For every reachable
g:racy g.mem.trace = falseandGPUProof.kept sig ((g.mem.trace.map (Event.toCore L)).reverse) = true; and if a race is enabled ing(the core'sRaceEnabledatsig),gfaults (the racing thread's step is a fault; a fire that completes at a reachable grid adds no race, since the trace is not racy). So on reachable grids the core'sBadatsigisGPUProof.Faultsatsig: no reachable grid has a racy trace or a broken promise of!invariant.load. An enabled race is not excluded, only one whose step succeeds:Stmt.pendinglists an access before the step checks it, so a thread's pending access may conflict with another's and still be one the memory refuses. - Corollary through
Grid.sound_of_one_run(adapter.md): if one run reaches a done gridzandobsdepends only onMem.blocks, thenGrid.Sound L c g₀ obs RwithRthe property ofobs z.mem.hTandhKcome from conclusion 2.hFholds becauseBadisFaults.hDandhOhold becauseSamekeeps the threads and the blocks. The Lean form (Grid.owned_sound) goes through T2'sGrid.start_soundinstead, and needs the discipline of the done grid's trace only: section "Conclusion 3 in Lean".
Why the bad set is Faults. Bad := False would make the adapter's hF say that no reachable
grid faults, which the hypotheses do not give: a kernel can fault in every schedule. Bad := Faults
needs bad_step, which says that faults persist. Under hH this is a frame argument (case P
below). It needs neither the core's RaceEnabled nor L7.
Lemmas used
- L1a (trace):
stepandGrid.firedo not readMem.trace. A successful step's trace isThread.pending ... ++ m.trace. A fire's trace ism.tracewith one.barrierevent in front for a barrier, and unchanged for a shuffle. (Proved for steps:step_pending, withexec_pendingfor every statement; a step that stops at a collective has no pending accesses;Grid.advance_pending, the same for a grid step of threadn, for every grid. Proved for fires:Grid.fire_trace, for every grid. Proved: no step or fire reads the trace, for every context, statement, scope, thread, grid and tracetr: on the input with its trace replaced bytr, the result is the same withtrand the logged events in front as its trace (exec_retrace,step_retrace,Grid.advance_retrace,Grid.fire_retrace).) -
L1b (writes): a successful step of thread
nchangesmonly:- at the bytes of its pending writes;
- at the liveness of blocks with owner
.thread n, and at all bytes of such a block, whichlifetime.startmakes undefined with no pending write (Mem.setLive; LangRef,llvm.lifetime.start, resets the contents of an object that is already live too); - by adding the block
m.fresh n kat anallocaat stepk, which no block had (anallocawhose number is in use faults); - at the trace.
It never changes
m.nextand never removes a block. A fire changes no block.In Lean (MojoProof/Sem/Writes.lean, for every context, thread and memory):
Mem.Changes L N m m'says thatm'keeps every block ofmwith its shape, read-only flag and owner (andMem.Keeps), adds blocks only at numbers ofN, and has the trace ofmwith eventsdin front, and that a block at a number outsideLkeeps its liveness, and its bytes but those below its size that an access ofdwrites (Access.Writes; a store's check bounds the bytes it writes by the block's size,Mem.store_changes). So such a block keeps its owner, its size and its bytes from its size on (Mem.Changes.above).step_changes: a step of threadc.who.threadthat continues or returns satisfiesMem.Changes L N m m'ifLhas every block the thread owns and every alloca of its frames, andNhasm.fresh c.who.thread t.k; bystep_pending,dis its pending accesses. The premise on the allocas is needed: a return makes the frame's allocas dead (Mem.setLive), and a frame whose allocas list a block of every thread makes that block dead (MojoProofTest/Writes.lean,ret_twin).Grid.advance_changesis the same for the step of threadnof a grid, in contextc n;Grid.fire_blocks(Schedule.lean): a fire changes no block and keepsnext. The premise on the allocas holds in every grid reachable from the start (Grid.Owns: every alloca of a frame of threadnis a block that thread(c n).who.threadowns;Grid.start_owns,Grid.advance_owns,Grid.fire_owns,Grid.reach_owns), soGrid.start_reach_changes, for every program, kernel, launch, arguments, memory and choices: in every grid reachable from the start, a step of the running threadtatnisMem.ChangeswithLthe blocks thread(c n).who.threadowns andNits fresh blockfresh (c n).who.thread t.k. The twin of the invariant: the grid of a thread whose frame lists a block of every thread among its allocas is notGrid.Owns(owns_twin).Grid.start_reach_above, for the same scope: in every grid reachable from the start, every block of the start's memory whose owner is no thread has its owner, its size and its bytes from its size on (a step byMem.Changes.above, a fire changes no block), and the caller's blocks are blocks of the start's memory (Grid.start_blocks). This is the part of the equality of a final block with an expected one that evaluation cannot check, the bytes being a function: a check of the fields and of the bytes below a bound at least every block's size gives the rest. The premise on the owner is needed: making a block live (Mem.setLive) makes every byte of it undefined, those from its size on too (MojoProofTest/Writes.lean,above_twin). Instance: vector_addition's run on one thread (MojoProofTest/SkelVadd.lean,above_vadd). -
L2 (frame): let
tbe a threadnwith step countk, and let its names be the block numbers of the pointers it holds (Grid.Closedbelow) and, if it is at analloca,m.fresh n k. Ifm₁andm₂agree on three things, then the steps oftfromm₁and fromm₂both fault, or both succeed with the same thread state and the same changes (L1b) applied to their memories. The three things are:next, and the bytes of the pending reads oftin blocks that another thread does not own;- the existence, space, size, addresses,
readOnlyand owner of every blocktnames; - the liveness of every block
tnames that another thread does not own.
An access of
tto a block that another thread owns faults in every state, before any byte is read (Mem.check: dead, or live and owned by another thread), and so does a lifetime marker ofton it (unsupported); sotreads neither the liveness nor the bytes of such a block.A step looks a block up only through a pointer it holds (
Mem.block,Mem.addr,Mem.check) or at itsalloca's number, so it does not see the blocks it does not name. The same holds of a fire and the names of its participants.In Lean (MojoProof/Sem/Frame.lean; the definitions in MojoProof/Sem/SigDef.lean), for steps.
Mem.Agree w S P m₁ m₂:m₁andm₂have the samenext, and at every numberbofSboth have no block, or blocks with the same shape,readOnlyand owner which, if the owner admits threadw(Owner.admits), have the same liveness and the same bytes where a read access ofPreads (Reads P b;Block.Agree).Sstands for the names: it must have every number the scope of the innermost frame names (Scope.NamesIn: null, the registers and the globals) and thealloca's number. The changes are a list ofMem.Op(bytes put into a block, a block set, a block made live or dead, an event logged), applied in order byMem.run;Step.withMem m risrwith its memory, if it has one, replaced bym. For every context, thread and memories:theorem step_agree {c : Ctx} {t : Thread} {m₁ m₂ : Mem} {r : Step} (hs : ∀ f, t.frames.head? = some f → (c.scope m₁ f t.k).NamesIn S) (hk : S (m₁.fresh c.who.thread t.k)) (h : m₁.Agree c.who S (t.pending c m₁) m₂) (he : step c t m₁ = .ok r) : ∃ ops, (∀ o ∈ ops, o.At m₁ c.who (fun b => m₁.Owned c.who.thread b ∨ ∃ f ∈ t.frames, b ∈ f.allocas) (· = m₁.fresh c.who.thread t.k) (ops.filterMap Mem.Op.event)) ∧ r = r.withMem (m₁.run ops) ∧ step c t m₂ = .ok (r.withMem (m₂.run ops))and
step_fault, under the same hypotheses:step c t m₁ = .error x → ∃ y, step c t m₂ = .error y(bystep_agreefromm₂, withMem.Agree.symmandThread.pending_shape). So both fault, or both succeed with the same result and the same changes.exec_agreeis the same for a statement. Instances: MojoProofTest/Frame.lean (agree,store, andload: a load of ani32gives 7 from two memories that differ only at bytes it does not read); the twinsnames_twin(a store through a pointer to a block outsideScontinues on a memory with that block and faults on one without it) andload_twin(two memories that differ at a byte the load reads give 7 and 8). The first conjunct, where the ops act (Mem.Op.At), is step 2 of the two-thread case below. L2 for the grid's steps isGrid.advance_agree, and its use in the diamond with the changes of another thread's step (L1b) isGrid.advance_agree_otherandGrid.advance_comm(MojoProof/Sem/Confluence.lean). A fire changes no block (Grid.fire_blocks). -
L3 (choices): step
kof threadnreads its skeleton choices(c n).addrand its oracleoracle natNat.pair k ionly, andkis the thread's own count. This holds by construction, with no Lean lemma:Ctx.scopegives stepkthe oracle numbersc.oracle (Nat.pair k i), andexecgives anallocathe addressesc.addr (Nat.pair k 0)andc.addr (Nat.pair k 1). - W1 (closed grids):
Grid.Closedholds of every reachable grid (next section). W2: a block made by threadn'sallocahas owner.thread n. W5: a waiting thread is at its collective call, and a block's space, size and addresses never change. Both parts of W5 are proved:Grid.reach_keeps(MojoProof/Sem/Schedule.lean), andGrid.start_reach_atCalls(MojoProof/Sem/Closed.lean;Thread.AtCall c t i fn: the current statement oftcallsfn, the function ofc's module whose intrinsic isi). Mem.check(MojoProof/Sem/Value.lean): an access to a block owned by another thread is undefined behavior, dead or live. So such an access faults in every order, and a logged access to a thread-local block is by its owner.
The closed-grid invariant (W1)
A grid holds a pointer if it occurs (inside vectors and aggregates too) in a register, the fmul
operands (prods) or the allocas of a frame of a thread, in the arguments of a waiting thread, in the
globals table of a context, or as a pointer byte .ptr p i below the size of a block. Define
Grid.Closed g := ∀ p held by g, p.block < g.mem.next ∨ (g.mem.blocks p.block).isSome.
- Start. After
Grid.start, every held pointer names a number belownext. The caller's memory is closed for the arguments (the hypothesishm,Mem.Closed):nextis at least 1, block 0 and every number fromnexton have no block, and every block number the arguments name (Val.bounds) and every pointer byte below the size of a block names is belownext. The start changes none of its blocks (Grid.start_blocks) and allocates the shared memory and thebyvalcopies atnextand up, whose bytes encode the arguments. The registers of a thread are itslaunchArgs: abyvalcopy is a pointer to a block belownext, and every other argument is its value underwithAttrs, which holds no pointer the value does not hold. The globals tables point to shared blocks belownext. Null is block 0, belownext(at least 1), and no block has number 0, so an access through null is undefined behavior in every grid. - Step. Every successful step of a closed grid gives a closed grid.
nextnever changes and no block is removed (L1b), so a pointer that was held keeps its property. The new held pointers are: analloca's result (its block exists); a GEP,select,phi, call or return of held values (GEP keeps the block number); a loaded value, decoded from pointer bytes of an existing block (held) or null from zero bytes; the bytes a store writes, encoded from a held value (lifetime.startwrites undefined bytes, which hold no pointer); a shuffle's results, the operands of its lanes or poison. No operation makes a pointer from an integer (inttoptris unsupported), and no intrinsic returns a pointer it was not given.
In Lean (MojoProof/Sem/Names.lean and Closed.lean; Mem.Known in SigDef.lean), a number is known to
m (Mem.Known) if it
is below m.next or has a block, and Grid.Closed c g has four fields: null (0 is known),
threads (Status.NamesIn: every value in a register of a frame of a thread, and every argument of
a waiting thread, names only known numbers, Val.NamesIn), bytes (Mem.BytesIn: every pointer
byte below the size of a block does) and globals (every global of every context c n does). It
leaves out prods and allocas, as no step takes a pointer from them: an fadd makes only floats
from the fmul operands, and a return's setLive on the allocas changes only liveness. Proved,
for every program, context map, launch and grid: Step (exec_closed, enter_closed, step_closed,
Grid.advance_closed, Grid.fire_closed; every grid reachable from a closed grid is closed,
Grid.reach_closed) and C1 below (Grid.pending_reach, from Grid.reach_keeps,
Mem.Keeps.sameShape and Thread.pending_shape) and Start (Grid.start_closed, for every
program, kernel, launch, arguments, choices and memory whose pointer bytes name known numbers,
Mem.BytesIn m m.Known, which a closed memory has, Mem.Closed.bytesIn; the start's check gives the
rest, Mem.checkClosed_ok). So C1 holds from the start (Grid.start_reach_closed,
Grid.start_pending). The premise on the bytes is needed: the memory mem100 (next = 2, one block 5
holding a pointer to block 100) passes the check and starts, and its grid is not closed
(MojoProofTest/Closed.lean, start_twin). Not proved: C2.
Two consequences carry the proof.
- (C1) In a closed grid, a pointer to no block names a number below
next. No step makes a block belownext(analloca's numberm.fresh n k = next + Nat.pair n kis at leastnext), so the pointer points to no block in every grid reachable from there. - (C2) The number of the block a step of thread
madds,m.fresh m k, had no block and is at leastnext, so in the closed grid before the step nothing held a pointer to it: no other thread names it. It is also different from everym.fresh n k'withn ≠ m(Nat.pairis injective).
Under Closed, an alloca's number is never in use in a reachable grid. The blocks at numbers from
next on are those of earlier allocas, m.fresh n' k' with k' below thread n''s count. The check
in exec is kept anyway, because with it L1b needs no invariant.
Counterexample without the start's check (MojoProofTest/Eval.lean, startMemTests). Two
threads run %a = alloca i32; %x = ptrtoint ptr %p to i64; ret void with %p = .ptr ⟨5, 0⟩.
Without the check, next = 3 after the buffer (block 1) and the dynamic shared memory (block 2),
and thread 1's alloca takes 3 + Nat.pair 1 0 = 5. Thread 0 first: its ptrtoint is undefined
behavior (pointer to no block). Thread 1 first: block 5 exists, and both threads run to the end. A
step takes a faulting grid to one that does not fault, against bad_step. With the check, the
caller's memory (the buffer as block 1, next = 2) names block 5 from next on through the
argument, so the start (Mem.checkClosed) refuses the launch, in both orders; the test checks the
refusal.
Proof
Disjointness. Let two different threads n and m both run in a reachable g. Each write that n's step
makes lies in tile own n of a tiled block, or in a block owned by .thread n. Owned.read forbids writes to
untiled blocks. Each read that m's step makes lies in tile own m, in an untiled block, or in a block owned
by .thread m. Tiles own n and own m are disjoint (hX, Tiling.disj). Untiled blocks are never written,
and owners differ. So n's writes miss m's reads and m's writes. Both steps add blocks only at numbers
keyed by their own thread, which the other thread does not name (C2), and change liveness, and reset
bytes (lifetime.start), only of their own blocks, to which the other thread's accesses fault in every
state (Mem.check).
In Lean, the part about memories (MojoProof/Sem/Frame.lean, Mem.Changes.agree): if m' is m
changed as L1b allows (Mem.Changes L N m m', with events d in front of its trace), then m and
m' agree for thread w at S and the reads of P (L2's Mem.Agree) if N has no number of
S, no block of m at a number of S and L admits w, and no access of d writes a byte that
a read of P reads in a block of S that admits w:
theorem Mem.Changes.agree {L N : Nat → Prop} {m m' : Mem} {w : Who} {P : List Event}
(h : m.Changes L N m') (hN : ∀ b, S b → ¬ N b)
(hL : ∀ b x, S b → L b → m.blocks b = some x → x.owner.admits w = false)
(hd : ∀ d, m'.trace = d ++ m.trace → ∀ b x i a, S b → m.blocks b = some x →
x.owner.admits w = true → Reads P b i → .access a ∈ d → ¬ a.Writes b i) :
m.Agree w S P m'
With m' the memory after m's step (Grid.start_reach_changes: L the blocks m owns, N
its fresh block, d its pending accesses), S n's names and P n's pending accesses, the
three premises are C2, the owners and the tiles; that they hold in every reachable grid is not yet
proved. Instance: MojoProofTest/Frame.lean, changes_agree (a store of bytes 0 to 3 and a read of
bytes 4 to 7); the twin changes_twin: at a read of the bytes the store writes, the memories do not
agree.
refl, symm, trans hold because Same is equality and List.Perm.
bisim. Take Same a a' and a step a → b. By L1a the same thread (or the same fire) steps from a' to
b', with the same new events in front of a permuted trace. So Same b b'. (Lean: Grid.Same.step, for
every launch and context map.)
bad_eqv. Faults reads the threads and, through step and Grid.fire, the memory without its trace
(L1a). Same keeps both. (Proved for Faults: Grid.Same.faults.)
diamond. Take two steps g → b and g → b', with g reachable.
- A thread's step and the same thread's step.
Grid.advanceis a function, sob = b'andSame b b'. - The steps of two threads
n ≠ m. By disjointness and C2,n's step leavesm's footprint (the three things of L2) unchanged. So by L2,msteps frombtod, with the same changes as fromg. Symmetrically,nsteps fromb'tod'.dandd'apply the same two sets of changes in the two orders. The changes touch disjoint bytes, liveness flags and block numbers (a reset bylifetime.starttouches the bytes of the stepping thread's own block, which the other thread's successful step does not access), so the blocks are equal. The traces areEₘ ++ Eₙ ++ trandEₙ ++ Eₘ ++ tr, a permutation. Thread states change only atnandm, soSame d d'. L3 makes each thread's choices the same in both orders. - A thread's step and a fire at
k. An enabled fire has no running participant:Grid.firegivesnonewhile a participant of the barrier's block, or a lane of the shuffle's warp, runs (L5). So the running threadnis not a participant. Threadn's step leaves the participants' states unchanged. It also leaves the memory that the fire reads unchanged: the fire reads only block addresses, throughwithAttrs, and these never change (W5). So the fire is still enabled fromband gives the same resumed threads. The fire changes no block and no state ofn(L1a), son's step fromb'is the same as fromg(L2). The two results differ at most in the order of one.barrierevent in the trace, soSame d d'. - Two fires, at
kandj. If their participant sets are disjoint, they commute as in the previous case, with traces equal up to the order of two barrier events.- Two barriers sharing a thread: they share the whole block of threads. Each resumes every thread of the
block with no value and logs
.barrierof that block, sob = b'. - Two shuffles sharing a lane: they share the warp. Both enabled fires check every waiting lane against
their own mode, mask and position, so
k's andj's agree. Theirbase,lanes,partsand results are then equal, andb = b'. - A barrier and a shuffle cannot share a thread, because a thread waits at one call.
- Two barriers sharing a thread: they share the whole block of threads. Each resumes every thread of the
block with no value and logs
The two-thread case in Lean (done, under its premises). step_agree gave ∃ ops without saying
where the ops act, and two lists of ops commute only if they act at different places. Five steps,
all done:
-
Mem.Op.At m w L N W o(MojoProof/Sem/Frame.lean): where opoof a step of threadwon memorymacts. A put.put b o bsis into a block ofmwhose owner admitsw, and every byteifromobelowo + bs.lengthis written by an access ofW(Access.Writes a b i); a set.set b _hasN band no block atbinm; a liveness change.live b _hasL b; a log.log ehasean access (a step logs its pending accesses,step_pending, andStmt.pendinglists only accesses). The puts are the stores'Mem.writes, the sets theallocas at the fresh number, the liveness changeslifetime.startand.end(Intrinsic.ops,intrinsic_at) and the returns'allocas.def Mem.Op.At (m : Mem) (w : Who) (L N : Nat → Prop) (W : List Event) : Mem.Op → Prop | .put b o bs => (∃ x, m.blocks b = some x ∧ x.owner.admits w = true) ∧ ∀ i, o ≤ i → i < o + bs.length → ∃ a, .access a ∈ W ∧ a.Writes b i | .set b _ => N b ∧ m.blocks b = none | .live b _ => L b | .log e => ∃ a, e = .access a -
exec_agreeandstep_agreegive the places of their ops, withWthe events the ops log:step_agree's conclusion is∃ ops, (∀ o ∈ ops, o.At m₁ c.who (fun b => m₁.Owned c.who.thread b ∨ ∃ f ∈ t.frames, b ∈ f.allocas) (· = m₁.fresh c.who.thread t.k) (ops.filterMap Mem.Op.event)) ∧ r = r.withMem (m₁.run ops) ∧ step c t m₂ = .ok (r.withMem (m₂.run ops))and
exec_agree's the same withLthe blocks ofs.memthe thread owns andNthe fresh number at stepk.Mem.write_agreeandMem.store_agreegive a store's put inside the block, in a block that admits the thread. -
Mem.run_comm: ops at disjoint places commute. Proved (MojoProof/Sem/Confluence.lean, for every memory and all ops;Mem.Op.Apartand, for each numberc,Mem.Op.on c, what an op does to the block there). Two puts at one block are apart if no byte the first puts is one the second puts (∀ i, o ≤ i → i < o + bs.length → i < o' ∨ o' + bs'.length ≤ i), so empty puts are apart from every put:theorem Mem.run_comm (m : Mem) {os₁ os₂ : List Mem.Op} (h : ∀ o₁ ∈ os₁, ∀ o₂ ∈ os₂, o₁.Apart o₂) : ((m.run os₁).run os₂).blocks = ((m.run os₂).run os₁).blocks ∧ ((m.run os₁).run os₂).next = ((m.run os₂).run os₁).next ∧ Trace.Same ((m.run os₁).run os₂).trace ((m.run os₂).run os₁).traceInstances: MojoProofTest/Confluence.lean
commandsame(puts at the two halves of one block, writes by threads 0 and 1 logged, and a set); the twinstwin(two puts at byte 0: not apart, and the orders differ there) andtrace_twin(a write by thread 1 and a barrier of its block of threads: not apart, and after a write by thread 0 one order is racy and the other not). -
The ops of two threads' steps are apart (
Mem.Op.At.apart, for every memory and all ops): for threadsw,w'with different numbers, if their liveness changes are at blocks ofmeach owns, their sets at different numbers, and no write one logs overlaps a write the other logs. A put of one and a set of the other are at different blocks, as the put's block exists and the set's does not; a liveness change of one is at a block owned by it, whose owner admits no other thread, so not at the other's put or liveness change; two puts at one block are at disjoint bytes, as a common byte would be written by a write of each; two logs are accesses, which are independent.theorem Mem.Op.At.apart {m : Mem} {w w' : Who} {L L' N N' : Nat → Prop} {W W' : List Event} (hw : w.thread ≠ w'.thread) (hL : ∀ b, L b → m.Owned w.thread b) (hL' : ∀ b, L' b → m.Owned w'.thread b) (hN : ∀ b, N b → ¬N' b) (hW : ∀ a a', .access a ∈ W → .access a' ∈ W' → a.overlaps a' = true → a.write = false ∨ a'.write = false) : ∀ {o o' : Mem.Op}, o.At m w L N W → o'.At m w' L' N' W' → o.Apart o'In a grid whose threads own their allocas (
Grid.Owns) the blocks a step's frames allocated are blocks it owns, and the fresh numbers of two threads differ (Mem.fresh_inj, byNat.pair_eq_pair). -
The case (
Grid.advance_comm).Grid.advance_agreeliftsstep_agreeto the grid:n's step fromggives a statussand ops at those places withg' = {threads := g.threads.set! n s, mem := g.mem.run ops}, and from every grid withn's thread atnand a memorym₂that agrees withg.memforn, the samesand ops onm₂.Grid.advance_agree_other: in a grid whose threads own their allocas,k's step leaves the memory agreeing for a threadnwith another number at the numbersg.memknows andn's fresh number, if no write ofkoverlaps an access ofn(byMem.Changes.agreewithGrid.advance_changes). In a closed grid those numbers are alln's scope names (Grid.Closed.scope), son's step fromgₖisn's ops ongₖ.mem, and symmetrically; byMem.run_commandArray.setIfInBounds_commthe two grids areGrid.Same:theorem Grid.advance_comm {c : Nat → Ctx} {g gn gk : Grid} {n k : Nat} (hg : g.Closed c) (ho : g.Owns c) (hw : (c n).who.thread ≠ (c k).who.thread) (hn : g.advance c n = .ok (some gn)) (hk : g.advance c k = .ok (some gk)) (hW : ∀ a a', .access a ∈ g.pending c n → .access a' ∈ g.pending c k → a.conflict a' = false) : ∃ g₁ g₂, gn.advance c k = .ok (some g₁) ∧ gk.advance c n = .ok (some g₂) ∧ g₁.Same g₂For every grid, every context map and all threads
n,k.n ≠ kfollows fromhw. The premisehWasks that no pending access of either conflict with one of the other (Access.conflict). If some write of one overlaps an access of the other, the two are an exempt pair, twoatomicrmws of one commuting operation at one pointer and size, andGrid.advance_atomic_commgives the conclusion (L10, below). Otherwise no write of either overlaps any access of the other (not only the other's writes), as the agreement of L2 needs, and the argument above applies.
The instance (MojoProofTest/Confluence.lean diamond): on stores 4, threads 0 and 1 storing 7
and 8 to bytes 0 to 3 and 4 to 7 of one block, the premises hold and the two orders give the same
grid. The twins: diamond_twin (on stores 0 both store to bytes 0 to 3, two plain writes that
conflict, so hW fails, and the two orders give grids that differ at byte 0); who_twin (two threads at alloca with one thread number
in their contexts: both take block 2, and after either step the other's faults); owns_twin (a
thread whose frame lists block 1, of every thread, among its allocas, so the grid is not
Grid.Owns: its return makes block 1 dead, after which the other thread's store to block 1 faults,
while in the other order both succeed). Closed.pending_twin shows a grid that is not closed in
which one thread's step changes another's pending accesses.
From the start (Grid.start_reach_comm, Confluence.lean): the start's contexts give thread n
the program and the number n (Grid.start_ctx, Grid.lean), and every grid reachable from the
start's is closed and owns its allocas (Grid.start_reach_closed, Grid.start_owns,
Grid.reach_owns), so for every program, kernel, launch, arguments and choices on a caller's
memory with m.BytesIn m.Known:
theorem Grid.start_reach_comm {prog : Program} {fn : Func} {L : Launch} {args : List Val}
{m : Mem} {ch : Nat → Nat} {oracle : Nat → Nat → Nat} {g g' gn gk : Grid}
{c : Nat → Ctx} {n k : Nat} (h : Grid.start prog fn L args m ch oracle = .ok (g, c))
(hm : m.BytesIn m.Known) (hr : Relation.ReflTransGen (Grid.Step L c) g g')
(hnk : n ≠ k) (hn : g'.advance c n = .ok (some gn))
(hk : g'.advance c k = .ok (some gk))
(hW : ∀ a a', .access a ∈ g'.pending c n → .access a' ∈ g'.pending c k →
a.conflict a' = false) :
∃ g₁ g₂, gn.advance c k = .ok (some g₁) ∧ gk.advance c n = .ok (some g₂) ∧
g₁.Same g₂
Its instance (start_diamond): the launch on two threads of alloca i32, a store of 7 to it and
ret void; after both allocas (blocks 2 and 4) the two stores commute. The twin of n ≠ k
(start_twin): in the launch of MojoProofTest/Launch.lean thread 0's return from g4 succeeds
and accesses nothing, but after it thread 0 takes no step, so the conclusion fails for
n = k = 0. The twin of hm is Closed.start_twin (a memory that passes the start's check and
gives a grid that is not closed); that of hW is diamond_twin.
The case of a thread and a collective call in Lean (done for every grid).
Grid.advance_fire_comm (Confluence.lean), for every launch, contexts and grid, at a barrier or a
shuffle, with no premise of closedness or ownership:
theorem Grid.advance_fire_comm {L : Launch} {c : Nat → Ctx} {g gk gf : Grid} {n k : Nat}
(hk : g.advance c k = .ok (some gk)) (hf : g.fire L c n = .ok (some gf))
(hw : .barrier (c k).who.cta ∉ g.fired L n) :
∃ g₁ g₂, gk.fire L c n = .ok (some g₁) ∧ gf.advance c k = .ok (some g₂) ∧
g₁.Same g₂
Grid.fired L g n is [.barrier (n / size)] if n waits at a barrier and [] otherwise, so at
a barrier hw says that k's context is not of n's block of threads, and at a shuffle it holds.
The proof: k runs (Grid.advance_set). So k is not a participant: at a barrier a running
thread of n's block makes the barrier give none, and at a shuffle a running lane of n's warp
makes parts.mapM id none (Grid.fire_frame, by mapM_ok_mem and mapM_id_mem). The call
changes no thread but its participants (resumeAll_getElem?) and no block. The participants'
results go through withAttrs on the memory, which reads it only by Mem.addr, and so only at
the shape of a block that exists. A step keeps every block's shape (Grid.advance_keeps, W5), so
what the call gives on g's memory it gives on the memory after k's step (Mem.addr_keeps,
withAttrs_keeps, resumeAll_set). No closedness is needed: only success moves from one memory
to the other. So each order gives g with k's new status, the participants resumed and k's
memory, with traces Grid.fired L g n in front of k's accesses or behind them
(Grid.advance_retrace). Every one of those accesses is by (c k).who (Thread.pending_who), and
every event fired is .barrier (n / size) (Grid.mem_fired), so by hw the two are independent
and the traces are Trace.Same (L11).
Its instances (MojoProofTest/Confluence.lean):
fire_diamond: thread 0 at barrier 0 and thread 1 storing, one thread per block of threads, in contexts that put threadnin block of threadsn.shfl_diamond: thread 0 at a butterfly shuffle, which with one thread per block of threads is a warp of one lane.shfl_valuechecks that it gives thread 0 its own value5under the callee'srange(i32 0, 8).
The twin of hw (fire_twin): fire_diamond's grid in contexts that put both threads in block
of threads 0. After a write by thread 0 of the bytes thread 1 stores to, one order's trace is racy
and the other's not, so the grids are not the same.
The case of a thread and a collective call from the start (done). Grid.start_reach_fire_comm
(Confluence.lean), for every program, kernel, launch, arguments, caller's memory, address choice
and oracle, in every grid reachable from the start's, at a barrier or a shuffle, with no premise on
blocks of threads:
theorem Grid.start_reach_fire_comm {prog : Program} {fn : Func} {L : Launch} {args : List Val}
{m : Mem} {ch : Nat → Nat} {oracle : Nat → Nat → Nat} {g₀ g gk gf : Grid} {c : Nat → Ctx}
{n k : Nat}
(h : Grid.start prog fn L args m ch oracle = .ok (g₀, c))
(hr : Relation.ReflTransGen (Grid.Step L c) g₀ g)
(hk : g.advance c k = .ok (some gk)) (hf : g.fire L c n = .ok (some gf)) :
∃ g₁ g₂, gk.fire L c n = .ok (some g₁) ∧ gf.advance c k = .ok (some g₂) ∧
g₁.Same g₂
The proof: hw of Grid.advance_fire_comm. If the call fires a .barrier, it is n's
(Grid.mem_fired), and k is not among n's block's threads n / size * size + j, j < size
(Grid.fire_frame). The start's contexts give k the block of threads k / size
(Grid.start_ctx). If k / size = n / size, then k = n / size * size + k % size would be one of
them, with k % size < size when size > 0. And size > 0, as k is a thread
(Grid.start_reach_block). That holds because the start has a thread only if size > 0
(Grid.start_run), a step sets one thread, and a fire resumes threads at numbers that had one
(Grid.fire_threads).
Its instance (start_fire_diamond): the kernel of MojoProofTest/Launch.lean (an alloca, a
store, barrier 0, ret void) on two blocks of one thread, with thread 0 at the barrier and thread
1's store next. The twin of the start's contexts (start_fire_twin): fire_twin's contexts are no
start's, since the start of its launch puts thread 1 in block of threads 1.
The case of two collective calls in Lean (done). Grid.fire_fire_comm (Confluence.lean), for
every launch, contexts and grid, at barriers and shuffles alike, if the two threads' contexts have
one layout:
theorem Grid.fire_fire_comm {L : Launch} {c : Nat → Ctx} {g gn gj : Grid} {n j : Nat}
(hl : (c n).prog.layout = (c j).prog.layout)
(hn : g.fire L c n = .ok (some gn)) (hj : g.fire L c j = .ok (some gj)) :
gn = gj ∨
∃ g₁ g₂, gn.fire L c j = .ok (some g₁) ∧ gj.fire L c n = .ok (some g₂) ∧ g₁.Same g₂
The first alternative is the diamond's degenerate case: the two fires are one step of Grid.Step.
Grid.start_fire_fire_comm gives the same for every grid in the start's contexts, for every
program, kernel, launch, arguments, caller's memory and choices, since the start gives every
thread the one program (Grid.start_ctx).
The proof rests on Grid.fire_frame: a call that completes for n has a list P of participants,
each waiting (at a barrier, n's block of threads n / size * size + j, j < size), with
results v, such that
- the new threads are
resumeAll l g.mem g.threads P v, withlthe layout ofn's context; - the new memory is the old with
Grid.fired L g nin front of its trace; - every participant's fire, in all contexts that give it
n's layout, gives the same grid; - from every grid with
g's statuses atnand at the participants, the call gives the sameresumeAllon that grid's threads and memory, and the same log.
Then:
- If the participant lists meet, or
jis amongn's participants, ornamongj's, then one thread is a participant of both calls, and its fire givesgnandgjalike, sogn = gj(this needshl). - Otherwise each call keeps the other's participants (
resumeAll_getElem?), so it still completes after the other with the same results. The blocks do not change, and in the memory each call leaveswithAttrsgives what it gave (resumeAll_agree, byMem.Keeps). The threads agree at every number (Array.ext_getElem?), and the traces differ by the order of the two logs. Both logs are.barriers (Grid.mem_fired), which are independent, so the traces areTrace.Same(Trace.Same.swap).
A launch whose block of threads has no threads (size = 0) has no participant at a barrier, so
n ∉ P. The case split covers it by j ∈ P ∨ n ∈ Q.
Its instances (MojoProofTest/Confluence.lean):
bars_diamond: two threads at barrier 0, one thread per block of threads, where the fires give grids that differ and commute;bars_sameandshfls_same: both threads in one block of threads, or one warp, where either fire gives one grid;start_bars: the start ofstartBafter thread 1 reaches the barrier too (Grid.start_fire_fire_comm).
The twin of hl (layout_twin): two lanes at a shuffle of a nonnull pointer at address
2 ^ 32, thread 0 with 64-bit pointers and thread 1 with 32-bit ones. Thread 0's fire keeps the
pointer, thread 1's makes it poison, and after either fire no thread waits, so neither
alternative holds.
The premise on accesses from ownership in Lean (done for every grid). Grid.pending_apart
(Confluence.lean) is the disjointness argument above, for the two threads' pending accesses: for
every discipline (T, own), contexts and grid, hH at the two threads and hX at their contexts'
threads give hW.
theorem Grid.pending_apart {ι : Type} {T : Nat → Option (Tiling ι)} {own : Nat → ι}
{c : Nat → Ctx} {g gn gk : Grid} {n k : Nat}
(hn : g.advance c n = .ok (some gn)) (hk : g.advance c k = .ok (some gk))
(hX : own (c n).who.thread ≠ own (c k).who.thread)
(hH : ∀ p ∈ [n, k], ∀ a, .access a ∈ g.pending c p →
Owned T own a ∨ g.mem.Owned (c p).who.thread a.ptr.block) :
∀ a a', .access a ∈ g.pending c n → .access a' ∈ g.pending c k →
a.overlaps a' = true → a.write = false ∧ a'.write = false
The proof, for overlapping a of n and a' of k (so of one block):
- Both follow the discipline: they are by
(c n).whoand(c k).who(Grid.pending_who), whose tiles differ, so they are reads (Owned.apart, Trace.lean). At an untiled block both are reads (Owned.read). At a tiled one they lie in different tiles, which are disjoint (Tiling.disj), so they do not overlap.Owned.apartreplacesOwned.conflictand givesracy_eq_false_of_phasedtoo. - One is to a block that its thread owns,
.threadof that thread: the other thread's step succeeds, so its access is to a block whose owner admits it (Grid.advance_admits, fromMem.check_block, which also gives the admission), so the two threads' numbers are equal and their tiles too, againsthX.
From the start (Grid.start_reach_comm_of_owned): Grid.start_reach_comm with hW from
Grid.pending_apart through Access.conflict_eq_false (accesses that overlap only if neither
writes do not conflict), hX as own n ≠ own k (the start gives thread n the number n,
Grid.start_ctx), and the premise n ≠ k from it.
theorem Grid.start_reach_comm_of_owned {ι : Type} {T : Nat → Option (Tiling ι)}
{own : Nat → ι} {prog : Program} {fn : Func} {L : Launch} {args : List Val}
{m : Mem} {ch : Nat → Nat} {oracle : Nat → Nat → Nat} {g g' gn gk : Grid}
{c : Nat → Ctx} {n k : Nat} (h : Grid.start prog fn L args m ch oracle = .ok (g, c))
(hm : m.BytesIn m.Known) (hr : Relation.ReflTransGen (Grid.Step L c) g g')
(hX : own n ≠ own k)
(hn : g'.advance c n = .ok (some gn)) (hk : g'.advance c k = .ok (some gk))
(hH : ∀ p ∈ [n, k], ∀ a, .access a ∈ g'.pending c p →
Owned T own a ∨ g'.mem.Owned p a.ptr.block) :
∃ g₁ g₂, gn.advance c k = .ok (some g₁) ∧ gk.advance c n = .ok (some g₂) ∧
g₁.Same g₂
hH here is at the two threads of one grid. hH above, at every running thread of every reachable
grid, gives it. hI is not needed: invariant loads are reads.
Its instances (MojoProofTest/Confluence.lean):
stores_apart:hWonstores 4from the discipline(tiles, tileOf)that cuts block 1 into two tiles of 4 bytes (Tiling.strided 0 2 4), tile 0 for thread 0;- an
examplewithstart_diamond's statement: its stores are to the threads' ownallocas, which they own.
The twins (apart_twins), on stores 0, where hW and the conclusion fail (diamond_twin):
- of
hX: with every thread in tile 0 both stores follow the discipline; - of
hH: withtileOfthread 1's store is neither in its tile nor to a block it owns.
Two atomicrmws of one commuting operation on the same bytes (L10, done): Access.conflict exempts them,
so Grid.advance_comm covers them, but they follow no discipline with different tiles, so
Grid.start_reach_comm_of_owned does not. The steps, in Lean: Mem.load_store (Confluence.lean), for every layout, memory, two threads, type,
value, pointer and two alignments, gives that a load after a store that succeeds, at the same
pointer, by a thread and with an alignment whose check succeeds before the store, gives what the
stored bytes decode to, and Mem.load_store_codec that for a codec (integers, floats) this is the
stored value. So in the diamond the second thread's load, which succeeds from g, reads what the
first stored. Instances store_load, store_load_other; twins store_load_i0 (a poison i0
loads back as bits 0 0) and load_other (of the check). rmw_comm_of_ok (Rmw.lean) gives the final value from a
scalar for all operands, if both updates succeed from it, since an operand need not be a scalar.
Mem.load_scalar gives that the loaded value of an integer type is Val.Scalar (instance
load_scalar; twin load_ptr), and Layout.scalar_round that a scalar of a positive width
round-trips through its bytes (instance undef_round; twins poison_i0, ptr_i32). From these,
Mem.update_comm: for every two layouts, memory, threads, spaces, operands and alignments, every
operation and every positive width n, two updates (Mem.update, what exec does at an
atomicrmw) of type .int n at one pointer that each succeed from m succeed in both orders and
give the same blocks, next and trace up to swaps (instance update_comm, two adds). Then
exec_atomic, step_atomic and Grid.advance_atomic: a step that succeeds and would log an atomic
access e is, from every memory with the same shape, the update at e's pointer of type
.int (8 * e.len) (Stmt.checkAtomic_int) by an operand that is a scalar of that width (exec
checks it); and Grid.advance_atomic_comm, for every closed grid
and contexts: two threads n ≠ k whose steps succeed, with pending accesses by atomicrmws of one
operation at one pointer and size, give the same grid in both orders (Grid.Same) (instance
atomic_comm, two atomicrmw adds at one word; with Grid.advance_comm, atomic_diamond).
bad_step (P, faults persist). Let g fault at n and let g → g'.
n's advance faults. Thennruns and the step is by another thread or a fire.- Another thread
m: by disjointness,m's step changes none ofn's footprint.n's pending reads are owned or thread-local byhH, andm's writes miss them. Liveness, and the byteslifetime.startresets, change only form's own blocks, and an access bynto one of them faults in every state (Mem.check). The new block, if any, is onendoes not name (C2); a pointer ofnto no block keeps pointing to no block (C1), and anallocaofnwhose number is in use keeps it in use (no block is removed). Son's step fromg'faults too (L2). - A fire:
nis not a participant, and the fire changes no block.
- Another thread
-
n's fire faults. Thennwaits, and no step but a fire ofn's group movesn. That fire isn's fire up to the participant it is checked against, and it faults too.Grid.firefaults for one of four reasons.- Its own operands: the barrier number is not defined or is outside 0–15, or the member mask is not
defined or is partial. These do not change while
nwaits. - A returned participant.
.doneis final. - A participant
pthat waits at an incompatible call.pmoves only by a fire that includes it.- A barrier fire of
p's block includesn. It faults ifnwaits at another barrier, and it is not enabled ifnwaits at a shuffle (pure false). - A shuffle fire of
p's warp includesnand faults against it. Sopkeeps its state.
- A barrier fire of
- A lane at the same shuffle whose lane or clamp operand is undefined, or a fault of
withAttrsinresumeAll. The first is that lane's own operands, which do not change while it waits. The second reads only block addresses (Mem.addr), which never change (W5). A barrier resumes with no value and never callswithAttrs.
Grid.firechecks the participants in order, and an earlier participant can change from running to waiting ing'. Then the fire faults at that participant, or it reaches the same fault as ing. Either wayg'faults. - Its own operands: the barrier number is not defined or is outside 0–15, or the member mask is not
defined or is partial. These do not change while
Conclusion 2. By induction on reachability:
- every access in a reachable trace was logged by a successful step from a reachable grid, so it is a
pending access there (L1a), and owned or thread-local (
hH); - the thread-local ones are by the owner of their block (
Mem.check).
Two accesses of different threads to one block are therefore both owned. They lie in different tiles
(hX), so they do not race; this is racy_eq_false_of_owned with thread-local accesses added, a small
generalization. An invariant load is on an untiled block (hI), which no access writes (Owned.read), so
kept holds (kept_of_split).
In detail, the invariant, for every grid g reachable from g₀:
- every thread number
jofgis belowN; - for every access
aing.mem.trace:a.who.thread < N, and blocka.ptr.blockofg.memis somexwhose owner admitsa.who(Owner.admits), withOwned T own aorx.owner = .thread a.who.thread, and, ifa.invariant = true,T a.ptr.block = noneandx.owneris no.thread w.
At the start the trace is empty. A step of thread n (Grid.advance_set) keeps the size of the
threads and logs Grid.pending c g n in front of the trace. Each of these accesses is by (c n).who
(Grid.pending_who), whose thread is n (Grid.start_ctx), below N since n is a thread of g; it
is to a block whose owner admits n (Grid.advance_admits); hH gives the third part, and hI the
fourth, since an owner .thread w that admits n is .thread n. The step keeps every block's owner
(Grid.start_reach_changes, Mem.Changes.old), so the accesses already in the trace keep theirs. A
fire logs no access (Grid.fire_trace), changes no block (Grid.fire_blocks) and adds no thread
(Grid.fire_threads).
From the invariant: a clash of the trace (racy_iff) is of two accesses of different threads to one
block. If the block's owner is a .thread w, both threads are w (the owner admits them); otherwise
both accesses are owned, and Owned.apart with hX makes both reads, which do not conflict
(racy_eq_false_of_local, with "local to w" being g.mem.Owned w). For kept (the core's at
sig), kept_of_split with W b := T b ≠ none ∨ ∃ w, g.mem.Owned w b: an access that writes for
the core (a write or an atomicrmw's read) has a write with its pointer in the trace, which is
shaped (Trace.Shaped.writes, Grid.start_reach_shaped); that write is owned, so to a tiled
block (Owned.read), or thread-local; an invariant load is to an untiled block with no thread
owner.
The enabled race: if g has a running thread n with racy (Thread.pending (c n) t g.mem ++
g.mem.trace) = true and n's step succeeds, the grid after is reachable and its trace is that list
(Grid.advance_set), which is not racy by the above; a running thread's step is never none
(Grid.advance_run), so it faults.
Conclusion 3 in Lean
Statement (Grid.owned_sound, MojoProof/Sem/ScheduleOwned.lean). For every program, kernel,
launch, arguments, address choice, oracle and discipline (T, own), given hs, T2's hm, an
observation obs with obs m = obs m' whenever m.blocks = m'.blocks, a run
Path (Grid.Step L c) n g₀ z with GPUProof.Done sig (z, L, c), a predicate loc b w ("block b is local to thread
w"), and obs z.mem = some y with R y, if the trace tr := z.mem.trace meets
hO: every access oftrto a block local to no thread follows the discipline (Owned T own a);hL: every access oftrto a block local towis byw;hX: two accesses oftrto one tiled block (T b ≠ none) by threads with one tile (own) are by one thread;hI: no access oftrwith!invariant.loadis to the block of a write oftr,
then Grid.Sound L c g₀ obs R.
Proof.
- A done grid is bad only by its trace (
Grid.Done.bad_iff, every launch, contexts and grid with at most the launch's number of threads;Grid.start_reach_sizegiveszexactly that many). The core'sBadatsigisFaults ∨ racy ∨ ¬ kept ∨ RaceEnabled. A fault is a thread's step or fire that errs, and in a done grid every thread is.doneor absent, soGrid.advanceandGrid.firereturn.ok none(done_not_faults). An enabled race needs a thread.run tor a fire that completes (Grid.raceEnabled_iff), which a done grid has not (Grid.Done.not_raceEnabled). - The trace lemma (
racy_kept_of_local, every launch and every trace whose oldest-first form is shaped,Trace.Shaped tr.reverse).racy tr = falseisracy_eq_false_of_localfromhO,hLandhX. For the core'skeptatsig, letW bsay that some write oftris to blockb. Every access that writes for the core is to a block ofW: a write by definition, anatomicrmw's read by the write right after it (Trace.Shaped.writes); every access with!invariant.loadis to a block not inWbyhI; that iskept_of_split. - So
zis not bad: by 1, a badzhasracy tr = trueor a broken promise, and 2 refutes both, its shape fromGrid.start_reach_shaped.Grid.start_sound(T2's conclusion 2) then givesGrid.Soundfrom the run,hs,hmand the observation's premises.
Relation to the statement above. The hypotheses hH, hI and hX of T2′ give the trace
premises at every reachable grid with loc b w := g.mem.Owned w b (the argument of
Grid.owned_clean: an owned block's owner admits only its thread, and a write that follows the
discipline is to a tiled block, which an access with !invariant.load is not to). The Lean form
asks them of one trace only, which a checker evaluates on one run.
Instance and twin (MojoProofTest/ScheduleOwned.lean, one kernel each). cells_sound: the run
of cellK i16 in which thread 0 takes its four steps and then thread 1, with T := cells,
own := tileOf and no local block; the trace premises are a Boolean check on the done grid's trace
(traceB, sound for every discipline and trace by traceB_sound), and the observation is bytes 0
to 3 of block 1, 7 and 7 as 16-bit words. cells_sound_twin: in cellK i8 the same schedule
reaches a done grid with a racy trace, so it is bad, no (T, own, loc) meets the trace premises
(they would make it not racy), and the start's grid is sound for no observation.
The trace lemma of an elementwise kernel
Statement (racy_kept_of_cells, MojoProof/Sem/Trace.lean, every trace tr, output
block out, width w and cell map cell : Nat → Nat with Apart cell w, that is, for threads
i ≠ j, cell i + w ≤ cell j or cell j + w ≤ cell i). If
hc: every accessaoftrtooutlies in its thread's cell:cell a.who.thread ≤ a.ptr.offanda.ptr.off + a.len ≤ cell a.who.thread + w;hr: every access oftrto another block is a read that is not anatomicrmw's (a.write = false ∧ a.atomic = none);hi: no access oftrwith!invariant.loadis toout,
then, at every launch L, tr has no race when it is by threads of L in their blocks of
threads and shaped (the core's racy at sig, racy_iff's premises), and the core's kept at
sig keeps the trace's core events (GPUProof.kept sig ((tr.map (Event.toCore L)).reverse) =
true). This is the premise (hU → Trace.Shaped m'.trace.reverse →
GPUProof.racy sig ((m'.trace.map (Event.toCore L)).reverse) = false) ∧
GPUProof.kept sig ((m'.trace.map (Event.toCore L)).reverse) = true of a sequential run's
soundness, for a kernel in which each thread writes
only its own cells of one output block and reads blocks that nobody writes. It covers cells at
any offsets w bytes apart (a row-major tensor from any base offset) and accesses inside a cell
(a narrower load or store of it). racy_kept_pointwise (same file: cells k * w, each access the
whole cell, threads below a bound) is its corollary, with Apart from apart_stride.
Proof.
kept. LetW bsayb = out. Every access that writes for the core (a write or anatomicrmw's read,Access.kind_writes) is toout: one to another block contradictshr. Every access with!invariant.loadis to a block other thanoutbyhi. That iskept_of_split.racy. Supposeracy tr = true. Byracy_iffthere are two accessesx,yof the phases oftr, so oftr(mem_phases), that clash, which needs (among its conditions) different threadsi ≠ j, one block, overlapping bytes (x.off < y.off + y.lenandy.off < x.off + x.len), and at least one write.- If the block is
out, byhcx's bytes are inside[cell i, cell i + w)andy's inside[cell j, cell j + w). ByApartthese two intervals are disjoint, soxandydo not overlap: a contradiction (linear arithmetic). - Otherwise both accesses are reads by
hr, and a clash needs a write: a contradiction.
- If the block is
Instance and twins (MojoProofTest/ScheduleOwned.lean, statements about one trace). elemTr o:
two threads, each an !invariant.load of its 4 bytes of block 2 and a store of 4 bytes at offset
o n of block 1. elem_clean (every launch): with o n = 4 n + 8 and cells 4 n + 8 of width 4 (apart by
linear arithmetic, an offset racy_kept_pointwise does not cover), the premises hold for each of
the four accesses by evaluation, so the trace is not racy and keeps the promise. elem_owned:
the same trace follows the tiling elemCells (block 1 in two cells of 4 bytes from 8), so
racy_eq_false_of_owned also gives that it is not racy.
elem_twin_cell (the cell premise): with both stores at offset 8 the trace is racy.
elem_twin_inv (the !invariant.load premise): after a further !invariant.load of bytes 8 to
11 of block 1, which thread 0 stores, kept is false.
Route in Lean
T2 (Grid.confluent) gives Confluent rc Same Bad on γ from the start alone, with the core's
Bad at sig. A general lemma of the core, Confluent.transfer (GPUProof/Transfer.lean), moves
confluence along a surjection that maps the steps, the equivalence and the bad set; along the
identity it replaces the bad set of a confluent system by one with the same members. Conclusion 2
makes the core's Bad and the faults (GPUProof.Faults at sig) agree on γ (Grid.owned_bad),
so conclusion 1 is Grid.confluent with Confluent.transfer along the identity
(Grid.confluent_owned). This route needs hm : m.BytesIn m.Known (T2's hypothesis) and none of
the lemmas of the sections above that T2 does not already use.
Dependencies
Confluent,Confluent.one_run(GPUProof/OneRun.lean);Grid.start,Mem.checkClosed,Mem.allocNext,Grid.start_blocks,Val.bounds,Grid.Step,Grid.advance,Grid.fire,step,Mem.check,Mem.fresh(MojoProof/Sem/Grid.lean, Thread.lean, Value.lean);Mem.Known(MojoProof/Sem/SigDef.lean);Grid.Closed,Thread.AtCall,Grid.AtCalls(MojoProof/Sem/Closed.lean);Access.Writes,Mem.Changes,Mem.Owned,Status.AllocasIn,Grid.Owns(MojoProof/Sem/Writes.lean);Nat.pairand its injectivity (Mathlib);Tiling,Owned,racy_eq_false_of_owned,kept_of_split,Grid.sound_of_one_run,done_not_faults(MojoProof/Sem/Trace.lean);Grid.start_sound,Grid.Done.bad_iff(MojoProof/Sem/Determinism.lean);Grid.faults_iff,Grid.done_iff(MojoProof/Sem/Spec.lean);Trace.Shaped,racy_iff(MojoProof/Sem/RaceSig.lean);Grid.start_reach_size,Grid.start_reach_shaped,Grid.start_reach_fits(MojoProof/Sem/Bad.lean);Trace.Shaped.writes(MojoProof/Sem/ScheduleOwned.lean);GPUProof.FaultsandGPUProof.Done(GPUProof/Run.lean) andGPUProof.racyandGPUProof.kept(GPUProof/Race.lean) atsig;List.Perm,Relation.ReflTransGen(Lean core, Mathlib).
Instances and twins
Given (MojoProofTest/ScheduleOwned.lean, one kernel each, by evaluation of every reachable grid with
the checker every, whose soundness every_sound holds for every launch):
- the checker
discB c T own, which giveshHandhIfor the discipline(T, own)on every grid that meets it (discB_sound,disc_hyps, for every launch, contexts and discipline); - the instance
confluent_owned2andclean2:start2of MojoProofTest/Confluence.lean, two threads each at%0 = alloca i32,store i32 7, ptr %0,ret void. Every access of every reachable grid is to a block its thread owns (local2,discBwithT = noneandown = id), so the steps are confluent with the faults as the bad set, and on every reachable grid the trace is not racy, keeps its promises, and the grid is bad exactly when it faults; - the tiled instance
confluent_cells:cellK i16, two threads each at%1 = call i32 @llvm.nvvm.read.ptx.sreg.tid.x(),%2 = getelementptr inbounds i16, ptr addrspace(1) %0, i32 %1,store i16 7, ptr addrspace(1) %2, align 1,ret void, launched byGrid.startwith a pointer to the caller's buffer, block 1 of 4 bytes. WithTtiling block 1 into two cells of 2 bytes (Tiling.strided 0 2 2) andown = tileOf, every reachable grid meetsdiscB, each store being in its thread's cell, and does not fault (cells16), sohH,hIandhXhold, the steps are confluent with the faults as the bad set, and every reachable grid's trace is not racy and keeps its promises. Its twincells_twin: incellK i8the cells are one byte apart and the two stores of two bytes overlap, the grid after both stores is reachable with a racy trace, so no discipline whose tile map is injective on the two threads meetshHandhI; - the twin
inv_twin(counterexample (ii): a broken!invariant.loadpromise): one thread at%0 = alloca i32,%1 = load i32, ptr %0, !invariant.load,store i32 7, ptr %0,ret void. No reachable grid faults or has a racy trace, but the done grid breaks the promise of the load, so it is bad and does not fault, and no discipline meetshHandhIon its launch. The load's step is evaluated after unfoldingLayout.decodeonce (stepsI), and the checker runs from the grid after the load (reachI,safeL); - the instance
cells_soundof conclusion 3 (Grid.owned_sound) and its twincells_sound_twin, section "Conclusion 3 in Lean".
Not given here:
vector_additionon a 2 × 2 launch.- The tiling is
Tiling.stridedon the output buffer, andT = noneon the inputs (read only,!invariant.load).ownis the global thread index, injective on the threads of a one-dimensional launch (Launch.index_oneDim, index.md). hHis checked on the reachable grids by the summary of one thread, which is T3 and T4's job, so this instance is not given.racy_kept_pointwisecovers the trace half for this kernel.
- The tiling is
- Negative twins, which are not instances:
reduction: all block leaders write one word, sohXfails, and the commutation goes by L10;tiled_matmul: a shared tile is written by different threads in different barrier intervals, soOwnedfails for any oneT, and the proof goes by race freedom per barrier interval (barrier-owned.md).