[PATCH] D123702: [NVPTX] Disable parens for identifiers starting with '$'

Andrew Savonichev via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 13 12:15:29 PDT 2022


asavonic created this revision.
asavonic added reviewers: tra, jholewinski, slydiman.
Herald added a subscriber: hiraditya.
Herald added a project: All.
asavonic requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

ptxas fails to parse such syntax:

  mov.u64 %rd1, ($str);
  fatal   : Parsing error near '$str': syntax error

A new MCAsmInfo option was added because InParens parameter of
MCExpr::print is not sufficient to disable parens
completely. MCExpr::print resets it to false for a recursive call in
case of unary or binary expressions.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D123702

Files:
  llvm/include/llvm/MC/MCAsmInfo.h
  llvm/lib/MC/MCExpr.cpp
  llvm/lib/Target/NVPTX/MCTargetDesc/NVPTXMCAsmInfo.cpp
  llvm/test/CodeGen/NVPTX/no-extra-parens.ll


Index: llvm/test/CodeGen/NVPTX/no-extra-parens.ll
===================================================================
--- /dev/null
+++ llvm/test/CodeGen/NVPTX/no-extra-parens.ll
@@ -0,0 +1,14 @@
+; RUN: llc < %s -march=nvptx64 -mcpu=sm_20 | FileCheck %s
+
+; ptxas has no special meaning for '$' character, so it should be used
+; without parens.
+
+@"$str" = private addrspace(1) constant [4 x i8] c"str\00"
+
+declare void @str2(i8* %str)
+define void @str1() {
+entry:
+;; CHECK: mov.u64 %rd{{[0-9]+}}, $str;
+  tail call void @str2(i8* getelementptr ([4 x i8], [4 x i8]* addrspacecast ([4 x i8] addrspace(1)* @"$str" to [4 x i8]*), i64 0, i64 0))
+  ret void
+}
Index: llvm/lib/Target/NVPTX/MCTargetDesc/NVPTXMCAsmInfo.cpp
===================================================================
--- llvm/lib/Target/NVPTX/MCTargetDesc/NVPTXMCAsmInfo.cpp
+++ llvm/lib/Target/NVPTX/MCTargetDesc/NVPTXMCAsmInfo.cpp
@@ -58,4 +58,8 @@
   // ptxas does not support DWARF `.file fileno directory filename'
   // syntax as of v11.X.
   EnableDwarfFileDirectoryDefault = false;
+
+  // Avoid using parens for identifiers starting with $ - ptxas does
+  // not expect them.
+  UseParensForDollarSignNames = false;
 }
Index: llvm/lib/MC/MCExpr.cpp
===================================================================
--- llvm/lib/MC/MCExpr.cpp
+++ llvm/lib/MC/MCExpr.cpp
@@ -73,10 +73,15 @@
   case MCExpr::SymbolRef: {
     const MCSymbolRefExpr &SRE = cast<MCSymbolRefExpr>(*this);
     const MCSymbol &Sym = SRE.getSymbol();
+
     // Parenthesize names that start with $ so that they don't look like
     // absolute names.
-    bool UseParens =
-        !InParens && !Sym.getName().empty() && Sym.getName()[0] == '$';
+    bool UseParens = false;
+    if (MAI && MAI->useParensForDollarSignNames()) {
+      UseParens =
+          !InParens && !Sym.getName().empty() && Sym.getName()[0] == '$';
+    }
+
     if (UseParens) {
       OS << '(';
       Sym.print(OS, MAI);
Index: llvm/include/llvm/MC/MCAsmInfo.h
===================================================================
--- llvm/include/llvm/MC/MCAsmInfo.h
+++ llvm/include/llvm/MC/MCAsmInfo.h
@@ -482,6 +482,10 @@
   /// For example, foo(plt) instead of foo at plt.  Defaults to false.
   bool UseParensForSymbolVariant = false;
 
+  /// True if the target uses parens for symbol names starting with
+  /// '$' character to distinguish them from absolute names.
+  bool UseParensForDollarSignNames = true;
+
   /// True if the target supports flags in ".loc" directive, false if only
   /// location is allowed.
   bool SupportsExtendedDwarfLocDirective = true;
@@ -792,6 +796,9 @@
   bool doDwarfFDESymbolsUseAbsDiff() const { return DwarfFDESymbolsUseAbsDiff; }
   bool useDwarfRegNumForCFI() const { return DwarfRegNumForCFI; }
   bool useParensForSymbolVariant() const { return UseParensForSymbolVariant; }
+  bool useParensForDollarSignNames() const {
+    return UseParensForDollarSignNames;
+  }
   bool supportsExtendedDwarfLocDirective() const {
     return SupportsExtendedDwarfLocDirective;
   }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D123702.422590.patch
Type: text/x-patch
Size: 3057 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220413/56221037/attachment.bin>


More information about the llvm-commits mailing list