Schedule independence (T2, Grid.confluent)
Lean, in MojoProof/Sem/Schedule.lean unless named: changes S1 and S2 are made; Trace.Same,
Grid.Same and Thread.pending are defined (in MojoProof/Sem/SigDef.lean, since sig reaches
them); Trace.Same and Grid.Same have refl, symm and trans; L1a for steps is proved
(step_pending, for every context, thread and memory, and Grid.advance_pending, for every
grid), and for fires (Grid.fire_trace, for every grid), and no step or fire reads the
trace (exec_retrace, step_retrace, Grid.advance_retrace, Grid.fire_retrace), with its
corollaries bisim (Grid.Same.step) and the fault part of bad_eqv (Grid.Same.faults); L1c
for the memory (Thread.pending_shape; with C1, a running thread of a closed grid has the same
pending accesses in every reachable grid, Grid.pending_reach), and W5 (Grid.reach_keeps:
reachable grids keep every block; Grid.start_reach_atCalls: a waiting thread of a grid reachable
from the start is at its call); L9 (racy_iff, RaceSig.lean), L11 (Trace.Same.append,
Trace.Same.swap), L12 (Trace.Same.clash, Trace.Same.kept), L13 (racy_insert) and L10
(rmw_comm on values, Mem.update_comm on memory) are proved, each for every trace, list,
operation and width it names. In MojoProof/Sem/Bad.lean, at the core's Bad at sig
(Grid.raceEnabled_iff reads its RaceEnabled): bad_eqv is proved (Grid.Same.bad, for
every launch, contexts and grids), and
bad_step (Grid.Step.bad, for every launch, contexts with one layout and closed grid whose
threads own their allocas and have different thread numbers, in which no collective call that
completes logs a barrier of a running thread's block of threads; a fault at a collective call
persists, Grid.fire_error_advance, Grid.fire_error_fire; a fault persists after an exempt
update, Grid.advance_fault_atomic, by exec's check that an atomicrmw's operand is a scalar
of its type; and Grid.start_reach_bad in every grid reachable from the start; the premise of one
layout is needed, twin fire_fault_twin, the check, twin update_before, and the closed grid,
twin closed_twin, counterexample (i)). L3 holds by construction. Grid.confluent (conclusion 1)
and Grid.start_sound (conclusion 2) are proved, for every program, kernel, launch, arguments,
address choice and oracle on a caller's memory whose pointer bytes name known numbers
(Mem.BytesIn Mem.Known), in MojoProof/Sem/Determinism.lean, with the diamond's case of two
threads with conflicting pending accesses as two grids with a race enabled
(Grid.advance_raceEnabled, Bad.lean); instances and twins in MojoProofTest/Determinism.lean.
Conclusion 3 is Grid.start_refute; conclusion 4 (T2′) is Grid.confluent_owned in
MojoProof/Sem/ScheduleOwned.lean, by the route of item 4 below.
The semantics this proof uses: an alloca's block number is Mem.fresh (so no renaming of
blocks), the start checks the caller's memory (Mem.checkClosed) and every reachable grid is
closed (Grid.Closed, schedule-owned.md), lifetime.start resets the bytes of a block (L1b), and
two atomicrmws are exempt from Access.conflict only if they have one commuting operation
(RmwOp.commutes), pointer and size.
Scope. Every program, launch, start memory, argument values, skeleton choice and oracle. The
hypotheses are that Grid.start succeeds and that the caller's memory is closed for the arguments
(Mem.Closed). The start checks the fields of Mem.Closed except above (no block from next
on), which no function can check, and except the pointer bytes of the blocks from next on, which
above says do not exist (Mem.checkClosed; a closed memory passes, Mem.checkClosed_of_closed);
it deletes no block and refuses an allocation at a number that has one (Mem.allocNext), so it
keeps every block of the caller's memory (Grid.start_blocks, proved). Kernels with conflicting
accesses, with atomics on one location (reduction) and with tiles that change hands at barriers
(tiled_matmul) are all covered: a schedule dependence shows up as a bad grid. T2 says that the
schedule does not matter up to bad grids; it does not say that a kernel is race free. That is a
property of one run, which T2 transfers to all runs.
Design choices.
- No renaming of block numbers (
Mem.fresh), so the equivalenceGrid.Sameis equality up to the order of independent trace events. - The race predicate is
racyitself, with its exemption of two atomics of one commuting operation (RmwOp.commutes), pointer and size: such atomics commute (L10 below). Twoxchgs of the same bytes conflict. - W1 is
Grid.Closed(schedule-owned.md, section "The closed-grid invariant"). Confluentneeds one more alternative in its diamond (change S1 below).- One change to the semantics (change S2 below): an admitted
atomicrmwleaves its result register unset.
Changes it needs first
-
S1 (
Confluent, GPUProof/OneRun.lean).diamondgains the alternativeBad b ∧ Bad c:diamond : ∀ {a b c}, r a b → r a c → e b c ∨ (Bad b ∧ Bad c) ∨ ∃ d d', r b d ∧ r c d' ∧ (e d d' ∨ (Bad d ∧ Bad d'))Two conflicting steps from one grid give grids that are both bad at once (a race is enabled in each), but the second step need not be possible from either: it can fault.
one_runstill holds. Inkey, the third case hasBad a₁'for the second step's targeta₁';path_eqvcarries the runa₁ → … → ztoa₁', andbad_pathmakes its end bad, against¬ Bad z. This is two lines inkey. Of the instances, the one inGrid.sound_returning(MojoProof/Sem/Trace.lean) takes the third alternative (one-run.md) andGrid.confluent(MojoProof/Sem/Determinism.lean) all three. (Lean:Confluent.diamond,Confluent.key.) -
S2 (
exec, MojoProof/Sem/Thread.lean). An admittedatomicrmwsets its result register to the old value, which depends on the order of the atomics.Stmt.checkAtomicrefuses a used result, so no instruction reads that register, but the two orders give threads that differ in it, andGrid.Samecompares threads by equality. The step leaves the register unset instead. Nothing reads it, so leaving it unset changes no observation; Eval's atomic tests check it. (Lean:exec; EvalregsAfter, two threads'atomicrmw addin either order give the same registers, and without S2 that check fails.)
Definitions the proof adds
Thread.pending (c : Ctx) (t : Thread) (m : Mem) : List Event, as in schedule-owned.md: the accesses the step oftwould log, in trace order (newest first), whether the step then succeeds or faults. One definition serves T2 and T2′. (Proved, withStmt.pendingfor one statement, and L1a for steps:step_pending, a step that succeeds puts exactly these in front of the trace, and one that stops at a collective has none;Grid.advance_pending, a grid step of threadnis a step of a running thread and puts its pending accesses in front of the trace;Grid.fire_trace, a fire logs one.barrierof the thread's block at a barrier and nothing at another collective; instance and twin MojoProofTest/Eval.leanpendingTests: two threads' 10 accesses on one launch, and a load through null, whose step faults with its access pending.)Trace.Same tr tr':(phases tr).1is a permutation of(phases tr').1, and(phases tr).2.getD k 0 = (phases tr').2.getD k 0for every block of threadsk. It is the order of the trace forgotten up to the phase of each access (phases, MojoProof/Sem/SigDef.lean). (Proved as stated.)Grid.Same g g':g.threads = g'.threads,g.mem.blocks = g'.mem.blocks,g.mem.next = g'.mem.next, andTrace.Same g.mem.trace g'.mem.trace. It replaces schedule-owned.md'sList.Perm(whichTrace.Sameimplies for the accesses), so T2 and T2′ share it. (Proved as stated, withGrid.Same.refl,symm,trans; instance and twin MojoProofTest/Same.leangrids,grid_twin.)- A race is enabled in
g,GPUProof.RaceEnabled sig (g, L, c), when some threadnruns,g.threads[n]? = some (.run t), andracy (Thread.pending (c n) t g.mem ++ g.mem.trace) = true, or some fire ofncompletes andracy g.mem.trace = true(Grid.raceEnabled_iff, MojoProof/Sem/Bad.lean; instance MojoProofTest/Bad.leanrace_enabled). gis bad,GPUProof.Bad sig (g, L, c), whenGPUProof.Faults sig (g, L, c),racy g.mem.trace = true,GPUProof.kept sig ((g.mem.trace.map (Event.toCore L)).reverse) = falseor a race is enabled ing: the core'sBadatsig, with its faults andkept(Grid.faults_iff,kept_sig_iff).
Statement
Data. hs : Grid.start prog fn L args m ch oracle = .ok (g₀, c). 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.
Conclusions.
Grid.confluent hs : Confluent rc (fun x y => Grid.Same x.1 y.1) (fun x => GPUProof.Bad sig (x.1, L, c)). (Proved, with the caller's hypothesishm : m.BytesIn m.Known; instanceconfluent2.)- Through
Confluent.one_runandGrid.sound_of_one_run: if one run fromg₀reaches a done gridzwith a race-free trace that keeps!invariant.load(a done grid does not fault,done_not_faults, and has no running thread and no fire that completes, so no enabled race), thenGrid.Sound L c g₀ obs Rfor everyobsthat reads onlyMem.blocks, withRthe property ofobs z.mem. This is a statement about every schedule from one run; whether the run exists is a fact about one kernel and one input. (Lean:Grid.start_sound, taking the run as aPathwith¬ GPUProof.Bad sig (z, L, c); instancessound2andfinal2onstart2, two threads'allocaand store; twinsracy_twin, a done grid with a racy trace, so sound for no observation, andtrace_twin, an observation of the trace's newest event, for which two schedules differ, so sound for no relation.) - Refutation transfer, the same theorem read backwards: if some reachable grid is bad (a fault, a race or a
broken promise in one schedule), then every reachable grid without a step is bad. No schedule hides it.
(Lean:
Grid.start_refutebyConfluent.bad_normal; instancerefutePonstartP, twinrefute_twin: onstart2,done2has no step and is not bad.) - T2′ (schedule-owned.md) becomes a corollary. Under
hH,hXandhI, no reachable grid has a racy trace, a broken promise or an enabled race (schedule-owned.md, conclusion 2, applied to the trace with the pending accesses in front), so on the statesγBadisFaults, andConfluentwithBad := Faultsfollows by a general lemma:Confluent r e Band∀ a, B a ↔ B' agiveConfluent r e B'. (Lean:Confluent.transferalong the identity, GPUProof/Transfer.lean;Grid.owned_bad,Grid.confluent_owned, in MojoProof/Sem/ScheduleOwned.lean;hXthere isowninjective on the start grid's thread numbers, and the caller's memory meets T2'sMem.BytesIn Mem.Known.)
Lemmas used
From schedule-owned.md, unchanged: L1a (steps and fires do not read the trace; a successful step logs
exactly its pending accesses, proved as step_pending and, for the grid, Grid.advance_pending; a barrier fire logs one .barrier,
proved as Grid.fire_trace), L1b (what a step changes, including the bytes of the thread's own blocks
that lifetime.start makes undefined; done for every step in every grid
reachable from the start, Grid.start_reach_changes, MojoProof/Sem/Writes.lean), L2 (frame; done
for statements and steps, exec_agree, step_agree and step_fault, and the memory part of the
no-conflict argument below, Mem.Changes.agree, MojoProof/Sem/Frame.lean; that changes at
disjoint places commute, Mem.run_comm, and the case of two threads' steps of the diamond if no
pending access of either conflicts with one of the other (Access.conflict), Grid.advance_comm,
which covers both of its subcases below (no write of one overlaps an access of the other; or an
exempt pair of atomicrmws, Grid.advance_atomic_comm), and in every grid reachable from the
start for every two different threads, Grid.start_reach_comm; the premise of the first subcase
from ownership, if each of the two threads' pending accesses follows a discipline with different
tiles or is to a block its thread owns, Grid.pending_apart, so from the start
Grid.start_reach_comm_of_owned; its case of a thread's step and a collective call, a
barrier or a shuffle, for every grid, if the thread's context is not of the barrier's block of
threads, Grid.advance_fire_comm, and in every grid reachable from the start,
Grid.start_reach_fire_comm; its case of two collective calls, for every grid and contexts in
which the two threads have one layout, Grid.fire_fire_comm, so for every grid in the start's
contexts, Grid.start_fire_fire_comm, MojoProof/Sem/Confluence.lean),
L3 (choices), W2, W5, and Grid.Closed with C1 and C2 (which hold of every reachable grid; their proof does
not use ownership). New:
- L1c (pending):
Thread.pending (c n) t mreadsmonly at the existence, space, size and addresses of the blocks thattnames (a pointer operand is evaluated from registers, globals and constants;gepandptrtointreadMem.blockandMem.addr). These never change in reachable grids: a named block keeps them (W5), and a named number without a block never gets one (C1). So a thread's pending accesses are the same in every reachable grid in which its state ist. (Proved for the memory, for every context, thread and setSof block numbers: if the scope oft's innermost frame names only numbers ofS(Scope.NamesIn: null, the pointers in its registers,Val.NamesIn, and the globals) andm,m'have the same existence, space, size and addresses at every number ofS(Mem.SameShape), thent.pending c m = t.pending c m'(Thread.pending_shape, fromevalV_shape, which also says that evaluation names only numbers ofS). W5's block part and C1's part about memory are done:Mem.setLive,Mem.log, a successfulMem.writeandMem.store, andMem.allocat a number fromnexton with no block keep every block that exists, at its number and with its shape, keepnext, and add no block below it (Mem.Keeps,Mem.store_keepsand the others), and so do every statement that continues (exec_keeps; anallocaat a number in use is refused, and its numberMem.freshis at leastnext), step (step_keeps,Grid.advance_keeps) and fire (Grid.fire_keeps): every grid reachable fromgkeeps every block ofg, itsnext, and every number belownextwith no block free (Grid.reach_keeps, for every launch and context map). Sogand a grid reachable from it have the same shape at every number ofSif every number ofSwith no block ingis belownext(Mem.Keeps.sameShape). That premise for the numbers a thread names, C1's other part, is done for closed grids: every step and fire keeps the closed-grid invariant of schedule-owned.md (Grid.Closed;Grid.reach_closed, MojoProof/Sem/Closed.lean), and a running thread of a closed grid has the same pending accesses in every grid reachable from it (Grid.pending_reach, for every launch, context map and closed grid). The start's grid is closed if every pointer byte of the caller's memory names a known number (Grid.start_closed; a closed memory has that), so both hold from the start (Grid.start_reach_closed,Grid.start_pending). W5's other part is done: in every grid reachable from the start, every waiting thread is at its collective call (Grid.start_reach_atCalls,Thread.AtCall: its current statement calls the function whose intrinsic it waits at).Scope.NamesInasks forS 0, and number 0 is belownext(at least 1,Mem.Closed), so the premise holds at 0.) - L9 (
racy_iff, proved for every launch and every trace by its threads in their blocks of threads that is shaped, MojoProof/Sem/RaceSig.lean, withraces_ifffor one access andphase_le; it replacesracy_clashandraces_phases, its forward directions): the core'sracyatsigoftr's core events, oldest first, holds exactly when∃ x ∈ (phases tr).1, ∃ y ∈ (phases tr).1, clash x y = true(racy_sig_revto positions, thenpos_iff_phases). MojoProof has noracyof its own; in the rest of this fileracy trstands for the core's atsig. Backward: of two clashing accesses take the newer one; it is by another thread, they conflict, and they are by different blocks of threads or in one phase, in which case no barrier of their block lies between them (a phase is at most the final count,phase_le), soAccess.racesfinds the older one. - L11 (
Trace.Sameis a congruence; proved for all lists: the first half isTrace.Same.append, the secondTrace.Same.swap, with the condition asEvent.indepon each pair, by swapping one event at a time,Trace.Same.swap₁):Trace.Same tr tr'givesTrace.Same (E ++ tr) (E ++ tr')for every listE(phasesfolds from the oldest event, sophases (E ++ tr)is a function ofEandphases trthat commutes with permuting the first part). AndTrace.Same (E ++ F ++ tr) (F ++ E ++ tr)when no event of one ofE,Fis a barrier of the block of threads of an access of the other: such a swap moves no access past a barrier of its own block, so it keeps every access's phase and every count. - L12 (invariance;
Trace.Same.clash,Trace.Same.kept, proved for every trace, withphases_memforkept): clashes by phases andkept(the core's atsig, at every launch) are invariant underTrace.Same, and so isracyat two traces with L9's premises. Forkept, it reads only the accesses of the trace, as a set (kept_sig_iff), andTrace.Samepermutes them. - L13 (inserting events;
racy_insert, proved for all lists, as clashes by phases): ifP ++ trhas two accesses that clash,Pconsists of accesses of one threadw, andQcontains no.barrier w.cta, thenP ++ Q ++ trhas two. By induction onP, throughAccess.races(clash_access). If the clash is intr, it stays (clash_append). If an accessaofPraces with the rest, then it skips the other accesses ofP, which are by its own thread (races_skip), so it races withtr, and events with no barrier of its block of threads keep that race (races_insert). Instance: MojoProofTest/Same.leaninsert; the twin there shows a barrier of the access's block breaksTrace.Same(twin). -
L10 (two exempt atomics commute). Let
f := rmw w opfor an operationopthatStmt.checkAtomicadmits, at a widthwthat it admits. Then for all valuesv x y(bits of widthw, poison or undef):f v xfaults if and only ifvorxis undef, and otherwise its result is bits of widthwor poison (intBin,strict2inintMinMax);- if neither side faults,
f (f v x) y = f (f v y) x(commutativity and associativity of+,&&&,|||,^^^,maxandminonBitVec w, signed and unsigned; poison absorbs).
Lean, for the values, in MojoProof/Sem/Rmw.lean:
rmw_commfor every operation thatStmt.checkAtomicadmits (RmwOp.commutes:add,and,or,xor,max,min,umax,umin), every width and all scalarsVal.Scalar(bits of the width, poison or undef):rmw w op v x >>= (rmw w op · y) = rmw w op v y >>= (rmw w op · x), the same fault or the same value in either order. This one equation gives both items above as they are used: in the diamond, the second step succeeds from either side with the same value; inbad_step, a fault in one order is a fault in the other. It is proved once for every operator that propagates undef and poison as LLVM's do (Lifts,Lifts.comm) with a right-commutative operation on words;intBin_liftsandintMinMax_liftsare its instances, andselect_commgivesmax/minfor every order an injective map toIntreads. Instances: MojoProofTest/Atomics.lean (umax, poison, undef) and its twinadd_or(anaddand anordo not commute).subalso commutes but is not admitted.An operand need not be a scalar of width
w:evalV(Eval.lean) gives a register's value and a global's pointer whatever the type, andrmw w .max .poison (.ptr p)succeeds (strict2). So for the diamond,rmw_comm_of_ok(Rmw.lean): for a scalarvand all operandsx y, ifrmw w op v xandrmw w op v yboth succeed, the two orders give the same value (Lifts.comm_of_ok: from a word, an update that succeeds has a scalar operand,Lifts.scalar_r; from poison it gives poison,Lifts.poison_ok).rmw_liftsgivesLiftsfor every admitted operation, and bothrmw_commandrmw_comm_of_okgo through it. Instancemax_ptr(the pointer operand above); twinmax_faults(from 1, amaxby undef and one by a pointer fault differently in the two orders).On memory: two
atomicrmws by threadsn ≠ mwith operationop, pointerpand sizes(the same type, since the width is a power of two of at least 8) read and write the same bytes. The integer codec round-trips (Layout.Codec.int), so in either order the second reads what the first wrote, and the final bytes are the encoding off (f v x_n) x_m = f (f v x_m) x_n. The checks (Mem.check) depend on the pointer, the size and the alignment, not on the bytes. With S2 neither thread keeps the old value, so the two threads end in the same states in both orders.In Lean so far (Confluence.lean):
Mem.load_store, for every layout, memory, two threads, type, value, pointer and two alignments: after a store that succeeds, a load at the same pointer whose check succeeds before the store (premisehc; the load may be by another thread, at another alignment) succeeds and gives what the stored bytes decode to;Mem.load_store_codec: for a codec (Layout.Codec, integers and floats), that is the stored value.Mem.check_inv(Schedule.lean) is what a check that succeeds checked. Instancesstore_load(ani327) andstore_load_other(thread 1's load at alignment 2 after thread 0's store); twinsstore_load_i0(a poisoni0is stored as no bytes, and the load givesbits 0 0) andload_other(ofhc: in a block owned by thread 0, thread 1's load faults).Mem.load_scalar: a load of an integer type that succeeds gives a scalar of its width (instanceload_scalar; twinload_ptr, a load of a pointer type gives a pointer).Layout.scalar_round: for every layout, a scalar of a positive widthwround-trips through the bytes of.int w, so the first update's result is what the second thread loads (instanceundef_round; twinspoison_i0of0 < wandptr_i32of the scalar).rmw_comm_of_okabove gives the final value.The update itself (Confluence.lean):
Mem.updateis whatexecdoes at anatomicrmwonce its operands are evaluated (a load, its read logged,rmw, a store), andMem.update_eq_oksays when it succeeds (Mem.load_inv,Mem.store_inv: a load or a store that succeeds is its check, then a decode, or an encoding, a put and a log).Mem.update_comm: for every two layouts, memory, two threads, spaces, operands and alignments, every operation and every positive widthn, if two updates of type.int nat one pointer each succeed fromm, each succeeds after the other, and the two orders give the same blocks, the samenextand the same trace up to swaps. In either order the second check succeeds (Mem.check_log,Mem.check_put), the second load reads the first result (rmw_scalar,Layout.scalar_round), the two final values agree (rmw_comm_of_ok), and the second put covers the first (Block.put_put). Instance MojoProofTest/Confluence.leanupdate_comm(twoadds of 1 and 2 by threads 0 and 1); the twin of its one operation isadd_orabove. At width 0 nothing is stored and the conclusion holds too, but its proof differs, andexechas no such width.For steps:
Stmt.checkAtomic_int(an admittedatomicrmwis at.int wwithwa power of two of at least 8, so8 * l.stored (.int w) = w,Layout.eight_mul_stored), andexec_atomic,step_atomic,Grid.advance_atomic: a statement, step or grid step that succeeds and would log an atomic accesseis, from every memory with the same shape, the update of type.int (8 * e.len)ate's pointer bye's operation, with the same operand, a scalar of width8 * e.len(execchecks it), the same space and alignment, then the same thread. SoGrid.advance_atomic_comm: for every closed grid and contexts, two threadsn ≠ kwhose steps succeed and have pending accesses byatomicrmws of one operation at one pointer and size succeed in both orders and give the same grid (Grid.Same). Instance MojoProofTest/Confluence.leanatomic_comm: threads 0 and 1 atatomicrmw adds of 1 and 2 at offset 0 of block 1 (adds). The step is rewritten toMem.updatefor every context and memory, or to the fault at the operand if it is not a scalar of width 32 (exec_rmw,step_rmw,advance_rmw,advance_rmw_fault), and the update shown to succeed byMem.update_eq_ok(add_ok).atomic_diamond: the two threads' pending writes overlap and do not conflict (Access.conflict), andGrid.advance_commgives the same grid.
Proof
refl, symm, trans: equality and List.Perm on (phases tr).1. (Lean: Trace.Same.refl,
symm, trans and Grid.Same.refl, symm, trans.)
bisim. Take Same a a' and a step a → b. By L1a the same thread (or the same fire) steps from a',
since its outcome reads the threads and the memory without the trace, which Same keeps. It logs the same
events E and makes the same changes. So b' has the threads, blocks and next of b, and the traces are
E ++ tr and E ++ tr', which are Trace.Same by L11. (Lean: Grid.Same.step, for every launch and
context map; a grid the same as a is a with its trace replaced, Grid.Same.eq_retrace.)
bad_eqv. Faults reads the threads and the memory without its trace (L1a; done:
Grid.Same.faults). racy and the core's kept at sig are invariant by L12. For
RaceEnabled, the pending accesses are the same in a and a' (same threads, same
blocks), and Trace.Same (P ++ tr) (P ++ tr') by L11, so L12 applies. (Lean: Grid.Same.raceEnabled,
with Thread.pending_retrace: the pending accesses read no trace; Grid.Same.bad, for every launch,
contexts and grids; instance MojoProofTest/Bad.lean same_bad.)
diamond. Take g → b and g → c with g reachable.
- One thread, or one fire.
Grid.advanceandGrid.fireare functions, sob = c. - Two threads
n ≠ mwhose logged accesses conflict. Letn's step logEₙandm's step logEₘ, and letaₙ ∈ Eₙandaₘ ∈ Eₘconflict (Access.conflict). Inbthreadmstill runs with the same state, so its pending accesses areEₘ(L1c), and the trace isEₙ ++ g.mem.trace. InEₘ ++ Eₙ ++ g.mem.trace,aₘraces withaₙ: different threads, a conflict, and only accesses between them. SoRaceEnabled b, and symmetricallyRaceEnabled c. This is the alternativeBad b ∧ Bad cof S1. - Two threads
n ≠ mwhose logged accesses do not conflict. Thenn's writes missm's reads and writes, except where both are by exemptatomicrmws (one commuting operation, pointer and size); and conversely.- If no access pair is exempt,
n's step leavesm's footprint unchanged, as in schedule-owned.md's diamond with "disjointness" replaced by "no conflict": bytes are the pending reads; liveness, and the bytes thatlifetime.startresets (L1b), change only forn's own blocks (W2), andm's access to one of them faults in every state (Mem.check, live or dead), so L2 does not read them; a new block is atm.fresh n k, whichmdoes not name (C2). So by L2msteps frombtodwith the same changes as fromg, and symmetricallynfromctod'. The blocks ofdandd'are equal (the changes touch disjoint bytes, flags and numbers), so are the threads andnext, and the traces areEₘ ++ Eₙ ++ trandEₙ ++ Eₘ ++ tr,Trace.Sameby L11 (accesses only). - If an access pair is exempt, both steps are
atomicrmws with one commuting operation on the same bytes (anatomicrmw's two accesses have its own pointer and size). Every other byte, flag and number is as in the previous case. By L10 the second step succeeds from either side and the final bytes agree; by S2 the threads agree. The traces are as before. (Lean:Grid.advance_atomic_comm, andGrid.advance_commwith the premise that no two pending accesses conflict.)
- If no access pair is exempt,
- A thread's step and a fire. As in schedule-owned.md: an enabled fire has no running participant (L5),
so the thread is not a participant; the fire reads only waiting threads and block addresses (W5) and
changes no block. The two orders give equal threads and blocks, and traces that differ in the order of
one
.barrier kand the thread's accesses. The thread is not in block of threadsk(all ofk's threads wait), so L11 applies. - Two fires. As in schedule-owned.md: disjoint participants commute (traces differ in the order of two
barriers of different blocks of threads, or of none for a shuffle); two barriers that share a thread are
the same barrier of one block, and two shuffles that share a lane resume the same lanes with the same
values, so
b = c. Proved in Lean for every grid and contexts that give the two threads one layout (Grid.fire_fire_comm): with two layouts a shuffle's results can differ undernonnulloralign(MojoProofTest/Confluence.leanlayout_twin).
bad_step. Let g be bad and g → g'.
racyorkept = false. The trace ofg'is the trace ofgwith events in front (L1a);racy_cons,kept_cons. (Lean:Grid.Step.trace,racy_append,kept_append.)RaceEnabled g, at threadnwith pending accessesP. If the step isn's, it succeeds and logsP(L1a), soracy g'.mem.trace. Otherwisenstill runs with the same state and the sameP(L1c), and the trace isQ ++ trwhereQis another thread's accesses, or a.barrier kof a block of threads all of whose threads wait, so notn's. By L13,racy (P ++ Q ++ tr):RaceEnabled g'. (Lean:Grid.Step.raceEnabled, for every closed grid in which no collective call that completes logs a barrier of a running thread's block of threads; that holds when the blocks of threads are not empty and the contexts put each running threadkin block of threadsk / volume L.block(Grid.fired_run), so in a grid reachable from the start (Grid.start_reach_fired), andGrid.start_reach_bad_tracegives the three parts but faults.Grid.Step.raceEnabled_of_ctxtakes the premise on contexts in place of the one on barriers: underGrid.Fitsa running thread's racing accesses are by threads of the launch, so its blocks of threads are not empty. Instances MojoProofTest/Bad.leanfire_race(ofGrid.Step.raceEnabled_of_ctx) andstart_bad; twinfire_race_twin: in contexts that number a running thread as a thread of a waiting block of threads the barrier is the racing thread's, and after it no race is enabled.)Faults g, at threadn's advance. Thennruns, and the step is by another threadmor a fire.- A fire:
nis not a participant, and the fire changes no block. (Lean:Grid.advance_fault_fire, for every launch, contexts and grid.) - Thread
m, whose logged accessesEₘconflict with one ofn's pending accessesP: thenracy (P ++ Eₘ ++ tr)as in the diamond, soRaceEnabled g'. (Lean:racy_conflict, inGrid.Step.advance_fault, when a write ofm's overlaps an access ofn's that is not an exemptatomicrmwwith it.) - Thread
m, no conflict and no exempt pair:m's step leavesn's footprint unchanged (as in the diamond: a reset bym'slifetime.startis in a block ofm, whichncannot access), sonfaults fromg'too (L2). The cases of schedule-owned.md's case P apply: a pointer ofnto no block keeps pointing to no block (C1), anallocanumber in use stays in use. (Lean:Grid.advance_fault, for every closed grid whose threads own their allocas, bystep_fault, when no write ofm's overlaps an access ofn's; with the two cases aboveGrid.Step.advance_fault, and soGrid.Step.badandGrid.start_reach_bad. Instances MojoProofTest/Bad.leanfault_stays,fault_race,fault_bad,start_fault.) - Thread
m, with an exempt pair: both areatomicrmws with one commuting operation on the same bytes, andm's succeeded fromg, so the bytes decode to a value that is not undef, andm's result is not undef either (L10).n'satomicrmwchecks the same pointer (its own alignment and its own thread, which do not change) and reads a value that is not undef in both grids. So by L10 it faults fromg'exactly when it faults fromg: when its own operand is undef, or a check that reads no bytes fails. This needsn's operand to be a scalar of the type's width, whichexecchecks: with a pointer operand,rmwofmaxfaults on an old word and gives poison on an old poison. (Lean:Mem.update_before, for every two layouts, memories, threads, spaces, operations, alignments, pointer and widthn > 0: if an update succeeds after another at the same pointer and width and its operand is a scalar of widthn, it succeeds before it, byrmw_undef,rmw_ok,Lifts.okandLayout.scalar_round; soGrid.advance_fault_atomic, for every closed grid: weren's step to succeed afterm's,step_atomicfromg's memory andMem.update_beforewould make it succeed fromg. Instance MojoProofTest/Bad.leanrmw_fault(onmaxes, thread 0's operand is a pointer and its step faults at the check, before and after thread 1's exempt update of poison); the twin of the check,update_before: without it,Mem.updateby the pointer faults before thread 1's update and succeeds after it.)
- A fire:
Faults g, at a fire of threadn. schedule-owned.md's argument uses no ownership: the fire's operands, a returned participant, an incompatible waiting participant, a lane's operands, orwithAttrson block addresses. Each persists, in contexts with one layout: after a thread's step (Grid.fire_error_advance, for every closed grid: the stepping thread runs, so it is no participant, and the step keeps the shape of every known block, which is allwithAttrsreads) and after another collective call (Grid.fire_error_fire: its threads are none ofn's participants, for a lane ofn's warp or a thread at a barrier ofn's block of threads would completen's call too,Grid.fire_lane; instance MojoProofTest/Bad.leanfire_error_stays, a barrier with a poison number). In contexts with two layouts the fault need not persist, aswithAttrsreads the layout of the thread whose fire it is: MojoProofTest/Bad.leanfire_fault_twin, where thread 1's fire faults under its 32-bit layout, thread 0's completes under its 64-bit one, and the grid after is not bad. The start's contexts have one layout. On a grid that is not closed a fault need not persist: MojoProofTest/Bad.leanclosed_twin, where thread 0'sptrtointof a pointer to block 4, which has no block, faults, thread 1'sallocamakes block 4, and the grid after is not bad. Every grid reachable from the start is closed (Grid.start_reach_closed).
W3 and W4, and the invariants on vadd
The proof does not use W3 or W4. W1 to W5 hold at every grid reachable from the grid Grid.start
returns, for every program, and are checked on vadd on a launch of 2 blocks of 2 threads.
- W3 is
Grid.start_ctx: the start's context of threadnhas the program andwho = ⟨n, n / volume L.block⟩. - W4,
Grid.ByThreads L g: every access event ofg's trace is by a thread number below the number ofg's threads, in block of threadsthread / volume L.block.- Statement.
Grid.Step.byThreads: for every launch, contexts that give threadnthe numbernin block of threadsn / volume L.block, and grids, a step keeps W4.Grid.start_reach_byThreads: for every program, kernel, launch, arguments, memory, address choice and oracle, W4 holds in every grid reachable from the start's. - Proof. A step of thread
nneeds a status atn, sonis below the number of threads. It sets that status alone (set!, which keeps the number) and puts its pending accesses in front of the trace (Grid.advance_set), each by(c n).who(Thread.pending_who), which is⟨n, n / volume L.block⟩. A collective call resumes threads byset!(resumeAll, so the number is kept,resumeAll_size) and logs at most a barrier event, which is no access (Grid.fire_frame,Grid.mem_fired). The start's trace is empty and its contexts are W3's (Grid.start_ctx); induction on the run.
- Statement.
- The instance (MojoProofTest/Start.lean):
vaddon 2 blocks of 2 threads with buffers at blocks 1 to 3 of a caller memory with no pointer byte starts, for every address choice and oracle, with 4 running threads and an empty trace, and every grid reachable from its grid has W1 (Grid.start_reach_closed), W2 (Grid.start_owns,Grid.reach_owns), W3, W4 and W5 (Grid.reach_keeps,Grid.start_reach_atCalls) (invariants). Twins: W1 fails at the start's grid when a byte of the caller's memory points to a block fromnexton (closed_twin); W4 fails after a step in contexts that are not the start's (one block of threads for two threads on a launch of one thread per block,byThreads_twin).
Dependencies
Confluent,Confluent.one_run(GPUProof/OneRun.lean), with S1;Grid.start,Mem.checkClosed,Mem.allocNext,Grid.Step,Grid.advance,Grid.fire,step,exec(with S2),rmw,Stmt.checkAtomic,Mem.check,Mem.fresh,Layout.Codec.int(MojoProof/Sem/Grid.lean, Thread.lean, Value.lean, Buffer.lean);phases(MojoProof/Sem/SigDef.lean);clash(MojoProof/Sem/Race.lean);racy_iff,racy_cons(MojoProof/Sem/RaceSig.lean);Access.conflict,Grid.faults_iff,Grid.done_iff(MojoProof/Sem/Spec.lean);kept_cons,kept_sig_iff,done_not_faults,Grid.sound_of_one_run(MojoProof/Sem/Trace.lean);GPUProof.racyandGPUProof.kept(GPUProof/Race.lean) atsig;- schedule-owned.md:
Thread.pending, L1a, L1b, L2, L3, W2, W5,Grid.Closed, C1, C2; BitVeccommutativity and associativity of+,&&&,|||,^^^, and ofmax/minin both orders (Lean core, Mathlib).
Instances and twins
- The two-thread twins of
trace_examplesas grids: twoatomicrmw adds on one word (the diamond's L10 case: both orders give equal grids), and anaddthen anor(the conflict case: both grids after one step have an enabled race). Evalatomics, twiceandatomics of two operationsrun them. reduction(one launch): with S2 its two orders of two block leaders'atomicrmw addgiveSamegrids.- Negative twin: without S2 the two orders of the first instance differ in the old-value register, so
Samefails; a check that compares the threads of the two orders fails (to add with S2).