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

Andreas Jonson via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 24 08:23:46 PDT 2024


================
@@ -903,6 +916,98 @@ 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);
+  // Get common set of all attributes then handle each attr according to its
+  // intersect rule.
+  AttributeSet Merged = addAttributes(C, Other);
+  for (Attribute Attr : Merged) {
+    // Only supporting enum attrs for now.
+    if (!Attr.hasKindAsEnum())
+      return std::nullopt;
+
+    Attribute::AttrKind Kind = Attr.getKindAsEnum();
+    std::optional<Attribute> Attr0, Attr1;
+    if (hasAttribute(Kind))
+      Attr0 = getAttribute(Kind);
+    if (Other.hasAttribute(Kind))
+      Attr1 = Other.getAttribute(Kind);
+
+    auto IntersectEq = [&]() {
+      if (!Attr0 || !Attr1)
+        return false;
+      if (*Attr0 != *Attr1)
+        return false;
+      Intersected.addAttribute(Attr);
+      return true;
+    };
+
+    // Attribute we can intersect with "and"
+    if (Attribute::intersectWithAnd(Kind)) {
+      assert(Attribute::isEnumAttrKind(Kind) &&
+             "Invalid attr type of intersectAnd");
+      if (Attr0 && Attr1)
+        Intersected.addAttribute(Kind);
+      continue;
+    }
+    bool BothValid = Attr0 && Attr1 && Attr0->isValid() && Attr1->isValid();
+
+    // Attribute we can intersect with "min"
+    if (Attribute::intersectWithMin(Kind)) {
+      assert(Attribute::isIntAttrKind(Kind) &&
+             "Invalid attr type of intersectMin");
+      if (!BothValid)
+        continue;
+
+      uint64_t NewVal = std::min(getAttribute(Kind).getValueAsInt(),
+                                 Other.getAttribute(Kind).getValueAsInt());
+      Intersected.addRawIntAttr(Kind, NewVal);
+      continue;
+    }
+    // Attribute we can intersect but need a custom rule for.
+    if (Attribute::intersectWithCustom(Kind)) {
+      switch (Kind) {
+      case Attribute::Alignment:
----------------
andjo403 wrote:

or maybe bad to make assumptions about the type

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


More information about the llvm-commits mailing list