This is an archive of the discontinued LLVM Phabricator instance.

Implement -frecord-command-line for XCOFF
ClosedPublic

Authored by Jake-Egan on Jun 22 2023, 5:46 PM.

Details

Summary

This patch extends support of the option -frecord-command-line to XCOFF. XCOFF doesn’t have custom sections like ELF, so the command line data is emitted to a .info section instead. A C_INFO symbol is generated with the .info section to preserve the command line data past the link step. Multiple command lines are separated by newlines and null bytes. The command line data can be retrieved on AIX with command what file_name.

Diff Detail

Event Timeline

Jake-Egan created this revision.Jun 22 2023, 5:46 PM
Herald added a project: Restricted Project. · View Herald TranscriptJun 22 2023, 5:46 PM
Jake-Egan requested review of this revision.Jun 22 2023, 5:46 PM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptJun 22 2023, 5:46 PM
Jake-Egan edited the summary of this revision. (Show Details)Jun 22 2023, 5:48 PM
Jake-Egan edited the summary of this revision. (Show Details)Jun 22 2023, 5:58 PM

Sorry for the delay in review, I am not too familiar with XCOFF so I was hoping someone else would take a look first.

If my understanding of the COFF docs I could find is correct then this LGTM save for some nits/suggestions

llvm/lib/MC/MCAsmStreamer.cpp
971–1038

I sketched out some of the suggestions I had, although the bigger changes are just because the extra .info for the padded byte bugged me. If you aren't concerned with it I'm also happy with what you have

981

I couldn't quickly find a reference for the alignment requirement, but it seems like there is an additional requirement that the length must also be non-zero, not just even?

If so, can you update the comment?

I would also rather explicitly use alignTo and max to express this (see suggestion), but if we don't expect the optimizer to clean it up I'm fine with the more terse version.

981

Can you factor this out at function scope? It gets repeated below

985

Redundant cast

989

It seems odd to use a lambda when this is only used once. Why not just compute the last word directly?

1034

Can you factor this out as Separator at function scope?

llvm/test/CodeGen/PowerPC/aix-command-line-metadata.ll
22

Having the padded byte force a new .info directive doesn't seem ideal. I suggested something to avoid it, but I suppose it also doesn't really harm anything.

Jake-Egan updated this revision to Diff 536795.Jul 3 2023, 9:26 AM

Thanks for the review @scott.linder. I applied the changes you requested with some differences.

Jake-Egan marked 6 inline comments as done.Jul 3 2023, 9:32 AM
Jake-Egan added inline comments.
llvm/lib/MC/MCAsmStreamer.cpp
981
1022

I changed the assert that you requested because it would always fail:

assert(Length - Index == MetadataPaddingSize);
Jake-Egan marked an inline comment as done.Jul 3 2023, 9:33 AM
Jake-Egan added inline comments.
llvm/lib/MC/MCAsmStreamer.cpp
986

Handled the 0 length case here.

Jake-Egan marked an inline comment as done.Jul 3 2023, 9:34 AM
Jake-Egan updated this revision to Diff 536890.Jul 3 2023, 2:04 PM

Fix formatting

stephenpeckham added inline comments.Jul 3 2023, 3:37 PM
llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
2338–2340

I would add a comment explaining that for XCOFF, the command line metadata was emitted earlier.

llvm/lib/MC/MCAsmStreamer.cpp
981

There's no requirement to pad the .info section. When you generate assembly language, the .info pseudo-op can only generate words of data, so the if the data length is not 0(mod 4), the last word will have to be padded with low-order 0s. This means that the length of the .info section will be a multiple of 4. The length, on the other hand, should be exact. At link time, only "length" bytes will be copied to the output file.

If you emit object code directly, you will not need to emit any padding bytes.

987

Can this be moved up right after the computation of MetadataSize? Can't you just emit "0,"? Why call format_hex()? It seems odd to have a literal comma here instead of using Separator.

994

You should use MetadataSize and Separator here. In fact, you could emit the length before checking for 0, expecially because a length of 0 is a rare case (and impossible for this patch).

llvm/lib/Target/PowerPC/PPCAsmPrinter.cpp
2974

I would use a newline here. The AIX what command looks for @(#) and echos subsequent bytes until it sees a double quote, a backslash, a > symbol, newline, or null byte. The @(#) is not echoed, nor is the terminating character. The what command prints a newline after it finds a terminating character. This means that if the command line contains any of the special characters, the line will be truncated.

Exception: If the @(#) is followed by "opt " or " opt ", the terminating characters are only a newline or null byte. This allows any of the other special characters to be part of the command line. It doesn't really matter if you use a newline or a null byte, but the legacy XL compiler uses a newline. The "opt" keyword should appear if the command line can contain a double quote, a > or a backslash.

The legacy compiler also uses other keywords besides "opt", including "version" and "cfg". The what command doesn't do anything special with these keywords.

llvm/test/CodeGen/PowerPC/aix-command-line-metadata.ll
17

0x0000002e: The actual length should be emitted.

Jake-Egan updated this revision to Diff 536920.Jul 3 2023, 6:47 PM
Jake-Egan edited the summary of this revision. (Show Details)

Thanks for the review @stephenpeckham, I updated the patch with the requested changes.

Jake-Egan marked 4 inline comments as done.Jul 3 2023, 6:48 PM
llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
2338–2340

Minor nit: Typo.

llvm/lib/Target/PowerPC/PPCAsmPrinter.cpp
2974

As mentioned offline, newline on its own has potential of ambiguity because it can appear in command line options (null bytes cannot). If there is a preference for newline to be present, then having a null byte after could help.

Note that @(#)opt can appear on the command line too. Using what will have limitations (but we should leave the possibility open for other tools/methods to work).

Jake-Egan updated this revision to Diff 537156.Jul 4 2023, 12:31 PM
Jake-Egan edited the summary of this revision. (Show Details)

Updated to use both a newline and null byte to separate command lines.

Jake-Egan marked 3 inline comments as done.Jul 4 2023, 12:31 PM
Jake-Egan updated this revision to Diff 537160.Jul 4 2023, 12:50 PM

Fix formatting

llvm/lib/Target/PowerPC/PPCAsmPrinter.cpp
2974

Thanks; I confirm that my comment has been addressed. I have no further comments at this time. Feel free to commit if another reviewer approves the patch.

MaskRay added inline comments.Jul 5 2023, 3:38 PM
llvm/lib/MC/MCAsmStreamer.cpp
972
scott.linder added inline comments.Jul 5 2023, 3:41 PM
llvm/lib/MC/MCAsmStreamer.cpp
981

Does the comment still need updating then? It could capture the fact that the "lowest common denominator" is the assembly syntax as it works in terms of words, and so may requiring padding in the final word. We can be clear that we apply the same restriction to the object case judiciously, as it make the output identical and avoids more code paths. We could also note that the linker can use the length to optimize the final linked binary.

These all clear things up to a fresh reader, so I think they are worthwhile things to include in comments.

993

With the MetadataSize == 0 case handled the std::max should be redundant now, right?

Also the literal 4 can be WordSize

1018

Nit: for pointer casts I would prefer an explicit reinterpret_cast

Jake-Egan updated this revision to Diff 537572.Jul 5 2023, 8:09 PM

Address comments

Jake-Egan marked 3 inline comments as done.Jul 5 2023, 8:10 PM
Jake-Egan added inline comments.
llvm/lib/MC/MCAsmStreamer.cpp
981

I updated the comment. Please let me know if it's sufficient

scott.linder accepted this revision.Jul 6 2023, 11:53 AM

Thank you for the patch and the changes! This LGTM, and AFAICT all of the comments have been addressed.

This revision is now accepted and ready to land.Jul 6 2023, 11:53 AM
This revision was landed with ongoing or failed builds.Jul 10 2023, 9:47 AM
This revision was automatically updated to reflect the committed changes.