[llvm] [IR][Attribute] Add support for intersecting AttributeLists; NFC (PR #109719)
via llvm-commits
llvm-commits at lists.llvm.org
Tue Oct 1 07:03:05 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.
+
+ auto IntersectEq = [&]() {
+ if (Attr0 != Attr1)
+ return false;
+ Intersected.addAttribute(Attr0);
+ return true;
+ };
+
+ // Non-enum assume we must preserve
+ if (!Attr0.hasKindAsEnum()) {
+ if (!IntersectEq())
+ return std::nullopt;
+ continue;
+ }
+
+ Attribute::AttrKind Kind = Attr0.getKindAsEnum();
+ assert(Attr1.hasKindAsEnum() && Kind == Attr1.getKindAsEnum() &&
+ "Iterator picked up two different attributes in the same iteration");
+
+ // Attribute we can intersect with "and"
----------------
goldsteinn wrote:
Don't think its worthwhile. I am going to move up the string attr handling so we only need to handle that in one place though.
https://github.com/llvm/llvm-project/pull/109719
More information about the llvm-commits
mailing list