[m-rev.] for review: fix constant propagation and deep profiling

Julien Fischer juliensf at cs.mu.OZ.AU
Thu Aug 4 18:31:04 AEST 2005


(For the release branch I'm intending to just leave constant
propgation with deep profiling disabled.)

For review by anyone.

Estimated hours taken: 2
Branches: main

Fix a problem with deep profiling and constant propagation.
The compiler should not be attempting to perform constant
propagation after it has applied the deep profiling (or term-size
profiling) transformation.

Clean up the how the compiler invokes the simplification pass.

compiler/mercury_compile.m:

	Improve how we invoke the simplification pass at different
	stages of compilation.

	Add an optional run of the simplification pass immediately
	before the term-size and deep profiling transformations are
	applied.  We only run this pass if either of these
	transformations is being run.

	Make sure that the ll backend does not attempt to perform
	constant propagation after the deep profiling or term-size
	profiling transformations have been applied.

	Give each run of the simplification pass a more descriptive
	name in the HLDS dump.

	Fix some typos in comments.

Julien.

Index: compiler/mercury_compile.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/mercury_compile.m,v
retrieving revision 1.337
diff -u -r1.337 mercury_compile.m
--- compiler/mercury_compile.m	26 Jul 2005 01:56:24 -0000	1.337
+++ compiler/mercury_compile.m	4 Aug 2005 08:23:54 -0000
@@ -565,7 +565,7 @@
     % (2) If the argument doesn't end in `.m',
     % then we assume it is a module name.
     % (Is it worth checking that the name doesn't
-    % contain directory seperators, and issuing
+    % contain directory separators, and issuing
     % a warning or error in that case?)
     %
     (
@@ -853,7 +853,7 @@
     ;
         % If it doesn't end in `.m', then we assume it is
         % a module name.  (Is it worth checking that the
-        % name doesn't contain directory seperators, and issuing
+        % name doesn't contain directory separators, and issuing
         % a warning or error in that case?)
         file_name_to_module_name(String, ModuleName),
         FileOrModule = module(ModuleName)
@@ -2212,9 +2212,9 @@
             FoundStratError, !IO),
         mercury_compile__maybe_dump_hlds(!.HLDS, 60, "stratification", !IO),

-        mercury_compile__simplify(yes, no, Verbose, Stats,
+        mercury_compile__simplify(yes, frontend, Verbose, Stats,
             process_all_nonimported_procs, !HLDS, !IO),
-        mercury_compile__maybe_dump_hlds(!.HLDS, 65, "simplify", !IO),
+        mercury_compile__maybe_dump_hlds(!.HLDS, 65, "frontend_simplify", !IO),

         %
         % work out whether we encountered any errors
@@ -2357,6 +2357,18 @@

     mercury_compile__maybe_eliminate_dead_procs(Verbose, Stats, !HLDS, !IO),
     mercury_compile__maybe_dump_hlds(!.HLDS, 195, "dead_procs", !IO),
+
+    % If we are compiling in a deep profiling grade then now rerun
+    % simplify.  The reason for doing this now is that we want to
+    % take advantage of any opportunities the other optimizations have
+    % provided for constant propagation and we cannot do that once the
+    % term-size profiling/deep profiling transformations have been
+    % applied.
+    %
+    mercury_compile__simplify(yes, pre_prof_transforms, Verbose, Stats,
+        process_all_nonimported_procs, !HLDS, !IO),
+    mercury_compile__maybe_dump_hlds(!.HLDS, 197,
+        "pre_prof_transform_simplify", !IO),

     % The term size profiling transformation should be after all
     % transformations that construct terms of non-zero size. (Deep
@@ -2497,9 +2509,10 @@
     mercury_compile__maybe_followcode(Verbose, Stats, !HLDS, !IO),
     mercury_compile__maybe_dump_hlds(!.HLDS, 320, "followcode", !IO),

-    mercury_compile__simplify(no, yes, Verbose, Stats,
+    mercury_compile__simplify(no, ll_backend, Verbose, Stats,
         process_all_nonimported_nonaditi_procs, !HLDS, !IO),
-    mercury_compile__maybe_dump_hlds(!.HLDS, 325, "simplify2", !IO),
+    mercury_compile__maybe_dump_hlds(!.HLDS, 325,
+        "ll_backend_simplify", !IO),

     mercury_compile__compute_liveness(Verbose, Stats, !HLDS, !IO),
     mercury_compile__maybe_dump_hlds(!.HLDS, 330, "liveness", !IO),
@@ -2667,7 +2680,28 @@
     ;
         true
     ),
-    simplify__find_simplifications(no, Globals, Simplifications),
+    simplify__find_simplifications(no, Globals, Simplifications0),
+
+    globals.lookup_bool_option(Globals, profile_deep, DeepProf),
+    globals.lookup_bool_option(Globals, record_term_sizes_as_words, TSWProf),
+    globals.lookup_bool_option(Globals, record_term_sizes_as_cells, TSCProf),
+
+    ProfTrans = bool.or_list([DeepProf, TSWProf, TSCProf]),
+    (
+        % Don't run constant propagation if any of the profiling
+        % transformations has been applied.
+        %
+        % NOTE: Any changes here may also need to be made to
+        %       mercury_compile.simplify.
+        %
+        ProfTrans = yes,
+        Simplifications = list.delete_all(Simplifications0,
+            constant_prop)
+    ;
+        ProfTrans = no,
+        Simplifications = Simplifications0
+    ),
+
     simplify__proc([do_once | Simplifications], PredId, ProcId,
         !HLDS, !ProcInfo, !IO),
     write_proc_progress_message("% Computing liveness in ", PredId, ProcId,
@@ -2995,27 +3029,91 @@
         WarnDead = no
     ).

-:- pred mercury_compile__simplify(bool::in, bool::in, bool::in, bool::in,
+    % This type indicates what stage of compilation we are running
+    % the simplification pass at.  The exact simplifications we run
+    % will depend upon this.
+    %
+:- type simplify_pass
+    --->    frontend
+                % Immediately after the frontend passes.
+
+    ;       post_untuple
+                % After the untupling transformation has been applied.
+
+    ;       pre_prof_transforms
+                % If deep/term-size profiling is enabled then immediately
+                % before the source-to-source transformations that
+                % implement them.
+
+    ;       ml_backend
+                % The first stage of MLDS code generation.
+
+    ;       ll_backend.
+                % The first stage of LLDS code generation.
+
+:- pred mercury_compile__simplify(bool::in, simplify_pass::in, bool::in, bool::in,
     pred(task, module_info, module_info, io, io)::in(pred(task, in, out,
         di, uo) is det),
     module_info::in, module_info::out, io::di, io::uo) is det.

-mercury_compile__simplify(Warn, Once, Verbose, Stats, Process, !HLDS, !IO) :-
-    maybe_write_string(Verbose, "% Simplifying goals...\n", !IO),
-    maybe_flush_output(Verbose, !IO),
-    globals__io_get_globals(Globals, !IO),
-    simplify__find_simplifications(Warn, Globals, Simplifications0),
-    (
-        Once = yes,
-        Simplifications = [do_once | Simplifications0]
+mercury_compile__simplify(Warn, SimplifyPass, Verbose, Stats, Process,
+        !HLDS, !IO) :-
+
+    globals.io_get_globals(Globals, !IO),
+    globals.lookup_bool_option(Globals, profile_deep, DeepProf),
+    globals.lookup_bool_option(Globals, record_term_sizes_as_words, TSWProf),
+    globals.lookup_bool_option(Globals, record_term_sizes_as_cells, TSCProf),
+    %
+    % We run the simplify pass before the profiling transformations,
+    % only if those transformations are being applied - otherwise we
+    % just leave things to the backend simplification passes.
+    %
+    IsProfPass = bool.or_list([DeepProf, TSWProf, TSCProf]),
+    ( SimplifyPass = pre_prof_transforms, IsProfPass = no ->
+        true
     ;
-        Once = no,
-        Simplifications = Simplifications0
-    ),
-    call(Process, update_pred_error(simplify__pred(Simplifications)), !HLDS,
-        !IO),
-    maybe_write_string(Verbose, "% done.\n", !IO),
-    maybe_report_stats(Stats, !IO).
+        some [!Simplifications] (
+
+            maybe_write_string(Verbose, "% Simplifying goals...\n", !IO),
+            maybe_flush_output(Verbose, !IO),
+
+            simplify.find_simplifications(Warn, Globals, !:Simplifications),
+            (
+                SimplifyPass = frontend
+            ;
+                SimplifyPass = post_untuple,
+                list.cons(do_once, !Simplifications)
+            ;
+                SimplifyPass = pre_prof_transforms,
+                list.cons(do_once, !Simplifications)
+            ;
+                SimplifyPass = ml_backend,
+                list.cons(do_once, !Simplifications)
+            ;
+                % Don't perform constant propagation if one of the
+                % profiling transformations has been applied.
+                %
+                % NOTE: Any changes made here may also need to be made
+                % to the relevant parts of backend_pass_by_preds_4/12.
+                %
+                SimplifyPass = ll_backend,
+                ( IsProfPass = yes ->
+                    % XXX Why does find_simplifications return a list of
+                    % them rather than a set?
+                    list.delete_all(!.Simplifications, constant_prop,
+                        !:Simplifications)
+                ;
+                    true
+                ),
+                list.cons(do_once, !Simplifications)
+            ),
+            Process(update_pred_error(simplify.pred(!.Simplifications)), !HLDS,
+                !IO),
+
+            maybe_write_string(Verbose, "% done.\n", !IO),
+            maybe_report_stats(Stats, !IO)
+        )
+    ).

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

@@ -3294,7 +3392,7 @@
         maybe_flush_output(Verbose, !IO),
         untuple_arguments(!HLDS, !IO),
         maybe_write_string(Verbose, "% done.\n", !IO),
-        mercury_compile__simplify(no, yes, Verbose, Stats,
+        mercury_compile__simplify(no, post_untuple, Verbose, Stats,
             process_all_nonimported_nonaditi_procs, !HLDS, !IO),
         maybe_report_stats(Stats, !IO)
     ;
@@ -4188,9 +4286,9 @@
     globals__io_lookup_bool_option(verbose, Verbose, !IO),
     globals__io_lookup_bool_option(statistics, Stats, !IO),

-    mercury_compile__simplify(no, yes, Verbose, Stats,
+    mercury_compile__simplify(no, ml_backend, Verbose, Stats,
         process_all_nonimported_nonaditi_procs, !HLDS, !IO),
-    mercury_compile__maybe_dump_hlds(!.HLDS, 405, "simplify2", !IO),
+    mercury_compile__maybe_dump_hlds(!.HLDS, 405, "ml_backend_simplify", !IO),

     mercury_compile__maybe_add_trail_ops(Verbose, Stats, !HLDS, !IO),
     mercury_compile__maybe_dump_hlds(!.HLDS, 410, "add_trail_ops", !IO),

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