Files
mercury/tests/invalid/bug496.m
Zoltan Somogyi 036b97ff7d Emit a reminder about a limitation of cse_detection when needed.
Common subexpression elimination (cse) declines to do its job of transforming

    (
        X = f(A1, ..., An),
        goal A
    ;
        X = f(B1, ..., Bn),
        goal B
    )

into

    X = f(X1, ..., Xn),
    (
        A1 = X1, ..., An = Xn,
        goal A
    ;
        B1 = X1, ..., Bn = Xn,
        goal B
    )

when the insts of some of X's arguments are at least partially unique,
because mode analysis cannot track uniqueness through the extra unifications
that this transformation introduces. When this happens, and the procedure
this code is in does not match its declared determinism, generate a message
that gives this fact as a possible reason for that determinism mismatch.

This fixes Mantis bug #496 to the extent that we *can* fix it
without rewriting the whole of mode analysis.

compiler/hlds_pred.m:
    Provide a slot in the proc_info for recording whether cse has declined
    to pull a common unification out of a branched control structure because
    of this concern, and if so, at what locations in the source code.

    An unrelated change: move a slot used only by constraint-based mode
    analysis, which is never enabled, from the proc_info to the proc_sub_info.
    This should improve both speed and memory consumption, though very
    slightly.

compiler/cse_detection.m:
    Record each such location in this slot.

    Doing this requires a change in approach. Previously, we did not try
    to pull a unification out of any branch of a branched control structure
    if it involved (partially or wholly) unique arguments. However, doing
    this in just one branch cannot possibly affect the output of cse detection,
    since it pulls a unification out of a branch only if can pull the same
    unification out of all the other branches as well.

    Our new approach is to transform branched control structures regardless
    of uniqueness, and then *undo* the transformation (simply by discarding
    its result) if it involves unique arguments. It is such undoing that we
    record in the proc_info.

compiler/switch_detection.m:
    Conform to the new approach.

compiler/hlds_out_pred.m:
    Print the contents of the new slot in HLDS dumps.

compiler/det_report.m:
    If the actual determinism of a procedure, as computed by determinism
    analysis, does not match its declared determinism, *and* if the proc_info's
    new slot says that cse declined to pull some common unifications
    out of a branched control structure, then mention that fact, and
    the usual fix, as a possible explanation of the determinism problem.

tests/invalid/bug496.{m,err_exp}:
    The Mantis test case.

tests/invalid/Mmakefile:
    Enable the new test case.
2020-02-07 11:56:54 +11:00

72 lines
1.8 KiB
Mathematica

%---------------------------------------------------------------------------%
% vim: ft=mercury ts=4 sw=4 et
%---------------------------------------------------------------------------%
% This module compiles successfully with Mercury 14.01.1 but using Mercury
% 20.01 results in a determinism error.
%---------------------------------------------------------------------------%
:- module bug496.
:- interface.
:- import_module bool.
:- type action
---> action_canonicalise
; action_split.
:- type maybe_action
---> action_ok(action)
; action_unspecified
; action_conflict(string).
:- pred options_to_action(bool::in, bool::in, maybe_action::out) is det.
:- implementation.
:- import_module list.
:- import_module string.
options_to_action(Canonicalise, SplitSolns, MaybeAction) :-
some [!Actions] (
!:Actions = [],
(
Canonicalise = yes,
!:Actions = [action_canonicalise | !.Actions]
;
Canonicalise = no
),
(
SplitSolns = yes,
!:Actions = [action_split | !.Actions]
;
SplitSolns = no
),
AllActions = !.Actions
),
(
AllActions = [],
MaybeAction = action_unspecified
;
AllActions = [Action],
MaybeAction = action_ok(Action)
;
AllActions = [_, _ | _],
MaybeAction = action_conflict("")
).
%%% This version works.
/*
(
AllActions = [],
MaybeAction = action_unspecified
;
AllActions = [Action | OtherActions],
(
OtherActions = [],
MaybeAction = action_ok(Action)
;
OtherActions = [_ | _],
MaybeAction = action_conflict("")
)
).
*/