Skip to content

Commit 00bc571

Browse files
committed
fix for wasm
1 parent 45f3e0d commit 00bc571

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

compiler/lib/deadcode.ml

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -193,18 +193,26 @@ let empty_body b =
193193

194194
let remove_empty_blocks ~live_vars (p : Code.program) : Code.program =
195195
let shortcuts = Hashtbl.create 16 in
196-
let rec resolve_rec visited ((pc, args) as cont) =
196+
let rec resolve_rec keep_empty visited ((pc, args) as cont0) =
197197
if Addr.Set.mem pc visited
198-
then cont
198+
then cont0
199199
else
200200
match Hashtbl.find_opt shortcuts pc with
201201
| Some (params, cont) ->
202-
let pc', args' = resolve_rec (Addr.Set.add pc visited) cont in
203-
let s = Subst.from_map (Subst.build_mapping params args) in
204-
pc', List.map ~f:s args'
205-
| None -> cont
202+
let pc', args' = resolve_rec keep_empty (Addr.Set.add pc visited) cont in
203+
if keep_empty && not (List.is_empty args')
204+
then cont0
205+
else
206+
let s = Subst.from_map (Subst.build_mapping params args) in
207+
pc', List.map ~f:s args'
208+
| None -> cont0
209+
in
210+
let resolve cont = resolve_rec false Addr.Set.empty cont in
211+
let resolve_inside_switch =
212+
match Config.target () with
213+
| `JavaScript -> resolve
214+
| `Wasm -> resolve_rec true Addr.Set.empty
206215
in
207-
let resolve cont = resolve_rec Addr.Set.empty cont in
208216
Addr.Map.iter
209217
(fun pc block ->
210218
match block with
@@ -233,7 +241,7 @@ let remove_empty_blocks ~live_vars (p : Code.program) : Code.program =
233241
match branch with
234242
| Branch cont -> Branch (resolve cont)
235243
| Cond (x, cont1, cont2) -> Cond (x, resolve cont1, resolve cont2)
236-
| Switch (x, a1) -> Switch (x, Array.map ~f:resolve a1)
244+
| Switch (x, a1) -> Switch (x, Array.map ~f:resolve_inside_switch a1)
237245
| Pushtrap (cont1, x, cont2) -> Pushtrap (resolve cont1, x, resolve cont2)
238246
| Poptrap cont -> Poptrap (resolve cont)
239247
| Return _ | Raise _ | Stop -> branch)

0 commit comments

Comments
 (0)