[llvm-branch-commits] [llvm] IR: Make AttributeList::getAttributes inline (PR #241672)

Matt Arsenault via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Wed Nov 11 21:05:48 PST 2026


https://github.com/arsenm created https://github.com/llvm/llvm-project/pull/241672

Store the attribute sets and their count in AttributeListImplBase.

CTMark instructions:u: O3 -0.16%, ThinLTO -0.14%.

Co-authored-by: Claude Opus 5.5 <noreply at anthropic.com>

---

<sub>Stack created with <a href="https://github.com/github/gh-stack">GitHub Stacks CLI</a> • <a href="https://gh.io/stacks-feedback">Give Feedback 💬</a></sub>

>From f407b6552898d9a32963aa6c05e04a7647974f96 Mon Sep 17 00:00:00 2001
From: Matt Arsenault <Matthew.Arsenault at amd.com>
Date: Wed, 11 Nov 2026 18:21:36 -0800
Subject: [PATCH] IR: Make AttributeList::getAttributes inline

Store the attribute sets and their count in AttributeListImplBase.

CTMark instructions:u: O3 -0.16%, ThinLTO -0.14%.

Co-authored-by: Claude Opus 5.5 <noreply at anthropic.com>
---
 llvm/include/llvm/IR/Attributes.h | 33 ++++++++++++++++++++++-------
 llvm/include/llvm/IR/Function.h   |  4 +++-
 llvm/lib/IR/AttributeImpl.h       |  1 -
 llvm/lib/IR/Attributes.cpp        | 35 +++++--------------------------
 llvm/lib/IR/Function.cpp          |  4 ----
 5 files changed, 34 insertions(+), 43 deletions(-)

diff --git a/llvm/include/llvm/IR/Attributes.h b/llvm/include/llvm/IR/Attributes.h
index 422dc32d7096ac..b3f056b398fd98 100644
--- a/llvm/include/llvm/IR/Attributes.h
+++ b/llvm/include/llvm/IR/Attributes.h
@@ -40,6 +40,7 @@ class AttrBuilder;
 class AttributeMask;
 class AttributeImpl;
 class AttributeListImpl;
+class AttributeSet;
 class AttributeSetNode;
 class ConstantRange;
 class ConstantRangeList;
@@ -426,13 +427,19 @@ class AttributeSetNodeBase {
   }
 };
 
-/// The enum function attributes available in an AttributeListImpl, so that
-/// AttributeList can query them inline.
+/// The attribute sets and enum function attributes of an AttributeListImpl,
+/// so that AttributeList can query them inline.
 class AttributeListImplBase {
 protected:
+  /// The attribute sets, which are stored after the AttributeListImpl.
+  const AttributeSet *AttrSets = nullptr;
+  unsigned NumAttrSets = 0; ///< Number of entries in this set.
   AttributeBitSet AvailableFunctionAttrs;
 
 public:
+  const AttributeSet *getAttrSets() const { return AttrSets; }
+  unsigned getNumAttrSets() const { return NumAttrSets; }
+
   /// Return true if the function has an enum attribute of the given kind.
   bool hasFnAttribute(Attribute::AttrKind Kind) const {
     return AvailableFunctionAttrs.hasAttribute(Kind);
@@ -628,6 +635,9 @@ class AttributeList {
   /// Return the implementation of this list.
   AttributeListImpl *getListImpl() const;
 
+  /// Map an attribute index to the index of its set in the list.
+  static unsigned attrIdxToArrayIdx(unsigned Index) { return Index + 1; }
+
 public:
   /// Create an AttributeList with the specified parameters in it.
   LLVM_ABI static AttributeList
@@ -952,17 +962,24 @@ class AttributeList {
   //===--------------------------------------------------------------------===//
 
   /// The attributes for the specified index are returned.
-  LLVM_ABI AttributeSet getAttributes(unsigned Index) const;
+  AttributeSet getAttributes(unsigned Index) const {
+    Index = attrIdxToArrayIdx(Index);
+    if (!pImpl || Index >= pImpl->getNumAttrSets())
+      return {};
+    return pImpl->getAttrSets()[Index];
+  }
 
   /// The attributes for the argument or parameter at the given index are
   /// returned.
-  LLVM_ABI AttributeSet getParamAttrs(unsigned ArgNo) const;
+  AttributeSet getParamAttrs(unsigned ArgNo) const {
+    return getAttributes(ArgNo + FirstArgIndex);
+  }
 
   /// The attributes for the ret value are returned.
-  LLVM_ABI AttributeSet getRetAttrs() const;
+  AttributeSet getRetAttrs() const { return getAttributes(ReturnIndex); }
 
   /// The function attributes are returned.
-  LLVM_ABI AttributeSet getFnAttrs() const;
+  AttributeSet getFnAttrs() const { return getAttributes(FunctionIndex); }
 
   /// Return true if the attribute exists at the given index.
   LLVM_ABI bool hasAttributeAtIndex(unsigned Index,
@@ -1135,7 +1152,9 @@ class AttributeList {
   LLVM_ABI iterator begin() const;
   LLVM_ABI iterator end() const;
 
-  LLVM_ABI unsigned getNumAttrSets() const;
+  unsigned getNumAttrSets() const {
+    return pImpl ? pImpl->getNumAttrSets() : 0;
+  }
 
   // Implementation of indexes(). Produces iterators that wrap an index. Mostly
   // to hide the awkwardness of unsigned wrapping when iterating over valid
diff --git a/llvm/include/llvm/IR/Function.h b/llvm/include/llvm/IR/Function.h
index 17fa5efd01a9db..8f079c0f8fba97 100644
--- a/llvm/include/llvm/IR/Function.h
+++ b/llvm/include/llvm/IR/Function.h
@@ -398,7 +398,9 @@ class LLVM_ABI Function : public GlobalObject, public ilist_node<Function> {
   void removeParamAttrs(unsigned ArgNo, const AttributeMask &Attrs);
 
   /// Return true if the function has the attribute.
-  bool hasFnAttribute(Attribute::AttrKind Kind) const;
+  bool hasFnAttribute(Attribute::AttrKind Kind) const {
+    return AttributeSets.hasFnAttr(Kind);
+  }
 
   /// Return true if the function has the attribute.
   bool hasFnAttribute(StringRef Kind) const;
diff --git a/llvm/lib/IR/AttributeImpl.h b/llvm/lib/IR/AttributeImpl.h
index 128f9d83b1fd72..e683f7977164e2 100644
--- a/llvm/lib/IR/AttributeImpl.h
+++ b/llvm/lib/IR/AttributeImpl.h
@@ -328,7 +328,6 @@ class AttributeListImpl final
   friend TrailingObjects;
 
 private:
-  unsigned NumAttrSets; ///< Number of entries in this set.
   /// Union of enum attributes available at any index.
   AttributeBitSet AvailableSomewhereAttrs;
 
diff --git a/llvm/lib/IR/Attributes.cpp b/llvm/lib/IR/Attributes.cpp
index 172c88559e4f40..41dfaa6ea0d8da 100644
--- a/llvm/lib/IR/Attributes.cpp
+++ b/llvm/lib/IR/Attributes.cpp
@@ -1517,20 +1517,18 @@ std::string AttributeSetNode::getAsString(bool InAttrGrp) const {
 
 /// Map from AttributeList index to the internal array index. Adding one happens
 /// to work, because -1 wraps around to 0.
-static unsigned attrIdxToArrayIdx(unsigned Index) {
-  return Index + 1;
-}
-
-AttributeListImpl::AttributeListImpl(ArrayRef<AttributeSet> Sets)
-    : NumAttrSets(Sets.size()) {
+AttributeListImpl::AttributeListImpl(ArrayRef<AttributeSet> Sets) {
   assert(!Sets.empty() && "pointless AttributeListImpl");
 
   // There's memory after the node where we can store the entries in.
   llvm::copy(Sets, getTrailingObjects());
+  AttrSets = getTrailingObjects();
+  NumAttrSets = Sets.size();
 
   // Initialize AvailableFunctionAttrs and AvailableSomewhereAttrs
   // summary bitsets.
-  for (const auto &I : Sets[attrIdxToArrayIdx(AttributeList::FunctionIndex)])
+  for (const auto &I :
+       Sets[AttributeList::attrIdxToArrayIdx(AttributeList::FunctionIndex)])
     if (!I.isStringAttribute())
       AvailableFunctionAttrs.addAttribute(I.getKindAsEnum());
 
@@ -1960,18 +1958,6 @@ AttributeList::intersectWith(LLVMContext &C, AttributeList Other) const {
 // AttributeList Accessor Methods
 //===----------------------------------------------------------------------===//
 
-AttributeSet AttributeList::getParamAttrs(unsigned ArgNo) const {
-  return getAttributes(ArgNo + FirstArgIndex);
-}
-
-AttributeSet AttributeList::getRetAttrs() const {
-  return getAttributes(ReturnIndex);
-}
-
-AttributeSet AttributeList::getFnAttrs() const {
-  return getAttributes(FunctionIndex);
-}
-
 bool AttributeList::hasAttributeAtIndex(unsigned Index,
                                         Attribute::AttrKind Kind) const {
   return getAttributes(Index).hasAttribute(Kind);
@@ -2102,13 +2088,6 @@ std::string AttributeList::getAsString(unsigned Index, bool InAttrGrp) const {
   return getAttributes(Index).getAsString(InAttrGrp);
 }
 
-AttributeSet AttributeList::getAttributes(unsigned Index) const {
-  Index = attrIdxToArrayIdx(Index);
-  if (!pImpl || Index >= getNumAttrSets())
-    return {};
-  return getListImpl()->begin()[Index];
-}
-
 bool AttributeList::hasParentContext(LLVMContext &C) const {
   assert(!isEmpty() && "an empty attribute list has no parent context");
   FoldingSetInsertToken Token;
@@ -2127,10 +2106,6 @@ AttributeList::iterator AttributeList::end() const {
 // AttributeList Introspection Methods
 //===----------------------------------------------------------------------===//
 
-unsigned AttributeList::getNumAttrSets() const {
-  return pImpl ? getListImpl()->NumAttrSets : 0;
-}
-
 void AttributeList::print(raw_ostream &O) const {
   O << "AttributeList[\n";
 
diff --git a/llvm/lib/IR/Function.cpp b/llvm/lib/IR/Function.cpp
index c11e5f2ae12209..753f23c6649418 100644
--- a/llvm/lib/IR/Function.cpp
+++ b/llvm/lib/IR/Function.cpp
@@ -728,10 +728,6 @@ void Function::addDereferenceableParamAttr(unsigned ArgNo, uint64_t Bytes) {
       AttributeSets.addDereferenceableParamAttr(getContext(), ArgNo, Bytes);
 }
 
-bool Function::hasFnAttribute(Attribute::AttrKind Kind) const {
-  return AttributeSets.hasFnAttr(Kind);
-}
-
 bool Function::hasFnAttribute(StringRef Kind) const {
   return AttributeSets.hasFnAttr(Kind);
 }



More information about the llvm-branch-commits mailing list