From: Benjamin Kramer Date: Fri, 1 May 2020 12:12:17 +0000 (+0200) Subject: [IR] Make Attributes and AttributeLists trivially destructible and BumpPtrAllocate... X-Git-Tag: llvmorg-12-init~7241 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=1c3fe86f0f38737302017446e7e340ed81da24bc;p=platform%2Fupstream%2Fllvm.git [IR] Make Attributes and AttributeLists trivially destructible and BumpPtrAllocate them --- diff --git a/llvm/lib/IR/AttributeImpl.h b/llvm/lib/IR/AttributeImpl.h index bd4e5b4..94cde85 100644 --- a/llvm/lib/IR/AttributeImpl.h +++ b/llvm/lib/IR/AttributeImpl.h @@ -54,8 +54,6 @@ public: AttributeImpl(const AttributeImpl &) = delete; AttributeImpl &operator=(const AttributeImpl &) = delete; - virtual ~AttributeImpl(); - bool isEnumAttribute() const { return KindID == EnumAttrEntry; } bool isIntAttribute() const { return KindID == IntAttrEntry; } bool isStringAttribute() const { return KindID == StringAttrEntry; } @@ -104,6 +102,9 @@ public: } }; +static_assert(std::is_trivially_destructible::value, + "AttributeImpl should be trivially destructible"); + //===----------------------------------------------------------------------===// /// \class /// A set of classes that contain the value of the @@ -112,8 +113,6 @@ public: /// attribute enties, which are for target-dependent attributes. class EnumAttributeImpl : public AttributeImpl { - virtual void anchor(); - Attribute::AttrKind Kind; protected: @@ -130,8 +129,6 @@ public: class IntAttributeImpl : public EnumAttributeImpl { uint64_t Val; - void anchor() override; - public: IntAttributeImpl(Attribute::AttrKind Kind, uint64_t Val) : EnumAttributeImpl(IntAttrEntry, Kind), Val(Val) { @@ -142,24 +139,43 @@ public: uint64_t getValue() const { return Val; } }; -class StringAttributeImpl : public AttributeImpl { - virtual void anchor(); +class StringAttributeImpl final + : public AttributeImpl, + private TrailingObjects { + friend TrailingObjects; - std::string Kind; - std::string Val; + unsigned KindSize; + unsigned ValSize; + size_t numTrailingObjects(OverloadToken) const { + return KindSize + 1 + ValSize + 1; + } public: StringAttributeImpl(StringRef Kind, StringRef Val = StringRef()) - : AttributeImpl(StringAttrEntry), Kind(std::string(Kind)), - Val(std::string(Val)) {} + : AttributeImpl(StringAttrEntry), KindSize(Kind.size()), + ValSize(Val.size()) { + char *TrailingString = getTrailingObjects(); + // Some users rely on zero-termination. + llvm::copy(Kind, TrailingString); + TrailingString[KindSize] = '\0'; + llvm::copy(Val, &TrailingString[KindSize + 1]); + TrailingString[KindSize + 1 + ValSize] = '\0'; + } - StringRef getStringKind() const { return Kind; } - StringRef getStringValue() const { return Val; } + StringRef getStringKind() const { + return StringRef(getTrailingObjects(), KindSize); + } + StringRef getStringValue() const { + return StringRef(getTrailingObjects() + KindSize + 1, ValSize); + } + + static size_t totalSizeToAlloc(StringRef Kind, StringRef Val) { + return TrailingObjects::totalSizeToAlloc(Kind.size() + 1 + + Val.size() + 1); + } }; class TypeAttributeImpl : public EnumAttributeImpl { - void anchor() override; - Type *Ty; public: @@ -265,8 +281,6 @@ public: AttributeListImpl(const AttributeListImpl &) = delete; AttributeListImpl &operator=(const AttributeListImpl &) = delete; - void operator delete(void *p) { ::operator delete(p); } - /// Get the context that created this AttributeListImpl. LLVMContext &getContext() { return Context; } @@ -287,6 +301,9 @@ public: void dump() const; }; +static_assert(std::is_trivially_destructible::value, + "AttributeListImpl should be trivially destructible"); + } // end namespace llvm #endif // LLVM_LIB_IR_ATTRIBUTEIMPL_H diff --git a/llvm/lib/IR/Attributes.cpp b/llvm/lib/IR/Attributes.cpp index c5cb6ba..4ae6904 100644 --- a/llvm/lib/IR/Attributes.cpp +++ b/llvm/lib/IR/Attributes.cpp @@ -92,9 +92,9 @@ Attribute Attribute::get(LLVMContext &Context, Attribute::AttrKind Kind, // If we didn't find any existing attributes of the same shape then create a // new one and insert it. if (!Val) - PA = new EnumAttributeImpl(Kind); + PA = new (pImpl->Alloc) EnumAttributeImpl(Kind); else - PA = new IntAttributeImpl(Kind, Val); + PA = new (pImpl->Alloc) IntAttributeImpl(Kind, Val); pImpl->AttrsSet.InsertNode(PA, InsertPoint); } @@ -114,7 +114,10 @@ Attribute Attribute::get(LLVMContext &Context, StringRef Kind, StringRef Val) { if (!PA) { // If we didn't find any existing attributes of the same shape then create a // new one and insert it. - PA = new StringAttributeImpl(Kind, Val); + void *Mem = + pImpl->Alloc.Allocate(StringAttributeImpl::totalSizeToAlloc(Kind, Val), + alignof(StringAttributeImpl)); + PA = new (Mem) StringAttributeImpl(Kind, Val); pImpl->AttrsSet.InsertNode(PA, InsertPoint); } @@ -135,7 +138,7 @@ Attribute Attribute::get(LLVMContext &Context, Attribute::AttrKind Kind, if (!PA) { // If we didn't find any existing attributes of the same shape then create a // new one and insert it. - PA = new TypeAttributeImpl(Kind, Ty); + PA = new (pImpl->Alloc) TypeAttributeImpl(Kind, Ty); pImpl->AttrsSet.InsertNode(PA, InsertPoint); } @@ -554,17 +557,6 @@ void Attribute::Profile(FoldingSetNodeID &ID) const { // AttributeImpl Definition //===----------------------------------------------------------------------===// -// Pin the vtables to this file. -AttributeImpl::~AttributeImpl() = default; - -void EnumAttributeImpl::anchor() {} - -void IntAttributeImpl::anchor() {} - -void StringAttributeImpl::anchor() {} - -void TypeAttributeImpl::anchor() {} - bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const { if (isStringAttribute()) return false; return getKindAsEnum() == A; @@ -1040,8 +1032,9 @@ AttributeList AttributeList::getImpl(LLVMContext &C, // create a new one and insert it. if (!PA) { // Coallocate entries after the AttributeListImpl itself. - void *Mem = ::operator new( - AttributeListImpl::totalSizeToAlloc(AttrSets.size())); + void *Mem = pImpl->Alloc.Allocate( + AttributeListImpl::totalSizeToAlloc(AttrSets.size()), + alignof(AttributeListImpl)); PA = new (Mem) AttributeListImpl(C, AttrSets); pImpl->AttrsLists.InsertNode(PA, InsertPoint); } diff --git a/llvm/lib/IR/LLVMContextImpl.cpp b/llvm/lib/IR/LLVMContextImpl.cpp index ca2ee58..68b8f8a 100644 --- a/llvm/lib/IR/LLVMContextImpl.cpp +++ b/llvm/lib/IR/LLVMContextImpl.cpp @@ -104,21 +104,6 @@ LLVMContextImpl::~LLVMContextImpl() { delete CDSConstant.second; CDSConstants.clear(); - // Destroy attributes. - for (FoldingSetIterator I = AttrsSet.begin(), - E = AttrsSet.end(); I != E; ) { - FoldingSetIterator Elem = I++; - delete &*Elem; - } - - // Destroy attribute lists. - for (FoldingSetIterator I = AttrsLists.begin(), - E = AttrsLists.end(); - I != E;) { - FoldingSetIterator Elem = I++; - delete &*Elem; - } - // Destroy attribute node lists. for (FoldingSetIterator I = AttrsSetNodes.begin(), E = AttrsSetNodes.end(); I != E; ) {