seams-core v2.14.0
libyodaLib, the C++ engine of d-SEAMS
Loading...
Searching...
No Matches
cpu_features.hpp
Go to the documentation of this file.
1// Copyright (c) d-SEAMS developers
2#pragma once
3
4// x86 feature checks. __builtin_cpu_supports needs __cpu_model from
5// compiler-rt or libgcc, which some macOS toolchains do not link. CPUID
6// can trap to the hypervisor, so the answers are read once.
7// GCC and Clang keep the inline xgetbv. MSVC uses _xgetbv and __cpuidex.
8
9#include <compiler_compat.hpp>
10
11// clang-cl defines __x86_64__ and _MSC_VER. Clang's <cpuid.h> #defines
12// __cpuid, and the MSVC STL later includes <intrin.h>, whose declaration
13// void __cpuid(int[4], int) is then a macro invocation. Every _MSC_VER
14// compiler, including clang-cl, uses the intrinsic instead.
15#if defined(SEAMS_HOST_X86) && defined(_MSC_VER)
16#include <intrin.h>
17#elif defined(__x86_64__) || defined(__i386__)
18#include <cpuid.h>
19// Clang-cl defines both _MSC_VER and __clang__, so it takes this branch.
20// cpuid.h's __cpuid macro then rejects the function declaration in the
21// MSVC <intrin.h> that <chrono> includes.
22#ifdef __cpuid
23#undef __cpuid
24#endif
25#ifdef __cpuidex
26#undef __cpuidex
27#endif
28#endif
29
30namespace seams::cpu {
31
32struct Features {
33 bool avx2 = false;
34 bool fma = false;
35 bool avx512f = false;
36 bool avx512dq = false;
37 bool avx512vnni = false;
38 bool amxInt8 = false;
39};
40
42 Features out;
43#if defined(SEAMS_HOST_X86)
44 unsigned a = 0, b = 0, c = 0, d = 0;
45 unsigned xlo = 0;
46#if defined(_MSC_VER)
47 int regs[4] = {};
48 __cpuidex(regs, 1, 0);
49 a = static_cast<unsigned>(regs[0]);
50 b = static_cast<unsigned>(regs[1]);
51 c = static_cast<unsigned>(regs[2]);
52 d = static_cast<unsigned>(regs[3]);
53#else
54 if (!__get_cpuid(1, &a, &b, &c, &d)) {
55 return out;
56 }
57#endif
58 const bool osxsave = (c & (1u << 27)) != 0;
59 const bool avx = (c & (1u << 28)) != 0;
60 const bool fma = (c & (1u << 12)) != 0;
61 if (!osxsave || !avx) {
62 return out;
63 }
64#if defined(_MSC_VER)
65 xlo = static_cast<unsigned>(_xgetbv(0));
66#else
67 unsigned xhi = 0;
68 __asm__ volatile("xgetbv" : "=a"(xlo), "=d"(xhi) : "c"(0));
69 (void)xhi;
70#endif
71 // The OS saves the YMM state (bits 1, 2) and the ZMM state (bits 5-7).
72 const bool ymm = (xlo & 0x6u) == 0x6u;
73 const bool zmm = ymm && (xlo & 0xe0u) == 0xe0u;
74#if defined(_MSC_VER)
75 __cpuidex(regs, 7, 0);
76 a = static_cast<unsigned>(regs[0]);
77 b = static_cast<unsigned>(regs[1]);
78 c = static_cast<unsigned>(regs[2]);
79 d = static_cast<unsigned>(regs[3]);
80#else
81 if (!__get_cpuid_count(7, 0, &a, &b, &c, &d)) {
82 return out;
83 }
84#endif
85 out.fma = ymm && fma;
86 out.avx2 = ymm && (b & (1u << 5)) != 0;
87 out.avx512f = zmm && (b & (1u << 16)) != 0;
88 out.avx512dq = out.avx512f && (b & (1u << 17)) != 0;
89 out.avx512vnni = out.avx512f && (c & (1u << 11)) != 0;
90 // AMX-TILE and AMX-INT8, and the OS saves TILECFG and TILEDATA
91 // (bits 17, 18). Linux still has to grant TILEDATA per process.
92 out.amxInt8 = (d & (3u << 24)) == (3u << 24) && (xlo & 0x60000u) == 0x60000u;
93#endif
94 return out;
95}
96
97inline const Features &features() {
98 static const Features cached = detectFeatures();
99 return cached;
100}
101
102// false holds the kernels behind seams::ylm::avx512Kernels,
103// symm::avx2Kernels and the AVX2 cutoff stash scan on their generic path;
104// true leaves the choice to the CPU again. For tests.
105void allowSimdKernels(bool allow);
107
108} // namespace seams::cpu
const Features & features()
Features detectFeatures()
bool simdKernelsAllowed()
void allowSimdKernels(bool allow)