From ff1b9add2ffd47abc649895e33b3e5c30d6f2079 Mon Sep 17 00:00:00 2001 From: Tyker Date: Sun, 2 Feb 2020 14:47:00 +0100 Subject: [PATCH] [NFC] Factor out function to detect if an attribute has an argument. Reviewers: jdoerfert Reviewed By: jdoerfert Subscribers: hiraditya, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D72884 --- llvm/include/llvm/IR/Attributes.h | 3 +++ llvm/lib/IR/AttributeImpl.h | 5 +---- llvm/lib/IR/Attributes.cpp | 11 +++++++++-- llvm/lib/IR/Verifier.cpp | 7 +++++++ llvm/lib/Transforms/Utils/KnowledgeRetention.cpp | 5 +++++ 5 files changed, 25 insertions(+), 6 deletions(-) diff --git a/llvm/include/llvm/IR/Attributes.h b/llvm/include/llvm/IR/Attributes.h index 3ef8a3b..3871db9 100644 --- a/llvm/include/llvm/IR/Attributes.h +++ b/llvm/include/llvm/IR/Attributes.h @@ -111,6 +111,9 @@ public: static StringRef getNameFromAttrKind(Attribute::AttrKind AttrKind); + /// Return true if and only if the attribute has an Argument. + static bool doesAttrKindHaveArgument(Attribute::AttrKind AttrKind); + //===--------------------------------------------------------------------===// // Attribute Accessors //===--------------------------------------------------------------------===// diff --git a/llvm/lib/IR/AttributeImpl.h b/llvm/lib/IR/AttributeImpl.h index da6e993..8741c0c 100644 --- a/llvm/lib/IR/AttributeImpl.h +++ b/llvm/lib/IR/AttributeImpl.h @@ -134,10 +134,7 @@ class IntAttributeImpl : public EnumAttributeImpl { public: IntAttributeImpl(Attribute::AttrKind Kind, uint64_t Val) : EnumAttributeImpl(IntAttrEntry, Kind), Val(Val) { - assert((Kind == Attribute::Alignment || Kind == Attribute::StackAlignment || - Kind == Attribute::Dereferenceable || - Kind == Attribute::DereferenceableOrNull || - Kind == Attribute::AllocSize) && + assert(Attribute::doesAttrKindHaveArgument(Kind) && "Wrong kind for int attribute!"); } diff --git a/llvm/lib/IR/Attributes.cpp b/llvm/lib/IR/Attributes.cpp index 78ee00d..5f0008e 100644 --- a/llvm/lib/IR/Attributes.cpp +++ b/llvm/lib/IR/Attributes.cpp @@ -199,6 +199,14 @@ StringRef Attribute::getNameFromAttrKind(Attribute::AttrKind AttrKind) { } } +bool Attribute::doesAttrKindHaveArgument(Attribute::AttrKind AttrKind) { + return AttrKind == Attribute::Alignment || + AttrKind == Attribute::StackAlignment || + AttrKind == Attribute::Dereferenceable || + AttrKind == Attribute::AllocSize || + AttrKind == Attribute::DereferenceableOrNull; +} + //===----------------------------------------------------------------------===// // Attribute Accessor Methods //===----------------------------------------------------------------------===// @@ -1472,8 +1480,7 @@ void AttrBuilder::clear() { AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) { assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!"); - assert(Val != Attribute::Alignment && Val != Attribute::StackAlignment && - Val != Attribute::Dereferenceable && Val != Attribute::AllocSize && + assert(!Attribute::doesAttrKindHaveArgument(Val) && "Adding integer attribute without adding a value!"); Attrs[Val] = true; return *this; diff --git a/llvm/lib/IR/Verifier.cpp b/llvm/lib/IR/Verifier.cpp index 6698a68..6062baa 100644 --- a/llvm/lib/IR/Verifier.cpp +++ b/llvm/lib/IR/Verifier.cpp @@ -1572,6 +1572,13 @@ void Verifier::verifyAttributeTypes(AttributeSet Attrs, bool IsFunction, if (A.isStringAttribute()) continue; + if (A.isIntAttribute() != + Attribute::doesAttrKindHaveArgument(A.getKindAsEnum())) { + CheckFailed("Attribute '" + A.getAsString() + "' should have an Argument", + V); + return; + } + if (isFuncOnlyAttr(A.getKindAsEnum())) { if (!IsFunction) { CheckFailed("Attribute '" + A.getAsString() + diff --git a/llvm/lib/Transforms/Utils/KnowledgeRetention.cpp b/llvm/lib/Transforms/Utils/KnowledgeRetention.cpp index eea947f..266d102 100644 --- a/llvm/lib/Transforms/Utils/KnowledgeRetention.cpp +++ b/llvm/lib/Transforms/Utils/KnowledgeRetention.cpp @@ -124,6 +124,11 @@ struct AssumeBuilderState { SmallVector OpBundle; for (const AssumedKnowledge &Elem : AssumedKnowledgeSet) { SmallVector Args; + assert(Attribute::getAttrKindFromName(Elem.Name) == + Attribute::AttrKind::None || + static_cast(Elem.Argument) == + Attribute::doesAttrKindHaveArgument( + Attribute::getAttrKindFromName(Elem.Name))); if (Elem.WasOn.getPointer()) Args.push_back(Elem.WasOn.getPointer()); if (Elem.Argument) -- 2.7.4