Skip to content

Commit dc7340e

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 "build_target" preference for (optional) override.
1 parent 723446a commit dc7340e

File tree

4 files changed

+38
-12
lines changed

4 files changed

+38
-12
lines changed

src/HostCPUFeatures.jl

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,17 @@ 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
20+
const build_target = let _cpu_target = Base.unsafe_string(Base.JLOptions().cpu_target)
21+
if @has_preference("build_target")
22+
@load_preference("build_target", nothing)
23+
else
24+
occursin("native", _cpu_target) ? nothing : split(_cpu_target, ";")[1]
25+
end
26+
end
27+
1728
function get_cpu_name()::String
1829
if isdefined(Sys, :CPU_NAME)
1930
Sys.CPU_NAME
@@ -46,14 +57,26 @@ end
4657
const BASELINE_CPU_NAME = get_cpu_name()
4758
const allow_eval = @load_preference("allow_runtime_invalidation", false)
4859

60+
function make_generic(target)
61+
if Sys.ARCH === :x86_64 || Sys.ARCH === :i686
62+
make_generic_x86(target)
63+
return true
64+
else
65+
return false
66+
end
67+
end
68+
69+
if build_target !== nothing
70+
make_generic(build_target)
71+
end
72+
4973
function __init__()
5074
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)
55-
return nothing
56-
end
75+
build_target !== nothing && return # CPU info fixed at precompile-time
76+
77+
runtime_target = Base.unsafe_string(Base.JLOptions().cpu_target)
78+
if !occursin("native", runtime_target) && make_generic(runtime_target)
79+
return nothing
5780
end
5881
if BASELINE_CPU_NAME != Sys.CPU_NAME::String
5982
redefine()

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
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(build_target !== nothing ? 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 build_target === nothing && _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)