Skip to content

Commit

Permalink
Embed codegen hooks in the params object.
Browse files Browse the repository at this point in the history
Using pointer_objref is unsafe for the GC.
  • Loading branch information
maleadt committed Sep 4, 2017
1 parent 47986fa commit 5c6f423
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 43 deletions.
26 changes: 8 additions & 18 deletions base/reflection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -723,18 +723,6 @@ function method_instances(@nospecialize(f), @nospecialize(t), world::UInt = type
return results
end

# this type mirrors jl_cghooks_t (documented in julia.h)
struct CodegenHooks
module_setup::Ptr{Void}
module_activation::Ptr{Void}
raise_exception::Ptr{Void}

CodegenHooks(;module_setup=nothing, module_activation=nothing, raise_exception=nothing) =
new(pointer_from_objref(module_setup),
pointer_from_objref(module_activation),
pointer_from_objref(raise_exception))
end

# this type mirrors jl_cgparams_t (documented in julia.h)
struct CodegenParams
cached::Cint
Expand All @@ -747,18 +735,20 @@ struct CodegenParams
dynamic_alloc::Cint
prefer_specsig::Cint

hooks::CodegenHooks
module_setup::Any
module_activation::Any
raise_exception::Any

CodegenParams(;cached::Bool=true,
runtime::Bool=true, exceptions::Bool=true,
track_allocations::Bool=true, code_coverage::Bool=true,
static_alloc::Bool=true, dynamic_alloc::Bool=true,
prefer_specsig::Bool=false, hooks::CodegenHooks=CodegenHooks()) =
prefer_specsig::Bool=false,
module_setup=nothing, module_activation=nothing, raise_exception=nothing) =
new(Cint(cached),
Cint(runtime), Cint(exceptions),
Cint(track_allocations), Cint(code_coverage),
Cint(static_alloc), Cint(dynamic_alloc),
Cint(prefer_specsig), hooks)
Cint(runtime), Cint(exceptions), Cint(track_allocations), Cint(code_coverage),
Cint(static_alloc), Cint(dynamic_alloc), Cint(prefer_specsig),
module_setup, module_activation, raise_exception)
end

# Printing code representations in IR and assembly
Expand Down
10 changes: 5 additions & 5 deletions src/cgutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,10 @@ static Value *mark_callee_rooted(IRBuilder<> &irbuilder, Value *V)

// --- hook checks ---

#define JL_HOOK_TEST(params,hook) ((params)->hooks.hook != jl_nothing)
#define JL_HOOK_TEST(params,hook) ((params)->hook != jl_nothing)

#define JL_HOOK_CALL(params,hook,argc,...) \
_hook_call<argc>((params)->hooks.hook, {{__VA_ARGS__}});
_hook_call<argc>((params)->hook, {{__VA_ARGS__}});
template<int N>
static inline void _hook_call(jl_value_t *hook, std::array<jl_value_t*,N> args) {
jl_value_t **argv;
Expand Down Expand Up @@ -2357,7 +2357,7 @@ static int compare_cgparams(const jl_cgparams_t *a, const jl_cgparams_t *b)
(a->dynamic_alloc == b->dynamic_alloc) &&
(a->prefer_specsig == b->prefer_specsig) &&
// hooks
(a->hooks.module_setup == b->hooks.module_setup) &&
(a->hooks.module_activation == b->hooks.module_activation) &&
(a->hooks.raise_exception == b->hooks.raise_exception);
(a->module_setup == b->module_setup) &&
(a->module_activation == b->module_activation) &&
(a->raise_exception == b->raise_exception);
}
8 changes: 4 additions & 4 deletions src/jltypes.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ jl_value_t *jl_memory_exception;
jl_value_t *jl_readonlymemory_exception;
union jl_typemap_t jl_cfunction_list;

jl_cgparams_t jl_default_cgparams = {1, 1, 1, 1, 1, 1, 1, 0, {NULL, NULL, NULL}};
jl_cgparams_t jl_default_cgparams = {1, 1, 1, 1, 1, 1, 1, 0, NULL, NULL, NULL};

// --- type properties and predicates ---

Expand Down Expand Up @@ -1663,9 +1663,9 @@ void jl_init_types(void)
jl_methtable_type = jl_new_uninitialized_datatype();
jl_nothing = jl_gc_permobj(0, NULL);

jl_default_cgparams.hooks.module_setup = jl_nothing;
jl_default_cgparams.hooks.module_activation = jl_nothing;
jl_default_cgparams.hooks.raise_exception = jl_nothing;
jl_default_cgparams.module_setup = jl_nothing;
jl_default_cgparams.module_activation = jl_nothing;
jl_default_cgparams.raise_exception = jl_nothing;

jl_emptysvec = (jl_svec_t*)jl_gc_permobj(sizeof(void*), jl_simplevector_type);
jl_svec_set_len_unsafe(jl_emptysvec, 0);
Expand Down
28 changes: 12 additions & 16 deletions src/julia.h
Original file line number Diff line number Diff line change
Expand Up @@ -1804,7 +1804,18 @@ typedef struct {
// codegen interface ----------------------------------------------------------

typedef struct {
// to disable a hook: set to NULL or nothing
int cached; // can the compiler use/populate the compilation cache?

int runtime; // can we call into the runtime?
int exceptions; // are exceptions supported (requires runtime)?
int track_allocations; // can we track allocations (don't if disallowed)?
int code_coverage; // can we measure coverage (don't if disallowed)?
int static_alloc; // is the compiler allowed to allocate statically?
int dynamic_alloc; // is the compiler allowed to allocate dynamically (requires runtime)?
int prefer_specsig; // are specialized function signatures preferred?


// hooks

// module setup: prepare a module for code emission (data layout, DWARF version, ...)
// parameters: LLVMModuleRef as Ptr{Void}
Expand All @@ -1820,21 +1831,6 @@ typedef struct {
// parameters: LLVMBasicBlockRef as Ptr{Void}, LLVMValueRef as Ptr{Void}
// return value: none
jl_value_t *raise_exception;
} jl_cghooks_t;

typedef struct {
int cached; // can the compiler use/populate the compilation cache?

// language features (C-style integer booleans)
int runtime; // can we call into the runtime?
int exceptions; // are exceptions supported (requires runtime)?
int track_allocations; // can we track allocations (don't if disallowed)?
int code_coverage; // can we measure coverage (don't if disallowed)?
int static_alloc; // is the compiler allowed to allocate statically?
int dynamic_alloc; // is the compiler allowed to allocate dynamically (requires runtime)?
int prefer_specsig; // are specialized function signatures preferred?

jl_cghooks_t hooks;
} jl_cgparams_t;
extern JL_DLLEXPORT jl_cgparams_t jl_default_cgparams;

Expand Down

0 comments on commit 5c6f423

Please sign in to comment.