Skip to content

Commit 3324e4a

Browse files
hhugovouillon
andauthored
Fix method lookup cache with separate compilation (#2039)
* Fix method lookup cache with separate compilation * Tests: check-runtime now filter internal primitives * Runtime: caml_get_public_method is expose in stdlib mli. respect the arity Co-authored-by: Jérôme Vouillon <jerome.vouillon@gmail.com>
1 parent aa6b65e commit 3324e4a

19 files changed

+200
-82
lines changed

CHANGES.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
* Runtime: fix caml_string_concat when not using JS strings (#1874)
6565
* Runtime: consistent bigarray hashing across all architectures (#1977)
6666
* Runtime: fix caml_utf8_of_utf16 bug in high surrogate case (#2008)
67-
* Runtime/wasm: fix method lookup (#2034, #2038)
67+
* Runtime/wasm/js: fix method lookup (#2034, #2038, #2039)
6868
* Tools: fix jsoo_mktop and jsoo_mkcmis (#1877)
6969
* Toplevel: fix for when use-js-strings is disabled (#1997)
7070

compiler/bin-js_of_ocaml/check_runtime.ml

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,16 +91,30 @@ let f (runtime_files, bytecode, target_env) =
9191
in
9292
let needed = StringSet.of_list (List.map ~f:fst needed) in
9393
let needed =
94-
(* this list was copied from parse_bytecode *)
9594
List.fold_left
9695
~f:(fun acc x -> StringSet.remove x acc)
9796
~init:needed
98-
[ "caml_ensure_stack_capacity"
97+
[ (* this list was copied from parse_bytecode *)
98+
"caml_ensure_stack_capacity"
9999
; "caml_process_pending_actions_with_root"
100100
; "caml_make_array"
101101
; "caml_array_of_uniform_array"
102102
]
103103
in
104+
let needed =
105+
(* internal primitives *)
106+
List.fold_left
107+
~f:(fun acc x -> StringSet.add x acc)
108+
~init:needed
109+
[ "caml_register_global"
110+
; "caml_js_set"
111+
; "caml_js_get"
112+
; "caml_get_global_data"
113+
; "caml_oo_cache_id"
114+
; "caml_get_public_method"
115+
; "caml_get_cached_method"
116+
]
117+
in
104118
let from_runtime1 = Linker.list_all () in
105119
let from_runtime2 = Primitive.get_external () in
106120
(* [from_runtime2] is a superset of [from_runtime1].

compiler/lib/parse_bytecode.ml

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,7 @@ type globals =
532532
; mutable is_const : bool array
533533
; mutable is_exported : bool array
534534
; mutable named_value : string option array
535+
; mutable cache_ids : Var.t list
535536
; constants : Code.constant array
536537
; primitives : string array
537538
}
@@ -541,6 +542,7 @@ let make_globals size constants primitives =
541542
; is_const = Array.make size false
542543
; is_exported = Array.make size false
543544
; named_value = Array.make size None
545+
; cache_ids = []
544546
; constants
545547
; primitives
546548
}
@@ -818,8 +820,6 @@ let tagged_blocks = ref Addr.Map.empty
818820

819821
let compiled_blocks : (_ * instr list * last) Addr.Map.t ref = ref Addr.Map.empty
820822

821-
let method_cache_id = ref 1
822-
823823
let clo_offset_3 = 3
824824

825825
type compile_info =
@@ -2353,34 +2353,29 @@ and compile infos pc state (instrs : instr list) =
23532353
(Let (x, Prim (Ult, [ Pv z; Pv y ])) :: instrs)
23542354
| GETPUBMET ->
23552355
let n = gets32 code (pc + 1) in
2356-
let cache = !method_cache_id in
2357-
incr method_cache_id;
23582356
let obj = State.accu state in
23592357
let state = State.push state in
2360-
let tag, state = State.fresh_var state in
2358+
let cache_id = Var.fresh_n "cache_id" in
2359+
state.globals.cache_ids <- cache_id :: state.globals.cache_ids;
23612360
let m, state = State.fresh_var state in
2362-
2363-
if debug_parser () then Format.printf "%a = %ld@." Var.print tag n;
23642361
if debug_parser ()
23652362
then
23662363
Format.printf
2367-
"%a = caml_get_public_method(%a, %a)@."
2364+
"%a = caml_get_cached_method(%a, %ld)@."
23682365
Var.print
23692366
m
23702367
Var.print
23712368
obj
2372-
Var.print
2373-
tag;
2369+
n;
23742370
compile
23752371
infos
23762372
(pc + 3)
23772373
state
23782374
(Let
23792375
( m
23802376
, Prim
2381-
( Extern "caml_get_public_method"
2382-
, [ Pv obj; Pv tag; Pc (Int (Targetint.of_int_exn cache)) ] ) )
2383-
:: Let (tag, const32 n)
2377+
( Extern "caml_get_cached_method"
2378+
, [ Pv obj; Pc (Int (Targetint.of_int32_exn n)); Pv cache_id ] ) )
23842379
:: instrs)
23852380
| GETDYNMET ->
23862381
let tag = State.accu state in
@@ -2401,12 +2396,7 @@ and compile infos pc state (instrs : instr list) =
24012396
infos
24022397
(pc + 1)
24032398
state
2404-
(Let
2405-
( m
2406-
, Prim
2407-
( Extern "caml_get_public_method"
2408-
, [ Pv obj; Pv tag; Pc (Int Targetint.zero) ] ) )
2409-
:: instrs)
2399+
(Let (m, Prim (Extern "caml_get_public_method", [ Pv obj; Pv tag ])) :: instrs)
24102400
| GETMETHOD ->
24112401
let lab = State.accu state in
24122402
let obj = State.peek 0 state in
@@ -2537,7 +2527,12 @@ let parse_bytecode code globals debug_data =
25372527
in
25382528
compiled_blocks := Addr.Map.empty;
25392529
tagged_blocks := Addr.Map.empty;
2540-
Code.compact p
2530+
let p = Code.compact p in
2531+
let body =
2532+
List.fold_left globals.cache_ids ~init:[] ~f:(fun body cache_id ->
2533+
Let (cache_id, Prim (Extern "caml_oo_cache_id", [])) :: body)
2534+
in
2535+
Code.prepend p body
25412536

25422537
module Toc : sig
25432538
type t

compiler/tests-check-prim/main.4.14.output

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,8 @@ caml_string_concat
116116
caml_to_js_string (deprecated)
117117

118118
From +stdlib.js:
119-
caml_build_symbols
120119
caml_is_printable
121120
caml_maybe_print_stats
122-
caml_register_global
123-
jsoo_toplevel_reloc
124121

125122
From +sync.js:
126123
MlMutex

compiler/tests-check-prim/main.5.2.output

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,8 @@ caml_runtime_events_read_poll
118118
caml_runtime_events_user_resolve
119119

120120
From +stdlib.js:
121-
caml_build_symbols
122121
caml_is_printable
123122
caml_maybe_print_stats
124-
caml_register_global
125-
jsoo_toplevel_reloc
126123

127124
From +sys.js:
128125
caml_fatal_uncaught_exception

compiler/tests-check-prim/main.5.3.output

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,8 @@ caml_runtime_events_read_poll
117117
caml_runtime_events_user_resolve
118118

119119
From +stdlib.js:
120-
caml_build_symbols
121120
caml_is_printable
122121
caml_maybe_print_stats
123-
caml_register_global
124-
jsoo_toplevel_reloc
125122

126123
From +sys.js:
127124
caml_fatal_uncaught_exception

compiler/tests-check-prim/main.5.4.output

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,8 @@ caml_runtime_events_read_poll
116116
caml_runtime_events_user_resolve
117117

118118
From +stdlib.js:
119-
caml_build_symbols
120119
caml_is_printable
121120
caml_maybe_print_stats
122-
caml_register_global
123-
jsoo_toplevel_reloc
124121

125122
From +sys.js:
126123
caml_fatal_uncaught_exception

compiler/tests-check-prim/unix-Unix.4.14.output

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,11 +192,8 @@ caml_string_concat
192192
caml_to_js_string (deprecated)
193193

194194
From +stdlib.js:
195-
caml_build_symbols
196195
caml_is_printable
197196
caml_maybe_print_stats
198-
caml_register_global
199-
jsoo_toplevel_reloc
200197

201198
From +sync.js:
202199
MlMutex

compiler/tests-check-prim/unix-Unix.5.2.output

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,11 +194,8 @@ caml_runtime_events_read_poll
194194
caml_runtime_events_user_resolve
195195

196196
From +stdlib.js:
197-
caml_build_symbols
198197
caml_is_printable
199198
caml_maybe_print_stats
200-
caml_register_global
201-
jsoo_toplevel_reloc
202199

203200
From +sys.js:
204201
caml_fatal_uncaught_exception

compiler/tests-check-prim/unix-Unix.5.3.output

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,11 +193,8 @@ caml_runtime_events_read_poll
193193
caml_runtime_events_user_resolve
194194

195195
From +stdlib.js:
196-
caml_build_symbols
197196
caml_is_printable
198197
caml_maybe_print_stats
199-
caml_register_global
200-
jsoo_toplevel_reloc
201198

202199
From +sys.js:
203200
caml_fatal_uncaught_exception

0 commit comments

Comments
 (0)