[clang] [clang][bytecode][NFC] Simplify bytecode emission (PR #232215)
Timm Baeder via cfe-commits
cfe-commits at lists.llvm.org
Thu Oct 15 00:46:31 PDT 2026
https://github.com/tbaederr created https://github.com/llvm/llvm-project/pull/232215
Remove unused parameters and switch to returning the success boolean instead of writing to a reference parameter.
>From c80c05736f835c8ad523868e2eb2cbc68981f96f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Thu, 15 Oct 2026 09:44:01 +0200
Subject: [PATCH] [clang][bytecode][NFC] Simplify bytecode emission
Remove unused parameters and switch to returning the success boolean
instead of writing to a reference parameter.
---
clang/lib/AST/ByteCode/ByteCodeEmitter.cpp | 49 +++++++++-------------
1 file changed, 20 insertions(+), 29 deletions(-)
diff --git a/clang/lib/AST/ByteCode/ByteCodeEmitter.cpp b/clang/lib/AST/ByteCode/ByteCodeEmitter.cpp
index 2b2e6be3bdce25..e55bfb586fb7b2 100644
--- a/clang/lib/AST/ByteCode/ByteCodeEmitter.cpp
+++ b/clang/lib/AST/ByteCode/ByteCodeEmitter.cpp
@@ -130,8 +130,7 @@ int32_t ByteCodeEmitter::getOffset(LabelTy Label) {
/// Helper to write bytecode and bail out if 32-bit offsets become invalid.
template <typename T>
-static void emit(Program &P, llvm::SmallVectorImpl<std::byte> &Code,
- const T &Val, bool &Success) {
+static bool emit(llvm::SmallVectorImpl<std::byte> &Code, const T &Val) {
size_t ValPos = Code.size();
size_t Size;
@@ -140,10 +139,8 @@ static void emit(Program &P, llvm::SmallVectorImpl<std::byte> &Code,
else
Size = align(sizeof(T));
- if (ValPos + Size > std::numeric_limits<unsigned>::max()) {
- Success = false;
- return;
- }
+ if (ValPos + Size > std::numeric_limits<unsigned>::max())
+ return false;
// Access must be aligned!
assert(aligned(ValPos));
@@ -154,20 +151,19 @@ static void emit(Program &P, llvm::SmallVectorImpl<std::byte> &Code,
new (Code.data() + ValPos) uintptr_t(reinterpret_cast<uintptr_t>(Val));
else
new (Code.data() + ValPos) T(Val);
+ return true;
}
/// Emits a serializable value. These usually (potentially) contain
/// heap-allocated memory and aren't trivially copyable.
template <typename T>
-static void emitSerialized(llvm::SmallVectorImpl<std::byte> &Code, const T &Val,
- bool &Success) {
+static bool emitSerialized(llvm::SmallVectorImpl<std::byte> &Code,
+ const T &Val) {
size_t ValPos = Code.size();
size_t Size = align(Val.bytesToSerialize());
- if (ValPos + Size > std::numeric_limits<unsigned>::max()) {
- Success = false;
- return;
- }
+ if (ValPos + Size > std::numeric_limits<unsigned>::max())
+ return false;
// Access must be aligned!
assert(aligned(ValPos));
@@ -175,46 +171,41 @@ static void emitSerialized(llvm::SmallVectorImpl<std::byte> &Code, const T &Val,
Code.resize_for_overwrite(ValPos + Size);
Val.serialize(Code.data() + ValPos);
+ return true;
}
template <>
-void emit(Program &P, llvm::SmallVectorImpl<std::byte> &Code,
- const Floating &Val, bool &Success) {
- emitSerialized(Code, Val, Success);
+bool emit(llvm::SmallVectorImpl<std::byte> &Code, const Floating &Val) {
+ return emitSerialized(Code, Val);
}
template <>
-void emit(Program &P, llvm::SmallVectorImpl<std::byte> &Code,
- const IntegralAP<false> &Val, bool &Success) {
- emitSerialized(Code, Val, Success);
+bool emit(llvm::SmallVectorImpl<std::byte> &Code,
+ const IntegralAP<false> &Val) {
+ return emitSerialized(Code, Val);
}
template <>
-void emit(Program &P, llvm::SmallVectorImpl<std::byte> &Code,
- const IntegralAP<true> &Val, bool &Success) {
- emitSerialized(Code, Val, Success);
+bool emit(llvm::SmallVectorImpl<std::byte> &Code, const IntegralAP<true> &Val) {
+ return emitSerialized(Code, Val);
}
template <>
-void emit(Program &P, llvm::SmallVectorImpl<std::byte> &Code,
- const FixedPoint &Val, bool &Success) {
- emitSerialized(Code, Val, Success);
+bool emit(llvm::SmallVectorImpl<std::byte> &Code, const FixedPoint &Val) {
+ return emitSerialized(Code, Val);
}
template <typename... Tys>
bool ByteCodeEmitter::emitOp(Opcode Op, const Tys &...Args, SourceInfo SI) {
- bool Success = true;
-
// The opcode is followed by arguments. The source info is
// attached to the address after the opcode.
- emit(P, Code, Op, Success);
+ emit(Code, Op);
SI = LocOverride.value_or(SI);
if (SrcMap.empty() || SrcMap.back() != SI)
SrcMap.push(Code.size(), SI);
- (..., emit(P, Code, Args, Success));
- return Success;
+ return (emit(Code, Args) && ...);
}
bool ByteCodeEmitter::jumpTrue(const LabelTy &Label, SourceInfo SI) {
More information about the cfe-commits
mailing list