Skip to content

Commit 27d68bf

Browse files
authored
flambda-backend: Flambda1 region deletion and locals fixes (#1000)
1 parent 0e3e057 commit 27d68bf

File tree

3 files changed

+20
-9
lines changed

3 files changed

+20
-9
lines changed

Makefile.menhir

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ import-menhirLib:
111111
@ cp \
112112
$(addprefix `$(MENHIR) --suggest-menhirLib`/menhirLib.,ml mli) \
113113
boot/menhir
114+
# Partial applications of the form Obj.magic f x in menhirLib cause an issue with locals,
115+
# so rewrite these to Obj.magic (f x)
116+
@ sed -i 's/\b\(in\|then\|with\|else\)\b/@@@\1/g; s/Obj.magic \([a-z0-9_]\+\( [a-z0-9_]\+\)\+\)/Obj.magic (\1)/g; s/@@@//g' boot/menhir/menhirLib.ml
114117

115118

116119
## demote-menhir

boot/menhir/menhirLib.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1646,7 +1646,7 @@ module Make (T : TABLE) = struct
16461646
'a checkpoint
16471647
= function
16481648
| InputNeeded env ->
1649-
Obj.magic discard env
1649+
Obj.magic (discard env)
16501650
| _ ->
16511651
invalid_arg "offer expects InputNeeded"
16521652

@@ -1656,9 +1656,9 @@ module Make (T : TABLE) = struct
16561656
| HandlingError env ->
16571657
Obj.magic error ~strategy env
16581658
| Shifting (_, env, please_discard) ->
1659-
Obj.magic run env please_discard
1659+
Obj.magic (run env please_discard)
16601660
| AboutToReduce (env, prod) ->
1661-
Obj.magic reduce env prod
1661+
Obj.magic (reduce env prod)
16621662
| _ ->
16631663
invalid_arg "resume expects HandlingError | Shifting | AboutToReduce"
16641664

middle_end/flambda/inline_and_simplify.ml

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -675,11 +675,12 @@ and simplify_apply env r ~(apply : Flambda.apply) : Flambda.t * R.t =
675675
inlined = inlined_requested; specialise = specialise_requested;
676676
probe = probe_requested;
677677
} = apply in
678-
(* TODO: Most applications do not do local allocations in the current region,
679-
but this is not yet tracked, so we conservatively assume they may.
680-
Note that tail calls should always set the region used to true, because
681-
removing the surrounding region would change their meaning. *)
682-
let r = R.set_region_use r true in
678+
let r =
679+
match reg_close, mode with
680+
| (Rc_normal | Rc_nontail), Alloc_heap -> r
681+
| Rc_close_at_apply, _
682+
| _, Alloc_local -> R.set_region_use r true
683+
in
683684
let dbg = E.add_inlined_debuginfo env ~dbg in
684685
simplify_free_variable env lhs_of_application
685686
~f:(fun env lhs_of_application lhs_of_application_approx ->
@@ -840,6 +841,13 @@ and simplify_partial_application env r ~lhs_of_application
840841
List.fold_left (fun _mode (p,_) -> Parameter.alloc_mode p)
841842
function_decl.A.alloc_mode applied_args
842843
in
844+
if not (Lambda.sub_mode partial_mode mode) then
845+
Misc.fatal_errorf "Partial application of %a with wrong mode at %s"
846+
Closure_id.print closure_id_being_applied
847+
(Debuginfo.to_string dbg);
848+
let result_mode =
849+
if function_decl.A.region then Lambda.alloc_heap else Lambda.alloc_local
850+
in
843851
let wrapper_accepting_remaining_args =
844852
let body : Flambda.t =
845853
Apply {
@@ -848,7 +856,7 @@ and simplify_partial_application env r ~lhs_of_application
848856
kind = Direct closure_id_being_applied;
849857
dbg;
850858
reg_close = Rc_normal;
851-
mode;
859+
mode = result_mode;
852860
inlined = Default_inlined;
853861
specialise = Default_specialise;
854862
probe = None;

0 commit comments

Comments
 (0)