Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Target-specific code #1655

Merged
merged 33 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
347bd64
Target-specific code
vouillon Jun 7, 2024
a5351eb
Update changelog
OlivierNicole Sep 13, 2024
3badf5e
CR: remove GADT
OlivierNicole Sep 19, 2024
eb39569
CR
OlivierNicole Sep 19, 2024
0440867
CR
OlivierNicole Sep 20, 2024
6bd522b
Fix constant_identical for Wasm target
OlivierNicole Sep 20, 2024
f817ece
Fix wrong start value for Config.target
OlivierNicole Sep 20, 2024
ca7fced
CR: Config.target
OlivierNicole Sep 21, 2024
56f2ea4
CR
OlivierNicole Sep 23, 2024
377abe2
CR
OlivierNicole Sep 23, 2024
767a4d4
ocamlformat
OlivierNicole Sep 23, 2024
9f8c4aa
Fix
OlivierNicole Sep 23, 2024
a3f1ce8
CR: Add comment
OlivierNicole Sep 23, 2024
04dff5e
CR
OlivierNicole Sep 23, 2024
74a23ef
CR: Drop code for non-existent primitives
OlivierNicole Sep 23, 2024
cd24eef
CR
OlivierNicole Sep 23, 2024
4b5c126
Parse_bytecode: add comments
vouillon Sep 23, 2024
8543d08
Wasm: do not generate JavaScript strings for predefined exceptions
vouillon Sep 23, 2024
b19481f
CR
OlivierNicole Sep 24, 2024
a236900
small refactor
hhugo Sep 24, 2024
7448ac9
small refactor
hhugo Sep 24, 2024
861f6f5
small refactor
hhugo Sep 24, 2024
e3d1975
small refactor
hhugo Sep 24, 2024
41bf745
small refactoring of parse_bytecode
hhugo Sep 24, 2024
9f66948
small refactor
hhugo Sep 24, 2024
aa0ae8b
small refactor
hhugo Sep 24, 2024
191b917
CR
OlivierNicole Sep 25, 2024
abb8540
Add Int31 tests
OlivierNicole Sep 26, 2024
d2e8bd6
CR: Update compiler/tests-num/dune
OlivierNicole Sep 26, 2024
ece9f79
Fix tested property
OlivierNicole Sep 26, 2024
92108c7
Upgrade Dune lang to 3.15
OlivierNicole Sep 26, 2024
58c61ef
Merge remote-tracking branch 'jsoo/master' into converge-jsoo-tip-07
OlivierNicole Sep 26, 2024
226d865
Fix after #1692
OlivierNicole Sep 26, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
* Runtime: allow dynlink of precompiled js with separate compilation (#1676)
* Lib: Modify Typed_array API for compatibility with WebAssembly
* Compiler: improved global dead code elimination (#2206)
* Compiler: add support for the Wasm backend in parts of the pipeline, in
prevision for the merge of wasm_of_ocaml


## Bug fixes
Expand Down
2 changes: 1 addition & 1 deletion compiler/bin-js_of_ocaml/build_fs.ml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ function jsoo_create_file_extern(name,content){
~standalone:true
~wrap_with_fun:`Iife
~link:`Needed
pfs_fmt
~formatter:pfs_fmt
(Parse_bytecode.Debug.create ~include_cmis:false false)
code
in
Expand Down
1 change: 1 addition & 0 deletions compiler/bin-js_of_ocaml/check_runtime.ml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ let print_groups output l =
output_string output (Printf.sprintf "%s\n" name)))

let f (runtime_files, bytecode, target_env) =
Config.set_target `JavaScript;
Linker.reset ();
let runtime_files, builtin =
List.partition_map runtime_files ~f:(fun name ->
Expand Down
9 changes: 5 additions & 4 deletions compiler/bin-js_of_ocaml/compile.ml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ let run
} =
let include_cmis = toplevel && not no_cmis in
let custom_header = common.Jsoo_cmdline.Arg.custom_header in
Config.set_target `JavaScript;
hhugo marked this conversation as resolved.
Show resolved Hide resolved
Jsoo_cmdline.Arg.eval common;
Linker.reset ();
(match output_file with
Expand Down Expand Up @@ -186,7 +187,7 @@ let run
let init_pseudo_fs = fs_external && standalone in
let sm =
match output_file with
| `Stdout, fmt ->
| `Stdout, formatter ->
let instr =
List.concat
[ pseudo_fs_instr `create_file one.debug one.cmis
Expand All @@ -201,10 +202,10 @@ let run
~link
~wrap_with_fun
?source_map
fmt
~formatter
one.debug
code
| `File, fmt ->
| `File, formatter ->
let fs_instr1, fs_instr2 =
match fs_output with
| None -> pseudo_fs_instr `create_file one.debug one.cmis, []
Expand All @@ -225,7 +226,7 @@ let run
~link
~wrap_with_fun
?source_map
fmt
~formatter
one.debug
code
in
Expand Down
1 change: 1 addition & 0 deletions compiler/bin-js_of_ocaml/link.ml
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ let f
; mklib
; toplevel
} =
Config.set_target `JavaScript;
Jsoo_cmdline.Arg.eval common;
Linker.reset ();
let with_output f =
Expand Down
3 changes: 3 additions & 0 deletions compiler/lib-dynlink/js_of_ocaml_compiler_dynlink.ml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ let normalize_bytecode code =
Bytes.to_string b

let () =
(match Sys.backend_type with
| Sys.Other "js_of_ocaml" -> Config.set_target `JavaScript
| Sys.(Native | Bytecode | Other _) -> failwith "Expected backend `js_of_ocaml`");
let global = J.pure_js_expr "globalThis" in
Config.Flag.set "use-js-string" (Jsoo_runtime.Sys.Config.use_js_string ());
Config.Flag.set "effects" (Jsoo_runtime.Sys.Config.effects ());
Expand Down
1 change: 1 addition & 0 deletions compiler/lib-runtime-files/gen/gen.ml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ let rec list_product l =
let bool = [ true; false ]

let () =
Js_of_ocaml_compiler.Config.set_target `JavaScript;
let () = set_binary_mode_out stdout true in
match Array.to_list Sys.argv with
| [] -> assert false
Expand Down
18 changes: 16 additions & 2 deletions compiler/lib/code.ml
Original file line number Diff line number Diff line change
Expand Up @@ -856,6 +856,7 @@ let with_invariant = Debug.find "invariant"
let check_defs = false

let invariant { blocks; start; _ } =
let target = Config.target () in
if with_invariant ()
then (
assert (Addr.Map.mem start blocks);
Expand All @@ -870,15 +871,28 @@ let invariant { blocks; start; _ } =
assert (not (Var.ISet.mem defs x));
Var.ISet.add defs x)
in
let check_constant = function
| NativeInt _ | Int32 _ ->
assert (
match target with
| `Wasm -> true
| _ -> false)
| String _ | NativeString _ | Float _ | Float_array _ | Int _ | Int64 _
| Tuple (_, _, _) -> ()
in
let check_prim_arg = function
| Pc c -> check_constant c
| Pv _ -> ()
in
let check_expr = function
| Apply _ -> ()
| Block (_, _, _, _) -> ()
| Field (_, _, _) -> ()
| Closure (l, cont) ->
List.iter l ~f:define;
check_cont cont
| Constant _ -> ()
| Prim (_, _) -> ()
| Constant c -> check_constant c
| Prim (_, args) -> List.iter ~f:check_prim_arg args
| Special _ -> ()
in
let check_instr (i, _loc) =
Expand Down
12 changes: 8 additions & 4 deletions compiler/lib/config.ml
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ module Param = struct
p
~name:"tc"
~desc:"Set tailcall optimisation"
(enum [ "trampoline", TcTrampoline; (* default *) "none", TcNone ])
(enum [ "trampoline", TcTrampoline (* default *); "none", TcNone ])

let lambda_lifting_threshold =
(* When we reach this depth, we start looking for functions to be lifted *)
Expand All @@ -183,8 +183,12 @@ end

(****)

let target_ : [ `JavaScript | `Wasm ] ref = ref `JavaScript
let target_ : [ `JavaScript | `Wasm | `None ] ref = ref `None

let target () = !target_
let target () =
match !target_ with
| `None -> failwith "target was not set"
| (`JavaScript | `Wasm) as t -> t

let set_target t = target_ := t
let set_target (t : [ `JavaScript | `Wasm ]) =
target_ := (t :> [ `JavaScript | `Wasm | `None ])
69 changes: 41 additions & 28 deletions compiler/lib/driver.ml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ let debug = Debug.find "main"

let times = Debug.find "times"

type optimized_result =
{ program : Code.program
; variable_uses : Deadcode.variable_uses
; trampolined_calls : Effects.trampolined_calls
; in_cps : Effects.in_cps
; deadcode_sentinal : Code.Var.t
}

type profile =
| O1
| O2
Expand Down Expand Up @@ -194,14 +202,13 @@ let generate
~exported_runtime
~wrap_with_fun
~warn_on_unhandled_effect
~deadcode_sentinal
((p, live_vars), trampolined_calls, _) =
{ program; variable_uses; trampolined_calls; deadcode_sentinal; in_cps = _ } =
if times () then Format.eprintf "Start Generation...@.";
let should_export = should_export wrap_with_fun in
Generate.f
p
program
~exported_runtime
~live_vars
~live_vars:variable_uses
~trampolined_calls
~should_export
~warn_on_unhandled_effect
Expand Down Expand Up @@ -658,13 +665,19 @@ let configure formatter =
Code.Var.set_pretty (pretty && not (Config.Flag.shortvar ()));
Code.Var.set_stable (Config.Flag.stable_var ())

let full ~standalone ~wrap_with_fun ~profile ~link ~source_map formatter d p =
let exported_runtime = not standalone in
let link_and_pack ?(standalone = true) ?(wrap_with_fun = `Iife) ?(link = `No) p =
let export_runtime =
match link with
| `All | `All_from _ -> true
| `Needed | `No -> false
in
p
|> link' ~export_runtime ~standalone ~link
|> pack ~wrap_with_fun ~standalone
|> coloring
|> check_js

let optimize ~profile p =
let deadcode_sentinal =
(* If deadcode is disabled, this field is just fresh variable *)
Code.Var.fresh_n "dummy"
Expand All @@ -677,31 +690,31 @@ let full ~standalone ~wrap_with_fun ~profile ~link ~source_map formatter d p =
| O3 -> o3)
+> exact_calls ~deadcode_sentinal profile
+> effects ~deadcode_sentinal
+> map_fst (if Config.Flag.effects () then fun x -> x else Generate_closure.f)
+> map_fst
(match Config.target (), Config.Flag.effects () with
| `JavaScript, false -> Generate_closure.f
| `JavaScript, true | `Wasm, _ -> Fun.id)
+> map_fst deadcode'
in
let emit =
generate
d
~exported_runtime
~wrap_with_fun
~warn_on_unhandled_effect:standalone
~deadcode_sentinal
+> link' ~export_runtime ~standalone ~link
+> pack ~wrap_with_fun ~standalone
+> coloring
+> check_js
+> output formatter ~source_map ()
in
if times () then Format.eprintf "Start Optimizing...@.";
let t = Timer.make () in
let r = opt p in
let (program, variable_uses), trampolined_calls, in_cps = opt p in
let () = if times () then Format.eprintf " optimizations : %a@." Timer.print t in
emit r
{ program; variable_uses; trampolined_calls; in_cps; deadcode_sentinal }

let full ~standalone ~wrap_with_fun ~profile ~link ~source_map ~formatter d p =
let optimized_code = optimize ~profile p in
let exported_runtime = not standalone in
let emit formatter =
generate d ~exported_runtime ~wrap_with_fun ~warn_on_unhandled_effect:standalone
+> link_and_pack ~standalone ~wrap_with_fun ~link
+> output formatter ~source_map ()
in
emit formatter optimized_code

let full_no_source_map ~standalone ~wrap_with_fun ~profile ~link formatter d p =
let full_no_source_map ~formatter ~standalone ~wrap_with_fun ~profile ~link d p =
let (_ : Source_map.t option) =
full ~standalone ~wrap_with_fun ~profile ~link ~source_map:None formatter d p
full ~standalone ~wrap_with_fun ~profile ~link ~source_map:None ~formatter d p
in
()

Expand All @@ -711,22 +724,22 @@ let f
?(profile = O1)
~link
?source_map
formatter
~formatter
d
p =
full ~standalone ~wrap_with_fun ~profile ~link ~source_map formatter d p
full ~standalone ~wrap_with_fun ~profile ~link ~source_map ~formatter d p

let f' ?(standalone = true) ?(wrap_with_fun = `Iife) ?(profile = O1) ~link formatter d p =
full_no_source_map ~standalone ~wrap_with_fun ~profile ~link formatter d p
full_no_source_map ~formatter ~standalone ~wrap_with_fun ~profile ~link d p

let from_string ~prims ~debug s formatter =
let p, d = Parse_bytecode.from_string ~prims ~debug s in
full_no_source_map
~formatter
~standalone:false
~wrap_with_fun:`Anonymous
~profile:O1
~link:`No
formatter
d
p

Expand Down
19 changes: 18 additions & 1 deletion compiler/lib/driver.mli
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,23 @@

type profile

type optimized_result =
{ program : Code.program
; variable_uses : Deadcode.variable_uses
; trampolined_calls : Effects.trampolined_calls
; in_cps : Effects.in_cps
; deadcode_sentinal : Code.Var.t
}

val optimize : profile:profile -> Code.program -> optimized_result

val f :
?standalone:bool
-> ?wrap_with_fun:[ `Iife | `Anonymous | `Named of string ]
-> ?profile:profile
-> link:[ `All | `All_from of string list | `Needed | `No ]
-> ?source_map:Source_map.t
-> Pretty_print.t
-> formatter:Pretty_print.t
-> Parse_bytecode.Debug.t
-> Code.program
-> Source_map.t option
Expand All @@ -48,6 +58,13 @@ val from_string :
-> Pretty_print.t
-> unit

val link_and_pack :
?standalone:bool
-> ?wrap_with_fun:[ `Iife | `Anonymous | `Named of string ]
-> ?link:[ `All | `All_from of string list | `Needed | `No ]
-> Javascript.statement_list
-> Javascript.statement_list

val configure : Pretty_print.t -> unit

val profiles : (int * profile) list
Expand Down
Loading