Skip to content

Commit 889175d

Browse files
Add cpu model init for Windows.
1 parent c2063de commit 889175d

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

compiler-rt/lib/builtins/cpu_model/aarch64.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ struct {
7676
#elif defined(__linux__) && __has_include(<sys/auxv.h>)
7777
#include "aarch64/fmv/mrs.inc"
7878
#include "aarch64/fmv/getauxval.inc"
79+
#elif defined(_WIN32)
80+
#include "aarch64/fmv/windows.inc"
7981
#else
8082
#include "aarch64/fmv/unimplemented.inc"
8183
#endif
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#ifndef _ARM64_
2+
#define _ARM64_
3+
#endif
4+
#include <processthreadsapi.h>
5+
#include <stdint.h>
6+
7+
void __init_cpu_features_resolver(unsigned long hwcap,
8+
const __ifunc_arg_t *arg) {}
9+
10+
void CONSTRUCTOR_ATTRIBUTE __init_cpu_features(void) {
11+
if (__atomic_load_n(&__aarch64_cpu_features.features, __ATOMIC_RELAXED))
12+
return;
13+
14+
#define setCPUFeature(F) features |= 1ULL << F
15+
16+
uint64_t features = 0;
17+
18+
setCPUFeature(FEAT_INIT);
19+
setCPUFeature(FEAT_FP);
20+
21+
// https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-isprocessorfeaturepresent
22+
if (IsProcessorFeaturePresent(PF_ARM_V8_CRC32_INSTRUCTIONS_AVAILABLE))
23+
setCPUFeature(FEAT_CRC);
24+
if (IsProcessorFeaturePresent(PF_ARM_V81_ATOMIC_INSTRUCTIONS_AVAILABLE))
25+
setCPUFeature(FEAT_LSE);
26+
if (IsProcessorFeaturePresent(PF_ARM_V82_DP_INSTRUCTIONS_AVAILABLE))
27+
setCPUFeature(FEAT_DOTPROD);
28+
29+
if (IsProcessorFeaturePresent(PF_ARM_V8_CRYPTO_INSTRUCTIONS_AVAILABLE)) {
30+
setCPUFeature(FEAT_AES);
31+
setCPUFeature(FEAT_SHA2);
32+
setCPUFeature(FEAT_PMULL);
33+
}
34+
if (IsProcessorFeaturePresent(PF_ARM_V83_JSCVT_INSTRUCTIONS_AVAILABLE))
35+
setCPUFeature(FEAT_JSCVT);
36+
37+
if (IsProcessorFeaturePresent(PF_ARM_V83_LRCPC_INSTRUCTIONS_AVAILABLE))
38+
setCPUFeature(FEAT_RCPC);
39+
40+
__atomic_store(&__aarch64_cpu_features.features, &features,
41+
__ATOMIC_RELAXED);
42+
}

compiler-rt/lib/builtins/cpu_model/cpu_model.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,15 @@
3131
// We're choosing init priority 90 to force our constructors to run before any
3232
// constructors in the end user application (starting at priority 101). This
3333
// value matches the libgcc choice for the same functions.
34-
#define CONSTRUCTOR_ATTRIBUTE __attribute__((constructor(90)))
34+
#ifdef _WIN64
35+
// Contructor that replaces the ifunc runs currently with prio 10, see
36+
// the LowerIFuncPass. The resolver of FMV depends on the cpu features so set
37+
// the priority to 9.
38+
#define CONSTRUCTOR_PRIOTITY 9
39+
#else
40+
#define CONSTRUCTOR_PRIOTITY 90
41+
#endif
42+
#define CONSTRUCTOR_ATTRIBUTE __attribute__((constructor(CONSTRUCTOR_PRIOTITY)))
3543
#else
3644
// FIXME: For MSVC, we should make a function pointer global in .CRT$X?? so that
3745
// this runs during initialization.

0 commit comments

Comments
 (0)