mirror of
https://github.com/Mercury-Language/mercury.git
synced 2025-12-16 06:14:59 +00:00
Estimated hours taken: 40 Branches: main A large part of the cost of a large ground term is incurred not when the term is constructed, but when it is used. The inst of the term will be huge, and will typically have to be traversed many times. Some of those traversals would be linear if not for the fact that, in order to avoid infinite loops on recursive insts, the predicate doing the traversal has to keep a set of the insts visited so far. When the traversal is in the middle of the ground term's inst, it is looking up that inst in a set of the insts of its containing terms all the way up to the root. When the ground term contains a list with many repeated elements near the start, the cost of the traversal is cubic in the length of the list: a linear number of set membership tests, each of which tests the current inst against a linear number of large insts, the test itself being linear. This diff aims to totally sidestep all that. It extends the mer_inst type to allow (but not require) the creator of an inst to record what the outcome of some tests on the inst would be. Is it ground? Does it contain "any"? What inst names and types may it contain? If the creator records this answer, which the code that creates ground terms does, then many tests will now run in CONSTANT time, not linear, quadratic or cubic. We do this only for bound insts. While the concept can apply to all insts, for small insts it can cost more to interpret the results term than to do the test directly. Insts cannot be large without being composed mostly of bound insts, so by recording this info only for bound insts, we can speed up the handling of all large insts. This also has the side benefit that in many cases, a traversal that operates on an inst will often do so in order to compute an updated version of that inst. In many cases, the updated version is the same as the original version, but since the traversal has to be prepared for updates, it makes a copy of the inst anyway. The result of the traversal is thus an inst that has the same value as the original inst but not the same address. This makes it useless to try to do equality checks of related insts in constant time by looking at the pointers. With this diff, many such traversals can be avoided, allowing the updated inst to keep the address as well as the value of the corresponding original inst. Without this diff, the compiler takes more than 10 seconds to compile zm_rcpsp_cpx.m, with most of that time being spent in mode checking. With this diff, it takes less than 5 seconds. Basically, mode checking went from 6+ seconds to 1. The profile of the compiler is now flat on this input; no single pass takes much more time than the others. The speed of the compiler is unaffected on tools/speedtest. (Actually, it gets a very slight speedup, but it is in the noise.) compiler/prog_data.m: Change the bound/2 functor of the mer_inst type to bound/3, adding a field that gives the outcome of some common tests performed on insts. When we attach insts to the variables representing parts of ground terms, we mark the insts accordingly. This allows us to perform many tests on insts in constant time, not in a time that is linear, quadratic or worse in the size of the inst. compiler/add_pragma.m: compiler/const_prop.m: compiler/distance_granularity.m: compiler/equiv_type_hlds.m: compiler/float_regs.m: compiler/hlds_code_util.m: compiler/hlds_goal.m: compiler/inst_check.m: compiler/inst_match.m: compiler/inst_util.m: compiler/lco.m: compiler/mercury_to_mercury.m: compiler/mode_constraints.m: compiler/mode_debug.m: compiler/mode_util.m: compiler/modecheck_goal.m: compiler/modecheck_unify.m: compiler/modecheck_util.m: compiler/module_qual.m: compiler/pd_util.m: compiler/polymorphism.m: compiler/prog_io.m: compiler/prog_io_util.m: compiler/prog_mode.m: compiler/prog_util.m: compiler/recompilation.usage.m: compiler/recompilation.version.m: compiler/simplify.m: compiler/try_expand.m: compiler/unique_modes.m: compiler/unused_imports.m: compiler/xml_documentation.m: Conform to the above change. Obviously, this required the modification of most predicates dealing with insts. Where the original predicates used multiple clauses, inconsistent variable names and/or bad grouping or ordering of code, this diff fixes that. More to the point, while in many places, the new code ignores the new field in input insts as either not relevant or not useful, in several places, the new code - pays attention to this field in input insts and executes less or faster code if the result of some test it needs is already available in it, or - fills in the field in insts it generates as output. Most, but not all, of changes in the first of those two categories were in inst_util.m and inst_match.m. compiler/hlds_out_mode.m: When writing out insts, or converting them to term form as the first step in printing them out, print the new field if we are generating debug output (such as a HLDS dump), but do not do so if we are generating actual Mercury code (such as a .opt file). Reorder the arguments of many predicates to move the context argument BEFORE the argument representing the object to be printed or converted to a term, since this allows us to use list.map on lists of such objects. compiler/hlds_out_util.m: Define a type that allows us to distinguish between the two. compiler/hlds_out_goal.m: compiler/hlds_out_module.m: compiler/hlds_out_pred.m: Thread values of this flag type through a bunch of predicates as needed. compiler/intermod.m: Specify output_mercury when writing clauses for optimization files. This is needed because mode-specific clauses can have insts in their heads. (The mode declarations in .int* files are written out by a separate set of predicates, in mercury_to_mercury.m, which ALWAYS ignore the new field.) compiler/prog_util.m: There were two predicates named construct_qualified_term, with different arities: one took a context, the other didn't. Rename the former to avoid the ambiguity. compiler/goal_expr_to_goal.m: Conform to the change to prog_util.m. compiler/prog_io.m: There were two predicates named constrain_inst_vars_in_mode; rename one. Add an XXX about why they are here in the first place. compiler/format_call.m: Give some type and field names prefixes to avoid some ambiguities.
187 lines
6.0 KiB
Mathematica
187 lines
6.0 KiB
Mathematica
%-----------------------------------------------------------------------------%
|
|
% vim: ft=mercury ts=4 sw=4 et
|
|
%-----------------------------------------------------------------------------%
|
|
% Copyright (C) 1996-1997, 2003-2009, 2012 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: mode_debug.m.
|
|
% Main author: fjh.
|
|
%
|
|
% This module contains code for tracing the actions of the mode checker.
|
|
%
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
:- module check_hlds.mode_debug.
|
|
:- interface.
|
|
|
|
:- import_module check_hlds.mode_info.
|
|
|
|
% Print a debugging message which includes the port, message string,
|
|
% and the current instmap (but only if `--debug-modes' was enabled).
|
|
%
|
|
:- pred mode_checkpoint(port::in, string::in, mode_info::in, mode_info::out)
|
|
is det.
|
|
|
|
:- type port
|
|
---> enter
|
|
; exit
|
|
; wakeup.
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
:- implementation.
|
|
|
|
:- import_module libs.
|
|
:- import_module libs.file_util.
|
|
:- import_module hlds.
|
|
:- import_module hlds.hlds_out.
|
|
:- import_module hlds.hlds_out.hlds_out_mode.
|
|
:- import_module hlds.hlds_out.hlds_out_util.
|
|
:- import_module hlds.instmap.
|
|
:- import_module parse_tree.
|
|
:- import_module parse_tree.mercury_to_mercury.
|
|
:- import_module parse_tree.prog_data.
|
|
|
|
:- import_module assoc_list.
|
|
:- import_module bool.
|
|
:- import_module io.
|
|
:- import_module list.
|
|
:- import_module maybe.
|
|
:- import_module pair.
|
|
:- import_module term.
|
|
:- import_module varset.
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
mode_checkpoint(Port, Msg, !ModeInfo) :-
|
|
mode_info_get_debug_modes(!.ModeInfo, DebugModes),
|
|
(
|
|
DebugModes = no
|
|
;
|
|
DebugModes = yes(debug_flags(Verbose, Minimal, Statistics)),
|
|
mode_info_get_errors(!.ModeInfo, Errors),
|
|
(
|
|
Port = enter,
|
|
PortStr = "Enter ",
|
|
Detail = yes
|
|
;
|
|
Port = wakeup,
|
|
PortStr = "Wake ",
|
|
Detail = no
|
|
;
|
|
Port = exit,
|
|
(
|
|
Errors = [],
|
|
PortStr = "Exit ",
|
|
Detail = yes
|
|
;
|
|
Errors = [_ | _],
|
|
PortStr = "Delay ",
|
|
Detail = no
|
|
)
|
|
),
|
|
mode_info_get_instmap(!.ModeInfo, InstMap),
|
|
trace [io(!IO)] (
|
|
io.write_string(PortStr, !IO),
|
|
io.write_string(Msg, !IO),
|
|
(
|
|
Detail = yes,
|
|
io.write_string(":\n", !IO),
|
|
maybe_report_stats(Statistics, !IO),
|
|
maybe_flush_output(Statistics, !IO),
|
|
( instmap_is_reachable(InstMap) ->
|
|
instmap_to_assoc_list(InstMap, NewInsts),
|
|
mode_info_get_last_checkpoint_insts(!.ModeInfo, OldInstMap),
|
|
mode_info_get_varset(!.ModeInfo, VarSet),
|
|
mode_info_get_instvarset(!.ModeInfo, InstVarSet),
|
|
write_var_insts(NewInsts, OldInstMap, VarSet, InstVarSet,
|
|
Verbose, Minimal, !IO)
|
|
;
|
|
io.write_string("\tUnreachable\n", !IO)
|
|
)
|
|
;
|
|
Detail = no
|
|
),
|
|
io.write_string("\n", !IO),
|
|
io.flush_output(!IO)
|
|
),
|
|
(
|
|
Detail = yes,
|
|
mode_info_set_last_checkpoint_insts(InstMap, !ModeInfo)
|
|
;
|
|
Detail = no
|
|
)
|
|
).
|
|
|
|
:- pred write_var_insts(assoc_list(prog_var, mer_inst)::in, instmap::in,
|
|
prog_varset::in, inst_varset::in, bool::in, bool::in,
|
|
io::di, io::uo) is det.
|
|
|
|
write_var_insts([], _, _, _, _, _, !IO).
|
|
write_var_insts([Var - Inst | VarInsts], OldInstMap, VarSet, InstVarSet,
|
|
Verbose, Minimal, !IO) :-
|
|
instmap_lookup_var(OldInstMap, Var, OldInst),
|
|
(
|
|
(
|
|
identical_insts(Inst, OldInst)
|
|
;
|
|
Inst = OldInst
|
|
)
|
|
->
|
|
(
|
|
Verbose = yes,
|
|
io.write_string("\t", !IO),
|
|
mercury_output_var(VarSet, no, Var, !IO),
|
|
io.write_string(" ::", !IO),
|
|
io.write_string(" unchanged\n", !IO)
|
|
;
|
|
Verbose = no
|
|
)
|
|
;
|
|
io.write_string("\t", !IO),
|
|
mercury_output_var(VarSet, no, Var, !IO),
|
|
io.write_string(" ::", !IO),
|
|
(
|
|
Minimal = yes,
|
|
io.write_string(" changed\n", !IO)
|
|
;
|
|
Minimal = no,
|
|
io.write_string("\n", !IO),
|
|
mercury_output_structured_inst(Inst, 2,
|
|
output_debug, do_not_incl_addr, InstVarSet, !IO)
|
|
)
|
|
),
|
|
write_var_insts(VarInsts, OldInstMap, VarSet, InstVarSet,
|
|
Verbose, Minimal, !IO).
|
|
|
|
% In the usual case of a C backend, this predicate allows us to
|
|
% conclude that two insts are identical without traversing them.
|
|
% Since the terms can be very large, this is a big gain; it can
|
|
% turn the complexity of printing a checkpoint from quadratic in the
|
|
% number of variables live at the checkpoint (when the variables
|
|
% are e.g. all part of a single long list) to linear. The minor
|
|
% increase in the constant factor in cases where identical_insts fails
|
|
% is much easier to live with.
|
|
%
|
|
:- pred identical_insts(mer_inst::in, mer_inst::in) is semidet.
|
|
|
|
identical_insts(_, _) :-
|
|
semidet_fail.
|
|
|
|
:- pragma foreign_proc("C",
|
|
identical_insts(InstA::in, InstB::in),
|
|
[will_not_call_mercury, promise_pure],
|
|
"
|
|
if (InstA == InstB) {
|
|
SUCCESS_INDICATOR = MR_TRUE;
|
|
} else {
|
|
SUCCESS_INDICATOR = MR_FALSE;
|
|
}
|
|
").
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
%-----------------------------------------------------------------------------%
|