[PATCH] D138789: [LLParser] Support symbolic address space numbers

Alexander Richardson via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Nov 28 04:02:01 PST 2022


arichardson created this revision.
arichardson added reviewers: jrtc27, arsenm, nikic.
Herald added a subscriber: hiraditya.
Herald added a project: All.
arichardson requested review of this revision.
Herald added subscribers: llvm-commits, wdng.
Herald added a project: LLVM.

This allows the LLParser to also accept "A", "G", and "P" in `addrspace`
usages. "A" will be replaced by the alloca address space defined in the
globals, "G" by the default globals address space and "P" by the program
address space. This makes it easier to write tests that use different
address space and only only vary the RUN: lines. Currently, the only
alternative is to pre-process the sources with a tool such as `sed`

Importantly, these new string values are only accepted in .ll files and
not stored in the bitcode format, so it does not round-trip via llvm-as
and llvm-dis (see newly added test).


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D138789

Files:
  llvm/lib/AsmParser/LLParser.cpp
  llvm/test/Assembler/symbolic-addrspace.ll


Index: llvm/test/Assembler/symbolic-addrspace.ll
===================================================================
--- /dev/null
+++ llvm/test/Assembler/symbolic-addrspace.ll
@@ -0,0 +1,51 @@
+;; Check that we can parse symbolic addres space constants "A", "G", "P".
+;; NB: These do not round-trip via llvm-as, they are purely for initial parsing
+;; and will be converted to a numerical constant that does not depend on the
+;; datalayout by the .ll parser.
+; RUN: split-file %s %t --leading-lines
+; RUN: llvm-as < %t/valid.ll | llvm-dis | FileCheck %s
+; RUN: not llvm-as < %t/bad-not-string.ll 2>&1
+; RUN: not llvm-as < %t/bad-not-string.ll 2>&1 | FileCheck %s --check-prefix=ERR-NOT-STR
+; RUN: not llvm-as < %t/bad-unknown-char.ll 2>&1 | FileCheck %s --check-prefix=ERR-BAD-CHAR
+; RUN: not llvm-as < %t/bad-multiple-valid-chars.ll 2>&1 | FileCheck %s --check-prefix=ERR-MULTIPLE-CHARS
+; RUN: not llvm-as < %t/bad-using-at-symbol.ll 2>&1 | FileCheck %s --check-prefix=ERR-AT-SYMBOL
+
+;--- valid.ll
+target datalayout = "A1-G2-P3"
+; CHECK: target datalayout = "A1-G2-P3"
+
+; CHECK: @str = private addrspace(2) constant [4 x i8] c"str\00"
+ at str = private addrspace("G") constant [4 x i8] c"str\00"
+
+define void @foo() {
+  ; CHECK: %alloca = alloca i32, align 4, addrspace(1)
+  %alloca = alloca i32, addrspace("A")
+  ret void
+}
+
+; CHECK: define void @bar() addrspace(3) {
+define void @bar() addrspace("P") {
+  ; CHECK: call addrspace(3) void @foo()
+  call addrspace("P") void @foo()
+  ret void
+}
+
+;--- bad-not-string.ll
+target datalayout = "G2"
+ at str = private addrspace(D) constant [4 x i8] c"str\00"
+; ERR-NOT-STR: [[#@LINE-1]]:26: error: expected integer or string constant
+
+;--- bad-unknown-char.ll
+target datalayout = "G2"
+ at str = private addrspace("D") constant [4 x i8] c"str\00"
+; ERR-BAD-CHAR: [[#@LINE-1]]:26: error: invalid symbolic addrspace 'D'
+
+;--- bad-multiple-valid-chars.ll
+target datalayout = "A1-G2"
+ at str = private addrspace("AG") constant [4 x i8] c"str\00"
+; ERR-MULTIPLE-CHARS: [[#@LINE-1]]:26: error: invalid symbolic addrspace 'AG'
+
+;--- bad-using-at-symbol.ll
+target datalayout = "A1-G2"
+ at str = private addrspace(@A) constant [4 x i8] c"str\00"
+; ERR-AT-SYMBOL: [[#@LINE-1]]:26: error: expected integer or string constant
Index: llvm/lib/AsmParser/LLParser.cpp
===================================================================
--- llvm/lib/AsmParser/LLParser.cpp
+++ llvm/lib/AsmParser/LLParser.cpp
@@ -1786,8 +1786,29 @@
   AddrSpace = DefaultAS;
   if (!EatIfPresent(lltok::kw_addrspace))
     return false;
+
+  auto ParseAddrspaceValue = [&](unsigned &AddrSpace) -> bool {
+    if (Lex.getKind() == lltok::StringConstant) {
+      auto AddrSpaceStr = Lex.getStrVal();
+      if (AddrSpaceStr == "A") {
+        AddrSpace = M->getDataLayout().getAllocaAddrSpace();
+      } else if (AddrSpaceStr == "G") {
+        AddrSpace = M->getDataLayout().getDefaultGlobalsAddressSpace();
+      } else if (AddrSpaceStr == "P") {
+        AddrSpace = M->getDataLayout().getProgramAddressSpace();
+      } else {
+        return tokError("invalid symbolic addrspace '" + AddrSpaceStr + "'");
+      }
+      Lex.Lex();
+      return false;
+    } else if (Lex.getKind() != lltok::APSInt) {
+      return tokError("expected integer or string constant");
+    }
+    return parseUInt32(AddrSpace);
+  };
+
   return parseToken(lltok::lparen, "expected '(' in address space") ||
-         parseUInt32(AddrSpace) ||
+         ParseAddrspaceValue(AddrSpace) ||
          parseToken(lltok::rparen, "expected ')' in address space");
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D138789.478187.patch
Type: text/x-patch
Size: 3594 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221128/aa138055/attachment.bin>


More information about the llvm-commits mailing list