Debian Codegraph @

src/MiscCompilerPassesProofs.v

raw
1 From Coq Require Import ZArith.
2 From Coq Require Import List.
3 From Coq Require Import Morphisms.
4 From Coq Require Import MSetPositive.
5 From Coq Require Import FMapPositive.
6 Require Import Rewriter.Language.Language.
7 Require Import Rewriter.Language.Inversion.
8 Require Import Rewriter.Language.Wf.
9 Require Import Crypto.Language.TreeCaching.
10 Require Import Crypto.MiscCompilerPasses.
11 Require Import Crypto.Util.Bool.Reflect.
12 Require Import Crypto.Util.LetIn.
13 Require Import Crypto.Util.Tactics.BreakMatch.
14 Require Import Crypto.Util.Tactics.SplitInContext.
15 Require Import Crypto.Util.Tactics.SpecializeAllWays.
16 Require Import Crypto.Util.Tactics.RewriteHyp.
17 Require Import Crypto.Util.Prod.
18 Require Import Crypto.Util.ListUtil.
19 Import ListNotations. Local Open Scope list_scope.
20
21 Module Compilers.
22 Import Language.Compilers.
23 Import Language.Inversion.Compilers.
24 Import Language.Wf.Compilers.
25 Import MiscCompilerPasses.Compilers.
26 Import expr.Notations.
27 Import invert_expr.
28
29 Module Subst01.
30 Import MiscCompilerPasses.Compilers.Subst01.
31
32 Local Ltac simplifier_t_step :=
33 first [ progress cbn [fst snd] in *
34 | progress eta_expand ].
35
36 Section with_counter.
37 Context {T : Type}
38 (one : T)
39 (incr : T -> T)
40 (incr_always_live : option T -> T).
41
42 Section with_ident.
43 Context {base_type : Type}.
44 Local Notation type := (type.type base_type).
45 Context {ident : type -> Type}.
46 Local Notation expr := (@expr.expr base_type ident).
47 Context {base_type_beq : base_type -> base_type -> bool}
48 {try_make_transport_base_type_cps : @type.try_make_transport_cpsT base_type}
49 {reflect_base_type_beq : reflect_rel (@eq base_type) base_type_beq}
50 {exprDefault : forall var, @DefaultValue.type.base.DefaultT type (@expr var)}
51 {try_make_transport_base_type_cps_correct : type.try_make_transport_cps_correctT base_type}.
52 Context (is_ident_always_live : forall t, ident t -> bool).
53
54 Section with_var.
55 Context {doing_subst_debug : forall T1 T2, T1 -> (unit -> T2) -> T1}
56 {should_subst : T -> bool}
57 (Hdoing_subst_debug : forall T1 T2 x y, doing_subst_debug T1 T2 x y = x)
58 {var1 var2 : type -> Type}.
59 Local Notation tree := (@tree_nd.tree (option T * (unit -> positive * list (positive * T)))).
60 Local Notation expr1 := (@expr.expr base_type ident var1).
61 Local Notation expr2 := (@expr.expr base_type ident var2).
62 Local Notation subst0n1 := (@subst0n T base_type ident doing_subst_debug var1 should_subst).
63 Local Notation subst0n2 := (@subst0n T base_type ident doing_subst_debug var2 should_subst).
64
65 Lemma wf_subst0n_gen live G1 G2 t e1 e2
66 (HG1G2 : forall t v1 v2, List.In (existT _ t (v1, v2)) G1 -> expr.wf G2 v1 v2)
67 : expr.wf G1 (t:=t) e1 e2 -> expr.wf G2 (subst0n1 live e1) (subst0n2 live e2).
68 Proof using Hdoing_subst_debug.
69 intro Hwf; revert dependent G2; revert live; induction Hwf;
70 cbn [subst0n];
71 repeat first [ progress wf_safe_t
72 | simplifier_t_step
73 | rewrite Hdoing_subst_debug
74 | break_innermost_match_step
75 | solve [ wf_t ] ].
76 Qed.
77
78 Lemma wf_subst0n live t e1 e2
79 : expr.wf nil (t:=t) e1 e2 -> expr.wf nil (subst0n1 live e1) (subst0n2 live e2).
80 Proof using Hdoing_subst_debug. intro Hwf; eapply wf_subst0n_gen, Hwf; wf_safe_t. Qed.
81 End with_var.
82
83 Lemma Wf_Subst0n
84 {doing_subst_debug : forall T1 T2, T1 -> (unit -> T2) -> T1}
85 {should_subst : T -> bool}
86 (Hdoing_subst_debug : forall T1 T2 x y, doing_subst_debug T1 T2 x y = x)
87 {t} (e : @expr.Expr base_type ident t)
88 : expr.Wf e -> expr.Wf (Subst0n one incr incr_always_live is_ident_always_live doing_subst_debug should_subst e).
89 Proof using try_make_transport_base_type_cps_correct.
90 intros Hwf var1 var2; cbv [Subst0n Let_In].
91 apply wf_subst0n, GeneralizeVar.Wf_FromFlat_ToFlat, Hwf; assumption.
92 Qed.
93
94 Section interp.
95 Context {base_interp : base_type -> Type}
96 {interp_ident : forall t, ident t -> type.interp base_interp t}
97 {interp_ident_Proper : forall t, Proper (eq ==> type.eqv) (interp_ident t)}.
98
99 Section with_doing_subst_debug.
100 Context {doing_subst_debug : forall T1 T2, T1 -> (unit -> T2) -> T1}
101 {should_subst : T -> bool}
102 (Hdoing_subst_debug : forall T1 T2 x y, doing_subst_debug T1 T2 x y = x).
103
104 Lemma interp_subst0n_gen live G t (e1 e2 : expr t)
105 (HG : forall t v1 v2, List.In (existT _ t (v1, v2)) G -> expr.interp interp_ident v1 == v2)
106 (Hwf : expr.wf G e1 e2)
107 : expr.interp interp_ident (subst0n doing_subst_debug should_subst live e1) == expr.interp interp_ident e2.
108 Proof using Hdoing_subst_debug interp_ident_Proper.
109 revert live; induction Hwf; cbn [subst0n]; cbv [Proper respectful] in *;
110 repeat first [ progress interp_safe_t
111 | simplifier_t_step
112 | rewrite Hdoing_subst_debug
113 | break_innermost_match_step
114 | interp_unsafe_t_step ].
115 Qed.
116
117 Lemma interp_subst0n live t (e1 e2 : expr t)
118 (Hwf : expr.wf nil e1 e2)
119 : expr.interp interp_ident (subst0n doing_subst_debug should_subst live e1) == expr.interp interp_ident e2.
120 Proof using Hdoing_subst_debug interp_ident_Proper. clear -Hwf Hdoing_subst_debug interp_ident_Proper. apply interp_subst0n_gen with (G:=nil); cbn [In]; eauto with nocore; tauto. Qed.
121
122 Lemma Interp_Subst0n {t} (e : @expr.Expr base_type ident t) (Hwf : expr.Wf e)
123 : expr.Interp interp_ident (Subst0n one incr incr_always_live is_ident_always_live doing_subst_debug should_subst e) == expr.Interp interp_ident e.
124 Proof using Hdoing_subst_debug interp_ident_Proper try_make_transport_base_type_cps_correct.
125 cbv [Subst0n Let_In expr.Interp].
126 etransitivity; [ apply interp_subst0n, GeneralizeVar.Wf_FromFlat_ToFlat, Hwf | ].
127 apply GeneralizeVar.Interp_gen1_FromFlat_ToFlat; assumption.
128 Qed.
129 End with_doing_subst_debug.
130 End interp.
131 End with_ident.
132 End with_counter.
133
134 Section with_ident.
135 Context {base_type : Type}.
136 Local Notation type := (type.type base_type).
137 Context {ident : type -> Type}.
138 Local Notation expr := (@expr.expr base_type ident).
139 Context {base_type_beq : base_type -> base_type -> bool}
140 {try_make_transport_base_type_cps : @type.try_make_transport_cpsT base_type}
141 {reflect_base_type_beq : reflect_rel (@eq base_type) base_type_beq}
142 {exprDefault : forall var, @DefaultValue.type.base.DefaultT type (@expr var)}
143 {try_make_transport_base_type_cps_correct : type.try_make_transport_cps_correctT base_type}.
144 Context (is_ident_always_live : forall t, ident t -> bool).
145
146 Lemma Wf_Subst01 {t} (e : @expr.Expr base_type ident t)
147 : expr.Wf e -> expr.Wf (Subst01 is_ident_always_live e).
148 Proof using try_make_transport_base_type_cps_correct. eapply Wf_Subst0n; try assumption; reflexivity. Qed.
149
150 Section interp.
151 Context {base_interp : base_type -> Type}
152 {interp_ident : forall t, ident t -> type.interp base_interp t}
153 {interp_ident_Proper : forall t, Proper (eq ==> type.eqv) (interp_ident t)}.
154 Lemma Interp_Subst01 {t} (e : @expr.Expr base_type ident t) (Hwf : expr.Wf e)
155 : expr.Interp interp_ident (Subst01 is_ident_always_live e) == expr.Interp interp_ident e.
156 Proof using interp_ident_Proper try_make_transport_base_type_cps_correct. eapply Interp_Subst0n, Hwf; auto. Qed.
157 End interp.
158 End with_ident.
159
160 Ltac autorewrite_interp_side_condition_solver := assumption.
161 End Subst01.
162
163 #[global]
164 Hint Resolve Subst01.Wf_Subst01 : wf.
165 #[global]
166 Hint Opaque Subst01.Subst01 : wf interp rewrite.
167 #[global]
168 Hint Rewrite @Subst01.Interp_Subst01 using Subst01.autorewrite_interp_side_condition_solver : interp.
169
170 Module DeadCodeElimination.
171 Import MiscCompilerPasses.Compilers.DeadCodeElimination.
172
173 (** Rather than proving correctness of the computation of live
174 variables and requiring something about [live], we instead
175 rely on the fact that DCE in fact just inlines code it
176 believes to be dead. *)
177 Local Transparent OUGHT_TO_BE_UNUSED.
178 Lemma OUGHT_TO_BE_UNUSED_id {T1 T2} v v' : @OUGHT_TO_BE_UNUSED T1 T2 v v' = v.
179 Proof. exact eq_refl. Qed.
180 Local Opaque OUGHT_TO_BE_UNUSED.
181
182 Section with_ident.
183 Context {base_type : Type}.
184 Local Notation type := (type.type base_type).
185 Context {ident : type -> Type}.
186 Local Notation expr := (@expr.expr base_type ident).
187 Context {base_type_beq : base_type -> base_type -> bool}
188 {try_make_transport_base_type_cps : @type.try_make_transport_cpsT base_type}
189 {reflect_base_type_beq : reflect_rel (@eq base_type) base_type_beq}
190 {exprDefault : forall var, @DefaultValue.type.base.DefaultT type (@expr var)}
191 {try_make_transport_base_type_cps_correct : type.try_make_transport_cps_correctT base_type}.
192 Context (is_ident_always_live : forall t, ident t -> bool).
193
194 Lemma Wf_EliminateDead {t} (e : @expr.Expr base_type ident t)
195 : expr.Wf e -> expr.Wf (EliminateDead is_ident_always_live e).
196 Proof using try_make_transport_base_type_cps_correct. eapply Subst01.Wf_Subst0n; auto using @OUGHT_TO_BE_UNUSED_id. Qed.
197
198 Section interp.
199 Context {base_interp : base_type -> Type}
200 {interp_ident : forall t, ident t -> type.interp base_interp t}
201 {interp_ident_Proper : forall t, Proper (eq ==> type.eqv) (interp_ident t)}.
202
203 Lemma Interp_EliminateDead {t} (e : @expr.Expr base_type ident t) (Hwf : expr.Wf e)
204 : expr.Interp interp_ident (EliminateDead is_ident_always_live e) == expr.Interp interp_ident e.
205 Proof using interp_ident_Proper try_make_transport_base_type_cps_correct. eapply Subst01.Interp_Subst0n, Hwf; auto using @OUGHT_TO_BE_UNUSED_id. Qed.
206 End interp.
207 End with_ident.
208 Ltac autorewrite_interp_side_condition_solver := Subst01.autorewrite_interp_side_condition_solver.
209 End DeadCodeElimination.
210
211 #[global]
212 Hint Resolve DeadCodeElimination.Wf_EliminateDead : wf.
213 #[global]
214 Hint Opaque DeadCodeElimination.EliminateDead : wf interp rewrite.
215 #[global]
216 Hint Rewrite @DeadCodeElimination.Interp_EliminateDead using DeadCodeElimination.autorewrite_interp_side_condition_solver : interp.
217 End Compilers.