Skip to content

Commit b091c9a

Browse files
committed
LLParser: Accept align(N) as new syntax for parameter attribute
Every other value parameter attribute uses parentheses, so accept this as the preferred modern syntax. Updating everything to use the new syntax is left for a future change.
1 parent 443556c commit b091c9a

File tree

7 files changed

+51
-4
lines changed

7 files changed

+51
-4
lines changed

llvm/docs/LangRef.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1130,7 +1130,7 @@ Currently, only the following parameter attributes are defined:
11301130

11311131
.. _attr_align:
11321132

1133-
``align <n>``
1133+
``align <n>`` or ``align(<n>)``
11341134
This indicates that the pointer value may be assumed by the optimizer to
11351135
have the specified alignment. If the pointer value does not have the
11361136
specified alignment, behavior is undefined.

llvm/lib/AsmParser/LLParser.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1641,7 +1641,7 @@ bool LLParser::ParseOptionalParamAttrs(AttrBuilder &B) {
16411641
}
16421642
case lltok::kw_align: {
16431643
MaybeAlign Alignment;
1644-
if (ParseOptionalAlignment(Alignment))
1644+
if (ParseOptionalAlignment(Alignment, true))
16451645
return true;
16461646
B.addAlignmentAttr(Alignment);
16471647
continue;
@@ -2127,14 +2127,26 @@ bool LLParser::ParseOptionalFunctionMetadata(Function &F) {
21272127
/// ParseOptionalAlignment
21282128
/// ::= /* empty */
21292129
/// ::= 'align' 4
2130-
bool LLParser::ParseOptionalAlignment(MaybeAlign &Alignment) {
2130+
bool LLParser::ParseOptionalAlignment(MaybeAlign &Alignment, bool AllowParens) {
21312131
Alignment = None;
21322132
if (!EatIfPresent(lltok::kw_align))
21332133
return false;
21342134
LocTy AlignLoc = Lex.getLoc();
21352135
uint32_t Value = 0;
2136+
2137+
LocTy ParenLoc = Lex.getLoc();
2138+
bool HaveParens = false;
2139+
if (AllowParens) {
2140+
if (EatIfPresent(lltok::lparen))
2141+
HaveParens = true;
2142+
}
2143+
21362144
if (ParseUInt32(Value))
21372145
return true;
2146+
2147+
if (HaveParens && !EatIfPresent(lltok::rparen))
2148+
return Error(ParenLoc, "expected ')'");
2149+
21382150
if (!isPowerOf2_32(Value))
21392151
return Error(AlignLoc, "alignment is not a power of two");
21402152
if (Value > Value::MaximumAlignment)

llvm/lib/AsmParser/LLParser.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,8 @@ namespace llvm {
272272
void ParseOptionalVisibility(unsigned &Res);
273273
void ParseOptionalDLLStorageClass(unsigned &Res);
274274
bool ParseOptionalCallingConv(unsigned &CC);
275-
bool ParseOptionalAlignment(MaybeAlign &Alignment);
275+
bool ParseOptionalAlignment(MaybeAlign &Alignment,
276+
bool AllowParens = false);
276277
bool ParseOptionalDerefAttrBytes(lltok::Kind AttrKind, uint64_t &Bytes);
277278
bool ParseScopeAndOrdering(bool isAtomic, SyncScope::ID &SSID,
278279
AtomicOrdering &Ordering);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
; RUN: not llvm-as < %s 2>&1 | FileCheck %s
2+
; Test parse errors when using form of align attribute with parentheses
3+
4+
; CHECK: <stdin>:[[@LINE+1]]:38: error: expected ')'
5+
define void @missing_rparen(i8* align(4 %ptr) {
6+
ret void
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
; RUN: not llvm-as < %s 2>&1 | FileCheck %s
2+
; Test parse errors when using form of align attribute with parentheses
3+
4+
; CHECK: <stdin>:[[@LINE+1]]:42: error: expected '{' in function body
5+
define void @missing_Lparen(i8* align 4) %ptr) {
6+
ret void
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
; RUN: not llvm-as < %s 2>&1 | FileCheck %s
2+
; Test parse errors when using form of align attribute with parentheses
3+
4+
; CHECK: <stdin>:[[@LINE+1]]:39: error: expected integer
5+
define void @missing_value(i8* align () %ptr) {
6+
ret void
7+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
; RUN: llvm-as < %s | llvm-dis | llvm-as | llvm-dis | FileCheck %s
2+
3+
; Test that align(N) is accepted as an alternative syntax to align N
4+
5+
; CHECK: define void @param_align4(i8* align 4 %ptr) {
6+
define void @param_align4(i8* align(4) %ptr) {
7+
ret void
8+
}
9+
10+
; CHECK: define void @param_align128(i8* align 128 %0) {
11+
define void @param_align128(i8* align(128)) {
12+
ret void
13+
}

0 commit comments

Comments
 (0)