Files
mercury/runtime/mercury_memory.c
Zoltan Somogyi e854a5f9d9 Major improvements to tabling, of two types.
Estimated hours taken: 32
Branches: main

Major improvements to tabling, of two types. The first is the implementation
of the loopcheck and memo forms of tabling for model_non procedures, and the
second is a start on the implementation of a new method of implementing
minimal model tabling, one that has the potential for a proper fix of the
problem that we currently merely detect with the pneg stack (the detection
is followed by a runtime abort). Since this new method relies on giving each
own generator its own stack, the grade component denoting it is "mmos"
(minimal model own stack). The true name of the existing method is changed
from "mm" to "mmsc" (minimal model stack copy). The grade component "mm"
is now a shorthand for "mmsc"; when the new method works, "mm" will be changed
to be a shorthand for "mmos".

configure.in:
scripts/canonical_grade.sh-subr:
scripts/init_grade_options.sh-subr:
scripts/parse_grade_options.sh-subr:
scripts/final_grade_options.sh-subr:
compiler/options.m:
	Handle the new way of handling minimal model grades.

scripts/mgnuc.in:
compiler/compile_target_code.m:
	Conform to the changes in minimal model grade options.

compiler/table_gen.m:
	Implement the transformations required by the loopcheck and memo
	tabling of model_non procedures, and the minimal model own stack
	transformation.

	The new implementation transformations use foreign_procs with extra
	args, since there is no point in implementing them both that way and
	with separate calls to library predicates. This required making the
	choice of which method to use at the top level of each transformation.

	Fix an oversight that hasn't caused problems yet but may in the future:
	mark goals wrapping the original goals as not impure for determinism
	computations.

compiler/handle_options.m:
	Handle the new arrangement of the options for minimal model tabling.
	Detect simultaneous calls for both forms of minimal model tabling,
	and generate an error message. Allow for more than one error message
	generated at once; report them all once rather than separately.

compiler/globals.m:
	Add a mechanism to allow a fix a problem detected by the changes
	to handle_options: the fact that we currently may generate a usage
	message more than once for invocations with more than one error.

compiler/mercury_compile.m:
compiler/make.program_target.m:
compiler/make.util.m:
	Use the new mechanism in handle_options to avoid generating duplicate
	usage messages.

compiler/error_util.m:
	Add a utility predicate for use by handle_options.

compiler/hlds_pred.m:
	Allow memo tabling for model_non predicates, and handle own stack
	tabling.

compiler/hlds_out.m:
	Print information about the modes of the arguments of foreign_procs,
	since this is useful in debugging transformations such as tabling
	that generate them.

compiler/prog_data.m:
compiler/layout_out.m:
compiler/prog_out.m:
runtime/mercury_stack_layout.h:
	Mention the new evaluation method.

compiler/goal_util.m:
	Change the predicates for creating calls and foreign_procs to allow
	more than one goal feature to be attached to the new goal. table_gen.m
	now uses this capability.

compiler/add_heap_ops.m:
compiler/add_trail_ops.m:
compiler/polymorphism.m:
compiler/simplify.m:
compiler/size_prof.m:
compiler/typecheck.m:
compiler/unify_proc.m:
	Conform to the changes in goal_util.

compiler/code_info.m:
compiler/make_hlds.m:
compiler/modules.m:
compiler/prog_io_pragma.m:
	Conform to the new the options controlling minimal model
	tabling.

compiler/prog_util.m:
	Add a utility predicate for use by table_gen.m.

library/std_util.m:
	Conform to the changes in the macros for minimal model tabling grades.

library/table_builtin.m:
	Add the types and predicates required by the new transformations.

	Delete an obsolete comment.

runtime/mercury_grade.h:
	Handle the new minimal model grade component.

runtime/mercury_conf_param.h:
	List macros controlling minimal model grades.

runtime/mercury_tabling.[ch]:
	Define the types needed by the new transformations,

	Implement the performance-critical predicates that need to be
	hand-written for memo tabling of model_non predicates.

	Add utility predicates for debugging.

runtime/mercury_tabling_preds.h:
	Add the implementations of the predicates required by the new
	transformations.

runtime/mercury_mm_own_stacks.[ch]:
	This new module contains the first draft of the implementation
	of the own stack implementation of minimal model tabling.

runtime/mercury_imp.h:
	Include the new file if the grade needs it.

runtime/Mmakefile:
	Mention the new files, and sort the lists of filenames.

runtime/mercury_tabling_macros.h:
	Add a macro for allocating answer blocks without requiring them to be
	pointed to directly by trie nodes.

runtime/mercury_minimal_model.[ch]:
	The structure type holding answer lists is now in mercury_tabling.h,
	since it is now also needed by memo tabling of model_non predicates.
	It no longer has a field for an answer num, because while it is ok
	to require a separate grade for debugging minimal model tabling,
	it is not ok to require a separate grade for debugging memo tabling
	of model_non predicates. Instead of printing the answer numbers,
	print the answers themselves when we need to identify solutions
	for debugging.

	Change function names, macro names, error messages etc where this is
	useful to distinguish the two kinds of minimal model tabling.

	Fix some oversights wrt transient registers.

runtime/mercury_context.[ch]:
runtime/mercury_engine.[ch]:
runtime/mercury_memory.[ch]:
runtime/mercury_wrapper.[ch]:
	With own stack tabling, each subgoal has its own context, so record
	the identity of the subgoal owning a context in the context itself.
	The main computation's context is the exception: it has no owner.

	Record not just the main context, but also the contexts of subgoals
	in the engine.

	Add variables for holding the sizes of the det and nondet stacks
	of the contexts of subgoals (which should in general be smaller
	than the sizes of the corresponding stacks of the main context),
	and initialize them as needed.

	Initialize the variables holding the sizes of the gen, cut and pneg
	stacks, even in grades where the stacks are not used, for safety.

	Fix some out-of-date documentation, and conform to our coding
	guidelines.

runtime/mercury_memory_zones.[ch]:
	Add a function to test whether a pointer is in a zone, to help
	debugging.

runtime/mercury_debug.[ch]:
	Add some functions to help debugging in the presence of multiple
	contexts, and factor out some common code to help with this.

	Delete the obsolete, unused function MR_printdetslot_as_label.

runtime/mercury_context.h:
runtime/mercury_bootstrap.h:
	Move a bootstrapping #define from mercury_context.h to
	mercury_bootstrap.h.

runtime/mercury_context.h:
runtime/mercury_bootstrap.h:
	Move a bootstrapping #define from mercury_context.h to
	mercury_bootstrap.h.

runtime/mercury_types.h:
	Add some more forward declarations of type names.

runtime/mercury_dlist.[ch]:
	Rename a field to avoid assignments that dereference NULL.

runtime/mercury_debug.c:
runtime/mercury_memory.c:
runtime/mercury_ml_expand_body.h:
runtime/mercury_stack_trace.c:
runtime/mercury_stacks.[ch]:
trace/mercury_trace_util.c
	Update uses of the macros that control minimal model tabling.

runtime/mercury_stack_trace.c:
	Provide a mechanism to allow stack traces to be suppressed entirely.
	The intention is that by using this mechanism, by the testing system
	won't have to provide separate .exp files for hlc grades, nondebug
	LLDS grades and debug LLDS grades, as we do currently. The mechanism
	is the environment variable MERCURY_SUPPRESS_STACK_TRACE.

tools/bootcheck:
tools/test_mercury:
	Specify MERCURY_SUPPRESS_STACK_TRACE.

trace/mercury_trace.c:
	When performing retries across tabled calls, handle memo tabled
	model_non predicates, for which the call table tip variable holds
	a record with a back pointer to a trie node, instead of the trie node
	itself.

trace/mercury_trace_internal.c:
	When printing tables, handle memo tabled model_non predicates. Delete
	the code now moved to runtime/mercury_tabling.c.

	Add functions for printing the data structures for own stack minimal
	model tabling.

tests/debugger/print_table.{m,inp,exp}:
	Update this test case to also test the printing of tables for
	memo tabled model_non predicates.

tests/debugger/retry.{m,inp,exp}:
	Update this test case to also test retries across memo tabled
	model_non predicates.

tests/tabling/loopcheck_nondet.{m,exp}:
tests/tabling/loopcheck_nondet_non_loop.{m,exp}:
	New test cases to test loopcheck tabled model_non predicates.
	One test case has a loop to detect, one doesn't.

tests/tabling/memo_non.{m,exp}:
tests/tabling/tc_memo.{m,exp}:
tests/tabling/tc_memo2.{m,exp}:
	New test cases to test memo tabled model_non predicates.
	One test case has a loop to detect, one has a need for minimal model
	tabling to detect, and the third doesn't have either.

tests/tabling/Mmakefile:
	Add the new test cases, and reenable the existing tc_loop test case.

	Rename some make variables and targets to make them better reflect
	their meaning.

tests/tabling/test_mercury:
	Conform to the change in the name of the make target.
2004-07-20 04:41:55 +00:00

376 lines
9.0 KiB
C

/*
** Copyright (C) 1994-2000,2002-2004 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 register array and data regions of the
** execution algorithm.
** They are defined together here to allow us to control how they map
** onto direct mapped caches.
** We allocate a large arena, preferably aligned on a boundary that
** is a multiple of both the page size and the primary cache size.
**
** We then allocate the heap and the stacks in such a way that
**
** the register array
** the bottom of the heap
** the bottom of the detstack
** the bottom of the nondstack
**
** all start at different offsets from multiples of the primary cache size.
** This should reduce cache conflicts (especially for small programs).
**
** If the operating system of the machine supports the mprotect syscall,
** we also protect a chunk at the end of each area against access,
** thus detecting area overflow.
**
** The code for handling the allocation and management of different
** memory zones is in mercury_memory_zones.{c,h}.
** The code for handling overflows and memory access errors in general
** is in mercury_memory_handlers.{c,h}.
*/
/*---------------------------------------------------------------------------*/
#include "mercury_imp.h"
/*
** This include must come before anything else that might include <signal.h>.
** See the commments in mercury_signal.h.
*/
#include "mercury_signal.h"
#ifdef MR_HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <stdio.h>
#include <string.h>
#ifdef MR_HAVE_SYS_SIGINFO_H
#include <sys/siginfo.h>
#endif
#ifdef MR_HAVE_MPROTECT
#include <sys/mman.h>
#endif
#ifdef MR_HAVE_UCONTEXT_H
#include <ucontext.h>
#endif
#ifdef MR_HAVE_SYS_UCONTEXT_H
#include <sys/ucontext.h>
#endif
#include "mercury_imp.h"
#include "mercury_trace_base.h"
#include "mercury_memory_handlers.h"
/*---------------------------------------------------------------------------*/
#if defined(MR_HAVE_SYSCONF) && defined(_SC_PAGESIZE)
#define getpagesize() sysconf(_SC_PAGESIZE)
#elif !defined(MR_HAVE_GETPAGESIZE)
#if defined(MR_WIN32_GETSYSTEMINFO)
#include <windows.h>
static size_t
getpagesize(void)
{
SYSTEM_INFO SysInfo;
GetSystemInfo(&SysInfo);
return (size_t) SysInfo.dwPageSize;
}
#else
#define getpagesize() 8192
#endif
#endif
/*---------------------------------------------------------------------------*/
#ifdef MR_HAVE_SIGINFO
static MR_bool try_munprotect(void *address, void *context);
static char *explain_context(void *context);
#endif /* MR_HAVE_SIGINFO */
/*
** Define the memory zones used by the Mercury runtime.
** (The trail zone is declared in mercury_trail.c.)
** XXX All the zones should be in mercury_engine.h
*/
#ifdef MR_USE_MINIMAL_MODEL_STACK_COPY
MR_MemoryZone *MR_genstack_zone;
MR_MemoryZone *MR_cutstack_zone;
MR_MemoryZone *MR_pnegstack_zone;
#endif
size_t MR_unit;
size_t MR_page_size;
void
MR_init_memory(void)
{
static MR_bool already_initialized = MR_FALSE;
if (already_initialized != MR_FALSE)
return;
already_initialized = MR_TRUE;
/*
** Convert all the sizes are from kilobytes to bytes and
** make sure they are multiples of the page and cache sizes.
*/
MR_page_size = getpagesize();
MR_unit = MR_max(MR_page_size, MR_pcache_size);
#ifdef MR_CONSERVATIVE_GC
MR_heap_size = 0;
MR_heap_zone_size = 0;
MR_solutions_heap_size = 0;
MR_solutions_heap_zone_size = 0;
MR_global_heap_size = 0;
MR_global_heap_zone_size = 0;
MR_debug_heap_size = 0;
MR_debug_heap_zone_size = 0;
MR_heap_margin_size = 0;
#else
MR_heap_size = MR_round_up(MR_heap_size * 1024,
MR_unit);
MR_heap_zone_size = MR_round_up(MR_heap_zone_size * 1024,
MR_unit);
MR_solutions_heap_size = MR_round_up(MR_solutions_heap_size * 1024,
MR_unit);
MR_solutions_heap_zone_size = MR_round_up(
MR_solutions_heap_zone_size * 1024,
MR_unit);
MR_global_heap_size = MR_round_up(MR_global_heap_size * 1024,
MR_unit);
MR_global_heap_zone_size = MR_round_up(MR_global_heap_zone_size * 1024,
MR_unit);
MR_debug_heap_size = MR_round_up(MR_debug_heap_size * 1024,
MR_unit);
MR_debug_heap_zone_size = MR_round_up(MR_debug_heap_zone_size * 1024,
MR_unit);
/* Note that there's no need for the heap margin to be rounded up */
MR_heap_margin_size = MR_heap_margin_size * 1024;
#endif
MR_detstack_size = MR_round_up(MR_detstack_size * 1024,
MR_unit);
MR_detstack_zone_size = MR_round_up(MR_detstack_zone_size * 1024,
MR_unit);
MR_nondstack_size = MR_round_up(MR_nondstack_size * 1024,
MR_unit);
MR_nondstack_zone_size = MR_round_up(MR_nondstack_zone_size * 1024,
MR_unit);
#ifdef MR_USE_MINIMAL_MODEL_STACK_COPY
MR_genstack_size = MR_round_up(MR_genstack_size * 1024,
MR_unit);
MR_genstack_zone_size = MR_round_up(
MR_genstack_zone_size * 1024,
MR_unit);
MR_cutstack_size = MR_round_up(MR_cutstack_size * 1024,
MR_unit);
MR_cutstack_zone_size = MR_round_up(MR_cutstack_zone_size * 1024,
MR_unit);
MR_pnegstack_size = MR_round_up(MR_pnegstack_size * 1024,
MR_unit);
MR_pnegstack_zone_size = MR_round_up(MR_pnegstack_zone_size * 1024,
MR_unit);
#else
MR_genstack_size = 0;
MR_genstack_zone_size = 0;
MR_cutstack_size = 0;
MR_cutstack_zone_size = 0;
MR_pnegstack_size = 0;
MR_pnegstack_zone_size = 0;
#endif
#ifdef MR_USE_MINIMAL_MODEL_OWN_STACKS
MR_gen_detstack_size = MR_round_up(MR_gen_detstack_size * 1024,
MR_unit);
MR_gen_nonstack_size = MR_round_up(MR_gen_nonstack_size * 1024,
MR_unit);
MR_gen_detstack_zone_size = MR_round_up(
MR_gen_detstack_zone_size * 1024,
MR_unit);
MR_gen_nonstack_zone_size = MR_round_up(
MR_gen_nonstack_zone_size * 1024,
MR_unit);
#else
MR_gen_detstack_size = 0;
MR_gen_nonstack_size = 0;
MR_gen_detstack_zone_size = 0;
MR_gen_nonstack_zone_size = 0;
#endif
#ifdef MR_USE_TRAIL
MR_trail_size = MR_round_up(MR_trail_size * 1024,
MR_unit);
MR_trail_zone_size = MR_round_up(MR_trail_zone_size * 1024,
MR_unit);
#else
MR_trail_size = 0;
MR_trail_zone_size = 0;
#endif
/*
** If the zone sizes were set to something too big, then
** set them to a single unit.
*/
#ifndef MR_CONSERVATIVE_GC
if (MR_heap_zone_size >= MR_heap_size) {
MR_heap_zone_size = MR_unit;
}
if (MR_solutions_heap_zone_size >= MR_solutions_heap_size) {
MR_solutions_heap_zone_size = MR_unit;
}
if (MR_global_heap_zone_size >= MR_global_heap_size) {
MR_global_heap_zone_size = MR_unit;
}
if (MR_heap_margin_size >= MR_heap_size) {
MR_heap_margin_size = MR_unit;
}
#endif
if (MR_detstack_zone_size >= MR_detstack_size) {
MR_detstack_zone_size = MR_unit;
}
if (MR_nondstack_zone_size >= MR_nondstack_size) {
MR_nondstack_zone_size = MR_unit;
}
#ifdef MR_USE_TRAIL
if (MR_trail_zone_size >= MR_trail_size) {
MR_trail_zone_size = MR_unit;
}
#endif
MR_init_zones();
} /* end MR_init_memory() */
/*---------------------------------------------------------------------------*/
/*
** These routines allocate memory that will NOT be scanned by the
** conservative garbage collector. You MUST NOT uses these to
** store pointers into GC'ed memory.
**
*/
void *
MR_malloc(size_t n)
{
void *ptr;
ptr = malloc(n);
if (ptr == NULL && n != 0) {
MR_fatal_error("ran out of memory");
}
return ptr;
}
void *
MR_realloc(void *old_ptr, size_t num_bytes)
{
void *ptr;
ptr = realloc(old_ptr, num_bytes);
if (ptr == NULL && num_bytes != 0) {
MR_fatal_error("ran out of memory");
}
return ptr;
}
char *
MR_copy_string(const char *s)
{
int len;
char *copy;
if (s == NULL) {
return NULL;
} else {
len = strlen(s);
copy = MR_malloc(len + 1);
strcpy(copy, s);
return copy;
}
}
/*---------------------------------------------------------------------------*/
/*
** These routines allocate memory that will be scanned by the
** conservative garbage collector.
**
** XXX This is inefficient. If MR_BOEHM_GC is enabled,
** we should set `GC_oom_fn' (see boehm_gc/gc.h) rather than
** testing the return value from GC_MALLOC() or GC_MALLOC_UNCOLLECTABLE().
*/
void *
MR_GC_malloc(size_t num_bytes)
{
void *ptr;
#ifdef MR_CONSERVATIVE_GC
ptr = GC_MALLOC(num_bytes);
#else
ptr = malloc(num_bytes);
#endif
if (ptr == NULL && num_bytes != 0) {
MR_fatal_error("could not allocate memory");
}
return ptr;
}
void *
MR_GC_malloc_uncollectable(size_t num_bytes)
{
void *ptr;
#ifdef MR_CONSERVATIVE_GC
ptr = GC_MALLOC_UNCOLLECTABLE(num_bytes);
#else
ptr = malloc(num_bytes);
#endif
if (ptr == NULL && num_bytes != 0) {
MR_fatal_error("could not allocate memory");
}
return ptr;
}
void *
MR_GC_realloc(void *old_ptr, size_t num_bytes)
{
void *ptr;
#ifdef MR_CONSERVATIVE_GC
ptr = GC_REALLOC(old_ptr, num_bytes);
#else
ptr = realloc(old_ptr, num_bytes);
#endif
if (ptr == NULL && num_bytes != 0) {
MR_fatal_error("ran out of memory");
}
return ptr;
}
/*---------------------------------------------------------------------------*/