[llvm] [NFC][ADT] Replace memcpy with range-based copies (PR #229451)
Younes Yazami via llvm-commits
llvm-commits at lists.llvm.org
Tue Oct 6 08:14:39 PDT 2026
https://github.com/yelidris-public created https://github.com/llvm/llvm-project/pull/229451
None
>From 7e7977692709657b13be584523c2117163c56d04 Mon Sep 17 00:00:00 2001
From: yelidris <younes.elidrissiyazami at amd.com>
Date: Tue, 6 Oct 2026 11:02:07 -0400
Subject: [PATCH] [NFC][ADT] Replace memcpy with range-based copies
---
llvm/include/llvm/ADT/DenseMap.h | 9 +++++----
llvm/include/llvm/ADT/FunctionExtras.h | 5 +++--
llvm/include/llvm/Support/MathExtras.h | 15 ++++++++-------
3 files changed, 16 insertions(+), 13 deletions(-)
diff --git a/llvm/include/llvm/ADT/DenseMap.h b/llvm/include/llvm/ADT/DenseMap.h
index 56c56c489915e..0d217d15f286e 100644
--- a/llvm/include/llvm/ADT/DenseMap.h
+++ b/llvm/include/llvm/ADT/DenseMap.h
@@ -601,11 +601,12 @@ class DenseMapBase : public DebugEpochBase {
const unsigned NumBuckets = getNumBuckets();
UsedT *U = getUsed();
const UsedT *OtherU = other.getUsed();
- std::memcpy(U, OtherU,
- llvm::densemap::detail::usedWords(NumBuckets) * sizeof(UsedT));
+ llvm::copy(
+ ArrayRef<UsedT>(OtherU, llvm::densemap::detail::usedWords(NumBuckets)),
+ U);
if constexpr (densemap::detail::isRelocatableBucket<BucketT>) {
- memcpy(reinterpret_cast<void *>(Buckets), OtherBuckets,
- NumBuckets * sizeof(BucketT));
+ // BucketT is trivially copyable here (isRelocatableBucket constraint).
+ llvm::copy(ArrayRef<BucketT>(OtherBuckets, NumBuckets), Buckets);
} else {
llvm::densemap::detail::forEachUsed(U, NumBuckets, [&](unsigned I) {
::new (&Buckets[I].getFirst()) KeyT(OtherBuckets[I].getFirst());
diff --git a/llvm/include/llvm/ADT/FunctionExtras.h b/llvm/include/llvm/ADT/FunctionExtras.h
index 4d8f18e4cb3dc..636fc064afc71 100644
--- a/llvm/include/llvm/ADT/FunctionExtras.h
+++ b/llvm/include/llvm/ADT/FunctionExtras.h
@@ -204,8 +204,9 @@ template <typename ReturnT, typename... ParamTs> class UniqueFunctionBase {
DestroyMovePtr = RHS.DestroyMovePtr;
if (DestroyMovePtr)
DestroyMovePtr(this, &RHS);
- else // Trivial callable stored inline => memcpy.
- memcpy(&Storage.Inline, &RHS.Storage.Inline, InlineStorageSize);
+ else // Trivial callable stored inline; both arrays are InlineStorageSize
+ // std::bytes — copy via range so no separate count argument exists.
+ llvm::copy(RHS.Storage.Inline, std::begin(Storage.Inline));
RHS.CallPtr = nullptr;
RHS.DestroyMovePtr = nullptr; // Moved everything out of RHS.
diff --git a/llvm/include/llvm/Support/MathExtras.h b/llvm/include/llvm/Support/MathExtras.h
index e41e64fad0db6..697b96791f925 100644
--- a/llvm/include/llvm/Support/MathExtras.h
+++ b/llvm/include/llvm/Support/MathExtras.h
@@ -16,6 +16,7 @@
#include "llvm/ADT/STLForwardCompat.h"
#include "llvm/ADT/bit.h"
#include "llvm/Support/Compiler.h"
+#include <array>
#include <cassert>
#include <climits>
#include <cstdint>
@@ -134,13 +135,13 @@ template <typename T> constexpr T reverseBits(T Val) {
return __builtin_bitreverse64(Val);
#endif
- unsigned char in[sizeof(Val)];
- unsigned char out[sizeof(Val)];
- std::memcpy(in, &Val, sizeof(Val));
- for (unsigned i = 0; i < sizeof(Val); ++i)
- out[(sizeof(Val) - i) - 1] = BitReverseTable256[in[i]];
- std::memcpy(&Val, out, sizeof(Val));
- return Val;
+ // Reinterpret Val as a byte array, reverse into Out, then reinterpret back.
+ // bit_cast provides a compile-time size check and avoids raw byte counts.
+ auto In = llvm::bit_cast<std::array<unsigned char, sizeof(T)>>(Val);
+ std::array<unsigned char, sizeof(T)> Out;
+ for (unsigned i = 0; i < sizeof(T); ++i)
+ Out[(sizeof(T) - i) - 1] = BitReverseTable256[In[i]];
+ return llvm::bit_cast<T>(Out);
}
// NOTE: The following support functions use the _32/_64 extensions instead of
More information about the llvm-commits
mailing list