Author: Timm Baeder
Date: 2025-09-18T13:08:42+02:00
New Revision: f5ffedf81a1db4f0e149acb1b49cdc940e8c4233

URL: 
https://github.com/llvm/llvm-project/commit/f5ffedf81a1db4f0e149acb1b49cdc940e8c4233
DIFF: 
https://github.com/llvm/llvm-project/commit/f5ffedf81a1db4f0e149acb1b49cdc940e8c4233.diff

LOG: [clang][bytecode] Pass `SourceInfo` objects by value (#159532)

They are only pointer-sized and copying them is cheaper than taking the
const ref.

Added: 
    

Modified: 
    clang/lib/AST/ByteCode/ByteCodeEmitter.cpp
    clang/lib/AST/ByteCode/ByteCodeEmitter.h
    clang/lib/AST/ByteCode/EvalEmitter.cpp
    clang/lib/AST/ByteCode/Source.h
    clang/utils/TableGen/ClangOpcodesEmitter.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/ByteCode/ByteCodeEmitter.cpp 
b/clang/lib/AST/ByteCode/ByteCodeEmitter.cpp
index 274efccac79dc..ed743768d077e 100644
--- a/clang/lib/AST/ByteCode/ByteCodeEmitter.cpp
+++ b/clang/lib/AST/ByteCode/ByteCodeEmitter.cpp
@@ -207,8 +207,7 @@ void emit(Program &P, llvm::SmallVectorImpl<std::byte> 
&Code,
 }
 
 template <typename... Tys>
-bool ByteCodeEmitter::emitOp(Opcode Op, const Tys &...Args,
-                             const SourceInfo &SI) {
+bool ByteCodeEmitter::emitOp(Opcode Op, const Tys &...Args, SourceInfo SI) {
   bool Success = true;
 
   // The opcode is followed by arguments. The source info is

diff  --git a/clang/lib/AST/ByteCode/ByteCodeEmitter.h 
b/clang/lib/AST/ByteCode/ByteCodeEmitter.h
index c050b299d8f61..ca8dc38e65246 100644
--- a/clang/lib/AST/ByteCode/ByteCodeEmitter.h
+++ b/clang/lib/AST/ByteCode/ByteCodeEmitter.h
@@ -99,7 +99,7 @@ class ByteCodeEmitter {
 
   /// Emits an opcode.
   template <typename... Tys>
-  bool emitOp(Opcode Op, const Tys &...Args, const SourceInfo &L);
+  bool emitOp(Opcode Op, const Tys &...Args, SourceInfo L);
 
 protected:
 #define GET_LINK_PROTO

diff  --git a/clang/lib/AST/ByteCode/EvalEmitter.cpp 
b/clang/lib/AST/ByteCode/EvalEmitter.cpp
index c7287999dd9c0..007321791fdd4 100644
--- a/clang/lib/AST/ByteCode/EvalEmitter.cpp
+++ b/clang/lib/AST/ByteCode/EvalEmitter.cpp
@@ -177,7 +177,7 @@ bool EvalEmitter::speculate(const CallExpr *E, const 
LabelTy &EndLabel) {
   return this->emitBool(true, E);
 }
 
-template <PrimType OpType> bool EvalEmitter::emitRet(const SourceInfo &Info) {
+template <PrimType OpType> bool EvalEmitter::emitRet(SourceInfo Info) {
   if (!isActive())
     return true;
 
@@ -186,7 +186,7 @@ template <PrimType OpType> bool EvalEmitter::emitRet(const 
SourceInfo &Info) {
   return true;
 }
 
-template <> bool EvalEmitter::emitRet<PT_Ptr>(const SourceInfo &Info) {
+template <> bool EvalEmitter::emitRet<PT_Ptr>(SourceInfo Info) {
   if (!isActive())
     return true;
 
@@ -247,12 +247,12 @@ template <> bool EvalEmitter::emitRet<PT_Ptr>(const 
SourceInfo &Info) {
   return true;
 }
 
-bool EvalEmitter::emitRetVoid(const SourceInfo &Info) {
+bool EvalEmitter::emitRetVoid(SourceInfo Info) {
   EvalResult.setValid();
   return true;
 }
 
-bool EvalEmitter::emitRetValue(const SourceInfo &Info) {
+bool EvalEmitter::emitRetValue(SourceInfo Info) {
   const auto &Ptr = S.Stk.pop<Pointer>();
 
   if (!EvalResult.checkReturnValue(S, Ctx, Ptr, Info))
@@ -270,7 +270,7 @@ bool EvalEmitter::emitRetValue(const SourceInfo &Info) {
   return false;
 }
 
-bool EvalEmitter::emitGetPtrLocal(uint32_t I, const SourceInfo &Info) {
+bool EvalEmitter::emitGetPtrLocal(uint32_t I, SourceInfo Info) {
   if (!isActive())
     return true;
 
@@ -280,7 +280,7 @@ bool EvalEmitter::emitGetPtrLocal(uint32_t I, const 
SourceInfo &Info) {
 }
 
 template <PrimType OpType>
-bool EvalEmitter::emitGetLocal(uint32_t I, const SourceInfo &Info) {
+bool EvalEmitter::emitGetLocal(uint32_t I, SourceInfo Info) {
   if (!isActive())
     return true;
 
@@ -296,7 +296,7 @@ bool EvalEmitter::emitGetLocal(uint32_t I, const SourceInfo 
&Info) {
 }
 
 template <PrimType OpType>
-bool EvalEmitter::emitSetLocal(uint32_t I, const SourceInfo &Info) {
+bool EvalEmitter::emitSetLocal(uint32_t I, SourceInfo Info) {
   if (!isActive())
     return true;
 
@@ -310,7 +310,7 @@ bool EvalEmitter::emitSetLocal(uint32_t I, const SourceInfo 
&Info) {
   return true;
 }
 
-bool EvalEmitter::emitDestroy(uint32_t I, const SourceInfo &Info) {
+bool EvalEmitter::emitDestroy(uint32_t I, SourceInfo Info) {
   if (!isActive())
     return true;
 

diff  --git a/clang/lib/AST/ByteCode/Source.h b/clang/lib/AST/ByteCode/Source.h
index d5d294e31d90c..f355d14db5e30 100644
--- a/clang/lib/AST/ByteCode/Source.h
+++ b/clang/lib/AST/ByteCode/Source.h
@@ -92,6 +92,7 @@ class SourceInfo final {
 private:
   llvm::PointerUnion<const Decl *, const Stmt *> Source;
 };
+static_assert(sizeof(SourceInfo) == sizeof(void *));
 
 using SourceMap = std::vector<std::pair<unsigned, SourceInfo>>;
 

diff  --git a/clang/utils/TableGen/ClangOpcodesEmitter.cpp 
b/clang/utils/TableGen/ClangOpcodesEmitter.cpp
index 9d0773e1aff8f..d26122aca46bd 100644
--- a/clang/utils/TableGen/ClangOpcodesEmitter.cpp
+++ b/clang/utils/TableGen/ClangOpcodesEmitter.cpp
@@ -200,7 +200,7 @@ void ClangOpcodesEmitter::EmitEmitter(raw_ostream &OS, 
StringRef N,
       OS << (AsRef ? "const " : " ") << Name << " " << (AsRef ? "&" : "") << 
"A"
          << I << ", ";
     }
-    OS << "const SourceInfo &L) {\n";
+    OS << "SourceInfo L) {\n";
 
     // Emit a call to write the opcodes.
     OS << "  return emitOp<";
@@ -231,7 +231,7 @@ void ClangOpcodesEmitter::EmitProto(raw_ostream &OS, 
StringRef N,
       OS << (AsRef ? "const " : " ") << Name << " " << (AsRef ? "&" : "")
          << ", ";
     }
-    OS << "const SourceInfo &);\n";
+    OS << "SourceInfo);\n";
   });
 
   // Emit a template method for custom emitters to have less to implement.
@@ -248,7 +248,7 @@ void ClangOpcodesEmitter::EmitProto(raw_ostream &OS, 
StringRef N,
     OS << "bool emit" << N << "(";
     for (const auto *Arg : Args)
       OS << Arg->getValueAsString("Name") << ", ";
-    OS << "const SourceInfo &);\n";
+    OS << "SourceInfo);\n";
     OS << "#endif\n";
   }
 
@@ -272,7 +272,7 @@ void ClangOpcodesEmitter::EmitGroup(raw_ostream &OS, 
StringRef N,
     OS << "PrimType, ";
   for (auto *Arg : Args)
     OS << Arg->getValueAsString("Name") << ", ";
-  OS << "const SourceInfo &I);\n";
+  OS << "SourceInfo I);\n";
   OS << "#endif\n";
 
   // Emit the dispatch implementation in the source.
@@ -294,7 +294,7 @@ void ClangOpcodesEmitter::EmitGroup(raw_ostream &OS, 
StringRef N,
     OS << (AsRef ? "const " : " ") << Name << " " << (AsRef ? "&" : "") << "A"
        << I << ", ";
   }
-  OS << "const SourceInfo &I) {\n";
+  OS << "SourceInfo I) {\n";
 
   std::function<void(size_t, const Twine &)> Rec;
   SmallVector<const Record *, 2> TS;
@@ -368,7 +368,7 @@ void ClangOpcodesEmitter::EmitEval(raw_ostream &OS, 
StringRef N,
                 OS << (AsRef ? "const " : " ") << Name << " "
                    << (AsRef ? "&" : "") << "A" << I << ", ";
               }
-              OS << "const SourceInfo &L) {\n";
+              OS << "SourceInfo L) {\n";
               OS << "  if (!isActive()) return true;\n";
               OS << "  CurrentSource = L;\n";
 


        
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to