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

via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 1 06:55:09 PDT 2024


================
@@ -903,6 +956,132 @@ AttributeSet AttributeSet::removeAttributes(LLVMContext &C,
   return get(C, B);
 }
 
+std::optional<AttributeSet>
+AttributeSet::intersectWith(LLVMContext &C, AttributeSet Other) const {
+  if (*this == Other)
+    return *this;
+
+  AttrBuilder Intersected(C);
+  // Iterate over both attr sets at once.
+  auto ItBegin0 = begin();
+  auto ItEnd0 = end();
+  auto ItBegin1 = Other.begin();
+  auto ItEnd1 = Other.end();
+
+  while (ItBegin0 != ItEnd0 || ItBegin1 != ItEnd1) {
+    Attribute Attr0, Attr1;
+    if (ItBegin1 == ItEnd1)
+      Attr0 = *ItBegin0++;
+    else if (ItBegin0 == ItEnd0)
+      Attr1 = *ItBegin1++;
+    else {
+      int Cmp = ItBegin0->cmpKind(*ItBegin1);
+      if (Cmp == 0) {
+        Attr0 = *ItBegin0++;
+        Attr1 = *ItBegin1++;
+      } else if (Cmp < 0)
+        Attr0 = *ItBegin0++;
+      else
+        Attr1 = *ItBegin1++;
+    }
+    assert(Attr0.isValid() ||
+           Attr1.isValid() && "Iteration should never yield no valid attrs");
+
+    // If we don't have both attributes, then fail if the attribute is
+    // must-preserve or drop it otherwise.
+    if (!Attr0.isValid() || !Attr1.isValid()) {
+      Attribute Attr = Attr0.isValid() ? Attr0 : Attr1;
+      // Non-enum assume we must preserve.
+      if (!Attr.hasKindAsEnum())
+        return std::nullopt;
+      Attribute::AttrKind Kind = Attr.getKindAsEnum();
+      if (Attribute::intersectMustPreserve(Kind))
+        return std::nullopt;
+      continue;
+    }
+    // We have both attributes so apply the intersection rule.
----------------
goldsteinn wrote:

We could do that although I think it complicates the code. We still need all the unequal handling in `intersectMin`/`interesectCustom` so it doesn't really save any codes and the `ByVal` special case becomes more complicated.

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


More information about the llvm-commits mailing list