[m-rev.] diff: fix bug in mode analysis wrt quantification

Zoltan Somogyi zs at cs.mu.OZ.AU
Fri Oct 7 14:33:17 AEST 2005


For review by Ralph.

Zoltan.

Fix a bug I detected and Ralph tracked down. It was the result of an
interaction of the state variable transformation, mode analysis and
(lack of) quantification.

Sometimes, the final value of a state variable introduced with "some [!X]"
notation is not used along some paths of computation. In some of those cases,
a second pass of mode checking (usually unique mode checking) could delete the
unification building the final version of the state variable in one branch
of e.g. a switch while not being able to delete a procedure call building
that same final version transformation in another branch. Later compiler passes
then get errors because the branches bind different sets of variables.

The fix is to rerun quantification when this happens, to delete the final
version of the state variable from the switch's nonlocal set and thus from
the nonlocal set of any non-eliminated goals that bind it.

compiler/mode_info.m:
	Add a field to the mode info that records the need for a
	requantification.

compiler/modecheck_unify.m:
	When eliminating a unification, set the flag if the unification was
	inserted by the state variable transformation and may contain a
	singleton variable (such as the final version of the variable in the
	example above).

compiler/modes.m:
	Requantify the procedure at the end of mode analysis if the flag
	is set.

tests/valid/state_var_mode_bug.m:
	New test case, a very cut down and abstracted version of the original
	code the bug was detected in.

tests/valid/Mmakefile:
	Enable the new test case.

cvs diff: Diffing .
cvs diff: Diffing analysis
cvs diff: Diffing bindist
cvs diff: Diffing boehm_gc
cvs diff: Diffing boehm_gc/Mac_files
cvs diff: Diffing boehm_gc/cord
cvs diff: Diffing boehm_gc/cord/private
cvs diff: Diffing boehm_gc/doc
cvs diff: Diffing boehm_gc/include
cvs diff: Diffing boehm_gc/include/private
cvs diff: Diffing boehm_gc/tests
cvs diff: Diffing browser
cvs diff: Diffing bytecode
cvs diff: Diffing compiler
Index: compiler/mode_info.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/mode_info.m,v
retrieving revision 1.74
diff -u -b -r1.74 mode_info.m
--- compiler/mode_info.m	30 Sep 2005 08:08:27 -0000	1.74
+++ compiler/mode_info.m	6 Oct 2005 06:09:20 -0000
@@ -121,6 +121,7 @@
 :- pred mode_info_get_errors(mode_info::in, list(mode_error_info)::out) is det.
 :- pred mode_info_get_warnings(mode_info::in, list(mode_warning_info)::out)
     is det.
+:- pred mode_info_get_need_to_requantify(mode_info::in, bool::out) is det.
 :- pred mode_info_get_num_errors(mode_info::in, int::out) is det.
 :- pred mode_info_get_liveness(mode_info::in, set(prog_var)::out) is det.
 :- pred mode_info_get_varset(mode_info::in, prog_varset::out) is det.
@@ -168,6 +169,8 @@
     mode_info::in, mode_info::out) is det.
 :- pred mode_info_set_warnings(list(mode_warning_info)::in,
     mode_info::in, mode_info::out) is det.
+:- pred mode_info_set_need_to_requantify(bool::in,
+    mode_info::in, mode_info::out) is det.
 :- pred mode_info_add_live_vars(set(prog_var)::in,
     mode_info::in, mode_info::out) is det.
 :- pred mode_info_remove_live_vars(set(prog_var)::in,
@@ -240,6 +243,8 @@
 :- pred mode_info_add_warning(mode_warning_info::in,
     mode_info::in, mode_info::out) is det.
 
+:- pred mode_info_need_to_requantify(mode_info::in, mode_info::out) is det.
+
 %-----------------------------------------------------------------------------%
 
     % The mode_info contains a flag indicating whether initialisation calls,
@@ -309,7 +314,11 @@
                 initial_instmap         :: instmap,
 
                 % The mode warnings found.
-                warnings                :: list(mode_warning_info)
+                warnings                :: list(mode_warning_info),
+
+                % Set to `yes' if we need to requantify the procedure body
+                % after mode analysis finishes.
+                need_to_requantify      :: bool
             ).
 
 :- type mode_info
@@ -433,9 +442,10 @@
     Changed = no,
     CheckingExtraGoals = no,
     MayInitSolverVars = yes,
+    NeedToRequantify = no,
 
     ModeSubInfo = mode_sub_info(ProcId, VarSet, Unreachable, Changed,
-        CheckingExtraGoals, InstMapping0, WarningList),
+        CheckingExtraGoals, InstMapping0, WarningList, NeedToRequantify),
 
     ModeInfo = mode_info(ModuleInfo, PredId, VarTypes, Debug,
         Context, ModeContext, InstMapping0, LockedVars, DelayInfo,
@@ -457,6 +467,7 @@
 mode_info_get_locked_vars(MI, MI ^ locked_vars).
 mode_info_get_errors(MI, MI ^ errors).
 mode_info_get_warnings(MI, MI ^ mode_sub_info ^ warnings).
+mode_info_get_need_to_requantify(MI, MI ^ mode_sub_info ^ need_to_requantify).
 mode_info_get_delay_info(MI, MI ^ delay_info).
 mode_info_get_live_vars(MI, MI ^ live_vars).
 mode_info_get_nondet_live_vars(MI, MI ^ nondet_live_vars).
@@ -479,6 +490,8 @@
 mode_info_set_errors(Errors, MI, MI ^ errors := Errors).
 mode_info_set_warnings(Warnings, MI,
     MI ^ mode_sub_info ^ warnings := Warnings).
+mode_info_set_need_to_requantify(NTRQ, MI,
+    MI ^ mode_sub_info ^ need_to_requantify := NTRQ).
 mode_info_set_delay_info(DelayInfo, MI, MI ^ delay_info := DelayInfo).
 mode_info_set_live_vars(LiveVarsList, MI, MI ^ live_vars := LiveVarsList).
 mode_info_set_nondet_live_vars(NondetLiveVars, MI,
@@ -699,6 +712,9 @@
     mode_info_get_warnings(!.ModeInfo, Warnings0),
     list__append(Warnings0, [ModeWarningInfo], Warnings),
     mode_info_set_warnings(Warnings, !ModeInfo).
+
+mode_info_need_to_requantify(!ModeInfo) :-
+    mode_info_set_need_to_requantify(yes, !ModeInfo).
 
 %-----------------------------------------------------------------------------%
 
Index: compiler/modecheck_unify.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/modecheck_unify.m,v
retrieving revision 1.88
diff -u -b -r1.88 modecheck_unify.m
--- compiler/modecheck_unify.m	5 Oct 2005 06:33:47 -0000	1.88
+++ compiler/modecheck_unify.m	6 Oct 2005 06:09:20 -0000
@@ -88,8 +88,8 @@
 
 %-----------------------------------------------------------------------------%
 
-modecheck_unification(X, var(Y), Unification0, UnifyContext,
-        UnifyGoalInfo0, Unify, !ModeInfo, !IO) :-
+modecheck_unification(X, var(Y), Unification0, UnifyContext, UnifyGoalInfo0,
+        Unify, !ModeInfo, !IO) :-
     mode_info_get_module_info(!.ModeInfo, ModuleInfo0),
     mode_info_get_var_types(!.ModeInfo, VarTypes),
     mode_info_get_instmap(!.ModeInfo, InstMap0),
@@ -149,7 +149,8 @@
         ModeOfX = (InstOfX -> Inst),
         ModeOfY = (InstOfY -> Inst),
         categorize_unify_var_var(ModeOfX, ModeOfY, LiveX, LiveY, X, Y,
-            Det, UnifyContext, VarTypes, Unification0, Unify0, !ModeInfo),
+            Det, UnifyContext, UnifyGoalInfo0, VarTypes, Unification0, Unify0,
+            !ModeInfo),
         (
             MaybeInitX = no,
             Unify = Unify0
@@ -834,12 +835,12 @@
     %
 :- pred categorize_unify_var_var((mode)::in, (mode)::in,
     is_live::in, is_live::in, prog_var::in,
-    prog_var::in, determinism::in, unify_context::in,
+    prog_var::in, determinism::in, unify_context::in, hlds_goal_info::in,
     map(prog_var, type)::in, unification::in, hlds_goal_expr::out,
     mode_info::in, mode_info::out) is det.
 
 categorize_unify_var_var(ModeOfX, ModeOfY, LiveX, LiveY, X, Y, Det,
-        UnifyContext, VarTypes, Unification0, Unify, !ModeInfo) :-
+        UnifyContext, GoalInfo, VarTypes, Unification0, Unify, !ModeInfo) :-
     mode_info_get_module_info(!.ModeInfo, ModuleInfo0),
     (
         mode_is_output(ModuleInfo0, ModeOfX)
@@ -889,7 +890,7 @@
                 UnifyContext, Unification0, Unification, !ModeInfo)
         )
     ),
-    %
+
     % Optimize away unifications with dead variables and simple tests that
     % cannot fail by replacing them with `true'. (The optimization of simple
     % tests is necessary because otherwise determinism analysis assumes they
@@ -898,17 +899,18 @@
     %
     % Optimize away unifications which always fail by replacing them with
     % `fail'.
-    %
     (
         Unification = assign(AssignTarget, _),
         mode_info_var_is_live(!.ModeInfo, AssignTarget, dead)
     ->
-        Unify = conj([])
+        Unify = conj([]),
+        record_optimize_away(GoalInfo, !ModeInfo)
     ;
         Unification = simple_test(_, _),
         Det = det
     ->
-        Unify = conj([])
+        Unify = conj([]),
+        record_optimize_away(GoalInfo, !ModeInfo)
     ;
         Det = failure
     ->
@@ -935,6 +937,16 @@
         )
     ;
         Unify = unify(X, var(Y), ModeOfX - ModeOfY, Unification, UnifyContext)
+    ).
+
+:- pred record_optimize_away(hlds_goal_info::in,
+    mode_info::in, mode_info::out) is det.
+
+record_optimize_away(GoalInfo, !ModeInfo) :-
+    ( goal_info_has_feature(GoalInfo, dont_warn_singleton) ->
+        mode_info_need_to_requantify(!ModeInfo)
+    ;
+        true
     ).
 
     % Modecheck_complicated_unify does some extra checks that are needed
Index: compiler/modes.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/modes.m,v
retrieving revision 1.315
diff -u -b -r1.315 modes.m
--- compiler/modes.m	5 Oct 2005 06:33:48 -0000	1.315
+++ compiler/modes.m	6 Oct 2005 06:09:20 -0000
@@ -361,6 +361,7 @@
 :- import_module hlds__instmap.
 :- import_module hlds__make_hlds.
 :- import_module hlds__passes_aux.
+:- import_module hlds__quantification.
 :- import_module hlds__special_pred.
 :- import_module libs__globals.
 :- import_module libs__options.
@@ -943,10 +944,17 @@
         % VarTypes may be the same as VarTypes0, since mode checking can
         % add new variables (e.g. when handling calls in implied modes).
         mode_info_get_var_types(!.ModeInfo, VarTypes),
+        mode_info_get_need_to_requantify(!.ModeInfo, NeedToRequantify),
         proc_info_set_goal(Body, !ProcInfo),
         proc_info_set_varset(VarSet, !ProcInfo),
         proc_info_set_vartypes(VarTypes, !ProcInfo),
-        proc_info_set_argmodes(ArgModes, !ProcInfo)
+        proc_info_set_argmodes(ArgModes, !ProcInfo),
+        (
+            NeedToRequantify = no
+        ;
+            NeedToRequantify = yes,
+            requantify_proc(!ProcInfo)
+        )
     ).
 
 %-----------------------------------------------------------------------------%
cvs diff: Diffing compiler/notes
cvs diff: Diffing debian
cvs diff: Diffing debian/patches
cvs diff: Diffing deep_profiler
cvs diff: Diffing deep_profiler/notes
cvs diff: Diffing doc
cvs diff: Diffing extras
cvs diff: Diffing extras/aditi
cvs diff: Diffing extras/cgi
cvs diff: Diffing extras/complex_numbers
cvs diff: Diffing extras/complex_numbers/samples
cvs diff: Diffing extras/complex_numbers/tests
cvs diff: Diffing extras/concurrency
cvs diff: Diffing extras/curs
cvs diff: Diffing extras/curs/samples
cvs diff: Diffing extras/curses
cvs diff: Diffing extras/curses/sample
cvs diff: Diffing extras/dynamic_linking
cvs diff: Diffing extras/error
cvs diff: Diffing extras/graphics
cvs diff: Diffing extras/graphics/easyx
cvs diff: Diffing extras/graphics/easyx/samples
cvs diff: Diffing extras/graphics/mercury_glut
cvs diff: Diffing extras/graphics/mercury_opengl
cvs diff: Diffing extras/graphics/mercury_tcltk
cvs diff: Diffing extras/graphics/samples
cvs diff: Diffing extras/graphics/samples/calc
cvs diff: Diffing extras/graphics/samples/gears
cvs diff: Diffing extras/graphics/samples/maze
cvs diff: Diffing extras/graphics/samples/pent
cvs diff: Diffing extras/lazy_evaluation
cvs diff: Diffing extras/lex
cvs diff: Diffing extras/lex/samples
cvs diff: Diffing extras/lex/tests
cvs diff: Diffing extras/logged_output
cvs diff: Diffing extras/moose
cvs diff: Diffing extras/moose/samples
cvs diff: Diffing extras/moose/tests
cvs diff: Diffing extras/morphine
cvs diff: Diffing extras/morphine/non-regression-tests
cvs diff: Diffing extras/morphine/scripts
cvs diff: Diffing extras/morphine/source
cvs diff: Diffing extras/odbc
cvs diff: Diffing extras/posix
cvs diff: Diffing extras/quickcheck
cvs diff: Diffing extras/quickcheck/tutes
cvs diff: Diffing extras/references
cvs diff: Diffing extras/references/samples
cvs diff: Diffing extras/references/tests
cvs diff: Diffing extras/solver_types
cvs diff: Diffing extras/solver_types/library
cvs diff: Diffing extras/stream
cvs diff: Diffing extras/trailed_update
cvs diff: Diffing extras/trailed_update/samples
cvs diff: Diffing extras/trailed_update/tests
cvs diff: Diffing extras/xml
cvs diff: Diffing extras/xml/samples
cvs diff: Diffing extras/xml_stylesheets
cvs diff: Diffing java
cvs diff: Diffing java/runtime
cvs diff: Diffing library
cvs diff: Diffing mdbcomp
cvs diff: Diffing profiler
cvs diff: Diffing robdd
cvs diff: Diffing runtime
cvs diff: Diffing runtime/GETOPT
cvs diff: Diffing runtime/machdeps
cvs diff: Diffing samples
cvs diff: Diffing samples/c_interface
cvs diff: Diffing samples/c_interface/c_calls_mercury
cvs diff: Diffing samples/c_interface/cplusplus_calls_mercury
cvs diff: Diffing samples/c_interface/mercury_calls_c
cvs diff: Diffing samples/c_interface/mercury_calls_cplusplus
cvs diff: Diffing samples/c_interface/mercury_calls_fortran
cvs diff: Diffing samples/c_interface/simpler_c_calls_mercury
cvs diff: Diffing samples/c_interface/simpler_cplusplus_calls_mercury
cvs diff: Diffing samples/diff
cvs diff: Diffing samples/muz
cvs diff: Diffing samples/rot13
cvs diff: Diffing samples/solutions
cvs diff: Diffing samples/tests
cvs diff: Diffing samples/tests/c_interface
cvs diff: Diffing samples/tests/c_interface/c_calls_mercury
cvs diff: Diffing samples/tests/c_interface/cplusplus_calls_mercury
cvs diff: Diffing samples/tests/c_interface/mercury_calls_c
cvs diff: Diffing samples/tests/c_interface/mercury_calls_cplusplus
cvs diff: Diffing samples/tests/c_interface/mercury_calls_fortran
cvs diff: Diffing samples/tests/c_interface/simpler_c_calls_mercury
cvs diff: Diffing samples/tests/c_interface/simpler_cplusplus_calls_mercury
cvs diff: Diffing samples/tests/diff
cvs diff: Diffing samples/tests/muz
cvs diff: Diffing samples/tests/rot13
cvs diff: Diffing samples/tests/solutions
cvs diff: Diffing samples/tests/toplevel
cvs diff: Diffing scripts
cvs diff: Diffing slice
cvs diff: Diffing tests
cvs diff: Diffing tests/benchmarks
cvs diff: Diffing tests/debugger
cvs diff: Diffing tests/debugger/declarative
cvs diff: Diffing tests/dppd
cvs diff: Diffing tests/general
cvs diff: Diffing tests/general/accumulator
cvs diff: Diffing tests/general/string_format
cvs diff: Diffing tests/general/structure_reuse
cvs diff: Diffing tests/grade_subdirs
cvs diff: Diffing tests/hard_coded
cvs diff: Diffing tests/hard_coded/exceptions
cvs diff: Diffing tests/hard_coded/purity
cvs diff: Diffing tests/hard_coded/sub-modules
cvs diff: Diffing tests/hard_coded/typeclasses
cvs diff: Diffing tests/invalid
cvs diff: Diffing tests/invalid/purity
cvs diff: Diffing tests/misc_tests
cvs diff: Diffing tests/mmc_make
cvs diff: Diffing tests/mmc_make/lib
cvs diff: Diffing tests/recompilation
cvs diff: Diffing tests/tabling
cvs diff: Diffing tests/term
cvs diff: Diffing tests/valid
Index: tests/valid/Mmakefile
===================================================================
RCS file: /home/mercury/mercury1/repository/tests/valid/Mmakefile,v
retrieving revision 1.157
diff -u -b -r1.157 Mmakefile
--- tests/valid/Mmakefile	16 Aug 2005 05:17:07 -0000	1.157
+++ tests/valid/Mmakefile	6 Oct 2005 03:15:48 -0000
@@ -186,6 +186,7 @@
 	stack_alloc \
 	stack_opt_simplify \
 	state_var_bug \
+	state_var_mode_bug \
 	static \
 	subtype_switch \
 	switch_detection_bug \
Index: tests/valid/state_var_mode_bug.m
===================================================================
RCS file: tests/valid/state_var_mode_bug.m
diff -N tests/valid/state_var_mode_bug.m
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ tests/valid/state_var_mode_bug.m	7 Oct 2005 04:18:52 -0000
@@ -0,0 +1,97 @@
+%-----------------------------------------------------------------------------%
+% vim: ft=mercury ts=4 sw=4 et
+%-----------------------------------------------------------------------------%
+%
+% Author: Ralph Becket <rafe at cs.mu.oz.au>
+%
+% This is a regression test for a bug involving the interaction of the mode
+% system and quantification, triggered by the state variable transformation.
+%
+%-----------------------------------------------------------------------------%
+
+:- module state_var_mode_bug.
+
+:- interface.
+
+:- import_module bool, int.
+
+:- pred p(bool::in, bool::in, int::in, int::out) is semidet.
+
+:- implementation.
+
+p(A, B, !Y) :-
+    some [!X] (
+        p0(!:X),
+        p2(!X),
+        p2(!X),
+        (
+            A = yes
+        ->
+            (
+                B = yes,
+                p1(!.X),
+                p2(!X),
+                p1(!.X)
+            ;
+                B = no,
+                (
+                    p1(!.X),
+                    p2(!X)
+                ->
+                    p1(!.X)
+                ;
+                    true
+                )
+            )
+        ;
+            (
+                p1(!.X),
+                p2(!X)
+            ->
+                (
+                    A = yes,
+                    p1(!.X),
+                    p2(!X),
+                    % The bug occurs here. The final value of !X is defined
+                    % in both branches of this switch, but this final value
+                    % is not used. The unique modes can therefore optimize away
+                    % the copy unification in the "no" branch, but cannot
+                    % optimize away the binding of the variable representing
+                    % the value in the call to p2 in the "yes" branch. Later
+                    % compiler passes that check whether the two branches bind
+                    % the same set of variables in the process of recomputing
+                    % instmap delta then find that they don't, leading to a
+                    % compiler abort.
+                    (
+                        B = yes,
+                        p2(!X)
+                    ;
+                        B = no
+                    )
+                ;
+                    A = no
+                )
+            ;
+                p1(!.X)
+            ->
+                p1(!.X),
+                p2(!X),
+                p1(!.X)
+            ;
+                true
+            )
+        )
+    ).
+
+:- pred p0(int::out) is det.
+
+p0(0).
+
+:- pred p1(int::in) is semidet.
+
+p1(1).
+
+:- pred p2(int::in, int::out) is semidet.
+
+p2(!X) :-
+    p1(!.X).
cvs diff: Diffing tests/warnings
cvs diff: Diffing tools
cvs diff: Diffing trace
cvs diff: Diffing util
cvs diff: Diffing vim
cvs diff: Diffing vim/after
cvs diff: Diffing vim/ftplugin
cvs diff: Diffing vim/syntax
--------------------------------------------------------------------------
mercury-reviews mailing list
post:  mercury-reviews at cs.mu.oz.au
administrative address: owner-mercury-reviews at cs.mu.oz.au
unsubscribe: Address: mercury-reviews-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-reviews-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the reviews mailing list