Skip to content

Commit 0c804af

Browse files
authored
Merge pull request #156 from opencompl/alive-hardest-proof-1
feat: add proof of DivRemOfSelect
2 parents 5567f58 + 2d1d03d commit 0c804af

File tree

2 files changed

+66
-19
lines changed

2 files changed

+66
-19
lines changed

SSA/Projects/InstCombine/Alive.lean

+5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
-- Include these, as they are reasonably fast to typecheck.
44
import SSA.Projects.InstCombine.AliveStatements
55

6+
-- Handwritten Alive examples.
7+
-- This has those examples from alive that failed to be
8+
-- translated correctly due to bugs in the translator.
9+
import SSA.Projects.InstCombine.AliveHandwrittenExamples
10+
611
-- The semantics for the MLIR base dialect
712
import SSA.Projects.InstCombine.Base
813

Original file line numberDiff line numberDiff line change
@@ -1,40 +1,82 @@
1-
21
import SSA.Projects.InstCombine.LLVM.EDSL
32
import SSA.Projects.InstCombine.AliveStatements
43
import SSA.Projects.InstCombine.Refinement
54
import SSA.Projects.InstCombine.Tactic
5+
import SSA.Projects.InstCombine.Base
6+
import SSA.Core.ErasedContext
67

78
open MLIR AST
89
open Std (BitVec)
9-
open Ctxt (Var)
10+
open Ctxt (Var DerivedCtxt)
11+
open InstCombine (MOp)
1012

11-
namespace AliveAutoGenerated
13+
namespace AliveHandwritten
1214
set_option pp.proofs false
1315
set_option pp.proofs.withType false
1416

1517

1618
/-
1719
Name: SimplifyDivRemOfSelect
18-
20+
precondition: true
1921
%sel = select %c, %Y, 0
2022
%r = udiv %X, %sel
2123
=>
2224
%r = udiv %X, %Y
2325
2426
-/
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
2581

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

Comments
 (0)