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

Global Deadcode Elimination #1503

Merged
merged 134 commits into from
Nov 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
134 commits
Select commit Hold shift + click to select a range
ca8948a
init
micahcantor Jun 21, 2023
a39b91a
Merge branch 'ocsigen:master' into new_deadcode
micahcantor Jun 21, 2023
31fe9f0
more progress
micahcantor Jun 22, 2023
ee93b38
Merge branch 'new_deadcode' of https://github.com/micahcantor/js_of_o…
micahcantor Jun 22, 2023
55e413d
prep for debugging
micahcantor Jun 22, 2023
c63bd52
more updates, debugging info
micahcantor Jun 23, 2023
dbdf3bb
updates
micahcantor Jun 26, 2023
4a389e3
more updates
micahcantor Jun 27, 2023
f8e73ef
initial support for annotating blocks
micahcantor Jul 3, 2023
7d6e5bf
more updates
micahcantor Jul 4, 2023
e42a15b
join propagation
micahcantor Jul 4, 2023
a37fa06
remove old comments
micahcantor Jul 4, 2023
647d695
add block param defs
micahcantor Jul 4, 2023
ccc5dd5
fix equality, dep contribution
micahcantor Jul 5, 2023
cacad1d
move some logic from defs to deps
micahcantor Jul 5, 2023
3b7e811
clean up small bugs
micahcantor Jul 6, 2023
85ed6ae
don't count closure params as used
micahcantor Jul 6, 2023
eb7c895
add initial elimination alg
micahcantor Jul 7, 2023
20ca40d
rearranging
micahcantor Jul 7, 2023
49d5f9a
update deadcode sig
micahcantor Jul 10, 2023
bfd58d5
expose expr print
micahcantor Jul 10, 2023
b3db3dd
fix filter args and closure cont
micahcantor Jul 10, 2023
3a97925
fix bug with cond
micahcantor Jul 10, 2023
4e8097a
add sentinal var
micahcantor Jul 11, 2023
c940f38
add basic compaction block pass
micahcantor Jul 11, 2023
a61e0b2
fix pushtrap variable
micahcantor Jul 11, 2023
12fdc47
expose print constant
micahcantor Jul 12, 2023
56902fe
initial global support
micahcantor Jul 12, 2023
49464e0
more global support
micahcantor Jul 12, 2023
936c3e4
update print uses
micahcantor Jul 12, 2023
9c45d78
cleanup, fix block propagation
micahcantor Jul 19, 2023
461fd7b
add new debug flag
micahcantor Jul 19, 2023
64de253
switch to zeroing out
micahcantor Jul 20, 2023
ac4358a
call global flow once in driver
micahcantor Jul 20, 2023
176cf04
new test file
micahcantor Jul 20, 2023
a38d181
add back dgraph call in driver
micahcantor Jul 25, 2023
18678f6
revert changes to driver
micahcantor Jul 25, 2023
65b7d9d
tweaks
micahcantor Jul 26, 2023
2a3462f
pull out sentinal
micahcantor Jul 26, 2023
efc617c
Fix global flow analysis
vouillon Jul 26, 2023
2ff4b2b
Merge branch 'global-flow-fix' of https://github.com/ocsigen/js_of_oc…
micahcantor Jul 27, 2023
d73622c
add disable globaldeadcode flag, fix tests
micahcantor Jul 27, 2023
b901fde
expose variable_may_escape
micahcantor Jul 27, 2023
aa2c32d
use variable_may_escape
micahcantor Jul 27, 2023
938c8af
fix infinite loop bug
micahcantor Jul 27, 2023
264cb33
change sentinal to undefined
micahcantor Jul 28, 2023
9c4c80d
begin adding unit tests
micahcantor Jul 28, 2023
16606a6
add new test
micahcantor Jul 31, 2023
a6ee4f9
small change to stdlib
micahcantor Jul 31, 2023
4f0c0a0
documentation
micahcantor Jul 31, 2023
50bcd07
update comment
micahcantor Aug 1, 2023
ae242e8
Merge branch 'ocsigen:master' into new_deadcode
micahcantor Aug 2, 2023
0c8f861
changing how args are handled
micahcantor Aug 4, 2023
05080cd
Revert "changing how args are handled"
micahcantor Aug 4, 2023
40bfbe8
improve argument initial liveness/usage
micahcantor Aug 4, 2023
8ce76b6
Merge branch 'new_deadcode' of https://github.com/micahcantor/js_of_o…
micahcantor Aug 4, 2023
d67e8ea
promote var renaming changes
micahcantor Aug 4, 2023
5bfe4e3
Merge branch 'master' into new_deadcode
micahcantor Aug 4, 2023
5dce2b0
Merge branch 'new_deadcode' of https://github.com/micahcantor/js_of_o…
micahcantor Aug 4, 2023
89df59f
sentinal name, start recursive liveness
micahcantor Aug 8, 2023
2e62a34
fix printing vars
micahcantor Aug 8, 2023
d930a08
continue recursive blocks
micahcantor Aug 11, 2023
2f60c4e
promote stdlib changes
micahcantor Aug 11, 2023
d61469b
change code printing when pretty
micahcantor Aug 23, 2023
d55396c
Revert "change code printing when pretty"
micahcantor Aug 25, 2023
e35e594
Revert "promote stdlib changes"
micahcantor Aug 25, 2023
d729df0
update comment
micahcantor Aug 25, 2023
2264014
Revert "fix printing vars"
micahcantor Aug 25, 2023
7cb2d72
Revert "sentinal name, start recursive liveness"
micahcantor Aug 25, 2023
db8cbbf
sentinal name
micahcantor Aug 25, 2023
5bef65a
promote test changes
micahcantor Aug 25, 2023
ca46425
omit sentinal fields in generated code
micahcantor Aug 25, 2023
a702c99
default off
micahcantor Aug 26, 2023
3cdfb6f
promote changes from making default off
micahcantor Aug 27, 2023
a314456
Merge branch 'master' into new_deadcode
micahcantor Sep 11, 2023
f0d9506
change name to global deadcode
micahcantor Sep 17, 2023
013ebcc
Merge branch 'new_deadcode' of https://github.com/micahcantor/js_of_o…
micahcantor Sep 17, 2023
288b2dd
Merge branch 'new_deadcode' of https://github.com/micahcantor/js_of_o…
micahcantor Sep 17, 2023
c8c7c53
format
micahcantor Sep 17, 2023
4df5399
Change expr_vars to use Var.Set
micahcantor Sep 17, 2023
075c6f3
fix live_instruction, remove zero cont, contribution case
micahcantor Sep 17, 2023
a343dc9
re-organize redundant call
micahcantor Sep 17, 2023
3523cf5
don't generate dead return exprs
micahcantor Sep 17, 2023
adacd59
fix type error
micahcantor Sep 17, 2023
6f4ee2d
Merge branch 'new_deadcode' of https://github.com/micahcantor/js_of_o…
micahcantor Sep 17, 2023
3d656eb
refactor array compact
micahcantor Sep 17, 2023
5864030
move debug info before zeroing
micahcantor Sep 17, 2023
8e9f0c2
more descriptive sentinal name
micahcantor Sep 17, 2023
266221e
add omit return value test
micahcantor Sep 17, 2023
0371c8f
Merge branch 'master' into new_deadcode
micahcantor Sep 19, 2023
9ab2476
refactor driver final deadcode call
micahcantor Sep 20, 2023
69ff976
Merge branch 'new_deadcode' of https://github.com/micahcantor/js_of_o…
micahcantor Sep 20, 2023
d44390f
rearrange debug output
micahcantor Oct 2, 2023
238b5de
remove clojure parameters from expr_vars
micahcantor Oct 2, 2023
bbc3932
fix live_instruction
micahcantor Oct 2, 2023
eded8c1
Merge branch 'master' into new_deadcode
micahcantor Oct 2, 2023
09d353e
fix live_instruction
micahcantor Oct 2, 2023
15ee19b
Merge branch 'new_deadcode' of https://github.com/micahcantor/js_of_o…
micahcantor Oct 4, 2023
658dcb0
Move documentation
micahcantor Oct 4, 2023
164e21f
Merge branch 'master' into new_deadcode
micahcantor Oct 4, 2023
6b22d8b
Merge branch 'master' into new_deadcode
micahcantor Oct 10, 2023
6867324
remove option from deadcode_sentinal
micahcantor Oct 10, 2023
187ea52
Merge branch 'new_deadcode' of https://github.com/micahcantor/js_of_o…
micahcantor Oct 10, 2023
bd81aee
improve test
hhugo Oct 10, 2023
596dcb5
fix test
hhugo Oct 10, 2023
34edd45
remove deadcode flag from pure_expr
micahcantor Oct 17, 2023
cd3eb5a
equal exhaustive case
micahcantor Oct 17, 2023
04a7e87
definitions exhaustive case
micahcantor Oct 17, 2023
646b216
Use Var.Tbl instead of var array
micahcantor Oct 17, 2023
fc846de
small fixes
micahcantor Oct 17, 2023
5dfd1ca
make exhaustive
micahcantor Oct 17, 2023
dfb0c22
add comment about closure variables
micahcantor Oct 17, 2023
be14b11
clarifying comments
micahcantor Oct 17, 2023
0214876
small fixes
micahcantor Oct 17, 2023
0a06586
Merge branch 'master' into new_deadcode
micahcantor Oct 17, 2023
07c37bd
Don't expose add_sentinal
micahcantor Oct 17, 2023
4026c20
typos
micahcantor Oct 18, 2023
18fd434
small cleanup
hhugo Oct 18, 2023
527998c
CR
hhugo Oct 18, 2023
a817445
cleanup
micahcantor Oct 18, 2023
c7522e8
Merge branch 'new_deadcode' of https://github.com/micahcantor/js_of_o…
micahcantor Oct 18, 2023
48fccd4
remove pure_expr alias
micahcantor Oct 19, 2023
99caa92
Compiler: add purity for jsoo-special primitives
hhugo Oct 26, 2023
ddc3957
make globaldeadcode default on
micahcantor Oct 30, 2023
1c492f0
Fix offset_ref liveness
micahcantor Oct 30, 2023
70664e9
promote output changes from default on
micahcantor Oct 30, 2023
acd3908
Merge branch 'master' into new_deadcode
micahcantor Oct 31, 2023
8d1841f
tune
hhugo Oct 31, 2023
47a69f9
change log
micahcantor Nov 10, 2023
de564a6
Merge branch 'master' into new_deadcode
micahcantor Nov 10, 2023
d4c57b9
no global deadcode if no deadcode
hhugo Nov 10, 2023
dffa09a
rm trailing space
hhugo Nov 11, 2023
1a135ea
accept
hhugo Nov 11, 2023
a18be46
doc
hhugo Nov 11, 2023
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
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Dev (2023-??-??) - ??

## Features/Changes
* Compiler: global dead code elimination (Micah Cantor, #1503)
* Compiler: change control-flow compilation strategy (#1496)
* Compiler: Dead code elimination of unused references (#2076)
* Compiler: reduce memory consumption (#1516)
Expand Down
4 changes: 0 additions & 4 deletions TODO.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ Compiler optimizations

- cross-function optimizations

- deadcode elimination inside blocks
(for instance, elimination of function which are defined in a
functor but are not used)

- should we rebind variables from a deeper level ?
(only if used more than once...)

Expand Down
4 changes: 4 additions & 0 deletions compiler/lib/code.mli
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,10 @@ module Print : sig
| Instr of (instr * loc)
| Last of (last * loc)

val expr : Format.formatter -> expr -> unit

val constant : Format.formatter -> constant -> unit

val var_list : Format.formatter -> Var.t list -> unit

val instr : Format.formatter -> instr * loc -> unit
Expand Down
2 changes: 2 additions & 0 deletions compiler/lib/config.ml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ module Flag = struct

let deadcode = o ~name:"deadcode" ~default:true

let globaldeadcode = o ~name:"globaldeadcode" ~default:true

let shortvar = o ~name:"shortvar" ~default:true

let compact = o ~name:"compact" ~default:true
Expand Down
2 changes: 2 additions & 0 deletions compiler/lib/config.mli
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ module Flag : sig

val deadcode : unit -> bool

val globaldeadcode : unit -> bool

val optcall : unit -> bool

val shortvar : unit -> bool
Expand Down
20 changes: 15 additions & 5 deletions compiler/lib/driver.ml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ let effects p =
else p, (Code.Var.Set.empty : Effects.cps_calls)

let exact_calls profile p =
let deadcode_sentinal =
(* If deadcode is disabled, this field is just fresh variable *)
Code.Var.fresh ()
in
if not (Config.Flag.effects ())
then
let fast =
Expand All @@ -105,8 +109,14 @@ let exact_calls profile p =
| O1 | O2 -> true
in
let info = Global_flow.f ~fast p in
Specialize.f ~function_arity:(fun f -> Global_flow.function_arity info f) p
else p
let p =
if Config.Flag.globaldeadcode () && Config.Flag.deadcode ()
then Global_deadcode.f p ~deadcode_sentinal info
else p
in
let p = Specialize.f ~function_arity:(fun f -> Global_flow.function_arity info f) p in
p, deadcode_sentinal
else p, deadcode_sentinal

let print p =
if debug () then Code.Print.program (fun _ _ -> "") p;
Expand Down Expand Up @@ -175,7 +185,7 @@ let generate
~exported_runtime
~wrap_with_fun
~warn_on_unhandled_effect
((p, live_vars), cps_calls) =
(((p, live_vars), cps_calls), deadcode_sentinal) =
if times () then Format.eprintf "Start Generation...@.";
let should_export = should_export wrap_with_fun in
Generate.f
Expand All @@ -185,6 +195,7 @@ let generate
~cps_calls
~should_export
~warn_on_unhandled_effect
~deadcode_sentinal
d

let debug_linker = Debug.find "linker"
Expand Down Expand Up @@ -579,8 +590,7 @@ let full ~standalone ~wrap_with_fun ~profile ~linkall ~source_map formatter d p
| O2 -> o2
| O3 -> o3)
+> exact_calls profile
+> effects
+> map_fst (Generate_closure.f +> deadcode')
+> map_fst (effects +> map_fst (Generate_closure.f +> deadcode'))
in
let emit =
generate d ~exported_runtime ~wrap_with_fun ~warn_on_unhandled_effect:standalone
Expand Down
29 changes: 24 additions & 5 deletions compiler/lib/generate.ml
vouillon marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -285,12 +285,14 @@ module Ctx = struct
; should_export : bool
; effect_warning : bool ref
; cps_calls : Effects.cps_calls
; deadcode_sentinal : Var.t
}

let initial
~warn_on_unhandled_effect
~exported_runtime
~should_export
~deadcode_sentinal
blocks
live
cps_calls
Expand All @@ -304,6 +306,7 @@ module Ctx = struct
; should_export
; effect_warning = ref (not warn_on_unhandled_effect)
; cps_calls
; deadcode_sentinal
}
end

Expand Down Expand Up @@ -444,7 +447,7 @@ let rec constant_rec ~ctx x level instrs =
| Float_array a ->
( Mlvalue.Array.make
~tag:Obj.double_array_tag
~args:(Array.to_list (Array.map a ~f:float_const))
~args:(Array.to_list (Array.map a ~f:(fun x -> J.Element (float_const x))))
, instrs )
| Int64 i ->
let p =
Expand Down Expand Up @@ -490,9 +493,9 @@ let rec constant_rec ~ctx x level instrs =
let instrs =
(J.variable_declaration [ J.V v, (js, J.N) ], J.N) :: instrs
in
J.EVar (J.V v) :: acc, instrs
| _ -> js :: acc, instrs)
else List.rev l, instrs
J.Element (J.EVar (J.V v)) :: acc, instrs
| _ -> J.Element js :: acc, instrs)
else List.map ~f:(fun x -> J.Element x) (List.rev l), instrs
in
Mlvalue.Block.make ~tag ~args:l, instrs)
| Int i -> int32 i, instrs
Expand Down Expand Up @@ -1037,6 +1040,14 @@ let rec translate_expr ctx queue loc x e level : _ * J.statement_list =
List.fold_right
~f:(fun x (args, prop, queue) ->
let (prop', cx), queue = access_queue queue x in
let cx =
match cx with
| J.EVar (J.V v) ->
if Var.equal v ctx.deadcode_sentinal
then J.ElementHole
else J.Element cx
| _ -> J.Element cx
in
cx :: args, or_p prop prop', queue)
(Array.to_list a)
~init:([], const_p, queue)
Expand Down Expand Up @@ -1133,6 +1144,9 @@ let rec translate_expr ctx queue loc x e level : _ * J.statement_list =
let prim = Share.get_prim (runtime_fun ctx) name ctx.Ctx.share in
prim, const_p, queue
| Extern "%closure", _ -> assert false
| Extern "%undefined", [] ->
J.(EVar (ident (Utf8_string.of_string_exn "undefined"))), const_p, queue
| Extern "%undefined", _ -> assert false
| Extern "%caml_js_opt_call", f :: o :: l ->
let (pf, cf), queue = access_queue' ~ctx queue f in
let (po, co), queue = access_queue' ~ctx queue o in
Expand Down Expand Up @@ -1582,7 +1596,10 @@ and compile_conditional st queue ~fall_through last scope_stack : _ * _ =
match last with
| Return x ->
let (_px, cx), queue = access_queue queue x in
true, flush_all queue [ J.Return_statement (Some cx), loc ]
let return_expr =
if Var.equal st.ctx.deadcode_sentinal x then None else Some cx
in
true, flush_all queue [ J.Return_statement return_expr, loc ]
| Raise (x, k) ->
let (_px, cx), queue = access_queue queue x in
true, flush_all queue (throw_statement st.ctx cx k loc)
Expand Down Expand Up @@ -1791,6 +1808,7 @@ let f
~cps_calls
~should_export
~warn_on_unhandled_effect
~deadcode_sentinal
debug =
let t' = Timer.make () in
let share = Share.get ~cps_calls ~alias_prims:exported_runtime p in
Expand All @@ -1802,6 +1820,7 @@ let f
~warn_on_unhandled_effect
~exported_runtime
~should_export
~deadcode_sentinal
p.blocks
live_vars
cps_calls
Expand Down
1 change: 1 addition & 0 deletions compiler/lib/generate.mli
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ val f :
-> cps_calls:Effects.cps_calls
-> should_export:bool
-> warn_on_unhandled_effect:bool
-> deadcode_sentinal:Code.Var.t
-> Parse_bytecode.Debug.t
-> Javascript.program

Expand Down
Loading
Loading