Skip to content

Commit edca252

Browse files
committed
WIP
1 parent 52aec60 commit edca252

File tree

5 files changed

+40
-28
lines changed

5 files changed

+40
-28
lines changed

base/reflection.jl

+9-6
Original file line numberDiff line numberDiff line change
@@ -495,11 +495,14 @@ end
495495

496496
# this type mirrors jl_cghooks_t (documented in julia.h)
497497
immutable CodegenHooks
498-
module_setup::Any
499-
module_activation::Any
498+
module_setup::Ptr{Void}
499+
module_activation::Ptr{Void}
500+
raise_exception::Ptr{Void}
500501

501-
CodegenHooks(;module_setup=nothing, module_activation=nothing) =
502-
new(module_setup, module_activation)
502+
CodegenHooks(;module_setup=nothing, module_activation=nothing, raise_exception=nothing) =
503+
new(pointer_from_objref(module_setup),
504+
pointer_from_objref(module_activation),
505+
pointer_from_objref(raise_exception))
503506
end
504507

505508
# this type mirrors jl_cgparams_t (documented in julia.h)
@@ -555,9 +558,9 @@ function _dump_function(linfo::Core.MethodInstance, native::Bool, wrapper::Bool,
555558
throw(ArgumentError("'syntax' must be either :intel or :att"))
556559
end
557560
if native
558-
llvmf = ccall(:jl_get_llvmf_decl, Ptr{Void}, (Any, Bool, CodegenParams, CodegenHooks), linfo, wrapper, params, params.hooks)
561+
llvmf = ccall(:jl_get_llvmf_decl, Ptr{Void}, (Any, Bool, CodegenParams), linfo, wrapper, params)
559562
else
560-
llvmf = ccall(:jl_get_llvmf_defn, Ptr{Void}, (Any, Bool, Bool, CodegenParams, CodegenHooks), linfo, wrapper, optimize, params, params.hooks)
563+
llvmf = ccall(:jl_get_llvmf_defn, Ptr{Void}, (Any, Bool, Bool, CodegenParams), linfo, wrapper, optimize, params)
561564
end
562565
if llvmf == C_NULL
563566
error("could not compile the specified method")

src/cgutils.cpp

+22-13
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,20 @@ static Value *prepare_call(Value *Callee)
4141

4242
// --- hook checks ---
4343

44-
#define JL_HOOK_TEST(params,hook) \
45-
((params)->hooks.hook != jl_nothing)
46-
47-
// NOTE: this is just a throwing version of jl_call1...
48-
#define JL_HOOK_CALL1(params,hook,arg1) \
49-
jl_value_t **argv; \
50-
JL_GC_PUSHARGS(argv, 2); \
51-
argv[0] = (params)->hooks.hook; \
52-
argv[1] = arg1; \
53-
jl_apply(argv, 2); \
44+
#define JL_HOOK_TEST(params,hook) ((params)->hooks.hook != jl_nothing)
45+
46+
#define JL_HOOK_CALL(params,hook,argc,...) \
47+
_hook_call<argc>((params)->hooks.hook, {__VA_ARGS__});
48+
template<int N>
49+
static inline void _hook_call(jl_value_t *hook, std::array<jl_value_t*,N> args) {
50+
jl_value_t **argv;
51+
JL_GC_PUSHARGS(argv, N+1);
52+
argv[0] = hook;
53+
for (int i = 0; i < N; i++)
54+
argv[i+1] = args[i];
55+
jl_apply(argv, N+1);
5456
JL_GC_POP();
57+
}
5558

5659

5760
// --- string constants ---
@@ -688,12 +691,18 @@ static void error_unless(Value *cond, const std::string &msg, jl_codectx_t *ctx)
688691
static void raise_exception(Value *exc, jl_codectx_t *ctx,
689692
BasicBlock *contBB=nullptr)
690693
{
691-
JL_FEAT_REQUIRE(ctx, runtime);
694+
if (JL_HOOK_TEST(ctx->params, raise_exception)) {
695+
JL_HOOK_CALL(ctx->params, raise_exception, 2,
696+
jl_box_voidpointer(wrap(builder.GetInsertBlock())),
697+
jl_box_voidpointer(wrap(exc)));
698+
} else {
699+
JL_FEAT_REQUIRE(ctx, runtime);
692700
#if JL_LLVM_VERSION >= 30700
693-
builder.CreateCall(prepare_call(jlthrow_func), { exc });
701+
builder.CreateCall(prepare_call(jlthrow_func), { exc });
694702
#else
695-
builder.CreateCall(prepare_call(jlthrow_func), exc);
703+
builder.CreateCall(prepare_call(jlthrow_func), exc);
696704
#endif
705+
}
697706
builder.CreateUnreachable();
698707
if (!contBB) {
699708
contBB = BasicBlock::Create(jl_LLVMContext, "after_throw", ctx->f);

src/codegen.cpp

+6-8
Original file line numberDiff line numberDiff line change
@@ -925,7 +925,7 @@ jl_llvm_functions_t jl_compile_linfo(jl_method_instance_t *li, jl_code_info_t *s
925925

926926

927927
if (JL_HOOK_TEST(params, module_activation)) {
928-
JL_HOOK_CALL1(params, module_activation, jl_box_voidpointer(wrap(m.release())));
928+
JL_HOOK_CALL(params, module_activation, 1, jl_box_voidpointer(wrap(m.release())));
929929
} else {
930930
// Step 4. Prepare debug info to receive this function
931931
// record that this function name came from this linfo,
@@ -996,7 +996,7 @@ static Value *getModuleFlag(Module *m, StringRef Key)
996996
static void jl_setup_module(Module *m, jl_cgparams_t *params = &jl_default_cgparams)
997997
{
998998
if (JL_HOOK_TEST(params, module_setup)) {
999-
JL_HOOK_CALL1(params, module_setup, jl_box_voidpointer(wrap(m)));
999+
JL_HOOK_CALL(params, module_setup, 1, jl_box_voidpointer(wrap(m)));
10001000
return;
10011001
}
10021002

@@ -1266,9 +1266,8 @@ void jl_extern_c(jl_function_t *f, jl_value_t *rt, jl_value_t *argt, char *name)
12661266
// this is paired with jl_dump_function_ir and jl_dump_function_asm in particular ways:
12671267
// misuse will leak memory or cause read-after-free
12681268
extern "C" JL_DLLEXPORT
1269-
void *jl_get_llvmf_defn(jl_method_instance_t *linfo, bool getwrapper, bool optimize, jl_cgparams_t params, jl_cghooks_t _hooks)
1269+
void *jl_get_llvmf_defn(jl_method_instance_t *linfo, bool getwrapper, bool optimize, jl_cgparams_t params)
12701270
{
1271-
params.hooks = _hooks; // ccall doesn't know how to pass the nested jl_cghooks_t
12721271
// `source` is `NULL` for generated functions.
12731272
// The `isstaged` check can be removed if that is not the case anymore.
12741273
if (linfo->def && linfo->def->source == NULL && !linfo->def->isstaged) {
@@ -1349,9 +1348,8 @@ void *jl_get_llvmf_defn(jl_method_instance_t *linfo, bool getwrapper, bool optim
13491348

13501349

13511350
extern "C" JL_DLLEXPORT
1352-
void *jl_get_llvmf_decl(jl_method_instance_t *linfo, bool getwrapper, jl_cgparams_t params, jl_cghooks_t _hooks)
1351+
void *jl_get_llvmf_decl(jl_method_instance_t *linfo, bool getwrapper, jl_cgparams_t params)
13531352
{
1354-
params.hooks = _hooks; // ccall doesn't know how to pass the nested jl_cghooks_t
13551353
// `source` is `NULL` for generated functions.
13561354
// The `isstaged` check can be removed if that is not the case anymore.
13571355
if (linfo->def && linfo->def->source == NULL && !linfo->def->isstaged) {
@@ -1409,9 +1407,9 @@ void *jl_get_llvmf(jl_tupletype_t *tt, bool getwrapper, bool getdeclarations)
14091407
}
14101408
void *f;
14111409
if (getdeclarations)
1412-
f = jl_get_llvmf_decl(linfo, getwrapper, jl_default_cgparams, jl_no_cghooks);
1410+
f = jl_get_llvmf_decl(linfo, getwrapper, jl_default_cgparams);
14131411
else
1414-
f = jl_get_llvmf_defn(linfo, getwrapper, true, jl_default_cgparams, jl_no_cghooks);
1412+
f = jl_get_llvmf_defn(linfo, getwrapper, true, jl_default_cgparams);
14151413
JL_GC_POP();
14161414
return f;
14171415
}

src/jltypes.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -3532,7 +3532,7 @@ void jl_init_types(void)
35323532
jl_type_type_mt = jl_new_method_table(jl_type_type->name->name, ptls->current_module);
35333533
jl_type_type->name->mt = jl_type_type_mt;
35343534

3535-
jl_no_cghooks = (jl_cghooks_t){jl_nothing, jl_nothing};
3535+
jl_no_cghooks = (jl_cghooks_t){jl_nothing, jl_nothing, jl_nothing};
35363536
jl_default_cgparams = (jl_cgparams_t){1, 1, 1, 1, 1, 1, 1, jl_no_cghooks};
35373537

35383538
// initialize them. lots of cycles.

src/julia.h

+2
Original file line numberDiff line numberDiff line change
@@ -1767,6 +1767,8 @@ typedef struct {
17671767
// parameters: LLVMModuleRef as Ptr{Void}
17681768
// return value: none
17691769
jl_value_t *module_activation;
1770+
1771+
jl_value_t *raise_exception;
17701772
} jl_cghooks_t;
17711773
extern JL_DLLEXPORT jl_cghooks_t jl_no_cghooks;
17721774

0 commit comments

Comments
 (0)