Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding macros to push pinning roots #43

Merged
merged 4 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/interpreter.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,14 @@ extern void JL_GC_ENABLEFRAME(interpreter_state*) JL_NOTSAFEPOINT;

#else

#ifdef MMTK_GC
#define JL_GC_ENCODE_PUSHFRAME(n) ((((size_t)(n))<<3)|2)
// For roots that are not transitively pinned
#define JL_GC_ENCODE_PUSHFRAME_NO_TPIN(n) ((((size_t)(n))<<3)|6)
#else
#define JL_GC_ENCODE_PUSHFRAME(n) ((((size_t)(n))<<2)|2)
#define JL_GC_ENCODE_PUSHFRAME_NO_TPIN(n) JL_GC_ENCODE_PUSHFRAME(n)
#endif

#define JL_GC_PUSHFRAME(frame,locals,n) \
JL_CPPALLOCA(frame, sizeof(*frame)+(((n)+3)*sizeof(jl_value_t*))); \
Expand Down
95 changes: 94 additions & 1 deletion src/julia.h
Original file line number Diff line number Diff line change
Expand Up @@ -852,9 +852,40 @@ struct _jl_gcframe_t {

#define jl_pgcstack (jl_current_task->gcstack)

#ifndef MMTK_GC
#define JL_GC_ENCODE_PUSHARGS(n) (((size_t)(n))<<2)
#define JL_GC_ENCODE_PUSH(n) ((((size_t)(n))<<2)|1)

#define JL_GC_ENCODE_PUSHARGS_NO_TPIN(n) JL_GC_ENCODE_PUSHARGS(n)
#define JL_GC_ENCODE_PUSH_NO_TPIN(n) JL_GC_ENCODE_PUSH(n)
#else

// We use an extra bit (100) in the nroots value from the frame to indicate that the roots
// in the frame are/are not transitively pinning.
// There are currently 3 macros that encode passing nroots to the gcframe
// and they use the two lowest bits to encode information about what is in the frame (as below).
// To support the distinction between transtively pinning roots and non transitively pinning roots
// on the stack, we take another bit from nroots to encode information about whether or not to
// transitively pin the roots in the frame.
//
// So the ones that transitively pin look like:
// #define JL_GC_ENCODE_PUSHARGS(n) (((size_t)(n))<<3)
// #define JL_GC_ENCODE_PUSH(n) ((((size_t)(n))<<3)|1)
// #define JL_GC_ENCODE_PUSHFRAME(n) ((((size_t)(n))<<3)|2)
// and the ones that do not look like:
// #define JL_GC_ENCODE_PUSHARGS_NO_TPIN(n) (((size_t)(n))<<3|4)
// #define JL_GC_ENCODE_PUSH_NO_TPIN(n) ((((size_t)(n))<<3)|5)
// #define JL_GC_ENCODE_PUSHFRAME_NO_TPIN(n) ((((size_t)(n))<<3)|6)

// these are transitively pinning
#define JL_GC_ENCODE_PUSHARGS(n) (((size_t)(n))<<3)
#define JL_GC_ENCODE_PUSH(n) ((((size_t)(n))<<3)|1)

// these only pin the root object itself
#define JL_GC_ENCODE_PUSHARGS_NO_TPIN(n) (((size_t)(n))<<3|4)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if Julia documents this anywhere. It is a good idea to explain what those bits are used for.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There seems to be three different functions:

#define JL_GC_ENCODE_PUSHARGS(n)   (((size_t)(n))<<2)
#define JL_GC_ENCODE_PUSH(n)       ((((size_t)(n))<<2)|1)
#define JL_GC_ENCODE_PUSHFRAME(n)  ((((size_t)(n))<<2)|2)

I think they currently use 2 bits (<<2). It seems to indicate what stuff you'd push onto the shadow stack, but I might ask around so we can document this properly.

#define JL_GC_ENCODE_PUSH_NO_TPIN(n) ((((size_t)(n))<<3)|5)
#endif

#ifdef __clang_gcanalyzer__

// When running with the analyzer make these real function calls, that are
Expand Down Expand Up @@ -905,11 +936,11 @@ extern void JL_GC_POP() JL_NOTSAFEPOINT;
#define JL_GC_PUSH7(arg1, arg2, arg3, arg4, arg5, arg6, arg7) \
void *__gc_stkf[] = {(void*)JL_GC_ENCODE_PUSH(7), jl_pgcstack, arg1, arg2, arg3, arg4, arg5, arg6, arg7}; \
jl_pgcstack = (jl_gcframe_t*)__gc_stkf;

#define JL_GC_PUSH8(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8) \
void *__gc_stkf[] = {(void*)JL_GC_ENCODE_PUSH(8), jl_pgcstack, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8}; \
jl_pgcstack = (jl_gcframe_t*)__gc_stkf;


#define JL_GC_PUSHARGS(rts_var,n) \
rts_var = ((jl_value_t**)alloca(((n)+2)*sizeof(jl_value_t*)))+2; \
((void**)rts_var)[-2] = (void*)JL_GC_ENCODE_PUSHARGS(n); \
Expand All @@ -921,6 +952,68 @@ extern void JL_GC_POP() JL_NOTSAFEPOINT;

#endif

#ifdef MMTK_GC
// these are pinning roots: only the root object needs to be pinned as opposed to
// the functions above which are transitively pinning
#define JL_GC_PUSH1_NO_TPIN(arg1) \
void *__gc_stkf[] = {(void*)JL_GC_ENCODE_PUSH_NO_TPIN(1), jl_pgcstack, arg1}; \
jl_pgcstack = (jl_gcframe_t*)__gc_stkf;

#define JL_GC_PUSH2_NO_TPIN(arg1, arg2) \
void *__gc_stkf[] = {(void*)JL_GC_ENCODE_PUSH_NO_TPIN(2), jl_pgcstack, arg1, arg2}; \
jl_pgcstack = (jl_gcframe_t*)__gc_stkf;

#define JL_GC_PUSH3_NO_TPIN(arg1, arg2, arg3) \
void *__gc_stkf[] = {(void*)JL_GC_ENCODE_PUSH_NO_TPIN(3), jl_pgcstack, arg1, arg2, arg3}; \
jl_pgcstack = (jl_gcframe_t*)__gc_stkf;

#define JL_GC_PUSH4_NO_TPIN(arg1, arg2, arg3, arg4) \
void *__gc_stkf[] = {(void*)JL_GC_ENCODE_PUSH_NO_TPIN(4), jl_pgcstack, arg1, arg2, arg3, arg4}; \
jl_pgcstack = (jl_gcframe_t*)__gc_stkf;

#define JL_GC_PUSH5_NO_TPIN(arg1, arg2, arg3, arg4, arg5) \
void *__gc_stkf[] = {(void*)JL_GC_ENCODE_PUSH_NO_TPIN(5), jl_pgcstack, arg1, arg2, arg3, arg4, arg5}; \
jl_pgcstack = (jl_gcframe_t*)__gc_stkf;

#define JL_GC_PUSH6_NO_TPIN(arg1, arg2, arg3, arg4, arg5, arg6) \
void *__gc_stkf[] = {(void*)JL_GC_ENCODE_PUSH_NO_TPIN(6), jl_pgcstack, arg1, arg2, arg3, arg4, arg5, arg6}; \
jl_pgcstack = (jl_gcframe_t*)__gc_stkf;

#define JL_GC_PUSH7_NO_TPIN(arg1, arg2, arg3, arg4, arg5, arg6, arg7) \
void *__gc_stkf[] = {(void*)JL_GC_ENCODE_PUSH_NO_TPIN(7), jl_pgcstack, arg1, arg2, arg3, arg4, arg5, arg6, arg7}; \
jl_pgcstack = (jl_gcframe_t*)__gc_stkf;

#define JL_GC_PUSH8_NO_TPIN(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8) \
void *__gc_stkf[] = {(void*)JL_GC_ENCODE_PUSH_NO_TPIN(8), jl_pgcstack, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8}; \
jl_pgcstack = (jl_gcframe_t*)__gc_stkf;

#define JL_GC_PUSHARGS_NO_TPIN(rts_var,n) \
rts_var = ((jl_value_t**)alloca(((n)+2)*sizeof(jl_value_t*)))+2; \
((void**)rts_var)[-2] = (void*)JL_GC_ENCODE_PUSHARGS_NO_TPIN(n); \
((void**)rts_var)[-1] = jl_pgcstack; \
memset((void*)rts_var, 0, (n)*sizeof(jl_value_t*)); \
jl_pgcstack = (jl_gcframe_t*)&(((void**)rts_var)[-2])
#else
// When not using MMTk, default to the stock functions
#define JL_GC_PUSH1_NO_TPIN(arg1) JL_GC_PUSH1(arg1)

#define JL_GC_PUSH2_NO_TPIN(arg1, arg2) JL_GC_PUSH2(arg1, arg2)

#define JL_GC_PUSH3_NO_TPIN(arg1, arg2, arg3) JL_GC_PUSH3(arg1, arg2, arg3)

#define JL_GC_PUSH4_NO_TPIN(arg1, arg2, arg3, arg4) JL_GC_PUSH4(arg1, arg2, arg3, arg4)

#define JL_GC_PUSH5_NO_TPIN(arg1, arg2, arg3, arg4, arg5) JL_GC_PUSH5(arg1, arg2, arg3, arg4, arg5)

#define JL_GC_PUSH6_NO_TPIN(arg1, arg2, arg3, arg4, arg5, arg6) JL_GC_PUSH6(arg1, arg2, arg3, arg4, arg5, arg6)

#define JL_GC_PUSH7_NO_TPIN(arg1, arg2, arg3, arg4, arg5, arg6, arg7) JL_GC_PUSH7(arg1, arg2, arg3, arg4, arg5, arg6, arg7)

#define JL_GC_PUSH8_NO_TPIN(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8) JL_GC_PUSH8(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8)

#define JL_GC_PUSHARGS_NO_TPIN(rts_var,n) JL_GC_PUSHARGS(rts_var,n)
#endif

JL_DLLEXPORT int jl_gc_enable(int on);
JL_DLLEXPORT int jl_gc_is_enabled(void);

Expand Down