Laminar levels, happens-before and use maps
Lean: GPUProof/Laminar.lean and GPUProof/Uses.lean. Tests: the instance Levels toy and
reach_laminar in MojoProofTest/GPUProof/Toy.lean, MojoProofTest/GPUProof/Laminar.lean and
MojoProofTest/GPUProof/Uses.lean. The use maps are the section
Use maps decide race freedom.
Scope. Levels and Laminar are stated for every machine S : Sig; Laminar.agents holds
for every machine; Laminar.nested and HB.laminar_iff hold for every machine with Levels.
The instance and the tests are about one test machine (toy), at every number of levels and
widths for the instance and reach_laminar, and at two sizes for the #guards.
Definitions
Levels S, the laws of the level instances (Sig.group, Sig.levelOf, Sig.members), for
every launch L, levels ℓ ≤ ℓ', instance g and threads t, u:
mem_members:u ∈ S.members L g ↔ u ∈ S.threads L ∧ S.group L (S.levelOf g) u = g;levelOf_group:S.levelOf (S.group L ℓ t) = ℓ;nest:S.group L ℓ t = S.group L ℓ u → S.group L ℓ' t = S.group L ℓ' u;top: fort ∈ S.threads L, the members ofS.group L ⊤ tareS.threads Lup to order;bot: fort ∈ S.threads L, the members ofS.group L ⊥ tare[t];nodup:S.threads LandS.members L ghave no repetition.
These are the six fields the design gives the class, with the same statements.
Laminar S L tr: every event of tr is an access, a fence, or a rendezvous sync o ms for
which some instance g of L has c ∈ ms ↔ ∃ u ∈ S.members L g, c = .thread u for every agent
c. So a laminar trace has no rel and no acq, and every rendezvous in it is one instance's
members, as threads, in any order and with any repetition.
Statements
Laminar.agents(every machine): an event of a laminar trace has one agent, or exactly the members of an instance as its agents.Laminar.nested(every machine withLevels): two events of a laminar trace that share an agent have nested agent lists, each agent of one being an agent of the other.-
HB.laminar_iff(every machine withLevels): forhl : Laminar S L tr,HB S tr i j ↔ i < j ∧ ∃ e e', tr[i]? = some e ∧ tr[j]? = some e' ∧ ((∃ a ∈ e.agents, a ∈ e'.agents) ∨ ∃ k o ms, i < k ∧ k < j ∧ tr[k]? = some (.sync o ms) ∧ (∃ a ∈ e.agents, a ∈ ms) ∧ ∃ b ∈ e'.agents, b ∈ ms)
What the words of the design's statement ("the same agent, or some instance containing both
agents has a sync between them") mean here:
- between: at a position
kwithi < k < j, strictly. A rendezvous atiorjitself shares an agent with the other event when it orders them, so the first case covers it. - containing both agents: the rendezvous at
khas an agent of the event atiand an agent of the event atjamong its agents. ByLaminarits agents are the members of one instanceg, sogcontains an agent of each. For an event with several agents (a rendezvous) one of its agents suffices: byLaminar.nestedthe rest of them then lie ing, orgin them. - the same agent: the events share an agent, at any two positions (program order relates any
two events of one agent,
GPUProof.edge). - jobs: a job is in no instance, since an instance's members are threads, and a laminar trace has no rendezvous of a job. So an event of a job is ordered only with a later event that has that job among its agents: its own accesses and fences.
Proof
Laminar.nested. If either event has one agent, that agent is the shared one and the
inclusion is immediate. Otherwise their agents are the members of instances g and g', with
a common member u. Say S.levelOf g ≤ S.levelOf g' (le_total). By mem_members, u's
instances at those levels are g and g'. A member v of g has v's and u's instances at
g's level equal, so by nest also at g''s level, which is g'; by mem_members, v is a
member of g'.
HB.laminar_iff, right to left. A shared agent is one edge. A rendezvous at k with an
agent of each event gives the edges i → k and k → j, so HB by Relation.TransGen.tail.
Left to right. On a laminar trace an edge is a shared agent: the release-acquire case of
GPUProof.edge needs a rel at its first position, and a laminar trace has none. The invariant,
by induction on the TransGen path from i to m, is: i < m, the events at i and m have
agents, and some position k with i ≤ k ≤ m holds an event whose agents include all the agents
of the event at i and all those of the event at m.
- One edge
i → mwith shared agenta: byLaminar.nestedone of the two events' agent lists contains the other's, and that event's position isk. - A path
i → mwith witnessk, then an edgem → m'with shared agentc. ByLaminar.nestedatmandm', eitherm''s agents lie inm's, hence ink's, andkstays the witness; orm's lie inm''s. Thencis an agent ofkand ofm', andLaminar.nestedatkandm'gives eitherk's agents insidem''s, som'is the new witness, orm''s insidek's, sokstays.
At the end, with witness k for i and j: if k = i the event at i contains an agent of
j's, and if k = j the event at j contains one of i's, so they share an agent. If
i < k < j, the event at k is an access or a fence, whose one agent is then the agent of both,
or a rendezvous, which is the second case.
The proof uses mem_members and nest only, through Laminar.nested; the other four fields
are not needed for it.
Where the Lean form departs from the design
Laminarnames every event, not only the ordering ones. The design reads "every ordering event of the trace is asyncof the members of one level instance". Lean says each event is an access, a fence or such a rendezvous, so arelor anacqmakes a trace not laminar even when nothing acquires what it releases. A fence orders no positions ofHB(onlyOrderedreads it), so it is allowed.- A rendezvous's agents equal an instance's members as sets. The design's "a
syncof the members of one level instance" fixes no list. Lean asks for the same agents in any order and with any repetition, so a machine may log the members in the order they arrive. - The theorem names the rendezvous, not the instance. The right side gives the position
kand the agentsmsof the rendezvous. The instancegwithms's agents as members is recovered fromLaminar, so the statement needs noGidand holds on every machine withLevels. - The events at
iandjneed not be accesses, and the theorem relates every pair of positions.racyreads it at pairs of accesses.
The instance Levels toy
The toy machine's levels are WithTop ℕ (Lvl): 0, which is ⊥, the thread; ⊤ the launch;
each k between them a level of blocks of consecutive threads. A launch is a number of threads
and a list of widths from the thread up (toy.Launch := Nat × List Nat). A thread t's instance
of level k is (k, t / span ws k), where span ws k is the product of the first k widths, and
its instance of ⊤ is (⊤, 0) (groupOf). Past the last width every level has the instances of
the last one. members filters the launch's threads by their instance (membersOf), so
mem_members holds by List.mem_filter. Before this, levels were Bool and members n (true, k)
was the whole launch for every k, while group gave only (true, 0), so mem_members failed at
(true, 1).
nest: fork ≤ k + d,span ws (k + d) = span ws k * ((ws.drop k).take d).prod, andt / (a * b) = t / a / b(Nat.div_div_eq_div_mul); every level is below⊤, whose instance is the same for all threads.top: every thread's instance of⊤is(⊤, 0), so the filter keeps every thread.bot:span ws 0 = 1, so the instance of level0is the thread itself, which occurs once inList.range n.levelOf_groupis by definition, andnodupisList.nodup_rangeand its filter.
The barrier instruction names its level (Ins.bar ℓ). A thread waiting at ℓ completes the
barrier of its instance g of level ℓ once every member of g waits at ℓ (allWait); the
members of g run again and sync (barObj g) (members of g) is logged. barObj is 0 at ⊤,
the launch's barrier as before, and Nat.pair k i + 1 for instance i of level k.
reach_laminar shows that every state the machine reaches from launch P n m ws keeps n and
ws and has a trace that is laminar for the launch (n, ws).
Changed test text, all with the old meaning (the launch's barrier is ⊤'s, and a launch with
no widths has the levels ⊥ and ⊤ only):
MojoProofTest/GPUProof/Toy.lean,ring:.barbecame.bar ⊤.MojoProofTest/GPUProof/Clean.lean:own_goodStart's launchnbecame(n, []);weightandwork_lttakeStatus (List Ins) Lvl;step_own's proof no longer rewrites the barrier case.MojoProofTest/GPUProof/Imports.lean: the pinned module list gainsGPUProof.Laminar, and its#guard_msgscompares whitespace laxly so the list may wrap.
Tests (MojoProofTest/GPUProof/Laminar.lean)
handover warp top w runs on nw warps of w lanes (widths [w]): lane t of warp 0 waits
at t warp barriers, writes t to byte 0 of buffer 0 and waits at the other w - 1 - t;
then every thread waits at the launch's barrier and lane 0 of warp 1 reads the byte.
run_laminar gives Laminar for every such run, from reach_laminar. At two warps of 2 lanes
and three warps of 5:
- the run ends with every thread returned, the last lane's value in the byte,
2 w + 1events, a trace thatracypasses andkeptkeeps, andhbfrom the first write to the read, which share no agent; hbequals the right side ofHB.laminar_iff(viaSync) at every pair of positions, on the run and on the two mutants (no warp barriers, no launch barrier), which race.
The other side: the hand-over through a release and an acquire (relAcqTrace) is not laminar for
any launch (relAcq_not_laminar), and on it hb orders the write before the read while
viaSync refuses the pair, at 3 and 7 bytes.
Use maps decide race freedom
Scope. Use, Use.Admits, isSyncOf, phase, phases, Use.Separated, phase_mono,
phase_lt and racy_eq_false_of_uses are stated and proved for every machine S : Sig, with no
Levels and no Laminar premise. The tests are about one test machine (toy):
handover_separated at every number of lanes and warps, the rest at two sizes each.
Definitions
Use S:writer a p(agentareads and writes the byte),readers g p(the members of instancegread it),frozen p(anyone reads it),rmw op word p(only result-unused updates byopof exactly the bytesword). Each case names the proxypits accesses go through.Use.Admits S L u x:xgoes throughu's proxy, and is bya(writer); is a read or a frozen read (Kind.writes = false) by a member ofg, as a thread (readers); is a read or a frozen read (frozen); or has kind.rmw opandAccess.locs S x = word(rmw). It is decidable (Use.decAdmits).isSyncOf S L g o:ois a rendezvoussync _ mswhose agents areg's members as threads, as sets: every agent ofmsis a member ofg, and every member is inms.phase S L tr i g: the number of positionsk < iat whichisSyncOf S L g tr[k]?.phases S L tr: the accesses oftr, oldest first, each withphase S L tr iat its positioni.Use.Separated S L U: for all phase vectorsπ ≤ π'(pointwise), a bytel, and accessesxadmitted byU π landyadmitted byU π' lthat both touchland conflict (Access.conflict):x.proxy = y.proxy, andx.who = y.whoor some instanceghas both agents among its members (as threads) andπ g < π' g.
theorem racy_eq_false_of_uses {L : S.Launch} {tr : List S.Event}
(U : (S.Gid → Nat) → S.Loc → Use S) (hU : Use.Separated S L U)
(h : ∀ x ∈ phases S L tr, ∀ l ∈ x.1.locs S, (U x.2 l).Admits S L x.1) :
racy S tr = false
Proof
From GPUProof.racy_iff, a race is a pair of positions i < j holding accesses x and y that
conflict and are not Ordered. Conflict gives a byte l of both. Both pairs (x, phase at i)
and (y, phase at j) are in phases, so h gives the admissions, and phase_mono gives the
pointwise phase at i ≤ phase at j. hU then gives one proxy, so it suffices to show
GPUProof.HB from i to j (the first case of Ordered):
- one agent: one
edgefromitoj; - instance
gwithπ g < π' g:phase_ltgives a positionkwithi ≤ k < jandisSyncOfthere, sotr[k]is a rendezvous holding every member ofg. It is noti, which holds an access, soi < k, and the edgesi → kandk → j(both agents are members) giveHBthroughRelation.TransGen.tail.
phase_lt is by induction on j: the count grows from j to j + 1 only by the position j.
How the statement departs from the design, and why
- Each
Usenames a proxy, andUse.Separatedasks conflicting admitted accesses to go through one. The design'sUsehas no proxy, and its separation speaks of agents only. On the core'sRacy, two accesses through two proxies are ordered only through a fence (GPUProof.Ordered's second case), which no use map sees. Without the proxy inAdmits,writer (thread t)admits threadt's write through proxyfalseand its read back through proxytrue, with no fence between them, and the trace races (MojoProofTest.GPUProof.Toy.crossProxy false; the testcrossProxy_not_admittedchecks that, with the proxy, the read is not admitted). Without the proxy clause inSeparated, the useswriter (thread 0) falsebefore the launch's barrier andreaders (⊤, 0) trueafter it are separated by the launch's phase, every access of the trace write, barrier, read is admitted, and the trace races:HBorders the pair but not through one proxy (proxySwap_not_separated). The proxy inUseis the choice made here; a use map that moves a byte to another proxy does so at a phase step, and the fence that makes the move race free is outside what this theorem covers (a fence orders onlyOrdered's second case, and a use map would need the fence's position). That case is the permission history's (the design'sracy_eq_false_of_history, where a cell changes proxy at a fence of its holder). - No
Levels, noLaminar. The design states the theorem on laminar traces of a machine withLevels. The proof never uses either: a phase counts only rendezvous whose agents are exactlyg's members, so a phase step of an instance containing both agents is itself a rendezvous holding both, between the two positions, on every trace. The testwithRelAcq_race_freeapplies the theorem to a trace with a release and an acquire, which is not laminar (withRelAcq_not_laminar). The design's form is the special case. phases,AdmitsandSeparatedtake the launchL. The members of an instance areS.members L g, so the phase ofgand the agents ofreaders gneedL. The design'sphases (tr)names no launch.- "Two updates admitted by one
rmwuse must be exempt" is not a separate clause.Separatedasks its conclusion of conflicting pairs only, andAccess.conflictis false on the pairs the machine exempts. So two updates admitted by onermwuse that the machine does not exempt (other proxies, or a returned result,Kind.rmwRet, whichrmwdoes not admit) must be one agent's or told apart, as any other pair. - Separation includes one agent's pairs. The design's separation speaks of "different
agents". Here
x.who = y.whois the first case of the conclusion, which is the same. - The instance must contain both agents. If "told apart" did not ask for the instance to
contain both agents, the uses
writer (thread 0)before warp0's barrier andwriter (thread k)after it, where threadkis lane0of warp1, would be told apart by warp0's phase, and the trace write, warp barrier, write races (outside_not_separated). - A phase counts a rendezvous of all the members.
isSyncOfasks every member ofgto be among the rendezvous's agents; counting a rendezvous of some members only (a masked barrier) would growg's phase without ordering the members left out. It also asks every agent to be a member, which the proof does not need: a phase countsg's own rendezvous, not those of a larger instance, as MojoProof'sphasescounts its block's barriers only. - Jobs. A job is in no instance (members are threads), so a job's byte can only be its
writer's, or read-only.
A finding of this proof, for the tests: instance search does not unfold a machine's fields, so
Decidable (Use.Admits toy (n, ws) u x) fails on a pair (n, ws), whose type is
Nat × List Nat, not toy.Launch. The tests name their launches through toyLaunch, whose type
is toy.Launch. The same holds at any machine defined by a structure instance, sig included.
The MojoProof corollaries, for later
MojoProof.Sem.racy_eq_false_of_writers, racy_eq_false_of_phased and racy_eq_false_of_owned
(MojoProof/Sem/Trace.lean) keep their statements. racy_eq_false_of_phased and
racy_eq_false_of_owned keep their proofs through racy_eq_false_of_writers, which are one line
each already. Only racy_eq_false_of_writers moves:
theorem racy_eq_false_of_writers ... (hU ...) (hs ...) (hW ...) (hG ...) (hX ...) :
GPUProof.racy sig (tr.map (Event.toCore L)).reverse = false :=
GPUProof.racy_eq_false_of_uses (writersUse tr W own)
(writersUse_separated tr W own) (writersUse_admits hU hs hW hG hX)
The route, and what it needs besides GPUProof.racy_eq_false_of_uses:
racy_sig_iff(MojoProof/Sem/RaceSig.lean): the core'sracyatsigof a tracetrwhose accesses'whoagree with the launchL(hU), as two conflicting accesses of two threads that no barrier of their block orders (byGPUProof.HB.laminar_iff), withtoCorethe embedding of MojoProof's trace intosig's (newest-first to oldest-first, a barrier of blockkto the rendezvous ofk's threads). Withracy_iff, on shaped traces (hs), it is MojoProof'sclashoverphases. The three corollaries takehUandhs, whichGrid.start_reach_fitsgives at every grid reachable from the start: a deliberate restatement.writersUse tr W own π l, built from the trace. For the bytel = (b, i): if every access oftrtolis by blockc, readc's phasep = π (block instance c), and givewriter (thread t)whenW p b i = some o, withta thread of an access oftrtolat phasepwhoseown p t = o(a member ofcotherwise), andreaders (block instance c)whenW p b i = none; if accesses of two blocks touchl, givefrozen. Each throughsig's one proxy.writersUse_separated, by construction and for everyWandown: a frozen byte has no writes; a byte of blockchas its use a function ofπ c, so equal phases ofcgive one use (onewriter, orreaderswith no writes) and different ones are told apart byc, which contains every agent its uses admit.writersUse_admits, fromhW,hGandhX, with the phase correspondence: atsig,GPUProof.phaseof blockc's instance at an access's position is MojoProof's barrier count ofcthere.hWsays the access is its byte's owner's or a read of an unowned byte;hXmakes the owner's thread unique among the accesses at one phase of one block;hGwithhWandhXmakes a byte touched by two blocks unowned, so every access to it is a read.
This is not the one-line corollary the design names: the use map is built from the trace, and
steps 2 to 4 are new MojoProof lemmas. It needs sig, racy_iff and the phase
correspondence first.
Tests (MojoProofTest/GPUProof/Uses.lean)
handoverUse k: while the launch's phase is 0, the byte is lane min p (k - 1)'s, through
proxy false, where p is warp 0's phase; afterwards it is readers (⊤, 0) false.
handover_separated: the uses are separated for everyk ≥ 1lanes andnw ≥ 1warps.handover_race_free: at two warps of 2 lanes and three warps of 5, the hand-over's run is race free throughGPUProof.racy_eq_false_of_uses, with the admissions bydecide +kernel; a#guardevaluatesracytofalseon both.- The mutants, one per premise and design choice:
- no laminar premise:
withRelAcq(the run with a release and an acquire appended) is not laminar and is race free through the theorem; - not separated:
outsideWarp(thread0writes, warp0's barrier, threadkwrites) withoutsideUseadmits every access and races, so the uses are not separated, at 2 and 5 lanes; - not admitted: without the launch's barrier (
run true false)handoverUsedoes not admit the read, and the run races, at both sizes; - proxies:
proxySwapwithproxySwapUse(each admitted, through two proxies) races, so the uses are not separated;crossProxy falseraces, andownUse(each thread's own byte, through proxyfalse) does not admit its reads, at 3 and 7 threads.
- no laminar premise: