Skip to content

Commit b9d902c

Browse files
hhugovouillon
andauthored
Misc: reduce usage of polymorphic ops (#1941)
Co-authored-by: Jérôme Vouillon <[email protected]>
1 parent 61f8b95 commit b9d902c

29 files changed

+217
-103
lines changed

compiler/bin-js_of_ocaml/check_runtime.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ open Js_of_ocaml_compiler
2323
let group_by_snd l =
2424
l
2525
|> List.sort_uniq ~cmp:(fun (n1, l1) (n2, l2) ->
26-
match Poly.compare l1 l2 with
26+
match List.compare ~cmp:String.compare l1 l2 with
2727
| 0 -> String.compare n1 n2
2828
| c -> c)
29-
|> List.group ~f:(fun (_, g1) (_, g2) -> Poly.equal g1 g2)
29+
|> List.group ~f:(fun (_, g1) (_, g2) -> List.equal ~eq:String.equal g1 g2)
3030

3131
let print_groups output l =
3232
List.iter l ~f:(fun group ->

compiler/bin-wasm_of_ocaml/compile.ml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,13 @@ let build_runtime ~runtime_file =
9696
in
9797
match
9898
List.find_opt Runtime_files.precompiled_runtimes ~f:(fun (flags, _) ->
99-
assert (
100-
List.length flags = List.length variables
101-
&& List.for_all2 ~f:(fun (k, _) (k', _) -> String.equal k k') flags variables);
102-
Poly.equal flags variables)
99+
assert (List.length flags = List.length variables);
100+
List.equal
101+
~eq:(fun (k1, v1) (k2, v2) ->
102+
assert (String.equal k1 k2);
103+
Wat_preprocess.value_equal v1 v2)
104+
flags
105+
variables)
103106
with
104107
| Some (_, contents) -> Fs.write_file ~name:runtime_file ~contents
105108
| None ->

compiler/lib-wasm/binaryen.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,8 @@ let optimize
127127
let level =
128128
match profile with
129129
| None -> 1
130-
| Some p -> fst (List.find ~f:(fun (_, p') -> Poly.equal p p') Driver.profiles)
130+
| Some p ->
131+
fst (List.find ~f:(fun (_, p') -> Driver.profile_equal p p') Driver.profiles)
131132
in
132133
command
133134
("wasm-opt"

compiler/lib-wasm/code_generation.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ module Arith = struct
378378
let* e' = e' in
379379
return
380380
(match e, e' with
381-
| W.Const (I32 n), W.Const (I32 n') when Poly.(n' < 31l) ->
381+
| W.Const (I32 n), W.Const (I32 n') when Int32.(n' < 31l) ->
382382
W.Const (I32 (Int32.shift_left n (Int32.to_int n')))
383383
| _ -> W.BinOp (I32 Shl, e, e'))
384384

compiler/lib-wasm/link.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -868,7 +868,7 @@ let add_source_map files z sm =
868868
Wasm_source_map.iter_sources sm (fun i j file ->
869869
let z', files =
870870
match !st with
871-
| Some (i', st) when Poly.equal i i' -> st
871+
| Some (i', st) when Option.equal ( = ) i i' -> st
872872
| _ ->
873873
let st' = get_source_map_files ~tmp_buf files src_index in
874874
finalize ();

compiler/lib-wasm/wasm_link.ml

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,23 @@ type exportable =
107107
| Global
108108
| Tag
109109

110+
let heaptype_eq t1 t2 =
111+
Stdlib.phys_equal t1 t2
112+
||
113+
match t1, t2 with
114+
| Type i1, Type i2 -> i1 = i2
115+
| _ -> false
116+
117+
let reftype_eq { nullable = n1; typ = t1 } { nullable = n2; typ = t2 } =
118+
Bool.(n1 = n2) && heaptype_eq t1 t2
119+
120+
let valtype_eq t1 t2 =
121+
Stdlib.phys_equal t1 t2
122+
||
123+
match t1, t2 with
124+
| Ref t1, Ref t2 -> reftype_eq t1 t2
125+
| _ -> false
126+
110127
let rec output_uint ch i =
111128
if i < 128
112129
then output_byte ch i
@@ -489,23 +506,6 @@ module Read = struct
489506
(* We have large structs, that tend to hash to the same value *)
490507
Hashtbl.hash_param 15 100 t
491508

492-
let heaptype_eq t1 t2 =
493-
Stdlib.phys_equal t1 t2
494-
||
495-
match t1, t2 with
496-
| Type i1, Type i2 -> i1 = i2
497-
| _ -> false
498-
499-
let reftype_eq { nullable = n1; typ = t1 } { nullable = n2; typ = t2 } =
500-
Bool.(n1 = n2) && heaptype_eq t1 t2
501-
502-
let valtype_eq t1 t2 =
503-
Stdlib.phys_equal t1 t2
504-
||
505-
match t1, t2 with
506-
| Ref t1, Ref t2 -> reftype_eq t1 t2
507-
| _ -> false
508-
509509
let storagetype_eq t1 t2 =
510510
match t1, t2 with
511511
| Val v1, Val v2 -> valtype_eq v1 v2
@@ -1583,11 +1583,11 @@ let check_export_import_types ~subtyping_info ~files i (desc : importdesc) i' im
15831583
match desc, import.desc with
15841584
| Func t, Func t' -> subtype subtyping_info t t'
15851585
| Table { limits; typ }, Table { limits = limits'; typ = typ' } ->
1586-
check_limits limits limits' && Poly.(typ = typ')
1586+
check_limits limits limits' && reftype_eq typ typ'
15871587
| Mem limits, Mem limits' -> check_limits limits limits'
15881588
| Global { mut; typ }, Global { mut = mut'; typ = typ' } ->
15891589
Bool.(mut = mut')
1590-
&& if mut then Poly.(typ = typ') else val_subtype subtyping_info typ typ'
1590+
&& if mut then valtype_eq typ typ' else val_subtype subtyping_info typ typ'
15911591
| Tag t, Tag t' -> t = t'
15921592
| _ -> false
15931593
in

compiler/lib-wasm/wasm_output.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ end = struct
106106
output_sint ch (i asr 7))
107107

108108
let output_sint32 ch i =
109-
if Poly.(i >= -64l && i < 64l)
109+
if Int32.(i >= -64l && i < 64l)
110110
then
111111
let i = Int32.to_int i in
112112
if i >= 0 then output_byte ch i else output_byte ch (i + 128)
@@ -115,7 +115,7 @@ end = struct
115115
output_sint ch (Int32.to_int (Int32.shift_right i 7)))
116116

117117
let rec output_sint64 ch i =
118-
if Poly.(i >= -64L && i < 64L)
118+
if Int64.(i >= -64L && i < 64L)
119119
then
120120
let i = Int64.to_int i in
121121
if i >= 0 then output_byte ch i else output_byte ch (i + 128)

compiler/lib-wasm/wat_output.ml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,12 @@ type ctx = { mutable function_refs : Code.Var.Set.t }
302302

303303
let reference_function ctx f = ctx.function_refs <- Code.Var.Set.add f ctx.function_refs
304304

305-
let remove_nops l = List.filter ~f:(fun i -> not (Poly.equal i Nop)) l
305+
let remove_nops l =
306+
List.filter
307+
~f:(function
308+
| Nop -> false
309+
| _ -> true)
310+
l
306311

307312
let float64 _ f =
308313
match classify_float f with
@@ -604,7 +609,7 @@ let escape_string s =
604609
let b = Buffer.create (String.length s + 2) in
605610
for i = 0 to String.length s - 1 do
606611
let c = s.[i] in
607-
if Poly.(c >= ' ' && c <= '~' && c <> '"' && c <> '\\')
612+
if Char.(c >= ' ' && c <= '~' && c <> '"' && c <> '\\')
608613
then Buffer.add_char b c
609614
else Printf.bprintf b "\\%02x" (Char.code c)
610615
done;

compiler/lib-wasm/wat_preprocess.ml

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,10 @@ type value =
277277
| String of string
278278
| Version of int * int * int
279279

280+
let value_equal (a : value) b = Poly.equal a b
281+
282+
let value_compare (a : value) b = Poly.compare a b
283+
280284
type st =
281285
{ text : string
282286
; mutable pos : pos
@@ -305,7 +309,7 @@ let check_type ?typ expr actual_typ =
305309
match typ with
306310
| None -> ()
307311
| Some typ ->
308-
if Poly.(actual_typ <> typ)
312+
if not (Poly.equal actual_typ typ)
309313
then
310314
raise
311315
(Error
@@ -367,15 +371,17 @@ and bin_op st ?typ loc op args =
367371
let v = eval st expr in
368372
let v' = eval ~typ:(value_type v) st expr' in
369373
Bool
370-
Poly.(
371-
match op with
372-
| "=" -> v = v'
373-
| "<" -> v < v'
374-
| ">" -> v > v'
375-
| "<=" -> v <= v'
376-
| ">=" -> v >= v'
377-
| "<>" -> v <> v'
378-
| _ -> assert false)
374+
(let op =
375+
match op with
376+
| "=" -> ( = )
377+
| "<" -> ( < )
378+
| ">" -> ( > )
379+
| "<=" -> ( <= )
380+
| ">=" -> ( >= )
381+
| "<>" -> ( <> )
382+
| _ -> assert false
383+
in
384+
op (value_compare v v') 0)
379385
| _ -> raise (Error (position_of_loc loc, Printf.sprintf "Syntax error.\n"))
380386

381387
(****)

compiler/lib-wasm/wat_preprocess.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ type value =
33
| String of string
44
| Version of int * int * int
55

6+
val value_equal : value -> value -> bool
7+
68
val f : variables:(string * value) list -> filename:string -> contents:string -> string
79

810
type source =

0 commit comments

Comments
 (0)