This is an archive of the discontinued LLVM Phabricator instance.

[NVPTX] Disable parens for identifiers starting with '$'
ClosedPublic

Authored by asavonic on Apr 13 2022, 12:15 PM.

Details

Summary

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.

Diff Detail

Event Timeline

asavonic created this revision.Apr 13 2022, 12:15 PM
Herald added a project: Restricted Project. · View Herald TranscriptApr 13 2022, 12:15 PM
Herald added a subscriber: hiraditya. · View Herald Transcript
asavonic requested review of this revision.Apr 13 2022, 12:15 PM
Herald added a project: Restricted Project. · View Herald TranscriptApr 13 2022, 12:15 PM

This is the same issue as in D119669:

Looks like it broke CUDA:
https://lab.llvm.org/buildbot/#/builders/46/builds/23372/steps/3/logs/stdio

ptxas /tmp/complex-78abf1/complex-sm_75.s, line 250; fatal   : Parsing error near '$L__BB6_2': syntax error
asavonic updated this revision to Diff 422597.Apr 13 2022, 12:27 PM
  • Rebased and added patch context.
tra accepted this revision.Apr 13 2022, 2:30 PM

LGTM.

llvm/lib/MC/MCExpr.cpp
79–82

Nit: LLVM code style typically omits braces around single statement bodies.
It may be even worth folding it all into:

bool UseParens = MAI && MAI->useParensForDollarSignNames() && !(InParens || Sym.getName().empty() || Sym.getName()[0] != '$');
llvm/test/CodeGen/NVPTX/no-extra-parens.ll
12

For the record, right now LLVM generates ($str) and that's what ptxas complains about. https://cuda.godbolt.org/z/s7ahYn5oM

This revision is now accepted and ready to land.Apr 13 2022, 2:30 PM
This revision was landed with ongoing or failed builds.Apr 14 2022, 11:08 AM
This revision was automatically updated to reflect the committed changes.
tra added a comment.Apr 14 2022, 11:35 AM

What happened this time? AFAICT CUDA bots were fine with the change.

What happened this time? AFAICT CUDA bots were fine with the change.

The patch failed on Mips32:
https://lab.llvm.org/buildbot#builders/109/builds/36628

# CHECK: # fixup A - offset: 0, value: ($tmp0), kind: fixup_Mips_26
<stdin>:580:2: note: possible intended match here
# fixup A - offset: 0, value: $tmp0, kind: fixup_Mips_26

It is probably because MAI is missing for some reason..

asavonic updated this revision to Diff 423082.Apr 15 2022, 6:39 AM
asavonic edited the summary of this revision. (Show Details)
  • Fixed issues found in Mips LIT tests: MCAsmStreamer had one call to 'operator<<' function which doesn't pass MAI to MCExpr::print.
asavonic reopened this revision.Apr 15 2022, 6:39 AM
This revision is now accepted and ready to land.Apr 15 2022, 6:39 AM
tra added inline comments.Apr 15 2022, 11:25 AM
llvm/lib/MC/MCAsmStreamer.cpp
2256

This is awkward.
Perhaps we should consider adding some sort of withMAI(F.getValue(), MAI) -> std::pair<MCExpr*, MCAsmInfo*> wrapper and an overloaded operator<< to catch it and pass MAI along to MCExpr::print().

asavonic added inline comments.Apr 15 2022, 1:22 PM
llvm/lib/MC/MCAsmStreamer.cpp
2256

Well, this is how MCExpr is printed for assembly output - there are more than 50 occurrences in this file (also in MCTargetDesc). I personally find these two forms equally readable, so it's probably not worth it to refactor everything...

tra added inline comments.Apr 15 2022, 2:50 PM
llvm/lib/MC/MCAsmStreamer.cpp
2256

Up to you. I would find having to break OS << A << B .... chain for some values, but it's indeed a cosmetic change. It could be done as a clean-up in a separate patch.

This revision was landed with ongoing or failed builds.Apr 17 2022, 8:03 AM
This revision was automatically updated to reflect the committed changes.