From d907a0630b4551cfbb321366da6e96242ada25d1 Mon Sep 17 00:00:00 2001 From: Yi Lin Date: Fri, 10 May 2024 15:51:14 +1200 Subject: [PATCH] Port PR #42 and #44 to master (#48) * Call initialize_collection before enabling GC (#44) * Fix build with stock GC: mmtk_pin_object is conditionaly compiled (#42) --- src/array.c | 6 +++--- src/builtins.c | 6 +++--- src/datatype.c | 4 ++-- src/init.c | 6 +++--- src/julia.h | 7 +++++-- src/julia_internal.h | 18 +++++++++++++++++- 6 files changed, 33 insertions(+), 14 deletions(-) diff --git a/src/array.c b/src/array.c index bc73f582d63fa..2877604f7a900 100644 --- a/src/array.c +++ b/src/array.c @@ -243,7 +243,7 @@ JL_DLLEXPORT jl_array_t *jl_reshape_array(jl_value_t *atype, jl_array_t *data, // introspect the object to update the a->data field. To avoid doing that and // making scan_object much more complex we simply enforce that both owner and // buffers are always pinned - mmtk_pin_object(owner); + PTR_PIN(owner); a->flags.how = 3; a->data = data->data; a->flags.isshared = 1; @@ -296,7 +296,7 @@ JL_DLLEXPORT jl_array_t *jl_string_to_array(jl_value_t *str) // introspect the object to update the a->data field. To avoid doing that and // making scan_object much more complex we simply enforce that both owner and // buffers are always pinned - mmtk_pin_object(str); + PTR_PIN(str); a->flags.how = 3; a->flags.isshared = 1; size_t l = jl_string_len(str); @@ -695,7 +695,7 @@ static int NOINLINE array_resize_buffer(jl_array_t *a, size_t newlen) // introspect the object to update the a->data field. To avoid doing that and // making scan_object much more complex we simply enforce that both owner and // buffers are always pinned - mmtk_pin_object(s); + PTR_PIN(s); jl_array_data_owner(a) = s; jl_gc_wb(a, s); a->data = jl_string_data(s); diff --git a/src/builtins.c b/src/builtins.c index 0a2cc9cd42729..d961f36cbc707 100644 --- a/src/builtins.c +++ b/src/builtins.c @@ -346,7 +346,7 @@ static uintptr_t type_object_id_(jl_value_t *v, jl_varidx_t *env) JL_NOTSAFEPOIN } // FIXME: Pinning objects that get hashed // until we implement address space hashing. - mmtk_pin_object(v); + PTR_PIN(v); return inthash((uintptr_t)v); } if (tv == jl_uniontype_type) { @@ -398,7 +398,7 @@ static uintptr_t immut_id_(jl_datatype_t *dt, jl_value_t *v, uintptr_t h) JL_NOT // FIXME: Pinning objects that get hashed // until we implement address space hashing. - mmtk_pin_object(v); + PTR_PIN(v); // operate element-wise if there are unused bits inside, // otherwise just take the whole data block at once // a few select pointers (notably symbol) also have special hash values @@ -462,7 +462,7 @@ static uintptr_t NOINLINE jl_object_id__cold(jl_datatype_t *dt, jl_value_t *v) J if (dt->name->mutabl) { // FIXME: Pinning objects that get hashed // until we implement address space hashing. - mmtk_pin_object(v); + PTR_PIN(v); return inthash((uintptr_t)v); } return immut_id_(dt, v, dt->hash); diff --git a/src/datatype.c b/src/datatype.c index 9e6d480985c69..ae8853f37c688 100644 --- a/src/datatype.c +++ b/src/datatype.c @@ -67,7 +67,7 @@ JL_DLLEXPORT jl_typename_t *jl_new_typename_in(jl_sym_t *name, jl_module_t *modu jl_typename_type); // Typenames should be pinned since they are used as metadata, and are // read during scan_object - mmtk_pin_object(tn); + PTR_PIN(tn); tn->name = name; tn->module = module; tn->wrapper = NULL; @@ -101,7 +101,7 @@ jl_datatype_t *jl_new_uninitialized_datatype(void) jl_datatype_t *t = (jl_datatype_t*)jl_gc_alloc(ct->ptls, sizeof(jl_datatype_t), jl_datatype_type); // Types should be pinned since they are used as metadata, and are // read during scan_object - mmtk_pin_object(t); + PTR_PIN(t); jl_set_typetagof(t, jl_datatype_tag, 0); t->hash = 0; t->hasfreetypevars = 0; diff --git a/src/init.c b/src/init.c index 8a379a5922f5a..faa446a34cf22 100644 --- a/src/init.c +++ b/src/init.c @@ -833,9 +833,6 @@ JL_DLLEXPORT void julia_init(JL_IMAGE_SEARCH rel) #pragma GCC diagnostic pop JL_GC_PROMISE_ROOTED(ct); _finish_julia_init(rel, ptls, ct); -#ifdef MMTK_GC - mmtk_initialize_collection((void *)ptls); -#endif } static NOINLINE void _finish_julia_init(JL_IMAGE_SEARCH rel, jl_ptls_t ptls, jl_task_t *ct) @@ -883,6 +880,9 @@ static NOINLINE void _finish_julia_init(JL_IMAGE_SEARCH rel, jl_ptls_t ptls, jl_ } jl_start_threads(); +#ifdef MMTK_GC + mmtk_initialize_collection((void *)ptls); +#endif jl_gc_enable(1); if (jl_options.image_file && (!jl_generating_output() || jl_options.incremental) && jl_module_init_order) { diff --git a/src/julia.h b/src/julia.h index 3a33e59e3835a..ed6305715a87c 100644 --- a/src/julia.h +++ b/src/julia.h @@ -11,8 +11,11 @@ extern int mmtk_object_is_managed_by_mmtk(void* addr); extern unsigned char mmtk_pin_object(void* obj); // FIXME: Pinning objects that get hashed in the ptrhash table // until we implement address space hashing. -#define PTRHASH_PIN(key) \ - mmtk_pin_object(key); \ +#ifdef MMTK_GC +#define PTRHASH_PIN(key) mmtk_pin_object(key); +#else +#define PTRHASH_PIN(key) +#endif #ifdef __cplusplus } diff --git a/src/julia_internal.h b/src/julia_internal.h index a8593a23a4e40..25983ea6c0d27 100644 --- a/src/julia_internal.h +++ b/src/julia_internal.h @@ -3,6 +3,22 @@ #ifndef JL_INTERNAL_H #define JL_INTERNAL_H +#ifdef __cplusplus +extern "C" { +#endif + +extern int mmtk_object_is_managed_by_mmtk(void* addr); +extern unsigned char mmtk_pin_object(void* obj); +#ifdef MMTK_GC +#define PTR_PIN(key) mmtk_pin_object(key); +#else +#define PTR_PIN(key) +#endif + +#ifdef __cplusplus +} +#endif + #include "options.h" #include "julia_assert.h" #include "julia_locks.h" @@ -535,7 +551,7 @@ STATIC_INLINE jl_gc_tracked_buffer_t *jl_gc_alloc_buf(jl_ptls_t ptls, size_t sz) // introspect the object to update the a->data field. To avoid doing that and // making scan_object much more complex we simply enforce that both owner and // buffers are always pinned - mmtk_pin_object(buf); + PTR_PIN(buf); return buf; }