Skip to content

Commit 09a8d77

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 09a8d77

File tree

4 files changed

+48
-10
lines changed

4 files changed

+48
-10
lines changed

src/HostCPUFeatures.jl

Lines changed: 38 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-approximation of the CPU
18+
# features at precompile-time.
19+
#
20+
# This is only done by default if "native" was excluded from the CPU target (or via a preference).
21+
const build_cpu_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,31 @@ 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+
target == "native" && return false
66+
if Sys.ARCH === :x86_64 || Sys.ARCH === :i686
67+
make_generic_x86(target)
68+
return true
69+
else
70+
return false
71+
end
72+
end
73+
74+
if build_cpu_target !== nothing
75+
make_generic(build_cpu_target)
76+
end
77+
4978
function __init__()
5079
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)
80+
build_cpu_target !== nothing && return # CPU info fixed at precompile-time
81+
82+
runtime_target = Base.unsafe_string(Base.JLOptions().cpu_target)
83+
if !occursin("native", runtime_target)
84+
# The CPU target included "native" at pre-compile time, but at runtime it did not!
85+
#
86+
# Fixing this discepancy will invalidate the whole world (so it should probably
87+
# throw an error), but we do it anyway for backwards-compatibility.
88+
if make_generic(runtime_target)
5589
return nothing
5690
end
5791
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 in (nothing, "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 in (nothing, "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 in (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)