[m-rev.] for review: hlc.agc performance improvement

Fergus Henderson fjh at cs.mu.OZ.AU
Wed Apr 3 04:12:56 AEST 2002


This gives about a 4% improvement for tests/benchmarks/queens.m.

----------

A performance improvement for accurate GC.

compiler/mercury_compile.m:
	Fix an XXX comment: invoke tail call optimization before
	ml_elim_nested.m.  This ensures that the stack setup code for
	accurate garbage collection gets put outside the tail-recursive
	loop rather than inside the loop.

compiler/ml_optimize.m:
	When optimizing tail calls, generate assignments rather than
	initializers for the temporary variables.  This is needed now
	that tail call optimization is done before ml_elim_nested.m.
	Also mark those temporary variables as not needing GC.

Workspace: /home/ceres/fjh/mercury
Index: compiler/mercury_compile.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/mercury_compile.m,v
retrieving revision 1.243
diff -u -d -r1.243 mercury_compile.m
--- compiler/mercury_compile.m	2 Apr 2002 16:46:55 -0000	1.243
+++ compiler/mercury_compile.m	2 Apr 2002 17:46:48 -0000
@@ -3430,36 +3430,24 @@
 	% run the ml_optimize pass before ml_elim_nested,
 	% so that we eliminate as many local variables as possible
 	% before the ml_elim_nested transformations.
+	% We also want to do tail recursion optimization before
+	% ml_elim_nested, since this means that the stack-handling
+	% code for accurate GC will go outside the loop rather than
+	% inside the loop.
 	%
-	% However, we don't want to do tail call elimination at
-	% this point, because that would result in loops
-	% with no call to MR_GC_check().
-	% So we explicitly disable that here.
-	% [XXX The preceding comment is wrong -- it is based on
-	% the assumption that we insert calls to MR_GC_check()
-	% at the start of each function, but now we instead
-	% call MR_GC_check() at each allocation.
-	% XXX FIXME Probably we should change the code here.]
-	%
-	% Also, we need to disable optimize_initializations,
+	% However, we need to disable optimize_initializations,
 	% because ml_elim_nested doesn't correctly handle
 	% code containing initializations.
-	%
-	% The only optimization that ml_optimize will do on this
-	% pass is eliminating variables.
 	globals__io_lookup_bool_option(optimize, Optimize),
 	( { Optimize = yes } ->
 		globals__io_lookup_bool_option(optimize_initializations,
 			OptimizeInitializations),
-		globals__io_set_option(optimize_tailcalls, bool(no)),
 		globals__io_set_option(optimize_initializations, bool(no)),
 
 		maybe_write_string(Verbose, "% Optimizing MLDS...\n"),
 		ml_optimize__optimize(MLDS20, MLDS25),
 		maybe_write_string(Verbose, "% done.\n"),
 
-		globals__io_set_option(optimize_tailcalls,
-			bool(OptimizeTailCalls)),
 		globals__io_set_option(optimize_initializations,
 			bool(OptimizeInitializations))
 	;
@@ -3505,7 +3493,7 @@
 	mercury_compile__maybe_dump_mlds(MLDS35, "35", "nested_funcs"),
 
 	% run the ml_optimize pass again after ml_elim_nested,
-	% to do tail call elimination.  (It may also help pick
+	% to do optimize_initializations.  (It may also help pick
 	% up some additional optimization opportunities for the
 	% other optimizations in this pass.)
 	( { Optimize = yes } ->
Index: compiler/ml_optimize.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/ml_optimize.m,v
retrieving revision 1.19
diff -u -d -r1.19 ml_optimize.m
--- compiler/ml_optimize.m	20 Mar 2002 12:36:47 -0000	1.19
+++ compiler/ml_optimize.m	2 Apr 2002 17:29:58 -0000
@@ -301,8 +301,9 @@
 generate_assign_args(_, [], [_|_], [], []) :-
 	error("generate_assign_args: length mismatch").
 generate_assign_args(_, [], [], [], []).
-generate_assign_args(OptInfo, [mlds__argument(Name, Type, GC_TraceCode) | Rest],
-		[Arg | Args], Statements, TempDefns) :-
+generate_assign_args(OptInfo, [Arg | Args], [ArgRval | ArgRvals],
+		Statements, TempDefns) :-
+	Arg = mlds__argument(Name, Type, _ArgGCTraceCode),
 	(
 		%
 		% extract the variable name
@@ -314,9 +315,9 @@
 			% 
 			% don't bother assigning a variable to itself
 			%
-			Arg = lval(var(QualVarName, _VarType))
+			ArgRval = lval(var(QualVarName, _VarType))
 		->
-			generate_assign_args(OptInfo, Rest, Args, 
+			generate_assign_args(OptInfo, Args, ArgRvals, 
 				Statements, TempDefns)
 		;
 
@@ -325,34 +326,49 @@
 			% args, and then assign the temporary to the
 			% parameter:
 			%
-			%	SomeType argN__tmp_copy = new_argN_value;
+			%	SomeType argN__tmp_copy;
+			%	argN__tmp_copy = new_argN_value;
 			%	...
-			%	new_argN_value = argN_tmp_copy;
+			%	argN = argN_tmp_copy;
 			%
 			% The temporaries are needed for the case where
 			% we are e.g. assigning v1, v2 to v2, v1;
 			% they ensure that we don't try to reference the old
 			% value of a parameter after it has already been
 			% clobbered by the new value.
+			%
+			% Note that we have to use an assignment rather
+			% than an initializer to initialize the temp,
+			% because this pass comes before ml_elem_nested.m,
+			% and ml_elim_nested.m doesn't handle code containing
+			% initializers.
 
 			VarName = mlds__var_name(VarNameStr, MaybeNum),
 			TempName = mlds__var_name(VarNameStr ++ "__tmp_copy",
 				MaybeNum),
 			QualTempName = qual(OptInfo ^ module_name, 
 				TempName),
-			Initializer = init_obj(Arg),
+			Initializer = no_initializer,
+			% We don't need to trace the temporary variables
+			% for GC, since they are not live across a call
+			% or a heap allocation
+			GC_TraceCode = no,
 			TempDefn = ml_gen_mlds_var_decl(var(TempName),
 				Type, Initializer, GC_TraceCode,
 				OptInfo ^ context),
-
-			Statement = statement(
+			TempInitStatement = statement(
+				atomic(assign(var(QualTempName, Type),
+					ArgRval)), 
+				OptInfo ^ context),
+			AssignStatement = statement(
 				atomic(assign(
 					var(QualVarName, Type),
 					lval(var(QualTempName, Type)))), 
 				OptInfo ^ context),
-			generate_assign_args(OptInfo, Rest, Args, Statements0,
-				TempDefns0),
-			Statements = [Statement | Statements0],
+			generate_assign_args(OptInfo, Args, ArgRvals,
+				Statements0, TempDefns0),
+			Statements = [TempInitStatement | Statements0] ++
+				[AssignStatement],
 			TempDefns = [TempDefn | TempDefns0]
 		)
 	;

-- 
Fergus Henderson <fjh at cs.mu.oz.au>  |  "I have always known that the pursuit
The University of Melbourne         |  of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh>  |     -- the last words of T. S. Garp.
--------------------------------------------------------------------------
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