Files
mercury/compiler/disj_gen.m
Zoltan Somogyi 189b9215ae This diff implements stack slot optimization for the LLDS back end based on
Estimated hours taken: 400
Branches: main

This diff implements stack slot optimization for the LLDS back end based on
the idea that after a unification such as A = f(B, C, D), saving the
variable A on the stack indirectly also saves the values of B, C and D.

Figuring out what subset of {B,C,D} to access via A and what subset to access
via their own stack slots is a tricky optimization problem. The algorithm we
use to solve it is described in the paper "Using the heap to eliminate stack
accesses" by Zoltan Somogyi and Peter Stuckey, available in ~zs/rep/stackslot.
That paper also describes (and has examples of) the source-to-source
transformation that implements the optimization.

The optimization needs to know what variables are flushed at call sites
and at program points that establish resume points (e.g. entries to
disjunctions and if-then-elses). We already had code to compute this
information in live_vars.m, but this code was being invoked too late.
This diff modifies live_vars.m to allow it to be invoked both by the stack
slot optimization transformation and by the code generator, and allows its
function to be tailored to the requirements of each invocation.

The information computed by live_vars.m is specific to the LLDS back end,
since the MLDS back ends do not (yet) have the same control over stack
frame layout. We therefore store this information in a new back end specific
field in goal_infos. For uniformity, we make all the other existing back end
specific fields in goal_infos, as well as the similarly back end specific
store map field of goal_exprs, subfields of this new field. This happens
to significantly reduce the sizes of goal_infos.

To allow a more meaningful comparison of the gains produced by the new
optimization, do not save any variables across erroneous calls even if
the new optimization is not enabled.

compiler/stack_opt.m:
	New module containing the code that performs the transformation
	to optimize stack slot usage.

compiler/matching.m:
	New module containing an algorithm for maximal matching in bipartite
	graphs, specialized for the graphs needed by stack_opt.m.

compiler/mercury_compile.m:
	Invoke the new optimization if the options ask for it.

compiler/stack_alloc.m:
	New module containing code that is shared between the old,
	non-optimizing stack slot allocation system and the new, optimizing
	stack slot allocation system, and the code for actually allocating
	stack slots in the absence of optimization.

	Live_vars.m used to have two tasks: find out what variables need to be
	saved on the stack, and allocating those variables to stack slots.
	Live_vars.m now does only the first task; stack_alloc.m now does
	the second, using code that used to be in live_vars.m.

compiler/trace_params:
	Add a new function to test the trace level, which returns yes if we
	want to preserve the values of the input headvars.

compiler/notes/compiler_design.html:
	Document the new modules (as well as trace_params.m, which wasn't
	documented earlier).

compiler/live_vars.m:
	Delete the code that is now in stack_alloc.m and graph_colour.m.

	Separate out the kinds of stack uses due to nondeterminism: the stack
	slots used by nondet calls, and the stack slots used by resumption
	points, in order to allow the reuse of stack slots used by resumption
	points after execution has left their scope. This should allow the
	same stack slots to be used by different variables in the resumption
	point at the start of an else branch and nondet calls in the then
	branch, since the resumption point of the else branch is not in effect
	when the then branch is executed.

	If the new option --opt-no-return-calls is set, then say that we do not
	need to save any values across erroneous calls.

	Use type classes to allow the information generated by this module
	to be recorded in the way required by its invoker.

	Package up the data structures being passed around readonly into a
	single tuple.

compiler/store_alloc.m:
	Allow this module to be invoked by stack_opt.m without invoking the
	follow_vars transformation, since applying follow_vars before the form
	of the HLDS code is otherwise final can be a pessimization.

	Make the module_info a part of the record containing the readonly data
	passed around during the traversal.

compiler/common.m:
	Do not delete or move around unifications created by stack_opt.m.

compiler/call_gen.m:
compiler/code_info.m:
compiler/continuation_info.m:
compiler/var_locn.m:
	Allow the code generator to delete its last record of the location
	of a value when generating code to make an erroneous call, if the new
	--opt-no-return-calls option is set.

compiler/code_gen.m:
	Use a more useful algorithm to create the messages/comments that
	we put into incr_sp instructions, e.g. by distinguishing between
	predicates and functions. This is to allow the new scripts in the
	tool directory to gather statistics about the effect of the
	optimization on stack frame sizes.

library/exception.m:
	Make a hand-written incr_sp follow the new pattern.

compiler/arg_info.m:
	Add predicates to figure out the set of input, output and unused
	arguments of a procedure in several different circumstances.
	Previously, variants of these predicates were repeated in several
	places.

compiler/goal_util.m:
	Export some previously private utility predicates.

compiler/handle_options.m:
	Turn off stack slot optimizations when debugging, unless
	--trace-optimized is set.

	Add a new dump format useful for debugging --optimize-saved-vars.

compiler/hlds_llds.m:
	New module for handling all the stuff specific to the LLDS back end
	in HLDS goal_infos.

compiler/hlds_goal.m:
	Move all the relevant stuff into the new back end specific field
	in goal_infos.

compiler/notes/allocation.html:
	Update the documentation of store maps to reflect their movement
	into a subfield of goal_infos.

compiler/*.m:
	Minor changes to accomodate the placement of all back end specific
	information about goals from goal_exprs and individual fields of
	goal_infos into a new field in goal_infos that gathers together
	all back end specific information.

compiler/use_local_vars.m:
	Look for sequences in which several instructions use a fake register
	or stack slot as a base register pointing to a cell, and make those
	instructions use a local variable instead.

	Without this, a key assumption of the stack slot optimization,
	that accessing a field in a cell costs only one load or store
	instruction, would be much less likely to be true. (With this
	optimization, the assumption will be false only if the C compiler's
	code generator runs out of registers in a basic block, which for
	the code we generate should be unlikely even on x86s.)

compiler/options.m:
	Make the old option --optimize-saved-vars ask for both the old stack
	slot optimization (implemented by saved_vars.m) that only eliminates
	the storing of constants in stack slots, and the new optimization.

	Add two new options --optimize-saved-vars-{const,cell} to turn on
	the two optimizations separately.

	Add a bunch of options to specify the parameters of the new
	optimizations, both in stack_opt.m and use_local_vars.m. These are
	for implementors only; they are deliberately not documented.

	Add a new option, --opt-no-return-cells, that governs whether we avoid
	saving variables on the stack at calls that cannot return, either by
	succeeding or by failing. This is for implementors only, and thus
	deliberately documented only in comments. It is enabled by default.

compiler/optimize.m:
	Transmit the value of a new option to use_local_vars.m.

doc/user_guide.texi:
	Update the documentation of --optimize-saved-vars.

library/tree234.m:
	Undo a previous change of mine that effectively applied this
	optimization by hand. That change complicated the code, and now
	the compiler can do the optimization automatically.

tools/extract_incr_sp:
	A new script for extracting stack frame sizes and messages from
	stack increment operations in the C code for LLDS grades.

tools/frame_sizes:
	A new script that uses extract_incr_sp to extract information about
	stack frame sizes from the C files saved from a stage 2 directory
	by makebatch and summarizes the resulting information.

tools/avg_frame_size:
	A new script that computes average stack frame sizes from the files
	created by frame_sizes.

tools/compare_frame_sizes:
	A new script that compares the stack frame size information
	extracted from two different stage 2 directories by frame_sizes,
	reporting on both average stack frame sizes and on specific procedures
	that have different stack frame sizes in the two versions.
2002-03-28 03:44:41 +00:00

335 lines
11 KiB
Mathematica

%-----------------------------------------------------------------------------%
% Copyright (C) 1994-2000,2002 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.
%---------------------------------------------------------------------------%
%
% File: disj_gen.m:
%
% Main authors: conway, zs.
%
% The predicates of this module generate code for disjunctions.
%
%---------------------------------------------------------------------------%
%---------------------------------------------------------------------------%
:- module ll_backend__disj_gen.
:- interface.
:- import_module hlds__hlds_goal, backend_libs__code_model, ll_backend__llds.
:- import_module ll_backend__code_info.
:- import_module list.
:- pred disj_gen__generate_disj(code_model::in, list(hlds_goal)::in,
hlds_goal_info::in, code_tree::out, code_info::in, code_info::out)
is det.
%---------------------------------------------------------------------------%
:- implementation.
:- import_module parse_tree__prog_data.
:- import_module hlds__hlds_data, hlds__hlds_llds.
:- import_module ll_backend__code_gen.
:- import_module ll_backend__code_util, ll_backend__trace.
:- import_module libs__options, libs__globals, libs__tree.
:- import_module bool, set, libs__tree, map, std_util, term, require.
disj_gen__generate_disj(CodeModel, Goals, DisjGoalInfo, Code) -->
(
{ Goals = [] },
( { CodeModel = model_semi } ->
code_info__generate_failure(Code)
;
{ error("empty disjunction") }
)
;
{ Goals = [Goal | _] },
{ Goal = _ - GoalInfo },
{ goal_info_get_resume_point(GoalInfo, Resume) },
{ Resume = resume_point(ResumeVarsPrime, _) ->
ResumeVars = ResumeVarsPrime
;
set__init(ResumeVars)
},
disj_gen__generate_real_disj(CodeModel, ResumeVars,
Goals, DisjGoalInfo, Code)
).
%---------------------------------------------------------------------------%
:- pred disj_gen__generate_real_disj(code_model::in, set(prog_var)::in,
list(hlds_goal)::in, hlds_goal_info::in, code_tree::out,
code_info::in, code_info::out) is det.
disj_gen__generate_real_disj(CodeModel, ResumeVars, Goals, DisjGoalInfo, Code)
-->
% Make sure that the variables whose values will be needed
% on backtracking to any disjunct are materialized into
% registers or stack slots. Their locations are recorded
% in ResumeMap.
code_info__produce_vars(ResumeVars, ResumeMap, FlushCode),
% If we are using a trail, save the current trail state
% before the first disjunct.
% XXX We should use a scheme such as the one we use for heap
% recovery for semi and det disjunctions, and delay saving
% the ticket until necessary.
code_info__get_globals(Globals),
{ globals__lookup_bool_option(Globals, use_trail, UseTrail) },
code_info__maybe_save_ticket(UseTrail, SaveTicketCode,
MaybeTicketSlot),
% If we are using a grade in which we can recover memory
% by saving and restoring the heap pointer, set up for
% doing so if necessary.
( { CodeModel = model_non } ->
% With nondet disjunctions, we must recover memory
% across all disjuncts, even disjuncts that cannot
% themselves allocate memory, since we can backtrack
% to disjunct N after control leaves disjunct N-1.
{ globals__lookup_bool_option(Globals,
reclaim_heap_on_nondet_failure, ReclaimHeap) },
code_info__maybe_save_hp(ReclaimHeap, SaveHpCode,
MaybeHpSlot)
;
% With other disjunctions, we can backtrack to
% disjunct N only from disjunct N-1, so if disjunct
% N-1 does not allocate memory, we need not recover
% memory across it. Since it is possible (and common)
% for no disjunct to allocate memory, we delay saving
% the heap pointer and allocating a stack slot for
% the saved hp as long as possible.
{ globals__lookup_bool_option(Globals,
reclaim_heap_on_semidet_failure, ReclaimHeap) },
{ SaveHpCode = empty },
{ MaybeHpSlot = no }
),
% Save the values of any stack slots we may hijack,
% and if necessary, set the redofr slot of the top frame
% to point to this frame.
code_info__prepare_for_disj_hijack(CodeModel,
HijackInfo, PrepareHijackCode),
code_info__get_next_label(EndLabel),
code_info__remember_position(BranchStart),
disj_gen__generate_disjuncts(Goals, CodeModel, ResumeMap, no,
HijackInfo, DisjGoalInfo, EndLabel,
ReclaimHeap, MaybeHpSlot, MaybeTicketSlot,
BranchStart, no, MaybeEnd, GoalsCode),
{ goal_info_get_store_map(DisjGoalInfo, StoreMap) },
code_info__after_all_branches(StoreMap, MaybeEnd),
( { CodeModel = model_non } ->
code_info__set_resume_point_to_unknown
;
[]
),
{ Code =
tree(FlushCode,
tree(SaveTicketCode,
tree(SaveHpCode,
tree(PrepareHijackCode,
GoalsCode))))
}.
%---------------------------------------------------------------------------%
:- pred disj_gen__generate_disjuncts(list(hlds_goal)::in,
code_model::in, resume_map::in, maybe(resume_point_info)::in,
disj_hijack_info::in, hlds_goal_info::in, label::in,
bool::in, maybe(lval)::in, maybe(lval)::in, position_info::in,
maybe(branch_end_info)::in, maybe(branch_end_info)::out,
code_tree::out, code_info::in, code_info::out) is det.
disj_gen__generate_disjuncts([], _, _, _, _, _, _, _, _, _, _, _, _, _) -->
{ error("empty disjunction!") }.
disj_gen__generate_disjuncts([Goal0 | Goals], CodeModel, FullResumeMap,
MaybeEntryResumePoint, HijackInfo, DisjGoalInfo, EndLabel,
ReclaimHeap, MaybeHpSlot0, MaybeTicketSlot,
BranchStart0, MaybeEnd0, MaybeEnd, Code) -->
code_info__reset_to_position(BranchStart0),
% If this is not the first disjunct, generate the
% resume point by which arrive at this disjunct.
( { MaybeEntryResumePoint = yes(EntryResumePoint) } ->
code_info__generate_resume_point(EntryResumePoint,
EntryResumePointCode)
;
{ EntryResumePointCode = empty }
),
{ Goal0 = GoalExpr0 - GoalInfo0 },
{ goal_info_get_resume_point(GoalInfo0, Resume) },
(
{ Resume = resume_point(ResumeVars, ResumeLocs) }
->
% Emit code for a non-last disjunct, including setting things
% up for the execution of the next disjunct.
( { MaybeEntryResumePoint = yes(_) } ->
% Reset the heap pointer to recover memory
% allocated by the previous disjunct(s),
% if necessary.
code_info__maybe_restore_hp(MaybeHpSlot0,
RestoreHpCode),
% Reset the solver state if necessary.
code_info__maybe_reset_ticket(MaybeTicketSlot, undo,
RestoreTicketCode)
;
{ RestoreHpCode = empty },
{ RestoreTicketCode = empty }
),
% The pre_goal_update sanity check insists on
% no_resume_point, to make sure that all resume
% points have been handled by surrounding code.
{ goal_info_set_resume_point(GoalInfo0, no_resume_point,
GoalInfo) },
{ Goal = GoalExpr0 - GoalInfo },
% Save hp if it needs to be saved and hasn't been
% saved previously.
(
{ ReclaimHeap = yes },
{ code_util__goal_may_allocate_heap(Goal) },
{ MaybeHpSlot0 = no }
->
code_info__save_hp(SaveHpCode, HpSlot),
{ MaybeHpSlot = yes(HpSlot) },
% This method of updating BranchStart0 is
% ugly. The best alternative would be to
% arrange things so that a remember_position
% here could get BranchStart, but doing so
% is iffy because we have already created
% the resumption point for entry into this
% disjunction, which overwrites part of the
% location-dependent state originally in
% BranchStart0.
{ code_info__save_hp_in_branch(BranchSaveHpCode,
BranchHpSlot, BranchStart0, BranchStart) },
{ tree__flatten(SaveHpCode, HpCodeList) },
{ tree__flatten(BranchSaveHpCode, BranchHpCodeList) },
{ require(unify(HpCodeList, BranchHpCodeList),
"cannot use same code for saving hp") },
{ require(unify(HpSlot, BranchHpSlot),
"cannot allocate same slot for saved hp") }
;
{ SaveHpCode = empty },
{ MaybeHpSlot = MaybeHpSlot0 },
{ BranchStart = BranchStart0 }
),
code_info__make_resume_point(ResumeVars, ResumeLocs,
FullResumeMap, NextResumePoint),
code_info__effect_resume_point(NextResumePoint, CodeModel,
ModContCode),
trace__maybe_generate_internal_event_code(Goal, TraceCode),
{ goal_info_get_code_model(GoalInfo, GoalCodeModel) },
code_gen__generate_goal(GoalCodeModel, Goal, GoalCode),
( { CodeModel = model_non } ->
% We can backtrack to the next disjunct from outside,
% so we make sure every variable in the resume set
% is in its stack slot.
code_info__flush_resume_vars_to_stack(ResumeVarsCode),
% We hang onto any temporary slots holding saved
% heap pointers and/or tickets, thus ensuring that
% they will still be reserved after the disjunction.
{ PruneTicketCode = empty }
;
{ ResumeVarsCode = empty },
code_info__maybe_release_hp(MaybeHpSlot),
% We're committing to this disjunct if it succeeds.
code_info__maybe_reset_prune_and_release_ticket(
MaybeTicketSlot, commit, PruneTicketCode),
code_info__reset_resume_known(BranchStart)
),
% Forget the variables that are needed only at the
% resumption point at the start of the next disjunct,
% so that we don't generate exceptions when their
% storage is clobbered by the movement of the live
% variables to the places indicated in the store map.
code_info__pop_resume_point,
code_info__pickup_zombies(Zombies),
code_info__make_vars_forward_dead(Zombies),
% Put every variable whose value is needed after
% the disjunction to the place indicated by StoreMap,
% and accumulate information about the code_info state
% at the ends of the branches so far.
{ goal_info_get_store_map(DisjGoalInfo, StoreMap) },
code_info__generate_branch_end(StoreMap, MaybeEnd0, MaybeEnd1,
SaveCode),
{ BranchCode = node([
goto(label(EndLabel)) -
"skip to end of nondet disj"
]) },
disj_gen__generate_disjuncts(Goals, CodeModel, FullResumeMap,
yes(NextResumePoint), HijackInfo, DisjGoalInfo,
EndLabel, ReclaimHeap, MaybeHpSlot, MaybeTicketSlot,
BranchStart, MaybeEnd1, MaybeEnd, RestCode),
{ Code =
tree(EntryResumePointCode,
tree(RestoreHpCode,
tree(RestoreTicketCode,
tree(SaveHpCode,
tree(ModContCode,
tree(TraceCode,
tree(GoalCode,
tree(ResumeVarsCode,
tree(PruneTicketCode,
tree(SaveCode,
tree(BranchCode,
RestCode)))))))))))
}
;
% Emit code for the last disjunct
% Restore the heap pointer and solver state
% if necessary.
code_info__maybe_restore_and_release_hp(MaybeHpSlot0,
RestoreHpCode),
code_info__maybe_reset_discard_and_release_ticket(
MaybeTicketSlot, undo, RestoreTicketCode),
code_info__undo_disj_hijack(HijackInfo, UndoCode),
trace__maybe_generate_internal_event_code(Goal0, TraceCode),
code_gen__generate_goal(CodeModel, Goal0, GoalCode),
{ goal_info_get_store_map(DisjGoalInfo, StoreMap) },
code_info__generate_branch_end(StoreMap, MaybeEnd0, MaybeEnd,
SaveCode),
{ EndCode = node([
label(EndLabel) - "End of nondet disj"
]) },
{ Code =
tree(EntryResumePointCode,
tree(TraceCode,
tree(RestoreHpCode,
tree(RestoreTicketCode,
tree(UndoCode,
tree(GoalCode,
tree(SaveCode,
EndCode)))))))
}
).
%---------------------------------------------------------------------------%