| 1 |
From Coq Require Import ZArith. |
| 2 |
From Coq Require Import MSetPositive. |
| 3 |
From Coq Require Import FMapPositive. |
| 4 |
Require Import Crypto.Util.ListUtil Coq.Lists.List. |
| 5 |
Require Import Rewriter.Language.Language. |
| 6 |
Require Import Crypto.Util.LetIn. |
| 7 |
Require Import Crypto.Util.Notations. |
| 8 |
Require Import Crypto.Language.TreeCaching. |
| 9 |
Import ListNotations. Local Open Scope Z_scope. |
| 10 |
|
| 11 |
Module Compilers. |
| 12 |
Export Language.Compilers. |
| 13 |
Export Language.TreeCaching.Compilers. |
| 14 |
Import invert_expr. |
| 15 |
|
| 16 |
Module Subst01. |
| 17 |
Section with_counter. |
| 18 |
Context {t : Type} |
| 19 |
(one : t) |
| 20 |
(incr : t -> t) |
| 21 |
(incr_always_live : option t -> t). |
| 22 |
|
| 23 |
Local Notation PositiveMap_incr idx m |
| 24 |
:= (PositiveMap.add idx (match PositiveMap.find idx m with |
| 25 |
| Some n => incr n |
| 26 |
| None => one |
| 27 |
end) m). |
| 28 |
|
| 29 |
Local Notation PositiveMap_incr_always_live idx m |
| 30 |
:= (PositiveMap.add idx (incr_always_live (PositiveMap.find idx m)) m). |
| 31 |
|
| 32 |
Section with_ident. |
| 33 |
Context {base_type : Type}. |
| 34 |
Local Notation type := (type.type base_type). |
| 35 |
Context {ident : type -> Type} |
| 36 |
(** some identifiers, like [comment], might always be live *) |
| 37 |
(is_ident_always_live : forall t, ident t -> bool). |
| 38 |
Local Notation expr := (@expr.expr base_type ident). |
| 39 |
(* [option t] is "is the let-in here live?", meaningless elsewhere; the thunk is for debugging *) |
| 40 |
Local Notation tree := (@tree_nd.tree (option t * (unit -> positive * list (positive * t)))). |
| 41 |
(** N.B. This does not work well when let-binders are not at top-level *) |
| 42 |
Fixpoint contains_always_live_ident {var} (dummy : forall t, var t) {t} (e : @expr var t) |
| 43 |
: bool |
| 44 |
:= match e with |
| 45 |
| expr.Var _ _ => false |
| 46 |
| expr.Ident t idc => is_ident_always_live _ idc |
| 47 |
| expr.App s d f x |
| 48 |
=> contains_always_live_ident dummy f || contains_always_live_ident dummy x |
| 49 |
| expr.Abs s d f |
| 50 |
=> contains_always_live_ident dummy (f (dummy _)) |
| 51 |
| expr.LetIn tx tC ex eC |
| 52 |
=> contains_always_live_ident dummy ex || contains_always_live_ident dummy (eC (dummy _)) |
| 53 |
end%bool. |
| 54 |
Definition meaningless : option t * (unit -> positive * list (positive * t)) := (None, (fun 'tt => (1%positive, []%list))). |
| 55 |
Global Opaque meaningless. |
| 56 |
Fixpoint compute_live_counts' {t} (e : @expr (fun _ => positive) t) (cur_idx : positive) (live : PositiveMap.t _) |
| 57 |
: positive * PositiveMap.t _ * option tree |
| 58 |
:= match e with |
| 59 |
| expr.Var t v |
| 60 |
=> let '(idx, live) := (cur_idx, PositiveMap_incr v live) in |
| 61 |
(idx, live, Some (tree_nd.Var meaningless)) |
| 62 |
| expr.Ident t idc |
| 63 |
=> let '(idx, live) := (cur_idx, live) in |
| 64 |
(idx, live, Some (tree_nd.Ident meaningless)) |
| 65 |
| expr.App s d f x |
| 66 |
=> let '(idx, live, f_tree) := @compute_live_counts' _ f cur_idx live in |
| 67 |
let '(idx, live, x_tree) := @compute_live_counts' _ x idx live in |
| 68 |
(idx, live, Some (tree_nd.App meaningless f_tree x_tree)) |
| 69 |
| expr.Abs s d f |
| 70 |
=> let '(idx, live, f_tree) := @compute_live_counts' _ (f cur_idx) (Pos.succ cur_idx) live in |
| 71 |
(idx, live, Some (tree_nd.Abs meaningless f_tree)) |
| 72 |
| expr.LetIn tx tC ex eC |
| 73 |
=> let '(idx, live, C_tree) := @compute_live_counts' tC (eC cur_idx) (Pos.succ cur_idx) live in |
| 74 |
let live := if contains_always_live_ident (fun _ => cur_idx (* dummy *)) ex |
| 75 |
then PositiveMap_incr_always_live cur_idx live |
| 76 |
else live in |
| 77 |
let debug_info := fun 'tt => (Pos.succ cur_idx, PositiveMap.elements live) in |
| 78 |
match PositiveMap.find cur_idx live with |
| 79 |
| Some x_count |
| 80 |
=> let '(x_idx, x_live, x_tree) := @compute_live_counts' tx ex idx live in |
| 81 |
(x_idx, x_live, Some (tree_nd.LetIn (Some x_count, debug_info) x_tree C_tree)) |
| 82 |
| None |
| 83 |
=> (idx, live, Some (tree_nd.LetIn (None, debug_info) None C_tree)) |
| 84 |
end |
| 85 |
end%bool. |
| 86 |
Definition compute_live_counts {t} e : option tree := snd (@compute_live_counts' t e 1 (PositiveMap.empty _)). |
| 87 |
Definition ComputeLiveCounts {t} (e : expr.Expr t) := compute_live_counts (e _). |
| 88 |
|
| 89 |
Section with_var. |
| 90 |
(* [doing_subst_debug] is a hook for something like |
| 91 |
reduction-effects debug print, or for catching bad |
| 92 |
substitutions as in [OUGHT_TO_BE_UNUSED] below, so we can |
| 93 |
debug subst01; we forcibly stuff its second argument into |
| 94 |
a thunk so that it does not impact performance in cbv nor |
| 95 |
in extraction *) |
| 96 |
Context (doing_subst_debug : forall T1 T2, T1 -> (unit -> T2) -> T1) |
| 97 |
{var : type -> Type} |
| 98 |
(should_subst : t -> bool). |
| 99 |
(** When [live] is [None], we don't inline anything, just |
| 100 |
dropping [var]. This is required for preventing blowup |
| 101 |
in inlining lets in unused [LetIn]-bound expressions. |
| 102 |
*) |
| 103 |
Fixpoint subst0n (live : option tree) {t} (e : @expr (@expr var) t) |
| 104 |
: @expr var t |
| 105 |
:= match e with |
| 106 |
| expr.Var t v => v |
| 107 |
| expr.Ident t idc => expr.Ident idc |
| 108 |
| expr.App s d f x |
| 109 |
=> let '(f_live, x_live) |
| 110 |
:= match live with |
| 111 |
| Some (tree_nd.App _ f_live x_live) => (f_live, x_live) |
| 112 |
| _ => (None, None) |
| 113 |
end%core in |
| 114 |
let f' := @subst0n f_live _ f in |
| 115 |
let x' := @subst0n x_live _ x in |
| 116 |
expr.App f' x' |
| 117 |
| expr.Abs s d f |
| 118 |
=> let f_tree |
| 119 |
:= match live with |
| 120 |
| Some (tree_nd.Abs _ f_tree) => f_tree |
| 121 |
| _ => None |
| 122 |
end in |
| 123 |
expr.Abs (fun v => @subst0n f_tree _ (f (expr.Var v))) |
| 124 |
| expr.LetIn tx tC ex eC |
| 125 |
=> match live with |
| 126 |
| Some (tree_nd.LetIn (x_count, debug_info) x_tree C_tree) |
| 127 |
=> let ex' := @subst0n x_tree tx ex in |
| 128 |
let eC' := fun v => @subst0n C_tree tC (eC v) in |
| 129 |
if match x_count with |
| 130 |
| Some n => should_subst n |
| 131 |
| None => true |
| 132 |
end |
| 133 |
then eC' (doing_subst_debug _ _ ex' debug_info) |
| 134 |
else expr.LetIn ex' (fun v => eC' (expr.Var v)) |
| 135 |
| _ |
| 136 |
=> let ex' := @subst0n None tx ex in |
| 137 |
let eC' := fun v => @subst0n None tC (eC v) in |
| 138 |
expr.LetIn ex' (fun v => eC' (expr.Var v)) |
| 139 |
end |
| 140 |
end. |
| 141 |
End with_var. |
| 142 |
|
| 143 |
Section with_transport. |
| 144 |
Context {try_make_transport_base_type_cps : @type.try_make_transport_cpsT base_type} |
| 145 |
{exprDefault : forall var, @DefaultValue.type.base.DefaultT type (@expr var)}. |
| 146 |
(** We pass through [Flat] to ensure that the passed in |
| 147 |
[Expr] only gets invoked at a single [var] type *) |
| 148 |
Definition Subst0n (doing_subst_debug : forall T1 T2, T1 -> (unit -> T2) -> T1) (should_subst : t -> bool) {t} (E : expr.Expr t) : expr.Expr t |
| 149 |
:= dlet_nd e := GeneralizeVar.ToFlat E in |
| 150 |
let E := GeneralizeVar.FromFlat e in |
| 151 |
fun var => subst0n doing_subst_debug should_subst (ComputeLiveCounts E) (E _). |
| 152 |
End with_transport. |
| 153 |
End with_ident. |
| 154 |
End with_counter. |
| 155 |
|
| 156 |
Section for_01. |
| 157 |
Inductive one_or_more := one | more. |
| 158 |
Local Notation t := one_or_more. |
| 159 |
Let incr : t -> t := fun _ => more. |
| 160 |
Let should_subst : t -> bool |
| 161 |
:= fun v => match v with |
| 162 |
| one => true |
| 163 |
| more => false |
| 164 |
end. |
| 165 |
|
| 166 |
Definition Subst01 |
| 167 |
{base_type ident} |
| 168 |
{try_make_transport_base_type_cps : @type.try_make_transport_cpsT base_type} |
| 169 |
{exprDefault : forall var, @DefaultValue.type.base.DefaultT _ _} |
| 170 |
(is_ident_always_live : forall t, ident t -> bool) |
| 171 |
{t} (e : expr.Expr t) : expr.Expr t |
| 172 |
:= @Subst0n _ one incr (fun _ => more) base_type ident is_ident_always_live try_make_transport_base_type_cps exprDefault (fun _ _ x _ => x) should_subst t e. |
| 173 |
End for_01. |
| 174 |
End Subst01. |
| 175 |
|
| 176 |
Module DeadCodeElimination. |
| 177 |
Section with_ident. |
| 178 |
Context {base_type : Type}. |
| 179 |
Local Notation type := (type.type base_type). |
| 180 |
Definition OUGHT_TO_BE_UNUSED {T1 T2} (v : T1) (v' : T2) := v. |
| 181 |
Global Opaque OUGHT_TO_BE_UNUSED. |
| 182 |
Definition EliminateDead |
| 183 |
{ident : type -> Type} |
| 184 |
{try_make_transport_base_type_cps : @type.try_make_transport_cpsT base_type} |
| 185 |
{exprDefault : forall var, @DefaultValue.type.base.DefaultT _ _} |
| 186 |
(is_ident_always_live : forall t, ident t -> bool) |
| 187 |
{t} (e : expr.Expr t) |
| 188 |
: expr.Expr t |
| 189 |
:= @Subst01.Subst0n unit tt (fun _ => tt) (fun _ => tt) base_type ident is_ident_always_live try_make_transport_base_type_cps exprDefault (fun T1 T2 => OUGHT_TO_BE_UNUSED) (fun _ => false) t e. |
| 190 |
End with_ident. |
| 191 |
End DeadCodeElimination. |
| 192 |
End Compilers. |