Skip to content

Commit 723446a

Browse files
committed
Add Preference to disable runtime invalidation
This makes the package (nearly) `--trim` compatible and also provides a nice preference for users who do not wish for HostCPUFeatures.jl to ever cause an "invalidation storm", even if it means running with the wrong CPU info.
1 parent 568f9e8 commit 723446a

File tree

5 files changed

+50
-14
lines changed

5 files changed

+50
-14
lines changed

Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ version = "0.1.17"
77
BitTwiddlingConvenienceFunctions = "62783981-4cbd-42fc-bca8-16325de8dc4b"
88
IfElse = "615f187c-cbe4-4ef1-ba3b-2fcf58d6d173"
99
Libdl = "8f399da3-3557-5675-b5ff-fb832c97cbdb"
10+
Preferences = "21216c6a-2e73-6563-6e65-726566657250"
1011
Static = "aedffcd0-7271-4cad-89d0-dc628f76c6d3"
1112

1213
[compat]

src/HostCPUFeatures.jl

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ end
77
using Libdl, Static
88
using Static: Zero, One, lt, gt
99
using IfElse: ifelse
10+
using Preferences
1011

1112
using BitTwiddlingConvenienceFunctions: prevpow2, nextpow2, intlog2
1213

@@ -37,19 +38,26 @@ unwrap(::StaticSymbol{S}) where {S} = S
3738

3839
@noinline function redefine()
3940
@debug "Defining CPU name."
40-
define_cpu_name()
41+
redefine_cpu_name()
4142

4243
reset_features!()
4344
reset_extra_features!()
4445
end
4546
const BASELINE_CPU_NAME = get_cpu_name()
47+
const allow_eval = @load_preference("allow_runtime_invalidation", false)
48+
4649
function __init__()
4750
ccall(:jl_generating_output, Cint, ()) == 1 && return
4851
if Sys.ARCH === :x86_64 || Sys.ARCH === :i686
4952
target = Base.unsafe_string(Base.JLOptions().cpu_target)
50-
occursin("native", target) || return make_generic(target)
53+
if !occursin("native", target)
54+
make_generic(target)
55+
return nothing
56+
end
57+
end
58+
if BASELINE_CPU_NAME != Sys.CPU_NAME::String
59+
redefine()
5160
end
52-
BASELINE_CPU_NAME == Sys.CPU_NAME::String || redefine()
5361
return nothing
5462
end
5563

src/cpu_info.jl

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,12 @@ function reset_features!()
4848
for ext features
4949
feature, has = process_feature(ext)
5050
if _has_feature(feature) has
51-
@debug "Defining $(has ? "presence" : "absense") of feature $feature."
52-
set_feature(feature, has)
51+
if allow_eval
52+
@debug "Defining $(has ? "presence" : "absense") of feature $feature."
53+
set_feature(feature, has)
54+
else
55+
@warn "Runtime invalidation was disabled, but the CPU info is out-of-date.\nWill continue with incorrect CPU feature flag: $ext."
56+
end
5357
end
5458
end
5559
Libc.free(features_cstring)
@@ -58,8 +62,13 @@ end
5862
register_size(::Type{T}) where {T} = register_size()
5963
register_size(::Type{T}) where {T<:Union{Signed,Unsigned}} = simd_integer_register_size()
6064

61-
function define_cpu_name()
65+
function redefine_cpu_name()
6266
cpu = QuoteNode(Symbol(get_cpu_name()))
63-
@eval cpu_name() = Val{$cpu}()
67+
if allow_eval
68+
@eval cpu_name() = Val{$cpu}()
69+
else
70+
@warn "Runtime invalidation was disabled, but the CPU info is out-of-date.\nWill continue with incorrect CPU name (from build time)."
71+
end
6472
end
65-
define_cpu_name()
73+
cpu = QuoteNode(Symbol(get_cpu_name()))
74+
@eval cpu_name() = Val{$cpu}()

src/cpu_info_aarch64.jl

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,20 @@ end
3939

4040
function reset_extra_features!()
4141
drs = _dynamic_register_size()
42-
register_size() drs && _set_sve_vector_width!(drs)
42+
if register_size() drs
43+
if allow_eval
44+
_set_sve_vector_width!(drs)
45+
else
46+
@warn "Runtime invalidation was disabled, but the CPU info is out-of-date.\nWill continue with incorrect CPU register size."
47+
end
48+
end
4349
hassve = _has_aarch64_sve()
4450
if hassve has_feature(Val(:aarch64_sve_cpuid))
45-
@eval has_feature(::Val{:aarch64_sve_cpuid}) = $(Expr(:call, hassve ? :True : :False))
51+
if allow_eval
52+
@eval has_feature(::Val{:aarch64_sve_cpuid}) = $(Expr(:call, hassve ? :True : :False))
53+
else
54+
@warn "Runtime invalidation was disabled, but the CPU info is out-of-date.\nWill continue with incorrect CPU feature flag: :aarch64_sve_cpuid."
55+
end
4656
end
4757
end
4858

src/cpu_info_x86.jl

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,22 @@ fast_int64_to_double() = has_feature(Val(:x86_64_avx512dq))
3232

3333
fast_half() = False()
3434

35-
@noinline function setfeaturefalse(s)
35+
@inline function setfeaturefalse(s)
3636
if has_feature(Val(s)) === True()
37-
@eval has_feature(::Val{$(QuoteNode(s))}) = False()
37+
if allow_eval
38+
@eval has_feature(::Val{$(QuoteNode(s))}) = False()
39+
else
40+
@warn "Runtime invalidation was disabled, but the CPU info is out-of-date.\nWill continue with incorrect CPU feature flag: $s."
41+
end
3842
end
3943
end
40-
@noinline function setfeaturetrue(s)
44+
@inline function setfeaturetrue(s)
4145
if has_feature(Val(s)) === False()
42-
@eval has_feature(::Val{$(QuoteNode(s))}) = True()
46+
if allow_eval
47+
@eval has_feature(::Val{$(QuoteNode(s))}) = True()
48+
else
49+
@warn "Runtime invalidation was disabled, but the CPU info is out-of-date.\nWill continue with incorrect CPU feature flag: $s."
50+
end
4351
end
4452
end
4553

0 commit comments

Comments
 (0)