-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[libc] implement memalignment
#132493
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
Merged
Merged
[libc] implement memalignment
#132493
Changes from 24 commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
152894e
[libc] implement `memalignment`
hulxv 000e4e2
chore: fix formatting of `memalignment.h`
hulxv 845184e
test: adding tests for `memalignment`
hulxv 224d792
chore: fix formatting of `memalignment_test.cpp`
hulxv 76b752d
[libc] add `memalignment` to `x86_64` entrypoints
hulxv 40ed080
[libc] fix header of `memalignment`
hulxv ce731cc
[libc] add `memalignment` to other architectures entrypoints
hulxv 4593534
[libc] add `memalignment` to `stdlib-mallic.yaml`
hulxv 8b50acb
[libc] improve `memalignment`
hulxv a2ae994
chore: correct file extension of `memalignment`
hulxv 931115f
refactor: use `cpp::countr_zero` instead of `__builtin_ctzg`
hulxv ff7a9bc
build: add `memalignment` to `CMakeLists.txt`
hulxv edc13a9
chore: add new line to the EOF of `memalignment_test.cpp`
hulxv 5ab7da5
fix: `memalignment` build entrypoints
hulxv 23320f4
fix: correct the standard of `memalignment`
hulxv eb201b3
build: correct build entry for `memalignment`
hulxv c1f9e6f
test: add `AlignasSpecifiedAlignment` for `memalignment` unit tests
hulxv 0a8b610
build: remove wrong entrypoint
hulxv 373c02d
[libc] missing newline
hulxv 5dab7c5
[libc] move `memalignment` to `stdlib.yaml`
hulxv af0644a
[libc] fix header of `memalignment_test.cpp`
hulxv 4c0d249
[libc] move `libc.src.stdlib.memalignment`to correct entrypoints
hulxv ac80029
[libc] remove wrong external entrypoint in `stdlib`
hulxv 203ce17
Merge branch 'main' into libc/feat/memalignment
hulxv e8c38bd
Update libc/src/stdlib/memalignment.cpp
jhuber6 46d9f09
[libc] add header to `memalignment.cpp`
hulxv cee9d85
fix formatting
hulxv 3b56885
[libc] remove `MallocedPointers`
hulxv 9d04fa4
[libc] fix wrong header
hulxv 778f87d
fix formatting
hulxv 9e5616d
[libc][test] fix wrong expectation in `LlvmLibcMemAlignmentTest.Speci…
hulxv 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
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
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
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
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
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
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,15 @@ | ||
| #include "src/stdlib/memalignment.h" | ||
| #include "src/__support/macros/config.h" | ||
| #include "src/__support/CPP/bit.h" | ||
| namespace LIBC_NAMESPACE_DECL { | ||
|
|
||
| LLVM_LIBC_FUNCTION(size_t, memalignment, (const void *p)) { | ||
| if (p == nullptr) | ||
| return 0; | ||
|
|
||
| uintptr_t addr = reinterpret_cast<uintptr_t>(p); | ||
|
|
||
| return 1 << cpp::countr_zero(addr); | ||
jhuber6 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
hulxv marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
hulxv marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
hulxv marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
jhuber6 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| } // namespace LIBC_NAMESPACE_DECL | ||
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,21 @@ | ||
| //===-- Implementation header for memalignment ------------------*- C++ -*-===// | ||
| // | ||
| // 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 | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #ifndef LLVM_LIBC_SRC_STDLIB_MEM_ALIGNMENT_H | ||
| #define LLVM_LIBC_SRC_STDLIB_MEM_ALIGNMENT_H | ||
|
|
||
| #include "src/__support/macros/config.h" | ||
| #include "src/__support/CPP/cstddef.h" | ||
|
|
||
| namespace LIBC_NAMESPACE_DECL { | ||
|
|
||
| size_t memalignment(const void *p); | ||
|
|
||
| } // namespace LIBC_NAMESPACE_DECL | ||
|
|
||
| #endif // LLVM_LIBC_SRC_STDLIB_LDIV_H |
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,83 @@ | ||
| //===-- Unittests for memalignment ----------------------------------------===// | ||
| // | ||
| // 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 | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #include "src/stdlib/free.h" | ||
| #include "src/stdlib/malloc.h" | ||
| #include "src/stdlib/memalignment.h" | ||
| #include "test/UnitTest/Test.h" | ||
|
|
||
| #include <stdint.h> | ||
|
|
||
| TEST(LlvmLibcMemAlignmentTest, NullPointer) { | ||
| void *ptr = nullptr; | ||
| EXPECT_EQ(LIBC_NAMESPACE::memalignment(ptr), static_cast<size_t>(0)); | ||
| } | ||
|
|
||
| TEST(LlvmLibcMemAlignmentTest, MallocedPointers) { | ||
| int *int_ptr = reinterpret_cast<int *>(LIBC_NAMESPACE::malloc(sizeof(int))); | ||
| EXPECT_NE(reinterpret_cast<void *>(int_ptr), static_cast<void *>(nullptr)); | ||
|
|
||
| size_t int_alignment = LIBC_NAMESPACE::memalignment(int_ptr); | ||
| EXPECT_GE(int_alignment, alignof(int)); | ||
|
|
||
| LIBC_NAMESPACE::free(int_ptr); | ||
|
|
||
| // Allocate a double (typically 8-byte aligned) | ||
| double *double_ptr = | ||
| reinterpret_cast<double *>(LIBC_NAMESPACE::malloc(sizeof(double))); | ||
| EXPECT_NE(reinterpret_cast<void *>(double_ptr), static_cast<void *>(nullptr)); | ||
|
|
||
| size_t double_alignment = LIBC_NAMESPACE::memalignment(double_ptr); | ||
| EXPECT_GE(double_alignment, alignof(double)); | ||
|
|
||
| EXPECT_EQ(double_alignment & (double_alignment - 1), static_cast<size_t>(0)); | ||
|
|
||
| LIBC_NAMESPACE::free(double_ptr); | ||
| } | ||
|
|
||
| TEST(LlvmLibcMemAlignmentTest, SpecificAlignment) { | ||
|
|
||
hulxv marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // These addresses have known alignment patterns - if we can construct them | ||
| uintptr_t addr_align2 = 0x2; // 2-byte aligned | ||
| uintptr_t addr_align4 = 0x4; // 4-byte aligned | ||
| uintptr_t addr_align8 = 0x8; // 8-byte aligned | ||
| uintptr_t addr_align16 = 0x10; // 16-byte aligned | ||
| uintptr_t addr_align32 = 0x20; // 32-byte aligned | ||
|
|
||
| void *ptr_align2 = reinterpret_cast<void *>(addr_align2); | ||
| void *ptr_align4 = reinterpret_cast<void *>(addr_align4); | ||
| void *ptr_align8 = reinterpret_cast<void *>(addr_align8); | ||
| void *ptr_align16 = reinterpret_cast<void *>(addr_align16); | ||
| void *ptr_align32 = reinterpret_cast<void *>(addr_align32); | ||
|
|
||
| EXPECT_EQ(LIBC_NAMESPACE::memalignment(ptr_align2), static_cast<size_t>(2)); | ||
| EXPECT_EQ(LIBC_NAMESPACE::memalignment(ptr_align4), static_cast<size_t>(4)); | ||
| EXPECT_EQ(LIBC_NAMESPACE::memalignment(ptr_align8), static_cast<size_t>(8)); | ||
| EXPECT_EQ(LIBC_NAMESPACE::memalignment(ptr_align16), static_cast<size_t>(16)); | ||
| EXPECT_EQ(LIBC_NAMESPACE::memalignment(ptr_align32), static_cast<size_t>(32)); | ||
|
|
||
| uintptr_t addr_complex = 0x1234560; // 16-byte aligned (ends in 0) | ||
| void *ptr_complex = reinterpret_cast<void *>(addr_complex); | ||
| EXPECT_EQ(LIBC_NAMESPACE::memalignment(ptr_complex), static_cast<size_t>(16)); | ||
hulxv marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| TEST(LlvmLibcMemAlignmentTest, AlignasSpecifiedAlignment) { | ||
| alignas(16) static int aligned_16; | ||
| alignas(32) static int aligned_32; | ||
| alignas(64) static int aligned_64; | ||
| alignas(128) static int aligned_128; | ||
| alignas(256) static int aligned_256; | ||
|
|
||
| EXPECT_GE(LIBC_NAMESPACE::memalignment(&aligned_16), static_cast<size_t>(16)); | ||
| EXPECT_GE(LIBC_NAMESPACE::memalignment(&aligned_32), static_cast<size_t>(32)); | ||
| EXPECT_GE(LIBC_NAMESPACE::memalignment(&aligned_64), static_cast<size_t>(64)); | ||
| EXPECT_GE(LIBC_NAMESPACE::memalignment(&aligned_128), | ||
| static_cast<size_t>(128)); | ||
| EXPECT_GE(LIBC_NAMESPACE::memalignment(&aligned_256), | ||
| static_cast<size_t>(256)); | ||
| } | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.