[m-dev.] for review: fix bug in code generation for accurate gc grades

Zoltan Somogyi zs at cs.mu.OZ.AU
Tue Mar 6 13:22:05 AEDT 2001


For review by anyone.

Fix the interaction of the eager code generator with accurate gc to the extent
of getting to the end of stage 2 of a bootcheck in the asm_fast.agc grade.

compiler/call_gen.m:
compiler/pragma_c_gen.m:
	Kill the variables in the post-death set that are not needed after the
	call is made before we make the call, in particular before we clear
	the registers (to reflect the fact that the callee may modify any
	register). This prevents var_locn from throwing exceptions in accurate
	gc grades.

compiler/code_info.m:
	Delete the code for killing the variables in post-death set in
	code_info__setup_call, since this is now done in the setup_call's
	callers in call_gen.m.

compiler/var_locn.m:
	Turn back on the sanity check we had to turn off because we were
	deleting the states of variables before we killed them.

Zoltan.

cvs diff: Diffing .
cvs diff: Diffing bindist
cvs diff: Diffing boehm_gc
cvs diff: Diffing boehm_gc/Mac_files
cvs diff: Diffing boehm_gc/cord
cvs diff: Diffing boehm_gc/cord/private
cvs diff: Diffing boehm_gc/include
cvs diff: Diffing boehm_gc/include/private
cvs diff: Diffing browser
cvs diff: Diffing bytecode
cvs diff: Diffing compiler
Index: compiler/call_gen.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/call_gen.m,v
retrieving revision 1.146
diff -u -b -r1.146 call_gen.m
--- compiler/call_gen.m	2000/11/23 04:32:27	1.146
+++ compiler/call_gen.m	2001/03/05 03:41:36
@@ -1,5 +1,5 @@
 %---------------------------------------------------------------------------%
-% Copyright (C) 1994-2000 The University of Melbourne.
+% Copyright (C) 1994-2001 The University of Melbourne.
 % This file may only be copied under the terms of the GNU General
 % Public License - see the file COPYING in the Mercury distribution.
 %---------------------------------------------------------------------------%
@@ -74,6 +74,7 @@
 		% Save the necessary vars on the stack and move the input args
 		% to their registers.
 	code_info__setup_call(GoalInfo, ArgsInfos, LiveVals, SetupCode),
+	call_gen__kill_dead_input_vars(ArgsInfos, GoalInfo, NonLiveOutputs),
 
 		% Figure out what the call model is.
 	call_gen__prepare_for_call(CodeModel, CallModel, TraceCode),
@@ -104,7 +105,8 @@
 
 		% Update the code generator state to reflect the situation
 		% after the call.
-	call_gen__handle_return(ArgsInfos, ReturnInstMap, ReturnLiveLvalues),
+	call_gen__handle_return(ArgsInfos, GoalInfo, NonLiveOutputs,
+		ReturnInstMap, ReturnLiveLvalues),
 
 		% If the call can fail, generate code to check for and
 		% handle the failure.
@@ -156,6 +158,7 @@
 		% Save the necessary vars on the stack and move the input args
 		% defined by variables to their registers.
 	code_info__setup_call(GoalInfo, ArgInfos, LiveVals0, SetupCode),
+	call_gen__kill_dead_input_vars(ArgInfos, GoalInfo, NonLiveOutputs),
 
 		% Move the input args not defined by variables to their
 		% registers. Setting up these arguments last results in
@@ -192,8 +195,8 @@
 
 		% Update the code generator state to reflect the situation
 		% after the call.
-	call_gen__handle_return(OutArgsInfos, ReturnInstMap,
-		ReturnLiveLvalues),
+	call_gen__handle_return(OutArgsInfos, GoalInfo, NonLiveOutputs,
+		ReturnInstMap, ReturnLiveLvalues),
 
 		% If the call can fail, generate code to check for and
 		% handle the failure.
@@ -549,61 +552,85 @@
 
 %---------------------------------------------------------------------------%
 
-	% We must update the code generator state to reflect
-	% the situation after the call before building
-	% the return liveness info.
-	%
-	% It is possible for some output arguments to be local
-	% to this goal, i.e. for it not to appear in later code.
-	% In such cases, we consider them to be live at the point
-	% of the return, so that if debugging is enabled, their
-	% values can be inspected (or if they are typeinfos, their
-	% values can be used to inspect other variables), but then
-	% kill them so that they are not live beyond this goal.
-	%
+	% After we have placed all the input variables in their registers,
+	% we will want to clear all the registers so we can start updating
+	% the code generator state to reflect their contents after the call.
+	% (In the case of higher order calls, we may place some constant
+	% input arguments in registers before clearing them.) The register
+	% clearing code complains if it is asked to dispose of the last copy
+	% of a still live variable, so before we clear the registers, we must
+	% make forward-dead all the variables that are in this goal's
+	% post-death set. However, a variable may be in this set even if it
+	% is not live before the call, if it is bound by the call. (This can
+	% happen when the caller ignores some of the output arguments of the
+	% called procedure.) We handle such variables not by making them
+	% forward-dead but by simply never making them forward-live in the
+	% first place.
+
 	% ArgsInfos should list all the output arguments of the call.
-	% (It may contain the input arguments as well; handle_return
-	% ignores them.)
+	% It may contain the input arguments as well; kill_dead_input_vars
+	% and handle_return ignore them.
 
-:- pred call_gen__handle_return(assoc_list(prog_var, arg_info)::in,
-	instmap::in, list(liveinfo)::out,
+:- pred call_gen__kill_dead_input_vars(assoc_list(prog_var, arg_info)::in,
+	hlds_goal_info::in, set(prog_var)::out,
 	code_info::in, code_info::out) is det.
 
-call_gen__handle_return(ArgsInfos, ReturnInstMap, ReturnLiveLvalues) -->
-	call_gen__rebuild_registers(ArgsInfos, KillSet),
-	{ call_gen__output_arg_locs(ArgsInfos, OutputArgLocs) },
-	code_info__generate_return_live_lvalues(OutputArgLocs, ReturnInstMap,
-		ReturnLiveLvalues),
-	code_info__make_vars_forward_dead(KillSet).
+call_gen__kill_dead_input_vars(ArgsInfos, GoalInfo, NonLiveOutputs) -->
+	code_info__get_forward_live_vars(Liveness),
+	{ call_gen__find_nonlive_outputs(ArgsInfos, Liveness,
+		set__init, NonLiveOutputs) },
+	{ goal_info_get_post_deaths(GoalInfo, PostDeaths) },
+	{ set__difference(PostDeaths, NonLiveOutputs, ImmediatePostDeaths) },
+	code_info__make_vars_forward_dead(ImmediatePostDeaths).
 
-:- pred call_gen__rebuild_registers(assoc_list(prog_var, arg_info)::in,
-	set(prog_var)::out, code_info::in, code_info::out) is det.
+:- pred call_gen__handle_return(assoc_list(prog_var, arg_info)::in,
+	hlds_goal_info::in, set(prog_var)::in, instmap::in,
+	list(liveinfo)::out, code_info::in, code_info::out) is det.
 
-call_gen__rebuild_registers(Args, KillSet) -->
+call_gen__handle_return(ArgsInfos, _GoalInfo, _NonLiveOutputs, ReturnInstMap,
+		ReturnLiveLvalues) -->
 	code_info__clear_all_registers,
 	code_info__get_forward_live_vars(Liveness),
-	{ set__init(KillSet0) },
-	call_gen__rebuild_registers_2(Args, Liveness, KillSet0, KillSet).
+	call_gen__rebuild_registers(ArgsInfos, Liveness, OutputArgLocs),
+	code_info__generate_return_live_lvalues(OutputArgLocs, ReturnInstMap,
+		ReturnLiveLvalues).
+
+:- pred call_gen__find_nonlive_outputs(assoc_list(prog_var, arg_info)::in,
+	set(prog_var)::in, set(prog_var)::in, set(prog_var)::out) is det.
+
+call_gen__find_nonlive_outputs([], _, NonLiveOutputs, NonLiveOutputs).
+call_gen__find_nonlive_outputs([Var - arg_info(_ArgLoc, Mode) | Args],
+		Liveness, NonLiveOutputs0, NonLiveOutputs) :-
+	( Mode = top_out ->
+		( set__member(Var, Liveness) ->
+			NonLiveOutputs1 = NonLiveOutputs0
+		;
+			set__insert(NonLiveOutputs0, Var, NonLiveOutputs1)
+		)
+	;
+		NonLiveOutputs1 = NonLiveOutputs0
+	),
+	call_gen__find_nonlive_outputs(Args, Liveness,
+		NonLiveOutputs1, NonLiveOutputs).
 
-:- pred call_gen__rebuild_registers_2(assoc_list(prog_var, arg_info)::in,
-	set(prog_var)::in, set(prog_var)::in, set(prog_var)::out,
+:- pred call_gen__rebuild_registers(assoc_list(prog_var, arg_info)::in,
+	set(prog_var)::in, assoc_list(prog_var, arg_loc)::out,
 	code_info::in, code_info::out) is det.
 
-call_gen__rebuild_registers_2([], _, KillSet, KillSet) --> [].
-call_gen__rebuild_registers_2([Var - arg_info(ArgLoc, Mode) | Args], Liveness,
-		KillSet0, KillSet) -->
-	( { Mode = top_out } ->
+call_gen__rebuild_registers([], _, []) --> [].
+call_gen__rebuild_registers([Var - arg_info(ArgLoc, Mode) | Args], Liveness,
+		OutputArgLocs) -->
+	call_gen__rebuild_registers(Args, Liveness, OutputArgLocs1),
+	(
+		{ Mode = top_out },
+		{ set__member(Var, Liveness) }
+	->
 		{ code_util__arg_loc_to_register(ArgLoc, Register) },
 		code_info__set_var_location(Var, Register),
-		{ set__member(Var, Liveness) ->
-			KillSet1 = KillSet0
-		;
-			set__insert(KillSet0, Var, KillSet1)
-		}
+		{ OutputArgLocs = [Var - ArgLoc | OutputArgLocs1] }
 	;
-		{ KillSet1 = KillSet0 }
-	),
-	call_gen__rebuild_registers_2(Args, Liveness, KillSet1, KillSet).
+		{ OutputArgLocs = OutputArgLocs1 }
+	).
 
 %---------------------------------------------------------------------------%
 
Index: compiler/code_info.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/code_info.m,v
retrieving revision 1.262
diff -u -b -r1.262 code_info.m
--- compiler/code_info.m	2001/01/18 01:18:40	1.262
+++ compiler/code_info.m	2001/01/18 01:38:06
@@ -565,6 +565,11 @@
 :- pred code_info__post_goal_update(hlds_goal_info, code_info, code_info).
 :- mode code_info__post_goal_update(in, in, out) is det.
 
+	% Find out whether the body of the current procedure should use
+	% typeinfo liveness.
+:- pred code_info__body_typeinfo_liveness(bool::out,
+	code_info::in, code_info::out) is det.
+
 	% Find out the type of the given variable.
 :- pred code_info__variable_type(prog_var, type, code_info, code_info).
 :- mode code_info__variable_type(in, out, in, out) is det.
@@ -764,6 +769,14 @@
 
 %---------------------------------------------------------------------------%
 
+code_info__body_typeinfo_liveness(TypeInfoLiveness) -->
+	code_info__get_module_info(ModuleInfo),
+	code_info__get_pred_id(PredId),
+	{ module_info_pred_info(ModuleInfo, PredId, PredInfo) },
+	code_info__get_globals(Globals),
+	{ body_should_use_typeinfo_liveness(PredInfo, Globals,
+		TypeInfoLiveness) }.
+
 :- pred code_info__get_var_types(map(prog_var, type), code_info, code_info).
 :- mode code_info__get_var_types(out, in, out) is det.
 
@@ -3567,13 +3580,13 @@
 			VarLocnInfo0, VarLocnInfo) },
 		code_info__set_var_locns_info(var_locn_info(VarLocnInfo)),
 		{ assoc_list__values(AllLocs, LiveLocList) },
-		{ set__list_to_set(LiveLocList, LiveLocs) },
+		{ set__list_to_set(LiveLocList, LiveLocs) }
 
-		{ assoc_list__keys(InArgLocs, InArgVars) },
-		{ set__init(DeadVars0) },
-		code_info__which_variables_are_forward_live(InArgVars,
-			DeadVars0, DeadVars),
-		code_info__make_vars_forward_dead(DeadVars)
+		% { assoc_list__keys(InArgLocs, InArgVars) },
+		% { set__init(DeadVars0) },
+		% code_info__which_variables_are_forward_live(InArgVars,
+		% 	DeadVars0, DeadVars),
+		% code_info__make_vars_forward_dead(DeadVars)
 	).
 
 :- pred code_info__setup_call_args(assoc_list(prog_var, arg_info)::in,
@@ -3805,12 +3818,13 @@
 code_info__compute_forward_live_var_saves(OutArgs, VarLocs) -->
 	code_info__get_known_variables(Variables0),
 	{ set__list_to_set(Variables0, Vars0) },
-	code_info__get_module_info(ModuleInfo),
-	code_info__get_pred_id(PredId),
-	{ module_info_pred_info(ModuleInfo, PredId, PredInfo) },
-	code_info__get_globals(Globals),
-	{ body_should_use_typeinfo_liveness(PredInfo, Globals,
-		TypeInfoLiveness) },
+	code_info__body_typeinfo_liveness(TypeInfoLiveness),
+	% code_info__get_module_info(ModuleInfo),
+	% code_info__get_pred_id(PredId),
+	% { module_info_pred_info(ModuleInfo, PredId, PredInfo) },
+	% code_info__get_globals(Globals),
+	% { body_should_use_typeinfo_liveness(PredInfo, Globals,
+	% 	TypeInfoLiveness) },
 	code_info__get_proc_info(ProcInfo),
 	{ proc_info_vartypes(ProcInfo, VarTypes) },
 	{ proc_info_typeinfo_varmap(ProcInfo, TVarMap) },
Index: compiler/pragma_c_gen.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/pragma_c_gen.m,v
retrieving revision 1.42
diff -u -b -r1.42 pragma_c_gen.m
--- compiler/pragma_c_gen.m	2001/02/04 13:49:10	1.42
+++ compiler/pragma_c_gen.m	2001/02/06 15:45:05
@@ -304,13 +304,13 @@
 %	Of course we also need to #undef it afterwards.
 
 pragma_c_gen__generate_pragma_c_code(CodeModel, Attributes,
-		PredId, ProcId, ArgVars, ArgDatas, OrigArgTypes, _GoalInfo,
+		PredId, ProcId, ArgVars, ArgDatas, OrigArgTypes, GoalInfo,
 		PragmaImpl, Code) -->
 	(
 		{ PragmaImpl = ordinary(C_Code, Context) },
 		pragma_c_gen__ordinary_pragma_c_code(CodeModel, Attributes,
 			PredId, ProcId, ArgVars, ArgDatas, OrigArgTypes,
-			C_Code, Context, Code)
+			C_Code, Context, GoalInfo, Code)
 	;
 		{ PragmaImpl = nondet(
 			Fields, FieldsContext, First, FirstContext,
@@ -320,12 +320,13 @@
 			Fields, FieldsContext, First, FirstContext,
 			Later, LaterContext, Treat, Shared, SharedContext,
 			Code)
-	;	{ PragmaImpl = import(Name, HandleReturn, Vars, Context) },
+	;
+		{ PragmaImpl = import(Name, HandleReturn, Vars, Context) },
 		{ C_Code = string__append_list([HandleReturn, " ",
 				Name, "(", Vars, ");"]) },
 		pragma_c_gen__ordinary_pragma_c_code(CodeModel, Attributes,
 			PredId, ProcId, ArgVars, ArgDatas, OrigArgTypes,
-			C_Code, Context, Code)
+			C_Code, Context, GoalInfo, Code)
 	).
 
 %---------------------------------------------------------------------------%
@@ -333,12 +334,12 @@
 :- pred pragma_c_gen__ordinary_pragma_c_code(code_model::in,
 	pragma_foreign_code_attributes::in, pred_id::in, proc_id::in,
 	list(prog_var)::in, list(maybe(pair(string, mode)))::in, list(type)::in,
-	string::in, maybe(prog_context)::in, code_tree::out,
-	code_info::in, code_info::out) is det.
+	string::in, maybe(prog_context)::in, hlds_goal_info::in,
+	code_tree::out, code_info::in, code_info::out) is det.
 
 pragma_c_gen__ordinary_pragma_c_code(CodeModel, Attributes,
 		PredId, ProcId, ArgVars, ArgDatas, OrigArgTypes,
-		C_Code, Context, Code) -->
+		C_Code, Context, GoalInfo, Code) -->
 	
 	%
 	% Extract the attributes
@@ -354,8 +355,9 @@
 	{ pragma_select_in_args(Args, InArgs) },
 	{ pragma_select_out_args(Args, OutArgs) },
 
+	{ goal_info_get_post_deaths(GoalInfo, PostDeaths) },
 	{ set__init(DeadVars0) },
-	find_dead_input_vars(InArgs, DeadVars0, DeadVars),
+	{ find_dead_input_vars(InArgs, PostDeaths, DeadVars0, DeadVars) },
 
 	%
 	% Generate code to <save live variables on stack>
@@ -1108,17 +1110,17 @@
 %---------------------------------------------------------------------------%
 
 :- pred find_dead_input_vars(list(c_arg)::in, set(prog_var)::in,
-	set(prog_var)::out, code_info::in, code_info::out) is det.
+	set(prog_var)::in, set(prog_var)::out) is det.
 
-find_dead_input_vars([], DeadVars, DeadVars) --> [].
-find_dead_input_vars([Arg | Args], DeadVars0, DeadVars) -->
-	{ Arg = c_arg(Var, _MaybeName, _Type, _ArgInfo) },
-	( code_info__variable_is_forward_live(Var) ->
-		{ DeadVars1 = DeadVars0 }
+find_dead_input_vars([], _, DeadVars, DeadVars).
+find_dead_input_vars([Arg | Args], PostDeaths, DeadVars0, DeadVars) :-
+	Arg = c_arg(Var, _MaybeName, _Type, _ArgInfo),
+	( set__member(Var, PostDeaths) ->
+		set__insert(DeadVars0, Var, DeadVars1)
 	;
-		{ set__insert(DeadVars0, Var, DeadVars1) }
+		DeadVars1 = DeadVars0
 	),
-	find_dead_input_vars(Args, DeadVars1, DeadVars).
+	find_dead_input_vars(Args, PostDeaths, DeadVars1, DeadVars).
 
 %---------------------------------------------------------------------------%
 
Index: compiler/var_locn.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/var_locn.m,v
retrieving revision 1.3
diff -u -b -r1.3 var_locn.m
--- compiler/var_locn.m	2001/02/13 00:37:03	1.3
+++ compiler/var_locn.m	2001/03/05 03:44:07
@@ -1464,11 +1464,8 @@
 			var_locn__set_var_state_map(VarStateMap)
 		)
 	;
-		[]
-		% With the current liveness pass, this can fail.
-		% XXX This should be fixed.
-		% { require(unify(FirstTime, no),
-		% 	"var_locn__var_becomes_dead: premature deletion") }
+		{ require(unify(FirstTime, no),
+			"var_locn__var_becomes_dead: premature deletion") }
 	).
 
 % Given a set of lvals, return the set of root lvals among them and inside
cvs diff: Diffing compiler/notes
cvs diff: Diffing debian
cvs diff: Diffing doc
cvs diff: Diffing extras
cvs diff: Diffing extras/aditi
cvs diff: Diffing extras/cgi
cvs diff: Diffing extras/complex_numbers
cvs diff: Diffing extras/complex_numbers/samples
cvs diff: Diffing extras/complex_numbers/tests
cvs diff: Diffing extras/concurrency
cvs diff: Diffing extras/curs
cvs diff: Diffing extras/curs/samples
cvs diff: Diffing extras/curses
cvs diff: Diffing extras/curses/sample
cvs diff: Diffing extras/dynamic_linking
cvs diff: Diffing extras/graphics
cvs diff: Diffing extras/graphics/mercury_opengl
cvs diff: Diffing extras/graphics/mercury_tcltk
cvs diff: Diffing extras/graphics/samples
cvs diff: Diffing extras/graphics/samples/calc
cvs diff: Diffing extras/graphics/samples/maze
cvs diff: Diffing extras/graphics/samples/pent
cvs diff: Diffing extras/lazy_evaluation
cvs diff: Diffing extras/lex
cvs diff: Diffing extras/lex/samples
cvs diff: Diffing extras/logged_output
cvs diff: Diffing extras/moose
cvs diff: Diffing extras/moose/samples
cvs diff: Diffing extras/morphine
cvs diff: Diffing extras/morphine/non-regression-tests
cvs diff: Diffing extras/morphine/scripts
cvs diff: Diffing extras/morphine/source
cvs diff: Diffing extras/odbc
cvs diff: Diffing extras/posix
cvs diff: Diffing extras/references
cvs diff: Diffing extras/references/samples
cvs diff: Diffing extras/references/tests
cvs diff: Diffing extras/stream
cvs diff: Diffing extras/trailed_update
cvs diff: Diffing extras/trailed_update/samples
cvs diff: Diffing extras/trailed_update/tests
cvs diff: Diffing extras/xml
cvs diff: Diffing extras/xml/samples
cvs diff: Diffing java
cvs diff: Diffing library
cvs diff: Diffing profiler
cvs diff: Diffing robdd
cvs diff: Diffing runtime
cvs diff: Diffing runtime/GETOPT
cvs diff: Diffing runtime/machdeps
cvs diff: Diffing samples
cvs diff: Diffing samples/c_interface
cvs diff: Diffing samples/c_interface/c_calls_mercury
cvs diff: Diffing samples/c_interface/cplusplus_calls_mercury
cvs diff: Diffing samples/c_interface/mercury_calls_c
cvs diff: Diffing samples/c_interface/mercury_calls_cplusplus
cvs diff: Diffing samples/c_interface/mercury_calls_fortran
cvs diff: Diffing samples/c_interface/simpler_c_calls_mercury
cvs diff: Diffing samples/c_interface/simpler_cplusplus_calls_mercury
cvs diff: Diffing samples/diff
cvs diff: Diffing samples/muz
cvs diff: Diffing samples/rot13
cvs diff: Diffing samples/solutions
cvs diff: Diffing samples/tests
cvs diff: Diffing samples/tests/c_interface
cvs diff: Diffing samples/tests/c_interface/c_calls_mercury
cvs diff: Diffing samples/tests/c_interface/cplusplus_calls_mercury
cvs diff: Diffing samples/tests/c_interface/mercury_calls_c
cvs diff: Diffing samples/tests/c_interface/mercury_calls_cplusplus
cvs diff: Diffing samples/tests/c_interface/mercury_calls_fortran
cvs diff: Diffing samples/tests/c_interface/simpler_c_calls_mercury
cvs diff: Diffing samples/tests/c_interface/simpler_cplusplus_calls_mercury
cvs diff: Diffing samples/tests/diff
cvs diff: Diffing samples/tests/muz
cvs diff: Diffing samples/tests/rot13
cvs diff: Diffing samples/tests/solutions
cvs diff: Diffing samples/tests/toplevel
cvs diff: Diffing scripts
cvs diff: Diffing tests
cvs diff: Diffing tests/benchmarks
cvs diff: Diffing tests/debugger
cvs diff: Diffing tests/debugger/declarative
cvs diff: Diffing tests/dppd
cvs diff: Diffing tests/general
cvs diff: Diffing tests/general/accumulator
cvs diff: Diffing tests/hard_coded
cvs diff: Diffing tests/hard_coded/exceptions
cvs diff: Diffing tests/hard_coded/purity
cvs diff: Diffing tests/hard_coded/sub-modules
cvs diff: Diffing tests/hard_coded/typeclasses
cvs diff: Diffing tests/invalid
cvs diff: Diffing tests/invalid/purity
cvs diff: Diffing tests/misc_tests
cvs diff: Diffing tests/tabling
cvs diff: Diffing tests/term
cvs diff: Diffing tests/valid
cvs diff: Diffing tests/warnings
cvs diff: Diffing tools
cvs diff: Diffing trace
cvs diff: Diffing util
--------------------------------------------------------------------------
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