Skip to content
This repository was archived by the owner on Apr 23, 2020. It is now read-only.

Commit 672893c

Browse files
committed
[Alignment][NFC] Attributes use Align/MaybeAlign
Summary: This is patch is part of a series to introduce an Alignment type. See this thread for context: http://lists.llvm.org/pipermail/llvm-dev/2019-July/133851.html See this patch for the introduction of the type: https://reviews.llvm.org/D64790 Reviewers: courbet Subscribers: jholewinski, hiraditya, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D69278 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@375495 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 55b4ec9 commit 672893c

File tree

7 files changed

+72
-50
lines changed

7 files changed

+72
-50
lines changed

include/llvm/IR/Attributes.h

+25-9
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,11 @@ class Attribute {
151151

152152
/// Returns the alignment field of an attribute as a byte alignment
153153
/// value.
154-
unsigned getAlignment() const;
154+
MaybeAlign getAlignment() const;
155155

156156
/// Returns the stack alignment field of an attribute as a byte
157157
/// alignment value.
158-
unsigned getStackAlignment() const;
158+
MaybeAlign getStackAlignment() const;
159159

160160
/// Returns the number of dereferenceable bytes from the
161161
/// dereferenceable attribute.
@@ -285,8 +285,8 @@ class AttributeSet {
285285
/// Return the target-dependent attribute object.
286286
Attribute getAttribute(StringRef Kind) const;
287287

288-
unsigned getAlignment() const;
289-
unsigned getStackAlignment() const;
288+
MaybeAlign getAlignment() const;
289+
MaybeAlign getStackAlignment() const;
290290
uint64_t getDereferenceableBytes() const;
291291
uint64_t getDereferenceableOrNullBytes() const;
292292
Type *getByValType() const;
@@ -604,16 +604,16 @@ class AttributeList {
604604
}
605605

606606
/// Return the alignment of the return value.
607-
unsigned getRetAlignment() const;
607+
MaybeAlign getRetAlignment() const;
608608

609609
/// Return the alignment for the specified function parameter.
610-
unsigned getParamAlignment(unsigned ArgNo) const;
610+
MaybeAlign getParamAlignment(unsigned ArgNo) const;
611611

612612
/// Return the byval type for the specified function parameter.
613613
Type *getParamByValType(unsigned ArgNo) const;
614614

615615
/// Get the stack alignment.
616-
unsigned getStackAlignment(unsigned Index) const;
616+
MaybeAlign getStackAlignment(unsigned Index) const;
617617

618618
/// Get the number of dereferenceable bytes (or zero if unknown).
619619
uint64_t getDereferenceableBytes(unsigned Index) const;
@@ -796,13 +796,29 @@ class AttrBuilder {
796796
/// doesn't exist, pair(0, 0) is returned.
797797
std::pair<unsigned, Optional<unsigned>> getAllocSizeArgs() const;
798798

799+
/// This turns an alignment into the form used internally in Attribute.
800+
/// This call has no effect if Align is not set.
801+
AttrBuilder &addAlignmentAttr(MaybeAlign Align);
802+
799803
/// This turns an int alignment (which must be a power of 2) into the
800804
/// form used internally in Attribute.
801-
AttrBuilder &addAlignmentAttr(unsigned Align);
805+
/// This call has no effect if Align is 0.
806+
/// Deprecated, use the version using a MaybeAlign.
807+
inline AttrBuilder &addAlignmentAttr(unsigned Align) {
808+
return addAlignmentAttr(MaybeAlign(Align));
809+
}
810+
811+
/// This turns a stack alignment into the form used internally in Attribute.
812+
/// This call has no effect if Align is not set.
813+
AttrBuilder &addStackAlignmentAttr(MaybeAlign Align);
802814

803815
/// This turns an int stack alignment (which must be a power of 2) into
804816
/// the form used internally in Attribute.
805-
AttrBuilder &addStackAlignmentAttr(unsigned Align);
817+
/// This call has no effect if Align is 0.
818+
/// Deprecated, use the version using a MaybeAlign.
819+
inline AttrBuilder &addStackAlignmentAttr(unsigned Align) {
820+
return addStackAlignmentAttr(MaybeAlign(Align));
821+
}
806822

807823
/// This turns the number of dereferenceable bytes into the form used
808824
/// internally in Attribute.

include/llvm/IR/Function.h

+7-2
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,10 @@ class Function : public GlobalObject, public ilist_node<Function> {
343343
unsigned getFnStackAlignment() const {
344344
if (!hasFnAttribute(Attribute::StackAlignment))
345345
return 0;
346-
return AttributeSets.getStackAlignment(AttributeList::FunctionIndex);
346+
if (const auto MA =
347+
AttributeSets.getStackAlignment(AttributeList::FunctionIndex))
348+
return MA->value();
349+
return 0;
347350
}
348351

349352
/// hasGC/getGC/setGC/clearGC - The name of the garbage collection algorithm
@@ -433,7 +436,9 @@ class Function : public GlobalObject, public ilist_node<Function> {
433436

434437
/// Extract the alignment for a call or parameter (0=unknown).
435438
unsigned getParamAlignment(unsigned ArgNo) const {
436-
return AttributeSets.getParamAlignment(ArgNo);
439+
if (const auto MA = AttributeSets.getParamAlignment(ArgNo))
440+
return MA->value();
441+
return 0;
437442
}
438443

439444
/// Extract the byval type for a parameter.

include/llvm/IR/InstrTypes.h

+8-2
Original file line numberDiff line numberDiff line change
@@ -1567,11 +1567,17 @@ class CallBase : public Instruction {
15671567
}
15681568

15691569
/// Extract the alignment of the return value.
1570-
unsigned getRetAlignment() const { return Attrs.getRetAlignment(); }
1570+
unsigned getRetAlignment() const {
1571+
if (const auto MA = Attrs.getRetAlignment())
1572+
return MA->value();
1573+
return 0;
1574+
}
15711575

15721576
/// Extract the alignment for a call or parameter (0=unknown).
15731577
unsigned getParamAlignment(unsigned ArgNo) const {
1574-
return Attrs.getParamAlignment(ArgNo);
1578+
if (const auto MA = Attrs.getParamAlignment(ArgNo))
1579+
return MA->value();
1580+
return 0;
15751581
}
15761582

15771583
/// Extract the byval type for a call or parameter.

lib/IR/AttributeImpl.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,8 @@ class AttributeSetNode final
208208
Attribute getAttribute(Attribute::AttrKind Kind) const;
209209
Attribute getAttribute(StringRef Kind) const;
210210

211-
unsigned getAlignment() const;
212-
unsigned getStackAlignment() const;
211+
MaybeAlign getAlignment() const;
212+
MaybeAlign getStackAlignment() const;
213213
uint64_t getDereferenceableBytes() const;
214214
uint64_t getDereferenceableOrNullBytes() const;
215215
std::pair<unsigned, Optional<unsigned>> getAllocSizeArgs() const;

lib/IR/Attributes.cpp

+18-21
Original file line numberDiff line numberDiff line change
@@ -241,16 +241,16 @@ bool Attribute::hasAttribute(StringRef Kind) const {
241241
return pImpl && pImpl->hasAttribute(Kind);
242242
}
243243

244-
unsigned Attribute::getAlignment() const {
244+
MaybeAlign Attribute::getAlignment() const {
245245
assert(hasAttribute(Attribute::Alignment) &&
246246
"Trying to get alignment from non-alignment attribute!");
247-
return pImpl->getValueAsInt();
247+
return MaybeAlign(pImpl->getValueAsInt());
248248
}
249249

250-
unsigned Attribute::getStackAlignment() const {
250+
MaybeAlign Attribute::getStackAlignment() const {
251251
assert(hasAttribute(Attribute::StackAlignment) &&
252252
"Trying to get alignment from non-alignment attribute!");
253-
return pImpl->getValueAsInt();
253+
return MaybeAlign(pImpl->getValueAsInt());
254254
}
255255

256256
uint64_t Attribute::getDereferenceableBytes() const {
@@ -667,12 +667,12 @@ Attribute AttributeSet::getAttribute(StringRef Kind) const {
667667
return SetNode ? SetNode->getAttribute(Kind) : Attribute();
668668
}
669669

670-
unsigned AttributeSet::getAlignment() const {
671-
return SetNode ? SetNode->getAlignment() : 0;
670+
MaybeAlign AttributeSet::getAlignment() const {
671+
return SetNode ? SetNode->getAlignment() : None;
672672
}
673673

674-
unsigned AttributeSet::getStackAlignment() const {
675-
return SetNode ? SetNode->getStackAlignment() : 0;
674+
MaybeAlign AttributeSet::getStackAlignment() const {
675+
return SetNode ? SetNode->getStackAlignment() : None;
676676
}
677677

678678
uint64_t AttributeSet::getDereferenceableBytes() const {
@@ -833,18 +833,18 @@ Attribute AttributeSetNode::getAttribute(StringRef Kind) const {
833833
return {};
834834
}
835835

836-
unsigned AttributeSetNode::getAlignment() const {
836+
MaybeAlign AttributeSetNode::getAlignment() const {
837837
for (const auto I : *this)
838838
if (I.hasAttribute(Attribute::Alignment))
839839
return I.getAlignment();
840-
return 0;
840+
return None;
841841
}
842842

843-
unsigned AttributeSetNode::getStackAlignment() const {
843+
MaybeAlign AttributeSetNode::getStackAlignment() const {
844844
for (const auto I : *this)
845845
if (I.hasAttribute(Attribute::StackAlignment))
846846
return I.getStackAlignment();
847-
return 0;
847+
return None;
848848
}
849849

850850
Type *AttributeSetNode::getByValType() const {
@@ -1161,7 +1161,7 @@ AttributeList AttributeList::addAttributes(LLVMContext &C, unsigned Index,
11611161
#ifndef NDEBUG
11621162
// FIXME it is not obvious how this should work for alignment. For now, say
11631163
// we can't change a known alignment.
1164-
unsigned OldAlign = getAttributes(Index).getAlignment();
1164+
const MaybeAlign OldAlign = getAttributes(Index).getAlignment();
11651165
unsigned NewAlign = B.getAlignment();
11661166
assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
11671167
"Attempt to change alignment!");
@@ -1346,20 +1346,19 @@ Attribute AttributeList::getAttribute(unsigned Index, StringRef Kind) const {
13461346
return getAttributes(Index).getAttribute(Kind);
13471347
}
13481348

1349-
unsigned AttributeList::getRetAlignment() const {
1349+
MaybeAlign AttributeList::getRetAlignment() const {
13501350
return getAttributes(ReturnIndex).getAlignment();
13511351
}
13521352

1353-
unsigned AttributeList::getParamAlignment(unsigned ArgNo) const {
1353+
MaybeAlign AttributeList::getParamAlignment(unsigned ArgNo) const {
13541354
return getAttributes(ArgNo + FirstArgIndex).getAlignment();
13551355
}
13561356

13571357
Type *AttributeList::getParamByValType(unsigned Index) const {
13581358
return getAttributes(Index+FirstArgIndex).getByValType();
13591359
}
13601360

1361-
1362-
unsigned AttributeList::getStackAlignment(unsigned Index) const {
1361+
MaybeAlign AttributeList::getStackAlignment(unsigned Index) const {
13631362
return getAttributes(Index).getStackAlignment();
13641363
}
13651364

@@ -1516,8 +1515,7 @@ std::pair<unsigned, Optional<unsigned>> AttrBuilder::getAllocSizeArgs() const {
15161515
return unpackAllocSizeArgs(AllocSizeArgs);
15171516
}
15181517

1519-
AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned A) {
1520-
MaybeAlign Align(A);
1518+
AttrBuilder &AttrBuilder::addAlignmentAttr(MaybeAlign Align) {
15211519
if (!Align)
15221520
return *this;
15231521

@@ -1528,8 +1526,7 @@ AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned A) {
15281526
return *this;
15291527
}
15301528

1531-
AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned A) {
1532-
MaybeAlign Align(A);
1529+
AttrBuilder &AttrBuilder::addStackAlignmentAttr(MaybeAlign Align) {
15331530
// Default alignment, allow the target to define how to align it.
15341531
if (!Align)
15351532
return *this;

lib/Target/NVPTX/NVPTXAsmPrinter.cpp

+8-10
Original file line numberDiff line numberDiff line change
@@ -1473,12 +1473,11 @@ void NVPTXAsmPrinter::emitFunctionParamList(const Function *F, raw_ostream &O) {
14731473
// Just print .param .align <a> .b8 .param[size];
14741474
// <a> = PAL.getparamalignment
14751475
// size = typeallocsize of element type
1476-
unsigned align = PAL.getParamAlignment(paramIndex);
1477-
if (align == 0)
1478-
align = DL.getABITypeAlignment(Ty);
1476+
const Align align = DL.getValueOrABITypeAlignment(
1477+
PAL.getParamAlignment(paramIndex), Ty);
14791478

14801479
unsigned sz = DL.getTypeAllocSize(Ty);
1481-
O << "\t.param .align " << align << " .b8 ";
1480+
O << "\t.param .align " << align.value() << " .b8 ";
14821481
printParamName(I, paramIndex, O);
14831482
O << "[" << sz << "]";
14841483

@@ -1559,9 +1558,8 @@ void NVPTXAsmPrinter::emitFunctionParamList(const Function *F, raw_ostream &O) {
15591558
// Just print .param .align <a> .b8 .param[size];
15601559
// <a> = PAL.getparamalignment
15611560
// size = typeallocsize of element type
1562-
unsigned align = PAL.getParamAlignment(paramIndex);
1563-
if (align == 0)
1564-
align = DL.getABITypeAlignment(ETy);
1561+
Align align =
1562+
DL.getValueOrABITypeAlignment(PAL.getParamAlignment(paramIndex), ETy);
15651563
// Work around a bug in ptxas. When PTX code takes address of
15661564
// byval parameter with alignment < 4, ptxas generates code to
15671565
// spill argument into memory. Alas on sm_50+ ptxas generates
@@ -1573,10 +1571,10 @@ void NVPTXAsmPrinter::emitFunctionParamList(const Function *F, raw_ostream &O) {
15731571
// TODO: this will need to be undone when we get to support multi-TU
15741572
// device-side compilation as it breaks ABI compatibility with nvcc.
15751573
// Hopefully ptxas bug is fixed by then.
1576-
if (!isKernelFunc && align < 4)
1577-
align = 4;
1574+
if (!isKernelFunc && align < Align(4))
1575+
align = Align(4);
15781576
unsigned sz = DL.getTypeAllocSize(ETy);
1579-
O << "\t.param .align " << align << " .b8 ";
1577+
O << "\t.param .align " << align.value() << " .b8 ";
15801578
printParamName(I, paramIndex, O);
15811579
O << "[" << sz << "]";
15821580
continue;

unittests/IR/AttributesTest.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -144,15 +144,15 @@ TEST(Attributes, AddMatchingAlignAttr) {
144144
Attribute::getWithAlignment(C, Align(8)));
145145
AL = AL.addAttribute(C, AttributeList::FirstArgIndex + 1,
146146
Attribute::getWithAlignment(C, Align(32)));
147-
EXPECT_EQ(8U, AL.getParamAlignment(0));
148-
EXPECT_EQ(32U, AL.getParamAlignment(1));
147+
EXPECT_EQ(Align(8), AL.getParamAlignment(0));
148+
EXPECT_EQ(Align(32), AL.getParamAlignment(1));
149149

150150
AttrBuilder B;
151151
B.addAttribute(Attribute::NonNull);
152152
B.addAlignmentAttr(8);
153153
AL = AL.addAttributes(C, AttributeList::FirstArgIndex, B);
154-
EXPECT_EQ(8U, AL.getParamAlignment(0));
155-
EXPECT_EQ(32U, AL.getParamAlignment(1));
154+
EXPECT_EQ(Align(8), AL.getParamAlignment(0));
155+
EXPECT_EQ(Align(32), AL.getParamAlignment(1));
156156
EXPECT_TRUE(AL.hasParamAttribute(0, Attribute::NonNull));
157157
}
158158

0 commit comments

Comments
 (0)