Skip to content

Commit 2240f8e

Browse files
authored
Wrapper functions to EB (ocaml#352)
1 parent 50c4982 commit 2240f8e

File tree

7 files changed

+173
-161
lines changed

7 files changed

+173
-161
lines changed

.depend

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6038,6 +6038,7 @@ middle_end/flambda/simplify/expr_builder.cmo : \
60386038
middle_end/flambda/compilenv_deps/variable.cmi \
60396039
middle_end/flambda/basic/var_within_closure.cmi \
60406040
middle_end/flambda/naming/var_in_binding_pos.cmi \
6041+
middle_end/flambda/simplify/env/upwards_env.cmi \
60416042
middle_end/flambda/simplify/env/upwards_acc.cmi \
60426043
middle_end/flambda/compilenv_deps/target_imm.cmi \
60436044
lambda/tag.cmi \
@@ -6053,21 +6054,26 @@ middle_end/flambda/simplify/expr_builder.cmo : \
60536054
utils/misc.cmi \
60546055
middle_end/flambda/lifting/lifted_constant_state.cmi \
60556056
middle_end/flambda/lifting/lifted_constant.cmi \
6057+
middle_end/flambda/basic/kinded_parameter.cmi \
60566058
middle_end/flambda/terms/flambda_primitive.cmi \
6059+
middle_end/flambda/types/kinds/flambda_arity.cmi \
60576060
middle_end/flambda/terms/flambda.cmi \
60586061
lambda/debuginfo.cmi \
6062+
middle_end/flambda/basic/continuation.cmi \
60596063
middle_end/flambda/basic/code_id_or_symbol.cmi \
60606064
middle_end/flambda/basic/code_id.cmi \
60616065
middle_end/flambda/types/structures/code_age_relation.cmi \
60626066
utils/clflags.cmi \
60636067
middle_end/flambda/terms/bound_symbols.cmi \
60646068
middle_end/flambda/naming/bindable_let_bound.cmi \
6069+
middle_end/flambda/simplify/basic/apply_cont_rewrite.cmi \
60656070
middle_end/flambda/terms/apply_cont_expr.cmi \
60666071
middle_end/flambda/simplify/expr_builder.cmi
60676072
middle_end/flambda/simplify/expr_builder.cmx : \
60686073
middle_end/flambda/compilenv_deps/variable.cmx \
60696074
middle_end/flambda/basic/var_within_closure.cmx \
60706075
middle_end/flambda/naming/var_in_binding_pos.cmx \
6076+
middle_end/flambda/simplify/env/upwards_env.cmx \
60716077
middle_end/flambda/simplify/env/upwards_acc.cmx \
60726078
middle_end/flambda/compilenv_deps/target_imm.cmx \
60736079
lambda/tag.cmx \
@@ -6083,15 +6089,19 @@ middle_end/flambda/simplify/expr_builder.cmx : \
60836089
utils/misc.cmx \
60846090
middle_end/flambda/lifting/lifted_constant_state.cmx \
60856091
middle_end/flambda/lifting/lifted_constant.cmx \
6092+
middle_end/flambda/basic/kinded_parameter.cmx \
60866093
middle_end/flambda/terms/flambda_primitive.cmx \
6094+
middle_end/flambda/types/kinds/flambda_arity.cmx \
60876095
middle_end/flambda/terms/flambda.cmx \
60886096
lambda/debuginfo.cmx \
6097+
middle_end/flambda/basic/continuation.cmx \
60896098
middle_end/flambda/basic/code_id_or_symbol.cmx \
60906099
middle_end/flambda/basic/code_id.cmx \
60916100
middle_end/flambda/types/structures/code_age_relation.cmx \
60926101
utils/clflags.cmx \
60936102
middle_end/flambda/terms/bound_symbols.cmx \
60946103
middle_end/flambda/naming/bindable_let_bound.cmx \
6104+
middle_end/flambda/simplify/basic/apply_cont_rewrite.cmx \
60956105
middle_end/flambda/terms/apply_cont_expr.cmx \
60966106
middle_end/flambda/simplify/expr_builder.cmi
60976107
middle_end/flambda/simplify/expr_builder.cmi : \
@@ -6103,9 +6113,13 @@ middle_end/flambda/simplify/expr_builder.cmi : \
61036113
middle_end/flambda/naming/name_occurrences.cmi \
61046114
middle_end/flambda/lifting/lifted_constant_state.cmi \
61056115
middle_end/flambda/lifting/lifted_constant.cmi \
6116+
middle_end/flambda/types/kinds/flambda_arity.cmi \
61066117
middle_end/flambda/terms/flambda.cmi \
6118+
middle_end/flambda/basic/continuation.cmi \
61076119
middle_end/flambda/types/structures/code_age_relation.cmi \
61086120
middle_end/flambda/naming/bindable_let_bound.cmi \
6121+
middle_end/flambda/terms/apply_expr.cmi \
6122+
middle_end/flambda/basic/apply_cont_rewrite_id.cmi \
61096123
middle_end/flambda/terms/apply_cont_expr.cmi
61106124
middle_end/flambda/simplify/simplify.cmo : \
61116125
middle_end/flambda/compilenv_deps/variable.cmi \
@@ -6312,7 +6326,6 @@ middle_end/flambda/simplify/simplify_common.cmo : \
63126326
middle_end/flambda/types/kinds/flambda_arity.cmi \
63136327
middle_end/flambda/basic/exn_continuation.cmi \
63146328
middle_end/flambda/simplify/env/downwards_acc.cmi \
6315-
lambda/debuginfo.cmi \
63166329
middle_end/flambda/basic/continuation.cmi \
63176330
middle_end/flambda/terms/call_kind.cmi \
63186331
middle_end/flambda/simplify/basic/apply_cont_rewrite.cmi \
@@ -6331,7 +6344,6 @@ middle_end/flambda/simplify/simplify_common.cmx : \
63316344
middle_end/flambda/types/kinds/flambda_arity.cmx \
63326345
middle_end/flambda/basic/exn_continuation.cmx \
63336346
middle_end/flambda/simplify/env/downwards_acc.cmx \
6334-
lambda/debuginfo.cmx \
63356347
middle_end/flambda/basic/continuation.cmx \
63366348
middle_end/flambda/terms/call_kind.cmx \
63376349
middle_end/flambda/simplify/basic/apply_cont_rewrite.cmx \

middle_end/flambda/simplify/expr_builder.ml

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@
1919
open! Flambda.Import
2020

2121
module BLB = Bindable_let_bound
22+
module KP = Kinded_parameter
2223
module LC = Lifted_constant
2324
module LCS = Lifted_constant_state
2425
module P = Flambda_primitive
2526
module UA = Upwards_acc
27+
module UE = Upwards_env
2628
module VB = Var_in_binding_pos
2729

2830
(* The constructed values of this type aren't currently used, but will be
@@ -537,3 +539,137 @@ let create_switch uacc ~scrutinee ~arms =
537539
Expr.create_switch switch,
538540
UA.cost_metrics_add ~added:(Cost_metrics.switch switch) uacc
539541

542+
type add_wrapper_for_fixed_arity_continuation0_result =
543+
| This_continuation of Continuation.t
544+
| Apply_cont of Apply_cont.t
545+
| New_wrapper of Continuation.t * Continuation_handler.t * Cost_metrics.t
546+
547+
type cont_or_apply_cont =
548+
| Continuation of Continuation.t
549+
| Apply_cont of Apply_cont.t
550+
551+
let add_wrapper_for_fixed_arity_continuation0 uacc cont_or_apply_cont
552+
~use_id arity : add_wrapper_for_fixed_arity_continuation0_result =
553+
let uenv = UA.uenv uacc in
554+
let cont =
555+
match cont_or_apply_cont with
556+
| Continuation cont -> cont
557+
| Apply_cont apply_cont -> Apply_cont.continuation apply_cont
558+
in
559+
let original_cont = cont in
560+
let cont = UE.resolve_continuation_aliases uenv cont in
561+
match UE.find_apply_cont_rewrite uenv original_cont with
562+
| None -> This_continuation cont
563+
| Some rewrite when Apply_cont_rewrite.does_nothing rewrite ->
564+
(* CR mshinwell: think more about this check w.r.t. subkinds *)
565+
let arity = Flambda_arity.With_subkinds.to_arity arity in
566+
let arity_in_rewrite =
567+
Apply_cont_rewrite.original_params_arity rewrite
568+
|> Flambda_arity.With_subkinds.to_arity
569+
in
570+
if not (Flambda_arity.equal arity arity_in_rewrite) then begin
571+
Misc.fatal_errorf "Arity %a provided to fixed-arity-wrapper \
572+
addition function does not match arity %a in rewrite:@ %a"
573+
Flambda_arity.print arity
574+
Flambda_arity.print arity_in_rewrite
575+
Apply_cont_rewrite.print rewrite
576+
end;
577+
This_continuation cont
578+
| Some rewrite ->
579+
(* CR-someday mshinwell: This area should be improved and hence
580+
simplified. Allowing [Apply] to take extra arguments is probably the
581+
way forward. Although unboxing of variants requires untagging
582+
expressions to be inserted, so wrappers cannot always be avoided. *)
583+
let params = List.map (fun _kind -> Variable.create "param") arity in
584+
let kinded_params = List.map2 KP.create params arity in
585+
let new_wrapper expr ~free_names ~cost_metrics =
586+
let new_cont = Continuation.create () in
587+
let new_handler =
588+
Continuation_handler.create kinded_params ~handler:expr
589+
~free_names_of_handler:free_names
590+
~is_exn_handler:false
591+
in
592+
New_wrapper (new_cont, new_handler, cost_metrics)
593+
in
594+
match cont_or_apply_cont with
595+
| Continuation cont ->
596+
(* In this case, any generated [Apply_cont] will sit inside a wrapper
597+
that binds [kinded_params]. *)
598+
let args = List.map KP.simple kinded_params in
599+
let apply_cont = Apply_cont.create cont ~args ~dbg:Debuginfo.none in
600+
begin match Apply_cont_rewrite.rewrite_use rewrite use_id apply_cont with
601+
| Apply_cont apply_cont ->
602+
new_wrapper (Expr.create_apply_cont apply_cont)
603+
~free_names:(Known (Apply_cont.free_names apply_cont))
604+
~cost_metrics:(Cost_metrics.apply_cont apply_cont)
605+
| Expr build_expr ->
606+
let expr, cost_metrics, free_names =
607+
build_expr ~apply_cont_to_expr:(fun apply_cont ->
608+
Expr.create_apply_cont apply_cont,
609+
Cost_metrics.apply_cont apply_cont,
610+
Apply_cont.free_names apply_cont)
611+
in
612+
new_wrapper expr ~free_names:(Known free_names) ~cost_metrics
613+
end
614+
| Apply_cont apply_cont ->
615+
let apply_cont = Apply_cont.update_continuation apply_cont cont in
616+
match Apply_cont_rewrite.rewrite_use rewrite use_id apply_cont with
617+
| Apply_cont apply_cont -> Apply_cont apply_cont
618+
| Expr build_expr ->
619+
let expr, cost_metrics, free_names =
620+
build_expr ~apply_cont_to_expr:(fun apply_cont ->
621+
Expr.create_apply_cont apply_cont,
622+
Cost_metrics.apply_cont apply_cont,
623+
Apply_cont.free_names apply_cont)
624+
in
625+
new_wrapper expr ~free_names:(Known free_names) ~cost_metrics
626+
627+
type add_wrapper_for_switch_arm_result =
628+
| Apply_cont of Apply_cont.t
629+
| New_wrapper of Continuation.t * Continuation_handler.t * Cost_metrics.t
630+
631+
let add_wrapper_for_switch_arm uacc apply_cont ~use_id arity
632+
: add_wrapper_for_switch_arm_result =
633+
match
634+
add_wrapper_for_fixed_arity_continuation0 uacc (Apply_cont apply_cont)
635+
~use_id arity
636+
with
637+
| This_continuation cont ->
638+
Apply_cont (Apply_cont.update_continuation apply_cont cont)
639+
| Apply_cont apply_cont -> Apply_cont apply_cont
640+
| New_wrapper (cont, wrapper, cost_metrics) -> New_wrapper (cont, wrapper, cost_metrics)
641+
642+
let add_wrapper_for_fixed_arity_continuation uacc cont ~use_id arity ~around =
643+
match
644+
add_wrapper_for_fixed_arity_continuation0 uacc (Continuation cont)
645+
~use_id arity
646+
with
647+
| This_continuation cont -> around uacc cont
648+
| Apply_cont _ -> assert false
649+
| New_wrapper (new_cont, new_handler, cost_metrics_of_handler) ->
650+
let body, uacc = around uacc new_cont in
651+
let added =
652+
Cost_metrics.increase_due_to_let_cont_non_recursive ~cost_metrics_of_handler
653+
in
654+
Let_cont.create_non_recursive new_cont new_handler ~body
655+
~free_names_of_body:(Known (Expr.free_names body)),
656+
UA.cost_metrics_add ~added uacc
657+
658+
let add_wrapper_for_fixed_arity_apply uacc ~use_id arity apply =
659+
match Apply.continuation apply with
660+
| Never_returns ->
661+
Expr.create_apply apply,
662+
UA.cost_metrics_add ~added:(Cost_metrics.apply apply) uacc
663+
| Return cont ->
664+
add_wrapper_for_fixed_arity_continuation uacc cont
665+
~use_id arity
666+
~around:(fun uacc return_cont ->
667+
let exn_cont =
668+
UE.resolve_exn_continuation_aliases (UA.uenv uacc)
669+
(Apply.exn_continuation apply)
670+
in
671+
let apply =
672+
Apply.with_continuations apply (Return return_cont) exn_cont
673+
in
674+
Expr.create_apply apply,
675+
UA.cost_metrics_add ~added:(Cost_metrics.apply apply) uacc)

middle_end/flambda/simplify/expr_builder.mli

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,21 @@ val create_switch
7575
-> scrutinee:Simple.t
7676
-> arms:Apply_cont_expr.t Target_imm.Map.t
7777
-> Expr.t * Upwards_acc.t
78+
79+
type add_wrapper_for_switch_arm_result = private
80+
| Apply_cont of Apply_cont.t
81+
| New_wrapper of Continuation.t * Continuation_handler.t * Cost_metrics.t
82+
83+
val add_wrapper_for_switch_arm
84+
: Upwards_acc.t
85+
-> Apply_cont.t
86+
-> use_id:Apply_cont_rewrite_id.t
87+
-> Flambda_arity.With_subkinds.t
88+
-> add_wrapper_for_switch_arm_result
89+
90+
val add_wrapper_for_fixed_arity_apply
91+
: Upwards_acc.t
92+
-> use_id:Apply_cont_rewrite_id.t
93+
-> Flambda_arity.With_subkinds.t
94+
-> Apply_expr.t
95+
-> Expr.t * Upwards_acc.t

middle_end/flambda/simplify/simplify_apply_expr.ml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,7 @@ let rebuild_non_inlined_direct_full_application apply ~use_id ~exn_cont_use_id
8181
Expr.create_apply apply,
8282
UA.cost_metrics_add ~added:(Cost_metrics.apply apply) uacc
8383
| Some use_id ->
84-
Simplify_common.add_wrapper_for_fixed_arity_apply uacc ~use_id
85-
result_arity apply
84+
EB.add_wrapper_for_fixed_arity_apply uacc ~use_id result_arity apply
8685
in
8786
let uacc = UA.add_free_names uacc (Expr.free_names expr) in
8887
after_rebuild expr uacc
@@ -431,7 +430,7 @@ let rebuild_function_call_where_callee's_type_unavailable apply call_kind
431430
|> Simplify_common.update_exn_continuation_extra_args uacc ~exn_cont_use_id
432431
in
433432
let expr, uacc =
434-
Simplify_common.add_wrapper_for_fixed_arity_apply uacc ~use_id
433+
EB.add_wrapper_for_fixed_arity_apply uacc ~use_id
435434
(Call_kind.return_arity call_kind) apply
436435
in
437436
let uacc = UA.add_free_names uacc (Expr.free_names expr) in
@@ -670,7 +669,7 @@ let rebuild_method_call apply ~use_id ~exn_cont_use_id uacc ~after_rebuild =
670669
apply
671670
in
672671
let expr, uacc =
673-
Simplify_common.add_wrapper_for_fixed_arity_apply uacc ~use_id
672+
EB.add_wrapper_for_fixed_arity_apply uacc ~use_id
674673
(Flambda_arity.With_subkinds.create [K.With_subkind.any_value]) apply
675674
in
676675
let uacc = UA.add_free_names uacc (Expr.free_names expr) in
@@ -728,7 +727,7 @@ let rebuild_c_call apply ~use_id ~exn_cont_use_id ~return_arity uacc
728727
let expr, uacc =
729728
match use_id with
730729
| Some use_id ->
731-
Simplify_common.add_wrapper_for_fixed_arity_apply uacc ~use_id
730+
EB.add_wrapper_for_fixed_arity_apply uacc ~use_id
732731
(Flambda_arity.With_subkinds.of_arity return_arity) apply
733732
| None ->
734733
Expr.create_apply apply,

0 commit comments

Comments
 (0)