From ac47db6acad29b6e077593430338be69d81cb6c0 Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Tue, 11 Oct 2022 11:01:29 +0200 Subject: [PATCH] [Attributes] Return Optional from getAllocSizeArgs() (NFC) As suggested on D135572, return Optional<> from getAllocSizeArgs() rather than the peculiar pair(0, 0) sentinel. The method on Attribute itself does not return Optional, because the attribute must exist in that case. --- llvm/include/llvm/IR/Attributes.h | 10 ++++------ llvm/lib/IR/AttributeImpl.h | 2 +- llvm/lib/IR/Attributes.cpp | 20 +++++++++++++------- llvm/lib/IR/Verifier.cpp | 9 +++------ .../WebAssembly/WebAssemblyLowerEmscriptenEHSjLj.cpp | 6 ++---- 5 files changed, 23 insertions(+), 24 deletions(-) diff --git a/llvm/include/llvm/IR/Attributes.h b/llvm/include/llvm/IR/Attributes.h index 779b9f9..5b8b843 100644 --- a/llvm/include/llvm/IR/Attributes.h +++ b/llvm/include/llvm/IR/Attributes.h @@ -227,8 +227,7 @@ public: /// dereferenceable_or_null attribute. uint64_t getDereferenceableOrNullBytes() const; - /// Returns the argument numbers for the allocsize attribute (or pair(0, 0) - /// if not known). + /// Returns the argument numbers for the allocsize attribute. std::pair> getAllocSizeArgs() const; /// Returns the minimum value for the vscale_range attribute. @@ -371,7 +370,7 @@ public: Type *getPreallocatedType() const; Type *getInAllocaType() const; Type *getElementType() const; - std::pair> getAllocSizeArgs() const; + Optional>> getAllocSizeArgs() const; unsigned getVScaleRangeMin() const; Optional getVScaleRangeMax() const; UWTableKind getUWTableKind() const; @@ -1142,9 +1141,8 @@ public: /// Retrieve the inalloca type. Type *getInAllocaType() const { return getTypeAttr(Attribute::InAlloca); } - /// Retrieve the allocsize args, if the allocsize attribute exists. If it - /// doesn't exist, pair(0, 0) is returned. - std::pair> getAllocSizeArgs() const; + /// Retrieve the allocsize args, or None if the attribute does not exist. + Optional>> getAllocSizeArgs() const; /// Add integer attribute with raw value (packed/encoded if necessary). AttrBuilder &addRawIntAttr(Attribute::AttrKind Kind, uint64_t Value); diff --git a/llvm/lib/IR/AttributeImpl.h b/llvm/lib/IR/AttributeImpl.h index f2e258f..af01d6f 100644 --- a/llvm/lib/IR/AttributeImpl.h +++ b/llvm/lib/IR/AttributeImpl.h @@ -258,7 +258,7 @@ public: MaybeAlign getStackAlignment() const; uint64_t getDereferenceableBytes() const; uint64_t getDereferenceableOrNullBytes() const; - std::pair> getAllocSizeArgs() const; + Optional>> getAllocSizeArgs() const; unsigned getVScaleRangeMin() const; Optional getVScaleRangeMax() const; UWTableKind getUWTableKind() const; diff --git a/llvm/lib/IR/Attributes.cpp b/llvm/lib/IR/Attributes.cpp index 256d81f..3ae15b6 100644 --- a/llvm/lib/IR/Attributes.cpp +++ b/llvm/lib/IR/Attributes.cpp @@ -745,9 +745,11 @@ Type *AttributeSet::getElementType() const { return SetNode ? SetNode->getAttributeType(Attribute::ElementType) : nullptr; } -std::pair> AttributeSet::getAllocSizeArgs() const { - return SetNode ? SetNode->getAllocSizeArgs() - : std::pair>(0, 0); +Optional>> +AttributeSet::getAllocSizeArgs() const { + if (SetNode) + return SetNode->getAllocSizeArgs(); + return None; } unsigned AttributeSet::getVScaleRangeMin() const { @@ -913,11 +915,11 @@ uint64_t AttributeSetNode::getDereferenceableOrNullBytes() const { return 0; } -std::pair> +Optional>> AttributeSetNode::getAllocSizeArgs() const { if (auto A = findEnumAttribute(Attribute::AllocSize)) return A->getAllocSizeArgs(); - return std::make_pair(0, 0); + return None; } unsigned AttributeSetNode::getVScaleRangeMin() const { @@ -1653,8 +1655,12 @@ AttrBuilder &AttrBuilder::addRawIntAttr(Attribute::AttrKind Kind, return addAttribute(Attribute::get(Ctx, Kind, Value)); } -std::pair> AttrBuilder::getAllocSizeArgs() const { - return unpackAllocSizeArgs(getRawIntAttr(Attribute::AllocSize).value_or(0)); +Optional>> +AttrBuilder::getAllocSizeArgs() const { + Attribute A = getAttribute(Attribute::AllocSize); + if (A.isValid()) + return A.getAllocSizeArgs(); + return None; } AttrBuilder &AttrBuilder::addAlignmentAttr(MaybeAlign Align) { diff --git a/llvm/lib/IR/Verifier.cpp b/llvm/lib/IR/Verifier.cpp index 272f61d..2a19d25 100644 --- a/llvm/lib/IR/Verifier.cpp +++ b/llvm/lib/IR/Verifier.cpp @@ -2083,10 +2083,7 @@ void Verifier::verifyFunctionAttrs(FunctionType *FT, AttributeList Attrs, "Attribute 'jumptable' requires 'unnamed_addr'", V); } - if (Attrs.hasFnAttr(Attribute::AllocSize)) { - std::pair> Args = - Attrs.getFnAttrs().getAllocSizeArgs(); - + if (auto Args = Attrs.getFnAttrs().getAllocSizeArgs()) { auto CheckParam = [&](StringRef Name, unsigned ParamNo) { if (ParamNo >= FT->getNumParams()) { CheckFailed("'allocsize' " + Name + " argument is out of bounds", V); @@ -2103,10 +2100,10 @@ void Verifier::verifyFunctionAttrs(FunctionType *FT, AttributeList Attrs, return true; }; - if (!CheckParam("element size", Args.first)) + if (!CheckParam("element size", Args->first)) return; - if (Args.second && !CheckParam("number of elements", *Args.second)) + if (Args->second && !CheckParam("number of elements", *Args->second)) return; } diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyLowerEmscriptenEHSjLj.cpp b/llvm/lib/Target/WebAssembly/WebAssemblyLowerEmscriptenEHSjLj.cpp index fa468df..63397a2 100644 --- a/llvm/lib/Target/WebAssembly/WebAssemblyLowerEmscriptenEHSjLj.cpp +++ b/llvm/lib/Target/WebAssembly/WebAssemblyLowerEmscriptenEHSjLj.cpp @@ -545,12 +545,10 @@ Value *WebAssemblyLowerEmscriptenEHSjLj::wrapInvoke(CallBase *CI) { ArgAttributes.push_back(InvokeAL.getParamAttrs(I)); AttrBuilder FnAttrs(CI->getContext(), InvokeAL.getFnAttrs()); - if (FnAttrs.contains(Attribute::AllocSize)) { + if (auto Args = FnAttrs.getAllocSizeArgs()) { // The allocsize attribute (if any) referes to parameters by index and needs // to be adjusted. - unsigned SizeArg; - Optional NEltArg; - std::tie(SizeArg, NEltArg) = FnAttrs.getAllocSizeArgs(); + auto [SizeArg, NEltArg] = *Args; SizeArg += 1; if (NEltArg) NEltArg = NEltArg.value() + 1; -- 2.7.4