Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions clang/lib/AST/ByteCode/ByteCodeEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ int32_t ByteCodeEmitter::getOffset(LabelTy Label) {
/// Helper to write bytecode and bail out if 32-bit offsets become invalid.
/// Pointers will be automatically marshalled as 32-bit IDs.
template <typename T>
static void emit(Program &P, std::vector<std::byte> &Code, const T &Val,
bool &Success) {
static void emit(Program &P, llvm::SmallVectorImpl<std::byte> &Code,
const T &Val, bool &Success) {
size_t ValPos = Code.size();
size_t Size;

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

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

Val.serialize(Code.data() + ValPos);
}

template <>
void emit(Program &P, std::vector<std::byte> &Code, const Floating &Val,
bool &Success) {
void emit(Program &P, llvm::SmallVectorImpl<std::byte> &Code,
const Floating &Val, bool &Success) {
emitSerialized(Code, Val, Success);
}

template <>
void emit(Program &P, std::vector<std::byte> &Code,
void emit(Program &P, llvm::SmallVectorImpl<std::byte> &Code,
const IntegralAP<false> &Val, bool &Success) {
emitSerialized(Code, Val, Success);
}

template <>
void emit(Program &P, std::vector<std::byte> &Code, const IntegralAP<true> &Val,
bool &Success) {
void emit(Program &P, llvm::SmallVectorImpl<std::byte> &Code,
const IntegralAP<true> &Val, bool &Success) {
emitSerialized(Code, Val, Success);
}

template <>
void emit(Program &P, std::vector<std::byte> &Code, const FixedPoint &Val,
bool &Success) {
void emit(Program &P, llvm::SmallVectorImpl<std::byte> &Code,
const FixedPoint &Val, bool &Success) {
emitSerialized(Code, Val, Success);
}

Expand Down
2 changes: 1 addition & 1 deletion clang/lib/AST/ByteCode/ByteCodeEmitter.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class ByteCodeEmitter {
/// Location of label relocations.
llvm::DenseMap<LabelTy, llvm::SmallVector<unsigned, 5>> LabelRelocs;
/// Program code.
std::vector<std::byte> Code;
llvm::SmallVector<std::byte> Code;
/// Opcode to expression mapping.
SourceMap SrcMap;

Expand Down
8 changes: 4 additions & 4 deletions clang/lib/AST/ByteCode/Context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ bool Context::isPotentialConstantExpr(State &Parent, const FunctionDecl *FD) {
Compiler<ByteCodeEmitter>(*this, *P).compileFunc(
FD, const_cast<Function *>(Func));

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

return Func->isValid();
++EvalID;
// And run it.
Copy link
Collaborator

@shafik shafik Aug 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder how useful this inline comments are, I feel like that might be better and added to the comment on the declaration of isPotentialConstantExpr something like

"Check if the function is a potential constant expression by compiling the function and running it."

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really care either way but this comment isn't new from this patch, just moved down a bit.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a drive by nit but since you in this area already it feels like a worthwhile fix.

return Run(Parent, Func);
}

void Context::isPotentialConstantExprUnevaluated(State &Parent, const Expr *E,
Expand Down
4 changes: 2 additions & 2 deletions clang/lib/AST/ByteCode/Function.h
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ class Function final {
bool HasRVO, bool IsLambdaStaticInvoker);

/// Sets the code of a function.
void setCode(unsigned NewFrameSize, std::vector<std::byte> &&NewCode,
void setCode(unsigned NewFrameSize, llvm::SmallVector<std::byte> &&NewCode,
SourceMap &&NewSrcMap, llvm::SmallVector<Scope, 2> &&NewScopes,
bool NewHasBody) {
FrameSize = NewFrameSize;
Expand Down Expand Up @@ -266,7 +266,7 @@ class Function final {
/// Size of the argument stack.
unsigned ArgSize;
/// Program code.
std::vector<std::byte> Code;
llvm::SmallVector<std::byte> Code;
/// Opcode-to-expression mapping.
SourceMap SrcMap;
/// List of block descriptors.
Expand Down