Skip to content

Commit 80de59a

Browse files
committed
Force compilation with Expr(:meta, :compile, :force)
This more explicit directive distinguishes `@compile` (now moved to Experimental) from `@compiler_options` (which uses the same `:compile` symbol). It eliminates a collision between #42128 and #37041. Fixes #42373 Closes #42379
1 parent 84cc901 commit 80de59a

File tree

6 files changed

+53
-32
lines changed

6 files changed

+53
-32
lines changed

base/experimental.jl

+24
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,30 @@ macro compiler_options(args...)
162162
return opts
163163
end
164164

165+
"""
166+
Experimental.@compile
167+
168+
Force compilation of the block or function (Julia's built-in interpreter is blocked from executing it).
169+
170+
# Examples
171+
172+
```
173+
module WithPrecompiles
174+
#=
175+
code definitions
176+
=#
177+
178+
if Sys.iswindows()
179+
Experimental.@compile
180+
compile_me() # `compile_me` will be compiled before execution (not run in the interpreter)
181+
end
182+
183+
end
184+
```
185+
"""
186+
macro compile() Expr(:meta, :compile, :force) end
187+
188+
165189
# UI features for errors
166190

167191
"""

base/expr.jl

-7
Original file line numberDiff line numberDiff line change
@@ -384,13 +384,6 @@ macro propagate_inbounds(ex)
384384
esc(ex)
385385
end
386386

387-
"""
388-
@compile
389-
390-
Force compilation of the block or function (Julia's built-in interpreter is blocked from executing it).
391-
"""
392-
macro compile() Expr(:meta, :compile) end
393-
394387
"""
395388
@polly
396389

base/timing.jl

+5-5
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ julia> @time begin
213213
"""
214214
macro time(ex)
215215
quote
216-
@compile
216+
Experimental.@compile
217217
local stats = gc_num()
218218
local elapsedtime = time_ns()
219219
local compile_elapsedtime = cumulative_compile_time_ns_before()
@@ -260,7 +260,7 @@ pool allocs: 1
260260
"""
261261
macro timev(ex)
262262
quote
263-
@compile
263+
Experimental.@compile
264264
local stats = gc_num()
265265
local elapsedtime = time_ns()
266266
local compile_elapsedtime = cumulative_compile_time_ns_before()
@@ -294,7 +294,7 @@ julia> @elapsed sleep(0.3)
294294
"""
295295
macro elapsed(ex)
296296
quote
297-
@compile
297+
Experimental.@compile
298298
local t0 = time_ns()
299299
$(esc(ex))
300300
(time_ns() - t0) / 1e9
@@ -326,7 +326,7 @@ julia> @allocated rand(10^6)
326326
"""
327327
macro allocated(ex)
328328
quote
329-
@compile
329+
Experimental.@compile
330330
local b0 = Ref{Int64}(0)
331331
local b1 = Ref{Int64}(0)
332332
gc_bytes(b0)
@@ -374,7 +374,7 @@ julia> stats.gcstats.total_time
374374
"""
375375
macro timed(ex)
376376
quote
377-
@compile
377+
Experimental.@compile
378378
local stats = gc_num()
379379
local elapsedtime = time_ns()
380380
local val = $(esc(ex))

src/ast.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ jl_sym_t *coverageeffect_sym; jl_sym_t *escape_sym;
6868
jl_sym_t *aliasscope_sym; jl_sym_t *popaliasscope_sym;
6969
jl_sym_t *optlevel_sym; jl_sym_t *thismodule_sym;
7070
jl_sym_t *atom_sym; jl_sym_t *statement_sym; jl_sym_t *all_sym;
71-
jl_sym_t *compile_sym; jl_sym_t *infer_sym;
71+
jl_sym_t *compile_sym; jl_sym_t *infer_sym; jl_sym_t *force_sym;
7272

7373
jl_sym_t *atomic_sym;
7474
jl_sym_t *not_atomic_sym;
@@ -406,6 +406,7 @@ void jl_init_common_symbols(void)
406406
optlevel_sym = jl_symbol("optlevel");
407407
compile_sym = jl_symbol("compile");
408408
infer_sym = jl_symbol("infer");
409+
force_sym = jl_symbol("force");
409410
macrocall_sym = jl_symbol("macrocall");
410411
escape_sym = jl_symbol("escape");
411412
hygienicscope_sym = jl_symbol("hygienic-scope");

src/julia_internal.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -1411,7 +1411,7 @@ extern jl_sym_t *throw_undef_if_not_sym; extern jl_sym_t *getfield_undefref_sym;
14111411
extern jl_sym_t *gc_preserve_begin_sym; extern jl_sym_t *gc_preserve_end_sym;
14121412
extern jl_sym_t *coverageeffect_sym; extern jl_sym_t *escape_sym;
14131413
extern jl_sym_t *optlevel_sym; extern jl_sym_t *compile_sym;
1414-
extern jl_sym_t *infer_sym;
1414+
extern jl_sym_t *infer_sym; extern jl_sym_t *force_sym;
14151415
extern jl_sym_t *atom_sym; extern jl_sym_t *statement_sym; extern jl_sym_t *all_sym;
14161416

14171417
extern jl_sym_t *atomic_sym;

src/toplevel.c

+21-18
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ JL_DLLEXPORT jl_module_t *jl_base_relative_to(jl_module_t *m)
304304
return jl_top_module;
305305
}
306306

307-
static void expr_attributes(jl_value_t *v, int *has_intrinsics, int *has_defs, int *has_opaque)
307+
static void expr_attributes(jl_value_t *v, int *has_intrinsics, int *has_defs, int *has_opaque, int *has_forcedcompile)
308308
{
309309
if (!jl_is_expr(v))
310310
return;
@@ -339,6 +339,12 @@ static void expr_attributes(jl_value_t *v, int *has_intrinsics, int *has_defs, i
339339
*has_opaque = 1;
340340
return;
341341
}
342+
else if (head == meta_sym && jl_expr_nargs(e) == 2 &&
343+
jl_exprarg(e, 0) == (jl_value_t*)compile_sym &&
344+
jl_exprarg(e, 1) == (jl_value_t*)force_sym) {
345+
*has_forcedcompile = 1;
346+
return;
347+
}
342348
else if (head == call_sym && jl_expr_nargs(e) > 0) {
343349
jl_value_t *called = NULL;
344350
jl_value_t *f = jl_exprarg(e, 0);
@@ -368,7 +374,7 @@ static void expr_attributes(jl_value_t *v, int *has_intrinsics, int *has_defs, i
368374
for (i = 0; i < jl_array_len(e->args); i++) {
369375
jl_value_t *a = jl_exprarg(e, i);
370376
if (jl_is_expr(a))
371-
expr_attributes(a, has_intrinsics, has_defs, has_opaque);
377+
expr_attributes(a, has_intrinsics, has_defs, has_opaque, has_forcedcompile);
372378
}
373379
}
374380

@@ -377,19 +383,17 @@ int jl_code_requires_compiler(jl_code_info_t *src)
377383
jl_array_t *body = src->code;
378384
assert(jl_typeis(body, jl_array_any_type));
379385
size_t i;
380-
int has_intrinsics = 0, has_defs = 0, has_opaque = 0;
381-
if (jl_has_meta(body, compile_sym))
382-
return 1;
386+
int has_intrinsics = 0, has_defs = 0, has_opaque = 0, has_forcedcompile = 0;
383387
for(i=0; i < jl_array_len(body); i++) {
384388
jl_value_t *stmt = jl_array_ptr_ref(body,i);
385-
expr_attributes(stmt, &has_intrinsics, &has_defs, &has_opaque);
386-
if (has_intrinsics)
389+
expr_attributes(stmt, &has_intrinsics, &has_defs, &has_opaque, &has_forcedcompile);
390+
if (has_intrinsics || has_forcedcompile)
387391
return 1;
388392
}
389393
return 0;
390394
}
391395

392-
static void body_attributes(jl_array_t *body, int *has_intrinsics, int *has_defs, int *has_loops, int *has_opaque, int *has_compile)
396+
static void body_attributes(jl_array_t *body, int *has_intrinsics, int *has_defs, int *has_loops, int *has_opaque, int *has_forcedcompile)
393397
{
394398
size_t i;
395399
*has_loops = 0;
@@ -405,9 +409,8 @@ static void body_attributes(jl_array_t *body, int *has_intrinsics, int *has_defs
405409
*has_loops = 1;
406410
}
407411
}
408-
expr_attributes(stmt, has_intrinsics, has_defs, has_opaque);
412+
expr_attributes(stmt, has_intrinsics, has_defs, has_opaque, has_forcedcompile);
409413
}
410-
*has_compile = jl_has_meta(body, compile_sym);
411414
}
412415

413416
static jl_module_t *call_require(jl_module_t *mod, jl_sym_t *var) JL_GLOBALLY_ROOTED
@@ -851,20 +854,20 @@ jl_value_t *jl_toplevel_eval_flex(jl_module_t *JL_NONNULL m, jl_value_t *e, int
851854
return (jl_value_t*)ex;
852855
}
853856

854-
int has_intrinsics = 0, has_defs = 0, has_loops = 0, has_opaque = 0, has_compile = 0;
857+
int has_intrinsics = 0, has_defs = 0, has_loops = 0, has_opaque = 0, has_forcedcompile = 0;
855858
assert(head == thunk_sym);
856859
thk = (jl_code_info_t*)jl_exprarg(ex, 0);
857860
assert(jl_is_code_info(thk));
858861
assert(jl_typeis(thk->code, jl_array_any_type));
859-
body_attributes((jl_array_t*)thk->code, &has_intrinsics, &has_defs, &has_loops, &has_opaque, &has_compile);
862+
body_attributes((jl_array_t*)thk->code, &has_intrinsics, &has_defs, &has_loops, &has_opaque, &has_forcedcompile);
860863

861864
jl_value_t *result;
862-
if (has_intrinsics || (!has_defs && fast && has_loops &&
863-
jl_options.compile_enabled != JL_OPTIONS_COMPILE_OFF &&
864-
jl_options.compile_enabled != JL_OPTIONS_COMPILE_MIN &&
865-
jl_get_module_compile(m) != JL_OPTIONS_COMPILE_OFF &&
866-
jl_get_module_compile(m) != JL_OPTIONS_COMPILE_MIN) ||
867-
has_compile) {
865+
if (has_intrinsics || has_forcedcompile ||
866+
(!has_defs && fast && has_loops &&
867+
jl_options.compile_enabled != JL_OPTIONS_COMPILE_OFF &&
868+
jl_options.compile_enabled != JL_OPTIONS_COMPILE_MIN &&
869+
jl_get_module_compile(m) != JL_OPTIONS_COMPILE_OFF &&
870+
jl_get_module_compile(m) != JL_OPTIONS_COMPILE_MIN)) {
868871
// use codegen
869872
mfunc = method_instance_for_thunk(thk, m);
870873
jl_resolve_globals_in_ir((jl_array_t*)thk->code, m, NULL, 0);

0 commit comments

Comments
 (0)