https://github.com/tbaederr created 
https://github.com/llvm/llvm-project/pull/151824

We don't need to align Code.size(), since we always resize it to aligned 
values, so Code.size() is always aligned.

>From 6e241456b8ca3cd50fe89c42a225e704ccf463d1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbae...@redhat.com>
Date: Sat, 2 Aug 2025 16:51:19 +0200
Subject: [PATCH] [clang][bytecode][NFC] Code size is always aligned

We don't need to align Code.size(), since we always resize it to aligned
values, so Code.size() is always aligned.
---
 clang/lib/AST/ByteCode/ByteCodeEmitter.cpp | 19 +++++++++----------
 1 file changed, 9 insertions(+), 10 deletions(-)

diff --git a/clang/lib/AST/ByteCode/ByteCodeEmitter.cpp 
b/clang/lib/AST/ByteCode/ByteCodeEmitter.cpp
index 3288585683c10..d4746052c5cfe 100644
--- a/clang/lib/AST/ByteCode/ByteCodeEmitter.cpp
+++ b/clang/lib/AST/ByteCode/ByteCodeEmitter.cpp
@@ -137,21 +137,21 @@ int32_t ByteCodeEmitter::getOffset(LabelTy Label) {
 template <typename T>
 static void emit(Program &P, std::vector<std::byte> &Code, const T &Val,
                  bool &Success) {
+  size_t ValPos = Code.size();
   size_t Size;
 
   if constexpr (std::is_pointer_v<T>)
-    Size = sizeof(uint32_t);
+    Size = align(sizeof(uint32_t));
   else
-    Size = sizeof(T);
+    Size = align(sizeof(T));
 
-  if (Code.size() + Size > std::numeric_limits<unsigned>::max()) {
+  if (ValPos + Size > std::numeric_limits<unsigned>::max()) {
     Success = false;
     return;
   }
 
   // Access must be aligned!
-  size_t ValPos = align(Code.size());
-  Size = align(Size);
+  assert(aligned(ValPos));
   assert(aligned(ValPos + Size));
   Code.resize(ValPos + Size);
 
@@ -168,17 +168,16 @@ static void emit(Program &P, std::vector<std::byte> 
&Code, const T &Val,
 template <typename T>
 static void emitSerialized(std::vector<std::byte> &Code, const T &Val,
                            bool &Success) {
-  size_t Size = Val.bytesToSerialize();
+  size_t ValPos = Code.size();
+  size_t Size = align(Val.bytesToSerialize());
 
-  if (Code.size() + Size > std::numeric_limits<unsigned>::max()) {
+  if (ValPos + Size > std::numeric_limits<unsigned>::max()) {
     Success = false;
     return;
   }
 
   // Access must be aligned!
-  assert(aligned(Code.size()));
-  size_t ValPos = Code.size();
-  Size = align(Size);
+  assert(aligned(ValPos));
   assert(aligned(ValPos + Size));
   Code.resize(ValPos + Size);
 

_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to