Skip to content

Commit 116a624

Browse files
committed
Add build_target global to opt-in to generic CPU at precompile-time
We already support this dynamically, but for some reason it wasn't possible to get this configuration at precompilation time. Also adds a "freeze_cpu_target" preference to enable this behavior by default.
1 parent 723446a commit 116a624

File tree

4 files changed

+46
-10
lines changed

4 files changed

+46
-10
lines changed

src/HostCPUFeatures.jl

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,21 @@ using BitTwiddlingConvenienceFunctions: prevpow2, nextpow2, intlog2
1414
export has_feature, fma_fast, pick_vector_width, pick_vector_width_shift, register_count,
1515
register_size, simd_integer_register_size
1616

17+
# if not 'nothing', this build target will opt-in to "freeze" an under-
18+
# approximation of the CPU features at precompile-time. Currently we only
19+
# do this if "native" was excluded from the CPU target, or if a preference
20+
# is set.
21+
const build_target = begin
22+
_cpu_target = Base.unsafe_string(Base.JLOptions().cpu_target)
23+
if @has_preference("cpu_target")
24+
split(@load_preference("cpu_target"), ";")[1]
25+
elseif @load_preference("freeze_cpu_target", false) || !occursin("native", _cpu_target)
26+
split(_cpu_target, ";")[1]
27+
else
28+
nothing
29+
end
30+
end
31+
1732
function get_cpu_name()::String
1833
if isdefined(Sys, :CPU_NAME)
1934
Sys.CPU_NAME
@@ -46,12 +61,30 @@ end
4661
const BASELINE_CPU_NAME = get_cpu_name()
4762
const allow_eval = @load_preference("allow_runtime_invalidation", false)
4863

64+
function make_generic(target)
65+
if Sys.ARCH === :x86_64 || Sys.ARCH === :i686
66+
make_generic_x86(target)
67+
return true
68+
else
69+
return false
70+
end
71+
end
72+
73+
if build_target !== nothing && build_target !== "native"
74+
make_generic(build_target)
75+
end
76+
4977
function __init__()
5078
ccall(:jl_generating_output, Cint, ()) == 1 && return
51-
if Sys.ARCH === :x86_64 || Sys.ARCH === :i686
52-
target = Base.unsafe_string(Base.JLOptions().cpu_target)
53-
if !occursin("native", target)
54-
make_generic(target)
79+
build_target !== nothing && return # CPU info fixed at precompile-time
80+
81+
runtime_target = Base.unsafe_string(Base.JLOptions().cpu_target)
82+
if !occursin("native", runtime_target)
83+
# The CPU target included "native" at pre-compile time, but at runtime it did not!
84+
#
85+
# Fixing this discepancy will invalidate the whole world (so it should probably
86+
# throw an error), but we do it anyway for backwards-compatibility.
87+
if make_generic(runtime_target)
5588
return nothing
5689
end
5790
end

src/cpu_info.jl

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,10 @@ function set_features!()
3939
end
4040
Libc.free(features_cstring)
4141
end
42-
set_features!()
43-
4442

43+
if build_target === nothing || build_target === "native"
44+
set_features!()
45+
end
4546

4647
function reset_features!()
4748
features, features_cstring = feature_string()
@@ -70,5 +71,7 @@ function redefine_cpu_name()
7071
@warn "Runtime invalidation was disabled, but the CPU info is out-of-date.\nWill continue with incorrect CPU name (from build time)."
7172
end
7273
end
73-
cpu = QuoteNode(Symbol(get_cpu_name()))
74-
@eval cpu_name() = Val{$cpu}()
74+
75+
let _cpu = QuoteNode(Symbol(!in(build_target, (nothing, "native")) ? build_target : get_cpu_name()))
76+
@eval cpu_name() = Val{$_cpu}()
77+
end

src/cpu_info_aarch64.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ function _set_sve_vector_width!(bytes = _dynamic_register_size())
2828
end
2929

3030

31-
if _has_aarch64_sve()# && !(Bool(has_feature(Val(:aarch64_sve))))
31+
if !in(build_target, (nothing, "native")) && _has_aarch64_sve()# && !(Bool(has_feature(Val(:aarch64_sve))))
3232
has_feature(::Val{:aarch64_sve_cpuid}) = True()
3333
_set_sve_vector_width!()
3434
else

src/cpu_info_x86.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ end
5151
end
5252
end
5353

54-
function make_generic(target)
54+
function make_generic_x86(target)
5555
if occursin("tigerlake", target) || occursin("znver4", target) || occursin("sapphirerapids", target)
5656
# most feature-complete architectures we use
5757
setfeaturetrue(:x86_64_avx512ifma)

0 commit comments

Comments
 (0)