[clang] [clang][NFC] Remove padding from some AST nodes (PR #246310)

via cfe-commits cfe-commits at lists.llvm.org
Thu Nov 26 03:05:23 PST 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Gábor Horváth (Xazax-hun)

<details>
<summary>Changes</summary>

Reorder or pack fields to shrink TemplateTypeParmDecl (80 -> 72 bytes), FunctionTemplateSpecializationInfo (48 -> 40) and EnumConstantDecl (88 -> 80) on 64-bit Itanium ABI targets. TemplateTypeParmDecl now stores the number of expanded parameters in a 29-bit bitfield next to its flags, the same limit as TemplateParameterList::NumParams. On some LLVM TUs this saves 0.6-0.7% of the Decl memory reported by -print-stats.

Assisted-by: Claude Opus 5.5

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


3 Files Affected:

- (modified) clang/include/clang/AST/Decl.h (+2-1) 
- (modified) clang/include/clang/AST/DeclTemplate.h (+30-16) 
- (modified) clang/lib/Serialization/ASTReaderDecl.cpp (+1-1) 


``````````diff
diff --git a/clang/include/clang/AST/Decl.h b/clang/include/clang/AST/Decl.h
index 4104ddc89c50f9..cf7d3b12edc4a4 100644
--- a/clang/include/clang/AST/Decl.h
+++ b/clang/include/clang/AST/Decl.h
@@ -3602,8 +3602,9 @@ class FieldDecl : public DeclaratorDecl, public Mergeable<FieldDecl> {
 class EnumConstantDecl : public ValueDecl,
                          public Mergeable<EnumConstantDecl>,
                          public APIntStorage {
-  Stmt *Init; // an integer constant expression
+  // Declared first so that it can use the tail padding of APIntStorage.
   bool IsUnsigned;
+  Stmt *Init; // an integer constant expression
 
 protected:
   EnumConstantDecl(const ASTContext &C, DeclContext *DC, SourceLocation L,
diff --git a/clang/include/clang/AST/DeclTemplate.h b/clang/include/clang/AST/DeclTemplate.h
index 1a5ed48874dddb..aae6afe164c086 100644
--- a/clang/include/clang/AST/DeclTemplate.h
+++ b/clang/include/clang/AST/DeclTemplate.h
@@ -469,6 +469,12 @@ class FunctionTemplateSpecializationInfo final
     : public llvm::FoldingSetNode,
       private llvm::TrailingObjects<FunctionTemplateSpecializationInfo,
                                     MemberSpecializationInfo *> {
+public:
+  /// The point at which this function template specialization was
+  /// first instantiated.
+  SourceLocation PointOfInstantiation;
+
+private:
   /// The function template specialization that this structure describes and a
   /// flag indicating if the function is a member specialization.
   llvm::PointerIntPair<FunctionDecl *, 1, bool> Function;
@@ -488,20 +494,15 @@ class FunctionTemplateSpecializationInfo final
   /// FIXME: Normally null; tail-allocate this.
   const ASTTemplateArgumentListInfo *TemplateArgumentsAsWritten;
 
-  /// The point at which this function template specialization was
-  /// first instantiated.
-  SourceLocation PointOfInstantiation;
-
 private:
   FunctionTemplateSpecializationInfo(
       FunctionDecl *FD, FunctionTemplateDecl *Template,
       TemplateSpecializationKind TSK, TemplateArgumentList *TemplateArgs,
       const ASTTemplateArgumentListInfo *TemplateArgsAsWritten,
       SourceLocation POI, MemberSpecializationInfo *MSInfo)
-      : Function(FD, MSInfo ? true : false), Template(Template, TSK - 1),
-        TemplateArguments(TemplateArgs),
-        TemplateArgumentsAsWritten(TemplateArgsAsWritten),
-        PointOfInstantiation(POI) {
+      : PointOfInstantiation(POI), Function(FD, MSInfo ? true : false),
+        Template(Template, TSK - 1), TemplateArguments(TemplateArgs),
+        TemplateArgumentsAsWritten(TemplateArgsAsWritten) {
     if (MSInfo)
       getTrailingObjects()[0] = MSInfo;
   }
@@ -1176,18 +1177,22 @@ class TemplateTypeParmDecl final : public TypeDecl,
   /// the 'typename' keyword.
   ///
   /// If false, it was declared with the 'class' keyword.
-  bool Typename : 1;
+  LLVM_PREFERRED_TYPE(bool)
+  unsigned Typename : 1;
 
   /// Whether this template type parameter has a type-constraint construct.
-  bool HasTypeConstraint : 1;
+  LLVM_PREFERRED_TYPE(bool)
+  unsigned HasTypeConstraint : 1;
 
   /// Whether the type constraint has been initialized. This can be false if the
   /// constraint was not initialized yet or if there was an error forming the
   /// type constraint.
-  bool TypeConstraintInitialized : 1;
+  LLVM_PREFERRED_TYPE(bool)
+  unsigned TypeConstraintInitialized : 1;
 
-  /// The number of type parameters in an expanded parameter pack, if any.
-  UnsignedOrNone NumExpanded = std::nullopt;
+  /// The number of type parameters in an expanded parameter pack, if any,
+  /// stored as the internal representation of an UnsignedOrNone.
+  unsigned NumExpanded : 29;
 
   /// The default template argument, if any.
   using DefArgStorage =
@@ -1198,8 +1203,15 @@ class TemplateTypeParmDecl final : public TypeDecl,
                        SourceLocation IdLoc, IdentifierInfo *Id, bool Typename,
                        bool HasTypeConstraint, UnsignedOrNone NumExpanded)
       : TypeDecl(TemplateTypeParm, DC, IdLoc, Id, KeyLoc), Typename(Typename),
-        HasTypeConstraint(HasTypeConstraint), TypeConstraintInitialized(false),
-        NumExpanded(NumExpanded) {}
+        HasTypeConstraint(HasTypeConstraint), TypeConstraintInitialized(false) {
+    setNumExpansionParameters(NumExpanded);
+  }
+
+  void setNumExpansionParameters(UnsignedOrNone N) {
+    NumExpanded = N.toInternalRepresentation();
+    assert(NumExpanded == N.toInternalRepresentation() &&
+           "too many expansion parameters");
+  }
 
 public:
   static TemplateTypeParmDecl *
@@ -1309,7 +1321,9 @@ class TemplateTypeParmDecl final : public TypeDecl,
   /// \c X<int, unsigned int> results in \c Convertibles being an expanded
   /// parameter pack of size 2 (use getNumExpansionTypes() to get this number).
   /// Retrieves the number of parameters in an expanded parameter pack, if any.
-  UnsignedOrNone getNumExpansionParameters() const { return NumExpanded; }
+  UnsignedOrNone getNumExpansionParameters() const {
+    return UnsignedOrNone::fromInternalRepresentation(NumExpanded);
+  }
 
   /// Returns the type constraint associated with this template parameter (if
   /// any).
diff --git a/clang/lib/Serialization/ASTReaderDecl.cpp b/clang/lib/Serialization/ASTReaderDecl.cpp
index 9412fe77abc2a9..056db5620bef5c 100644
--- a/clang/lib/Serialization/ASTReaderDecl.cpp
+++ b/clang/lib/Serialization/ASTReaderDecl.cpp
@@ -2778,7 +2778,7 @@ void ASTDeclReader::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
     UnsignedOrNone ArgPackSubstIndex = Record.readUnsignedOrNone();
 
     D->setTypeConstraint(CR, ImmediatelyDeclaredConstraint, ArgPackSubstIndex);
-    D->NumExpanded = Record.readUnsignedOrNone();
+    D->setNumExpansionParameters(Record.readUnsignedOrNone());
   }
 
   if (Record.readInt())

``````````

</details>


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


More information about the cfe-commits mailing list