Skip to content

Add AEABI unaligned helpers to compiler-rt and make ARM backend lower unaligned i32/i64 loads/stores to __aeabi_uread/uwrite{4,8}#734

Merged
simpal01 merged 2 commits into
arm:release/arm-software/22.xfrom
simpal01:release/arm-software/22.x
Feb 24, 2026
Merged

Add AEABI unaligned helpers to compiler-rt and make ARM backend lower unaligned i32/i64 loads/stores to __aeabi_uread/uwrite{4,8}#734
simpal01 merged 2 commits into
arm:release/arm-software/22.xfrom
simpal01:release/arm-software/22.x

Conversation

@simpal01

Copy link
Copy Markdown
Contributor

Downstream issue:#727

This was committed upstream after the LLVM 22 branch was created. Mostly upstream will not accept it into the 22 branch, as it is not considered critical enough for backporting.

When targeting ARM (in particular architectures or variants that do not allow unaligned memory accesses, e.g., with -mno-unaligned-access or older cores like ARMv6-M) the backend currently handles unaligned loads or stores by expanding them into sequences of byte/half-word loads/stores.

This change proposes to detect unaligned loads/stores of size 4 bytes (i32) or 8 bytes (i64) when alignment is insufficient, and instead emit calls to the AEABI helper functions by llvm:

• __aeabi_uread4, __aeabi_uread8 for unaligned reads

• __aeabi_uwrite4, __aeabi_uwrite8 for unaligned writes

By lowering to these functions, code size can be reduced compared to in-lined byte/half-word sequences providing they are invoked more than once across a program. This also needs to add the implementations for the __aeabi_uread and __aeabi_uwrite helper functions to compiler-rt.

List of upstream PRs:

…r-rt (#167913)

This patch adds implementations for the __aeabi_uread and __aeabi_uwrite
helper functions to compiler-rt.

Without these helpers, LLVM would need to inline byte wise sequences ,
which can increases code size, especially at -Os/-Oz. Using the helper
functions allows to retain correctness while avoiding the code-size
growth.

GCC-based toolchains already provide these AEABI helpers, so supporting
them in compiler-rt ensures parity and avoids accidental dependencies on
libgcc when LLVM begins emitting these calls.

(cherry picked from commit 39413af)
When targeting architectures that do not support unaligned memory
accesses or when explictly pass -mno-unaligned-access, it requires the
compiler to expand each unaligned load/store into an inline sequences.
For 32-bit operations this typically involves:

	1. 4× LDRB (or 2× LDRH),
	2. multiple shift/or instructions

These sequences are emitted at every unaligned access site, and
therefore contribute significant code size in workloads that touch
packed or misaligned structures.

When compiling with -Oz and in combination with -mno-unaligned-access,
this patch lowers unaligned 32 bit and 64 bit loads and stores to below
AEABI heper calls:
```
         __aeabi_uread4
	__aeabi_uread8
	__aeabi_uwrite4
	__aeabi_uwrite8
```

And it provide a way to perform unaligned memory accesses on targets
that do not support them, such as ARMv6-M or when compiling with
-mno-unaligned-access. Although each use introduces a function call
making it less straightforward than using raw loads and stores the call
itself is often much smaller than the compiler emitted sequence of
multiple ldrb/strb operations. As a result, these helpers can greatly
reduce code-size providing they are invoked more than once across a
program.

1. Functions become smaller in AEABI mode once they contain more than a
few unaligned accesses.
2. The total image .text size becomes smaller whenever multiple
functions call the same helpers.

This PR is derived from https://reviews.llvm.org/D57595, with some minor
changes.
 Co-authored-by: David Green

(cherry picked from commit 09a6842)
@simpal01
simpal01 requested review from a team as code owners February 20, 2026 17:34
@github-actions github-actions Bot added the downstream-change Downstream change to LLVM tree label Feb 20, 2026
@github-actions

Copy link
Copy Markdown

This pull review modifies files outside of the arm-software directory, so please ensure it follows the Downstream Patch Policy.
An automated check will test if the tagging requirements have been met. Please wait for approving reviews from both Arm Toolchain for Embedded and Arm Toolchain for Linux teams before merging.

@simpal01 simpal01 changed the title Add AEABI unaligned helpers to compiler-rt and make LLVM ARM lower unaligned i32/i64 loads/stores to __aeabi_uread/uwrite{4,8} Add AEABI unaligned helpers to compiler-rt and make ARM backend lower unaligned i32/i64 loads/stores to __aeabi_uread/uwrite{4,8} Feb 20, 2026

@kiranchandramohan kiranchandramohan left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LG.

@simpal01
simpal01 merged commit 6ea4780 into arm:release/arm-software/22.x Feb 24, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

downstream-change Downstream change to LLVM tree

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants