-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[libc][c23] add memset_explicit #83577
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
48658ba
[libc][c23] add memset_explicit
SchrodingerZhu bb457ac
add noinline attr
SchrodingerZhu aa12ab7
address reviews
SchrodingerZhu c4d3ce2
remove extra code
SchrodingerZhu ff5fb31
undo aarch64 changes
SchrodingerZhu 364bb4b
remove template arg
SchrodingerZhu 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
//===-- Implementation of memset_explicit ---------------------------------===// | ||
// | ||
// 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/string/memset_explicit.h" | ||
#include "src/__support/common.h" | ||
#include "src/string/memory_utils/inline_memset.h" | ||
|
||
namespace LIBC_NAMESPACE { | ||
|
||
[[gnu::noinline]] LLVM_LIBC_FUNCTION(void *, memset_explicit, | ||
(void *dst, int value, size_t count)) { | ||
// Use the inline memset function to set the memory. | ||
inline_memset(dst, static_cast<uint8_t>(value), count); | ||
// avoid dead store elimination | ||
// The asm itself should also be sufficient to behave as a compiler barrier. | ||
asm("" : : "r"(dst) : "memory"); | ||
return dst; | ||
} | ||
|
||
} // namespace LIBC_NAMESPACE |
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,20 @@ | ||
//===-- Implementation header for memset_explicit ---------------*- 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_STRING_MEMSET_EXPLICIT_H | ||
#define LLVM_LIBC_SRC_STRING_MEMSET_EXPLICIT_H | ||
|
||
#include <stddef.h> // size_t | ||
|
||
namespace LIBC_NAMESPACE { | ||
|
||
[[gnu::noinline]] void *memset_explicit(void *ptr, int value, size_t count); | ||
|
||
} // namespace LIBC_NAMESPACE | ||
|
||
#endif // LLVM_LIBC_SRC_STRING_MEMSET_EXPLICIT_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,31 @@ | ||
//===-- Unittests for memset_explicit -------------------------------------===// | ||
// | ||
// 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 "memory_utils/memory_check_utils.h" | ||
#include "src/string/memset_explicit.h" | ||
#include "test/UnitTest/Test.h" | ||
|
||
namespace LIBC_NAMESPACE { | ||
|
||
// Apply the same tests as memset | ||
|
||
static inline void Adaptor(cpp::span<char> p1, uint8_t value, size_t size) { | ||
LIBC_NAMESPACE::memset_explicit(p1.begin(), value, size); | ||
} | ||
|
||
TEST(LlvmLibcmemsetExplicitTest, SizeSweep) { | ||
static constexpr size_t kMaxSize = 400; | ||
Buffer DstBuffer(kMaxSize); | ||
for (size_t size = 0; size < kMaxSize; ++size) { | ||
const char value = size % 10; | ||
auto dst = DstBuffer.span().subspan(0, size); | ||
ASSERT_TRUE((CheckMemset<Adaptor>(dst, value, size))); | ||
} | ||
} | ||
|
||
} // namespace LIBC_NAMESPACE |
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.
That seems inadequate. The idea is that
memset_explicit
works in situations wherememset
does not, so that's the functionality that really needs testing. Probably you need a lit test that showsmemset
is optimized away butmemset_explicit
is not. Or something like that.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.
FYI, we do not have
llvm-lit
for libc. Therefore, it is not a trivial thing to me to examine compiler behavior at this stage. Any suggestion?Uh oh!
There was an error while loading. Please reload this page.
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.
AFAIK, llvm-libc doesn't yet have testing frameworks in place to do what @pogo59 is suggesting. It may be useful to have at some point in the future, but I would say for right now, I struggle to think of additional cases where that would be useful. I'm sure that view will change over time and use cases for doing so will only grow, so we may end up eventually integrating some unit tests that make use of
llvm-lit
in the future. Because of the minimal need at the moment, I would say it's lower priority than say implementation work. When we have such testing framework in place, we should revisit verifying this invariant.Perhaps for now:
Over time, more use cases will be added to the issue which will help us reprioritize.