Skip to content

Commit 77450cd

Browse files
committed
[Alignment][NFC] Use MaybeAlign in AttrBuilder
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: hiraditya, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D69300 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@375496 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 672893c commit 77450cd

File tree

4 files changed

+39
-32
lines changed

4 files changed

+39
-32
lines changed

include/llvm/IR/Attributes.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -774,12 +774,10 @@ class AttrBuilder {
774774
bool hasAlignmentAttr() const;
775775

776776
/// Retrieve the alignment attribute, if it exists.
777-
uint64_t getAlignment() const { return Alignment ? Alignment->value() : 0; }
777+
MaybeAlign getAlignment() const { return Alignment; }
778778

779779
/// Retrieve the stack alignment attribute, if it exists.
780-
uint64_t getStackAlignment() const {
781-
return StackAlignment ? StackAlignment->value() : 0;
782-
}
780+
MaybeAlign getStackAlignment() const { return StackAlignment; }
783781

784782
/// Retrieve the number of dereferenceable bytes, if the
785783
/// dereferenceable attribute exists (zero is returned otherwise).

lib/AsmParser/LLParser.cpp

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ bool LLParser::ValidateEndOfModule() {
140140
// If the alignment was parsed as an attribute, move to the alignment
141141
// field.
142142
if (FnAttrs.hasAlignmentAttr()) {
143-
Fn->setAlignment(MaybeAlign(FnAttrs.getAlignment()));
143+
Fn->setAlignment(FnAttrs.getAlignment());
144144
FnAttrs.removeAttribute(Attribute::Alignment);
145145
}
146146

@@ -1122,9 +1122,9 @@ bool LLParser::ParseGlobal(const std::string &Name, LocTy NameLoc,
11221122
if (ParseToken(lltok::StringConstant, "expected partition string"))
11231123
return true;
11241124
} else if (Lex.getKind() == lltok::kw_align) {
1125-
unsigned Alignment;
1125+
MaybeAlign Alignment;
11261126
if (ParseOptionalAlignment(Alignment)) return true;
1127-
GV->setAlignment(MaybeAlign(Alignment));
1127+
GV->setAlignment(Alignment);
11281128
} else if (Lex.getKind() == lltok::MetadataVar) {
11291129
if (ParseGlobalObjectMetadataAttachment(*GV))
11301130
return true;
@@ -1229,12 +1229,13 @@ bool LLParser::ParseFnAttributeValuePairs(AttrBuilder &B,
12291229
// As a hack, we allow function alignment to be initially parsed as an
12301230
// attribute on a function declaration/definition or added to an attribute
12311231
// group and later moved to the alignment field.
1232-
unsigned Alignment;
1232+
MaybeAlign Alignment;
12331233
if (inAttrGrp) {
12341234
Lex.Lex();
1235-
if (ParseToken(lltok::equal, "expected '=' here") ||
1236-
ParseUInt32(Alignment))
1235+
uint32_t Value = 0;
1236+
if (ParseToken(lltok::equal, "expected '=' here") || ParseUInt32(Value))
12371237
return true;
1238+
Alignment = Align(Value);
12381239
} else {
12391240
if (ParseOptionalAlignment(Alignment))
12401241
return true;
@@ -1603,7 +1604,7 @@ bool LLParser::ParseOptionalParamAttrs(AttrBuilder &B) {
16031604
continue;
16041605
}
16051606
case lltok::kw_align: {
1606-
unsigned Alignment;
1607+
MaybeAlign Alignment;
16071608
if (ParseOptionalAlignment(Alignment))
16081609
return true;
16091610
B.addAlignmentAttr(Alignment);
@@ -1720,7 +1721,7 @@ bool LLParser::ParseOptionalReturnAttrs(AttrBuilder &B) {
17201721
continue;
17211722
}
17221723
case lltok::kw_align: {
1723-
unsigned Alignment;
1724+
MaybeAlign Alignment;
17241725
if (ParseOptionalAlignment(Alignment))
17251726
return true;
17261727
B.addAlignmentAttr(Alignment);
@@ -2069,16 +2070,19 @@ bool LLParser::ParseOptionalFunctionMetadata(Function &F) {
20692070
/// ParseOptionalAlignment
20702071
/// ::= /* empty */
20712072
/// ::= 'align' 4
2072-
bool LLParser::ParseOptionalAlignment(unsigned &Alignment) {
2073-
Alignment = 0;
2073+
bool LLParser::ParseOptionalAlignment(MaybeAlign &Alignment) {
2074+
Alignment = None;
20742075
if (!EatIfPresent(lltok::kw_align))
20752076
return false;
20762077
LocTy AlignLoc = Lex.getLoc();
2077-
if (ParseUInt32(Alignment)) return true;
2078-
if (!isPowerOf2_32(Alignment))
2078+
uint32_t Value = 0;
2079+
if (ParseUInt32(Value))
2080+
return true;
2081+
if (!isPowerOf2_32(Value))
20792082
return Error(AlignLoc, "alignment is not a power of two");
2080-
if (Alignment > Value::MaximumAlignment)
2083+
if (Value > Value::MaximumAlignment)
20812084
return Error(AlignLoc, "huge alignments are not supported yet");
2085+
Alignment = Align(Value);
20822086
return false;
20832087
}
20842088

@@ -2115,7 +2119,7 @@ bool LLParser::ParseOptionalDerefAttrBytes(lltok::Kind AttrKind,
21152119
///
21162120
/// This returns with AteExtraComma set to true if it ate an excess comma at the
21172121
/// end.
2118-
bool LLParser::ParseOptionalCommaAlign(unsigned &Alignment,
2122+
bool LLParser::ParseOptionalCommaAlign(MaybeAlign &Alignment,
21192123
bool &AteExtraComma) {
21202124
AteExtraComma = false;
21212125
while (EatIfPresent(lltok::comma)) {
@@ -5370,7 +5374,7 @@ bool LLParser::ParseFunctionHeader(Function *&Fn, bool isDefine) {
53705374
LocTy BuiltinLoc;
53715375
std::string Section;
53725376
std::string Partition;
5373-
unsigned Alignment;
5377+
MaybeAlign Alignment;
53745378
std::string GC;
53755379
GlobalValue::UnnamedAddr UnnamedAddr = GlobalValue::UnnamedAddr::None;
53765380
unsigned AddrSpace = 0;
@@ -6865,7 +6869,7 @@ bool LLParser::ParseCall(Instruction *&Inst, PerFunctionState &PFS,
68656869
int LLParser::ParseAlloc(Instruction *&Inst, PerFunctionState &PFS) {
68666870
Value *Size = nullptr;
68676871
LocTy SizeLoc, TyLoc, ASLoc;
6868-
unsigned Alignment = 0;
6872+
MaybeAlign Alignment;
68696873
unsigned AddrSpace = 0;
68706874
Type *Ty = nullptr;
68716875

@@ -6913,7 +6917,8 @@ int LLParser::ParseAlloc(Instruction *&Inst, PerFunctionState &PFS) {
69136917
if (Size && !Size->getType()->isIntegerTy())
69146918
return Error(SizeLoc, "element count must have integer type");
69156919

6916-
AllocaInst *AI = new AllocaInst(Ty, AddrSpace, Size, Alignment);
6920+
AllocaInst *AI =
6921+
new AllocaInst(Ty, AddrSpace, Size, Alignment ? Alignment->value() : 0);
69176922
AI->setUsedWithInAlloca(IsInAlloca);
69186923
AI->setSwiftError(IsSwiftError);
69196924
Inst = AI;
@@ -6926,7 +6931,7 @@ int LLParser::ParseAlloc(Instruction *&Inst, PerFunctionState &PFS) {
69266931
/// 'singlethread'? AtomicOrdering (',' 'align' i32)?
69276932
int LLParser::ParseLoad(Instruction *&Inst, PerFunctionState &PFS) {
69286933
Value *Val; LocTy Loc;
6929-
unsigned Alignment = 0;
6934+
MaybeAlign Alignment;
69306935
bool AteExtraComma = false;
69316936
bool isAtomic = false;
69326937
AtomicOrdering Ordering = AtomicOrdering::NotAtomic;
@@ -6964,7 +6969,8 @@ int LLParser::ParseLoad(Instruction *&Inst, PerFunctionState &PFS) {
69646969
return Error(ExplicitTypeLoc,
69656970
"explicit pointee type doesn't match operand's pointee type");
69666971

6967-
Inst = new LoadInst(Ty, Val, "", isVolatile, Alignment, Ordering, SSID);
6972+
Inst = new LoadInst(Ty, Val, "", isVolatile,
6973+
Alignment ? Alignment->value() : 0, Ordering, SSID);
69686974
return AteExtraComma ? InstExtraComma : InstNormal;
69696975
}
69706976

@@ -6975,7 +6981,7 @@ int LLParser::ParseLoad(Instruction *&Inst, PerFunctionState &PFS) {
69756981
/// 'singlethread'? AtomicOrdering (',' 'align' i32)?
69766982
int LLParser::ParseStore(Instruction *&Inst, PerFunctionState &PFS) {
69776983
Value *Val, *Ptr; LocTy Loc, PtrLoc;
6978-
unsigned Alignment = 0;
6984+
MaybeAlign Alignment;
69796985
bool AteExtraComma = false;
69806986
bool isAtomic = false;
69816987
AtomicOrdering Ordering = AtomicOrdering::NotAtomic;
@@ -7011,7 +7017,8 @@ int LLParser::ParseStore(Instruction *&Inst, PerFunctionState &PFS) {
70117017
Ordering == AtomicOrdering::AcquireRelease)
70127018
return Error(Loc, "atomic store cannot use Acquire ordering");
70137019

7014-
Inst = new StoreInst(Val, Ptr, isVolatile, Alignment, Ordering, SSID);
7020+
Inst = new StoreInst(Val, Ptr, isVolatile, Alignment ? Alignment->value() : 0,
7021+
Ordering, SSID);
70157022
return AteExtraComma ? InstExtraComma : InstNormal;
70167023
}
70177024

lib/AsmParser/LLParser.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,14 +281,14 @@ namespace llvm {
281281
void ParseOptionalVisibility(unsigned &Res);
282282
void ParseOptionalDLLStorageClass(unsigned &Res);
283283
bool ParseOptionalCallingConv(unsigned &CC);
284-
bool ParseOptionalAlignment(unsigned &Alignment);
284+
bool ParseOptionalAlignment(MaybeAlign &Alignment);
285285
bool ParseOptionalDerefAttrBytes(lltok::Kind AttrKind, uint64_t &Bytes);
286286
bool ParseScopeAndOrdering(bool isAtomic, SyncScope::ID &SSID,
287287
AtomicOrdering &Ordering);
288288
bool ParseScope(SyncScope::ID &SSID);
289289
bool ParseOrdering(AtomicOrdering &Ordering);
290290
bool ParseOptionalStackAlignment(unsigned &Alignment);
291-
bool ParseOptionalCommaAlign(unsigned &Alignment, bool &AteExtraComma);
291+
bool ParseOptionalCommaAlign(MaybeAlign &Alignment, bool &AteExtraComma);
292292
bool ParseOptionalCommaAddrSpace(unsigned &AddrSpace, LocTy &Loc,
293293
bool &AteExtraComma);
294294
bool ParseOptionalCommaInAlloca(bool &IsInAlloca);

lib/IR/Attributes.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -779,10 +779,12 @@ AttributeSetNode *AttributeSetNode::get(LLVMContext &C, const AttrBuilder &B) {
779779
Attr = Attribute::getWithByValType(C, B.getByValType());
780780
break;
781781
case Attribute::Alignment:
782-
Attr = Attribute::getWithAlignment(C, Align(B.getAlignment()));
782+
assert(B.getAlignment() && "Alignment must be set");
783+
Attr = Attribute::getWithAlignment(C, *B.getAlignment());
783784
break;
784785
case Attribute::StackAlignment:
785-
Attr = Attribute::getWithStackAlignment(C, Align(B.getStackAlignment()));
786+
assert(B.getStackAlignment() && "StackAlignment must be set");
787+
Attr = Attribute::getWithStackAlignment(C, *B.getStackAlignment());
786788
break;
787789
case Attribute::Dereferenceable:
788790
Attr = Attribute::getWithDereferenceableBytes(
@@ -1162,7 +1164,7 @@ AttributeList AttributeList::addAttributes(LLVMContext &C, unsigned Index,
11621164
// FIXME it is not obvious how this should work for alignment. For now, say
11631165
// we can't change a known alignment.
11641166
const MaybeAlign OldAlign = getAttributes(Index).getAlignment();
1165-
unsigned NewAlign = B.getAlignment();
1167+
const MaybeAlign NewAlign = B.getAlignment();
11661168
assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
11671169
"Attempt to change alignment!");
11681170
#endif
@@ -1460,9 +1462,9 @@ AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) {
14601462
Attrs[Kind] = true;
14611463

14621464
if (Kind == Attribute::Alignment)
1463-
Alignment = MaybeAlign(Attr.getAlignment());
1465+
Alignment = Attr.getAlignment();
14641466
else if (Kind == Attribute::StackAlignment)
1465-
StackAlignment = MaybeAlign(Attr.getStackAlignment());
1467+
StackAlignment = Attr.getStackAlignment();
14661468
else if (Kind == Attribute::ByVal)
14671469
ByValType = Attr.getValueAsType();
14681470
else if (Kind == Attribute::Dereferenceable)

0 commit comments

Comments
 (0)