[m-rev.] for review: trace goals being optimised away

Peter Wang novalazy at gmail.com
Mon Jul 26 14:00:19 AEST 2010


Branches: main

Treat trace goals like impure goals for these passes.

compiler/constraint.m:
        Do not propagate constraints into or past trace goals.

compiler/simplify.m:
        Do not replace trace goals by `fail' or `true'.

        Make the predicate `goal_contains_trace' do what its name suggests.
        Previously it would only ever strip `feature_contains_trace' features
        from all goals and then return `contains_no_trace_goal'.

tests/hard_coded/Mmakefile:
tests/hard_coded/trace_goal_4.exp:
tests/hard_coded/trace_goal_4.m:
        Add test case.

diff --git a/compiler/constraint.m b/compiler/constraint.m
index 343eb8a..c5be0c9 100644
--- a/compiler/constraint.m
+++ b/compiler/constraint.m
@@ -476,9 +476,12 @@ annotate_conj_constraints(ModuleInfo,
         Constraints1 = [],
         Goals1 = [Goal - [] | Goals0]
     ;
-        % Don't propagate constraints into or past impure goals.
+        % Don't propagate constraints into or past impure goals
+        % or trace goals.
         Goal = hlds_goal(_, GoalInfo),
-        goal_info_get_purity(GoalInfo) = purity_impure
+        ( goal_info_get_purity(GoalInfo) = purity_impure
+        ; goal_info_has_feature(GoalInfo, feature_contains_trace)
+        )
     ->
         Constraints1 = [],
         flatten_constraints(Constraints0,
diff --git a/compiler/simplify.m b/compiler/simplify.m
index a6af89e..0db26cd 100644
--- a/compiler/simplify.m
+++ b/compiler/simplify.m
@@ -742,7 +742,10 @@ simplify_goal(Goal0, hlds_goal(GoalExpr, GoalInfo), !Info) :-
         % with `fail'.
 
         Detism = detism_failure,
-        ( Purity = purity_pure ; Purity = purity_semipure ),
+        ( Purity = purity_pure
+        ; Purity = purity_semipure
+        ),
+        goal_contains_trace(Goal0, _, contains_no_trace_goal),
         ( det_info_get_fully_strict(DetInfo, no)
         ; Goal0CanLoopOrThrow = cannot_loop_or_throw
         )
@@ -801,7 +804,10 @@ simplify_goal(Goal0, hlds_goal(GoalExpr, GoalInfo), !Info) :-
         NonLocalVars = goal_info_get_nonlocals(GoalInfo0),
         simplify_info_get_instmap(!.Info, InstMap0),
         det_no_output_vars(NonLocalVars, InstMap0, InstMapDelta, DetInfo),
-        ( Purity = purity_pure ; Purity = purity_semipure ),
+        ( Purity = purity_pure
+        ; Purity = purity_semipure
+        ),
+        goal_contains_trace(Goal0, _, contains_no_trace_goal),
         ( det_info_get_fully_strict(DetInfo, no)
         ; Goal0CanLoopOrThrow = cannot_loop_or_throw
         )
@@ -3518,10 +3524,23 @@ goal_contains_trace(hlds_goal(GoalExpr0, GoalInfo0),
         GoalExpr = negation(SubGoal)
     ;
         GoalExpr0 = scope(Reason, SubGoal0),
-        ( Reason = from_ground_term(_, from_ground_term_construct) ->
+        (
+            Reason = trace_goal(_, _, _, _, _),
+            SubGoal = SubGoal0,
+            ContainsTrace = contains_trace_goal
+        ;
+            Reason = from_ground_term(_, from_ground_term_construct),
             SubGoal = SubGoal0,
             ContainsTrace = contains_no_trace_goal
         ;
+            ( Reason = exist_quant(_)
+            ; Reason = promise_solutions(_, _)
+            ; Reason = promise_purity(_)
+            ; Reason = commit(_)
+            ; Reason = barrier(_)
+            ; Reason = from_ground_term(_, from_ground_term_deconstruct)
+            ; Reason = from_ground_term(_, from_ground_term_other)
+            ),
             goal_contains_trace(SubGoal0, SubGoal, ContainsTrace)
         ),
         GoalExpr = scope(Reason, SubGoal)
diff --git a/tests/hard_coded/Mmakefile b/tests/hard_coded/Mmakefile
index 5516d35..76e9303 100644
--- a/tests/hard_coded/Mmakefile
+++ b/tests/hard_coded/Mmakefile
@@ -264,6 +264,7 @@ ORDINARY_PROGS=	\
 	trace_goal_1 \
 	trace_goal_2 \
 	trace_goal_3 \
+	trace_goal_4 \
 	transform_value \
 	transitive_inst_type \
 	trigraphs \
diff --git a/tests/hard_coded/trace_goal_4.exp b/tests/hard_coded/trace_goal_4.exp
new file mode 100644
index 0000000..ae5861c
--- /dev/null
+++ b/tests/hard_coded/trace_goal_4.exp
@@ -0,0 +1,2 @@
+Progress reported 1
+Progress reported 2
diff --git a/tests/hard_coded/trace_goal_4.m b/tests/hard_coded/trace_goal_4.m
new file mode 100644
index 0000000..32f395a
--- /dev/null
+++ b/tests/hard_coded/trace_goal_4.m
@@ -0,0 +1,45 @@
+% Check trace goals do not get optimised away unexpectedly.
+
+:- module trace_goal_4.
+
+:- interface.
+
+:- import_module io.
+
+:- pred main(io::di, io::uo) is det.
+
+:- implementation.
+
+main(!IO) :-
+    (
+        trace [] impure progress_report(1),
+        fail
+    ->
+        true
+    ;
+        trace [] impure progress_report(2)
+    ).
+
+:- impure pred progress_report(int::in) is det.
+
+:- pragma foreign_proc("C",
+    progress_report(X::in),
+    [will_not_call_mercury, thread_safe, tabled_for_io],
+"
+    printf(""Progress reported %d\\n"", X);
+").
+
+:- pragma foreign_proc("Java",
+    progress_report(X::in),
+    [will_not_call_mercury, thread_safe, tabled_for_io],
+"
+    System.out.println(""Progress reported "" + X);
+").
+
+:- pragma foreign_proc("Erlang",
+    progress_report(X::in),
+    [will_not_call_mercury, thread_safe, tabled_for_io],
+"
+    io:format(""Progress reported ~a~n"", [X])
+").
+

--------------------------------------------------------------------------
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