This is an archive of the discontinued LLVM Phabricator instance.

[NVPTX] Disable DWARF .file directory for PTX
ClosedPublic

Authored by asavonic on Mar 9 2022, 8:30 AM.

Details

Summary

Default behavior for .file directory was changed in D105856, but
ptxas (CUDA 11.5 release) refuses to parse it:

$ llc -march=nvptx64 llvm/test/DebugInfo/NVPTX/debug-file-loc.ll
$ ptxas debug-file-loc.s
ptxas debug-file-loc.s, line 42; fatal   : Parsing error near
'"foo.h"': syntax error

Added a new field to MCAsmInfo to control default value of UseDwarfDirectory.
This value is used if -dwarf-directory command line option is not specified.

Diff Detail

Event Timeline

asavonic created this revision.Mar 9 2022, 8:30 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 9 2022, 8:30 AM
asavonic requested review of this revision.Mar 9 2022, 8:30 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 9 2022, 8:30 AM
tra added a comment.Mar 9 2022, 10:30 AM

Can you please update the diff with more context as described in LLVM docs: https://llvm.org/docs/Phabricator.html#requesting-a-review-via-the-web-interface

Can you list the clang and ptxas commands to be clearer? Also, should the change be in clang/lib/Driver? There are existing settings for CUDA.
It's also worth adding comments like "As of <date>/<version>, ptxas does not support ..." so that others can revisit the opt-out in the future.

asavonic updated this revision to Diff 414167.Mar 9 2022, 11:23 AM

Re-upload the patch with -U99999

asavonic edited the summary of this revision. (Show Details)Mar 9 2022, 11:48 AM
tra added a comment.Mar 9 2022, 11:53 AM

Can you list the clang and ptxas commands to be clearer?

I do not think ptxas gets any special command line options. Debug info format affects the generated PTX outout only.

Also, should the change be in clang/lib/Driver? There are existing settings for CUDA.
It's also worth adding comments like "As of <date>/<version>, ptxas does not support ..." so that others can revisit the opt-out in the future.

We already clamp DWARF version for targets that don't support newer ones.
If split directory/file arguments for .file directive are properties of DWARF5 (AFAICT, they are: https://sourceware.org/binutils/docs/as/File.html) , then not generating this format for older versions looks sensible to me. It does not have anything to do with CUDA itself.
The question is whether this is the right place to do it, or would it better to do it wherever UseDwarfDirectory is set.

llvm/lib/MC/MCAsmStreamer.cpp
1557–1559 ↗(On Diff #414167)

Nit. It may be more readable to rephrase it as:

bool DwarfDirIsSupported = getContext().getDwarfVersion() >= 5 || MAI->useIntegratedAssembler();

printDwarfFileDirective(FileNo, Directory, Filename, Checksum, Source,
                          UseDwarfDirectory && DwarfDirIsSupported, OS1);
This comment was removed by MaskRay.

I am a bit unsure whether we should make such logic spread in two places (clang Driver and MCAsmStreamer).
It's nice that llc without other options (-dwarf-directory=0) produced assembly can be passed to the assembler for the target but that's not a requirement.

The question is whether this is the right place to do it, or would it better to do it wherever UseDwarfDirectory is set.

This option is first set in llc.cpp and then propagated to MCAsmStreamer. I think this is the right place for to do it, but we can move this code to the class constructor instead.

I am a bit unsure whether we should make such logic spread in two places (clang Driver and MCAsmStreamer).
It's nice that llc without other options (-dwarf-directory=0) produced assembly can be passed to the assembler for the target but that's not a requirement.

I think it is important for llc to produce valid assembly output by default, especially considering that the error message from ptxas is not obvious.
Although the way we should change this default depends on use cases and expectations for -dwarf-directory. I traced it back to 2011 (commit 40f8f2ff "Add support for a new extension to the .file directive") , where it it was added as an extension and (I assume) later included into DWARFv5. Do we still want to support it as an extension for DWARF < 5, or it is only needed for integrated assembler?

I traced it back to 2011 (commit 40f8f2ff "Add support for a new extension to the .file directive") , where it it was added as an extension and (I assume) later included into DWARFv5. Do we still want to support it as an extension for DWARF < 5, or it is only needed for integrated assembler?

The DWARF line table has had separate directory and file lists going back to DWARF v2; this .file directive syntax is unrelated to DWARF version.
If some external assemblers don't support the split .file syntax, the flags/conditions should reflect that based on target or whatever. Certainly not on DWARF version.

I traced it back to 2011 (commit 40f8f2ff "Add support for a new extension to the .file directive") , where it it was added as an extension and (I assume) later included into DWARFv5. Do we still want to support it as an extension for DWARF < 5, or it is only needed for integrated assembler?

The DWARF line table has had separate directory and file lists going back to DWARF v2; this .file directive syntax is unrelated to DWARF version.
If some external assemblers don't support the split .file syntax, the flags/conditions should reflect that based on target or whatever. Certainly not on DWARF version.

Thanks a lot! So .file fileno directory filename syntax *can* be lowered to DWARF < 5, but some assemblers decide not to support it: GNU as only suppots it for DWARFv5 [1], ptxas does not support it at all. Therefore it is important to keep -dwarf-directory option as a way to force this feature, but we also need a way to override the default value - setting it to true does not make sense for NVPTX.

[1]: https://sourceware.org/binutils/docs/as/File.html

asavonic updated this revision to Diff 417931.Mar 24 2022, 7:43 AM
asavonic edited the summary of this revision. (Show Details)
asavonic added a reviewer: probinson.
  • Added a new field to MCAsmInfo to control default value of UseDwarfDirectory. This value is used if -dwarf-directory command line option is not specified.
tra accepted this revision.Mar 24 2022, 10:44 AM
This revision is now accepted and ready to land.Mar 24 2022, 10:44 AM

I am a bit unsure whether we should make such logic spread in two places (clang Driver and MCAsmStreamer).
It's nice that llc without other options (-dwarf-directory=0) produced assembly can be passed to the assembler for the target but that's not a requirement.

I think it is important for llc to produce valid assembly output by default, especially considering that the error message from ptxas is not obvious.

I just worry whether this patch may be busywork. It seems like a "UI" thing of llc anyway, not really affecting Clang.
Have you tried contacting CUDA folks about supporting the syntax in ptxas?
If ptxas will soon get the support, in the future someone may propose to revert this patch (the enum DwarfDirectory mechanism won't be needed).

Although the way we should change this default depends on use cases and expectations for -dwarf-directory. I traced it back to 2011 (commit 40f8f2ff "Add support for a new extension to the .file directive") , where it it was added as an extension and (I assume) later included into DWARFv5. Do we still want to support it as an extension for DWARF < 5, or it is only needed for integrated assembler?

[This has been answered by Paul]

MaskRay requested changes to this revision.Mar 24 2022, 10:58 AM
MaskRay added inline comments.
llvm/include/llvm/MC/MCTargetOptions.h
61

I am not sure the presumably temporary ptxas issue justifies the complexity here and in MCAsmInfo.h. If you really think so strongly that llc should use non-directory .file for NVPTX, you can temporarily only change llc instead.
But as my comment suggests, it will be unneeded.

This revision now requires changes to proceed.Mar 24 2022, 10:58 AM
tra added a comment.Mar 24 2022, 11:01 AM

I just worry whether this patch may be busywork. It seems like a "UI" thing of llc anyway, not really affecting Clang.
Have you tried contacting CUDA folks about supporting the syntax in ptxas?
If ptxas will soon get the support, in the future someone may propose to revert this patch (the enum DwarfDirectory mechanism won't be needed).

I can ask NVIDIA, but it's not going to help us much.
Even if we can convince NVIDIA to support this syntax, it will only apply to ptxas in some future CUDA version.
We'll still need to deal with ptxas that has been shipped with all existing CUDA versions we support.

I just worry whether this patch may be busywork. It seems like a "UI" thing of llc anyway, not really affecting Clang.
Have you tried contacting CUDA folks about supporting the syntax in ptxas?
If ptxas will soon get the support, in the future someone may propose to revert this patch (the enum DwarfDirectory mechanism won't be needed).

I can ask NVIDIA, but it's not going to help us much.

Thanks

Even if we can convince NVIDIA to support this syntax, it will only apply to ptxas in some future CUDA version.
We'll still need to deal with ptxas that has been shipped with all existing CUDA versions we support.

AIUI the complexity is to make llc -mtriple=nvptx64-nvidia-cuda -dwarf-directory=0 a.ll users convenient, but they can use clang --target=nvptx64-nvidia-cuda -S a.ll instead, to avoid setting -dwarf-directory.
What we need to do is make tradeoff between complexity and convenience. Omitting -dwarf-directory=0 considering that (a) clang doesn't need the manual setting (b) the mechanism will need to be removed in the future
makes me unsure we need to introduce the seemingly heavy infrastructure as implemented in this patch.

If the patch is reworked to avoid EnableDwarfFileDirectoryDefault, I may concede.

I just worry whether this patch may be busywork. It seems like a "UI" thing of llc anyway, not really affecting Clang.

Yes, this is only about llc command line interface for PTX.

AIUI the complexity is to make llc -mtriple=nvptx64-nvidia-cuda -dwarf-directory=0 a.ll users convenient, but they can use clang --target=nvptx64-nvidia-cuda -S a.ll instead, to avoid setting -dwarf-directory.

Well, my motivation is to avoid adding this option to every llc LIT test that requires it (as part of D121727), and there is no way to use clang for them.
Also, I believe is that "by default" llc should output valid assembly for the target. If it doesn't, then it is inconvenient to use at best, or a source of errors for no good reason.

What we need to do is make tradeoff between complexity and convenience. Omitting -dwarf-directory=0 considering that (a) clang doesn't need the manual setting (b) the mechanism will need to be removed in the future makes me unsure we need to introduce the seemingly heavy infrastructure as implemented in this patch.

Clang driver is a separate subject. Many tests do not use clang driver, developers may not use it, and there are other frontends that all need to pass this option.

If the patch is reworked to avoid EnableDwarfFileDirectoryDefault, I may concede.

I see a couple of options:

  • revert this default back to false.
  • add a check for Triple = NVPTX directly to llc.
tra added a comment.Mar 24 2022, 11:54 AM

AIUI the complexity is to make llc -mtriple=nvptx64-nvidia-cuda -dwarf-directory=0 a.ll users convenient, but they can use clang --target=nvptx64-nvidia-cuda -S a.ll instead, to avoid setting -dwarf-directory.

What we need to do is make tradeoff between complexity and convenience. Omitting -dwarf-directory=0 considering that (a) clang doesn't need the manual setting (b) the mechanism will need to be removed in the future
makes me unsure we need to introduce the seemingly heavy infrastructure as implemented in this patch.

To me it looks like yet another knob to accommodate a target-specific quirk. We already have to deal with various oddities of PTX, including limits on supported DWARF version and various oddities of PTX syntax. This particular knob appears relatively noninvasive to me.

I would argue that it's worth making things 'just work'. We can not expect all users to remember that if they want to compile for target X with debug info, then they need to specify additional options.

asavonic updated this revision to Diff 418011.Mar 24 2022, 12:10 PM
  • Fixed Clang build.
Herald added a project: Restricted Project. · View Herald TranscriptMar 24 2022, 12:10 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript

@MaskRay, should we merge this patch or rework it?

I'm going to merge the patch in a couple of days, unless anyone has a strong opinion against it.
Alternative options:

  • revert this default back to false (not great for other targets)
  • add a check for Triple == NVPTX directly to llc (tools other than llc will have to replicate the same logic)

I'm okay with doing it this way. My impression over the years (as a debug-info guy, not as a ptxas user) is that ptxas evolves slowly and this is not really "busy work."

This revision was not accepted when it landed; it landed in state Needs Review.Apr 26 2022, 11:43 AM
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.