[m-dev.] [m-users.] Tail recursion optimisation

Zoltan Somogyi zoltan.somogyi at runbox.com
Mon May 18 19:10:19 AEST 2026



On Mon, 18 May 2026 17:52:46 +1000 (AEST), "Zoltan Somogyi" <zoltan.somogyi at runbox.com> wrote:
> We could handle this with a third value
> on the new dimension, e.g. by splitting "in_tailrec_grades_only" into two, for which
> the descriptive but too-long named could be "in_tailrec_grades_only_with_intermod"
> and "in_tailrec_grades_only_all_in_one_module". The first would not warn about
> mutual tailrec between modules, the second would. Better and/or shorter names
> welcome.

Actually, scratch that idea. This should be controlled by a second new dimension,
which would control *just this*. The new dimension could have two values, say
"include_visible_intermod_procs" and its negation. The current behaviour is to include
opt_imported predicates in SCCs, i.e. include_visible_intermod_procs. This has the
bad property that if you have an SCC in which all mutually recursive calls are tail calls,
and then move one of the predicates to another module,

- you still get mutual tail recursion with the moved predicate with --intermod-opt,
- you do NOT get mutual tail recursion with --no-intermod-opt (which is fine),
  BUT you also do not get a warning about that loss.

I do not see a way to fix this issue as long as we report only warnings (or errors)
about missing tail recursion, because the compiler cannot warn about non-tail-recursion
if (in the absence of the code of the callee) it does not know the call is a mutually recursive call.

OTOH, with --intermod-opt, for a proc whose pragma specifies do_not_include_visible_intermod_procs,
we *could* add an informational message that tells users "predicate p3 from another module
would become part of your local SCC that now includes p1 and p2 if it were moved to this module,
and this (possibly empty) list of calls from (p1 or p2) to p3, or vice versa, would be/would not be
tail calls then".

This would of course be a separate feature, and if we added it, it would probably call for
a name such as "inform_intermod_recursion" instead of do_not_include_visible_intermod_procs.

Zoltan.




More information about the developers mailing list