Files
mercury/runtime/mercury_deep_copy.c
Peter Wang b86f973fa9 Allow the use of Mercury abstract machine float registers for passing
Branches: main

Allow the use of Mercury abstract machine float registers for passing
double-precision float arguments in higher order calls.

In of itself this is not so useful for typical Mercury code.  However, as
all non-local procedures are potentially the targets of higher order calls,
without this change first order calls to non-local procedures could not use
float registers either.  That is the actual motivation for this change.

The basic mechanism is straightforward.  As before, do_call_closure_* is
invoked to place the closure's hidden arguments into r1, ..., rN, and extra
input arguments shifted into rN+1, etc.  With float registers, extra input
arguments may also be in f1, f2, etc. and the closure may also have hidden
float arguments.  Optimising for calls, we order the closure's hidden
arguments so that all float register arguments come after all regular
register arguments in the vector.  Having the arguments out of order does
complicate code which needs to deconstruct closures, but that is not so
important.

Polymorphism complicates things.  A closure with type pred(float) may be
passed to a procedure expecting pred(T).  Due to the `float' argument type,
the closure expects its argument in a float register.  But when passed to the
procedure, the polymorphic argument type means it would be called with the
argument in a regular register.

Higher-order insts already contain information about the calling convention,
without which a higher-order term cannot be called.  We extend higher-order
insts to include information about the register class required for each
argument.  For example, we can distinguish between:

	pred(in) is semidet /* arg regs: [reg_f] */
and
	pred(in) is semidet /* arg regs: [reg_r] */

Using this information, we can create a wrapper around a higher-order
variable if it appears in a context requiring a different calling convention.
We do this in a new HLDS pass, called float_regs.m.

Note: Mercury code has a tendency to lose insts for higher-order terms, then
"recover" them by hacky means.  The float_regs pass depends on higher-order
insts; it is impossible to create a wrapper for a procedure without knowing
how to call it.  The float_regs pass will report errors which we otherwise
accepted, due to higher-order insts being unavailable.  It should be possible
for the user to adjust the code to satisfy the pass, though the user may not
understand why it should be necessary.  In most cases, it probably really
*is* unnecessary.  We may be able to make the float_regs pass more tolerant
of missing higher-order insts in the future.

Class method calls do not use float registers because I didn't want to deal
with them yet.


compiler/options.m:
compiler/handle_options.m:
	Always enable float registers in low-level C grades when floats are
	wider than a word.

compiler/make_hlds_passes.m:
	Always allow double word floats to be stored unboxed in cells on C
	grades.

compiler/hlds_goal.m:
	Add an extra field to `generic_call' which gives the register class
	to use for each argument.  This is set by the float_regs pass.

compiler/prog_data.m:
	Add an extra field to `pred_inst_info' which records the register class
	to use for each argument.  This is set by the float_regs pass.

compiler/hlds_pred.m:
	Add a field to `proc_sub_info' which lists the headvars which must be
	passed via regular registers despite their types.

	Add a field to `pred_sub_info' to record the original unsubstituted
	argument types for instance method predicates.

compiler/check_typeclass.m:
	In the pred_info of an instance method predicate, record the original
	argument types before substituting the type variables for the instance.

compiler/float_regs.m:
compiler/transform_hlds.m:
	Add the new HLDS pass.

compiler/mercury_compile_middle_passes.m:
	Run the new pass if float registers are enabled.

compiler/lambda.m:
	Export the predicate to produce a predicate from a lambda.
	This is reused by float_regs.m to create wrapper closures.

	Add an argument to `expand_lambda' to set the reg_r_headvars field on
	the newly created procedure.

	Delete some unused fields from `lambda_info'.

compiler/arg_info.m:
	Make `generate_proc_arg_info' no longer always use regular registers
	for calls to exported procedures.  Do always use regular registers for
	class methods calls.

	Add a version of `make_arg_infos' which takes an explicit list of
	argument registers.  Rename the previous version.

	Add `generic_call_arg_reg_types' to return the argument registers
	for a generic call.

	Add a version of `compute_in_and_out_vars' which additionally separates
	arguments for float and regular registers.

compiler/call_gen.m:
	Use float registers for argument passing in higher-order calls, as
	directed by the new field in `generic_call'.

compiler/code_util.m:
	Add a function to encode the number of regular and float register
	arguments when making a higher-order call.

compiler/llds.m:
	Say that the `do_call_closure_N' functions only work for zero float
	register arguments.

compiler/follow_vars.m:
compiler/interval.m:
	Account for the use of float registers by generic call goals in these
	passes.

compiler/unify_gen.m:
	Move float register arguments to the end of a closure's hidden
	arguments vector, after regular register arguments.

	Count hidden regular and float register arguments separately, but
	encode them in the same word in the closure.  This is preferable to
	using two words because it reduces the differences between grades
	with and without float registers present.

	Disable generating code which creates a closure from an existing
	closure, if float registers exist.  That code does not understand the
	reordered hidden arguments vector yet.

compiler/continuation_info.m:
	Replace an argument's type_info in the closure layout if the argument
	is a float *and* is passed via a regular register, when floats are
	normally passed via float registers.  Instead, give it the type_info
	for `private_builtin.float_box'.

compiler/builtin_lib_types.m:
	Add function to return the type of `private_builtin.float_box/0'.

compiler/hlds_out_goal.m:
compiler/hlds_out_pred.m:
compiler/mercury_to_mercury.m:
	Dump the new fields added to `generic_call', `pred_inst_info' and
	`proc_sub_info'.

compiler/prog_type.m:
	Add helper predicate.

compiler/*.m:
	Conform to changes.

library/private_builtin.m:
	Add a type `float_box'.

runtime/mercury_ho_call.h:
	Describe the modified closure representation.

	Rename the field which counts the number of hidden arguments to prevent
	it being used incorrectly, as it now encodes two numbers (potentially).

	Add macros to unpack the encoded field.

runtime/mercury_ho_call.c:
	Update the description of how higher-order calls work.

	Update code which extracts closure arguments to take account the
	arguments being reordered in the hidden arguments vector.

runtime/mercury_deep_copy.c:
runtime/mercury_deep_copy_body.h:
runtime/mercury_layout_util.c:
runtime/mercury_ml_expand_body.h:
	Update code which extracts closure arguments to take account the
	arguments being reordered in the hidden arguments vector.

runtime/mercury_type_info.c:
runtime/mercury_type_info.h:
	Add helper function.

tools/make_spec_ho_call:
	Update the generated do_call_closure_* functions to place float
	register arguments.

tests/hard_coded/Mercury.options:
tests/hard_coded/Mmakefile:
tests/hard_coded/ho_float_reg.exp:
tests/hard_coded/ho_float_reg.m:
	Add new test case.

tests/hard_coded/copy_pred.exp:
tests/hard_coded/copy_pred.m:
tests/hard_coded/deconstruct_arg.exp:
tests/hard_coded/deconstruct_arg.exp2:
tests/hard_coded/deconstruct_arg.m:
	Extend test cases with float arguments in closures.

tests/debugger/higher_order.exp2:
	Add alternative output, changed due to closure wrapping.

tests/hard_coded/ho_univ_to_type.m:
	Adjust test case so that the float_regs pass does not report errors
	about missing higher-order insts.

compiler/notes/compiler_design.html:
	Describe the new module.

	Delete a duplicated paragraph.

compiler/notes/todo.html:
TODO:
	Delete one hundred billion year old todos.
2012-02-13 00:11:57 +00:00

197 lines
6.9 KiB
C

/*
** vim: ts=4 sw=4 expandtab
*/
/*
** Copyright (C) 1997-2006 The University of Melbourne.
** This file may only be copied under the terms of the GNU Library General
** Public License - see the file COPYING.LIB in the Mercury distribution.
*/
/*
** This module defines the MR_deep_copy() functions.
**
** Deep copy is used for a number of different purposes. Each variant
** has the same basic control structure, but differs in how memory
** is allocated, or whether forwarding pointers are left behind.
*/
#include "mercury_imp.h"
#include "mercury_builtin_types.h"
#include "mercury_deep_copy.h"
#include "mercury_type_info.h"
#include "mercury_ho_call.h"
#include "mercury_layout_util.h"
#include "mercury_memory.h"
#include "mercury_accurate_gc.h"
/*
** MR_deep_copy(): see mercury_deep_copy.h for documentation.
*/
#undef in_range
#define in_range(X) (lower_limit == NULL || \
((X) >= lower_limit && (X) <= upper_limit))
#undef copy
#define copy MR_deep_copy
#undef copy_arg
#define copy_arg MR_deep_copy_arg
#undef copy_type_info
#define copy_type_info MR_deep_copy_type_info
#undef copy_pseudo_type_info
#define copy_pseudo_type_info MR_deep_copy_pseudo_type_info
#undef copy_typeclass_info
#define copy_typeclass_info MR_deep_copy_typeclass_info
#undef if_forwarding_pointer
#define if_forwarding_pointer(Data, ACTION)
#undef leave_forwarding_pointer
#define leave_forwarding_pointer(Data, Offset, NewData)
#undef found_out_of_range_pointer
#define found_out_of_range_pointer(Data)
#include "mercury_deep_copy_body.h"
/*
** agc_deep_copy(): see mercury_deep_copy.h for documentation.
*/
#ifdef MR_NATIVE_GC
/* in_range() is true iff X is in the from-space */
#undef in_range
#define in_range(X) ((X) >= lower_limit && (X) <= upper_limit)
#undef copy
#define copy MR_agc_deep_copy
#undef copy_arg
#define copy_arg MR_agc_deep_copy_arg
#undef copy_type_info
#define copy_type_info MR_agc_deep_copy_type_info
#undef copy_pseudo_type_info
#define copy_pseudo_type_info MR_agc_deep_copy_pseudo_type_info
#undef copy_typeclass_info
#define copy_typeclass_info MR_agc_deep_copy_typeclass_info
#ifdef MR_DEBUG_AGC_FORWARDING
#define FORWARD_DEBUG_MSG(Msg, Data) \
fprintf(stderr, Msg, Data)
#else
#define FORWARD_DEBUG_MSG(Msg, Data) ((void)0)
#endif
/*
** This points to a bitmap, which is used to record which objects
** have already been copied and now hold forwarding pointers.
*/
MR_Word *MR_has_forwarding_pointer;
#define mark_as_forwarding_pointer(Data) \
do { \
size_t fwdptr_offset = (MR_Word *)(Data) - (MR_Word *) lower_limit; \
size_t fwdptr_word = fwdptr_offset / MR_WORDBITS; \
size_t fwdptr_bit = fwdptr_offset % MR_WORDBITS; \
MR_has_forwarding_pointer[fwdptr_word] |= (1 << fwdptr_bit); \
} while (0)
#undef if_forwarding_pointer
#define if_forwarding_pointer(Data, ACTION) \
do { \
size_t fwdptr_offset = (MR_Word *)(Data) - (MR_Word *) lower_limit; \
size_t fwdptr_word = fwdptr_offset / MR_WORDBITS; \
size_t fwdptr_bit = fwdptr_offset % MR_WORDBITS; \
if (MR_has_forwarding_pointer[fwdptr_word] & (1 << fwdptr_bit)) { \
ACTION; \
} \
} while (0)
#undef leave_forwarding_pointer
#define leave_forwarding_pointer(Data, Offset, NewData) \
do { \
FORWARD_DEBUG_MSG("forwarding to %lx\n", (long) NewData); \
* (((MR_Word *) Data) + Offset) = NewData; \
mark_as_forwarding_pointer(Data); \
} while (0)
#undef found_out_of_range_pointer
#define found_out_of_range_pointer(Data) \
FORWARD_DEBUG_MSG("not on this heap: %lx\n", (long) Data)
#include "mercury_deep_copy_body.h"
#endif
/*---------------------------------------------------------------------------*/
#define SWAP(val1, val2, type) \
do { \
type swap_tmp; \
swap_tmp = (val1); \
(val1) = (val2); \
(val2) = swap_tmp; \
} while (0)
/*
** The above macro won't suffice when we want to swap the pointers to the
** heap and the global heap since it will cause gcc to emit warnings about
** lvalue casts being deprecated. In that case we use the following macro.
*/
#define SWAP_HEAP_AND_GLOBAL_HEAP \
do { \
MR_Word *swap_tmp; \
swap_tmp = MR_hp; \
MR_hp_word = (MR_Word) MR_global_hp; \
MR_global_hp = swap_tmp; \
} while (0)
#ifdef MR_MIGHT_RECLAIM_HP_ON_FAILURE
/*
** MR_make_long_lived(): see mercury_deep_copy.h for documentation.
*/
MR_Word
MR_make_long_lived(MR_Word term, MR_TypeInfo type_info, MR_Word *lower_limit)
{
MR_Word result;
MR_restore_transient_hp(); /* Because we play with MR_hp */
if (lower_limit < MR_ENGINE(MR_eng_heap_zone)->MR_zone_bottom ||
lower_limit > MR_ENGINE(MR_eng_heap_zone)->MR_zone_top)
{
lower_limit = MR_ENGINE(MR_eng_heap_zone)->MR_zone_bottom;
}
/* temporarily swap the heap with the global heap */
SWAP(MR_ENGINE(MR_eng_heap_zone), MR_ENGINE(MR_eng_global_heap_zone),
MR_MemoryZone *);
SWAP_HEAP_AND_GLOBAL_HEAP;
/* copy values from the heap to the global heap */
MR_save_transient_hp();
result = MR_deep_copy(term, type_info, lower_limit,
MR_ENGINE(MR_eng_global_heap_zone)->MR_zone_top);
MR_restore_transient_hp();
/* swap the heap and global heap back again */
SWAP(MR_ENGINE(MR_eng_heap_zone), MR_ENGINE(MR_eng_global_heap_zone),
MR_MemoryZone *);
SWAP_HEAP_AND_GLOBAL_HEAP;
MR_save_transient_hp(); /* Because we played with MR_hp */
return result;
}
#endif /* MIGHT_RECLAIM_HP_ON_FAILURE */