[llvm] b091c9a - LLParser: Accept align(N) as new syntax for parameter attribute

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 26 15:18:16 PDT 2020


Author: Matt Arsenault
Date: 2020-06-26T18:10:21-04:00
New Revision: b091c9a3e180f45c35a7e1fdd8e383c0098d9210

URL: https://github.com/llvm/llvm-project/commit/b091c9a3e180f45c35a7e1fdd8e383c0098d9210
DIFF: https://github.com/llvm/llvm-project/commit/b091c9a3e180f45c35a7e1fdd8e383c0098d9210.diff

LOG: 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.

Added: 
    llvm/test/Assembler/align-param-attr-error0.ll
    llvm/test/Assembler/align-param-attr-error1.ll
    llvm/test/Assembler/align-param-attr-error2.ll
    llvm/test/Assembler/align-param-attr-format.ll

Modified: 
    llvm/docs/LangRef.rst
    llvm/lib/AsmParser/LLParser.cpp
    llvm/lib/AsmParser/LLParser.h

Removed: 
    


################################################################################
diff  --git a/llvm/docs/LangRef.rst b/llvm/docs/LangRef.rst
index 4adf958d86a6..0b2a908b9c65 100644
--- a/llvm/docs/LangRef.rst
+++ b/llvm/docs/LangRef.rst
@@ -1130,7 +1130,7 @@ Currently, only the following parameter attributes are defined:
 
 .. _attr_align:
 
-``align <n>``
+``align <n>`` or ``align(<n>)``
     This indicates that the pointer value may be assumed by the optimizer to
     have the specified alignment.  If the pointer value does not have the
     specified alignment, behavior is undefined.

diff  --git a/llvm/lib/AsmParser/LLParser.cpp b/llvm/lib/AsmParser/LLParser.cpp
index 9bd56cddb5c7..db4fbfd7e3c9 100644
--- a/llvm/lib/AsmParser/LLParser.cpp
+++ b/llvm/lib/AsmParser/LLParser.cpp
@@ -1641,7 +1641,7 @@ bool LLParser::ParseOptionalParamAttrs(AttrBuilder &B) {
     }
     case lltok::kw_align: {
       MaybeAlign Alignment;
-      if (ParseOptionalAlignment(Alignment))
+      if (ParseOptionalAlignment(Alignment, true))
         return true;
       B.addAlignmentAttr(Alignment);
       continue;
@@ -2127,14 +2127,26 @@ bool LLParser::ParseOptionalFunctionMetadata(Function &F) {
 /// ParseOptionalAlignment
 ///   ::= /* empty */
 ///   ::= 'align' 4
-bool LLParser::ParseOptionalAlignment(MaybeAlign &Alignment) {
+bool LLParser::ParseOptionalAlignment(MaybeAlign &Alignment, bool AllowParens) {
   Alignment = None;
   if (!EatIfPresent(lltok::kw_align))
     return false;
   LocTy AlignLoc = Lex.getLoc();
   uint32_t Value = 0;
+
+  LocTy ParenLoc = Lex.getLoc();
+  bool HaveParens = false;
+  if (AllowParens) {
+    if (EatIfPresent(lltok::lparen))
+      HaveParens = true;
+  }
+
   if (ParseUInt32(Value))
     return true;
+
+  if (HaveParens && !EatIfPresent(lltok::rparen))
+    return Error(ParenLoc, "expected ')'");
+
   if (!isPowerOf2_32(Value))
     return Error(AlignLoc, "alignment is not a power of two");
   if (Value > Value::MaximumAlignment)

diff  --git a/llvm/lib/AsmParser/LLParser.h b/llvm/lib/AsmParser/LLParser.h
index cc3aae6be593..ebd8655dc35e 100644
--- a/llvm/lib/AsmParser/LLParser.h
+++ b/llvm/lib/AsmParser/LLParser.h
@@ -272,7 +272,8 @@ namespace llvm {
     void ParseOptionalVisibility(unsigned &Res);
     void ParseOptionalDLLStorageClass(unsigned &Res);
     bool ParseOptionalCallingConv(unsigned &CC);
-    bool ParseOptionalAlignment(MaybeAlign &Alignment);
+    bool ParseOptionalAlignment(MaybeAlign &Alignment,
+                                bool AllowParens = false);
     bool ParseOptionalDerefAttrBytes(lltok::Kind AttrKind, uint64_t &Bytes);
     bool ParseScopeAndOrdering(bool isAtomic, SyncScope::ID &SSID,
                                AtomicOrdering &Ordering);

diff  --git a/llvm/test/Assembler/align-param-attr-error0.ll b/llvm/test/Assembler/align-param-attr-error0.ll
new file mode 100644
index 000000000000..7760c014998b
--- /dev/null
+++ b/llvm/test/Assembler/align-param-attr-error0.ll
@@ -0,0 +1,7 @@
+; RUN: not llvm-as < %s 2>&1 | FileCheck %s
+; Test parse errors when using form of align attribute with parentheses
+
+; CHECK:  <stdin>:[[@LINE+1]]:38: error: expected ')'
+define void @missing_rparen(i8* align(4 %ptr) {
+  ret void
+}

diff  --git a/llvm/test/Assembler/align-param-attr-error1.ll b/llvm/test/Assembler/align-param-attr-error1.ll
new file mode 100644
index 000000000000..465fb2bfc629
--- /dev/null
+++ b/llvm/test/Assembler/align-param-attr-error1.ll
@@ -0,0 +1,7 @@
+; RUN: not llvm-as < %s 2>&1 | FileCheck %s
+; Test parse errors when using form of align attribute with parentheses
+
+; CHECK:  <stdin>:[[@LINE+1]]:42: error: expected '{' in function body
+define void @missing_Lparen(i8* align 4) %ptr) {
+  ret void
+}

diff  --git a/llvm/test/Assembler/align-param-attr-error2.ll b/llvm/test/Assembler/align-param-attr-error2.ll
new file mode 100644
index 000000000000..7bfcd614a740
--- /dev/null
+++ b/llvm/test/Assembler/align-param-attr-error2.ll
@@ -0,0 +1,7 @@
+; RUN: not llvm-as < %s 2>&1 | FileCheck %s
+; Test parse errors when using form of align attribute with parentheses
+
+; CHECK:  <stdin>:[[@LINE+1]]:39: error: expected integer
+define void @missing_value(i8* align () %ptr) {
+  ret void
+}

diff  --git a/llvm/test/Assembler/align-param-attr-format.ll b/llvm/test/Assembler/align-param-attr-format.ll
new file mode 100644
index 000000000000..91ec0c6f006c
--- /dev/null
+++ b/llvm/test/Assembler/align-param-attr-format.ll
@@ -0,0 +1,13 @@
+; RUN: llvm-as < %s | llvm-dis | llvm-as | llvm-dis | FileCheck %s
+
+; Test that align(N) is accepted as an alternative syntax to align N
+
+; CHECK: define void @param_align4(i8* align 4 %ptr) {
+define void @param_align4(i8* align(4) %ptr) {
+  ret void
+}
+
+; CHECK: define void @param_align128(i8* align 128 %0) {
+define void @param_align128(i8* align(128)) {
+  ret void
+}


        


More information about the llvm-commits mailing list