Skip to content

release/22.x: [MC] Explicitly use memcpy in emitBytes() (NFC) (#177187)#177320

Merged
c-rhodes merged 1 commit intollvm:release/22.xfrom
llvmbot:issue177187
Jan 23, 2026
Merged

release/22.x: [MC] Explicitly use memcpy in emitBytes() (NFC) (#177187)#177320
c-rhodes merged 1 commit intollvm:release/22.xfrom
llvmbot:issue177187

Conversation

@llvmbot
Copy link
Copy Markdown
Member

@llvmbot llvmbot commented Jan 22, 2026

Backport 15e421d

Requested by: @nikic

@llvmbot
Copy link
Copy Markdown
Member Author

llvmbot commented Jan 22, 2026

@MaskRay What do you think about merging this PR to the release branch?

@llvmbot llvmbot requested a review from MaskRay January 22, 2026 08:32
@llvmbot llvmbot added the llvm:mc Machine (object) code label Jan 22, 2026
@llvmbot
Copy link
Copy Markdown
Member Author

llvmbot commented Jan 22, 2026

@llvm/pr-subscribers-llvm-mc

Author: None (llvmbot)

Changes

Backport 15e421d

Requested by: @nikic


Full diff: https://github.com/llvm/llvm-project/pull/177320.diff

1 Files Affected:

  • (modified) llvm/lib/MC/MCObjectStreamer.cpp (+3-1)
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();
 }

@nikic nikic moved this from Needs Triage to Needs Review in LLVM Release Status Jan 22, 2026
@github-project-automation github-project-automation Bot moved this from Needs Review to Needs Merge in LLVM Release Status 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)
@c-rhodes c-rhodes merged commit e4ff2fc into llvm:release/22.x Jan 23, 2026
@github-project-automation github-project-automation Bot moved this from Needs Merge to Done in LLVM Release Status Jan 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

llvm:mc Machine (object) code

Projects

Development

Successfully merging this pull request may close these issues.

4 participants