forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlam_beta_reduce.ml
315 lines (286 loc) · 10.5 KB
/
lam_beta_reduce.ml
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
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* In addition to the permissions granted to you by the LGPL, you may combine
* or link a "work that uses the Library" with a publicly distributed version
* of this file to produce a combined library or application, then distribute
* that combined work under the terms of your choosing, with no requirement
* to comply with the obligations normally placed on you by section 4 of the
* LGPL version 3 (or the corresponding section of a later version of the LGPL
* should you choose to use a later version).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
(*
Given an [map], rewrite all let bound variables into new variables,
note that the [map] is changed
example
{[
let a/112 = 3 in a/112
]}
would be converted into
{[
let a/113 = 3 in a/113
]}
ATTENTION: [let] bound idents have to be renamed,
Note we rely on an invariant that parameter could not be rebound
*)
(*
Small function inline heuristics:
Even if a function is small, it does not mean it is good for inlining,
for example, in list.ml
{[
let rec length_aux len = function
[] -> len
| a::l -> length_aux (len + 1) l
let length l = length_aux 0 l
]}
if we inline [length], it will expose [length_aux] to the user, first, it make
the code not very friendly, also since [length_aux] is used everywhere now, it
may affect that we will not do the inlining of [length_aux] in [length]
Criteior for sure to inline
1. small size, does not introduce extra symbols, non-exported and non-recursive
non-recursive is required if we re-apply the strategy
Other Factors:
2. number of invoked times
3. arguments are const or not
*)
let rewrite (map : _ Ident_hashtbl.t)
(lam : Lam.t) : Lam.t =
let rebind i =
let i' = Ident.rename i in
Ident_hashtbl.add map i (Lam.var i');
i' in
(* order matters, especially for let bindings *)
let rec
option_map op =
match op with
| None -> None
| Some x -> Some (aux x)
and aux (lam : Lam.t) : Lam.t =
match lam with
| Lvar v ->
Ident_hashtbl.find_default map v lam
| Llet(str, v, l1, l2) ->
let v = rebind v in
let l1 = aux l1 in
let l2 = aux l2 in
Lam.let_ str v l1 l2
| Lletrec(bindings, body) ->
(*order matters see GPR #405*)
let vars = List.map (fun (k, _) -> rebind k) bindings in
let bindings = List.map2 (fun var (_,l) -> var, aux l) vars bindings in
let body = aux body in
Lam.letrec bindings body
| Lfunction{arity; kind; params; body} ->
let params = List.map rebind params in
let body = aux body in
Lam.function_ ~arity ~kind ~params ~body
| Lstaticcatch(l1, (i,xs), l2) ->
let l1 = aux l1 in
let xs = List.map rebind xs in
let l2 = aux l2 in
Lam.staticcatch l1 (i,xs) l2
| Lfor(ident, l1, l2, dir, l3) ->
let ident = rebind ident in
let l1 = aux l1 in
let l2 = aux l2 in
let l3 = aux l3 in
Lam.for_ ident (aux l1) l2 dir l3
| Lconst _ -> lam
| Lprim {primitive; args ; loc} ->
(* here it makes sure that global vars are not rebound *)
Lam.prim ~primitive ~args:(List.map aux args) loc
| Lglobal_module _ -> lam
| Lapply {fn; args; loc; status } ->
let fn = aux fn in
let args = List.map aux args in
Lam.apply fn args loc status
| Lswitch(l, {sw_failaction;
sw_consts;
sw_blocks;
sw_numblocks;
sw_numconsts;
}) ->
let l = aux l in
Lam.switch l
{sw_consts =
List.map (fun (v, l) -> v, aux l) sw_consts;
sw_blocks = List.map (fun (v, l) -> v, aux l) sw_blocks;
sw_numconsts = sw_numconsts;
sw_numblocks = sw_numblocks;
sw_failaction = option_map sw_failaction
}
| Lstringswitch(l, sw, d) ->
let l = aux l in
Lam.stringswitch l
(List.map (fun (i, l) -> i,aux l) sw)
(option_map d)
| Lstaticraise (i,ls)
-> Lam.staticraise i (List.map aux ls)
| Ltrywith(l1, v, l2) ->
let l1 = aux l1 in
let v = rebind v in
let l2 = aux l2 in
Lam.try_ l1 v l2
| Lifthenelse(l1, l2, l3) ->
let l1 = aux l1 in
let l2 = aux l2 in
let l3 = aux l3 in
Lam.if_ l1 l2 l3
| Lsequence(l1, l2) ->
let l1 = aux l1 in
let l2 = aux l2 in
Lam.seq l1 l2
| Lwhile(l1, l2) ->
let l1 = aux l1 in
let l2 = aux l2 in
Lam.while_ l1 l2
| Lassign(v, l)
-> Lam.assign v (aux l)
| Lsend(u, m, o, ll, v) ->
let m = aux m in
let o = aux o in
let ll = List.map aux ll in
Lam.send u m o ll v
| Lifused(v, l) ->
let l = aux l in
Lam.ifused v l
in
aux lam
let refresh lam = rewrite (Ident_hashtbl.create 17 : Lam.t Ident_hashtbl.t ) lam
(*
A naive beta reduce would break the invariants of the optmization.
The sane but slowest way:
when we do a beta reduction, we need rename all variables inlcuding
let-bound ones
A conservative one:
- for internal one
rename params and let bound variables
- for external one (seriaized)
if it's enclosed environment should be good enough
so far, we only inline enclosed lambdas
TODO: rename
Optimizations:
{[
(fun x y -> ... ) 100 3
]}
we can bound [x] to [100] in a single step
*)
let propogate_beta_reduce
(meta : Lam_stats.meta) params body args =
match Lam_beta_reduce_util.simple_beta_reduce params body args with
| Some x -> x
| None ->
let rest_bindings, rev_new_params =
List.fold_left2
(fun (rest_bindings, acc) old_param (arg : Lam.t) ->
match arg with
| Lconst _
| Lvar _ -> rest_bindings , arg :: acc
| _ ->
let p = Ident.rename old_param in
(p,arg) :: rest_bindings , (Lam.var p) :: acc
) ([],[]) params args in
let new_body = rewrite (Ident_hashtbl.of_list2 (List.rev params) (rev_new_params)) body in
List.fold_right
(fun (param, (arg : Lam.t)) l ->
let arg =
match arg with
| Lvar v ->
begin
match Ident_hashtbl.find_opt meta.ident_tbl v with
| None -> ()
| Some ident_info ->
Ident_hashtbl.add meta.ident_tbl param ident_info
end;
arg
| Lglobal_module ident
->
(* It's not completeness, its to make it sound..
Pass global module as an argument
*)
Lam_compile_global.query_lambda ident meta.env
(* alias meta param ident (Module (Global ident)) Strict *)
| Lprim {primitive = Pmakeblock (_, _, Immutable) ;args ; _} ->
Ident_hashtbl.replace meta.ident_tbl param
(Lam_util.kind_of_lambda_block Normal args ); (** *)
arg
| _ -> arg in
Lam_util.refine_let ~kind:Strict param arg l)
rest_bindings new_body
let propogate_beta_reduce_with_map
(meta : Lam_stats.meta) (map : Lam_closure.stats Ident_map.t ) params body args =
match Lam_beta_reduce_util.simple_beta_reduce params body args with
| Some x -> x
| None ->
let rest_bindings, rev_new_params =
List.fold_left2
(fun (rest_bindings, acc) old_param (arg : Lam.t) ->
match arg with
| Lconst _
| Lvar _ -> rest_bindings , arg :: acc
| Lglobal_module ident
(* We can pass Global, but you also need keep track of it*)
->
let p = Ident.rename old_param in
(p,arg) :: rest_bindings , (Lam.var p) :: acc
| _ ->
if Lam_analysis.no_side_effects arg then
begin match Ident_map.find_exn old_param map with
| exception Not_found -> assert false
| {top = true ; times = 0 }
| {top = true ; times = 1 }
->
rest_bindings, arg :: acc
| _ ->
let p = Ident.rename old_param in
(p,arg) :: rest_bindings , (Lam.var p) :: acc
end
else
let p = Ident.rename old_param in
(p,arg) :: rest_bindings , (Lam.var p) :: acc
) ([],[]) params args in
let new_body = rewrite (Ident_hashtbl.of_list2 (List.rev params) (rev_new_params)) body in
List.fold_right
(fun (param, (arg : Lam.t)) l ->
let arg =
match arg with
| Lvar v ->
begin
match Ident_hashtbl.find_opt meta.ident_tbl v with
| None -> ()
| Some ident_info ->
Ident_hashtbl.add meta.ident_tbl param ident_info
end;
arg
| Lglobal_module ident
->
(* It's not completeness, its to make it sound.. *)
Lam_compile_global.query_lambda ident meta.env
(* alias meta param ident (Module (Global ident)) Strict *)
| Lprim {primitive = Pmakeblock (_, _, Immutable ) ; args} ->
Ident_hashtbl.replace meta.ident_tbl param
(Lam_util.kind_of_lambda_block Normal args ); (** *)
arg
| _ -> arg in
Lam_util.refine_let ~kind:Strict param arg l)
rest_bindings new_body
let beta_reduce params body args =
match Lam_beta_reduce_util.simple_beta_reduce params body args with
| Some x -> x
| None ->
List.fold_left2
(fun l param arg ->
Lam_util.refine_let ~kind:Strict param arg l)
body params args