Skip to content

Commit 835cd0f

Browse files
authored
Force compilation with @force_compile (#42379)
There was an unintended collision in meaning between #42128 and #37041. Since both use the `Expr(:meta)` mechanism, it doesn't really seem feasible to have them both use the same name. Consequently, it's better to rename the newer meaning. Fixes #42373
1 parent 011fda9 commit 835cd0f

File tree

6 files changed

+42
-23
lines changed

6 files changed

+42
-23
lines changed

base/experimental.jl

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

165+
"""
166+
Experimental.@force_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
181+
end
182+
183+
end
184+
```
185+
"""
186+
macro force_compile() Expr(:meta, :force_compile) end
187+
165188
# UI features for errors
166189

167190
"""

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.@force_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.@force_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.@force_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.@force_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.@force_compile
378378
local stats = gc_num()
379379
local elapsedtime = time_ns()
380380
local val = $(esc(ex))

src/ast.c

+2
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ JL_DLLEXPORT jl_sym_t *jl_atom_sym;
100100
JL_DLLEXPORT jl_sym_t *jl_statement_sym;
101101
JL_DLLEXPORT jl_sym_t *jl_all_sym;
102102
JL_DLLEXPORT jl_sym_t *jl_compile_sym;
103+
JL_DLLEXPORT jl_sym_t *jl_force_compile_sym;
103104
JL_DLLEXPORT jl_sym_t *jl_infer_sym;
104105
JL_DLLEXPORT jl_sym_t *jl_atomic_sym;
105106
JL_DLLEXPORT jl_sym_t *jl_not_atomic_sym;
@@ -437,6 +438,7 @@ void jl_init_common_symbols(void)
437438
jl_specialize_sym = jl_symbol("specialize");
438439
jl_optlevel_sym = jl_symbol("optlevel");
439440
jl_compile_sym = jl_symbol("compile");
441+
jl_force_compile_sym = jl_symbol("force_compile");
440442
jl_infer_sym = jl_symbol("infer");
441443
jl_macrocall_sym = jl_symbol("macrocall");
442444
jl_escape_sym = jl_symbol("escape");

src/julia_internal.h

+1
Original file line numberDiff line numberDiff line change
@@ -1435,6 +1435,7 @@ extern JL_DLLEXPORT jl_sym_t *jl_atom_sym;
14351435
extern JL_DLLEXPORT jl_sym_t *jl_statement_sym;
14361436
extern JL_DLLEXPORT jl_sym_t *jl_all_sym;
14371437
extern JL_DLLEXPORT jl_sym_t *jl_compile_sym;
1438+
extern JL_DLLEXPORT jl_sym_t *jl_force_compile_sym;
14381439
extern JL_DLLEXPORT jl_sym_t *jl_infer_sym;
14391440
extern JL_DLLEXPORT jl_sym_t *jl_atomic_sym;
14401441
extern JL_DLLEXPORT jl_sym_t *jl_not_atomic_sym;

src/toplevel.c

+11-11
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ int jl_code_requires_compiler(jl_code_info_t *src)
378378
assert(jl_typeis(body, jl_array_any_type));
379379
size_t i;
380380
int has_intrinsics = 0, has_defs = 0, has_opaque = 0;
381-
if (jl_has_meta(body, jl_compile_sym))
381+
if (jl_has_meta(body, jl_force_compile_sym))
382382
return 1;
383383
for(i=0; i < jl_array_len(body); i++) {
384384
jl_value_t *stmt = jl_array_ptr_ref(body,i);
@@ -389,7 +389,7 @@ int jl_code_requires_compiler(jl_code_info_t *src)
389389
return 0;
390390
}
391391

392-
static void body_attributes(jl_array_t *body, int *has_intrinsics, int *has_defs, int *has_loops, int *has_opaque, int *has_compile)
392+
static void body_attributes(jl_array_t *body, int *has_intrinsics, int *has_defs, int *has_loops, int *has_opaque, int *forced_compile)
393393
{
394394
size_t i;
395395
*has_loops = 0;
@@ -407,7 +407,7 @@ static void body_attributes(jl_array_t *body, int *has_intrinsics, int *has_defs
407407
}
408408
expr_attributes(stmt, has_intrinsics, has_defs, has_opaque);
409409
}
410-
*has_compile = jl_has_meta(body, jl_compile_sym);
410+
*forced_compile = jl_has_meta(body, jl_force_compile_sym);
411411
}
412412

413413
static jl_module_t *call_require(jl_module_t *mod, jl_sym_t *var) JL_GLOBALLY_ROOTED
@@ -851,20 +851,20 @@ jl_value_t *jl_toplevel_eval_flex(jl_module_t *JL_NONNULL m, jl_value_t *e, int
851851
return (jl_value_t*)ex;
852852
}
853853

854-
int has_intrinsics = 0, has_defs = 0, has_loops = 0, has_opaque = 0, has_compile = 0;
854+
int has_intrinsics = 0, has_defs = 0, has_loops = 0, has_opaque = 0, forced_compile = 0;
855855
assert(head == jl_thunk_sym);
856856
thk = (jl_code_info_t*)jl_exprarg(ex, 0);
857857
assert(jl_is_code_info(thk));
858858
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);
859+
body_attributes((jl_array_t*)thk->code, &has_intrinsics, &has_defs, &has_loops, &has_opaque, &forced_compile);
860860

861861
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) {
862+
if (forced_compile || has_intrinsics ||
863+
(!has_defs && fast && has_loops &&
864+
jl_options.compile_enabled != JL_OPTIONS_COMPILE_OFF &&
865+
jl_options.compile_enabled != JL_OPTIONS_COMPILE_MIN &&
866+
jl_get_module_compile(m) != JL_OPTIONS_COMPILE_OFF &&
867+
jl_get_module_compile(m) != JL_OPTIONS_COMPILE_MIN)) {
868868
// use codegen
869869
mfunc = method_instance_for_thunk(thk, m);
870870
jl_resolve_globals_in_ir((jl_array_t*)thk->code, m, NULL, 0);

0 commit comments

Comments
 (0)