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

via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 26 13:39:27 PDT 2024


================
@@ -1614,6 +1773,33 @@ 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;
+
+  SmallVector<std::pair<unsigned, AttributeSet>> IntersectedAttrs;
+  //  AttributeList IntersectedAttrs{};
+  for (unsigned Idx : indexes()) {
+    auto IntersectedAS =
+        getAttributes(Idx).intersectWith(C, Other.getAttributes(Idx));
+    // If any index fails to intersect, fail.
+    if (!IntersectedAS)
+      return std::nullopt;
+    if (!IntersectedAS->hasAttributes())
+      continue;
+    IntersectedAttrs.push_back(std::make_pair(Idx, *IntersectedAS));
+  }
+
+  llvm::sort(IntersectedAttrs, llvm::less_first());
----------------
goldsteinn wrote:

I was hitting assert. I assume the fn index isn't handled in its numeric order.

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


More information about the llvm-commits mailing list