Skip to content

Commit c083b38

Browse files
authored
[builtins][FMV][Apple] Use builtin atomic load/store, instead of libdispatch (#78807)
1 parent 43b1334 commit c083b38

File tree

1 file changed

+15
-13
lines changed
  • compiler-rt/lib/builtins/cpu_model/aarch64/fmv

1 file changed

+15
-13
lines changed

compiler-rt/lib/builtins/cpu_model/aarch64/fmv/apple.inc

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,17 @@ static bool isKnownAndSupported(const char *name) {
1414
void __init_cpu_features_resolver(void) {
1515
// On Darwin platforms, this may be called concurrently by multiple threads
1616
// because the resolvers that use it are called lazily at runtime (unlike on
17-
// ELF platforms, where IFuncs are resolved serially at load time). This
18-
// function's effect on __aarch64_cpu_features should be idempotent, but even
19-
// so we need dispatch_once to resolve the race condition. Dispatch is
20-
// available through libSystem, which we need anyway for the sysctl, so this
21-
// does not add a new dependency.
17+
// ELF platforms, where IFuncs are resolved serially at load time). This
18+
// function's effect on __aarch64_cpu_features must be idempotent.
19+
20+
if (!__atomic_load_n(&__aarch64_cpu_features.features, __ATOMIC_RELAXED)) {
21+
uint64_t features = 0;
2222

23-
static dispatch_once_t onceToken = 0;
24-
dispatch_once(&onceToken, ^{
2523
// https://developer.apple.com/documentation/kernel/1387446-sysctlbyname/determining_instruction_set_characteristics
2624
static const struct {
2725
const char *sysctl_name;
2826
enum CPUFeatures feature;
29-
} features[] = {
27+
} feature_checks[] = {
3028
{"hw.optional.arm.FEAT_FlagM", FEAT_FLAGM},
3129
{"hw.optional.arm.FEAT_FlagM2", FEAT_FLAGM2},
3230
{"hw.optional.arm.FEAT_FHM", FEAT_FP16FML},
@@ -58,12 +56,16 @@ void __init_cpu_features_resolver(void) {
5856
{"hw.optional.arm.FEAT_BTI", FEAT_BTI},
5957
};
6058

61-
for (size_t I = 0, E = sizeof(features) / sizeof(features[0]); I != E; ++I)
62-
if (isKnownAndSupported(features[I].sysctl_name))
63-
__aarch64_cpu_features.features |= (1ULL << features[I].feature);
59+
for (size_t I = 0, E = sizeof(feature_checks) / sizeof(feature_checks[0]);
60+
I != E; ++I)
61+
if (isKnownAndSupported(feature_checks[I].sysctl_name))
62+
features |= (1ULL << feature_checks[I].feature);
63+
64+
features |= (1ULL << FEAT_INIT);
6465

65-
__aarch64_cpu_features.features |= (1ULL << FEAT_INIT);
66-
});
66+
__atomic_store(&__aarch64_cpu_features.features, &features,
67+
__ATOMIC_RELAXED);
68+
}
6769
}
6870

6971
#endif // TARGET_OS_OSX || TARGET_OS_IPHONE

0 commit comments

Comments
 (0)