Skip to content

Commit

Permalink
flambda-backend: make mode system scale better with more axes (#2395)
Browse files Browse the repository at this point in the history
* make mode constants scale

* make mode checking scale

* decompose axis access

* reuse existing representation of axis

* decompose monadic vs. comonadic
  • Loading branch information
riaqn authored Apr 4, 2024
1 parent c3c8b47 commit 4571032
Show file tree
Hide file tree
Showing 8 changed files with 617 additions and 721 deletions.
4 changes: 2 additions & 2 deletions lambda/translmode.ml
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ let transl_locality_mode_r locality =

let transl_alloc_mode_l mode =
(* we only take the locality axis *)
Alloc.locality mode |> transl_locality_mode_l
Alloc.proj (Comonadic Areality) mode |> transl_locality_mode_l

let transl_alloc_mode_r mode =
(* we only take the locality axis *)
Alloc.locality mode |> transl_locality_mode_r
Alloc.proj (Comonadic Areality) mode |> transl_locality_mode_r

let transl_modify_mode locality =
match Locality.zap_to_floor locality with
Expand Down
8 changes: 4 additions & 4 deletions typing/ctype.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1583,9 +1583,9 @@ let prim_mode mvar = function
put in [mode.ml] *)
let with_locality locality m =
let m' = Alloc.newvar () in
Locality.equate_exn (Alloc.locality m') locality;
Alloc.submode_exn m' (Alloc.join_with_locality Locality.Const.max m);
Alloc.submode_exn (Alloc.meet_with_locality Locality.Const.min m) m';
Locality.equate_exn (Alloc.proj (Comonadic Areality) m') locality;
Alloc.submode_exn m' (Alloc.join_with (Comonadic Areality) Locality.Const.max m);
Alloc.submode_exn (Alloc.meet_with (Comonadic Areality) Locality.Const.min m) m';
m'

let rec instance_prim_locals locals mvar macc finalret ty =
Expand Down Expand Up @@ -5578,7 +5578,7 @@ let mode_cross_left env ty mode =
now; will return and figure this out later. *)
let jkind = type_jkind_purely env ty in
let upper_bounds = Jkind.get_modal_upper_bounds jkind in
Alloc.meet_with upper_bounds mode
Alloc.meet_const upper_bounds mode

(* CR layouts v2.8: merge with Typecore.expect_mode_cross when [Value]
and [Alloc] get unified *)
Expand Down
14 changes: 7 additions & 7 deletions typing/env.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2958,7 +2958,7 @@ let lookup_ident_module (type a) (load : a load) ~errors ~use ~loc s env =
let escape_mode ~errors ~env ~loc id vmode escaping_context =
match
Mode.Regionality.submode
(Mode.Value.regionality vmode)
(Mode.Value.proj (Comonadic Areality) vmode)
(Mode.Regionality.global)
with
| Ok () -> ()
Expand All @@ -2969,13 +2969,13 @@ let escape_mode ~errors ~env ~loc id vmode escaping_context =
let share_mode ~errors ~env ~loc id vmode shared_context =
match
Mode.Linearity.submode
(Mode.Value.linearity vmode)
(Mode.Value.proj (Comonadic Linearity) vmode)
Mode.Linearity.many
with
| Error _ ->
may_lookup_error errors loc env
(Once_value_used_in (id, shared_context))
| Ok () -> Mode.Value.join [Mode.Value.min_with_uniqueness Mode.Uniqueness.shared; vmode]
| Ok () -> Mode.Value.join [Mode.Value.min_with (Monadic Uniqueness) Mode.Uniqueness.shared; vmode]

let closure_mode ~errors ~env ~loc id {Mode.monadic; comonadic}
closure_context comonadic0 : Mode.Value.l =
Expand All @@ -2998,7 +2998,7 @@ let closure_mode ~errors ~env ~loc id {Mode.monadic; comonadic}
let exclave_mode ~errors ~env ~loc id vmode =
match
Mode.Regionality.submode
(Mode.Value.regionality vmode)
(Mode.Value.proj (Comonadic Areality) vmode)
Mode.Regionality.regional
with
| Ok () -> vmode |> Mode.value_to_alloc_r2l |> Mode.alloc_as_value
Expand Down Expand Up @@ -3962,15 +3962,15 @@ let report_lookup_error _loc env ppf = function
| Value_used_in_closure (lid, error, context) ->
let e0, e1 =
match error with
| `Regionality _ -> "local", "might escape"
| `Linearity _ -> "once", "is many"
| Error (Areality, _) -> "local", "might escape"
| Error (Linearity, _) -> "once", "is many"
in
fprintf ppf
"@[The value %a is %s, so cannot be used \
inside a closure that %s.@]"
!print_longident lid e0 e1;
begin match error, context with
| `Regionality _, Some Tailcall_argument ->
| Error (Areality, _), Some Tailcall_argument ->
fprintf ppf "@.@[Hint: The closure might escape because it \
is an argument to a tail call@]"
| _ -> ()
Expand Down
Loading

0 comments on commit 4571032

Please sign in to comment.