[llvm] [IR][Attribute] Add support for intersecting AttributeLists; NFC (PR #109719)
via llvm-commits
llvm-commits at lists.llvm.org
Tue Sep 24 15:57:45 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();
----------------
goldsteinn wrote:
> Is there a reason BothValid is only computed after handling intersectWithAnd?
intersectWithAnd doesn't explicitly need to deref pImpl so it wasn't strictly necessary. I've moved it before.
>
> Separately, it looks like getAttribute(Kind) will return a not isValid() Attribute if not hasAttribute(Kind), so could the layer of std::optional<> be avoided and just check isValid() (and just call getAttribute without checking hasAttribute first)?
After dropping the merged list iteration, the `getAttribute` calls have been dropped so this is a non-issue now.
https://github.com/llvm/llvm-project/pull/109719
More information about the llvm-commits
mailing list