release/22.x: [MC] Explicitly use memcpy in emitBytes() (NFC) (#177187)#177320
Merged
c-rhodes merged 1 commit intollvm:release/22.xfrom Jan 23, 2026
Merged
release/22.x: [MC] Explicitly use memcpy in emitBytes() (NFC) (#177187)#177320c-rhodes merged 1 commit intollvm:release/22.xfrom
c-rhodes merged 1 commit intollvm:release/22.xfrom
Conversation
Member
Author
|
@MaskRay What do you think about merging this PR to the release branch? |
Member
Author
|
@llvm/pr-subscribers-llvm-mc Author: None (llvmbot) ChangesBackport 15e421d Requested by: @nikic 1 Files Affected:
diff --git a/llvm/lib/MC/MCObjectStreamer.cpp b/llvm/lib/MC/MCObjectStreamer.cpp
index 94468140a30b9..5fd30eccb45c5 100644
--- a/llvm/lib/MC/MCObjectStreamer.cpp
+++ b/llvm/lib/MC/MCObjectStreamer.cpp
@@ -109,7 +109,9 @@ void MCObjectStreamer::addSpecialFragment(MCFragment *Frag) {
void MCObjectStreamer::appendContents(ArrayRef<char> Contents) {
ensureHeadroom(Contents.size());
assert(FragSpace >= Contents.size());
- llvm::copy(Contents, getCurFragEnd());
+ // As this is performance-sensitive code, explicitly use std::memcpy.
+ // Optimization of std::copy to memmove is unreliable.
+ std::memcpy(getCurFragEnd(), Contents.begin(), Contents.size());
CurFrag->FixedSize += Contents.size();
FragSpace -= Contents.size();
}
|
MaskRay
approved these changes
Jan 22, 2026
We've observed a compile-time regression in LLVM 22 when including large blobs. The root cause was that emitBytes() was copying bytes one-by-one, which is much slower than using memcpy for large objects. Optimization of std::copy to memmove is apparently much less reliable than one might think. In particular, when using a non-bleeding-edge libstdc++ (anything older than version 15), this does not happen if the types of the input and output iterators do not match (like here, where there is a signed/unsigned mismatch). As this code is performance sensitive, I think it makes sense to directly use memcpy. Previously this code used SmallVector::append, which explicitly uses memcpy. (cherry picked from commit 15e421d)
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Backport 15e421d
Requested by: @nikic