Skip to content

Commit

Permalink
Add CPUID submodule
Browse files Browse the repository at this point in the history
This is a small internal module which defines the API to query the Instruction
Set Architecture (ISA) of the current CPU.  This can be used for example to
select an artifact in a JLL package which is compatible with the current CPU.
  • Loading branch information
giordano committed Sep 8, 2020
1 parent f13e11c commit 4a9ed1e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
35 changes: 35 additions & 0 deletions base/sysinfo.jl
Original file line number Diff line number Diff line change
Expand Up @@ -526,4 +526,39 @@ function which(program_name::String)
end
which(program_name::AbstractString) = which(String(program_name))

### Submodule with information about CPU features
module CPUID

"""
ISA(features::Set{String})
A structure which represents the Instruction Set Architecture (ISA) of a
computer. It holds the `Set` of features of the CPU.
"""
struct ISA
features::Set{String}
end

Base.:<(a::ISA, b::ISA) = a.features < b.features

"""
CPU_ISA
The [`ISA`](@ref) (instruction set architecture) of the current CPU.
"""
const CPU_ISA = let features = filter!(f -> startswith(f, "+"),
split(unsafe_string(ccall(:LLVMGetHostCPUFeatures, Cstring, ())), ","))
ISA(Set(replace.(features, r"^\+" => "")))
end

# Sets of features for the x86-64 microarchitectures come from
# https://gcc.gnu.org/onlinedocs/gcc/x86-Options.html
const x86_64_generic = ISA(Set(("mmx", "sse", "sse2")))
const avx = ISA(Set((x86_64_generic.features..., "sse3", "ssse3", "sse4.1", "sse4.2", "popcnt", "avx", "aes", "pclmul")))
const avx2 = ISA(Set((avx.features..., "movbe", "avx2", "fsgsbase", "rdrnd", "fma", "bmi", "bmi2", "f16c")))
const avx512 = ISA(Set((avx2.features..., "pku", "rdseed", "adx", "prfchw", "clflushopt", "xsavec", "xsaves", "avx512f", "clwb", "avx512vl", "avx512bw", "avx512dq", "avx512cd")))
const x86_64_isas = [avx512, avx2, avx, x86_64_generic]

end # module CPUID

end # module Sys
4 changes: 4 additions & 0 deletions test/sysinfo.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@
sprint(Base.Sys.cpu_summary)
@test Base.Sys.uptime() > 0
Base.Sys.loadavg()

@test Base.Sys.CPUID.CPU_ISA isa Base.Sys.CPUID.ISA
@test Base.Sys.CPUID.x86_64_generic < Base.Sys.CPUID.avx2
@test Base.Sys.CPUID.avx512 > Base.Sys.CPUID.avx

0 comments on commit 4a9ed1e

Please sign in to comment.