Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.Sign up
Constants emitted several times #6337
Original bug ID: 6337
The following code:
let g () = let r = ref 0 in let rec f () = incr r; print_endline "foo"; in f
results in the string constant "foo" to be emitted twice. ocamlopt -dcmm shows:
(data int 1024 global "camlFoo" "camlFoo": skip 8) (data int 2295 "camlFoo__3": addr "camlFoo__g_1010" int 3) (data global "camlFoo__1" int 1276 "camlFoo__1": string "foo" skip 4 byte 4) (data global "camlFoo__2" int 1276 "camlFoo__2": string "foo" skip 4 byte 4) ...
Note the presence of camlFoo_1 and camlFoo_2. Only the second one is actually used.
This comes from Closure.close_functions, which tries to compile first the functions under the assumption that their environment is not used, and do it again if this assumption is invalidated. Since structured constants are now allocated during this pass, they can be allocated several times.
One possible fix is to backtrack the list of allocated structured constants when a second pass is required.
Note that even if the sharing of constants is added (#5779), this will still apply because of string literals (which won't be shared, since they are mutable).