Skip to content

Make tailrec label optional #48

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed

Conversation

xclerc
Copy link
Contributor

@xclerc xclerc commented Jun 10, 2021

This pull request changes the type of the fun_tailrec_entry_point_label
field from Label.t to Label.t option. The rationale is that the block
acting as the "tailrec" entry point can be deleted by the fallthrough/dead
code optimization, leaving the field to point to a block not present in
the blocks field. While this did not trigger any issue in the current code,
it appears safer to keep the fun_tailrec_entry_point_label and blocks
fields consistent with each other.

@gretay-js
Copy link
Contributor

This leaves Linear.fundecl.fun_tailrec_entry_point_label dangling if the "tailrec" block is eliminated.
We can resolve it by making the field in Linear optional.

@xclerc
Copy link
Contributor Author

xclerc commented Jun 10, 2021

I naïvely expected the compiler to spot such an issue...
The culprit is, obviously, this expression; are we sure the
other fields will never be in need of an update?

26ae9da is propagating the change to Linear.fundecl.

@xclerc
Copy link
Contributor Author

xclerc commented Jun 10, 2021

Actually, I am not sure this change is correct.

Could not we be in a situation were we remove an
empty "tailrec" block but still have recursive tail calls?

We can probably not hit this case currently, as
Linearize.linear will not produce empty blocks.

@@ -138,7 +142,7 @@ let replace_successor_labels t ~normal ~exn block ~f =
t.fun_tailrec_entry_point_label and the tailrec entry point
block has as its predecessors *all* the "tailcall self" blocks. *)
t.fun_tailrec_entry_point_label <-
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the case of Tailcall(Self), t.fun_tailrec_entry_point_label must not be None, so Option.map is fine, but may be worth checking.

@gretay-js
Copy link
Contributor

The culprit is, obviously, this expression; are we sure the
other fields will never be in need of an update?

Yes, I expect that we will update most of the other fields. Ocamlcfg.Cfg_with_layout.to_linear should return Linear.fundecl instead of Linear.instruction.

Copy link
Contributor

@gretay-js gretay-js left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change makes it explicit whether the tailrec entry is used or not. Can you please add a check that sets this field correctly (see comments)?

Could not we be in a situation were we remove an empty "tailrec" block but still have recursive tail calls?

yes, it's possible if tailrec block is a fallthrough block and empty. I think this is common and i think this PR correctly updates fun_tailrec_entry_point_label of the CFG in this case. Could we have a small test for it?

It is not likely that the tailrec block is dead. but we should either have an assert false in that case or handle it correctly.

Comment on lines +68 to +73
begin match cfg.fun_tailrec_entry_point_label with
| None -> ()
| Some label ->
if List.exists (Label.equal label) found_dead then
Cfg.set_fun_tailrec_entry_point_label cfg None
end)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this check is not correct here: if there is another Tailcall Self that is not dead, we cannot set fun_tailrec_entry_point_label to None. Instead, we should check at the end of CFG construction and transformations whether any block has Tailcall Self and if not, then we can set fun_tailrec_entry_point_label to None.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry, this is actually fine and I was confused about who is dead! I still think it's better to do this check at the end.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still think it's better to do this check at the end.

I am slightly confused: it looks like the operation is the very
last of the function, and hence the whole transformation.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check will happen at the end of each recursive call.
What I meant is to have a separate pass that checks if the input Cfg has any Tailcall Self, and if it does not, sets tailrec label to None. This pass can be called at the end of Linear_to_cfg.run, and at the end of Eliminate_dead_blocks.run and at the end of Eliminate_fallthrough_blocks.run.
What you have here is more efficient, because it fixes up tailrec label only as needed, but I think we have this problem in the first place because the representation of tailrec label is problematic.

As suggested offline, this problem would completely go away if we change the representation of Tailcall Self to include the label, and also remove fun_tailrec_entry_point_label in Cfg. It is still worth changing Linear.fundecl.fun_tailrec_entry_point_label to an option. The pass Cfg_to_linear can compute the field and check its consistency (i.e., all Tailrec Self have the same destination label).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand your point about the recursive calls.
However, it sounds simpler to have these calls accumulate
the set of dead blocks rather than have a whole new phase/
traversal at the end of run. run would get the set of
eliminated blocks from eliminate_dead_blocks and the
code currently at lines 68-73 would be at the end of run.

Comment on lines +78 to +80
if List.exists (Label.equal label) found then
Cfg.set_fun_tailrec_entry_point_label cfg None
end)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above, I think this check should be done at the end of the entire transformation.

Unlike the above case, this seem correct for a subtle reason: if tailrec block was a fallthrough and got eliminated, then either (a) there are Tailcall Self, and then the label here will be the new fun_tailrec_entry_point_label which is the successor of the original one, so won't be in found or (b) there are no Tailcall Self, and the label is not used and can be safely set to None.

@xclerc
Copy link
Contributor Author

xclerc commented Jun 11, 2021

Independently of the discussion about the proposed code,
do we think that removing the notion of tailrec calls(thus
replacing such calls with mere jumps) should happen?

I am always a bit wary of removing structure from a
representation, but the only real reason I am not sure
whether this change is a good idea it related to the
emission of DWARF info. Would DWARF allow us to mark
a jump as actually being a tailrec call?

@gretay-js @mshinwell

@gretay-js
Copy link
Contributor

My original proposal of replacing "Tailcall Self" with jump was wrong, as you point out, because it wouldn't work with DWARF. The current proposal is to add the destination label to the terminator itself, instead of storing (and sharing) the label at function level.

@xclerc
Copy link
Contributor Author

xclerc commented Jun 11, 2021

Sorry, I missed this change. Then, there is no loss of information.

@xclerc
Copy link
Contributor Author

xclerc commented Jun 23, 2021

(Made obsolete by #51.)

@xclerc xclerc closed this Jun 23, 2021
stedolan added a commit to stedolan/flambda-backend that referenced this pull request Jan 18, 2022
1924269795 Several fixes for partial application and currying
4fee6ae2e8 Pprintast support for new local syntax
8df43e93e5 Quieten Makefile when runtime dep files are not present
88ec84e29e Typecheck x |> f y as (f y x), not ((f y) x)
87a10e3348 Remove autogeneration of @ocaml.curry
c656dc9 Merge flambda-backend changes
11b5424 Avoid printing double spaces in function argument lists
7751faa Restore locations to Typedtree.{pat,let}_bound_idents_full
e450b6c add build_ocaml_compiler.sexp
0403bb3 Revert PR 9895 to continue installing VERSION
b3447db Ensure new local attributes are namespaced properly
7f213fc Allow empty functions again
8f22ad8 Bugfix: ensure local domain state is initialised
80f54dd Bugfix for Selectgen with regions
e8133a1 Fix external-external signature inclusion
9840051 Bootstrap
d879f23 Merge remote-tracking branch 'jane/local-reviewed' into local-merge
94454f5 Use Local_store for the local allocations ref
54a164c Create fewer regions, according to typechecking (ocaml-flambda#59)
1c2479b Merge flambda-backend changes
ce34678 Fix printing of modes in return types
91f2281 Hook mode variable solving into Btype.snapshot/backtrack
54e4b09 Move Alloc_mode and Value_mode to Btype
ff4611e Merge flambda-backend changes
ce62e45 Ensure allocations are initialised, even dead ones
6b6ec5a Fix the alloc.ml test on 32-bit builds
81e9879 Merge flambda-backend changes
40a7f89 Update repo URL for ocaml-jst, and rename script.
0454ee7 Add some new locally-allocating primitives (ocaml-flambda#57)
8acdda1 Reset the local stack pointer in exception handlers (ocaml-flambda#56)
8dafa98 Improve typing for (||) and (&&) (ocaml-flambda#55)
8c64754 Fix make_check_all_arches (ocaml-flambda#54)
b50cd45 Allow arguments to primitives to be local even in tail position (ocaml-flambda#53)
cad125d Fix modes from or-patterns (ocaml-flambda#50)
4efdb72 Fix tailcalls tests with inlining (ocaml-flambda#52)
4a795cb Flambda support (ocaml-flambda#49)
74722cb Add [@ocaml.principal] and [@ocaml.noprincipal] attributes, and use in oo.mli
6d7d3b8 Ensure that functions are evaluated after their arguments (flambda-backend ocaml-flambda#353)
89bda6b Keep Sys.opaque_identity in Cmm and Mach (port upstream PR 9412)
a39126a Fix tailcalls within regions (ocaml-flambda#48)
4ac4cfd Fix stdlib manpages build
3a95f5e Merge flambda-backend changes
efe80c9 Add jane/pull-flambda-patches script
fca94c4 Register allocations for Omitted parameter closures (ocaml-flambda#47)
103b139 Remove various FIXMEs (ocaml-flambda#46)
62ba2c1 Bootstrap
a0062ad Allow local allocations for various primitives (ocaml-flambda#43)
7a2165e Allow primitives to be poly-moded (ocaml-flambda#43)
2af3f55 Fix a flaky test by refactoring TypePairs (ocaml/ocaml#10638)
58dd807 Bootstrap
ee3be10 Fix modes in build_apply for partial applications
fe73656 Tweak for evaluation order of labelled partial applications (#10653)
0527570 Fix caml_modify on local allocations (ocaml-flambda#40)
e657e99 Relax modes for `as` patterns (ocaml-flambda#42)
f815bf2 Add special mode handling for tuples in matches and let bindings (ocaml-flambda#38)
39f1211 Only take the upper bounds of modes associated with allocations (ocaml-flambda#37)
aec6fde Interpret arrow types in "local positions" differently
c4f3319 Bootstrap
ff6fdad Add some missing regions
40d586d Bootstrap
66d8110 Switch to a system with 3 modes for values
f2c5a85 Bugfix for Comballoc with local allocations. (ocaml-flambda#41)
83bcd09 Fix bug with root scanning during compaction (ocaml-flambda#39)
1b5ec83 Track modes in Lambda.lfunction and onwards (ocaml-flambda#33)
f1e2e97 Port ocaml/ocaml#10728
56703cd Port ocaml/ocaml#10081
eb66785 Support local allocations in i386 and fix amd64 bug (ocaml-flambda#31)
c936b19 Disallow local recursive non-functions (ocaml-flambda#30)
c7a193a GC support for local allocations (ocaml-flambda#29)
8dd7270 Nonlocal fields (ocaml-flambda#28)
e19a2f0 Bootstrap
694b9ac Add syntax to the parser for local allocations (ocaml-flambda#26)
f183008 Lower initial stack size
918226f Allow local closure allocations (ocaml-flambda#27)
2552e7d Introduce mode variables (ocaml-flambda#25)
bc41c99 Minor fixes for local allocations (ocaml-flambda#24)
a2a4e60 Runtime and compiler support for more local allocations (ocaml-flambda#23)
d030554 Typechecking for local allocations (ocaml-flambda#21)
9ee2332 Bugfix missing from ocaml-flambda#20
02c4cef Retain block-structured local regions until Mach.
86dbe1c amd64: Move stack realloc calls out-of-line
324d218 More typing modes and locking of environments
a4080b8 Initial version of local allocation (unsafe)

git-subtree-dir: ocaml
git-subtree-split: 1924269795db2450be5c084f7799340e0e003e19
stedolan added a commit that referenced this pull request Feb 1, 2022
173842c Merge flambda-backend changes
ed7eba2 Remove leading space from LINE. (#484)
bd61170 Bump magic numbers (#5)
c50c47d Add CI builds with local allocations enabled
1412792 Move local allocations support behind '-extension local'
6d8e42a Better tail call behaviour in caml_applyN
c7dac3d Typemod: toplevel bindings escape even if no variables are bound
82d6c3e Several fixes for partial application and currying
d05c70c Pprintast support for new local syntax
e0e62fc Typecheck x |> f y as (f y x), not ((f y) x)
d7e34ce Remove autogeneration of @ocaml.curry
b9a0593 Port #493
0a872d9 Code review fixes from #491
6c168bb Remove local allocation counting
3c6e7f0 Code review fixes from #478
bb97207 Rename Lambda.apply_position
a7cb650 Quieten Makefile when runtime dep files are not present
c656dc9 Merge flambda-backend changes
11b5424 Avoid printing double spaces in function argument lists
7751faa Restore locations to Typedtree.{pat,let}_bound_idents_full
e450b6c add build_ocaml_compiler.sexp
0403bb3 Revert PR 9895 to continue installing VERSION
b3447db Ensure new local attributes are namespaced properly
7f213fc Allow empty functions again
8f22ad8 Bugfix: ensure local domain state is initialised
80f54dd Bugfix for Selectgen with regions
e8133a1 Fix external-external signature inclusion
9840051 Bootstrap
d879f23 Merge remote-tracking branch 'jane/local-reviewed' into local-merge
94454f5 Use Local_store for the local allocations ref
54a164c Create fewer regions, according to typechecking (#59)
1c2479b Merge flambda-backend changes
ce34678 Fix printing of modes in return types
91f2281 Hook mode variable solving into Btype.snapshot/backtrack
54e4b09 Move Alloc_mode and Value_mode to Btype
ff4611e Merge flambda-backend changes
ce62e45 Ensure allocations are initialised, even dead ones
6b6ec5a Fix the alloc.ml test on 32-bit builds
81e9879 Merge flambda-backend changes
40a7f89 Update repo URL for ocaml-jst, and rename script.
0454ee7 Add some new locally-allocating primitives (#57)
8acdda1 Reset the local stack pointer in exception handlers (#56)
8dafa98 Improve typing for (||) and (&&) (#55)
8c64754 Fix make_check_all_arches (#54)
b50cd45 Allow arguments to primitives to be local even in tail position (#53)
cad125d Fix modes from or-patterns (#50)
4efdb72 Fix tailcalls tests with inlining (#52)
4a795cb Flambda support (#49)
74722cb Add [@ocaml.principal] and [@ocaml.noprincipal] attributes, and use in oo.mli
6d7d3b8 Ensure that functions are evaluated after their arguments (flambda-backend #353)
89bda6b Keep Sys.opaque_identity in Cmm and Mach (port upstream PR 9412)
a39126a Fix tailcalls within regions (#48)
4ac4cfd Fix stdlib manpages build
3a95f5e Merge flambda-backend changes
efe80c9 Add jane/pull-flambda-patches script
fca94c4 Register allocations for Omitted parameter closures (#47)
103b139 Remove various FIXMEs (#46)
62ba2c1 Bootstrap
a0062ad Allow local allocations for various primitives (#43)
7a2165e Allow primitives to be poly-moded (#43)
2af3f55 Fix a flaky test by refactoring TypePairs (ocaml/ocaml#10638)
58dd807 Bootstrap
ee3be10 Fix modes in build_apply for partial applications
fe73656 Tweak for evaluation order of labelled partial applications (#10653)
0527570 Fix caml_modify on local allocations (#40)
e657e99 Relax modes for `as` patterns (#42)
f815bf2 Add special mode handling for tuples in matches and let bindings (#38)
39f1211 Only take the upper bounds of modes associated with allocations (#37)
aec6fde Interpret arrow types in "local positions" differently
c4f3319 Bootstrap
ff6fdad Add some missing regions
40d586d Bootstrap
66d8110 Switch to a system with 3 modes for values
f2c5a85 Bugfix for Comballoc with local allocations. (#41)
83bcd09 Fix bug with root scanning during compaction (#39)
1b5ec83 Track modes in Lambda.lfunction and onwards (#33)
f1e2e97 Port ocaml/ocaml#10728
56703cd Port ocaml/ocaml#10081
eb66785 Support local allocations in i386 and fix amd64 bug (#31)
c936b19 Disallow local recursive non-functions (#30)
c7a193a GC support for local allocations (#29)
8dd7270 Nonlocal fields (#28)
e19a2f0 Bootstrap
694b9ac Add syntax to the parser for local allocations (#26)
f183008 Lower initial stack size
918226f Allow local closure allocations (#27)
2552e7d Introduce mode variables (#25)
bc41c99 Minor fixes for local allocations (#24)
a2a4e60 Runtime and compiler support for more local allocations (#23)
d030554 Typechecking for local allocations (#21)
9ee2332 Bugfix missing from #20
02c4cef Retain block-structured local regions until Mach.
86dbe1c amd64: Move stack realloc calls out-of-line
324d218 More typing modes and locking of environments
a4080b8 Initial version of local allocation (unsafe)

git-subtree-dir: ocaml
git-subtree-split: 173842c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants