Skip to content

Commit

Permalink
Add --trace-dispatch
Browse files Browse the repository at this point in the history
Similar to `--trace-compile`, emit the `precompile` statement for a method
once, but only when it is dynamically dispatched.

For this, we add a `dispatched` field into `jl_method_instance_t` that is
initialized to 0, and when the method is dispatched, it is set to 1 and the
precompile statement is emitted (once).
  • Loading branch information
kpamnany committed Sep 23, 2024
1 parent 0fade45 commit e138bc3
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 3 deletions.
34 changes: 34 additions & 0 deletions src/gf.c
Original file line number Diff line number Diff line change
Expand Up @@ -2561,6 +2561,40 @@ static void record_precompile_statement(jl_method_instance_t *mi, double compila
JL_UNLOCK(&precomp_statement_out_lock);
}

jl_mutex_t dispatch_statement_out_lock;

static void record_dispatch_statement(jl_method_instance_t *mi)
{
static ios_t f_dispatch;
static JL_STREAM* s_dispatch = NULL;
jl_method_t *def = mi->def.method;
if (jl_options.trace_dispatch == NULL)
return;
if (!jl_is_method(def))
return;

JL_LOCK(&dispatch_statement_out_lock);
if (s_dispatch == NULL) {
const char *t = jl_options.trace_dispatch;
if (!strncmp(t, "stderr", 6)) {
s_dispatch = JL_STDERR;
}
else {
if (ios_file(&f_dispatch, t, 1, 1, 1, 1) == NULL)
jl_errorf("cannot open dispatch statement file \"%s\" for writing", t);
s_dispatch = (JL_STREAM*) &f_dispatch;
}
}
if (!jl_has_free_typevars(mi->specTypes)) {
jl_printf(s_dispatch, "precompile(");
jl_static_show(s_dispatch, mi->specTypes);
jl_printf(s_dispatch, ")\n");
if (s_dispatch != JL_STDERR)
ios_flush(&f_dispatch);
}
JL_UNLOCK(&dispatch_statement_out_lock);
}

// If waitcompile is 0, this will return NULL if compiling is on-going in the JIT. This is
// useful for the JIT itself, since it just doesn't cause redundant work or missed updates,
// but merely causes it to look into the current JIT worklist.
Expand Down
8 changes: 8 additions & 0 deletions src/jloptions.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ JL_DLLEXPORT void jl_init_options(void)
1, // can_inline
JL_OPTIONS_POLLY_ON, // polly
NULL, // trace_compile
NULL, // trace_dispatch
JL_OPTIONS_FAST_MATH_DEFAULT,
0, // worker
NULL, // cookie
Expand Down Expand Up @@ -286,6 +287,7 @@ JL_DLLEXPORT void jl_parse_opts(int *argcp, char ***argvp)
opt_polly,
opt_trace_compile,
opt_trace_compile_timing,
opt_trace_dispatch,
opt_math_mode,
opt_worker,
opt_bind_to,
Expand Down Expand Up @@ -363,6 +365,7 @@ JL_DLLEXPORT void jl_parse_opts(int *argcp, char ***argvp)
{ "polly", required_argument, 0, opt_polly },
{ "trace-compile", required_argument, 0, opt_trace_compile },
{ "trace-compile-timing", no_argument, 0, opt_trace_compile_timing },
{ "trace-dispatch", required_argument, 0, opt_trace_dispatch },
{ "math-mode", required_argument, 0, opt_math_mode },
{ "handle-signals", required_argument, 0, opt_handle_signals },
// hidden command line options
Expand Down Expand Up @@ -818,6 +821,11 @@ JL_DLLEXPORT void jl_parse_opts(int *argcp, char ***argvp)
case opt_trace_compile_timing:
jl_options.trace_compile_timing = 1;
break;
case opt_trace_dispatch:
jl_options.trace_dispatch = strdup(optarg);
if (!jl_options.trace_dispatch)
jl_errorf("fatal error: failed to allocate memory: %s", strerror(errno));
break;
case opt_math_mode:
if (!strcmp(optarg,"ieee"))
jl_options.fast_math = JL_OPTIONS_FAST_MATH_OFF;
Expand Down
1 change: 1 addition & 0 deletions src/jloptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ typedef struct {
int8_t can_inline;
int8_t polly;
const char *trace_compile;
const char *trace_dispatch;
int8_t fast_math;
int8_t worker;
const char *cookie;
Expand Down
8 changes: 5 additions & 3 deletions src/jltypes.c
Original file line number Diff line number Diff line change
Expand Up @@ -3610,21 +3610,23 @@ void jl_init_types(void) JL_GC_DISABLED
jl_method_instance_type =
jl_new_datatype(jl_symbol("MethodInstance"), core,
jl_any_type, jl_emptysvec,
jl_perm_symsvec(7,
jl_perm_symsvec(8,
"def",
"specTypes",
"sparam_vals",
"backedges",
"cache",
"cache_with_orig",
"precompiled"),
jl_svec(7,
"precompiled",
"dispatched"),
jl_svec(8,
jl_new_struct(jl_uniontype_type, jl_method_type, jl_module_type),
jl_any_type,
jl_simplevector_type,
jl_array_any_type,
jl_any_type/*jl_code_instance_type*/,
jl_bool_type,
jl_bool_type,
jl_bool_type),
jl_emptysvec,
0, 1, 3);
Expand Down
1 change: 1 addition & 0 deletions src/julia.h
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,7 @@ struct _jl_method_instance_t {
_Atomic(struct _jl_code_instance_t*) cache;
uint8_t cache_with_orig; // !cache_with_specTypes
_Atomic(uint8_t) precompiled; // true if this instance was generated by an explicit `precompile(...)` call
uint8_t dispatched; // true if this instance was dispatched to
};

// OpaqueClosure
Expand Down
1 change: 1 addition & 0 deletions src/method.c
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,7 @@ JL_DLLEXPORT jl_method_instance_t *jl_new_method_instance_uninit(void)
jl_atomic_store_relaxed(&mi->cache, NULL);
mi->cache_with_orig = 0;
jl_atomic_store_relaxed(&mi->precompiled, 0);
mi->dispatched = 0;
return mi;
}

Expand Down

0 comments on commit e138bc3

Please sign in to comment.