diff --git a/src/HostCPUFeatures.jl b/src/HostCPUFeatures.jl index 7560a17..f3d6d3c 100644 --- a/src/HostCPUFeatures.jl +++ b/src/HostCPUFeatures.jl @@ -23,7 +23,8 @@ end const build_cpu_target = if occursin("native", _cpu_target) "native" # 'native' takes priority if provided else - split(_cpu_target, ";")[1] + # first `;`-separated spec, without any `,+feature`/`-feature` modifiers + first(split(first(split(_cpu_target, ';')), ',')) end # If true, this will opt-in to "freeze" an under-approximation of the CPU features at precompile- @@ -65,17 +66,20 @@ end const BASELINE_CPU_NAME = get_cpu_name() const allow_eval = @load_preference("allow_runtime_invalidation", false) -function make_generic(target) +function make_generic(target; runtime::Bool = false) target == "native" && return false if Sys.ARCH === :x86_64 || Sys.ARCH === :i686 - make_generic_x86(target) + make_generic_x86(target; runtime) return true else return false end end -make_generic(build_cpu_target) +# Restrict reported features to those available in every `cpu_target` spec and +# on the host, so generated code can be instruction-selected for every clone of +# a multiversioned image and still run in this session. +make_generic(_cpu_target) function __init__() ccall(:jl_generating_output, Cint, ()) == 1 && return @@ -87,7 +91,7 @@ function __init__() # # Fixing this discepancy will invalidate the whole world (so it should probably # throw an error), but we do it anyway for backwards-compatibility. - if make_generic(runtime_target) + if make_generic(runtime_target; runtime = true) return nothing end end diff --git a/src/cpu_info.jl b/src/cpu_info.jl index cac0552..1bbfb7f 100644 --- a/src/cpu_info.jl +++ b/src/cpu_info.jl @@ -26,21 +26,32 @@ has_feature(_) = False() end end -function set_features!() +function host_features() features, features_cstring = feature_string() znver3 = get_cpu_name() === "znver3" + s = Set{Symbol}() for ext ∈ features feature, has = process_feature(ext) if znver3 && occursin("512", feature) has = false end - has && push!(FEATURE_SET, feature) - set_feature(feature, has) + has && push!(s, Symbol(feature)) end Libc.free(features_cstring) + return s end -if build_cpu_target == "native" +function set_features!() + for feature ∈ host_features() + f = string(feature) + push!(FEATURE_SET, f) + set_feature(f, true) + end +end +# x86 always enables host features here and then intersects them with the +# `cpu_target` specs in `make_generic`; other architectures keep the previous +# all-false default for non-native targets. +if build_cpu_target == "native" || Sys.ARCH === :x86_64 || Sys.ARCH === :i686 set_features!() end diff --git a/src/cpu_info_x86.jl b/src/cpu_info_x86.jl index 0cf1f50..c1de5ba 100644 --- a/src/cpu_info_x86.jl +++ b/src/cpu_info_x86.jl @@ -51,67 +51,167 @@ end end end -function make_generic_x86(target) - if occursin("tigerlake", target) || occursin("znver4", target) || occursin("sapphirerapids", target) - # most feature-complete architectures we use - setfeaturetrue(:x86_64_avx512ifma) - setfeaturetrue(:x86_64_avx512vl) - setfeaturetrue(:x86_64_avx512bw) - setfeaturetrue(:x86_64_avx512dq) - setfeaturetrue(:x86_64_avx512f) - setfeaturetrue(:x86_64_avx2) - setfeaturetrue(:x86_64_bmi2) - setfeaturetrue(:x86_64_fma) - setfeaturetrue(:x86_64_avx) - elseif occursin("icelake", target) || occursin("skylake-avx512", target) || occursin("rocketlake", target) || occursin("cascadelake", target) - # no ifma, but avx512f and avx512dq - setfeaturefalse(:x86_64_avx512ifma) - setfeaturetrue(:x86_64_avx512vl) - setfeaturetrue(:x86_64_avx512bw) - setfeaturetrue(:x86_64_avx512dq) - setfeaturetrue(:x86_64_avx512f) - setfeaturetrue(:x86_64_avx2) - setfeaturetrue(:x86_64_bmi2) - setfeaturetrue(:x86_64_fma) - setfeaturetrue(:x86_64_avx) - elseif occursin("znver", target) || occursin("lake", target) || occursin("well", target) - # no avx512, but avx2, fma, and bmi2 - # znver tries to capture all zen < 4 - # lake tries to capture lakes we didn't single out above as having avx512 - # - setfeaturefalse(:x86_64_avx512ifma) - setfeaturefalse(:x86_64_avx512vl) - setfeaturefalse(:x86_64_avx512bw) - setfeaturefalse(:x86_64_avx512dq) - setfeaturefalse(:x86_64_avx512f) - setfeaturetrue(:x86_64_avx2) - setfeaturetrue(:x86_64_bmi2) - setfeaturetrue(:x86_64_fma) - setfeaturetrue(:x86_64_avx) - elseif occursin("ivybridge", target) || occursin("sandybridge", target) - # has avx, and that is about it we care about - setfeaturefalse(:x86_64_avx512ifma) - setfeaturefalse(:x86_64_avx512vl) - setfeaturefalse(:x86_64_avx512bw) - setfeaturefalse(:x86_64_avx512dq) - setfeaturefalse(:x86_64_avx512f) - setfeaturefalse(:x86_64_avx2) - setfeaturefalse(:x86_64_bmi2) - setfeaturefalse(:x86_64_fma) - setfeaturetrue(:x86_64_avx) - else - # hopefully we didn't miss something - # TODO: sapphire rapids - setfeaturefalse(:x86_64_avx512ifma) - setfeaturefalse(:x86_64_avx512vl) - setfeaturefalse(:x86_64_avx512bw) - setfeaturefalse(:x86_64_avx512dq) - setfeaturefalse(:x86_64_avx512f) - setfeaturefalse(:x86_64_avx2) - setfeaturefalse(:x86_64_bmi2) - setfeaturefalse(:x86_64_fma) - setfeaturefalse(:x86_64_avx) +# Feature sets use LLVM feature names. The sets are conservative: listing a +# feature a CPU lacks can produce code that fails instruction selection, while +# omitting one only loses performance. +_fset(xs...) = Set{Symbol}(Symbol(archstr() * x) for x ∈ xs) + +const X86_BASE = _fset("sse", "sse2", "mmx", "fxsr", "cx8", "cx16", "sahf", "cmov") +const X86_SSE3 = X86_BASE ∪ _fset("sse3") +const X86_SSSE3 = X86_SSE3 ∪ _fset("ssse3") +const X86_SSE42 = X86_SSSE3 ∪ _fset("sse4.1", "sse4.2", "popcnt", "crc32") +const X86_WESTMERE = X86_SSE42 ∪ _fset("aes", "pclmul") +const X86_SILVERMONT = X86_SSE42 ∪ _fset("movbe", "rdrnd", "xsave", "xsaveopt", "prfchw") +const X86_GOLDMONT = X86_SILVERMONT ∪ _fset("rdseed", "sha", "fsgsbase", "xsavec", "xsaves", "clflushopt") +const X86_TREMONT = X86_GOLDMONT ∪ _fset("gfni", "vaes", "vpclmulqdq", "cldemote", "movdiri", "movdir64b", "waitpkg", "clwb") + +const X86_AVX_SNB = X86_WESTMERE ∪ _fset("avx", "xsave", "xsaveopt") +const X86_AVX_IVB = X86_AVX_SNB ∪ _fset("f16c", "rdrnd", "fsgsbase") +const X86_AVX2 = X86_AVX_IVB ∪ _fset("avx2", "fma", "bmi", "bmi2", "lzcnt", "movbe", "invpcid") +const X86_AVX2_BDW = X86_AVX2 ∪ _fset("rdseed", "adx", "prfchw") +const X86_AVX2_SKL = X86_AVX2_BDW ∪ _fset("clflushopt", "xsavec", "xsaves") +const X86_AVX2_ADL = X86_AVX2_SKL ∪ _fset("gfni", "vaes", "vpclmulqdq", "serialize", "tsxldtrk", "waitpkg", "cldemote", "movdiri", "movdir64b", "hreset", "ptwrite", "avxvnni", "rdpid") +const X86_AVX2_MTL = setdiff(X86_AVX2_ADL, _fset("avxvnni")) +const X86_AVX2_SRF = X86_AVX2_SKL ∪ _fset("gfni", "vaes", "vpclmulqdq", "sha", "cldemote", "movdiri", "movdir64b", "waitpkg", "serialize", "tsxldtrk", "hreset", "avxifma", "avxvnniint8", "avxneconvert", "cmpccxadd") + +const X86_SKX = X86_AVX2_SKL ∪ _fset("pku", "clwb", "avx512f", "avx512cd", "avx512bw", "avx512dq", "avx512vl") +const X86_CNL = X86_SKX ∪ _fset("avx512ifma", "avx512vbmi", "sha") +const X86_ICL = X86_SKX ∪ _fset("avx512ifma", "avx512vbmi", "avx512vbmi2", "avx512vnni", "avx512bitalg", "avx512vpopcntdq", "gfni", "vaes", "vpclmulqdq", "rdpid") +const X86_ICL_SERVER = X86_ICL ∪ _fset("pconfig", "wbnoinvd") +const X86_CAS = X86_SKX ∪ _fset("avx512vnni") +const X86_CPL = X86_SKX ∪ _fset("avx512vnni", "avx512bf16") +const X86_TGL = X86_ICL ∪ _fset("avx512vp2intersect", "movdiri", "movdir64b") +const X86_SPR = X86_ICL ∪ _fset("avx512bf16", "avx512fp16", "amx-bf16", "amx-int8", "amx-tile", "serialize", "tsxldtrk", "waitpkg", "cldemote", "ptwrite", "hreset", "uintr", "avxvnni", "movdiri", "movdir64b") +const X86_KNL = X86_AVX2 ∪ _fset("avx512f", "avx512cd", "avx512er", "avx512pf", "prefetchwt1") + +# x86-64 microarchitecture levels (psABI) +const X86_V3 = X86_SSE42 ∪ _fset("avx", "avx2", "fma", "f16c", "bmi", "bmi2", "lzcnt", "movbe", "xsave") +const X86_V4 = X86_V3 ∪ _fset("avx512f", "avx512bw", "avx512dq", "avx512vl") + +# AMD +const X86_K10 = X86_SSE42 ∪ _fset("sse4a", "lzcnt") +const X86_BTVER1 = X86_SSSE3 ∪ _fset("sse4a", "lzcnt") +const X86_BTVER2 = X86_SSE42 ∪ _fset("avx", "aes", "pclmul", "f16c", "bmi", "movbe", "sse4a", "xsave", "xsaveopt", "lzcnt", "prfchw") +const X86_BDVER1 = X86_SSE42 ∪ _fset("avx", "aes", "pclmul", "xsave", "xsaveopt", "sse4a", "lzcnt", "xop", "fma4", "lwp") +const X86_BDVER2 = X86_BDVER1 ∪ _fset("fma", "f16c", "bmi", "tbm", "fsgsbase") +const X86_BDVER4 = X86_AVX2 ∪ _fset("sse4a", "fma4", "prfchw", "mwaitx") +const X86_ZNVER = X86_AVX2 ∪ _fset("sse4a", "prfchw", "sha", "adx", "rdseed", "mwaitx", "clflushopt") +const X86_ZNVER2 = X86_ZNVER ∪ _fset("clzero", "rdpru", "wbnoinvd", "xsavec", "xsaves") +const X86_ZNVER3 = X86_ZNVER2 ∪ _fset("vaes", "vpclmulqdq", "pku") +const X86_ZNVER4 = X86_ZNVER3 ∪ _fset("avx512f", "avx512cd", "avx512bw", "avx512dq", "avx512vl", "avx512ifma", "avx512vbmi", "avx512vbmi2", "avx512vnni", "avx512bitalg", "avx512vpopcntdq", "avx512bf16", "gfni", "serialize") +const X86_ZNVER5 = X86_ZNVER4 ∪ _fset("avxvnni", "prefetchi") + +const X86_CPU_FEATURES = Dict{String,Set{Symbol}}( + "generic" => X86_BASE, "x86-64" => X86_BASE, "x86-64-v1" => X86_BASE, + "x86-64-v2" => X86_SSE42, "x86-64-v3" => X86_V3, "x86-64-v4" => X86_V4, + "pentium4" => X86_SSE3, "nocona" => X86_SSE3, "prescott" => X86_SSE3, + "core2" => X86_SSSE3, "penryn" => X86_SSSE3, "bonnell" => X86_SSSE3, "atom" => X86_SSSE3, + "nehalem" => X86_SSE42, "corei7" => X86_SSE42, "westmere" => X86_WESTMERE, + "silvermont" => X86_SILVERMONT, "slm" => X86_SILVERMONT, "airmont" => X86_SILVERMONT, + "goldmont" => X86_GOLDMONT, "goldmont-plus" => X86_GOLDMONT, "tremont" => X86_TREMONT, + "sandybridge" => X86_AVX_SNB, "corei7-avx" => X86_AVX_SNB, + "ivybridge" => X86_AVX_IVB, "core-avx-i" => X86_AVX_IVB, + "haswell" => X86_AVX2, "core-avx2" => X86_AVX2, "broadwell" => X86_AVX2_BDW, + "skylake" => X86_AVX2_SKL, "kabylake" => X86_AVX2_SKL, "coffeelake" => X86_AVX2_SKL, + "cometlake" => X86_AVX2_SKL, "whiskeylake" => X86_AVX2_SKL, "amberlake" => X86_AVX2_SKL, + "alderlake" => X86_AVX2_ADL, "raptorlake" => X86_AVX2_ADL, + "meteorlake" => X86_AVX2_MTL, "arrowlake" => X86_AVX2_MTL, "arrowlake-s" => X86_AVX2_MTL, + "lunarlake" => X86_AVX2_MTL, "pantherlake" => X86_AVX2_MTL, "wildcatlake" => X86_AVX2_MTL, + "sierraforest" => X86_AVX2_SRF, "grandridge" => X86_AVX2_SRF, "clearwaterforest" => X86_AVX2_SRF, + "skylake-avx512" => X86_SKX, "skx" => X86_SKX, + "cannonlake" => X86_CNL, "icelake" => X86_ICL, "icelake-client" => X86_ICL, + "icelake-server" => X86_ICL_SERVER, "cascadelake" => X86_CAS, "cooperlake" => X86_CPL, + "rocketlake" => X86_ICL, "tigerlake" => X86_TGL, + "sapphirerapids" => X86_SPR, "emeraldrapids" => X86_SPR, "graniterapids" => X86_SPR, + "graniterapids-d" => X86_SPR, "diamondrapids" => X86_SPR, + "knl" => X86_KNL, "knm" => X86_KNL, + "amdfam10" => X86_K10, "barcelona" => X86_K10, "shanghai" => X86_K10, + "istanbul" => X86_K10, "magny-cours" => X86_K10, + "k8" => X86_BASE, "opteron" => X86_BASE, "athlon64" => X86_BASE, "athlon-fx" => X86_BASE, + "athlon64-sse3" => X86_SSE3, "k8-sse3" => X86_SSE3, + "btver1" => X86_BTVER1, "btver2" => X86_BTVER2, + "bdver1" => X86_BDVER1, "bdver2" => X86_BDVER2, "bdver3" => X86_BDVER2, "bdver4" => X86_BDVER4, + "znver" => X86_ZNVER, "znver2" => X86_ZNVER2, "znver3" => X86_ZNVER3, + "znver4" => X86_ZNVER4, "znver5" => X86_ZNVER5, +) + +function cpu_features(name::AbstractString) + features = get(X86_CPU_FEATURES, name, nothing) + if features === nothing + # Unknown CPU: pick a safe tier. Newer AVX-512 parts tend to carry "-avx512" + # or "rapids" in their names; otherwise assume at most the AVX2 level. + features = if occursin("avx512", name) + X86_SKX + elseif occursin("znver", name) || occursin("rapids", name) || + occursin("lake", name) || occursin("well", name) || + occursin("forest", name) + X86_AVX2 + elseif occursin("bridge", name) + X86_AVX_SNB + else + X86_BASE + end + end + return copy(features) +end + +# Feature set of one `;`-separated spec ("cpu,+feat,-feat,clone_all,base(n)"). +# Returns `nothing` for specs that contain no CPU name. +function spec_features(spec::AbstractString) + parts = split(spec, ','; keepempty = false) + isempty(parts) && return nothing + cpu = strip(String(first(parts))) + (cpu == "clone_all" || startswith(cpu, "base(")) && return nothing + feats = cpu == "native" ? host_features() : cpu_features(cpu) + for m in Iterators.drop(parts, 1) + m = strip(String(m)) + if startswith(m, "+") + push!(feats, Symbol(archstr() * m[2:end])) + elseif startswith(m, "-") + delete!(feats, Symbol(archstr() * m[2:end])) + end end + return feats +end + +# Features usable by generated code: present on the host (any code this session +# JIT-compiles still runs on the host) and in every spec of the target, so that +# the same generated IR can be instruction-selected for every clone. +function target_features(target::AbstractString) + feats = host_features() + for spec in split(target, ';'; keepempty = false) + f = spec_features(spec) + f === nothing && continue + intersect!(feats, f) + end + return feats +end + +const X86_ALL_FEATURES = union( + X86_SKX, X86_CNL, X86_ICL, X86_ICL_SERVER, X86_CAS, X86_CPL, X86_TGL, X86_SPR, + X86_KNL, X86_V3, X86_ZNVER4, X86_ZNVER5, X86_BDVER4, X86_AVX2_ADL, X86_AVX2_SRF, + X86_TREMONT, X86_BDVER2, X86_BTVER2, X86_WESTMERE, X86_AVX_IVB, X86_AVX_SNB, + X86_GOLDMONT, X86_SILVERMONT, X86_SSE42, X86_SSSE3, X86_SSE3, X86_BASE, +) + +function apply_features!(S::Set{Symbol}; runtime::Bool = false) + for f ∈ X86_ALL_FEATURES ∪ S ∪ Set(Symbol.(collect(FEATURE_SET))) + enable = f ∈ S + if runtime + enable ? setfeaturetrue(f) : setfeaturefalse(f) + if allow_eval + enable ? push!(FEATURE_SET, string(f)) : delete!(FEATURE_SET, string(f)) + end + else + set_feature(string(f), enable) + enable ? push!(FEATURE_SET, string(f)) : delete!(FEATURE_SET, string(f)) + end + end + return nothing +end + +function make_generic_x86(target; runtime::Bool = false) + apply_features!(target_features(target); runtime) return nothing end diff --git a/test/runtests.jl b/test/runtests.jl index 29bf1a3..19539d0 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -48,4 +48,83 @@ using Test @test HostCPUFeatures.unwrap(HostCPUFeatures.static(:a)) === :a end + if Sys.ARCH === :x86_64 + @time @testset "cpu_target feature sets" begin + @test HostCPUFeatures.spec_features("clone_all") === nothing + @test :x86_64_avx512f ∈ HostCPUFeatures.spec_features("skylake-avx512") + @test :x86_64_avx512vl ∈ HostCPUFeatures.spec_features("skylake-avx512") + @test :x86_64_avx512ifma ∉ HostCPUFeatures.spec_features("skylake-avx512") + @test :x86_64_avx512ifma ∈ HostCPUFeatures.spec_features("sapphirerapids") + @test :x86_64_avx512bw ∉ HostCPUFeatures.spec_features("knl") + @test :x86_64_avx512f ∈ HostCPUFeatures.spec_features("znver4") + @test :x86_64_avx512f ∉ HostCPUFeatures.spec_features("znver2") + @test :x86_64_avx512f ∈ HostCPUFeatures.spec_features("x86-64-v4") + @test :x86_64_avx512f ∉ HostCPUFeatures.spec_features("x86-64-v3") + @test :x86_64_avx ∉ HostCPUFeatures.spec_features("generic") + @test :x86_64_avx ∈ HostCPUFeatures.spec_features("sandybridge") + @test :x86_64_avx2 ∉ HostCPUFeatures.spec_features("sandybridge") + # per-spec feature modifiers + @test :x86_64_rdrnd ∉ HostCPUFeatures.spec_features("haswell,-rdrnd") + @test :x86_64_rdrnd ∈ HostCPUFeatures.spec_features("haswell") + @test :x86_64_avx512f ∈ HostCPUFeatures.spec_features("haswell,+avx512f") + end + + @time @testset "cpu_target intersection" begin + tf = HostCPUFeatures.target_features + # a feature missing from any clone spec can never be reported + @test :x86_64_avx512f ∉ tf("skylake-avx512;generic") + @test :x86_64_avx512f ∉ tf("generic;skylake-avx512") + @test :x86_64_avx ∉ tf("generic;sandybridge,-xsaveopt,clone_all;haswell,-rdrnd,base(1)") + # `native` in a spec means the host + @test tf("native") == HostCPUFeatures.host_features() + @test tf("native;generic") == tf("generic") + # generated code must also run in the current session + @test tf("sapphirerapids") ⊆ HostCPUFeatures.host_features() + end + + @time @testset "non-native cpu_target image build" begin + # "skylake-avx512;generic" puts an AVX-512 spec first: reporting host or + # first-spec features would emit AVX-512 IR that the `generic` clone + # cannot instruction-select (`LLVM ERROR: Cannot select: X86ISD::RCP14`, + # JuliaSIMD/VectorizationBase.jl#131). + prefsfile = joinpath(dirname(Base.active_project()), "LocalPreferences.toml") + backup = isfile(prefsfile) ? read(prefsfile) : nothing + write(prefsfile, "[HostCPUFeatures]\nallow_runtime_invalidation = true\n") + try + mktempdir() do dir + script = joinpath(dir, "build.jl") + obj = joinpath(dir, "img.o") + write(script, """ + Base.reinit_stdio() + @eval Sys BINDIR = ccall(:jl_get_julia_bindir, Any, ())::String + @eval Sys STDLIB = $(repr(Sys.STDLIB)) + copy!(LOAD_PATH, [$(repr(Base.active_project())), "@stdlib"]) + Base.init_depot_path() + using HostCPUFeatures, Static + const VT = NTuple{8,Core.VecElement{Float64}} + # The pattern VectorizationBase uses: emit the AVX-512 intrinsic iff + # has_feature reports it. If it leaked into this build, instruction + # selection aborts the image build / the call below. + if has_feature(Val(:x86_64_avx512f)) === True() + const RCP14 = ("declare <8 x double> @llvm.x86.avx512.rcp14.pd.512(<8 x double>, <8 x double>, i8) nounwind readnone\\ndefine <8 x double> @entry(<8 x double> %0) alwaysinline {\\n %res = call <8 x double> @llvm.x86.avx512.rcp14.pd.512(<8 x double> %0, <8 x double> zeroinitializer, i8 -1)\\n ret <8 x double> %res\\n}\\n", "entry") + @eval rcp(v::VT) = \$(Expr(:call, GlobalRef(Base, :llvmcall), RCP14, VT, Tuple{VT}, :v)) + else + @eval rcp(v::VT) = v + end + rcp(ntuple(_ -> Core.VecElement(1.5), Val(8))) + @assert has_feature(Val(:x86_64_avx512f)) === False() + println("image build OK") + """) + exe = Base.julia_cmd().exec[1] + img = unsafe_string(Base.JLOptions().image_file) + cmd = `$exe --startup-file=no --sysimage=$img --output-o=$obj -C skylake-avx512\;generic $script` + @test success(pipeline(cmd; stdout = devnull)) + @test isfile(obj) + end + finally + backup === nothing ? rm(prefsfile; force = true) : write(prefsfile, backup) + end + end + end + end