[clang] [clang][AST] Inline StmtVisitor fallback methods (PR #203125)

David Zbarsky via cfe-commits cfe-commits at lists.llvm.org
Mon Jul 6 07:16:16 PDT 2026


https://github.com/dzbarsky updated https://github.com/llvm/llvm-project/pull/203125

>From 21132d1b0dcb8b8742726183b534037e35116dff Mon Sep 17 00:00:00 2001
From: David Zbarsky <dzbarsky at gmail.com>
Date: Wed, 10 Jun 2026 19:48:13 -0400
Subject: [PATCH] [clang][AST] Inline StmtVisitor fallback methods

Mark the trivial StmtVisitorBase fallback methods always_inline so each VisitFoo-to-VisitParent delegation is folded into its caller instead of retained as an out-of-line template thunk.

In matched Release assertions-off Darwin arm64 builds, stripped clang decreases by 266,656 bytes, stripped clangd decreases by 232,912 bytes, and the stripped upstream llvm-driver multicall decreases by 266,336 bytes. Linked fixups are unchanged in all three binaries.

A 16-pair constexpr-heavy compilation benchmark measured -1.63% CPU time with a 95% confidence interval of [-2.23%, -1.02%]. Five focused AST-dump and constant-expression lit tests pass.

Co-authored-by: OpenAI Codex <noreply at openai.com>
---
 clang/include/clang/AST/StmtVisitor.h | 32 +++++++++++++++++----------
 1 file changed, 20 insertions(+), 12 deletions(-)

diff --git a/clang/include/clang/AST/StmtVisitor.h b/clang/include/clang/AST/StmtVisitor.h
index 8b7b728deaff2..45e203578c9fd 100644
--- a/clang/include/clang/AST/StmtVisitor.h
+++ b/clang/include/clang/AST/StmtVisitor.h
@@ -115,16 +115,19 @@ class StmtVisitorBase {
 
   // If the implementation chooses not to implement a certain visit method, fall
   // back on VisitExpr or whatever else is the superclass.
-#define STMT(CLASS, PARENT)                                   \
-  RetTy Visit ## CLASS(PTR(CLASS) S, ParamTys... P) { DISPATCH(PARENT, PARENT); }
+#define STMT(CLASS, PARENT)                                                    \
+  LLVM_ATTRIBUTE_ALWAYS_INLINE                                                 \
+  RetTy Visit##CLASS(PTR(CLASS) S, ParamTys... P) { DISPATCH(PARENT, PARENT); }
 #include "clang/AST/StmtNodes.inc"
 
   // If the implementation doesn't implement binary operator methods, fall back
   // on VisitBinaryOperator.
-#define BINOP_FALLBACK(NAME) \
-  RetTy VisitBin ## NAME(PTR(BinaryOperator) S, ParamTys... P) { \
-    DISPATCH(BinaryOperator, BinaryOperator); \
+#define BINOP_FALLBACK(NAME)                                                   \
+  LLVM_ATTRIBUTE_ALWAYS_INLINE                                                 \
+  RetTy VisitBin##NAME(PTR(BinaryOperator) S, ParamTys... P) {                 \
+    DISPATCH(BinaryOperator, BinaryOperator);                                  \
   }
+  // clang-format off
   BINOP_FALLBACK(PtrMemD)                    BINOP_FALLBACK(PtrMemI)
   BINOP_FALLBACK(Mul)   BINOP_FALLBACK(Div)  BINOP_FALLBACK(Rem)
   BINOP_FALLBACK(Add)   BINOP_FALLBACK(Sub)  BINOP_FALLBACK(Shl)
@@ -143,9 +146,10 @@ class StmtVisitorBase {
 
   // If the implementation doesn't implement compound assignment operator
   // methods, fall back on VisitCompoundAssignOperator.
-#define CAO_FALLBACK(NAME) \
-  RetTy VisitBin ## NAME(PTR(CompoundAssignOperator) S, ParamTys... P) { \
-    DISPATCH(CompoundAssignOperator, CompoundAssignOperator); \
+#define CAO_FALLBACK(NAME)                                                     \
+  LLVM_ATTRIBUTE_ALWAYS_INLINE                                                 \
+  RetTy VisitBin##NAME(PTR(CompoundAssignOperator) S, ParamTys... P) {         \
+    DISPATCH(CompoundAssignOperator, CompoundAssignOperator);                  \
   }
   CAO_FALLBACK(MulAssign) CAO_FALLBACK(DivAssign) CAO_FALLBACK(RemAssign)
   CAO_FALLBACK(AddAssign) CAO_FALLBACK(SubAssign) CAO_FALLBACK(ShlAssign)
@@ -155,9 +159,10 @@ class StmtVisitorBase {
 
   // If the implementation doesn't implement unary operator methods, fall back
   // on VisitUnaryOperator.
-#define UNARYOP_FALLBACK(NAME) \
-  RetTy VisitUnary ## NAME(PTR(UnaryOperator) S, ParamTys... P) { \
-    DISPATCH(UnaryOperator, UnaryOperator);    \
+#define UNARYOP_FALLBACK(NAME)                                                 \
+  LLVM_ATTRIBUTE_ALWAYS_INLINE                                                 \
+  RetTy VisitUnary##NAME(PTR(UnaryOperator) S, ParamTys... P) {                \
+    DISPATCH(UnaryOperator, UnaryOperator);                                    \
   }
   UNARYOP_FALLBACK(PostInc)   UNARYOP_FALLBACK(PostDec)
   UNARYOP_FALLBACK(PreInc)    UNARYOP_FALLBACK(PreDec)
@@ -170,7 +175,10 @@ class StmtVisitorBase {
 #undef UNARYOP_FALLBACK
 
   // Base case, ignore it. :)
-  RetTy VisitStmt(PTR(Stmt) Node, ParamTys... P) { return RetTy(); }
+  LLVM_ATTRIBUTE_ALWAYS_INLINE RetTy VisitStmt(PTR(Stmt) Node, ParamTys... P) {
+    return RetTy();
+  }
+  // clang-format on
 
 #undef PTR
 #undef DISPATCH



More information about the cfe-commits mailing list