[clang] [clang][bytecode] Avoid copying APValue into EvaluationResult (PR #155757)

via cfe-commits cfe-commits at lists.llvm.org
Wed Aug 27 23:04:46 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Timm Baeder (tbaederr)

<details>
<summary>Changes</summary>

Move the `APValue` into `EvaluationResult` instead.

For a large primitive array (`#embed` of the sqlite3 amalgamation), this improves compile times by around 25%.

---
Full diff: https://github.com/llvm/llvm-project/pull/155757.diff


2 Files Affected:

- (modified) clang/lib/AST/ByteCode/EvalEmitter.cpp (+6-6) 
- (modified) clang/lib/AST/ByteCode/EvaluationResult.h (+1-1) 


``````````diff
diff --git a/clang/lib/AST/ByteCode/EvalEmitter.cpp b/clang/lib/AST/ByteCode/EvalEmitter.cpp
index 1ebadae811bdf..2860a09ae6e2d 100644
--- a/clang/lib/AST/ByteCode/EvalEmitter.cpp
+++ b/clang/lib/AST/ByteCode/EvalEmitter.cpp
@@ -184,7 +184,7 @@ template <PrimType OpType> bool EvalEmitter::emitRet(const SourceInfo &Info) {
     return true;
 
   using T = typename PrimConv<OpType>::T;
-  EvalResult.setValue(S.Stk.pop<T>().toAPValue(Ctx.getASTContext()));
+  EvalResult.takeValue(S.Stk.pop<T>().toAPValue(Ctx.getASTContext()));
   return true;
 }
 
@@ -195,7 +195,7 @@ template <> bool EvalEmitter::emitRet<PT_Ptr>(const SourceInfo &Info) {
   const Pointer &Ptr = S.Stk.pop<Pointer>();
 
   if (Ptr.isFunctionPointer()) {
-    EvalResult.setValue(Ptr.toAPValue(Ctx.getASTContext()));
+    EvalResult.takeValue(Ptr.toAPValue(Ctx.getASTContext()));
     return true;
   }
 
@@ -224,7 +224,7 @@ template <> bool EvalEmitter::emitRet<PT_Ptr>(const SourceInfo &Info) {
 
     if (std::optional<APValue> V =
             Ptr.toRValue(Ctx, EvalResult.getSourceType())) {
-      EvalResult.setValue(*V);
+      EvalResult.takeValue(std::move(*V));
     } else {
       return false;
     }
@@ -233,14 +233,14 @@ template <> bool EvalEmitter::emitRet<PT_Ptr>(const SourceInfo &Info) {
     // the result, even if the pointer is dead.
     // This will later be diagnosed by CheckLValueConstantExpression.
     if (Ptr.isBlockPointer() && !Ptr.block()->isStatic()) {
-      EvalResult.setValue(Ptr.toAPValue(Ctx.getASTContext()));
+      EvalResult.takeValue(Ptr.toAPValue(Ctx.getASTContext()));
       return true;
     }
 
     if (!Ptr.isLive() && !Ptr.isTemporary())
       return false;
 
-    EvalResult.setValue(Ptr.toAPValue(Ctx.getASTContext()));
+    EvalResult.takeValue(Ptr.toAPValue(Ctx.getASTContext()));
   }
 
   return true;
@@ -261,7 +261,7 @@ bool EvalEmitter::emitRetValue(const SourceInfo &Info) {
 
   if (std::optional<APValue> APV =
           Ptr.toRValue(S.getASTContext(), EvalResult.getSourceType())) {
-    EvalResult.setValue(*APV);
+    EvalResult.takeValue(std::move(*APV));
     return true;
   }
 
diff --git a/clang/lib/AST/ByteCode/EvaluationResult.h b/clang/lib/AST/ByteCode/EvaluationResult.h
index 3b6c65eff1ef8..49a3e5c00eea3 100644
--- a/clang/lib/AST/ByteCode/EvaluationResult.h
+++ b/clang/lib/AST/ByteCode/EvaluationResult.h
@@ -55,7 +55,7 @@ class EvaluationResult final {
 
   void setSource(DeclTy D) { Source = D; }
 
-  void setValue(const APValue &V) {
+  void takeValue(APValue &&V) {
     // V could still be an LValue.
     assert(empty());
     Value = std::move(V);

``````````

</details>


https://github.com/llvm/llvm-project/pull/155757


More information about the cfe-commits mailing list