From a2ab8fc46c63e6d21b6648202907a33064fbfb6e Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Mon, 31 Oct 2022 10:34:51 +0100 Subject: [PATCH] [Attributes] Add additional MemoryEffects APIs (NFC) This adds the usual complement of APIs for creating and fetching a non-trivial attribute. Split out from D135780. --- llvm/include/llvm/IR/Attributes.h | 5 +++++ llvm/lib/IR/AttributeImpl.h | 1 + llvm/lib/IR/Attributes.cpp | 19 +++++++++++++++++++ 3 files changed, 25 insertions(+) diff --git a/llvm/include/llvm/IR/Attributes.h b/llvm/include/llvm/IR/Attributes.h index 71cd7fa..e2db9c6 100644 --- a/llvm/include/llvm/IR/Attributes.h +++ b/llvm/include/llvm/IR/Attributes.h @@ -146,6 +146,7 @@ public: static Attribute getWithPreallocatedType(LLVMContext &Context, Type *Ty); static Attribute getWithInAllocaType(LLVMContext &Context, Type *Ty); static Attribute getWithUWTableKind(LLVMContext &Context, UWTableKind Kind); + static Attribute getWithMemoryEffects(LLVMContext &Context, MemoryEffects ME); /// For a typed attribute, return the equivalent attribute with the type /// changed to \p ReplacementTy. @@ -379,6 +380,7 @@ public: Optional getVScaleRangeMax() const; UWTableKind getUWTableKind() const; AllocFnKind getAllocKind() const; + MemoryEffects getMemoryEffects() const; std::string getAsString(bool InAttrGrp = false) const; /// Return true if this attribute set belongs to the LLVMContext. @@ -878,6 +880,9 @@ public: AllocFnKind getAllocKind() const; + /// Returns memory effects of the function. + MemoryEffects getMemoryEffects() const; + /// Return the attributes at the index as a string. std::string getAsString(unsigned Index, bool InAttrGrp = false) const; diff --git a/llvm/lib/IR/AttributeImpl.h b/llvm/lib/IR/AttributeImpl.h index af01d6f..86ddb68 100644 --- a/llvm/lib/IR/AttributeImpl.h +++ b/llvm/lib/IR/AttributeImpl.h @@ -263,6 +263,7 @@ public: Optional getVScaleRangeMax() const; UWTableKind getUWTableKind() const; AllocFnKind getAllocKind() const; + MemoryEffects getMemoryEffects() const; std::string getAsString(bool InAttrGrp) const; Type *getAttributeType(Attribute::AttrKind Kind) const; diff --git a/llvm/lib/IR/Attributes.cpp b/llvm/lib/IR/Attributes.cpp index 333caf7..fc5d274 100644 --- a/llvm/lib/IR/Attributes.cpp +++ b/llvm/lib/IR/Attributes.cpp @@ -211,6 +211,11 @@ Attribute Attribute::getWithUWTableKind(LLVMContext &Context, return get(Context, UWTable, uint64_t(Kind)); } +Attribute Attribute::getWithMemoryEffects(LLVMContext &Context, + MemoryEffects ME) { + return get(Context, Memory, ME.toIntValue()); +} + Attribute Attribute::getWithAllocSizeArgs(LLVMContext &Context, unsigned ElemSizeArg, const Optional &NumElemsArg) { @@ -831,6 +836,10 @@ AllocFnKind AttributeSet::getAllocKind() const { return SetNode ? SetNode->getAllocKind() : AllocFnKind::Unknown; } +MemoryEffects AttributeSet::getMemoryEffects() const { + return SetNode ? SetNode->getMemoryEffects() : MemoryEffects::unknown(); +} + std::string AttributeSet::getAsString(bool InAttrGrp) const { return SetNode ? SetNode->getAsString(InAttrGrp) : ""; } @@ -1009,6 +1018,12 @@ AllocFnKind AttributeSetNode::getAllocKind() const { return AllocFnKind::Unknown; } +MemoryEffects AttributeSetNode::getMemoryEffects() const { + if (auto A = findEnumAttribute(Attribute::Memory)) + return A->getMemoryEffects(); + return MemoryEffects::unknown(); +} + std::string AttributeSetNode::getAsString(bool InAttrGrp) const { std::string Str; for (iterator I = begin(), E = end(); I != E; ++I) { @@ -1561,6 +1576,10 @@ AllocFnKind AttributeList::getAllocKind() const { return getFnAttrs().getAllocKind(); } +MemoryEffects AttributeList::getMemoryEffects() const { + return getFnAttrs().getMemoryEffects(); +} + std::string AttributeList::getAsString(unsigned Index, bool InAttrGrp) const { return getAttributes(Index).getAsString(InAttrGrp); } -- 2.7.4