Skip to content

Commit 1e44255

Browse files
authored
Add --serialize-machine-code-only capability (#253)
As developed by Cody Tapscott of JuliaHub, delivered via Slack at: https://relationalai.slack.com/archives/C01BC96H69W/p1759440236522629?thread_ts=1750270043.364009&cid=C01BC96H69W
1 parent 061a139 commit 1e44255

File tree

7 files changed

+35
-9
lines changed

7 files changed

+35
-9
lines changed

base/options.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ struct JLOptions
6262
safe_crash_log_file::Ptr{UInt8}
6363
task_metrics::Int8
6464
timeout_for_safepoint_straggler_s::Int16
65+
serialize_machine_code_only::Int8
6566
end
6667

6768
# This runs early in the sysimage != is not defined yet

src/gf.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2882,6 +2882,10 @@ static void _generate_from_hint(jl_method_instance_t *mi, size_t world)
28822882
if (jl_atomic_load_relaxed(&((jl_code_instance_t*)codeinst)->invoke) == jl_fptr_const_return)
28832883
return; // probably not a good idea to generate code
28842884
jl_atomic_store_relaxed(&((jl_code_instance_t*)codeinst)->precompile, 1);
2885+
if (jl_options.serialize_machine_code_only) {
2886+
// also trigger compilation so that JIT-based compilation heuristics can succeed
2887+
(void)jl_compile_method_internal(mi, world);
2888+
}
28852889
}
28862890
}
28872891

src/jloptions.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ JL_DLLEXPORT void jl_init_options(void)
9595
NULL, // safe_crash_log_file
9696
0, // task_metrics
9797
25, // timeout_for_safepoint_straggler_s
98+
0, // serialize_machine_code_only
9899
};
99100
jl_options_initialized = 1;
100101
}
@@ -245,6 +246,7 @@ JL_DLLEXPORT void jl_parse_opts(int *argcp, char ***argvp)
245246
opt_warn_scope,
246247
opt_inline,
247248
opt_polly,
249+
opt_serialize_machine_code_only,
248250
opt_timeout_for_safepoint_straggler,
249251
opt_trace_compile,
250252
opt_trace_compile_timing,
@@ -325,6 +327,7 @@ JL_DLLEXPORT void jl_parse_opts(int *argcp, char ***argvp)
325327
{ "warn-scope", required_argument, 0, opt_warn_scope },
326328
{ "inline", required_argument, 0, opt_inline },
327329
{ "polly", required_argument, 0, opt_polly },
330+
{ "serialize-machine-code-only", no_argument, 0, opt_serialize_machine_code_only },
328331
{ "timeout-for-safepoint-straggler", required_argument, 0, opt_timeout_for_safepoint_straggler },
329332
{ "trace-compile", required_argument, 0, opt_trace_compile },
330333
{ "trace-compile-timing", no_argument, 0, opt_trace_compile_timing },
@@ -893,6 +896,9 @@ JL_DLLEXPORT void jl_parse_opts(int *argcp, char ***argvp)
893896
jl_errorf("julia: --timeout-for-safepoint-straggler=<seconds>; seconds must be an integer between 1 and %d", INT16_MAX);
894897
jl_options.timeout_for_safepoint_straggler_s = (int16_t)timeout;
895898
break;
899+
case opt_serialize_machine_code_only:
900+
jl_options.serialize_machine_code_only = 1;
901+
break;
896902
case opt_task_metrics:
897903
if (!strcmp(optarg, "no"))
898904
jl_options.task_metrics = JL_OPTIONS_TASK_METRICS_OFF;

src/jloptions.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ typedef struct {
6666
const char *safe_crash_log_file;
6767
int8_t task_metrics;
6868
int16_t timeout_for_safepoint_straggler_s;
69+
int8_t serialize_machine_code_only;
6970
} jl_options_t;
7071

7172
#endif

src/precompile_utils.c

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -184,14 +184,20 @@ static int precompile_enq_specialization_(jl_method_instance_t *mi, void *closur
184184
int do_compile = 0;
185185
if (jl_atomic_load_relaxed(&codeinst->invoke) != jl_fptr_const_return) {
186186
jl_value_t *inferred = jl_atomic_load_relaxed(&codeinst->inferred);
187-
if (inferred &&
188-
inferred != jl_nothing &&
189-
jl_ir_flag_inferred(inferred) &&
190-
(jl_ir_inlining_cost(inferred) == UINT16_MAX)) {
191-
do_compile = 1;
192-
}
193-
else if (jl_atomic_load_relaxed(&codeinst->invoke) != NULL || jl_atomic_load_relaxed(&codeinst->precompile)) {
194-
do_compile = 1;
187+
if (jl_options.serialize_machine_code_only) {
188+
if (jl_atomic_load_relaxed(&codeinst->invoke) != NULL) {
189+
do_compile = 1;
190+
}
191+
} else {
192+
if (inferred &&
193+
inferred != jl_nothing &&
194+
jl_ir_flag_inferred(inferred) &&
195+
(jl_ir_inlining_cost(inferred) == UINT16_MAX)) {
196+
do_compile = 1;
197+
}
198+
else if (jl_atomic_load_relaxed(&codeinst->invoke) != NULL || jl_atomic_load_relaxed(&codeinst->precompile)) {
199+
do_compile = 1;
200+
}
195201
}
196202
}
197203
if (do_compile) {

src/staticdata_utils.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,10 @@ static jl_array_t *queue_external_cis(jl_array_t *list)
234234
continue;
235235
jl_method_instance_t *mi = ci->def;
236236
jl_method_t *m = mi->def.method;
237-
if (ci->inferred && jl_is_method(m) && jl_object_in_image((jl_value_t*)m->module)) {
237+
void *code = (jl_options.serialize_machine_code_only) ? (void *)ci->invoke : (void *)ci->inferred;
238+
int for_serialized_method = jl_is_method(m) && jl_object_in_image((jl_value_t*)m->module);
239+
int has_code = code != NULL;
240+
if (has_code && for_serialized_method) {
238241
int found = has_backedge_to_worklist(mi, &visited, &stack);
239242
assert(found == 0 || found == 1 || found == 2);
240243
assert(stack.len == 0);

test/cmdlineargs.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1132,3 +1132,8 @@ end
11321132
timeout = 120
11331133
@test parse(Int,read(`$exename --timeout-for-safepoint-straggler=$timeout -E "Base.JLOptions().timeout_for_safepoint_straggler_s"`, String)) == timeout
11341134
end
1135+
1136+
@testset "--serialize-machine-code-only" begin
1137+
exename = `$(Base.julia_cmd())`
1138+
@test parse(Int,read(`$exename --serialize-machine-code-only -E "Base.JLOptions().serialize_machine_code_only"`, String)) == 1
1139+
end

0 commit comments

Comments
 (0)