[llvm] [IR][Attribute] Add support for intersecting AttributeLists; NFC (PR #109719)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 24 09:08:16 PDT 2024


================
@@ -1614,6 +1719,31 @@ AttributeList AttributeList::addAllocSizeParamAttr(
   return addParamAttributes(C, Index, B);
 }
 
+std::optional<AttributeList>
+AttributeList::intersectAttributes(LLVMContext &C, AttributeList Other) const {
+  // Trivial case, the two lists are equal.
+  if (*this == Other)
+    return *this;
+
+  // At least for now, only intersect lists with same number of params.
+  if (getNumAttrSets() != Other.getNumAttrSets())
+    return std::nullopt;
+
+  AttributeList IntersectedAttrs{};
+  for (unsigned Idx : indexes()) {
+    auto IntersectedAB =
+        getAttributes(Idx).intersectWith(C, Other.getAttributes(Idx));
+    // If any index fails to intersect, fail.
+    if (!IntersectedAB)
+      return std::nullopt;
+
+    IntersectedAttrs =
+        IntersectedAttrs.setAttributesAtIndex(C, Idx, *IntersectedAB);
----------------
nikic wrote:

This is inefficient, it's better to collect them into a vector first and then convert it into AttributeList.

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


More information about the llvm-commits mailing list