[llvm] 52053aa - [NVPTX] Disable parens for identifiers starting with '$'

Andrew Savonichev via llvm-commits llvm-commits at lists.llvm.org
Sun Apr 17 08:02:55 PDT 2022


Author: Andrew Savonichev
Date: 2022-04-17T18:02:33+03:00
New Revision: 52053aa94fe951ebf71ce3db01cf93ac9855ba8f

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

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

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.

Targets that require parens around identifiers that start with '$'
should always pass MCAsmInfo to MCExpr::print.
Therefore 'operator<<(raw_ostream &, MCExpr&)' should be avoided
because it calls MCExpr::print with nullptr MAI.

Differential Revision: https://reviews.llvm.org/D123702

Added: 
    llvm/test/CodeGen/NVPTX/no-extra-parens.ll

Modified: 
    llvm/include/llvm/MC/MCAsmInfo.h
    llvm/lib/MC/MCAsmStreamer.cpp
    llvm/lib/MC/MCExpr.cpp
    llvm/lib/Target/NVPTX/MCTargetDesc/NVPTXMCAsmInfo.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/MC/MCAsmInfo.h b/llvm/include/llvm/MC/MCAsmInfo.h
index 1be65a77610b3..578518e6dc80a 100644
--- a/llvm/include/llvm/MC/MCAsmInfo.h
+++ b/llvm/include/llvm/MC/MCAsmInfo.h
@@ -478,6 +478,10 @@ class MCAsmInfo {
   /// 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;
@@ -789,6 +793,9 @@ class MCAsmInfo {
   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;
   }

diff  --git a/llvm/lib/MC/MCAsmStreamer.cpp b/llvm/lib/MC/MCAsmStreamer.cpp
index b27efea5c9c5a..d73025d2d26cc 100644
--- a/llvm/lib/MC/MCAsmStreamer.cpp
+++ b/llvm/lib/MC/MCAsmStreamer.cpp
@@ -2251,8 +2251,10 @@ void MCAsmStreamer::AddEncodingComment(const MCInst &Inst,
     MCFixup &F = Fixups[i];
     const MCFixupKindInfo &Info =
         getAssembler().getBackend().getFixupKindInfo(F.getKind());
-    OS << "  fixup " << char('A' + i) << " - " << "offset: " << F.getOffset()
-       << ", value: " << *F.getValue() << ", kind: " << Info.Name << "\n";
+    OS << "  fixup " << char('A' + i) << " - "
+       << "offset: " << F.getOffset() << ", value: ";
+    F.getValue()->print(OS, MAI);
+    OS << ", kind: " << Info.Name << "\n";
   }
 }
 

diff  --git a/llvm/lib/MC/MCExpr.cpp b/llvm/lib/MC/MCExpr.cpp
index 64deb041ce5d7..45a3d938257ae 100644
--- a/llvm/lib/MC/MCExpr.cpp
+++ b/llvm/lib/MC/MCExpr.cpp
@@ -75,8 +75,9 @@ void MCExpr::print(raw_ostream &OS, const MCAsmInfo *MAI, bool InParens) const {
     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 = MAI && MAI->useParensForDollarSignNames() && !InParens &&
+                     !Sym.getName().empty() && Sym.getName()[0] == '$';
+
     if (UseParens) {
       OS << '(';
       Sym.print(OS, MAI);

diff  --git a/llvm/lib/Target/NVPTX/MCTargetDesc/NVPTXMCAsmInfo.cpp b/llvm/lib/Target/NVPTX/MCTargetDesc/NVPTXMCAsmInfo.cpp
index f275011018a32..6bc3e43db1d6f 100644
--- a/llvm/lib/Target/NVPTX/MCTargetDesc/NVPTXMCAsmInfo.cpp
+++ b/llvm/lib/Target/NVPTX/MCTargetDesc/NVPTXMCAsmInfo.cpp
@@ -54,4 +54,8 @@ NVPTXMCAsmInfo::NVPTXMCAsmInfo(const Triple &TheTriple,
   GlobalDirective = "\t// .globl\t";
 
   UseIntegratedAssembler = false;
+
+  // Avoid using parens for identifiers starting with $ - ptxas does
+  // not expect them.
+  UseParensForDollarSignNames = false;
 }

diff  --git a/llvm/test/CodeGen/NVPTX/no-extra-parens.ll b/llvm/test/CodeGen/NVPTX/no-extra-parens.ll
new file mode 100644
index 0000000000000..22a89b8096306
--- /dev/null
+++ b/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
+}


        


More information about the llvm-commits mailing list