[m-dev.] poor code generation for field accesses

Simon Taylor stayl at cs.mu.OZ.AU
Mon Jun 10 02:52:20 AEST 2002


On 04-Jun-2002, Fergus Henderson <fjh at cs.mu.OZ.AU> wrote:
> Currently we seem to generate poor code for field accesses,
> in at least some situations.
> Here's an example, from the Mercury ICFP2000 submission.
> For the following source (line 256 of renderer.m),
> 
> 	snd(Intersection)^object_id \= IgnoreId
> 
> which is equivalent to 
> 
> 	\+ (
> 		snd(Intersection)^object_id = IgnoreId
> 	),
> 
> we generate code
> 
> 	\+ (
> 		V_24 <= intersection(IgnoreId, _, _, _, _), % construction
> 		V_40 <= snd(Intersection),		    % function call
> 		V_24 = V_40                                 % unification
> 	)
> 
> This is bad, because the initial unification with V_24 is a
> construction, which allocates memory.

This problem isn't specific to field accesses.

For example
	intersection(IgnoreId, _, _, _, _, _) \= snd(Intersection)
allocates memory, but
	snd(Intersection) \= intersection(IgnoreId, _, _, _, _)
doesn't.

For the field access example, changing make_hlds.m to place the
computation of the inputs of the field access function before
the call to the field access function fixes the problem.

It might be worth writing an optimization pass to reorder code
to avoid memory allocation. Inlining/partially evaluating the
unification predicate would also work.

Simon.



Estimated hours taken: 0.25
Branches: main

compiler/make_hlds.m:
	Generate the input arguments for field access functions
	before the call, not after, to avoid allocating memory
	in code which tests the value of a field.

tests/invalid/record_syntax_errors.err_exp:
	Update the expected error output.

Index: compiler/make_hlds.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/make_hlds.m,v
retrieving revision 1.412
diff -u -u -r1.412 make_hlds.m
--- compiler/make_hlds.m	1 Jun 2002 13:34:34 -0000	1.412
+++ compiler/make_hlds.m	9 Jun 2002 10:11:47 -0000
@@ -6181,7 +6181,7 @@
 	{ ArgContext = functor(Functor, MainContext, SubContext0) },
 	{ goal_info_init(Context, GoalInfo) },
 	{ conj_list_to_goal(Goals1, GoalInfo, Conj0) },
-	append_arg_unifications(FieldArgVars, FieldArgs, Context, ArgContext,
+	insert_arg_unifications(FieldArgVars, FieldArgs, Context, ArgContext,
 		Conj0, VarSet4, Conj, VarSet, Info3, Info),
 	{ goal_to_conj_list(Conj, Goals) }.
 
@@ -6293,7 +6293,7 @@
 	{ ArgContext = functor(Functor, MainContext, SubContext0) },
 	{ goal_info_init(Context, GoalInfo) },
 	{ conj_list_to_goal(Goals2, GoalInfo, Conj0) },
-	append_arg_unifications(FieldArgVars, FieldArgs, Context, ArgContext,
+	insert_arg_unifications(FieldArgVars, FieldArgs, Context, ArgContext,
 		Conj0, VarSet3, Conj, VarSet, Info2, Info),
 	{ goal_to_conj_list(Conj, Goals) }.
 
@@ -7365,7 +7365,7 @@
 			VarSet1, VarSet2, Functor, _, Goal0, Info1, Info2),
 
 		{ ArgContext = functor(Functor, MainContext, SubContext) },
-		append_arg_unifications([InputTermVar], [InputTerm],
+		insert_arg_unifications([InputTermVar], [InputTerm],
 			FunctorContext, ArgContext, Goal0,
 			VarSet2, Goal, VarSet, Info2, Info)
 	;
@@ -7390,22 +7390,15 @@
 
 		{ TermArgContext = functor(Functor, MainContext, SubContext) },
 		{ TermArgNumber = 1 },
-		append_arg_unification(InputTermVar, InputTerm,
-			FunctorContext, TermArgContext, TermArgNumber,
-			TermUnifyConj, VarSet3, VarSet4, Info2, Info3),
-
 		{ FieldArgContext = functor(InnerFunctor,
 			MainContext, FieldSubContext) },
 		{ FieldArgNumber = 2 },
-		append_arg_unification(FieldValueVar, FieldValueTerm,
-			FunctorContext, FieldArgContext, FieldArgNumber,
-			FieldUnifyConj, VarSet4, VarSet, Info3, Info),
-
-		{ Goal0 = _ - GoalInfo0 },
-		{ goal_to_conj_list(Goal0, GoalList0) },
-		{ list__condense([GoalList0, TermUnifyConj, FieldUnifyConj],
-			GoalList) },
-		{ conj_list_to_goal(GoalList, GoalInfo0, Goal) }
+		{ ArgContexts = [TermArgNumber - TermArgContext,
+				FieldArgNumber - FieldArgContext] },
+		insert_arg_unifications_with_supplied_contexts(
+			[InputTermVar, FieldValueVar],
+			[InputTerm, FieldValueTerm], ArgContexts,
+			Context, Goal0, VarSet3, Goal, VarSet, Info2, Info)
 	;
 		{ parse_qualified_term(RHS, RHS, "", MaybeFunctor) },
 		(
Index: tests/invalid/record_syntax_errors.err_exp
===================================================================
RCS file: /home/mercury1/repository/tests/invalid/record_syntax_errors.err_exp,v
retrieving revision 1.6
diff -u -u -r1.6 record_syntax_errors.err_exp
--- tests/invalid/record_syntax_errors.err_exp	5 Jun 2002 16:41:30 -0000	1.6
+++ tests/invalid/record_syntax_errors.err_exp	8 Jun 2002 22:46:11 -0000
@@ -30,23 +30,11 @@
 record_syntax_errors.m:005:   in definition of constructor `record_syntax_errors:exist_cons/3 '.
 record_syntax_errors.m:046: In clause for predicate `record_syntax_errors:arg_type_error/1':
 record_syntax_errors.m:046:   in argument 2 of functor `field6 :=/2':
-record_syntax_errors.m:046:   in argument 2 of functor `field7 :=/2':
-record_syntax_errors.m:046:   type error in unification of argument
-record_syntax_errors.m:046:   and constant `"invalid value"'.
-record_syntax_errors.m:046:   argument has type `int',
-record_syntax_errors.m:046:   constant `"invalid value"' has type `string'.
-	The partial type assignment was:
-	Cons :: (record_syntax_errors:cons)
-	Cons0 :: (record_syntax_errors:cons)
-	V_4 :: int
-	V_5 :: (record_syntax_errors:cons2)
-	V_6 :: (record_syntax_errors:cons2)
-	V_7 :: int
-	V_8 :: int
-	V_9 :: (record_syntax_errors:cons2)
-	V_10 :: int
-	V_11 :: int
-
+record_syntax_errors.m:046:   in unification of argument
+record_syntax_errors.m:046:   and term `'field7 :='(V_5, V_4)':
+record_syntax_errors.m:046:   type error in argument(s) of functor `field7 :=/2'.
+record_syntax_errors.m:046:   Argument 2 has type `string',
+record_syntax_errors.m:046:   expected type was `int'.
 record_syntax_errors.m:050: In clause for predicate `record_syntax_errors:term_type_error/1':
 record_syntax_errors.m:050:   in argument 2 of functor `field6 :=/2':
 record_syntax_errors.m:050:   in unification of argument
--------------------------------------------------------------------------
mercury-developers mailing list
Post messages to:       mercury-developers at cs.mu.oz.au
Administrative Queries: owner-mercury-developers at cs.mu.oz.au
Subscriptions:          mercury-developers-request at cs.mu.oz.au
--------------------------------------------------------------------------



More information about the developers mailing list