24814 Commits

Author SHA1 Message Date
Zoltan Somogyi
48b81ba74d Make dumps of switch cases more readable. 2025-12-02 22:13:07 +11:00
Zoltan Somogyi
f9dcac624e Expand try goals inside lambda expressions.
compiler/try_expand.m:
    As above.

tests/valid/try_inside_lambda.m:
    A minimal version of Volker's test case, with English names.

tests/valid/Mmakefile:
    Enable the new test case.
2025-12-02 18:32:35 +11:00
Zoltan Somogyi
80e169717e Minor style improvements.
goal_expr_to_goal.m:
    Handle the simpler case before the more complex one.

superhomogeneous.m:
    Fix a comment.

try_expand.m:
    Fix comment rot.
2025-12-02 17:28:08 +11:00
Julien Fischer
07cc25b786 Add cc_multi versions of higher-order map predicates.
library/map.m:
library/tree234.m:
    Add cc_multi versions of some of the higher-order predicates in these
    modules.

    Re-order of mode declarations where they are inconsistent with our usual
    ordering.

NEWS.md:
    Announce the above additions.
2025-12-02 16:46:06 +11:00
Zoltan Somogyi
c199d1873b Fix comments and indentation. 2025-12-02 13:18:37 +11:00
Zoltan Somogyi
0aff352a12 Add a specific warning for "!V ^ fn = ...".
compiler/parse_goal.m:
    As above.

tests/invalid/field_syntax_error.{m,err_exp}:
    Extend this test case with a test of the new warning.
    Document its old as well as its new parts.
2025-11-30 03:38:12 +11:00
Zoltan Somogyi
8b98afbfeb Generalize some names. 2025-11-30 00:13:32 +11:00
Zoltan Somogyi
2550aebe3b Update obsolete variable names. 2025-11-29 23:34:40 +11:00
Zoltan Somogyi
f2ac8bc60a Merge --warn-interface-imports into --warn-unused-interface-imports.
compiler/options.m:
    Merge the two options into one, which can be specified by either name.

compiler/module_qual.m:
    Update their documentation.

compiler/module_qual.qualify_items.m:
    Conform to the change above.

tests/warnings/help_text.err_exp:
    Expect the merged option's documentation.

NEWS.md:
    Announce the new option name.

RELEASE_NOTES_NEXT:
    Schedule the old option name for deletion.

extras/base64/Mercury.options:
extras/fixed/Mercury.options:
extras/graphics/mercury_allegro/Mercury.options:
extras/graphics/mercury_cairo/Mercury.options:
extras/graphics/mercury_glfw/Mercury.options:
extras/graphics/mercury_glut/Mercury.options:
extras/graphics/mercury_glut/Mmakefile:
extras/graphics/mercury_glut/Mmakefile.MacOSX:
extras/graphics/mercury_opengl/Mercury.options:
extras/graphics/mercury_opengl/Mmakefile:
extras/graphics/mercury_opengl/Mmakefile.MacOSX:
extras/odbc/Mercury.options:
extras/odbc/Mmakefile:
extras/references/Mercury.options:
extras/solver_types/library/Mercury.options:
extras/solver_types/library/Mmakefile:
extras/trail/Mercury.options:
tests/submodules/Mercury.options:
tests/warnings/Mercury.options:
    Replace all references to --warn-interface-imports with
    --warn-unused-interface-imports, the new preferred name.

    In several places, compiler options (both module-specific
    and otherwise) were specified in Mmakefiles. Move them
    to Mercury.options files. In some cases, this meant creating
    new Mercury.options files.
2025-11-29 02:23:32 +11:00
Zoltan Somogyi
ae54908b7e Make --warn-unused-imports more effective.
Specifically, make it warn about interface imports that are not used
in the initial HLDS, even if they *are* used after the expansion of
type-, inst- and mode-equivalences.

compiler/hlds_module.m:
    Add a slot to the HLDS to store the set of modules that are
    imported in the interface but are unused there when the HLDS
    is first constructed.

compiler/module_qual.qualify_items.m:
    Compute this set, and return it to mercury_compile_make_hlds.m.

    Make the code module qualifying aug_compilation_units warn about
    unused interface imports only if unused_imports.m won't do the same later.

compiler/mercury_compile_make_hlds.m:
    Pass the set to make_hlds_passes.m.

compiler/make_hlds_passes.m:
    Store the set in the initial HLDS.

compiler/prog_data_used_modules.m:
    Replace set_ordlists with set_tree234s.

compiler/unused_imports.m:
    Consider an interface-imported module unused in the interface
    if module_qual.qualify_items.m considered it unused, even if
    changes made by equiv_type.m has added uses of it later.

compiler/handle_options.m:
    Stop making --warn-unused-imports imply --no-warn-unused-interface-imports,
    since new logic in module_qual.qualify_items.m makes this unnecessary.

compiler/make_module_file_names.m:
compiler/type_inst_mode_map.m:
compiler/write_deps_file.m:
    Move imports from the interface section to the implementation section,
    in response to the new, more thorough warnings.
2025-11-27 17:07:41 +11:00
Zoltan Somogyi
a239de1f5b Make --warn-unused-interface-imports report just one context. 2025-11-26 18:33:00 +11:00
Zoltan Somogyi
ea28d721c7 Fix a bad predicate name. 2025-11-26 15:58:42 +11:00
Zoltan Somogyi
ab297c220d Eliminate mostly-duplicate work in frameopt.m.
compiler/frameopt.m:
    The key decision in frame optimization is "which basic blocks need
    a stack frame"?

    One aspect of this decision is that if a block needs a frame,
    then all successors of that block will get a frame. We therefore
    unconditionally propagate the need for a frame forwards.

    Another aspect is that if all the successors of a block need a frame,
    then it is better for that block to allocate a frame than for
    all its successors to do so, since this saves space, and does not
    cost any time. We therefore *conditionally* propagate the need
    for a frame backwards.

    This diff speeds up conditional propagation to predecessors
    by processing each predecessor just once, which reduces the
    complexity of the algorithm from quadratic to linear. This speeds
    up the compilation of options.m by about 13%.

    This change can change the output slightly (because the result of the
    "do all successors of this predicate need a frame" test can change
    as the algorithm learns more about frames), but it seems that
    in practice, this happens only very rarely, and even then
    its impact on the performance of the generated code is negligible.

    The code doing propagation to successors already had a mechanism
    to handle each block just once. Speed up this mechanism also by
    switching the "have we done this block already" test from using
    plain sets (O(N) lookup) to using set_tree234s (O(log N) lookup).

    Also improve some minor aspects of programming style.
2025-11-26 14:33:23 +11:00
Zoltan Somogyi
b9839a97bc Put predicate groups into execution order. 2025-11-25 23:35:40 +11:00
Zoltan Somogyi
b811baed8e Move related predicates next to each other. 2025-11-25 21:28:09 +11:00
Zoltan Somogyi
589b5463b2 Use a bespoke type to replace bools. 2025-11-25 21:27:31 +11:00
Zoltan Somogyi
84f32da154 Delete some unneeded arguments. 2025-11-21 20:23:20 +11:00
Zoltan Somogyi
13d750ef2a Update copyright years. 2025-11-21 19:35:05 +11:00
Zoltan Somogyi
85538c95ec Reorder argument lists according to our style. 2025-11-21 19:35:05 +11:00
Zoltan Somogyi
f01b1878b8 Some simplifications. 2025-11-20 21:07:57 +11:00
Zoltan Somogyi
fe329d2c73 Replace a bool with a bespoke type. 2025-11-20 18:09:36 +11:00
Zoltan Somogyi
dd48b8826f Speed up var_locn.m a bit.
compiler/var_locn.m:
    Do not construct comments for instructions that we decide
    not to generate.

    Look up a variable in the var_table just once.

    Reduce pressure on stack frame sizes by reducing the number
    of live variables at some program points.

    Use string.format where appropriate.

    Use our standard programming style for setter predicates.

compiler/Mercury.options:
    Inline getters and setters in var_locn.m.
2025-11-20 15:38:36 +11:00
Zoltan Somogyi
2bbda5abfa Check for a var_table being empty directly.
This yields a speedup of about 2.4% when compiling options.m.

compiler/polymorphism_clause.m:
    Don't count the variables in a var_table when we need only
    an emptyness test.

library/tree234.m:
    Make the code counting map elements partially tail recursive.
2025-11-20 14:47:46 +11:00
Zoltan Somogyi
ee9c7d3a84 Speed up bound vs ground inst checks.
The code that checks whether a bound inst wrapped around
a list of bound_functors matched the ground inst did several things
in a suboptimal fashion.

- It looked up the definition of the type constructor of the relevant type
  (the type of the variable the inst is for) more than once. (This was
  not easily visible because the lookups were in different predicates.)
  This diff factors these out, not for the immesurably small speedup,
  but to make possible the fixes for the next two issues.

- To simplify the "is there a bound_functor for each constructor in the type"
  check, it sorted the constructors of the type by name and arity. (Lists of
  bound_functors are always sorted by name and arity.) Given that most
  modules contain more than one bound inst for any given type constructor,
  any sorting after the first was unnecessarily repeated work. This diff
  therefore extends the representation of du types, which until now has
  include only a list of the data constructors in the type definition
  in definition order, with a list of those exact same data constructors
  in name/arity order.

- Even if a list of bound_functors lists all the constructors of a type,
  the bound inst containing them is not equivalent to ground if the inst
  of some argument of some bound_inst is not equivalent to ground.
  This means that we need to know the actual argument of each constructor.
  The du type definition lists argument types that refer to the type
  constructor's type parameters; we need the instances of these argument types
  that apply to type of the variable at hand, which usually binds concrete
  types to those type parameters.

  We used to apply the type-parameter-to-actual-type substitution to
  each argument of each data constructor in the type before we compared
  the resulting filled-in data constructor descriptions against the list of
  bound_functors. However, in cases where the comparison fails, the
  substitution applications to arguments beyond the point of failure
  are all wasted work. This diff therefore applies the substitution
  only when its result is about to be needed.

This diff leads to a speedup of about 3.5% on tools/speedtest,
and about 38% (yes, more than a third) when compiling options.m.

compiler/hlds_data.m:
    Add the new field to the representation of du types.

    Add a utility predicate that helps construct that field, since it is
    now needed by two modules (add_type.m and equiv_type_hlds.m).

    Delete two functions that were used only by det_check_switch.m,
    which this diff moves to that module (in modified form).

compiler/inst_match.m:
    Implement the first and third changes listed above, and take advantage
    of the second.

    The old call to all_du_ctor_arg_types, which this diff replaces,
    effectively lied about the list of constructors it returned,
    by simply not returning any constructors containing existentially
    quantified  types, on the grounds that they "were not handled yet".
    We now fail explicitly when we find any such constructors.

    Perform the check for one-to-one match between bound_functors and
    constructors with less argument passing.

compiler/det_check_switch.m:
    Move the code deleted from hlds_data.m here, and simplify it,
    taking advantage of the new field in du types.

compiler/Mercury.options:
    Specify --optimize-constructor-last-call for det_check_switch.m
    to optimize the updated moved code.

compiler/add_foreign_enum.m:
compiler/add_special_pred.m:
compiler/add_type.m:
compiler/check_typeclass.m:
compiler/code_info.m:
compiler/dead_proc_elim.m:
compiler/direct_arg_in_out.m:
compiler/du_type_layout.m:
compiler/equiv_type_hlds.m:
compiler/hlds_out_type_table.m:
compiler/inst_check.m:
compiler/intermod.m:
compiler/intermod_decide.m:
compiler/lookup_switch_util.m:
compiler/ml_type_gen.m:
compiler/ml_unify_gen_test.m:
compiler/ml_unify_gen_util.m:
compiler/mlds.m:
compiler/post_term_analysis.m:
compiler/recompilation.usage.m:
compiler/resolve_unify_functor.m:
compiler/simplify_goal_ite.m:
compiler/table_gen.m:
compiler/tag_switch_util.m:
compiler/term_norm.m:
compiler/type_ctor_info.m:
compiler/type_util.m:
compiler/typecheck_coerce.m:
compiler/unify_proc.m:
compiler/unused_imports.m:
compiler/xml_documentation.m:
    Conform to the changes above. This mostly means handling
    the new field in du types (usually by ignoring it).
2025-11-19 22:09:04 +11:00
Zoltan Somogyi
6aa56eb555 Simplify the lookup of a type's data constructors.
compiler/type_util.m:
    Look up a type constructor's data constructors in that type constructor's
    definition, not in the cons table.
2025-11-19 11:37:46 +11:00
Zoltan Somogyi
36f020bd1d Encode an invariant in insts.
compiler/inst_match.m:
    The data structure that we pass through most of the code of this module
    can be set up to either gather and record substitions, or not.
    After initialization with one of these choices, the choice stayed fixed,
    with one exception, which turned on gathering substitions. However,
    this exception had no effect other than generating a slow-down, because
    every predicate that initialized this field to "don't gather substitions"
    also ignored any substitions that this turning caused to be gathered.

    This diff undoes that exception. It also changes every predicate
    that this data structure passes through to have two modes,
    one for each choice.

    Replace a call to set.fold with a new predicate that does the same job,
    but preserves the more-specific-than-ground insts. Do not call it
    in cases where every iteration would be a noop.

    This diff yields a speedup of about 0.6% on tools/speedtest, and
    very roughly three times that when compiling options.m.
2025-11-19 09:08:59 +11:00
Zoltan Somogyi
9baadb3d88 Style improvements in browser and deep_profiler.
deep_profiler/var_use_analysis.m:
    Pass never-varying and slower-varying input arguments first.

    Consistently pass goal lists before the position of the first goal
    in the overall list.

browser/declarative_analyser.m:
browser/declarative_edt.m:
deep_profiler/analysis_utils.m:
deep_profiler/autopar_reports.m:
deep_profiler/autopar_types.m:
deep_profiler/program_representation_utils.m:
deep_profiler/read_profile.m:
deep_profiler/recursion_patterns.m:
deep_profiler/timeout.m:
deep_profiler/top_procs.m:
deep_profiler/util.m:
    Minor improvements in programming style.
2025-11-18 16:11:20 +11:00
Zoltan Somogyi
d5ed3079de Carve type_inst_mode_map.m out of convert_parse_tree.m.
compiler/convert_parse_tree.m:
compiler/type_inst_mode_map.m:
    As above.

compiler/parse_tree.m:
compiler/notes/compiler_design.html:
    Add the new module to the parse_tree package, and document it.

compiler/comp_unit_interface.m:
compiler/recompilation.version.m:
    Conform to the changes above.
2025-11-18 09:28:14 +11:00
Zoltan Somogyi
18f9fc3f2d Delete now-redundant blocks of unifications ...
compiler/print_help.m:
    ... that used to be required to help out switch detection.

configure.ac:
    Require the installed compiler to have the commit that makes those
    duplicate unifications unnecessary.
2025-11-17 18:47:02 +11:00
Zoltan Somogyi
fb69294bc7 Put entries into lexicographic order. 2025-11-17 11:30:38 +11:00
Zoltan Somogyi
665ea9ef93 Break up a large predicate. 2025-11-17 09:19:13 +11:00
Zoltan Somogyi
a0c8e78edc Replace clauses with explicit disjunctions ...
... and inline some predicates at their only call sites.
2025-11-16 21:04:02 +11:00
Zoltan Somogyi
38995e52e1 Simplify some code. 2025-11-16 19:31:25 +11:00
Zoltan Somogyi
7597f24121 Break up two large predicates. 2025-11-16 19:30:04 +11:00
Zoltan Somogyi
82da94f071 Break up three large predicates. 2025-11-16 18:05:59 +11:00
Zoltan Somogyi
653f8d1c28 Fix typos. 2025-11-16 15:31:44 +11:00
Zoltan Somogyi
d5c08567e4 Make format after switch work.
compiler/simplify_proc.m:
    If a first attempt to analyse and optimize format calls fails
    due to insufficient information about format strings and/or values,
    then try the whole process again after pushing copies of the format calls,
    and the conjuncts that precede them, into the last preceding branched
    control structure (disjunction, switch, or if-then-else). This will
    fix the problem if each branch does construct known format strings
    and/or values, and is harmless if this is not the case.

tests/valid/format_after_switch.m:
    A test case for the new capability.

tests/valid/Mmakefile:
tests/valid/Mercury.options:
    Enable the new test case.
2025-11-16 13:42:36 +11:00
Zoltan Somogyi
bdd2a574a8 Replace conditional_specs with a new severity.
compiler/error_spec.m:
    Delete conditional_spec from the error_spec type, which had
    exactly one use left in the compiler. Replace it with a conditional form
    of severity_error, which handles that use case.

    Rename error_severity to spec_severity, since obviously not all severities
    represent errors.

    For the same reason, rename error_phase to spec_phase.

compiler/error_util.m:
    Export a predicate for write_error_spec.m.

compiler/write_error_spec.m:
    Use that export to delete what used to be duplicate code.

compiler/add_pragma.m:
compiler/check_typeclass.m:
compiler/compiler_util.m:
compiler/error_sort.m:
compiler/mark_tail_calls.m:
compiler/parse_error.m:
compiler/parse_item.m:
    Conform to the changes above.
2025-11-15 10:29:23 +11:00
Zoltan Somogyi
c6ab9b02c1 Break up a predicate. 2025-11-15 07:09:03 +11:00
Zoltan Somogyi
c9e549996a Let switch detection handle deeper disjunctions.
compiler/switch_detection.m:
    The existing switch detection algorithm does a single forward
    traversal of the procedure body, doing only very limited lookahead.
    This prevents it from recognizing some switches. To fix this, add
    a new prepass that provides unlimited lookahead.

    Add one use of this lookahead information. Later diffs should add
    more uses.

compiler/print_help.m:
    Fix the code that served as motivation for this change.
    It was a block of unifications that was intended to supply
    the lookahead that the old algorithm needed but did not have,
    but it actually lied: it said that the the following disjunction
    covered cons_ids that it actually did not cover. The fact that
    the compiler did not diagnose this lie until now was a bug.
    (After this diff, the compiler now reports an error.)

deep_profiler/message.m:
    Avoid the limitation of the new algorithm that was discussed today
    on m-dev.

compiler/options.m:
    Add a way to test for the presence of this new capability
    in the installed compiler.

tests/warnings/help_text.err_exp:
    Expect the new option name.
2025-11-15 05:08:29 +11:00
Zoltan Somogyi
87d04f3428 Add get_values_for_key/2 to multi_map and one_or_more_map.
library/multi_map.m:
library/one_or_more_map.m:
    As above.

NEWS.md:
    Announce the additions.
2025-11-14 12:55:43 +11:00
Zoltan Somogyi
f4b3329e4e Put predicates into top-down order. 2025-11-13 15:10:28 +11:00
Zoltan Somogyi
1a5568fbfd Put switch arms in type declaration order. 2025-11-13 14:30:26 +11:00
Julien Fischer
91cfdc5978 Fix documentation copy-and-paste errors.
library/multi_map.m:
library/one_or_more_map.m:
    As above.
2025-11-11 10:34:21 +11:00
Zoltan Somogyi
9b7bce9c1c Add history to a module comment. 2025-11-07 10:46:09 +11:00
Zoltan Somogyi
cf37b1bd53 Fix typos. 2025-11-07 10:45:20 +11:00
Julien Fischer
88b7a207a2 Fix markup in NEWS file.
NEWS.md:
    As above.
2025-11-06 11:07:26 +11:00
Julien Fischer
499d193580 Fix a typo.
compiler/state_vars.m:
    s/errrors/errors/
2025-11-05 21:41:27 +11:00
Zoltan Somogyi
9c850f3557 Replace clauses with explicit disjunction. 2025-11-05 16:02:47 +11:00
Zoltan Somogyi
6e41bcd9c0 Document a function symbol. 2025-11-05 16:02:15 +11:00