Skip to content

Commit

Permalink
[Attributes] Return Optional from getAllocSizeArgs() (NFC)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nikic committed Oct 11, 2022
1 parent 2569767 commit ac47db6
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 24 deletions.
10 changes: 4 additions & 6 deletions llvm/include/llvm/IR/Attributes.h
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,7 @@ class Attribute {
/// 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<unsigned, Optional<unsigned>> getAllocSizeArgs() const;

/// Returns the minimum value for the vscale_range attribute.
Expand Down Expand Up @@ -371,7 +370,7 @@ class AttributeSet {
Type *getPreallocatedType() const;
Type *getInAllocaType() const;
Type *getElementType() const;
std::pair<unsigned, Optional<unsigned>> getAllocSizeArgs() const;
Optional<std::pair<unsigned, Optional<unsigned>>> getAllocSizeArgs() const;
unsigned getVScaleRangeMin() const;
Optional<unsigned> getVScaleRangeMax() const;
UWTableKind getUWTableKind() const;
Expand Down Expand Up @@ -1142,9 +1141,8 @@ class AttrBuilder {
/// 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<unsigned, Optional<unsigned>> getAllocSizeArgs() const;
/// Retrieve the allocsize args, or None if the attribute does not exist.
Optional<std::pair<unsigned, Optional<unsigned>>> getAllocSizeArgs() const;

/// Add integer attribute with raw value (packed/encoded if necessary).
AttrBuilder &addRawIntAttr(Attribute::AttrKind Kind, uint64_t Value);
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/IR/AttributeImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ class AttributeSetNode final
MaybeAlign getStackAlignment() const;
uint64_t getDereferenceableBytes() const;
uint64_t getDereferenceableOrNullBytes() const;
std::pair<unsigned, Optional<unsigned>> getAllocSizeArgs() const;
Optional<std::pair<unsigned, Optional<unsigned>>> getAllocSizeArgs() const;
unsigned getVScaleRangeMin() const;
Optional<unsigned> getVScaleRangeMax() const;
UWTableKind getUWTableKind() const;
Expand Down
20 changes: 13 additions & 7 deletions llvm/lib/IR/Attributes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -745,9 +745,11 @@ Type *AttributeSet::getElementType() const {
return SetNode ? SetNode->getAttributeType(Attribute::ElementType) : nullptr;
}

std::pair<unsigned, Optional<unsigned>> AttributeSet::getAllocSizeArgs() const {
return SetNode ? SetNode->getAllocSizeArgs()
: std::pair<unsigned, Optional<unsigned>>(0, 0);
Optional<std::pair<unsigned, Optional<unsigned>>>
AttributeSet::getAllocSizeArgs() const {
if (SetNode)
return SetNode->getAllocSizeArgs();
return None;
}

unsigned AttributeSet::getVScaleRangeMin() const {
Expand Down Expand Up @@ -913,11 +915,11 @@ uint64_t AttributeSetNode::getDereferenceableOrNullBytes() const {
return 0;
}

std::pair<unsigned, Optional<unsigned>>
Optional<std::pair<unsigned, Optional<unsigned>>>
AttributeSetNode::getAllocSizeArgs() const {
if (auto A = findEnumAttribute(Attribute::AllocSize))
return A->getAllocSizeArgs();
return std::make_pair(0, 0);
return None;
}

unsigned AttributeSetNode::getVScaleRangeMin() const {
Expand Down Expand Up @@ -1653,8 +1655,12 @@ AttrBuilder &AttrBuilder::addRawIntAttr(Attribute::AttrKind Kind,
return addAttribute(Attribute::get(Ctx, Kind, Value));
}

std::pair<unsigned, Optional<unsigned>> AttrBuilder::getAllocSizeArgs() const {
return unpackAllocSizeArgs(getRawIntAttr(Attribute::AllocSize).value_or(0));
Optional<std::pair<unsigned, Optional<unsigned>>>
AttrBuilder::getAllocSizeArgs() const {
Attribute A = getAttribute(Attribute::AllocSize);
if (A.isValid())
return A.getAllocSizeArgs();
return None;
}

AttrBuilder &AttrBuilder::addAlignmentAttr(MaybeAlign Align) {
Expand Down
9 changes: 3 additions & 6 deletions llvm/lib/IR/Verifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2083,10 +2083,7 @@ void Verifier::verifyFunctionAttrs(FunctionType *FT, AttributeList Attrs,
"Attribute 'jumptable' requires 'unnamed_addr'", V);
}

if (Attrs.hasFnAttr(Attribute::AllocSize)) {
std::pair<unsigned, Optional<unsigned>> 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);
Expand All @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<unsigned> NEltArg;
std::tie(SizeArg, NEltArg) = FnAttrs.getAllocSizeArgs();
auto [SizeArg, NEltArg] = *Args;
SizeArg += 1;
if (NEltArg)
NEltArg = NEltArg.value() + 1;
Expand Down

0 comments on commit ac47db6

Please sign in to comment.