-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathInstanceListSema.v
More file actions
397 lines (358 loc) · 11.6 KB
/
Copy pathInstanceListSema.v
File metadata and controls
397 lines (358 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
(** instance list semantics *)
Require Import Bool.
Require Import Base.
Require Import List.
Require Import SetoidList.
Require Import MSets.
Require Import Coq.MSets.MSetProperties.
Require Import Setoid Morphisms.
Require Import Linalg.
Require Import Base.
Require Import LinalgExt.
Require Import Classical.
Require Import ZArith.
Require Import PolyBase.
Require Import Misc.
Require Import Sorting.Sorted.
Require Import Permutation.
Require Import Coqlib.
Require Import LibTactics.
Require Import sflib.
Import ListNotations.
Require Import StateTy.
Require Import InstrTy.
Require Import AST.
Require Import OpenScop.
Require Import Result.
Import ListNotations.
Module ILSema (Instr: INSTR).
Module State := Instr.State.
Record InstrPoint := {
ip_nth: nat; (** belongs to nth polyhedral instruction *)
ip_index: DomIndex; (** index of the domain, i.e., iterator's value *)
ip_transformation: Transformation; (** transformation function *)
ip_time_stamp: TimeStamp; (** schedule *)
ip_instruction: Instr.t; (** basic instruction *)
ip_depth: nat; (** surrounded iterator depth *)
}.
Definition naive_instr_point: InstrPoint :=
{|
ip_nth := 0;
ip_index := nil;
ip_transformation := nil;
ip_time_stamp := nil;
ip_instruction := Instr.dummy_instr;
ip_depth := 0;
|}.
Definition eq_except_sched (ip1 ip2: InstrPoint): Prop :=
ip1.(ip_nth) = ip2.(ip_nth) /\
ip1.(ip_index) = ip2.(ip_index) /\
ip1.(ip_transformation) = ip2.(ip_transformation) /\
ip1.(ip_instruction) = ip2.(ip_instruction) /\
ip1.(ip_depth) = ip2.(ip_depth).
Inductive instr_point_sema (ip: InstrPoint)
(st1 st2: State.t): Prop :=
| ip_sema_intro: forall wcs rcs,
Instr.instr_semantics ip.(ip_instruction)
(affine_product ip.(ip_transformation) ip.(ip_index)) wcs rcs st1 st2 ->
instr_point_sema ip st1 st2.
Definition instr_point_sched_le (ip1 ip2: InstrPoint): Prop :=
lex_compare ip1.(ip_time_stamp) ip2.(ip_time_stamp) = Lt \/
lex_compare ip1.(ip_time_stamp) ip2.(ip_time_stamp) = Eq.
Lemma instr_point_sched_le_trans:
forall ip1 ip2 ip3,
instr_point_sched_le ip1 ip2 ->
instr_point_sched_le ip2 ip3 ->
instr_point_sched_le ip1 ip3.
Proof.
intros. unfolds instr_point_sched_le.
destruct H; destruct H0.
- left. eapply lex_compare_trans; eauto.
- left.
rewrite <- is_eq_iff_cmp_eq in H0.
eapply lex_compare_right_eq with (t1:=ip_time_stamp ip1) in H0; eauto.
rewrite <- H0; trivial.
- left.
rewrite <- is_eq_iff_cmp_eq in H.
eapply lex_compare_left_eq with (t3:=ip_time_stamp ip3) in H; eauto.
rewrite H; trivial.
- right. eapply lex_compare_trans; eauto.
Qed.
Definition instr_point_sched_ltb (ip1 ip2: InstrPoint): bool :=
comparison_eqb (lex_compare ip1.(ip_time_stamp) ip2.(ip_time_stamp)) Lt.
Definition instr_point_sched_eqb (ip1 ip2: InstrPoint): bool :=
comparison_eqb (lex_compare ip1.(ip_time_stamp) ip2.(ip_time_stamp)) Eq.
Definition instr_point_sched_lt (ip1 ip2: InstrPoint): Prop :=
instr_point_sched_ltb ip1 ip2 = true.
Definition instr_point_sched_eq (ip1 ip2: InstrPoint): Prop :=
instr_point_sched_eqb ip1 ip2 = true.
Definition Permutable (ip1 ip2: InstrPoint) :=
forall st1,
Instr.NonAlias st1 ->
(forall st2' st3,
instr_point_sema ip1 st1 st2' ->
instr_point_sema ip2 st2' st3 ->
exists st2'' st3',
instr_point_sema ip2 st1 st2'' /\
instr_point_sema ip1 st2'' st3' /\
Instr.State.eq st3 st3'
) /\
(forall st2' st3,
instr_point_sema ip2 st1 st2' ->
instr_point_sema ip1 st2' st3 ->
exists st2'' st3',
instr_point_sema ip1 st1 st2'' /\
instr_point_sema ip2 st2'' st3' /\
Instr.State.eq st3 st3'
).
Lemma Permutable_symm:
forall ip1 ip2,
Permutable ip1 ip2 ->
Permutable ip2 ip1.
Proof.
intros.
unfolds Permutable.
intros.
split.
eapply H; eauto.
eapply H; eauto.
Qed.
Inductive instr_point_list_semantics: list InstrPoint ->
State.t -> State.t -> Prop:=
| IPLS_nil: forall st st',
Instr.State.eq st st' ->
instr_point_list_semantics [] st st'
| IPLS_cons: forall st1 st2 st3 ip il,
instr_point_sema ip st1 st2 ->
instr_point_list_semantics il st2 st3 ->
instr_point_list_semantics (ip::il) st1 st3.
Lemma instr_point_list_sema_stable_under_state_eq:
forall l st1 st2 st1' st2',
instr_point_list_semantics l st1 st2 ->
Instr.State.eq st1 st1' ->
Instr.State.eq st2 st2' ->
instr_point_list_semantics l st1' st2'.
Proof.
induction l.
-
intros.
inv H.
simpls. econs; eauto.
eapply Instr.State.eq_sym in H0.
eapply Instr.State.eq_trans; eauto.
eapply Instr.State.eq_trans; eauto.
-
intros. inv H.
inv H4.
eapply Instr.instr_semantics_stable_under_state_eq
with (st1':=st1') (st2:=st3) (st2':=st3) in H; eauto.
2: {eapply Instr.State.eq_refl. }
econs; eauto. instantiate (1:=st3). econs; eauto.
eapply IHl; eauto. eapply Instr.State.eq_refl.
Qed.
Definition veq_instance (ip1 ip2: InstrPoint): Prop :=
ip1.(ip_nth) = ip2.(ip_nth)
/\ veq ip1.(ip_index) ip2.(ip_index)
/\ ip1.(ip_transformation) = ip2.(ip_transformation)
/\ ip1.(ip_time_stamp) = ip2.(ip_time_stamp)
/\ ip1.(ip_instruction) = ip2.(ip_instruction)
/\ ip1.(ip_depth) = ip2.(ip_depth)
.
Lemma veq_instance_refl:
forall ip,
veq_instance ip ip.
Proof.
intros. destruct ip. unfold veq_instance; splits; simpls; trivial.
eapply veq_refl.
Qed.
Require Import Setoid Morphisms.
(* Require Import List. *)
Require Import SetoidList.
Require Import sflib.
Require Import LibTactics.
(* Require Import MSets. *)
Require Import Sorting.Sorted.
Instance ip_ts_eq: Equivalence instr_point_sched_eq.
Proof.
constructor.
- intros x. unfold instr_point_sched_eq.
unfold instr_point_sched_eqb.
rewrite lex_compare_reflexive. unfold comparison_eqb. simpl. trivial.
- intros x y Hxy. unfold instr_point_sched_eq in *.
unfold instr_point_sched_eqb in *.
rewrite lex_compare_antisym.
rewrite comparison_eqb_iff_eq in Hxy.
rewrite Hxy. simpl. trivial.
- intros x y z Hxy Hyz. unfold instr_point_sched_eq in *. unfolds instr_point_sched_eqb.
rewrite comparison_eqb_iff_eq in Hxy.
rewrite comparison_eqb_iff_eq in Hyz.
rewrite comparison_eqb_iff_eq.
eapply lex_compare_trans; eauto.
Qed.
Lemma instr_point_sched_le_antisym:
forall x1 x2,
instr_point_sched_le x1 x2 ->
instr_point_sched_le x2 x1 ->
instr_point_sched_eq x1 x2.
Proof.
intros.
unfolds instr_point_sched_le.
unfold instr_point_sched_eq.
unfold instr_point_sched_eqb.
rewrite comparison_eqb_iff_eq.
destruct H; destruct H0; try contradiction; eauto.
-
rewrite lex_compare_antisym in H.
rewrite H0 in H.
unfold CompOpp in H. trivial.
discriminate.
-
rewrite lex_compare_antisym in H.
rewrite H0 in H.
unfold CompOpp in H. trivial.
discriminate.
Qed.
Lemma strongly_sorted_lists_by_timestamp_equal :
forall l1 l2,
StronglySorted instr_point_sched_le l1 ->
StronglySorted instr_point_sched_le l2 ->
NoDupA instr_point_sched_eq l1 ->
NoDupA instr_point_sched_eq l2 ->
(forall x, In x l1 <-> In x l2) ->
l1 = l2.
Proof.
induction l1 as [|x1 l1 IH]; intros l2 Hs1 Hs2 Hnd1 Hnd2 Hin.
- destruct l2; [reflexivity |].
exfalso. apply (Hin i). apply in_eq.
- destruct l2 as [|x2 l2']; [> exfalso; apply (Hin x1); left; reflexivity |].
rename l1 into l1'.
assert (x1 = x2).
{
assert (Hin1: In x1 (x1:: l1')) by apply in_eq.
assert (Hin2: In x2 (x2 :: l2')) by apply in_eq.
assert (In x1 (x2 :: l2')). {
eapply Hin; eauto.
}
destruct H as [Heq | Hin_tail]; symmetry; trivial.
-
(* use NoDup to derive contradiction *)
assert (Hback: In x2 (x1 :: l1')). {
eapply Hin; eauto.
}
destruct Hback as [Heq' | Hin_tail'].
+ symmetry in Heq'. trivial.
+
assert (instr_point_sched_le x1 x2).
{
inv Hs1.
eapply Forall_forall with (x:=x2) in H2; eauto.
}
assert (instr_point_sched_le x2 x1).
{
inv Hs2.
eapply Forall_forall with (x:=x1) in H3; eauto.
}
assert (instr_point_sched_eq x1 x2). {
eapply instr_point_sched_le_antisym; eauto.
}
inv Hnd1.
eapply In_InA with (eqA:=instr_point_sched_eq) in Hin_tail'. 2: {eapply ip_ts_eq. }
clear - Hin_tail' H4 H1.
eapply InA_eqA with (y:=x1) in Hin_tail'; eauto.
{try contradiction. }
{eapply ip_ts_eq. }
{eapply ip_ts_eq. trivial. }
}
subst x2.
f_equal.
apply IH.
+ eapply StronglySorted_inv; eauto.
+ eapply StronglySorted_inv; eauto.
+
replace l1' with ([] ++ l1'); trivial.
eapply NoDupA_split; eauto.
+
replace l2' with ([] ++ l2'); trivial.
eapply NoDupA_split; eauto.
+ intros x. split; intro Hx.
* assert (In x (x1 :: l1')). { eapply in_cons; trivial. }
eapply Hin in H; eauto.
destruct H as [Hx1 | Hx1].
-- subst.
clear - Hnd1 Hx.
inv Hnd1. eapply In_InA with (eqA := instr_point_sched_eq ) in Hx; eauto. contradiction.
eapply ip_ts_eq.
-- trivial.
* assert (In x (x1 :: l2')). { eapply in_cons; trivial. }
eapply Hin in H; eauto.
destruct H as [Hx1 | Hx1].
-- subst.
clear - Hnd2 Hx.
inv Hnd2. eapply In_InA with (eqA := instr_point_sched_eq ) in Hx; eauto. contradiction.
eapply ip_ts_eq.
-- trivial.
Qed.
Lemma Sorted_incl_eq :
forall l1 l2,
Sorted instr_point_sched_le l1 ->
Sorted instr_point_sched_le l2 ->
(forall x, In x l1 <-> In x l2) ->
NoDupA instr_point_sched_eq l1 ->
NoDupA instr_point_sched_eq l2 ->
l1 = l2.
Proof.
intros l1 l2 Hs1 Hs2 Heq Hinj1 Hinj2.
pose proof instr_point_sched_le_trans as Htrans.
apply Sorted_StronglySorted in Hs1; auto.
apply Sorted_StronglySorted in Hs2; auto.
eapply strongly_sorted_lists_by_timestamp_equal; eauto.
Qed.
Lemma Sorted_same_ele_nodup_implies_sema_eq:
forall l1 l2 st1 st2,
Sorted instr_point_sched_le l1 ->
Sorted instr_point_sched_le l2 ->
NoDupA instr_point_sched_eq l1 ->
NoDupA instr_point_sched_eq l2 ->
(forall x, In x l1 <-> In x l2) ->
instr_point_list_semantics l1 st1 st2 ->
instr_point_list_semantics l2 st1 st2.
Proof.
intros l1 l2 st1 st2 Hs1 Hs2 Hd1 Hd2 Hin Hsem1.
assert (l1 = l2).
{ eapply Sorted_incl_eq; eauto. }
subst. trivial.
Qed.
Lemma Sorted_same_ele_nodup_implies_sema_eq_stable:
forall l1 l2 st1 st2 st1' st2',
Sorted instr_point_sched_le l1 ->
Sorted instr_point_sched_le l2 ->
NoDupA instr_point_sched_eq l1 ->
NoDupA instr_point_sched_eq l2 ->
(forall x, In x l1 <-> In x l2) ->
instr_point_list_semantics l1 st1 st2 ->
State.eq st1 st1' ->
State.eq st2 st2' ->
instr_point_list_semantics l2 st1' st2'.
Proof.
intros l1 l2 st1 st2 st1' st2' Hs1 Hs2 Hd1 Hd2 Hin Hsem1 Hst1 Hst2.
assert (l1 = l2).
{ eapply Sorted_incl_eq; eauto. }
eapply instr_point_list_sema_stable_under_state_eq with (st1':=st1') (st2':=st2') in Hsem1; eauto.
subst. trivial.
Qed.
Lemma instr_point_list_sema_concat:
forall l1 l2 st1 st2 st3,
instr_point_list_semantics l1 st1 st2 ->
instr_point_list_semantics l2 st2 st3 ->
instr_point_list_semantics (l1 ++ l2) st1 st3.
Proof.
intros l1 l2 st1 st2 st3 Hsem1 Hsem2.
induction Hsem1.
- (* Base case: l1 = [] *)
simpl. eapply instr_point_list_sema_stable_under_state_eq; eauto.
+ eapply Instr.State.eq_sym. trivial.
+ eapply Instr.State.eq_refl.
- (* Inductive case: l1 = ip :: il *)
simpl. econstructor; eauto.
Qed.
End ILSema.