Skip to content

Commit 105963a

Browse files
authored
[clang][bytecode] Use SmallVector for Function::Code (llvm#151821)
This way we can use resize_for_overwrite, which is slightly more efficient: https://llvm-compile-time-tracker.com/compare.php?from=7bdab76350970a3ac471da6a30035dd5b7ef14f3&to=b55bd2c74f230e2150e54b0523db6a8426eab54d&stat=instructions:u
1 parent 6df66a0 commit 105963a

File tree

4 files changed

+19
-19
lines changed

4 files changed

+19
-19
lines changed

clang/lib/AST/ByteCode/ByteCodeEmitter.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ int32_t ByteCodeEmitter::getOffset(LabelTy Label) {
135135
/// Helper to write bytecode and bail out if 32-bit offsets become invalid.
136136
/// Pointers will be automatically marshalled as 32-bit IDs.
137137
template <typename T>
138-
static void emit(Program &P, std::vector<std::byte> &Code, const T &Val,
139-
bool &Success) {
138+
static void emit(Program &P, llvm::SmallVectorImpl<std::byte> &Code,
139+
const T &Val, bool &Success) {
140140
size_t ValPos = Code.size();
141141
size_t Size;
142142

@@ -153,7 +153,7 @@ static void emit(Program &P, std::vector<std::byte> &Code, const T &Val,
153153
// Access must be aligned!
154154
assert(aligned(ValPos));
155155
assert(aligned(ValPos + Size));
156-
Code.resize(ValPos + Size);
156+
Code.resize_for_overwrite(ValPos + Size);
157157

158158
if constexpr (!std::is_pointer_v<T>) {
159159
new (Code.data() + ValPos) T(Val);
@@ -166,7 +166,7 @@ static void emit(Program &P, std::vector<std::byte> &Code, const T &Val,
166166
/// Emits a serializable value. These usually (potentially) contain
167167
/// heap-allocated memory and aren't trivially copyable.
168168
template <typename T>
169-
static void emitSerialized(std::vector<std::byte> &Code, const T &Val,
169+
static void emitSerialized(llvm::SmallVectorImpl<std::byte> &Code, const T &Val,
170170
bool &Success) {
171171
size_t ValPos = Code.size();
172172
size_t Size = align(Val.bytesToSerialize());
@@ -179,32 +179,32 @@ static void emitSerialized(std::vector<std::byte> &Code, const T &Val,
179179
// Access must be aligned!
180180
assert(aligned(ValPos));
181181
assert(aligned(ValPos + Size));
182-
Code.resize(ValPos + Size);
182+
Code.resize_for_overwrite(ValPos + Size);
183183

184184
Val.serialize(Code.data() + ValPos);
185185
}
186186

187187
template <>
188-
void emit(Program &P, std::vector<std::byte> &Code, const Floating &Val,
189-
bool &Success) {
188+
void emit(Program &P, llvm::SmallVectorImpl<std::byte> &Code,
189+
const Floating &Val, bool &Success) {
190190
emitSerialized(Code, Val, Success);
191191
}
192192

193193
template <>
194-
void emit(Program &P, std::vector<std::byte> &Code,
194+
void emit(Program &P, llvm::SmallVectorImpl<std::byte> &Code,
195195
const IntegralAP<false> &Val, bool &Success) {
196196
emitSerialized(Code, Val, Success);
197197
}
198198

199199
template <>
200-
void emit(Program &P, std::vector<std::byte> &Code, const IntegralAP<true> &Val,
201-
bool &Success) {
200+
void emit(Program &P, llvm::SmallVectorImpl<std::byte> &Code,
201+
const IntegralAP<true> &Val, bool &Success) {
202202
emitSerialized(Code, Val, Success);
203203
}
204204

205205
template <>
206-
void emit(Program &P, std::vector<std::byte> &Code, const FixedPoint &Val,
207-
bool &Success) {
206+
void emit(Program &P, llvm::SmallVectorImpl<std::byte> &Code,
207+
const FixedPoint &Val, bool &Success) {
208208
emitSerialized(Code, Val, Success);
209209
}
210210

clang/lib/AST/ByteCode/ByteCodeEmitter.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ class ByteCodeEmitter {
8888
/// Location of label relocations.
8989
llvm::DenseMap<LabelTy, llvm::SmallVector<unsigned, 5>> LabelRelocs;
9090
/// Program code.
91-
std::vector<std::byte> Code;
91+
llvm::SmallVector<std::byte> Code;
9292
/// Opcode to expression mapping.
9393
SourceMap SrcMap;
9494

clang/lib/AST/ByteCode/Context.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ bool Context::isPotentialConstantExpr(State &Parent, const FunctionDecl *FD) {
4545
Compiler<ByteCodeEmitter>(*this, *P).compileFunc(
4646
FD, const_cast<Function *>(Func));
4747

48-
++EvalID;
49-
// And run it.
50-
if (!Run(Parent, Func))
48+
if (!Func->isValid())
5149
return false;
5250

53-
return Func->isValid();
51+
++EvalID;
52+
// And run it.
53+
return Run(Parent, Func);
5454
}
5555

5656
void Context::isPotentialConstantExprUnevaluated(State &Parent, const Expr *E,

clang/lib/AST/ByteCode/Function.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ class Function final {
236236
bool HasRVO, bool IsLambdaStaticInvoker);
237237

238238
/// Sets the code of a function.
239-
void setCode(unsigned NewFrameSize, std::vector<std::byte> &&NewCode,
239+
void setCode(unsigned NewFrameSize, llvm::SmallVector<std::byte> &&NewCode,
240240
SourceMap &&NewSrcMap, llvm::SmallVector<Scope, 2> &&NewScopes,
241241
bool NewHasBody) {
242242
FrameSize = NewFrameSize;
@@ -266,7 +266,7 @@ class Function final {
266266
/// Size of the argument stack.
267267
unsigned ArgSize;
268268
/// Program code.
269-
std::vector<std::byte> Code;
269+
llvm::SmallVector<std::byte> Code;
270270
/// Opcode-to-expression mapping.
271271
SourceMap SrcMap;
272272
/// List of block descriptors.

0 commit comments

Comments
 (0)