Documentation

Sparkle.IR.ReorderInvariance

theorem Sparkle.IR.Reorder.evalExpr_congr (we : Semantics.WEnv) (env env' : Semantics.Env) (e : AST.Expr) (h : ∀ (n : String), n ∈ refsOf e → env n = env' n) :

Environments agreeing on an expression's refs evaluate it equally (widths and the concat combiner are env-free).

theorem Sparkle.IR.Reorder.evalList_congr (we : Semantics.WEnv) (env env' : Semantics.Env) (args : List AST.Expr) (h : ∀ (n : String), n ∈ refsOf.refsList args → env n = env' n) :
Semantics.evalList we env args = Semantics.evalList we env' args

Names a statement WRITES during the combinational fold.

Equations
Instances For

    Names a statement READS during the combinational fold.

    Equations
    Instances For

      The reorder fragment: with the open-module semantics (instances are no-ops) EVERY statement is in it. Kept as a def so the fragment can narrow again if a closed hierarchical semantics lands.

      Equations
      Instances For

        Well-ordered w.r.t. already-evaluated names done: every read of a locally-written name comes after its (unique) write. This is the semantic content of "topologically sorted".

        Instances For
          theorem Sparkle.IR.Reorder.writesOf_perm_mem {xs ys : List AST.Stmt} (hp : xs.Perm ys) (n : String) :

          Membership in writesOf is permutation-invariant.

          theorem Sparkle.IR.Reorder.WO_done_congr {d1 d2 : List String} {body : List AST.Stmt} (hd : ∀ (n : String), n ∈ d1 ↔ n ∈ d2) (h : WO d1 body) :
          WO d2 body

          done matters only up to membership.

          theorem Sparkle.IR.Reorder.comboReads_frame (we : Semantics.WEnv) (mems : Semantics.MEnv) (name : String) (aw dw : Nat) (ports : List (AST.Expr × String)) (env r : Semantics.Env) :
          Semantics.comboReads we mems name aw dw ports env = some r → ∀ (n : String), ¬n ∈ List.map Prod.snd ports → r n = env n
          theorem Sparkle.IR.Reorder.comboReads_agree (we : Semantics.WEnv) (mems : Semantics.MEnv) (name : String) (aw dw : Nat) (ports : List (AST.Expr × String)) (env₁ env₂ : Semantics.Env) :
          (∀ (n : String), n ∈ List.flatMap (fun (p : AST.Expr × String) => refsOf p.fst) ports → env₁ n = env₂ n) → match Semantics.comboReads we mems name aw dw ports env₁, Semantics.comboReads we mems name aw dw ports env₂ with | some r₁, some r₂ => ∀ (n : String), n ∈ List.map Prod.snd ports → r₁ n = r₂ n | none, none => True | x, x_1 => False
          theorem Sparkle.IR.Reorder.nop_step (we : Semantics.WEnv) (mems : Semantics.MEnv) {s : AST.Stmt} (hs : SimpleStmt s) (hw : stmtWrites s = []) (rest : List AST.Stmt) (env : Semantics.Env) :
          Semantics.evalAssigns we mems (s :: rest) env = Semantics.evalAssigns we mems rest env

          In the v1 fragment, a non-writing statement is a combinational no-op.

          theorem Sparkle.IR.Reorder.evalAssigns_cons_congr (we : Semantics.WEnv) (mems : Semantics.MEnv) {X Y : List AST.Stmt} (h : ∀ (env : Semantics.Env), Semantics.evalAssigns we mems X env = Semantics.evalAssigns we mems Y env) (p : AST.Stmt) (hp : SimpleStmt p) (env : Semantics.Env) :
          Semantics.evalAssigns we mems (p :: X) env = Semantics.evalAssigns we mems (p :: Y) env

          Rewriting under one leading statement: if two tails agree at every environment, so do the bodies with the same head.

          theorem Sparkle.IR.Reorder.nop_second (we : Semantics.WEnv) (mems : Semantics.MEnv) {a b : AST.Stmt} (hb : SimpleStmt b) (ha : SimpleStmt a) (haw : stmtWrites a = []) (rest : List AST.Stmt) (env : Semantics.Env) :
          Semantics.evalAssigns we mems (b :: a :: rest) env = Semantics.evalAssigns we mems (b :: rest) env

          A no-op in SECOND position can be dropped.

          theorem Sparkle.IR.Reorder.swap_assign_mem (we : Semantics.WEnv) (mems : Semantics.MEnv) (la : String) (ra : AST.Expr) (nB : String) (awB dwB : Nat) (cB : String) (waB wdB wenB raB : AST.Expr) (rdB : String) (ewB : List (AST.Expr × AST.Expr × AST.Expr)) (erB : List (AST.Expr × String)) (hla_refs : ¬la ∈ List.flatMap (fun (p : AST.Expr × String) => refsOf p.fst) ((raB, rdB) :: erB)) (hla_rds : ¬la ∈ List.map Prod.snd ((raB, rdB) :: erB)) (hrds_ra : ∀ (n : String), n ∈ List.map Prod.snd ((raB, rdB) :: erB) → ¬n ∈ refsOf ra) (rest : List AST.Stmt) (env0 : Semantics.Env) :
          Semantics.evalAssigns we mems (AST.Stmt.assign la ra :: AST.Stmt.memory nB awB dwB cB waB wdB wenB raB rdB true ewB erB :: rest) env0 = Semantics.evalAssigns we mems (AST.Stmt.memory nB awB dwB cB waB wdB wenB raB rdB true ewB erB :: AST.Stmt.assign la ra :: rest) env0

          An assign and a combo-read memory with disjoint footprints commute.

          theorem Sparkle.IR.Reorder.swap_mem_mem (we : Semantics.WEnv) (mems : Semantics.MEnv) (nA : String) (awA dwA : Nat) (cA : String) (waA wdA wenA raA : AST.Expr) (rdA : String) (ewA : List (AST.Expr × AST.Expr × AST.Expr)) (erA : List (AST.Expr × String)) (nB : String) (awB dwB : Nat) (cB : String) (waB wdB wenB raB : AST.Expr) (rdB : String) (ewB : List (AST.Expr × AST.Expr × AST.Expr)) (erB : List (AST.Expr × String)) (hA_refsB : ∀ (n : String), n ∈ List.map Prod.snd ((raA, rdA) :: erA) → ¬n ∈ List.flatMap (fun (p : AST.Expr × String) => refsOf p.fst) ((raB, rdB) :: erB)) (hA_rdsB : ∀ (n : String), n ∈ List.map Prod.snd ((raA, rdA) :: erA) → ¬n ∈ List.map Prod.snd ((raB, rdB) :: erB)) (hB_refsA : ∀ (n : String), n ∈ List.map Prod.snd ((raB, rdB) :: erB) → ¬n ∈ List.flatMap (fun (p : AST.Expr × String) => refsOf p.fst) ((raA, rdA) :: erA)) (rest : List AST.Stmt) (env0 : Semantics.Env) :
          Semantics.evalAssigns we mems (AST.Stmt.memory nA awA dwA cA waA wdA wenA raA rdA true ewA erA :: AST.Stmt.memory nB awB dwB cB waB wdB wenB raB rdB true ewB erB :: rest) env0 = Semantics.evalAssigns we mems (AST.Stmt.memory nB awB dwB cB waB wdB wenB raB rdB true ewB erB :: AST.Stmt.memory nA awA dwA cA waA wdA wenA raA rdA true ewA erA :: rest) env0

          Two combo-read memories with disjoint footprints commute.

          theorem Sparkle.IR.Reorder.evalAssigns_swap (we : Semantics.WEnv) (mems : Semantics.MEnv) {a b : AST.Stmt} (ha : SimpleStmt a) (hb : SimpleStmt b) (hwa_rb : ∀ (n : String), n ∈ stmtWrites a → ¬n ∈ stmtReads b) (hwa_wb : ∀ (n : String), n ∈ stmtWrites a → ¬n ∈ stmtWrites b) (hwb_ra : ∀ (n : String), n ∈ stmtWrites b → ¬n ∈ stmtReads a) (rest : List AST.Stmt) (env0 : Semantics.Env) :
          Semantics.evalAssigns we mems (a :: b :: rest) env0 = Semantics.evalAssigns we mems (b :: a :: rest) env0

          Adjacent independent statements commute.

          theorem Sparkle.IR.Reorder.evalAssigns_bubble (we : Semantics.WEnv) (mems : Semantics.MEnv) {s : AST.Stmt} (hs : SimpleStmt s) (pre : List AST.Stmt) :
          (∀ (p : AST.Stmt), p ∈ pre → SimpleStmt p) → (∀ (p : AST.Stmt), p ∈ pre → (∀ (n : String), n ∈ stmtWrites s → ¬n ∈ stmtReads p ∧ ¬n ∈ stmtWrites p) ∧ ∀ (n : String), n ∈ stmtWrites p → ¬n ∈ stmtReads s) → ∀ (post : List AST.Stmt) (env0 : Semantics.Env), Semantics.evalAssigns we mems (pre ++ s :: post) env0 = Semantics.evalAssigns we mems (s :: (pre ++ post)) env0

          Bubble an independent statement to the front.

          theorem Sparkle.IR.Reorder.WO_all_ok {done : List String} {body : List AST.Stmt} (h : WO done body) (p : AST.Stmt) :
          p ∈ body → SimpleStmt p
          theorem Sparkle.IR.Reorder.WO_middle_indep {pre : List AST.Stmt} {done : List String} {s : AST.Stmt} {post : List AST.Stmt} :
          WO done (pre ++ s :: post) → ∀ (p : AST.Stmt), p ∈ pre → (∀ (n : String), n ∈ stmtReads p → ¬n ∈ stmtWrites s) ∧ ∀ (n : String), n ∈ stmtWrites p → ¬n ∈ stmtWrites s

          Everything in pre reads and writes nothing that anything AFTER it (in particular s) writes.

          theorem Sparkle.IR.Reorder.WO_remove_middle {pre : List AST.Stmt} {done : List String} {s : AST.Stmt} {post : List AST.Stmt} :
          WO done (pre ++ s :: post) → WO (done ++ stmtWrites s) (pre ++ post)

          Removing a middle statement (independent of everything before it) preserves well-ordering, with its writes marked done.

          theorem Sparkle.IR.Reorder.evalAssigns_perm (we : Semantics.WEnv) (mems : Semantics.MEnv) {body body' : List AST.Stmt} {done : List String} :
          body.Perm body' → WO done body → WO done body' → ∀ (env0 : Semantics.Env), Semantics.evalAssigns we mems body env0 = Semantics.evalAssigns we mems body' env0

          Reorder invariance: two well-ordered arrangements of the same statements fold to the same environment. This is the semantic license for the shipping topoSortBody: any topological order of the roundtrip image evaluates like the original order.

          One statement's contribution to the register-update list.

          Equations
          Instances For
            theorem Sparkle.IR.Reorder.regNexts_cons (we : Semantics.WEnv) (mems : Semantics.MEnv) (envF : Semantics.Env) (s : AST.Stmt) (t : List AST.Stmt) :
            Semantics.regNexts we mems (s :: t) envF = (stmtNexts we mems envF s).bind fun (c : List (String × Nat)) => Option.map (fun (x : List (String × Nat)) => c ++ x) (Semantics.regNexts we mems t envF)

            regNexts is the in-order concatenation of contributions.

            def Sparkle.IR.Reorder.OPerm {α : Type u_1} :
            Option (List α) → Option (List α) → Prop

            Permutation relation on optional lists.

            Equations
            Instances For
              theorem Sparkle.IR.Reorder.OPerm.refl {α : Type u_1} (o : Option (List α)) :
              OPerm o o
              theorem Sparkle.IR.Reorder.OPerm.trans {α : Type u_1} {a b c : Option (List α)} :
              OPerm a b → OPerm b c → OPerm a c
              theorem Sparkle.IR.Reorder.regNexts_perm (we : Semantics.WEnv) (mems : Semantics.MEnv) (envF : Semantics.Env) {body body' : List AST.Stmt} (hp : body.Perm body') :
              OPerm (Semantics.regNexts we mems body envF) (Semantics.regNexts we mems body' envF)

              The register-update lists of two permuted bodies are permutations of each other (or both fail).

              theorem Sparkle.IR.Reorder.key_unique {l : List (String × Nat)} (hnd : (List.map Prod.fst l).Nodup) {q q' : String × Nat} (hq : q ∈ l) (hq' : q' ∈ l) (hk : q.fst = q'.fst) :
              q = q'

              Two members of a key-nodup pair list with the same key coincide.

              applyNexts only cares about the update MULTISET when keys are unique.

              theorem Sparkle.IR.Reorder.evalPayload_mems_congr {we : Semantics.WEnv} {m₁ m₂ : Semantics.MEnv} {env : Semantics.Env} {arr : String} {aw dw : Nat} (hm : ∀ (i : Nat), m₁ arr i = m₂ arr i) (e : AST.Expr) :
              Semantics.evalPayload we m₁ env arr aw dw e = Semantics.evalPayload we m₂ env arr aw dw e

              Payload evaluation depends on the memory state only through its OWN array's slice.

              One statement's memory-state effect: mems0 for read resolution is the state the statement receives (Verilog nonblocking: payloads see the pre-write state — and earlier statements only touch OTHER arrays, so this equals the pre-cycle state of the OWN array).

              Equations
              Instances For
                theorem Sparkle.IR.Reorder.memNexts_cons (we : Semantics.WEnv) (envF : Semantics.Env) (s : AST.Stmt) (t : List AST.Stmt) (mems : Semantics.MEnv) :
                Semantics.memNexts we (s :: t) mems envF = (stmtMemUpd we envF s mems).bind fun (m' : Semantics.MEnv) => Semantics.memNexts we t m' envF
                theorem Sparkle.IR.Reorder.memWritePorts_isSome (we : Semantics.WEnv) (env : Semantics.Env) (name : String) (aw dw : Nat) (ports : List (AST.Expr × AST.Expr × AST.Expr)) (ms₁ ms₂ m₁ m₂ : Semantics.MEnv) :
                (∀ (i : Nat), ms₁ name i = ms₂ name i) → (Semantics.memWritePorts we ms₁ env name aw dw ports m₁).isSome = (Semantics.memWritePorts we ms₂ env name aw dw ports m₂).isSome

                Success of a write fold does not depend on the THREADED state, and tracks the read-resolution state only through the own slice.

                theorem Sparkle.IR.Reorder.memWritePorts_frame (we : Semantics.WEnv) (env : Semantics.Env) (name : String) (aw dw : Nat) (ports : List (AST.Expr × AST.Expr × AST.Expr)) (ms m r : Semantics.MEnv) :
                Semantics.memWritePorts we ms env name aw dw ports m = some r → ∀ (nm : String), nm ≠ name → ∀ (i : Nat), r nm i = m nm i

                A write fold leaves every OTHER array untouched.

                theorem Sparkle.IR.Reorder.memWritePorts_slice (we : Semantics.WEnv) (env : Semantics.Env) (name : String) (aw dw : Nat) (ports : List (AST.Expr × AST.Expr × AST.Expr)) (ms₁ ms₂ m₁ m₂ r₁ r₂ : Semantics.MEnv) :
                (∀ (i : Nat), ms₁ name i = ms₂ name i) → (∀ (i : Nat), m₁ name i = m₂ name i) → Semantics.memWritePorts we ms₁ env name aw dw ports m₁ = some r₁ → Semantics.memWritePorts we ms₂ env name aw dw ports m₂ = some r₂ → ∀ (i : Nat), r₁ name i = r₂ name i

                A write fold's OWN slice is determined by the incoming own slices (both the read-resolution state's and the threaded state's).

                Equations
                Instances For
                  theorem Sparkle.IR.Reorder.stmtMemUpd_comm (we : Semantics.WEnv) (envF : Semantics.Env) {x y : AST.Stmt} (hx : SimpleStmt x) (hy : SimpleStmt y) (hname : ∀ (nx : String), stmtMemName x = some nx → ∀ (ny : String), stmtMemName y = some ny → nx ≠ ny) (m : Semantics.MEnv) :
                  (stmtMemUpd we envF x m).bind (stmtMemUpd we envF y) = (stmtMemUpd we envF y m).bind (stmtMemUpd we envF x)

                  Memory effects of two statements with distinct array names commute: each fold touches only its own slice, resolves reads only against its own slice, and its success is independent of everything else.

                  theorem Sparkle.IR.Reorder.memNexts_perm (we : Semantics.WEnv) (envF : Semantics.Env) {body body' : List AST.Stmt} (hp : body.Perm body') :
                  (∀ (p : AST.Stmt), p ∈ body → SimpleStmt p) → (List.filterMap stmtMemName body).Nodup → ∀ (mems : Semantics.MEnv), Semantics.memNexts we body mems envF = Semantics.memNexts we body' mems envF

                  The memory phase only cares about the statement MULTISET when array names are distinct.

                  The keys regNexts will emit, in body order.

                  Equations
                  • One or more equations did not get rendered due to their size.
                  Instances For
                    theorem Sparkle.IR.Reorder.syncReadLatches_keys (we : Semantics.WEnv) (mems : Semantics.MEnv) (name : String) (aw dw : Nat) (envF : Semantics.Env) (ports : List (AST.Expr × String)) (l : List (String × Nat)) :
                    Semantics.syncReadLatches we mems name aw dw ports envF = some l → List.map Prod.fst l = List.map Prod.snd ports

                    The update list's keys are nextKeys, independent of values.

                    theorem Sparkle.IR.Reorder.stepModule_inv {we : Semantics.WEnv} {body : List AST.Stmt} {env0 : Semantics.Env} {mems : Semantics.MEnv} {envF : Semantics.Env} {n : List (String × Nat)} {m : Semantics.MEnv} (h : Semantics.stepModule we body env0 mems = some (envF, n, m)) :
                    Semantics.evalAssigns we mems body env0 = some envF ∧ Semantics.regNexts we mems body envF = some n ∧ Semantics.memNexts we body mems envF = some m

                    Invert one step into its three stages.

                    theorem Sparkle.IR.Reorder.stepModule_perm (we : Semantics.WEnv) {body body' : List AST.Stmt} (hp : body.Perm body') (hWO : WO [] body) (hWO' : WO [] body') (hmem : (List.filterMap stmtMemName body).Nodup) (env0 : Semantics.Env) (mems : Semantics.MEnv) :
                    match Semantics.stepModule we body env0 mems, Semantics.stepModule we body' env0 mems with | some (e, n, m), some (e', n', m') => e = e' ∧ n.Perm n' ∧ m = m' | none, none => True | x, x_1 => False

                    One cycle under reordering: the final env and the memory state agree exactly; the register updates agree as a multiset.

                    theorem Sparkle.IR.Reorder.runModule_perm (we : Semantics.WEnv) {body body' : List AST.Stmt} (hp : body.Perm body') (hWO : WO [] body) (hWO' : WO [] body') (hmem : (List.filterMap stmtMemName body).Nodup) (hkeys : (nextKeys body).Nodup) (seed : Nat → (String → Nat) → Semantics.Env) (k : Nat) (st : String → Nat) (mems : Semantics.MEnv) :
                    Semantics.runModule we body seed k st mems = Semantics.runModule we body' seed k st mems

                    Trace equivalence under reordering: with unique update keys the observable trace is identical for any well-ordered arrangement.

                    Equations
                    • One or more equations did not get rendered due to their size.
                    Instances For

                      Structural equality on Expr (the DERIVED BEq is well-founded recursion and opaque to both kernel reduction and lawfulness proofs, so we roll a structural one and prove it sound).

                      Equations
                      Instances For
                        Equations
                        Instances For

                          Boolean well-ordering check (sound for WO).

                          Equations
                          Instances For
                            theorem Sparkle.IR.Reorder.woCheck_sound (done : List String) (body : List AST.Stmt) (h : woCheck done body = true) :
                            WO done body
                            def Sparkle.IR.Reorder.isPermOf {α : Type u_1} [DecidableEq α] :
                            List α → List α → Bool

                            Sound (one-directional) permutation check, erase-based.

                            Equations
                            Instances For
                              theorem Sparkle.IR.Reorder.isPermOf_sound {α : Type u_1} [DecidableEq α] {l₁ l₂ : List α} :
                              isPermOf l₁ l₂ = true → l₁.Perm l₂