Skip to content

Commit f481fc9

Browse files
add --trace-compile-timing arg to add compile timing comments (JuliaLang#54662) (#187)
Co-authored-by: Ian Butterworth <[email protected]>
1 parent 01980b3 commit f481fc9

File tree

7 files changed

+37
-8
lines changed

7 files changed

+37
-8
lines changed

base/options.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ struct JLOptions
5858
strip_ir::Int8
5959
permalloc_pkgimg::Int8
6060
heap_size_hint::UInt64
61+
trace_compile_timing::Int8
6162
safe_crash_log_file::Ptr{UInt8}
6263
end
6364

doc/man/julia.1

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,10 @@ Generate an incremental output file (rather than complete)
266266
--trace-compile={stderr,name}
267267
Print precompile statements for methods compiled during execution or save to a path
268268

269+
.TP
270+
--trace-compile-timing=
271+
If --trace-compile is enabled show how long each took to compile in ms
272+
269273
.TP
270274
-image-codegen
271275
Force generate code in imaging mode

doc/src/manual/command-line-interface.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,8 @@ The following is a complete list of command-line switches available when launchi
140140
|`--output-bc <name>` |Generate LLVM bitcode (.bc)|
141141
|`--output-asm <name>` |Generate an assembly file (.s)|
142142
|`--output-incremental={yes\|no*}` |Generate an incremental output file (rather than complete)|
143-
|`--trace-compile={stderr,name}` |Print precompile statements for methods compiled during execution or save to a path|
143+
|`--trace-compile={stderr\|name}` |Print precompile statements for methods compiled during execution or save to a path|
144+
|`--trace-compile-timing` |If --trace-compile is enabled show how long each took to compile in ms|
144145
|`--image-codegen` |Force generate code in imaging mode|
145146
|`--heap-size-hint=<size>` |Forces garbage collection if memory usage is higher than that value. The memory hint might be specified in megabytes (e.g., 500M) or gigabytes (e.g., 1G)|
146147

src/gf.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2333,7 +2333,7 @@ jl_code_instance_t *jl_method_compiled(jl_method_instance_t *mi, size_t world)
23332333

23342334
jl_mutex_t precomp_statement_out_lock;
23352335

2336-
static void record_precompile_statement(jl_method_instance_t *mi)
2336+
static void record_precompile_statement(jl_method_instance_t *mi, double compilation_time)
23372337
{
23382338
static ios_t f_precompile;
23392339
static JL_STREAM* s_precompile = NULL;
@@ -2356,6 +2356,8 @@ static void record_precompile_statement(jl_method_instance_t *mi)
23562356
}
23572357
}
23582358
if (!jl_has_free_typevars(mi->specTypes)) {
2359+
if (jl_options.trace_compile_timing)
2360+
jl_printf(s_precompile, "#= %6.1f =# ", compilation_time / 1e6);
23592361
jl_printf(s_precompile, "precompile(");
23602362
jl_static_show(s_precompile, mi->specTypes);
23612363
jl_printf(s_precompile, ")\n");
@@ -2484,7 +2486,7 @@ jl_code_instance_t *jl_compile_method_internal(jl_method_instance_t *mi, size_t
24842486
codeinst->rettype_const = unspec->rettype_const;
24852487
jl_atomic_store_release(&codeinst->invoke, unspec_invoke);
24862488
jl_mi_cache_insert(mi, codeinst);
2487-
record_precompile_statement(mi);
2489+
record_precompile_statement(mi, 0);
24882490
return codeinst;
24892491
}
24902492
}
@@ -2501,7 +2503,7 @@ jl_code_instance_t *jl_compile_method_internal(jl_method_instance_t *mi, size_t
25012503
0, 1, ~(size_t)0, 0, 0, jl_nothing, 0);
25022504
jl_atomic_store_release(&codeinst->invoke, jl_fptr_interpret_call);
25032505
jl_mi_cache_insert(mi, codeinst);
2504-
record_precompile_statement(mi);
2506+
record_precompile_statement(mi, 0);
25052507
return codeinst;
25062508
}
25072509
if (compile_option == JL_OPTIONS_COMPILE_OFF) {
@@ -2511,8 +2513,11 @@ jl_code_instance_t *jl_compile_method_internal(jl_method_instance_t *mi, size_t
25112513
}
25122514
}
25132515

2516+
double compile_time = jl_hrtime();
25142517
int did_compile = 0;
25152518
codeinst = jl_generate_fptr(mi, world, &did_compile);
2519+
compile_time = jl_hrtime() - compile_time;
2520+
25162521
if (!codeinst) {
25172522
jl_method_instance_t *unspec = jl_get_unspecialized_from_mi(mi);
25182523
jl_code_instance_t *ucache = jl_get_method_inferred(unspec, (jl_value_t*)jl_any_type, 1, ~(size_t)0);
@@ -2553,7 +2558,7 @@ jl_code_instance_t *jl_compile_method_internal(jl_method_instance_t *mi, size_t
25532558
jl_mi_cache_insert(mi, codeinst);
25542559
}
25552560
else if (did_compile) {
2556-
record_precompile_statement(mi);
2561+
record_precompile_statement(mi, compile_time);
25572562
}
25582563
jl_atomic_store_relaxed(&codeinst->precompile, 1);
25592564
return codeinst;

src/jloptions.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ JL_DLLEXPORT void jl_init_options(void)
9191
0, // strip-ir
9292
0, // permalloc_pkgimg
9393
0, // heap-size-hint
94+
0, // trace_compile_timing
9495
NULL, // safe_crash_log_file
9596
};
9697
jl_options_initialized = 1;
@@ -213,6 +214,7 @@ static const char opts_hidden[] =
213214
" Generate an incremental output file (rather than complete)\n"
214215
" --trace-compile={stderr,name}\n"
215216
" Print precompile statements for methods compiled during execution or save to a path\n"
217+
" --trace-compile-timing If --trace-compile is enabled show how long each took to compile in ms\n"
216218
" --image-codegen Force generate code in imaging mode\n"
217219
" --permalloc-pkgimg={yes|no*} Copy the data section of package images into memory\n"
218220
;
@@ -235,6 +237,7 @@ JL_DLLEXPORT void jl_parse_opts(int *argcp, char ***argvp)
235237
opt_inline,
236238
opt_polly,
237239
opt_trace_compile,
240+
opt_trace_compile_timing,
238241
opt_trace_dispatch,
239242
opt_math_mode,
240243
opt_worker,
@@ -741,7 +744,7 @@ JL_DLLEXPORT void jl_parse_opts(int *argcp, char ***argvp)
741744
jl_errorf("julia: invalid argument to --inline (%s)", optarg);
742745
}
743746
break;
744-
case opt_polly:
747+
case opt_polly:
745748
if (!strcmp(optarg,"yes"))
746749
jl_options.polly = JL_OPTIONS_POLLY_ON;
747750
else if (!strcmp(optarg,"no"))
@@ -750,12 +753,15 @@ JL_DLLEXPORT void jl_parse_opts(int *argcp, char ***argvp)
750753
jl_errorf("julia: invalid argument to --polly (%s)", optarg);
751754
}
752755
break;
753-
case opt_trace_compile:
756+
case opt_trace_compile:
754757
jl_options.trace_compile = strdup(optarg);
755758
if (!jl_options.trace_compile)
756759
jl_errorf("fatal error: failed to allocate memory: %s", strerror(errno));
757760
break;
758-
case opt_trace_dispatch:
761+
case opt_trace_compile_timing:
762+
jl_options.trace_compile_timing = 1;
763+
break;
764+
case opt_trace_dispatch:
759765
jl_options.trace_dispatch = strdup(optarg);
760766
if (!jl_options.trace_dispatch)
761767
jl_errorf("fatal error: failed to allocate memory: %s", strerror(errno));

src/jloptions.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ typedef struct {
6262
int8_t strip_ir;
6363
int8_t permalloc_pkgimg;
6464
uint64_t heap_size_hint;
65+
int8_t trace_compile_timing;
6566
const char *safe_crash_log_file;
6667
} jl_options_t;
6768

test/cmdlineargs.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -761,6 +761,17 @@ let exename = `$(Base.julia_cmd()) --startup-file=no --color=no`
761761
@test occursin("precompile(Tuple{typeof(Main.foo), Int", _stderr)
762762
end
763763

764+
# --trace-compile-timing
765+
let
766+
io = IOBuffer()
767+
v = writereadpipeline(
768+
"foo(x) = begin Base.Experimental.@force_compile; x; end; foo(1)",
769+
`$exename --trace-compile=stderr --trace-compile-timing -i`,
770+
stderr=io)
771+
_stderr = String(take!(io))
772+
@test occursin(" ms =# precompile(Tuple{typeof(Main.foo), Int", _stderr)
773+
end
774+
764775
# --trace-dispatch
765776
let
766777
io = IOBuffer()

0 commit comments

Comments
 (0)