This is an archive of the discontinued LLVM Phabricator instance.

[NVPTX] Work around syntax error in PTX assembly
Needs ReviewPublic

Authored by ldrumm on Jan 5 2023, 10:00 AM.

Details

Summary

The PTX assembler doesn't like dots in symbol names. We have a pass that
fixes this (NVPTXAssignValidGlobalNames - since 264cd4672d), but there's
a pernicious bug in the way debug info is emitted that means this pass
alone won't work properly if it's ever run as part of the codegen
pipeline involving debug info emission.

AsmPrinter::doInitialization collects all metadata and initializes all
the DWARF DIEs that are later emitted after the main module has been
printed. Naturally, doInitialization is executed *before* any of the
other passes contained in the same PassManager execute their
runOnModule functions that may arbitrarily modify the module.
If any of those functions try and modify the module in such a way that
the debuginfo goes out of date, then the debuginfo *will* be out of date
once the AsmPrinter actually gets to finalize printing the debug info.

As far as I can tell: any code that runs in the same PassManager as the
AsmPrinter could result in bad debug info.

I don't see a simple way around this, unless DWARF debug info is
collected and printed a different way - probably during the
runOnModule method. That will be much more disruptive change, so for
now the small (!) hack introduced here sanitizes the symbol names
referred to by the MCSymbolRefExpr that is earlier constructed by the
AsmPrinter's DWARF handler.

N.B. There's the slight possibility that this also introduces a new bug

  • sanitized symbol names referred to by the MCExprs may not be of

private linkage, and so may not actually exist. Given that symbols with
dots in them are are already illegal according to ptxas, I don't think
this is a practical issue to worry about.

Since the NVPTXUtilities are now used by the MC layer, I've hoisted them
into their own Utils lib as per ARM and AMDGPU.

Fixes https://github.com/intel/llvm/issues/5980

Diff Detail

Event Timeline

ldrumm created this revision.Jan 5 2023, 10:00 AM
Herald added a project: Restricted Project. · View Herald TranscriptJan 5 2023, 10:00 AM
ldrumm requested review of this revision.Jan 5 2023, 10:00 AM
Herald added a project: Restricted Project. · View Herald TranscriptJan 5 2023, 10:00 AM
Herald added a subscriber: wdng. · View Herald Transcript

TODO: Testcase

tra added a comment.Jan 5 2023, 10:51 AM

LGTM in principle. It should be good to go once the tests are added.

ldrumm added a comment.Jan 9 2023, 4:44 AM

Slight fly in the ointment: The existing tests exposed that that this implementation is too naive and is renaming things it shouldn't. I'll be back with a more mature implementation. Thanks for the reviews and patience

tra added a comment.Jan 9 2023, 2:06 PM

Slight fly in the ointment: The existing tests exposed that that this implementation is too naive and is renaming things it shouldn't. I'll be back with a more mature implementation. Thanks for the reviews and patience

For what it's worth, we did have a handful of attempts to improve PTX symbol character set compliance in the past, but we've failed to find a much better fix than what we have now. IIRC, the proposal closest to 'generic' solution was to make back-end responsible for providing the legal character set for the symbols and then make all IT transforms use it, instead of the current implicit assumptions we're relying on.

arsenm resigned from this revision.Feb 15 2023, 10:48 AM

I'd expect this to be dealt with in Mangler