Skip to content

Commit 8fa0f0e

Browse files
authored
[clang] [ARM] Explicitly enable NEON for Windows/Darwin targets (llvm#122095)
Upstream LLVM implicitly enables NEON for any ARMv7 target. Many platform ABIs with an ARMv7 baseline also include NEON in that, this is the case on e.g. Windows and iOS. On Linux, however, things are not quite as clearly defined. Some distributions target an ARMv7 baseline without NEON available (this is the case for e.g. Debian/Ubuntu for the "armhf" architecture). To achieve this, Debian/Ubuntu patch LLVM downstream to make ARMv7 only implicitly enable VPFv3-D16, not NEON - see [1]. That patch has the (unintended) effect that NEON no longer is available by default for Windows/ARMv7 and iOS/ARMv7. In practice, when compiling C for Windows/ARMv7 with Debian patched Clang, NEON actually still is available, but not when compiling assembly files. This is due to ARM::getARMCPUForArch (in llvm/lib/TargetParser/ARMTargetParser.cpp) returning "cortex-a9" for Windows. This difference, between C and assembly, is due to how getARMTargetCPU is called in getARMTargetFeatures (in clang/lib/Driver/ToolChains/Arch/ARM.cpp) to get defaults, only when ForAS is not set - see [2]. There is an existing case in getARMTargetFeatures, for Android, which explicitly enables NEON when targeting ARM >= v7. As Windows and iOS have NEON as part of their ABI baseline just like Android does these days (see [3] for where this default was added for Android), add the implicit default in a similar way. However, first do the general lookup of getARMTargetCPU (unless ForAS); this makes sure that we get the same default CPU as before ("cortex-a9" for Windows and "swift" for the "armv7s" architecture on Darwin). [1] https://salsa.debian.org/pkg-llvm-team/llvm-toolchain/-/blob/19/debian/patches/clang-arm-default-vfp3-on-armv7a.patch?ref_type=heads [2] llvm@b8baa2a [3] llvm@d0fbef9
1 parent 94609ae commit 8fa0f0e

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

clang/lib/Driver/ToolChains/Arch/ARM.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,13 +659,21 @@ llvm::ARM::FPUKind arm::getARMTargetFeatures(const Driver &D,
659659
CPUArgFPUKind != llvm::ARM::FK_INVALID ? CPUArgFPUKind : ArchArgFPUKind;
660660
(void)llvm::ARM::getFPUFeatures(FPUKind, Features);
661661
} else {
662+
bool Generic = true;
662663
if (!ForAS) {
663664
std::string CPU = arm::getARMTargetCPU(CPUName, ArchName, Triple);
665+
if (CPU != "generic")
666+
Generic = false;
664667
llvm::ARM::ArchKind ArchKind =
665668
arm::getLLVMArchKindForARM(CPU, ArchName, Triple);
666669
FPUKind = llvm::ARM::getDefaultFPU(CPU, ArchKind);
667670
(void)llvm::ARM::getFPUFeatures(FPUKind, Features);
668671
}
672+
if (Generic && (Triple.isOSWindows() || Triple.isOSDarwin()) &&
673+
getARMSubArchVersionNumber(Triple) >= 7) {
674+
FPUKind = llvm::ARM::parseFPU("neon");
675+
(void)llvm::ARM::getFPUFeatures(FPUKind, Features);
676+
}
669677
}
670678

671679
// Now we've finished accumulating features from arch, cpu and fpu,

clang/test/Driver/arm-mfpu.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,8 +356,10 @@
356356
// CHECK-HF-DAG: "-target-cpu" "arm1176jzf-s"
357357

358358
// RUN: %clang -target armv7-apple-darwin -x assembler %s -### -c 2>&1 \
359-
// RUN: | FileCheck --check-prefix=ASM %s
360-
// ASM-NOT: -target-feature
359+
// RUN: | FileCheck --check-prefix=ASM-NEON %s
360+
// RUN: %clang -target armv7-windows -x assembler %s -### -c 2>&1 \
361+
// RUN: | FileCheck --check-prefix=ASM-NEON %s
362+
// ASM-NEON: "-target-feature" "+neon"
361363

362364
// RUN: %clang -target armv8-linux-gnueabi -mfloat-abi=soft -mfpu=none %s -### -c 2>&1 \
363365
// RUN: | FileCheck --check-prefix=CHECK-SOFT-ABI-FP %s

clang/test/Preprocessor/arm-target-features.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,30 @@
132132
// CHECK-V7VE-DEFAULT-ABI-SOFT: #define __ARM_ARCH_EXT_IDIV__ 1
133133
// CHECK-V7VE-DEFAULT-ABI-SOFT: #define __ARM_FP 0xc
134134

135+
// RUN: %clang -target x86_64-apple-macosx10.10 -arch armv7 -x c -E -dM %s -o - | FileCheck -match-full-lines --check-prefix=CHECK-DARWIN-V7 %s
136+
// CHECK-DARWIN-V7: #define __ARMEL__ 1
137+
// CHECK-DARWIN-V7: #define __ARM_ARCH 7
138+
// CHECK-DARWIN-V7: #define __ARM_ARCH_7A__ 1
139+
// CHECK-DARWIN-V7-NOT: __ARM_FEATURE_CRC32
140+
// CHECK-DARWIN-V7-NOT: __ARM_FEATURE_NUMERIC_MAXMIN
141+
// CHECK-DARWIN-V7-NOT: __ARM_FEATURE_DIRECTED_ROUNDING
142+
// CHECK-DARWIN-V7: #define __ARM_FP 0xc
143+
// CHECK-DARWIN-V7: #define __ARM_NEON 1
144+
// CHECK-DARWIN-V7: #define __ARM_NEON_FP 0x4
145+
// CHECK-DARWIN-V7: #define __ARM_NEON__ 1
146+
147+
// RUN: %clang -target armv7-windows -x c -E -dM %s -o - | FileCheck -match-full-lines --check-prefix=CHECK-WINDOWS-V7 %s
148+
// CHECK-WINDOWS-V7: #define __ARMEL__ 1
149+
// CHECK-WINDOWS-V7: #define __ARM_ARCH 7
150+
// CHECK-WINDOWS-V7: #define __ARM_ARCH_7A__ 1
151+
// CHECK-WINDOWS-V7-NOT: __ARM_FEATURE_CRC32
152+
// CHECK-WINDOWS-V7-NOT: __ARM_FEATURE_NUMERIC_MAXMIN
153+
// CHECK-WINDOWS-V7-NOT: __ARM_FEATURE_DIRECTED_ROUNDING
154+
// CHECK-WINDOWS-V7: #define __ARM_FP 0xe
155+
// CHECK-WINDOWS-V7: #define __ARM_NEON 1
156+
// CHECK-WINDOWS-V7: #define __ARM_NEON_FP 0x6
157+
// CHECK-WINDOWS-V7: #define __ARM_NEON__ 1
158+
135159
// RUN: %clang -target x86_64-apple-macosx10.10 -arch armv7s -x c -E -dM %s -o - | FileCheck -match-full-lines --check-prefix=CHECK-V7S %s
136160
// CHECK-V7S: #define __ARMEL__ 1
137161
// CHECK-V7S: #define __ARM_ARCH 7
@@ -140,6 +164,9 @@
140164
// CHECK-V7S-NOT: __ARM_FEATURE_NUMERIC_MAXMIN
141165
// CHECK-V7S-NOT: __ARM_FEATURE_DIRECTED_ROUNDING
142166
// CHECK-V7S: #define __ARM_FP 0xe
167+
// CHECK-V7S: #define __ARM_NEON 1
168+
// CHECK-V7S: #define __ARM_NEON_FP 0x6
169+
// CHECK-V7S: #define __ARM_NEON__ 1
143170

144171
// RUN: %clang -target arm-arm-none-eabi -march=armv7-m -mfloat-abi=soft -x c -E -dM %s | FileCheck -match-full-lines --check-prefix=CHECK-VFP-FP %s
145172
// RUN: %clang -target arm-arm-none-eabi -march=armv7-m -mfloat-abi=softfp -x c -E -dM %s | FileCheck -match-full-lines --check-prefix=CHECK-VFP-FP %s

0 commit comments

Comments
 (0)