-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[Compiler-rt] Implement AEABI Unaligned Read/Write Helpers in compiler-rt #167913
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
simpal01
wants to merge
3
commits into
llvm:main
Choose a base branch
from
simpal01:add-aeabi-unaligned-helpers
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+190
−0
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
ce6febc
[Compiler-rt] Implement AEABI Unaligned Read/Write Helpers in compile…
simpal01 4a69df0
fixup! [Compiler-rt] Implement AEABI Unaligned Read/Write Helpers in …
simpal01 a3ce141
fixup! [Compiler-rt] Implement AEABI Unaligned Read/Write Helpers in …
simpal01 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| //===-- aeabi_uread4.c - ARM EABI Helper — Unaligned 4-Byte Memory Read ---===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| // Implements __aeabi_uread4 for unaligned memory accesses. | ||
| // Reference: Arm RTABI32 Specification. | ||
| // https://github.com/ARM-software/abi-aa/blob/main/rtabi32/rtabi32.rst#unaligned-memory-access | ||
| //===-------------------------------------------------------------------------------------===// | ||
|
|
||
| typedef struct { | ||
| char v[4]; | ||
| } v4; | ||
|
|
||
| int __aeabi_uread4(void *p) { | ||
| union { | ||
| v4 v; | ||
| int u; | ||
| } u; | ||
|
|
||
| u.v = *(v4 *)p; | ||
| return u.u; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| //===-- aeabi_uread8.c - ARM EABI Helper — Unaligned 8-Byte Memory Read----===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| // Implements __aeabi_uread8 for unaligned memory accesses. | ||
| // Reference: Arm RTABI32 Specification. | ||
| // https://github.com/ARM-software/abi-aa/blob/main/rtabi32/rtabi32.rst#unaligned-memory-access | ||
| //===-------------------------------------------------------------------------------------===// | ||
|
|
||
| typedef struct { | ||
| char v[8]; | ||
| } v8; | ||
|
|
||
| long long __aeabi_uread8(void *p) { | ||
| union { | ||
| v8 v; | ||
| long long u; | ||
| } u; | ||
|
|
||
| u.v = *(v8 *)p; | ||
| return u.u; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| //===-- aeabi_uwrite4.c - ARM EABI Helper — Unaligned 4-Byte Memory Write--===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| // Implements __aeabi_uwrite4 for unaligned memory accesses. | ||
| // Reference: Arm RTABI32 Specification. | ||
| // https://github.com/ARM-software/abi-aa/blob/main/rtabi32/rtabi32.rst#unaligned-memory-access | ||
| //===-------------------------------------------------------------------------------------===// | ||
|
|
||
| typedef struct { | ||
| char v[4]; | ||
| } v4; | ||
|
|
||
| int __aeabi_uwrite4(int val, void *p) { | ||
| union { | ||
| v4 v; | ||
| int u; | ||
| } u; | ||
|
|
||
| u.u = val; | ||
| *(v4 *)p = u.v; | ||
|
|
||
| return val; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| //===-- aeabi_uwrite8.c - ARM EABI Helper — Unaligned 8-Byte Memory Write--===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| // Implements __aeabi_uwrite8 for unaligned memory accesses. | ||
| // Reference: Arm RTABI32 Specification. | ||
| // https://github.com/ARM-software/abi-aa/blob/main/rtabi32/rtabi32.rst#unaligned-memory-access | ||
| //===-------------------------------------------------------------------------------------===// | ||
|
|
||
| typedef struct { | ||
| char v[8]; | ||
| } v8; | ||
|
|
||
| long long __aeabi_uwrite8(long long val, void *p) { | ||
| union { | ||
| v8 v; | ||
| long long u; | ||
| } u; | ||
|
|
||
| u.u = val; | ||
| *(v8 *)p = u.v; | ||
|
|
||
| return val; | ||
| } |
86 changes: 86 additions & 0 deletions
86
compiler-rt/test/builtins/Unit/arm/aeabi_unaligned_access_test.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| // REQUIRES: arm-target-arch || armv6m-target-arch | ||
| // RUN: %clang_builtins %s %librt -o %t && %run %t | ||
|
|
||
| #include <stdint.h> | ||
| #include <stdio.h> | ||
| #include <string.h> | ||
|
|
||
| extern int __aeabi_uread4(void *); | ||
| extern int __aeabi_uwrite4(int, void *); | ||
| extern long long __aeabi_uread8(void *); | ||
| extern long long __aeabi_uwrite8(long long, void *); | ||
|
|
||
| int test_unaligned(void) { | ||
| long long target8; | ||
| int target4; | ||
| const char source[] = "abcdefghijklmno"; | ||
| static char dest1[_Countof(source)], dest2[_Countof(source)]; | ||
| int i, j; | ||
|
|
||
| for (i = 0; i < 7; i++) { | ||
| memcpy(&target8, source + i, 8); | ||
| if (__aeabi_uread8(source + i) != target8) { | ||
| printf("error in __aeabi_uread8 => output = %llx, expected %llx\n", | ||
| __aeabi_uread8(source + i), target8); | ||
| return 1; | ||
| } | ||
|
|
||
| memcpy(dest1, source, _Countof(source)); | ||
| memcpy(dest2, source, _Countof(source)); | ||
| target8 = 0x4142434445464748ULL; | ||
| if (__aeabi_uwrite8(target8, dest1 + i) != target8) { | ||
| printf("error in __aeabi_uwrite8 => output = %llx, expected %llx\n", | ||
| __aeabi_uwrite8(target8, dest1 + i), target8); | ||
| return 1; | ||
| } | ||
| memcpy(dest2 + i, &target8, 8); | ||
| if (memcmp(dest1, dest2, _Countof(source)) != 0) { | ||
| int pos = -1; | ||
| printf("error in __aeabi_uwrite8: memcmp failed: buffers differ!\n"); | ||
| for (int j = 0; j < 8; ++j) { | ||
| if (dest1[j] != dest2[j]) { | ||
| pos = j; | ||
| break; | ||
| } | ||
| } | ||
| printf("error: 8-byte write mismatch at offset %d\n", pos); | ||
| return 1; | ||
| } | ||
|
|
||
| memcpy(&target4, source + i, 4); | ||
| if (__aeabi_uread4(source + i) != target4) { | ||
| printf("error in __aeabi_uread4 => output = %x, expected %x\n", | ||
| __aeabi_uread4(source + i), target4); | ||
| return 1; | ||
| } | ||
|
|
||
| memcpy(dest1, source, _Countof(source)); | ||
| memcpy(dest2, source, _Countof(source)); | ||
| target4 = 0x414243444; | ||
| if (__aeabi_uwrite4(target4, dest1 + i) != target4) { | ||
| printf("error in __aeabi_uwrite4 => output = %x, expected %x\n", | ||
| __aeabi_uwrite4(target4, dest1 + i), target4); | ||
| return 1; | ||
| } | ||
| memcpy(dest2 + i, &target4, 4); | ||
| if (memcmp(dest1, dest2, _Countof(source)) != 0) { | ||
| int pos = -1; | ||
| printf("error in __aeabi_uwrite4: memcmp failed: buffers differ!\n"); | ||
| for (int j = 0; j < 4; ++j) { | ||
| if (dest1[j] != dest2[j]) { | ||
| pos = j; | ||
| break; | ||
| } | ||
| } | ||
| printf("error: 4-byte write mismatch at offset %d\n", pos); | ||
| return 1; | ||
| } | ||
| } | ||
| return 0; | ||
| } | ||
|
|
||
| int main() { | ||
| if (test_unaligned()) | ||
| return 1; | ||
| return 0; | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was going to say that surely the read functions should take a
const void *, but in RTABI, they don't! I suppose because nobody ever calls these functions from C, so the C prototype isn't really important, only the machine-level PCS that it translates into.