-
Notifications
You must be signed in to change notification settings - Fork 298
Refactor cpuid #1251
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
AntoinePrv
wants to merge
18
commits into
xtensor-stack:master
Choose a base branch
from
AntoinePrv:cpuid
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+406
−137
Open
Refactor cpuid #1251
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
1482e8e
Refactor x86 CPU features
AntoinePrv 44ab310
Remove unsafe memset
AntoinePrv 3c0af6f
Fix warning
AntoinePrv f4a8fae
Fix inline
AntoinePrv e27e3fe
Refactor bit utils
AntoinePrv a5b1732
Fix inline header in wrong location
AntoinePrv a5f9338
Fix cpuid aliases
AntoinePrv d324351
Add future TODO
AntoinePrv 1691bad
Fix missing var
AntoinePrv 525e8f8
Com[act lines
AntoinePrv a5cdb36
Add doc
AntoinePrv f629a99
Shorter name
AntoinePrv db51ffe
Add default minimal factory for xcr0
AntoinePrv bde1481
Move cpu features to config
AntoinePrv 8083e0c
Make register class implementation private
AntoinePrv 363258b
Document all xcr0 bits
AntoinePrv ad8556a
Improve x86 cpu feature code clarity
AntoinePrv acf30de
Refine documentation
AntoinePrv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,310 @@ | ||
| /*************************************************************************** | ||
| * Copyright (c) Johan Mabille, Sylvain Corlay, Wolf Vollprecht and * | ||
| * Martin Renou * | ||
| * Copyright (c) QuantStack * | ||
| * Copyright (c) Serge Guelton * | ||
| * * | ||
| * Distributed under the terms of the BSD 3-Clause License. * | ||
| * * | ||
| * The full license is in the file LICENSE, distributed with this software. * | ||
| ****************************************************************************/ | ||
|
|
||
| #ifndef XSIMD_CPU_FEATURES_X86_HPP | ||
| #define XSIMD_CPU_FEATURES_X86_HPP | ||
|
|
||
| #include <array> | ||
| #include <cassert> | ||
| #include <cstdint> | ||
|
|
||
| #include "../utils/bits.hpp" | ||
| #include "./xsimd_config.hpp" | ||
|
|
||
| #if XSIMD_TARGET_X86 && defined(_MSC_VER) | ||
| #include <intrin.h> // Contains the definition of __cpuidex | ||
| #endif | ||
|
|
||
| namespace xsimd | ||
| { | ||
| namespace detail | ||
| { | ||
| using cpuid_reg_t = std::array<int, 4>; | ||
| inline cpuid_reg_t get_cpuid(int level, int count = 0) noexcept; | ||
|
|
||
| using xcr0_reg_t = std::uint32_t; | ||
| inline xcr0_reg_t get_xcr0_low() noexcept; | ||
| } | ||
|
|
||
| /** | ||
| * CPU Identification (CPUID) instruction results. | ||
| * | ||
| * The CPUID instruction provides detailed information about the processor, | ||
| * including supported instruction set extensions (SSE, AVX, AVX-512, etc.). | ||
| * This utility parses CPUID leaf values to detect available CPU features. | ||
| * | ||
| * @see https://en.wikipedia.org/wiki/CPUID | ||
| */ | ||
| class x86_cpu_id | ||
| { | ||
| public: | ||
| /** Read the CpuId registers from the CPU if on the correct architecture. */ | ||
| inline static x86_cpu_id read() | ||
| { | ||
| cpu_id_regs regs = {}; | ||
| // TODO(C++20): Use designated initializer | ||
| regs.reg1 = detail::get_cpuid(0x1); | ||
| regs.reg7 = detail::get_cpuid(0x7); | ||
| regs.reg7a = detail::get_cpuid(0x7, 0x1); | ||
| regs.reg8 = detail::get_cpuid(0x80000001); | ||
| return x86_cpu_id(regs); | ||
| } | ||
|
|
||
| /** Create a value which return false to everything. */ | ||
| constexpr x86_cpu_id() noexcept = default; | ||
|
|
||
| constexpr bool sse2() const noexcept { return utils::all_bits_set<26>(m_regs.reg1[3]); } | ||
|
|
||
| constexpr bool sse3() const noexcept { return utils::all_bits_set<0>(m_regs.reg1[2]); } | ||
|
|
||
| constexpr bool ssse3() const noexcept { return utils::all_bits_set<9>(m_regs.reg1[2]); } | ||
|
|
||
| constexpr bool sse4_1() const noexcept { return utils::all_bits_set<19>(m_regs.reg1[2]); } | ||
|
|
||
| constexpr bool sse4_2() const noexcept { return utils::all_bits_set<20>(m_regs.reg1[2]); } | ||
|
|
||
| constexpr bool fma3() const noexcept { return utils::all_bits_set<12>(m_regs.reg1[2]); } | ||
|
|
||
| /** | ||
| * Indicates whether the OS has enabled extended state management. | ||
| * | ||
| * When true, the OS has set bit 18 (OSXSAVE) in the CR4 control register, | ||
| * enabling the XGETBV/XSETBV instructions to access XCR0 and support | ||
| * processor extended state management using XSAVE/XRSTOR. | ||
| * | ||
| * This value is read from CPUID leaf 0x1, ECX bit 27, which reflects | ||
| * the state of CR4.OSXSAVE. | ||
| */ | ||
| constexpr bool osxsave() const noexcept { return utils::all_bits_set<27>(m_regs.reg1[2]); } | ||
|
|
||
| constexpr bool avx() const noexcept { return utils::all_bits_set<28>(m_regs.reg1[2]); } | ||
|
|
||
| constexpr bool avx2() const noexcept { return utils::all_bits_set<5>(m_regs.reg7[1]); } | ||
|
|
||
| constexpr bool avx512f() const noexcept { return utils::all_bits_set<16>(m_regs.reg7[1]); } | ||
|
|
||
| constexpr bool avx512dq() const noexcept { return utils::all_bits_set<17>(m_regs.reg7[1]); } | ||
|
|
||
| constexpr bool avx512ifma() const noexcept { return utils::all_bits_set<21>(m_regs.reg7[1]); } | ||
|
|
||
| constexpr bool avx512pf() const noexcept { return utils::all_bits_set<26>(m_regs.reg7[1]); } | ||
|
|
||
| constexpr bool avx512er() const noexcept { return utils::all_bits_set<27>(m_regs.reg7[1]); } | ||
|
|
||
| constexpr bool avx512cd() const noexcept { return utils::all_bits_set<28>(m_regs.reg7[1]); } | ||
|
|
||
| constexpr bool avx512bw() const noexcept { return utils::all_bits_set<30>(m_regs.reg7[1]); } | ||
|
|
||
| constexpr bool avx512vbmi() const noexcept { return utils::all_bits_set<1>(m_regs.reg7[2]); } | ||
|
|
||
| constexpr bool avx512vbmi2() const noexcept { return utils::all_bits_set<6>(m_regs.reg7[2]); } | ||
|
|
||
| constexpr bool avx512vnni_bw() const noexcept { return utils::all_bits_set<11>(m_regs.reg7[2]); } | ||
|
|
||
| constexpr bool avxvnni() const noexcept { return utils::all_bits_set<4>(m_regs.reg7a[0]); } | ||
|
|
||
| constexpr bool fma4() const noexcept { return utils::all_bits_set<16>(m_regs.reg8[2]); } | ||
|
|
||
| private: | ||
| struct cpu_id_regs | ||
| { | ||
| using reg_t = detail::cpuid_reg_t; | ||
|
|
||
| reg_t reg1 = {}; | ||
| reg_t reg7 = {}; | ||
| reg_t reg7a = {}; | ||
| reg_t reg8 = {}; | ||
| }; | ||
|
|
||
| /** Parse CpuInfo register values into individual components. */ | ||
| constexpr explicit x86_cpu_id(const cpu_id_regs& regs) noexcept | ||
| : m_regs(regs) | ||
| { | ||
| } | ||
| cpu_id_regs m_regs = {}; | ||
| }; | ||
|
|
||
| /* | ||
| * Extended Control Register 0 (XCR0). | ||
| * | ||
| * Operating systems can explicitly disable the usage of instruction set (such | ||
| * as SSE or AVX extensions) by setting an appropriate flag in XCR0 register. | ||
| * This utility parses such bit values. | ||
| * | ||
| * @see https://docs.kernel.org/admin-guide/hw-vuln/gather_data_sampling.html | ||
| */ | ||
| class x86_xcr0 | ||
| { | ||
| public: | ||
| /** | ||
| * Create a default value with only SSE enabled. | ||
| * | ||
| * AVX and AVX512 strictly require OSXSAVE to be enabled by the OS. | ||
| * If OSXSAVE is disabled (e.g., via bcdedit /set xsavedisable 1), AVX state won't | ||
| * be preserved across context switches, so AVX cannot be used. | ||
| * SSE is therefore the only value safe to assume. | ||
| */ | ||
| constexpr static x86_xcr0 safe_default() noexcept | ||
| { | ||
| reg_t low = {}; | ||
| low = utils::make_bit_mask(static_cast<reg_t>(bit::sse)); | ||
| return x86_xcr0(low); | ||
| } | ||
|
|
||
| /** | ||
| * Read the XCR0 register from the CPU if on the correct architecture. | ||
| * | ||
| * This is only safe to call if bit 18 of CR4.OSXSAVE has been set. | ||
| * | ||
| * @see cpu_id::osxsave | ||
| */ | ||
| inline static x86_xcr0 read() | ||
| { | ||
| assert(x86_cpu_id::read().osxsave()); | ||
| return x86_xcr0(detail::get_xcr0_low()); | ||
| } | ||
|
|
||
| /** Create a value which return false to everything. */ | ||
| constexpr x86_xcr0() noexcept = default; | ||
|
|
||
| constexpr bool sse_enabled() const noexcept | ||
| { | ||
| return all_bits_set<bit::sse>(m_low); | ||
| } | ||
|
|
||
| constexpr bool avx_enabled() const noexcept | ||
| { | ||
| // Check both SSE and AVX bits even though AVX must imply SSE | ||
| return all_bits_set<bit::sse, bit::avx>(m_low); | ||
| } | ||
|
|
||
| constexpr bool avx512_enabled() const noexcept | ||
| { | ||
| // Check all SSE, AVX, and AVX512 bits even though AVX512 must | ||
| // imply AVX and SSE | ||
| return all_bits_set<bit::sse, bit::avx, bit::zmm_hi256>(m_low); | ||
| } | ||
|
|
||
| private: | ||
| using reg_t = detail::xcr0_reg_t; | ||
|
|
||
| enum class bit : reg_t | ||
| { | ||
| /** x87 FPU/MMX support (must be 1). */ | ||
| x87 = 0, | ||
| /** XSAVE support for MXCSR and XMM registers. */ | ||
| sse = 1, | ||
| /** AVX enabled and XSAVE support for upper halves of YMM registers. */ | ||
| avx = 2, | ||
| /** MPX enabled and XSAVE support for BND0-BND3 registers. */ | ||
| bndreg = 3, | ||
| /** MPX enabled and XSAVE support for BNDCFGU and BNDSTATUS registers. */ | ||
| bndcsr = 4, | ||
| /** AVX-512 enabled and XSAVE support for opmask registers k0-k7. */ | ||
| opmask = 5, | ||
| /** AVX-512 enabled and XSAVE support for upper halves of lower ZMM registers. */ | ||
| zmm_hi256 = 6, | ||
| /** AVX-512 enabled and XSAVE support for upper ZMM registers. */ | ||
| hi16_zmm = 7, | ||
| /** Saving/restoring Intel Processor Trace state via XSAVE enabled.*/ | ||
| processor_trace = 8, | ||
| /** XSAVE support for PKRU register. */ | ||
| pkru = 9, | ||
| }; | ||
|
|
||
| template <bit... Bits> | ||
| static constexpr bool all_bits_set(reg_t value) noexcept | ||
| { | ||
| return utils::all_bits_set<static_cast<reg_t>(Bits)...>(value); | ||
| } | ||
|
|
||
| /** Parse a XCR0 value into individual components. */ | ||
| constexpr explicit x86_xcr0(reg_t low) noexcept | ||
| : m_low(low) | ||
| { | ||
| } | ||
| reg_t m_low = {}; | ||
| }; | ||
|
|
||
| namespace detail | ||
| { | ||
| #if XSIMD_TARGET_X86 | ||
|
|
||
| inline cpuid_reg_t get_cpuid(int leaf, int subleaf) noexcept | ||
| { | ||
| cpuid_reg_t reg = {}; | ||
| #if defined(_MSC_VER) | ||
| __cpuidex(reg.data(), leaf, subleaf); | ||
|
|
||
| #elif defined(__INTEL_COMPILER) | ||
| __cpuid(reg.data(), leaf); | ||
|
|
||
| #elif defined(__GNUC__) || defined(__clang__) | ||
|
|
||
| #if defined(__i386__) && defined(__PIC__) | ||
| // %ebx may be the PIC register | ||
| __asm__("xchg{l}\t{%%}ebx, %1\n\t" | ||
| "cpuid\n\t" | ||
| "xchg{l}\t{%%}ebx, %1\n\t" | ||
| : "=a"(reg[0]), "=r"(reg[1]), "=c"(reg[2]), "=d"(reg[3]) | ||
| : "0"(leaf), "2"(subleaf)); | ||
|
|
||
| #else | ||
| __asm__("cpuid\n\t" | ||
| : "=a"(reg[0]), "=b"(reg[1]), "=c"(reg[2]), "=d"(reg[3]) | ||
| : "0"(leaf), "2"(subleaf)); | ||
| #endif | ||
| #endif | ||
| return reg; | ||
| } | ||
|
|
||
| inline xcr0_reg_t get_xcr0_low() noexcept | ||
| { | ||
| #if defined(_MSC_VER) | ||
| #if _MSC_VER >= 1400 | ||
| return static_cast<xcr0_reg_t>(_xgetbv(0)); | ||
| #else | ||
| #error "_MSC_VER < 1400 is not supported" | ||
| #endif | ||
|
|
||
| #elif defined(__GNUC__) | ||
| xcr0_reg_t xcr0 = {}; | ||
| __asm__( | ||
| "xorl %%ecx, %%ecx\n" | ||
| "xgetbv\n" | ||
| : "=a"(xcr0) | ||
| : | ||
| #if defined(__i386__) | ||
| : "ecx", "edx" | ||
| #else | ||
| : "rcx", "rdx" | ||
| #endif | ||
| ); | ||
| return xcr0; | ||
| #endif | ||
| } | ||
|
|
||
| #else // XSIMD_TARGET_X86 | ||
|
|
||
| inline cpuid_reg_t get_cpuid(int /* leaf */, int /* subleaf */) noexcept | ||
| { | ||
| return {}; // All bits to zero | ||
| } | ||
|
|
||
| inline xcr0_reg_t get_xcr0_low() noexcept | ||
| { | ||
| return {}; // All bits to zero | ||
| } | ||
|
|
||
| #endif // XSIMD_TARGET_X86 | ||
| } | ||
| } | ||
| #endif | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about
__clang__and__INTEL_COMPILER? Should we reproduce theget_cpuidpattern?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I think so. Also, IIRC,
__clang__comes with__GNUC__defined too, and Intel compiler defines__GNUC__or_MSC_VERdepending on the platform. So we may simplify this logic (but first we need to verify that my asumption is right).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's true that clang defines
__GNUC__. And it happens to be true for icx too, see https://godbolt.org/z/56j57vhnT