Skip to content

Commit ac47db6

Browse files
committed
[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.
1 parent 2569767 commit ac47db6

File tree

5 files changed

+23
-24
lines changed

5 files changed

+23
-24
lines changed

llvm/include/llvm/IR/Attributes.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -227,8 +227,7 @@ class Attribute {
227227
/// dereferenceable_or_null attribute.
228228
uint64_t getDereferenceableOrNullBytes() const;
229229

230-
/// Returns the argument numbers for the allocsize attribute (or pair(0, 0)
231-
/// if not known).
230+
/// Returns the argument numbers for the allocsize attribute.
232231
std::pair<unsigned, Optional<unsigned>> getAllocSizeArgs() const;
233232

234233
/// Returns the minimum value for the vscale_range attribute.
@@ -371,7 +370,7 @@ class AttributeSet {
371370
Type *getPreallocatedType() const;
372371
Type *getInAllocaType() const;
373372
Type *getElementType() const;
374-
std::pair<unsigned, Optional<unsigned>> getAllocSizeArgs() const;
373+
Optional<std::pair<unsigned, Optional<unsigned>>> getAllocSizeArgs() const;
375374
unsigned getVScaleRangeMin() const;
376375
Optional<unsigned> getVScaleRangeMax() const;
377376
UWTableKind getUWTableKind() const;
@@ -1142,9 +1141,8 @@ class AttrBuilder {
11421141
/// Retrieve the inalloca type.
11431142
Type *getInAllocaType() const { return getTypeAttr(Attribute::InAlloca); }
11441143

1145-
/// Retrieve the allocsize args, if the allocsize attribute exists. If it
1146-
/// doesn't exist, pair(0, 0) is returned.
1147-
std::pair<unsigned, Optional<unsigned>> getAllocSizeArgs() const;
1144+
/// Retrieve the allocsize args, or None if the attribute does not exist.
1145+
Optional<std::pair<unsigned, Optional<unsigned>>> getAllocSizeArgs() const;
11481146

11491147
/// Add integer attribute with raw value (packed/encoded if necessary).
11501148
AttrBuilder &addRawIntAttr(Attribute::AttrKind Kind, uint64_t Value);

llvm/lib/IR/AttributeImpl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ class AttributeSetNode final
258258
MaybeAlign getStackAlignment() const;
259259
uint64_t getDereferenceableBytes() const;
260260
uint64_t getDereferenceableOrNullBytes() const;
261-
std::pair<unsigned, Optional<unsigned>> getAllocSizeArgs() const;
261+
Optional<std::pair<unsigned, Optional<unsigned>>> getAllocSizeArgs() const;
262262
unsigned getVScaleRangeMin() const;
263263
Optional<unsigned> getVScaleRangeMax() const;
264264
UWTableKind getUWTableKind() const;

llvm/lib/IR/Attributes.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -745,9 +745,11 @@ Type *AttributeSet::getElementType() const {
745745
return SetNode ? SetNode->getAttributeType(Attribute::ElementType) : nullptr;
746746
}
747747

748-
std::pair<unsigned, Optional<unsigned>> AttributeSet::getAllocSizeArgs() const {
749-
return SetNode ? SetNode->getAllocSizeArgs()
750-
: std::pair<unsigned, Optional<unsigned>>(0, 0);
748+
Optional<std::pair<unsigned, Optional<unsigned>>>
749+
AttributeSet::getAllocSizeArgs() const {
750+
if (SetNode)
751+
return SetNode->getAllocSizeArgs();
752+
return None;
751753
}
752754

753755
unsigned AttributeSet::getVScaleRangeMin() const {
@@ -913,11 +915,11 @@ uint64_t AttributeSetNode::getDereferenceableOrNullBytes() const {
913915
return 0;
914916
}
915917

916-
std::pair<unsigned, Optional<unsigned>>
918+
Optional<std::pair<unsigned, Optional<unsigned>>>
917919
AttributeSetNode::getAllocSizeArgs() const {
918920
if (auto A = findEnumAttribute(Attribute::AllocSize))
919921
return A->getAllocSizeArgs();
920-
return std::make_pair(0, 0);
922+
return None;
921923
}
922924

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

1656-
std::pair<unsigned, Optional<unsigned>> AttrBuilder::getAllocSizeArgs() const {
1657-
return unpackAllocSizeArgs(getRawIntAttr(Attribute::AllocSize).value_or(0));
1658+
Optional<std::pair<unsigned, Optional<unsigned>>>
1659+
AttrBuilder::getAllocSizeArgs() const {
1660+
Attribute A = getAttribute(Attribute::AllocSize);
1661+
if (A.isValid())
1662+
return A.getAllocSizeArgs();
1663+
return None;
16581664
}
16591665

16601666
AttrBuilder &AttrBuilder::addAlignmentAttr(MaybeAlign Align) {

llvm/lib/IR/Verifier.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2083,10 +2083,7 @@ void Verifier::verifyFunctionAttrs(FunctionType *FT, AttributeList Attrs,
20832083
"Attribute 'jumptable' requires 'unnamed_addr'", V);
20842084
}
20852085

2086-
if (Attrs.hasFnAttr(Attribute::AllocSize)) {
2087-
std::pair<unsigned, Optional<unsigned>> Args =
2088-
Attrs.getFnAttrs().getAllocSizeArgs();
2089-
2086+
if (auto Args = Attrs.getFnAttrs().getAllocSizeArgs()) {
20902087
auto CheckParam = [&](StringRef Name, unsigned ParamNo) {
20912088
if (ParamNo >= FT->getNumParams()) {
20922089
CheckFailed("'allocsize' " + Name + " argument is out of bounds", V);
@@ -2103,10 +2100,10 @@ void Verifier::verifyFunctionAttrs(FunctionType *FT, AttributeList Attrs,
21032100
return true;
21042101
};
21052102

2106-
if (!CheckParam("element size", Args.first))
2103+
if (!CheckParam("element size", Args->first))
21072104
return;
21082105

2109-
if (Args.second && !CheckParam("number of elements", *Args.second))
2106+
if (Args->second && !CheckParam("number of elements", *Args->second))
21102107
return;
21112108
}
21122109

llvm/lib/Target/WebAssembly/WebAssemblyLowerEmscriptenEHSjLj.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -545,12 +545,10 @@ Value *WebAssemblyLowerEmscriptenEHSjLj::wrapInvoke(CallBase *CI) {
545545
ArgAttributes.push_back(InvokeAL.getParamAttrs(I));
546546

547547
AttrBuilder FnAttrs(CI->getContext(), InvokeAL.getFnAttrs());
548-
if (FnAttrs.contains(Attribute::AllocSize)) {
548+
if (auto Args = FnAttrs.getAllocSizeArgs()) {
549549
// The allocsize attribute (if any) referes to parameters by index and needs
550550
// to be adjusted.
551-
unsigned SizeArg;
552-
Optional<unsigned> NEltArg;
553-
std::tie(SizeArg, NEltArg) = FnAttrs.getAllocSizeArgs();
551+
auto [SizeArg, NEltArg] = *Args;
554552
SizeArg += 1;
555553
if (NEltArg)
556554
NEltArg = NEltArg.value() + 1;

0 commit comments

Comments
 (0)