Skip to content

Commit 1907ccf

Browse files
committed
Inline min/max
1 parent 5e9ba5b commit 1907ccf

File tree

1 file changed

+36
-6
lines changed

1 file changed

+36
-6
lines changed

compiler/lib/inline.ml

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -237,17 +237,39 @@ let sum ~context f pc =
237237
blocks
238238
0
239239

240-
let rec block_size ~recurse ~context { branch; body; _ } =
240+
let rec block_size ~inline_comparisons ~recurse ~context { branch; body; _ } =
241241
List.fold_left
242242
~f:(fun n i ->
243243
match i with
244244
| Event _ -> n
245+
| Let
246+
( _
247+
, Prim
248+
( Extern
249+
( "caml_lessthan"
250+
| "caml_lessequal"
251+
| "caml_greaterthan"
252+
| "caml_greaterequal"
253+
| "caml_equal"
254+
| "caml_notequal" )
255+
, _ ) )
256+
when inline_comparisons ->
257+
(* Bias toward inlining functions containing polymorphic
258+
comparisons, such as min and max, in the hope that
259+
polymorphic comparisons can be specialized. *)
260+
n - 1
245261
| Let (f, Closure (_, (pc, _), _)) ->
246262
if recurse
247263
then
248264
match Var.Map.find f context.env with
249-
| exception Not_found -> size ~recurse ~context pc + n + 1
250-
| info -> cache ~info info.full_size (size ~recurse:true ~context) + n + 1
265+
| exception Not_found -> size ~inline_comparisons ~recurse ~context pc + n + 1
266+
| info ->
267+
cache
268+
~info
269+
info.full_size
270+
(size ~inline_comparisons ~recurse:true ~context)
271+
+ n
272+
+ 1
251273
else n + 1
252274
| _ -> n + 1)
253275
~init:
@@ -257,13 +279,21 @@ let rec block_size ~recurse ~context { branch; body; _ } =
257279
| _ -> 0)
258280
body
259281

260-
and size ~recurse ~context = sum ~context (block_size ~recurse ~context)
282+
and size ~inline_comparisons ~recurse ~context =
283+
sum ~context (block_size ~inline_comparisons ~recurse ~context)
261284

262285
(** Size of the function body *)
263-
let body_size ~context info = cache ~info info.body_size (size ~recurse:false ~context)
286+
let body_size ~context info =
287+
let inline_comparisons =
288+
match Config.target () with
289+
| `JavaScript -> false
290+
| `Wasm -> true
291+
in
292+
cache ~info info.body_size (size ~inline_comparisons ~recurse:false ~context)
264293

265294
(** Size of the function, including the size of the closures it contains *)
266-
let full_size ~context info = cache ~info info.full_size (size ~recurse:true ~context)
295+
let full_size ~context info =
296+
cache ~info info.full_size (size ~inline_comparisons:false ~recurse:true ~context)
267297

268298
let closure_count_uncached ~context =
269299
sum ~context (fun { body; _ } ->

0 commit comments

Comments
 (0)