Skip to content

Commit bbd20e1

Browse files
committed
Add build_cpu_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 bbd20e1

File tree

4 files changed

+50
-10
lines changed

4 files changed

+50
-10
lines changed

src/HostCPUFeatures.jl

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,25 @@ 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+
_cpu_target = if @has_preference("cpu_target")
18+
@load_preference("cpu_target")
19+
else
20+
Base.unsafe_string(Base.JLOptions().cpu_target)
21+
end
22+
23+
const build_cpu_target = if occursin("native", _cpu_target)
24+
"native" # 'native' takes priority if provided
25+
else
26+
split(_cpu_target, ";")[1]
27+
end
28+
29+
# If true, this will opt-in to "freeze" an under-approximation of the CPU features at precompile-
30+
# time based on the CPU target.
31+
#
32+
# This is only done by default if "native" was excluded from the CPU target (or via a preference).
33+
const freeze_cpu_target =
34+
@load_preference("freeze_cpu_target", false) || build_cpu_target != "native"
35+
1736
function get_cpu_name()::String
1837
if isdefined(Sys, :CPU_NAME)
1938
Sys.CPU_NAME
@@ -46,12 +65,29 @@ end
4665
const BASELINE_CPU_NAME = get_cpu_name()
4766
const allow_eval = @load_preference("allow_runtime_invalidation", false)
4867

68+
function make_generic(target)
69+
target == "native" && return false
70+
if Sys.ARCH === :x86_64 || Sys.ARCH === :i686
71+
make_generic_x86(target)
72+
return true
73+
else
74+
return false
75+
end
76+
end
77+
78+
make_generic(build_cpu_target)
79+
4980
function __init__()
5081
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)
82+
freeze_cpu_target && return # CPU info fixed at precompile-time
83+
84+
runtime_target = Base.unsafe_string(Base.JLOptions().cpu_target)
85+
if !occursin("native", runtime_target)
86+
# The CPU target included "native" at pre-compile time, but at runtime it did not!
87+
#
88+
# Fixing this discepancy will invalidate the whole world (so it should probably
89+
# throw an error), but we do it anyway for backwards-compatibility.
90+
if make_generic(runtime_target)
5591
return nothing
5692
end
5793
end

src/cpu_info.jl

Lines changed: 8 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_cpu_target == "native"
44+
set_features!()
45+
end
4546

4647
function reset_features!()
4748
features, features_cstring = feature_string()
@@ -70,5 +71,8 @@ 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(build_cpu_target == "native" ?
76+
get_cpu_name() : build_cpu_target))
77+
@eval cpu_name() = Val{$_cpu}()
78+
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 build_cpu_target == "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)