From 1f29da6177682c5f3679a08b0cdc06f73fd29d5c Mon Sep 17 00:00:00 2001 From: Hugo Heuzard Date: Sun, 21 Jan 2024 14:00:43 +0100 Subject: [PATCH 1/2] Test: expose global DCE issue with the toplevel --- compiler/tests-toplevel/test_toplevel.ml | 9 +++++---- compiler/tests-toplevel/test_toplevel.reference | 4 ++-- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/compiler/tests-toplevel/test_toplevel.ml b/compiler/tests-toplevel/test_toplevel.ml index 8ab784373e..db7a81eba6 100644 --- a/compiler/tests-toplevel/test_toplevel.ml +++ b/compiler/tests-toplevel/test_toplevel.ml @@ -1,6 +1,7 @@ let () = let content = {| let () = print_endline "hello";; +1+1;; 1+;; Missing_module.f;; |} in @@ -12,10 +13,10 @@ Missing_module.f;; try Location.reset (); let phr = !Toploop.parse_toplevel_phrase lexbuf in - if not (Toploop.execute_phrase true Format.std_formatter phr) then raise Exit + let res = Toploop.execute_phrase true Format.std_formatter phr in + flush_all (); + if not res then raise Exit with - | End_of_file -> - flush_all (); - exit 0 + | End_of_file -> exit 0 | x -> Location.report_exception Format.std_formatter x done diff --git a/compiler/tests-toplevel/test_toplevel.reference b/compiler/tests-toplevel/test_toplevel.reference index 3ab394a970..1695687c25 100644 --- a/compiler/tests-toplevel/test_toplevel.reference +++ b/compiler/tests-toplevel/test_toplevel.reference @@ -1,5 +1,5 @@ hello -Line 3, characters 2-4: +Line 4, characters 2-4: Error: Syntax error -Line 4, characters 0-16: +Line 5, characters 0-16: Error: Unbound module Missing_module From 0afbc3baf7774e5e49adbed9abff3478f621c130 Mon Sep 17 00:00:00 2001 From: Hugo Heuzard Date: Sun, 21 Jan 2024 08:26:48 +0100 Subject: [PATCH 2/2] Compiler: fix toplevel with globaldeacode --- CHANGES.md | 3 +++ compiler/lib/global_deadcode.ml | 9 +++++++++ compiler/tests-toplevel/test_toplevel.reference | 1 + 3 files changed, 13 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index 291479df2d..aff4441c57 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -5,6 +5,9 @@ * Compiler: no longer rely on IIFE for scoping variable inside loops * Lib: add ellipse to canvasRenderingContext2D (@FayCarsons, #1555) +## Bug fixes +* Compiler: fix global dead code elimination in a toplevel context + # 5.6.0 (2024-01-02) - Lille ## Features/Changes diff --git a/compiler/lib/global_deadcode.ml b/compiler/lib/global_deadcode.ml index bc8017d5d9..9af29d950e 100644 --- a/compiler/lib/global_deadcode.ml +++ b/compiler/lib/global_deadcode.ml @@ -246,6 +246,15 @@ let liveness prog pure_funs (global_info : Global_flow.info) = | Stop | Branch _ | Poptrap _ | Pushtrap _ -> () in Addr.Map.iter (fun _ block -> live_block block) prog.blocks; + Code.traverse + { Code.fold = Code.fold_children } + (fun pc () -> + match Addr.Map.find pc prog.blocks with + | { branch = Return x, _; _ } -> add_top x + | _ -> ()) + prog.start + prog.blocks + (); live_vars (* Returns the set of variables given a table of variables. *) diff --git a/compiler/tests-toplevel/test_toplevel.reference b/compiler/tests-toplevel/test_toplevel.reference index 1695687c25..58eb84ed85 100644 --- a/compiler/tests-toplevel/test_toplevel.reference +++ b/compiler/tests-toplevel/test_toplevel.reference @@ -1,4 +1,5 @@ hello +- : int = 2 Line 4, characters 2-4: Error: Syntax error Line 5, characters 0-16: