Skip to content

Commit 98a66ba

Browse files
authored
Embed mutator in _jl_tls_states_t (#16)
This PR embeds the MMTk mutator struct in `_jl_tls_states_t`, and also adds `jl_deinit_thread_heap` to allow a proper destruction of the mutator struct.
1 parent fb024c6 commit 98a66ba

File tree

8 files changed

+37
-15
lines changed

8 files changed

+37
-15
lines changed

src/gc.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3501,6 +3501,11 @@ void jl_init_thread_heap(jl_ptls_t ptls)
35013501
jl_atomic_store_relaxed(&ptls->gc_num.allocd, -(int64_t)gc_num.interval);
35023502
}
35033503

3504+
void jl_deinit_thread_heap(jl_ptls_t ptls)
3505+
{
3506+
// Do nothing
3507+
}
3508+
35043509
// System-wide initializations
35053510
void jl_gc_init(void)
35063511
{

src/julia.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2391,7 +2391,7 @@ STATIC_INLINE void mmtk_gc_wb_full(const void *parent, const void *ptr) JL_NOTSA
23912391
{
23922392
jl_task_t *ct = jl_current_task;
23932393
jl_ptls_t ptls = ct->ptls;
2394-
mmtk_object_reference_write_post(ptls->mmtk_mutator_ptr, parent, ptr);
2394+
mmtk_object_reference_write_post(&ptls->mmtk_mutator, parent, ptr);
23952395
}
23962396

23972397
// Inlined fastpath
@@ -2405,7 +2405,7 @@ STATIC_INLINE void mmtk_gc_wb_fast(const void *parent, const void *ptr) JL_NOTSA
24052405
if (((byte_val >> shift) & 1) == 1) {
24062406
jl_task_t *ct = jl_current_task;
24072407
jl_ptls_t ptls = ct->ptls;
2408-
mmtk_object_reference_write_slow(ptls->mmtk_mutator_ptr, parent, ptr);
2408+
mmtk_object_reference_write_slow(&ptls->mmtk_mutator, parent, ptr);
24092409
}
24102410
}
24112411
}

src/julia_internal.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,7 @@ STATIC_INLINE jl_value_t *jl_gc_permobj(size_t sz, void *ty) JL_NOTSAFEPOINT
550550
o->header = tag | GC_OLD_MARKED;
551551
#ifdef MMTK_GC
552552
jl_ptls_t ptls = jl_current_task->ptls;
553-
mmtk_post_alloc(ptls->mmtk_mutator_ptr, jl_valueof(o), allocsz, 1);
553+
mmtk_post_alloc(&ptls->mmtk_mutator, jl_valueof(o), allocsz, 1);
554554
#endif
555555
return jl_valueof(o);
556556
}
@@ -918,6 +918,7 @@ void jl_init_serializer(void);
918918
void jl_gc_init(void);
919919
void jl_init_uv(void);
920920
void jl_init_thread_heap(jl_ptls_t ptls) JL_NOTSAFEPOINT;
921+
void jl_deinit_thread_heap(jl_ptls_t ptls) JL_NOTSAFEPOINT;
921922
void jl_init_int32_int64_cache(void);
922923
JL_DLLEXPORT void jl_init_options(void);
923924

src/julia_threads.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -282,9 +282,7 @@ typedef struct _jl_tls_states_t {
282282
)
283283

284284
#ifdef MMTK_GC
285-
MMTkMutatorContext* mmtk_mutator_ptr;
286-
void* cursor;
287-
void* limit;
285+
MMTkMutatorContext mmtk_mutator;
288286
#endif
289287

290288
// some hidden state (usually just because we don't have the type's size declaration)

src/llvm-final-gc-lowering.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -280,17 +280,23 @@ Value *FinalLowerGC::lowerGCAllocBytes(CallInst *target, Function &F)
280280
#else // MMTK_GC
281281
auto pool_osize_i32 = ConstantInt::get(Type::getInt32Ty(F.getContext()), osize);
282282
auto pool_osize = ConstantInt::get(Type::getInt64Ty(F.getContext()), osize);
283-
auto cursor_pos = ConstantInt::get(Type::getInt64Ty(target->getContext()), offsetof(jl_tls_states_t, cursor));
284-
auto limit_pos = ConstantInt::get(Type::getInt64Ty(target->getContext()), offsetof(jl_tls_states_t, limit));
283+
284+
// Assuming we use the first immix allocator.
285+
// FIXME: We should get the allocator index and type from MMTk.
286+
auto allocator_offset = offsetof(jl_tls_states_t, mmtk_mutator) + offsetof(MMTkMutatorContext, allocators) + offsetof(Allocators, immix);
287+
288+
auto cursor_pos = ConstantInt::get(Type::getInt64Ty(target->getContext()), allocator_offset + offsetof(ImmixAllocator, cursor));
289+
auto limit_pos = ConstantInt::get(Type::getInt64Ty(target->getContext()), allocator_offset + offsetof(ImmixAllocator, limit));
285290

286291
auto cursor_tls_i8 = builder.CreateGEP(Type::getInt8Ty(target->getContext()), ptls, cursor_pos);
287292
auto cursor_ptr = builder.CreateBitCast(cursor_tls_i8, PointerType::get(Type::getInt64Ty(target->getContext()), 0), "cursor_ptr");
288293
auto cursor = builder.CreateLoad(Type::getInt64Ty(target->getContext()), cursor_ptr, "cursor");
289294

290-
295+
// offset = 8
291296
auto delta_offset = builder.CreateNSWSub(ConstantInt::get(Type::getInt64Ty(target->getContext()), 0), ConstantInt::get(Type::getInt64Ty(target->getContext()), 8));
292297
auto delta_cursor = builder.CreateNSWSub(ConstantInt::get(Type::getInt64Ty(target->getContext()), 0), cursor);
293298
auto delta_op = builder.CreateNSWAdd(delta_offset, delta_cursor);
299+
// alignment 16 (15 = 16 - 1)
294300
auto delta = builder.CreateAnd(delta_op, ConstantInt::get(Type::getInt64Ty(target->getContext()), 15), "delta");
295301
auto result = builder.CreateNSWAdd(cursor, delta, "result");
296302

src/mmtk-gc.c

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -266,8 +266,17 @@ void jl_init_thread_heap(jl_ptls_t ptls)
266266
memset(&ptls->gc_num, 0, sizeof(ptls->gc_num));
267267
jl_atomic_store_relaxed(&ptls->gc_num.allocd, -(int64_t)gc_num.interval);
268268

269+
// Create mutator
269270
MMTk_Mutator mmtk_mutator = mmtk_bind_mutator((void *)ptls, ptls->tid);
270-
ptls->mmtk_mutator_ptr = ((MMTkMutatorContext*)mmtk_mutator);
271+
// Copy the mutator to the thread local storage
272+
memcpy(&ptls->mmtk_mutator, mmtk_mutator, sizeof(MMTkMutatorContext));
273+
// Call post_bind to maintain a list of active mutators and to reclaim the old mutator (which is no longer needed)
274+
mmtk_post_bind_mutator(&ptls->mmtk_mutator, mmtk_mutator);
275+
}
276+
277+
void jl_deinit_thread_heap(jl_ptls_t ptls)
278+
{
279+
mmtk_destroy_mutator(&ptls->mmtk_mutator);
271280
}
272281

273282
// System-wide initialization
@@ -506,7 +515,7 @@ void disable_collection(void)
506515
JL_DLLEXPORT void jl_gc_array_ptr_copy(jl_array_t *dest, void **dest_p, jl_array_t *src, void **src_p, ssize_t n) JL_NOTSAFEPOINT
507516
{
508517
jl_ptls_t ptls = jl_current_task->ptls;
509-
mmtk_memory_region_copy(ptls->mmtk_mutator_ptr, jl_array_owner(src), src_p, jl_array_owner(dest), dest_p, n);
518+
mmtk_memory_region_copy(&ptls->mmtk_mutator, jl_array_owner(src), src_p, jl_array_owner(dest), dest_p, n);
510519
}
511520

512521
// No inline write barrier -- only used for debugging
@@ -524,20 +533,20 @@ JL_DLLEXPORT void jl_gc_wb1_slow(const void *parent) JL_NOTSAFEPOINT
524533
{
525534
jl_task_t *ct = jl_current_task;
526535
jl_ptls_t ptls = ct->ptls;
527-
mmtk_object_reference_write_slow(ptls->mmtk_mutator_ptr, parent, (const void*) 0);
536+
mmtk_object_reference_write_slow(&ptls->mmtk_mutator, parent, (const void*) 0);
528537
}
529538

530539
JL_DLLEXPORT void jl_gc_wb2_slow(const void *parent, const void* ptr) JL_NOTSAFEPOINT
531540
{
532541
jl_task_t *ct = jl_current_task;
533542
jl_ptls_t ptls = ct->ptls;
534-
mmtk_object_reference_write_slow(ptls->mmtk_mutator_ptr, parent, ptr);
543+
mmtk_object_reference_write_slow(&ptls->mmtk_mutator, parent, ptr);
535544
}
536545

537546
void *jl_gc_perm_alloc_nolock(size_t sz, int zero, unsigned align, unsigned offset)
538547
{
539548
jl_ptls_t ptls = jl_current_task->ptls;
540-
void* addr = mmtk_alloc(ptls->mmtk_mutator_ptr, sz, align, offset, 1);
549+
void* addr = mmtk_alloc(&ptls->mmtk_mutator, sz, align, offset, 1);
541550
return addr;
542551
}
543552

src/symbol.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ static jl_sym_t *mk_symbol(const char *str, size_t len) JL_NOTSAFEPOINT
4141
jl_set_typetagof(sym, jl_symbol_tag, GC_OLD_MARKED);
4242
#ifdef MMTK_GC
4343
jl_ptls_t ptls = jl_current_task->ptls;
44-
mmtk_post_alloc(ptls->mmtk_mutator_ptr, jl_valueof(tag), nb, 1);
44+
mmtk_post_alloc(&ptls->mmtk_mutator, jl_valueof(tag), nb, 1);
4545
#endif
4646
jl_atomic_store_relaxed(&sym->left, NULL);
4747
jl_atomic_store_relaxed(&sym->right, NULL);

src/threading.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,9 @@ static void jl_delete_thread(void *value) JL_NOTSAFEPOINT_ENTER
478478
#else
479479
pthread_mutex_unlock(&in_signal_lock);
480480
#endif
481+
482+
jl_deinit_thread_heap(ptls);
483+
481484
// then park in safe-region
482485
(void)jl_gc_safe_enter(ptls);
483486
}

0 commit comments

Comments
 (0)