Skip to content

Commit e2851af

Browse files
committed
Replace extra bindings in Simplify_primitive_result by rewriting
1 parent 5e0dbc9 commit e2851af

9 files changed

Lines changed: 411 additions & 386 deletions

middle_end/flambda2/simplify/simplify_let_expr.ml

Lines changed: 66 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -443,73 +443,77 @@ let simplify_let0 ~simplify_expr ~simplify_function_body dacc let_expr
443443
let dacc, prior_lifted_constants = DA.get_and_clear_lifted_constants dacc in
444444
(* Simplify the defining expression. *)
445445
let defining_expr = L.defining_expr let_expr in
446-
let simplify_named_result, removed_operations =
446+
match
447447
Simplify_named.simplify_named dacc bound_pattern defining_expr
448448
~simplify_function_body
449-
in
450-
(* We must make sure that if [Invalid] is going to be produced, [uacc] doesn't
451-
contain any extraneous data for e.g. lifted constants that will never be
452-
placed, since this can lead to errors when loading .cmx files or similar.
453-
To avoid this we don't traverse [body]. *)
454-
match simplify_named_result with
455-
| Invalid ->
456-
down_to_up original_dacc ~rebuild:(fun uacc ~after_rebuild ->
457-
let uacc = UA.notify_removed ~operation:removed_operations uacc in
458-
EB.rebuild_invalid uacc
459-
(Defining_expr_of_let (bound_pattern, defining_expr))
460-
~after_rebuild)
461-
| Ok simplify_named_result ->
462-
let dacc = Simplify_named_result.dacc simplify_named_result in
463-
(* First accumulate variable, symbol and code ID usage information. *)
464-
(* CR-someday gbury/pchambart : in the case of an invalid, we currently
465-
over-approximate the uses. In case of an invalid, we might want to
466-
instead flush the uses of the current control flow branch (but this would
467-
require a more precise stack). *)
468-
(* We currently over-approximate the use of variables in symbols: both in
469-
the lifted constants, and in the bound constants, which we consider to be
470-
always used, leading to the free_names in their defining expressions to
471-
be considered as used unconditionally. *)
472-
let closure_info = DE.closure_info (DA.denv dacc) in
473-
(* Next remember any lifted constants that were generated during the
474-
simplification of the defining expression and sort them, since they may
475-
be mutually recursive. Then add back in to [dacc] the
476-
[prior_lifted_constants] remembered above. This results in the
477-
definitions and types for all these constants being available at a
478-
subsequent [Let_cont]. At such a point, [dacc] will be queried to
479-
retrieve all of the constants, which are then manually transferred into
480-
the computed [dacc] at the join point for subsequent simplification of
481-
the continuation handler(s).
449+
with
450+
| Rewritten f -> simplify_expr dacc (f body) ~down_to_up
451+
| Simplified (simplify_named_result, removed_operations) -> (
452+
(* We must make sure that if [Invalid] is going to be produced, [uacc]
453+
doesn't contain any extraneous data for e.g. lifted constants that will
454+
never be placed, since this can lead to errors when loading .cmx files or
455+
similar. To avoid this we don't traverse [body]. *)
456+
match simplify_named_result with
457+
| Invalid ->
458+
down_to_up original_dacc ~rebuild:(fun uacc ~after_rebuild ->
459+
let uacc = UA.notify_removed ~operation:removed_operations uacc in
460+
EB.rebuild_invalid uacc
461+
(Defining_expr_of_let (bound_pattern, defining_expr))
462+
~after_rebuild)
463+
| Ok simplify_named_result ->
464+
let dacc = Simplify_named_result.dacc simplify_named_result in
465+
(* First accumulate variable, symbol and code ID usage information. *)
466+
(* CR-someday gbury/pchambart : in the case of an invalid, we currently
467+
over-approximate the uses. In case of an invalid, we might want to
468+
instead flush the uses of the current control flow branch (but this
469+
would require a more precise stack). *)
470+
(* We currently over-approximate the use of variables in symbols: both in
471+
the lifted constants, and in the bound constants, which we consider to
472+
be always used, leading to the free_names in their defining expressions
473+
to be considered as used unconditionally. *)
474+
let closure_info = DE.closure_info (DA.denv dacc) in
475+
(* Next remember any lifted constants that were generated during the
476+
simplification of the defining expression and sort them, since they may
477+
be mutually recursive. Then add back in to [dacc] the
478+
[prior_lifted_constants] remembered above. This results in the
479+
definitions and types for all these constants being available at a
480+
subsequent [Let_cont]. At such a point, [dacc] will be queried to
481+
retrieve all of the constants, which are then manually transferred into
482+
the computed [dacc] at the join point for subsequent simplification of
483+
the continuation handler(s).
482484
483-
Note that no lifted constants are ever placed during the simplification
484-
of the defining expression. (Not even in the case of a [Set_of_closures]
485-
binding, since "let symbol" is disallowed under a lambda.) *)
486-
let lifted_constants_from_defining_expr = DA.get_lifted_constants dacc in
487-
let dacc =
488-
DA.add_to_lifted_constant_accumulator dacc prior_lifted_constants
489-
in
490-
let rewrite_id = Named_rewrite_id.create () in
491-
let dacc =
492-
DA.map_flow_acc dacc
493-
~f:
494-
(update_data_flow dacc closure_info ~rewrite_id
495-
~lifted_constants_from_defining_expr simplify_named_result)
496-
in
497-
let at_unit_toplevel = DE.at_unit_toplevel (DA.denv dacc) in
498-
(* Simplify the body of the let-expression and make the new [Let] bindings
499-
around the simplified body. [Simplify_named] will already have prepared
500-
[dacc] with the necessary bindings for the simplification of the body. *)
501-
let down_to_up dacc ~rebuild:rebuild_body =
502-
let rebuild uacc ~after_rebuild =
503-
let after_rebuild body uacc =
504-
rebuild_let simplify_named_result removed_operations
505-
~lifted_constants_from_defining_expr ~at_unit_toplevel ~closure_info
506-
~body uacc ~after_rebuild ~rewrite_id
485+
Note that no lifted constants are ever placed during the simplification
486+
of the defining expression. (Not even in the case of a
487+
[Set_of_closures] binding, since "let symbol" is disallowed under a
488+
lambda.) *)
489+
let lifted_constants_from_defining_expr = DA.get_lifted_constants dacc in
490+
let dacc =
491+
DA.add_to_lifted_constant_accumulator dacc prior_lifted_constants
492+
in
493+
let rewrite_id = Named_rewrite_id.create () in
494+
let dacc =
495+
DA.map_flow_acc dacc
496+
~f:
497+
(update_data_flow dacc closure_info ~rewrite_id
498+
~lifted_constants_from_defining_expr simplify_named_result)
499+
in
500+
let at_unit_toplevel = DE.at_unit_toplevel (DA.denv dacc) in
501+
(* Simplify the body of the let-expression and make the new [Let] bindings
502+
around the simplified body. [Simplify_named] will already have prepared
503+
[dacc] with the necessary bindings for the simplification of the
504+
body. *)
505+
let down_to_up dacc ~rebuild:rebuild_body =
506+
let rebuild uacc ~after_rebuild =
507+
let after_rebuild body uacc =
508+
rebuild_let simplify_named_result removed_operations
509+
~lifted_constants_from_defining_expr ~at_unit_toplevel
510+
~closure_info ~body uacc ~after_rebuild ~rewrite_id
511+
in
512+
rebuild_body uacc ~after_rebuild
507513
in
508-
rebuild_body uacc ~after_rebuild
514+
down_to_up dacc ~rebuild
509515
in
510-
down_to_up dacc ~rebuild
511-
in
512-
simplify_expr dacc body ~down_to_up
516+
simplify_expr dacc body ~down_to_up)
513517

514518
let simplify_let ~simplify_expr ~simplify_function_body dacc let_expr
515519
~down_to_up =

0 commit comments

Comments
 (0)