[llvm] 66dcb86 - [Attributes] Remove trailing empty attribute sets (PR59746)
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Thu Jan 5 08:14:43 PST 2023
Author: Nikita Popov
Date: 2023-01-05T17:14:37+01:00
New Revision: 66dcb86b8bd9bdd1510c8eb797b4845cac0d3263
URL: https://github.com/llvm/llvm-project/commit/66dcb86b8bd9bdd1510c8eb797b4845cac0d3263
DIFF: https://github.com/llvm/llvm-project/commit/66dcb86b8bd9bdd1510c8eb797b4845cac0d3263.diff
LOG: [Attributes] Remove trailing empty attribute sets (PR59746)
In setAttributesAtIndex(), remove any trailing empty attribute sets.
Also make sure that all the different attribute removal APIs go
through that method.
Fixes https://github.com/llvm/llvm-project/issues/59746.
Added:
Modified:
llvm/lib/IR/Attributes.cpp
llvm/unittests/IR/AttributesTest.cpp
Removed:
################################################################################
diff --git a/llvm/lib/IR/Attributes.cpp b/llvm/lib/IR/Attributes.cpp
index 8b45d5eba833f..2c27c1aba5482 100644
--- a/llvm/lib/IR/Attributes.cpp
+++ b/llvm/lib/IR/Attributes.cpp
@@ -1333,6 +1333,12 @@ AttributeList AttributeList::setAttributesAtIndex(LLVMContext &C,
if (Index >= AttrSets.size())
AttrSets.resize(Index + 1);
AttrSets[Index] = Attrs;
+
+ // Remove trailing empty attribute sets.
+ while (!AttrSets.empty() && !AttrSets.back().hasAttributes())
+ AttrSets.pop_back();
+ if (AttrSets.empty())
+ return {};
return AttributeList::getImpl(C, AttrSets);
}
@@ -1375,14 +1381,8 @@ AttributeList::removeAttributeAtIndex(LLVMContext &C, unsigned Index,
Attribute::AttrKind Kind) const {
if (!hasAttributeAtIndex(Index, Kind))
return *this;
-
- Index = attrIdxToArrayIdx(Index);
- SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end());
- assert(Index < AttrSets.size());
-
- AttrSets[Index] = AttrSets[Index].removeAttribute(C, Kind);
-
- return getImpl(C, AttrSets);
+ return setAttributesAtIndex(C, Index,
+ getAttributes(Index).removeAttribute(C, Kind));
}
AttributeList AttributeList::removeAttributeAtIndex(LLVMContext &C,
@@ -1390,14 +1390,8 @@ AttributeList AttributeList::removeAttributeAtIndex(LLVMContext &C,
StringRef Kind) const {
if (!hasAttributeAtIndex(Index, Kind))
return *this;
-
- Index = attrIdxToArrayIdx(Index);
- SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end());
- assert(Index < AttrSets.size());
-
- AttrSets[Index] = AttrSets[Index].removeAttribute(C, Kind);
-
- return getImpl(C, AttrSets);
+ return setAttributesAtIndex(C, Index,
+ getAttributes(Index).removeAttribute(C, Kind));
}
AttributeList AttributeList::removeAttributesAtIndex(
@@ -1415,12 +1409,9 @@ AttributeList::removeAttributesAtIndex(LLVMContext &C,
unsigned WithoutIndex) const {
if (!pImpl)
return {};
- WithoutIndex = attrIdxToArrayIdx(WithoutIndex);
- if (WithoutIndex >= getNumAttrSets())
+ if (attrIdxToArrayIdx(WithoutIndex) >= getNumAttrSets())
return *this;
- SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end());
- AttrSets[WithoutIndex] = AttributeSet();
- return getImpl(C, AttrSets);
+ return setAttributesAtIndex(C, WithoutIndex, AttributeSet());
}
AttributeList AttributeList::addDereferenceableRetAttr(LLVMContext &C,
diff --git a/llvm/unittests/IR/AttributesTest.cpp b/llvm/unittests/IR/AttributesTest.cpp
index fcd7a22d2050f..5883ad71f9f6f 100644
--- a/llvm/unittests/IR/AttributesTest.cpp
+++ b/llvm/unittests/IR/AttributesTest.cpp
@@ -294,4 +294,17 @@ TEST(Attributes, MismatchedABIAttrs) {
}
}
+TEST(Attributes, RemoveParamAttributes) {
+ LLVMContext C;
+ AttributeList AL;
+ AL = AL.addParamAttribute(C, 1, Attribute::NoUndef);
+ EXPECT_EQ(AL.getNumAttrSets(), 4);
+ AL = AL.addParamAttribute(C, 3, Attribute::NonNull);
+ EXPECT_EQ(AL.getNumAttrSets(), 6);
+ AL = AL.removeParamAttributes(C, 3);
+ EXPECT_EQ(AL.getNumAttrSets(), 4);
+ AL = AL.removeParamAttribute(C, 1, Attribute::NoUndef);
+ EXPECT_EQ(AL.getNumAttrSets(), 0);
+}
+
} // end anonymous namespace
More information about the llvm-commits
mailing list