[clang] [clang][bytecode][NFC] Simplify bytecode emission (PR #232215)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Oct 15 00:47:32 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Timm Baeder (tbaederr)
<details>
<summary>Changes</summary>
Remove unused parameters and switch to returning the success boolean instead of writing to a reference parameter.
---
Full diff: https://github.com/llvm/llvm-project/pull/232215.diff
1 Files Affected:
- (modified) clang/lib/AST/ByteCode/ByteCodeEmitter.cpp (+20-29)
``````````diff
diff --git a/clang/lib/AST/ByteCode/ByteCodeEmitter.cpp b/clang/lib/AST/ByteCode/ByteCodeEmitter.cpp
index 2b2e6be3bdce2..e55bfb586fb7b 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) {
``````````
</details>
https://github.com/llvm/llvm-project/pull/232215
More information about the cfe-commits
mailing list