diff options
author | Nikita Popov <npopov@redhat.com> | 2023-01-05 17:12:31 +0100 |
---|---|---|
committer | Nikita Popov <npopov@redhat.com> | 2023-01-05 17:14:37 +0100 |
commit | 66dcb86b8bd9bdd1510c8eb797b4845cac0d3263 (patch) | |
tree | 67f4f351c15d9df8539fef1abfec82b457843286 /llvm/lib/IR/Attributes.cpp | |
parent | eebfee8f9ea7dfc9e7cf4326decadc779969994e (diff) | |
download | llvm-66dcb86b8bd9bdd1510c8eb797b4845cac0d3263.zip llvm-66dcb86b8bd9bdd1510c8eb797b4845cac0d3263.tar.gz llvm-66dcb86b8bd9bdd1510c8eb797b4845cac0d3263.tar.bz2 |
[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.
Diffstat (limited to 'llvm/lib/IR/Attributes.cpp')
-rw-r--r-- | llvm/lib/IR/Attributes.cpp | 33 |
1 files changed, 12 insertions, 21 deletions
diff --git a/llvm/lib/IR/Attributes.cpp b/llvm/lib/IR/Attributes.cpp index 8b45d5e..2c27c1a 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, |