Skip to content

Commit 87227d4

Browse files
authored
typeinfer: Add force_enable_inference to override Module-level inference settings (JuliaLang#57653)
Allows `--trim` to infer all the way through `__init__()` in JLLWrappers.jl despite the `@compiler_options` disabling inference (https://github.com/JuliaPackaging/JLLWrappers.jl/blob/ecaba50a4462209714f0979667c64c1bf28ee892/src/toplevel_generators.jl#L55) Required for JuliaLang#57587.
1 parent 21c245e commit 87227d4

File tree

2 files changed

+36
-16
lines changed

2 files changed

+36
-16
lines changed

src/typeinfer.jl

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -876,7 +876,7 @@ function typeinf_edge(interp::AbstractInterpreter, method::Method, @nospecialize
876876
end
877877
end
878878
end
879-
if ccall(:jl_get_module_infer, Cint, (Any,), method.module) == 0
879+
if !InferenceParams(interp).force_enable_inference && ccall(:jl_get_module_infer, Cint, (Any,), method.module) == 0
880880
add_remark!(interp, caller, "[typeinf_edge] Inference is disabled for the target module")
881881
return Future(MethodCallResult(interp, caller, method, Any, Any, Effects(), nothing, edgecycle, edgelimited))
882882
end
@@ -1160,15 +1160,17 @@ function typeinf_ext(interp::AbstractInterpreter, mi::MethodInstance, source_mod
11601160
end
11611161
end
11621162
end
1163-
if isa(def, Method) && ccall(:jl_get_module_infer, Cint, (Any,), def.module) == 0
1164-
src = retrieve_code_info(mi, get_inference_world(interp))
1165-
if src isa CodeInfo
1166-
finish!(interp, mi, ci, src)
1167-
else
1168-
engine_reject(interp, ci)
1163+
if !InferenceParams(interp).force_enable_inference
1164+
if isa(def, Method) && ccall(:jl_get_module_infer, Cint, (Any,), def.module) == 0
1165+
src = retrieve_code_info(mi, get_inference_world(interp))
1166+
if src isa CodeInfo
1167+
finish!(interp, mi, ci, src)
1168+
else
1169+
engine_reject(interp, ci)
1170+
end
1171+
ccall(:jl_typeinf_timing_end, Cvoid, (UInt64,), start_time)
1172+
return ci
11691173
end
1170-
ccall(:jl_typeinf_timing_end, Cvoid, (UInt64,), start_time)
1171-
return ci
11721174
end
11731175
result = InferenceResult(mi, typeinf_lattice(interp))
11741176
result.ci = ci
@@ -1314,7 +1316,10 @@ function typeinf_ext_toplevel(methods::Vector{Any}, worlds::Vector{UInt}, trim_m
13141316
# first compute the ABIs of everything
13151317
latest = true # whether this_world == world_counter()
13161318
for this_world in reverse(sort!(worlds))
1317-
interp = NativeInterpreter(this_world)
1319+
interp = NativeInterpreter(
1320+
this_world;
1321+
inf_params = InferenceParams(; force_enable_inference = trim_mode != TRIM_NO)
1322+
)
13181323
for i = 1:length(methods)
13191324
# each item in this list is either a MethodInstance indicating something
13201325
# to compile, or an svec(rettype, sig) describing a C-callable alias to create.
@@ -1355,7 +1360,7 @@ function typeinf_ext_toplevel(methods::Vector{Any}, worlds::Vector{UInt}, trim_m
13551360
src = codeinfo_for_const(interp, mi, callee.rettype_const)
13561361
elseif haskey(interp.codegen, callee)
13571362
src = interp.codegen[callee]
1358-
elseif isa(def, Method) && ccall(:jl_get_module_infer, Cint, (Any,), def.module) == 0 && trim_mode == TRIM_NO
1363+
elseif isa(def, Method) && !InferenceParams(interp).force_enable_inference && ccall(:jl_get_module_infer, Cint, (Any,), def.module) == 0
13591364
src = retrieve_code_info(mi, get_inference_world(interp))
13601365
else
13611366
# TODO: typeinf_code could return something with different edges/ages/owner/abi (needing an update to callee), which we don't handle here

src/types.jl

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,10 @@ Parameters that control abstract interpretation-based type inference operation.
191191
it will `throw`). Defaults to `false` since this assumption does not hold in Julia's
192192
semantics for native code execution.
193193
---
194+
- `inf_params.force_enable_inference::Bool = false`\\
195+
If `true`, inference will be performed on functions regardless of whether it was disabled
196+
at the module level via `Base.Experimental.@compiler_options`.
197+
---
194198
"""
195199
struct InferenceParams
196200
max_methods::Int
@@ -202,6 +206,7 @@ struct InferenceParams
202206
aggressive_constant_propagation::Bool
203207
assume_bindings_static::Bool
204208
ignore_recursion_hardlimit::Bool
209+
force_enable_inference::Bool
205210

206211
function InferenceParams(
207212
max_methods::Int,
@@ -212,7 +217,9 @@ struct InferenceParams
212217
ipo_constant_propagation::Bool,
213218
aggressive_constant_propagation::Bool,
214219
assume_bindings_static::Bool,
215-
ignore_recursion_hardlimit::Bool)
220+
ignore_recursion_hardlimit::Bool,
221+
force_enable_inference::Bool,
222+
)
216223
return new(
217224
max_methods,
218225
max_union_splitting,
@@ -222,7 +229,9 @@ struct InferenceParams
222229
ipo_constant_propagation,
223230
aggressive_constant_propagation,
224231
assume_bindings_static,
225-
ignore_recursion_hardlimit)
232+
ignore_recursion_hardlimit,
233+
force_enable_inference,
234+
)
226235
end
227236
end
228237
function InferenceParams(
@@ -235,7 +244,9 @@ function InferenceParams(
235244
#=ipo_constant_propagation::Bool=# true,
236245
#=aggressive_constant_propagation::Bool=# false,
237246
#=assume_bindings_static::Bool=# false,
238-
#=ignore_recursion_hardlimit::Bool=# false);
247+
#=ignore_recursion_hardlimit::Bool=# false,
248+
#=force_enable_inference::Bool=# false
249+
);
239250
max_methods::Int = params.max_methods,
240251
max_union_splitting::Int = params.max_union_splitting,
241252
max_apply_union_enum::Int = params.max_apply_union_enum,
@@ -244,7 +255,9 @@ function InferenceParams(
244255
ipo_constant_propagation::Bool = params.ipo_constant_propagation,
245256
aggressive_constant_propagation::Bool = params.aggressive_constant_propagation,
246257
assume_bindings_static::Bool = params.assume_bindings_static,
247-
ignore_recursion_hardlimit::Bool = params.ignore_recursion_hardlimit)
258+
ignore_recursion_hardlimit::Bool = params.ignore_recursion_hardlimit,
259+
force_enable_inference::Bool = params.force_enable_inference,
260+
)
248261
return InferenceParams(
249262
max_methods,
250263
max_union_splitting,
@@ -254,7 +267,9 @@ function InferenceParams(
254267
ipo_constant_propagation,
255268
aggressive_constant_propagation,
256269
assume_bindings_static,
257-
ignore_recursion_hardlimit)
270+
ignore_recursion_hardlimit,
271+
force_enable_inference,
272+
)
258273
end
259274

260275
"""

0 commit comments

Comments
 (0)