[m-rev.] for post-commit review: try goals and dead proc elimination

Peter Wang novalazy at gmail.com
Wed Mar 11 17:33:58 AEDT 2009


Branches: main

Dead proc elimination was expecting all `shorthand' goals to have been
expanded out by the time it is run.  However, when intermodule optimisation is
enabled, dead proc elimination is run early on and would abort on try and
atomic goals (which are treated as shorthands).

compiler/dead_proc_elim.m:
	Make pre_modecheck_examine_goal_expr look in the sub-goals of try and
	atomic goals rather than aborting.

	Don't eliminate predicates that try-goal expansion may introduce calls
	to.

compiler/try_expand.m:
	List predicates that dead proc elimination should not eliminate.
	(A similar thing would be required for the stm transformation.)

diff --git a/compiler/dead_proc_elim.m b/compiler/dead_proc_elim.m
index 926fdbb..54b1119 100644
--- a/compiler/dead_proc_elim.m
+++ b/compiler/dead_proc_elim.m
@@ -77,6 +77,7 @@
 :- implementation.

 :- import_module check_hlds.simplify.
+:- import_module check_hlds.try_expand.
 :- import_module hlds.hlds_clauses.
 :- import_module hlds.hlds_data.
 :- import_module hlds.hlds_error_util.
@@ -972,6 +973,14 @@ dead_pred_elim_initialize(PredId, DeadInfo0, DeadInfo) :-
                 simplify_may_introduce_calls(PredModuleName, PredName,
                     PredArity)
             ;
+                % Try-goal expansion may introduce calls to predicates in
+                % `exception'.
+                % XXX it should actually be calling predicates in a new
+                % exception_builtin module, which would obviate the need for
+                % this check
+                PredModule = mercury_exception_module,
+                try_expand_may_introduce_calls(PredName, PredArity)
+            ;
                 % Don't attempt to eliminate local preds here, since we want
                 % to do semantic checking on those even if they aren't used.
                 \+ pred_info_is_imported(PredInfo),
@@ -1070,10 +1079,21 @@
pre_modecheck_examine_goal_expr(call_foreign_proc(_, _, _, _, _, _,
_),
         !DeadInfo).
 pre_modecheck_examine_goal_expr(unify(_, Rhs, _, _, _), !DeadInfo) :-
     pre_modecheck_examine_unify_rhs(Rhs, !DeadInfo).
-pre_modecheck_examine_goal_expr(shorthand(_), !DeadInfo) :-
-    % These should have been expanded out by now.
-    unexpected(this_file,
-        "pre_modecheck_examine_goal_expr: unexpected shorthand").
+pre_modecheck_examine_goal_expr(shorthand(ShortHand), !DeadInfo) :-
+    (
+        ShortHand = atomic_goal(_GoalType, _Outer, _Inner, _MaybeOutputVars,
+            MainGoal, OrElseGoals, _OrElseInners),
+        pre_modecheck_examine_goal(MainGoal, !DeadInfo),
+        list.foldl(pre_modecheck_examine_goal, OrElseGoals, !DeadInfo)
+    ;
+        ShortHand = try_goal(_MaybeIO, _ResultVar, SubGoal),
+        pre_modecheck_examine_goal(SubGoal, !DeadInfo)
+    ;
+        ShortHand = bi_implication(_, _),
+        % These should have been expanded out by now.
+        unexpected(this_file,
+            "pre_modecheck_examine_goal_expr: unexpected bi_implication")
+    ).

 :- pred pre_modecheck_examine_unify_rhs(unify_rhs::in,
     pred_elim_info::in, pred_elim_info::out) is det.
diff --git a/compiler/try_expand.m b/compiler/try_expand.m
index ae3ade7..91f5c12 100644
--- a/compiler/try_expand.m
+++ b/compiler/try_expand.m
@@ -193,6 +193,7 @@

 :- import_module hlds.
 :- import_module hlds.hlds_module.
+:- import_module parse_tree.prog_data.

 :- import_module io.

@@ -201,6 +202,13 @@
 :- pred expand_try_goals(module_info::in, module_info::out, io::di, io::uo)
     is det.

+    % try_expand_may_introduce_calls(PredName, Arity):
+    %
+    % Succeed if the transformation may introduce calls to a predicate
+    % or function with the given name in the exception module.
+    %
+:- pred try_expand_may_introduce_calls(string::in, arity::in) is semidet.
+
 %-----------------------------------------------------------------------------%
 %-----------------------------------------------------------------------------%

@@ -224,7 +232,6 @@
 :- import_module mdbcomp.prim_data.
 :- import_module parse_tree.
 :- import_module parse_tree.error_util.
-:- import_module parse_tree.prog_data.
 :- import_module parse_tree.prog_mode.
 :- import_module parse_tree.prog_type.

@@ -920,6 +927,12 @@ exception_cons_id =
cons(qualified(mercury_exception_module, "exception"), 1).

 %-----------------------------------------------------------------------------%

+try_expand_may_introduce_calls("try", 2).
+try_expand_may_introduce_calls("try_io", 4).
+try_expand_may_introduce_calls("unreachable", 0).
+
+%-----------------------------------------------------------------------------%
+
 :- func this_file = string.

 this_file = "try_expand.m".
--------------------------------------------------------------------------
mercury-reviews mailing list
Post messages to:       mercury-reviews at csse.unimelb.edu.au
Administrative Queries: owner-mercury-reviews at csse.unimelb.edu.au
Subscriptions:          mercury-reviews-request at csse.unimelb.edu.au
--------------------------------------------------------------------------



More information about the reviews mailing list