Skip to content

Conversation

mizvekov
Copy link
Contributor

@mizvekov mizvekov commented Aug 23, 2025

This is a small performance improvement:

This helps recover the performance lost in #155028, reversing it into a small positive instead.
image

@mizvekov mizvekov changed the title [clang] NFC: Provide inline definitions for {get,cast}TagDecl and fri… [clang] NFC: Provide inline definitions for {get,cast}TagDecl and friends Aug 23, 2025
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Aug 23, 2025
@llvmbot
Copy link
Member

llvmbot commented Aug 23, 2025

@llvm/pr-subscribers-clang

Author: Matheus Izvekov (mizvekov)

Changes

…ends

This is a small performance improvement:

This helps recover the performance lost in #155028, reversing it into a small positive instead.


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

3 Files Affected:

  • (modified) clang/include/clang/AST/Type.h (+49)
  • (modified) clang/include/clang/AST/TypeBase.h (+8-8)
  • (modified) clang/lib/AST/Type.cpp (-49)
diff --git a/clang/include/clang/AST/Type.h b/clang/include/clang/AST/Type.h
index 70510d95a67b5..ab8a3398bde94 100644
--- a/clang/include/clang/AST/Type.h
+++ b/clang/include/clang/AST/Type.h
@@ -23,6 +23,55 @@
 
 namespace clang {
 
+inline CXXRecordDecl *Type::getAsCXXRecordDecl() const {
+  const auto *TT = dyn_cast<TagType>(CanonicalType);
+  if (!isa_and_present<RecordType, InjectedClassNameType>(TT))
+    return nullptr;
+  auto *TD = TT->getOriginalDecl();
+  if (!isa<InjectedClassNameType>(TT) && !isa<CXXRecordDecl>(TD))
+    return nullptr;
+  return cast<CXXRecordDecl>(TD)->getDefinitionOrSelf();
+}
+
+inline CXXRecordDecl *Type::castAsCXXRecordDecl() const {
+  const auto *TT = cast<TagType>(CanonicalType);
+  return cast<CXXRecordDecl>(TT->getOriginalDecl())->getDefinitionOrSelf();
+}
+
+inline RecordDecl *Type::getAsRecordDecl() const {
+  const auto *TT = dyn_cast<TagType>(CanonicalType);
+  if (!isa_and_present<RecordType, InjectedClassNameType>(TT))
+    return nullptr;
+  return cast<RecordDecl>(TT->getOriginalDecl())->getDefinitionOrSelf();
+}
+
+inline RecordDecl *Type::castAsRecordDecl() const {
+  const auto *TT = cast<TagType>(CanonicalType);
+  return cast<RecordDecl>(TT->getOriginalDecl())->getDefinitionOrSelf();
+}
+
+inline EnumDecl *Type::getAsEnumDecl() const {
+  if (const auto *TT = dyn_cast<EnumType>(CanonicalType))
+    return TT->getOriginalDecl()->getDefinitionOrSelf();
+  return nullptr;
+}
+
+inline EnumDecl *Type::castAsEnumDecl() const {
+  return cast<EnumType>(CanonicalType)
+      ->getOriginalDecl()
+      ->getDefinitionOrSelf();
+}
+
+inline TagDecl *Type::getAsTagDecl() const {
+  if (const auto *TT = dyn_cast<TagType>(CanonicalType))
+    return TT->getOriginalDecl()->getDefinitionOrSelf();
+  return nullptr;
+}
+
+inline TagDecl *Type::castAsTagDecl() const {
+  return cast<TagType>(CanonicalType)->getOriginalDecl()->getDefinitionOrSelf();
+}
+
 inline bool QualType::hasNonTrivialToPrimitiveDefaultInitializeCUnion() const {
   if (auto *RD = getTypePtr()->getBaseElementTypeUnsafe()->getAsRecordDecl())
     return hasNonTrivialToPrimitiveDefaultInitializeCUnion(RD);
diff --git a/clang/include/clang/AST/TypeBase.h b/clang/include/clang/AST/TypeBase.h
index 35729b37b005f..75f1895cc79f1 100644
--- a/clang/include/clang/AST/TypeBase.h
+++ b/clang/include/clang/AST/TypeBase.h
@@ -2882,24 +2882,24 @@ class alignas(TypeAlignment) Type : public ExtQualsTypeCommonBase {
   /// Retrieves the CXXRecordDecl that this type refers to, either
   /// because the type is a RecordType or because it is the injected-class-name
   /// type of a class template or class template partial specialization.
-  CXXRecordDecl *getAsCXXRecordDecl() const;
-  CXXRecordDecl *castAsCXXRecordDecl() const;
+  inline CXXRecordDecl *getAsCXXRecordDecl() const;
+  inline CXXRecordDecl *castAsCXXRecordDecl() const;
 
   /// Retrieves the RecordDecl this type refers to.
-  RecordDecl *getAsRecordDecl() const;
-  RecordDecl *castAsRecordDecl() const;
+  inline RecordDecl *getAsRecordDecl() const;
+  inline RecordDecl *castAsRecordDecl() const;
 
   /// Retrieves the TagDecl that this type refers to, either
   /// because the type is a TagType or because it is the injected-class-name
   /// type of a class template or class template partial specialization.
-  EnumDecl *getAsEnumDecl() const;
-  EnumDecl *castAsEnumDecl() const;
+  inline EnumDecl *getAsEnumDecl() const;
+  inline EnumDecl *castAsEnumDecl() const;
 
   /// Retrieves the TagDecl that this type refers to, either
   /// because the type is a TagType or because it is the injected-class-name
   /// type of a class template or class template partial specialization.
-  TagDecl *getAsTagDecl() const;
-  TagDecl *castAsTagDecl() const;
+  inline TagDecl *getAsTagDecl() const;
+  inline TagDecl *castAsTagDecl() const;
 
   /// If this is a pointer or reference to a RecordType, return the
   /// CXXRecordDecl that the type refers to.
diff --git a/clang/lib/AST/Type.cpp b/clang/lib/AST/Type.cpp
index b34a0d556b596..9ff573f33d3cb 100644
--- a/clang/lib/AST/Type.cpp
+++ b/clang/lib/AST/Type.cpp
@@ -1917,55 +1917,6 @@ const CXXRecordDecl *Type::getPointeeCXXRecordDecl() const {
   return PointeeType->getAsCXXRecordDecl();
 }
 
-CXXRecordDecl *Type::getAsCXXRecordDecl() const {
-  const auto *TT = dyn_cast<TagType>(CanonicalType);
-  if (!isa_and_present<RecordType, InjectedClassNameType>(TT))
-    return nullptr;
-  auto *TD = TT->getOriginalDecl();
-  if (!isa<InjectedClassNameType>(TT) && !isa<CXXRecordDecl>(TD))
-    return nullptr;
-  return cast<CXXRecordDecl>(TD)->getDefinitionOrSelf();
-}
-
-CXXRecordDecl *Type::castAsCXXRecordDecl() const {
-  const auto *TT = cast<TagType>(CanonicalType);
-  return cast<CXXRecordDecl>(TT->getOriginalDecl())->getDefinitionOrSelf();
-}
-
-RecordDecl *Type::getAsRecordDecl() const {
-  const auto *TT = dyn_cast<TagType>(CanonicalType);
-  if (!isa_and_present<RecordType, InjectedClassNameType>(TT))
-    return nullptr;
-  return cast<RecordDecl>(TT->getOriginalDecl())->getDefinitionOrSelf();
-}
-
-RecordDecl *Type::castAsRecordDecl() const {
-  const auto *TT = cast<TagType>(CanonicalType);
-  return cast<RecordDecl>(TT->getOriginalDecl())->getDefinitionOrSelf();
-}
-
-EnumDecl *Type::getAsEnumDecl() const {
-  if (const auto *TT = dyn_cast<EnumType>(CanonicalType))
-    return TT->getOriginalDecl()->getDefinitionOrSelf();
-  return nullptr;
-}
-
-EnumDecl *Type::castAsEnumDecl() const {
-  return cast<EnumType>(CanonicalType)
-      ->getOriginalDecl()
-      ->getDefinitionOrSelf();
-}
-
-TagDecl *Type::getAsTagDecl() const {
-  if (const auto *TT = dyn_cast<TagType>(CanonicalType))
-    return TT->getOriginalDecl()->getDefinitionOrSelf();
-  return nullptr;
-}
-
-TagDecl *Type::castAsTagDecl() const {
-  return cast<TagType>(CanonicalType)->getOriginalDecl()->getDefinitionOrSelf();
-}
-
 const TemplateSpecializationType *
 Type::getAsNonAliasTemplateSpecializationType() const {
   const auto *TST = getAs<TemplateSpecializationType>();

@mizvekov mizvekov force-pushed the users/mizvekov/ast-type-provide-inline-defs branch from 1787600 to 324e8f3 Compare August 23, 2025 05:34
@mizvekov mizvekov force-pushed the users/mizvekov/rename-ast-readd-type branch from cb3309c to 76970a3 Compare August 23, 2025 05:34
@mizvekov mizvekov force-pushed the users/mizvekov/ast-type-provide-inline-defs branch from 324e8f3 to 8978115 Compare August 23, 2025 05:42
@mizvekov mizvekov force-pushed the users/mizvekov/rename-ast-readd-type branch from 76970a3 to ae2c34d Compare August 23, 2025 05:42
@mizvekov mizvekov force-pushed the users/mizvekov/rename-ast-readd-type branch from ae2c34d to 9393676 Compare August 23, 2025 17:05
@mizvekov mizvekov force-pushed the users/mizvekov/ast-type-provide-inline-defs branch 2 times, most recently from 55b388d to fa8712f Compare August 23, 2025 20:45
@mizvekov mizvekov force-pushed the users/mizvekov/rename-ast-readd-type branch from 9393676 to 367b46c Compare August 25, 2025 01:29
@mizvekov mizvekov force-pushed the users/mizvekov/ast-type-provide-inline-defs branch from fa8712f to 199662e Compare August 25, 2025 01:29
@mizvekov mizvekov self-assigned this Aug 25, 2025
@mizvekov mizvekov force-pushed the users/mizvekov/ast-type-provide-inline-defs branch from 199662e to f724916 Compare August 25, 2025 23:41
@mizvekov mizvekov force-pushed the users/mizvekov/rename-ast-readd-type branch from 367b46c to b4178e9 Compare August 25, 2025 23:41
@mizvekov mizvekov force-pushed the users/mizvekov/ast-type-provide-inline-defs branch from f724916 to 44cad6f Compare August 26, 2025 19:08
@mizvekov mizvekov force-pushed the users/mizvekov/rename-ast-readd-type branch from b4178e9 to 3ec61b4 Compare August 26, 2025 19:08
@mizvekov mizvekov force-pushed the users/mizvekov/ast-type-provide-inline-defs branch from 44cad6f to c3d411a Compare August 26, 2025 19:44
@mizvekov mizvekov force-pushed the users/mizvekov/rename-ast-readd-type branch from 3ec61b4 to a260168 Compare August 26, 2025 19:44
@mizvekov mizvekov force-pushed the users/mizvekov/ast-type-provide-inline-defs branch from c3d411a to d8a50b1 Compare August 27, 2025 10:09
@mizvekov mizvekov force-pushed the users/mizvekov/rename-ast-readd-type branch from a260168 to 5245315 Compare August 27, 2025 10:09
@mizvekov mizvekov force-pushed the users/mizvekov/ast-type-provide-inline-defs branch from d8a50b1 to c142961 Compare August 27, 2025 16:11
@mizvekov mizvekov force-pushed the users/mizvekov/rename-ast-readd-type branch from 5245315 to 829b1bf Compare August 27, 2025 16:11
Base automatically changed from users/mizvekov/rename-ast-readd-type to main August 27, 2025 16:11
…ends

This is a small performance improvement:

This helps recover the performance lost in #155028, reversing it
into a small positive instead.
@mizvekov mizvekov force-pushed the users/mizvekov/ast-type-provide-inline-defs branch from c142961 to c5d57c5 Compare August 27, 2025 16:12
@mizvekov mizvekov enabled auto-merge (squash) August 27, 2025 16:25
@mizvekov mizvekov merged commit aa46657 into main Aug 27, 2025
9 checks passed
@mizvekov mizvekov deleted the users/mizvekov/ast-type-provide-inline-defs branch August 27, 2025 17:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants