Skip to content

Commit c304a2b

Browse files
authored
[clang][bytecode][NFC] Code size is always aligned (#151824)
We don't need to align Code.size(), since we always resize it to aligned values, so Code.size() is always aligned.
1 parent 969b246 commit c304a2b

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

clang/lib/AST/ByteCode/ByteCodeEmitter.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -137,21 +137,21 @@ int32_t ByteCodeEmitter::getOffset(LabelTy Label) {
137137
template <typename T>
138138
static void emit(Program &P, std::vector<std::byte> &Code, const T &Val,
139139
bool &Success) {
140+
size_t ValPos = Code.size();
140141
size_t Size;
141142

142143
if constexpr (std::is_pointer_v<T>)
143-
Size = sizeof(uint32_t);
144+
Size = align(sizeof(uint32_t));
144145
else
145-
Size = sizeof(T);
146+
Size = align(sizeof(T));
146147

147-
if (Code.size() + Size > std::numeric_limits<unsigned>::max()) {
148+
if (ValPos + Size > std::numeric_limits<unsigned>::max()) {
148149
Success = false;
149150
return;
150151
}
151152

152153
// Access must be aligned!
153-
size_t ValPos = align(Code.size());
154-
Size = align(Size);
154+
assert(aligned(ValPos));
155155
assert(aligned(ValPos + Size));
156156
Code.resize(ValPos + Size);
157157

@@ -168,17 +168,16 @@ static void emit(Program &P, std::vector<std::byte> &Code, const T &Val,
168168
template <typename T>
169169
static void emitSerialized(std::vector<std::byte> &Code, const T &Val,
170170
bool &Success) {
171-
size_t Size = Val.bytesToSerialize();
171+
size_t ValPos = Code.size();
172+
size_t Size = align(Val.bytesToSerialize());
172173

173-
if (Code.size() + Size > std::numeric_limits<unsigned>::max()) {
174+
if (ValPos + Size > std::numeric_limits<unsigned>::max()) {
174175
Success = false;
175176
return;
176177
}
177178

178179
// Access must be aligned!
179-
assert(aligned(Code.size()));
180-
size_t ValPos = Code.size();
181-
Size = align(Size);
180+
assert(aligned(ValPos));
182181
assert(aligned(ValPos + Size));
183182
Code.resize(ValPos + Size);
184183

0 commit comments

Comments
 (0)