Skip to content

Commit e314622

Browse files
authored
[clang][driver] Allow unaligned access on ARMv7 and higher by default (#82400)
ARM's Clang and GCC embedded compilers default to allowing unaligned access for ARMv7+. This patch changes the Clang driver default to match. Users can opt out with `-mno-unaligned-access`. Fixes #59560
1 parent e2f0826 commit e314622

File tree

3 files changed

+38
-12
lines changed

3 files changed

+38
-12
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,17 @@ X86 Support
302302
Arm and AArch64 Support
303303
^^^^^^^^^^^^^^^^^^^^^^^
304304

305+
- ARMv7+ targets now default to allowing unaligned access, except Armv6-M, and
306+
Armv8-M without the Main Extension. Baremetal targets should check that the
307+
new default will work with their system configurations, since it requires
308+
that SCTLR.A is 0, SCTLR.U is 1, and that the memory in question is
309+
configured as "normal" memory. This brings Clang in-line with the default
310+
settings for GCC and Arm Compiler. Aside from making Clang align with other
311+
compilers, changing the default brings major performance and code size
312+
improvements for most targets. We have not changed the default behavior for
313+
ARMv6, but may revisit that decision in the future. Users can restore the old
314+
behavior with -m[no-]unaligned-access.
315+
305316
Android Support
306317
^^^^^^^^^^^^^^^
307318

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -890,25 +890,25 @@ llvm::ARM::FPUKind arm::getARMTargetFeatures(const Driver &D,
890890
// SCTLR.U bit, which is architecture-specific. We assume ARMv6
891891
// Darwin and NetBSD targets support unaligned accesses, and others don't.
892892
//
893-
// ARMv7 always has SCTLR.U set to 1, but it has a new SCTLR.A bit
894-
// which raises an alignment fault on unaligned accesses. Linux
895-
// defaults this bit to 0 and handles it as a system-wide (not
896-
// per-process) setting. It is therefore safe to assume that ARMv7+
897-
// Linux targets support unaligned accesses. The same goes for NaCl
898-
// and Windows.
893+
// ARMv7 always has SCTLR.U set to 1, but it has a new SCTLR.A bit which
894+
// raises an alignment fault on unaligned accesses. Assume ARMv7+ supports
895+
// unaligned accesses, except ARMv6-M, and ARMv8-M without the Main
896+
// Extension. This aligns with the default behavior of ARM's downstream
897+
// versions of GCC and Clang.
899898
//
900-
// The above behavior is consistent with GCC.
899+
// Users can change the default behavior via -m[no-]unaliged-access.
901900
int VersionNum = getARMSubArchVersionNumber(Triple);
902901
if (Triple.isOSDarwin() || Triple.isOSNetBSD()) {
903902
if (VersionNum < 6 ||
904903
Triple.getSubArch() == llvm::Triple::SubArchType::ARMSubArch_v6m)
905904
Features.push_back("+strict-align");
906-
} else if (Triple.isOSLinux() || Triple.isOSNaCl() ||
907-
Triple.isOSWindows()) {
908-
if (VersionNum < 7)
909-
Features.push_back("+strict-align");
910-
} else
905+
} else if (VersionNum < 7 ||
906+
Triple.getSubArch() ==
907+
llvm::Triple::SubArchType::ARMSubArch_v6m ||
908+
Triple.getSubArch() ==
909+
llvm::Triple::SubArchType::ARMSubArch_v8m_baseline) {
911910
Features.push_back("+strict-align");
911+
}
912912
}
913913

914914
// llvm does not support reserving registers in general. There is support

clang/test/Driver/arm-alignment.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,21 @@
2222
// RUN: %clang -target armv7-windows -### %s 2> %t
2323
// RUN: FileCheck --check-prefix=CHECK-UNALIGNED-ARM < %t %s
2424

25+
// RUN: %clang --target=armv6 -### %s 2> %t
26+
// RUN: FileCheck --check-prefix=CHECK-ALIGNED-ARM < %t %s
27+
28+
// RUN: %clang --target=armv7 -### %s 2> %t
29+
// RUN: FileCheck --check-prefix=CHECK-UNALIGNED-ARM < %t %s
30+
31+
// RUN: %clang -target thumbv6m-none-gnueabi -mcpu=cortex-m0 -### %s 2> %t
32+
// RUN: FileCheck --check-prefix CHECK-ALIGNED-ARM <%t %s
33+
34+
// RUN: %clang -target thumb-none-gnueabi -mcpu=cortex-m0 -### %s 2> %t
35+
// RUN: FileCheck --check-prefix CHECK-ALIGNED-ARM <%t %s
36+
37+
// RUN: %clang -target thumbv8m.base-none-gnueabi -### %s 2> %t
38+
// RUN: FileCheck --check-prefix CHECK-ALIGNED-ARM <%t %s
39+
2540
// RUN: %clang --target=aarch64 -munaligned-access -### %s 2> %t
2641
// RUN: FileCheck --check-prefix=CHECK-UNALIGNED-AARCH64 < %t %s
2742

0 commit comments

Comments
 (0)