Skip to content

Commit

Permalink
Revert "Revert "Merge pull request #15860 from JuliaLang/kf/tracing""
Browse files Browse the repository at this point in the history
This reverts commit c734c75.
  • Loading branch information
Keno committed Apr 16, 2016
1 parent 22e3344 commit 3431b62
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 4 deletions.
2 changes: 1 addition & 1 deletion base/reflection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ uncompressed_ast(l::LambdaInfo) =
# Printing code representations in IR and assembly
function _dump_function(f, t::ANY, native, wrapper, strip_ir_metadata, dump_module)
t = tt_cons(Core.Typeof(f), to_tuple_type(t))
llvmf = ccall(:jl_get_llvmf, Ptr{Void}, (Any, Any, Bool, Bool), f, t, wrapper, native)
llvmf = ccall(:jl_get_llvmf, Ptr{Void}, (Any, Bool, Bool), t, wrapper, native)

if llvmf == C_NULL
error("no method found for the specified argument types")
Expand Down
1 change: 1 addition & 0 deletions src/alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,7 @@ JL_DLLEXPORT jl_method_t *jl_new_method_uninit(void)
m->invokes.unknown = NULL;
m->isstaged = 0;
m->needs_sparam_vals_ducttape = 2;
m->traced = 0;
return m;
}

Expand Down
5 changes: 3 additions & 2 deletions src/codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1175,14 +1175,15 @@ void jl_extern_c(jl_function_t *f, jl_value_t *rt, jl_value_t *argt, char *name)
// this is paired with jl_dump_function_ir and jl_dump_function_asm in particular ways:
// misuse will leak memory or cause read-after-free
extern "C" JL_DLLEXPORT
void *jl_get_llvmf(jl_function_t *f, jl_tupletype_t *tt, bool getwrapper, bool getdeclarations)
void *jl_get_llvmf(jl_tupletype_t *tt, bool getwrapper, bool getdeclarations)
{
jl_lambda_info_t *linfo = NULL;
JL_GC_PUSH2(&linfo, &tt);
if (tt != NULL) {
linfo = jl_get_specialization1(tt);
if (linfo == NULL) {
linfo = jl_method_lookup_by_type(jl_gf_mtable(f), tt, 0, 0);
linfo = jl_method_lookup_by_type(
((jl_datatype_t*)jl_tparam0(tt))->name->mt, tt, 0, 0);
if (linfo == NULL) {
JL_GC_POP();
return NULL;
Expand Down
1 change: 1 addition & 0 deletions src/dump.c
Original file line number Diff line number Diff line change
Expand Up @@ -1539,6 +1539,7 @@ static jl_value_t *jl_deserialize_value_(ios_t *s, jl_value_t *vtag, jl_value_t
m->invokes.unknown = jl_deserialize_value(s, (jl_value_t**)&m->invokes);
jl_gc_wb(m, m->invokes.unknown);
m->needs_sparam_vals_ducttape = read_int8(s);
m->traced = 0;
return (jl_value_t*)m;
}
else if (vtag == (jl_value_t*)jl_lambda_info_type) {
Expand Down
7 changes: 7 additions & 0 deletions src/gf.c
Original file line number Diff line number Diff line change
Expand Up @@ -1250,6 +1250,8 @@ static jl_tupletype_t *join_tsig(jl_tupletype_t *tt, jl_tupletype_t *sig)
static jl_value_t *ml_matches(union jl_typemap_t ml, int offs,
jl_tupletype_t *type, int lim);

extern void (*jl_linfo_tracer)(jl_lambda_info_t *tracee);

static jl_lambda_info_t *cache_method(jl_methtable_t *mt, union jl_typemap_t *cache, jl_value_t *parent,
jl_tupletype_t *type, jl_tupletype_t *origtype,
jl_typemap_entry_t *m, jl_svec_t *sparams)
Expand Down Expand Up @@ -1549,6 +1551,8 @@ static jl_lambda_info_t *cache_method(jl_methtable_t *mt, union jl_typemap_t *ca
}
JL_GC_POP();
JL_UNLOCK(codegen);
if (definition->traced && jl_linfo_tracer)
jl_linfo_tracer(newmeth);
return newmeth;
}

Expand Down Expand Up @@ -1693,6 +1697,7 @@ static void update_max_args(jl_methtable_t *mt, jl_tupletype_t *type)
mt->max_args = na;
}

extern void (*jl_newmeth_tracer)(jl_method_t *tracee);
void jl_method_table_insert(jl_methtable_t *mt, jl_tupletype_t *type, jl_tupletype_t *simpletype,
jl_method_t *method, jl_svec_t *tvars)
{
Expand All @@ -1711,6 +1716,8 @@ void jl_method_table_insert(jl_methtable_t *mt, jl_tupletype_t *type, jl_tuplety
check_ambiguous_matches(mt->defs, newentry);
invalidate_conflicting(&mt->cache, (jl_value_t*)type, (jl_value_t*)mt);
update_max_args(mt, type);
if (jl_newmeth_tracer)
jl_newmeth_tracer(method);
JL_SIGATOMIC_END();
}

Expand Down
25 changes: 25 additions & 0 deletions src/jlapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,31 @@ JL_DLLEXPORT const char *jl_git_commit(void)
return commit;
}

JL_DLLEXPORT void jl_trace_method(jl_method_t *m)
{
assert(jl_is_method(m));
m->traced = 1;
}

JL_DLLEXPORT void jl_untrace_method(jl_method_t *m)
{
assert(jl_is_method(m));
m->traced = 0;
}

void (*jl_linfo_tracer)(jl_lambda_info_t *tracee) = 0;
JL_DLLEXPORT void jl_register_tracer(void (*callback)(jl_lambda_info_t *tracee))
{
jl_linfo_tracer = callback;
}

void (*jl_newmeth_tracer)(jl_method_t *tracee) = 0;
JL_DLLEXPORT void jl_register_newmeth_tracer(void (*callback)(jl_method_t *tracee))
{
jl_newmeth_tracer = callback;
}


// Create function versions of some useful macros
JL_DLLEXPORT jl_taggedvalue_t *(jl_astaggedvalue)(jl_value_t *v)
{
Expand Down
7 changes: 7 additions & 0 deletions src/julia.h
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ typedef struct _jl_method_t {
// and so unspecialized will be created for each linfo instead of using linfo->def->template
// 0 = no, 1 = yes, 2 = not yet known
uint8_t needs_sparam_vals_ducttape;
uint8_t traced;
} jl_method_t;

// this holds the static data for a runnable thunk:
Expand Down Expand Up @@ -1297,6 +1298,12 @@ JL_DLLEXPORT jl_value_t *jl_interpret_toplevel_expr_in(jl_module_t *m, jl_value_
jl_lambda_info_t *lam);
JL_DLLEXPORT jl_module_t *jl_base_relative_to(jl_module_t *m);

// tracing
JL_DLLEXPORT void jl_trace_method(jl_method_t *m);
JL_DLLEXPORT void jl_untrace_method(jl_method_t *m);
JL_DLLEXPORT void jl_register_tracer(void (*callback)(jl_lambda_info_t *tracee));
JL_DLLEXPORT void jl_register_newmeth_tracer(void (*callback)(jl_method_t *tracee));

// AST access
JL_DLLEXPORT int jl_is_rest_arg(jl_value_t *ex);

Expand Down
28 changes: 27 additions & 1 deletion test/reflection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ definitely_not_in_sysimg() = nothing
for (f,t) in ((definitely_not_in_sysimg,Tuple{}),
(Base.throw_boundserror,Tuple{UnitRange{Int64},Int64}))
t = Base.tt_cons(Core.Typeof(f), Base.to_tuple_type(t))
llvmf = ccall(:jl_get_llvmf, Ptr{Void}, (Any, Any, Bool, Bool), f, t, false, true)
llvmf = ccall(:jl_get_llvmf, Ptr{Void}, (Any, Bool, Bool), t, false, true)
@test llvmf != C_NULL
@test ccall(:jl_get_llvm_fptr, Ptr{Void}, (Ptr{Void},), llvmf) != C_NULL
end
Expand Down Expand Up @@ -373,3 +373,29 @@ test_typed_ast_printing(g15714, Tuple{Vector{Float32}},
[:array_var15714, :index_var15714])
@test used_dup_var_tested15714
@test used_unique_var_tested15714

# Linfo Tracing test
tracefoo(x, y) = x+y
didtrace = false
tracer(x::Ptr{Void}) = (@test isa(unsafe_pointer_to_objref(x), LambdaInfo); global didtrace = true; nothing)
ccall(:jl_register_tracer, Void, (Ptr{Void},), cfunction(tracer, Void, (Ptr{Void},)))
meth = which(tracefoo,Tuple{Any,Any}).func
ccall(:jl_trace_method, Void, (Any,), meth)
@test tracefoo(1, 2) == 3
ccall(:jl_untrace_method, Void, (Any,), meth)
@test didtrace
didtrace = false
@test tracefoo(1.0, 2.0) == 3.0
@test !didtrace
ccall(:jl_register_tracer, Void, (Ptr{Void},), C_NULL)

# Method Tracing test
methtracer(x::Ptr{Void}) = (@test isa(unsafe_pointer_to_objref(x), Method); global didtrace = true; nothing)
ccall(:jl_register_newmeth_tracer, Void, (Ptr{Void},), cfunction(methtracer, Void, (Ptr{Void},)))
tracefoo2(x, y) = x*y
@test didtrace
didtrace = false
tracefoo(x::Int64, y::Int64) = x*y
@test didtrace
didtrace = false
ccall(:jl_register_newmeth_tracer, Void, (Ptr{Void},), C_NULL)

0 comments on commit 3431b62

Please sign in to comment.