|
1 |
| - |
2 | 1 | import SSA.Projects.InstCombine.LLVM.EDSL
|
3 | 2 | import SSA.Projects.InstCombine.AliveStatements
|
4 | 3 | import SSA.Projects.InstCombine.Refinement
|
5 | 4 | import SSA.Projects.InstCombine.Tactic
|
| 5 | +import SSA.Projects.InstCombine.Base |
| 6 | +import SSA.Core.ErasedContext |
6 | 7 |
|
7 | 8 | open MLIR AST
|
8 | 9 | open Std (BitVec)
|
9 |
| -open Ctxt (Var) |
| 10 | +open Ctxt (Var DerivedCtxt) |
| 11 | +open InstCombine (MOp) |
10 | 12 |
|
11 |
| -namespace AliveAutoGenerated |
| 13 | +namespace AliveHandwritten |
12 | 14 | set_option pp.proofs false
|
13 | 15 | set_option pp.proofs.withType false
|
14 | 16 |
|
15 | 17 |
|
16 | 18 | /-
|
17 | 19 | Name: SimplifyDivRemOfSelect
|
18 |
| -
|
| 20 | +precondition: true |
19 | 21 | %sel = select %c, %Y, 0
|
20 | 22 | %r = udiv %X, %sel
|
21 | 23 | =>
|
22 | 24 | %r = udiv %X, %Y
|
23 | 25 |
|
24 | 26 | -/
|
| 27 | +def alive_DivRemOfSelect_src (w : Nat) := |
| 28 | + [alive_icom (w)| { |
| 29 | + ^bb0(%c: i1, %y : _, %x : _): |
| 30 | + %c0 = "llvm.mlir.constant" () { value = 0 : _ } :() -> (_) |
| 31 | + %v1 = "llvm.select" (%c,%y, %c0) : (i1, _, _) -> (_) |
| 32 | + %v2 = "llvm.udiv"(%x, %v1) : (_, _) -> (_) |
| 33 | + "llvm.return" (%v2) : (_) -> () |
| 34 | + }] |
| 35 | + |
| 36 | +def alive_DivRemOfSelect_tgt (w : Nat) := |
| 37 | + [alive_icom (w)| { |
| 38 | + ^bb0(%c: i1, %y : _, %x : _): |
| 39 | + %v1 = "llvm.udiv" (%x,%y) : (_, _) -> (_) |
| 40 | + "llvm.return" (%v1) : (_) -> () |
| 41 | + }] |
| 42 | + |
| 43 | +@[simp] |
| 44 | +theorem BitVec.ofNat_toNat_zero : |
| 45 | +BitVec.toNat (BitVec.ofInt w 0) = 0 := |
| 46 | + by simp[BitVec.toNat, BitVec.ofInt, BitVec.toFin, BitVec.ofNat, OfNat.ofNat] |
| 47 | + |
| 48 | +theorem alive_DivRemOfSelect (w : Nat) : |
| 49 | + alive_DivRemOfSelect_src w ⊑ alive_DivRemOfSelect_tgt w := by |
| 50 | + unfold alive_DivRemOfSelect_src alive_DivRemOfSelect_tgt |
| 51 | + intros Γv |
| 52 | + simp_peephole at Γv |
| 53 | + simp (config := {decide := false}) only [OpDenote.denote, |
| 54 | + InstCombine.Op.denote, HVector.toPair, HVector.toTriple, pairMapM, BitVec.Refinement, |
| 55 | + bind, Option.bind, pure, DerivedCtxt.ofCtxt, DerivedCtxt.snoc, |
| 56 | + Ctxt.snoc, ConcreteOrMVar.instantiate, Vector.get, HVector.toSingle, |
| 57 | + LLVM.and?, LLVM.or?, LLVM.xor?, LLVM.add?, LLVM.sub?, |
| 58 | + LLVM.mul?, LLVM.udiv?, LLVM.sdiv?, LLVM.urem?, LLVM.srem?, |
| 59 | + LLVM.sshr, LLVM.lshr?, LLVM.ashr?, LLVM.shl?, LLVM.select?, |
| 60 | + LLVM.const?, LLVM.icmp?, |
| 61 | + HVector.toTuple, List.nthLe, bitvec_minus_one] |
| 62 | + intro y x c |
| 63 | + simp only [List.length_singleton, Fin.zero_eta, List.get_cons_zero, List.map_eq_map, List.map_cons, |
| 64 | + List.map_nil, CharP.cast_eq_zero, Ctxt.Valuation.snoc_last, pairBind, bind, Option.bind, Int.ofNat_eq_coe] |
| 65 | + clear Γv |
| 66 | + cases c |
| 67 | + -- | select condition is itself `none`, nothing more to be done. propagate the `none`. |
| 68 | + case none => cases x <;> cases y <;> simp |
| 69 | + case some cond => |
| 70 | + obtain ⟨vcond, hcond⟩ := cond |
| 71 | + obtain (h | h) : vcond = 1 ∨ vcond = 0 := by |
| 72 | + norm_num at hcond |
| 73 | + rcases vcond with zero | vcond <;> simp; |
| 74 | + rcases vcond with zero | vcond <;> simp; |
| 75 | + linarith |
| 76 | + . subst h |
| 77 | + simp |
| 78 | + . subst h; simp |
| 79 | + cases' x with vx <;> |
| 80 | + cases' y with vy <;> simp |
25 | 81 |
|
26 |
| -def alive_simplifyDivRemOfSelect (w : Nat) : |
27 |
| -[mlir_icom ( w )| { |
28 |
| -^bb0(%c : i1, %X : _, %Y : _): |
29 |
| - %v0 = "llvm.mlir.constant" () { value = 0 : _ } :() -> (_) |
30 |
| - %sel = "llvm.select" (%c,%Y,%v0) : (i1, _, _) -> (_) |
31 |
| - %r = "llvm.udiv" (%X,%sel) : (_, _) -> (_) |
32 |
| - "llvm.return" (%r) : (_) -> () |
33 |
| -}] ⊑ [mlir_icom ( w )| { |
34 |
| -^bb0(%c : i1, %X : _, %Y : _): |
35 |
| - %r = "llvm.udiv" (%X,%Y) : (_, _) -> (_) |
36 |
| - "llvm.return" (%r) : (_) -> () |
37 |
| -}] := by |
38 |
| - simp_alive_peephole |
39 |
| - -- goal: ⊢ BitVec.udiv? x1✝ (BitVec.select x2✝ x0✝ (BitVec.ofInt w 0)) ⊑ BitVec.udiv? x1✝ x0✝ |
40 |
| - sorry |
| 82 | +end AliveHandwritten |
0 commit comments