This is an archive of the discontinued LLVM Phabricator instance.

[MC] Emit Stackmaps before debug info
ClosedPublic

Authored by zero9178 on Aug 25 2022, 3:29 PM.

Details

Summary

This patch is essentially an alternative to https://reviews.llvm.org/D75836 and was mentioned by @lhames in a comment.

The gist of the issue is that Mach-O has restrictions on which kind of sections are allowed after debug info has been emitted, which is also properly asserted within LLVM. Problem is that stack maps are currently emitted as one of the last sections in each target-specific AsmPrinter so far, which would cause the assertion to trigger. The current approach of special casing for the __LLVM_STACKMAPS section is not viable either, as downstream users can overwrite the stackmap format using plugins, which may want to use different sections.

This patch fixes the issue by emitting the stack map earlier, right before debug info is emitted. The way this is implemented is by taking the choice when to emit the StackMap away from the target AsmPrinter and doing so in the base class. The only disadvantage of this approach is that the StackMaps member is now part of the base class, even for targets that do not support them. This is functionaly not a problem however, as emitting an empty StackMaps is a no-op.

Diff Detail

Event Timeline

zero9178 created this revision.Aug 25 2022, 3:29 PM
Herald added a project: Restricted Project. · View Herald TranscriptAug 25 2022, 3:29 PM
zero9178 requested review of this revision.Aug 25 2022, 3:29 PM
Herald added a project: Restricted Project. · View Herald TranscriptAug 25 2022, 3:29 PM
zero9178 updated this revision to Diff 455745.Aug 25 2022, 3:49 PM

Fix diff creation

zero9178 updated this revision to Diff 455757.Aug 25 2022, 4:40 PM

Don't forget about PowerPC and SystemZ. Boop CI

zero9178 set the repository for this revision to rG LLVM Github Monorepo.

Friendly ping

dantrushin accepted this revision.Sep 6 2022, 8:35 AM

I did a quick testing downstream (on X86) and did not encounter any issues. Looks good to me.

This revision is now accepted and ready to land.Sep 6 2022, 8:35 AM
This revision was landed with ongoing or failed builds.Sep 6 2022, 11:21 AM
This revision was automatically updated to reflect the committed changes.
lhames added a comment.Sep 6 2022, 4:00 PM

The current approach of special casing for the __LLVM_STACKMAPS section is not viable either, as downstream users can overwrite the stackmap format using plugins, which may want to use different sections.

@zero9178 Could you elaborate on this? Was __LLVM_STACKMAPS handling being special cased in assertions in LLVM, or in tools that consumed MachO?

What do you mean by "downstream users can overwrite the stackmap format using plugins"? When do those plugins run, and how would they interfere with the special casing?

I don't mind this approach, but am curious about the design constraints on stack maps and how they're being used at the moment.

The current approach of special casing for the __LLVM_STACKMAPS section is not viable either, as downstream users can overwrite the stackmap format using plugins, which may want to use different sections.

@zero9178 Could you elaborate on this? Was __LLVM_STACKMAPS handling being special cased in assertions in LLVM, or in tools that consumed MachO?

What do you mean by "downstream users can overwrite the stackmap format using plugins"? When do those plugins run, and how would they interfere with the special casing?

I don't mind this approach, but am curious about the design constraints on stack maps and how they're being used at the moment.

With the special case I was simply referring to the assertion.
These plugins I was referring to are the GCMetaDataPrinter defined here https://github.com/llvm/llvm-project/blob/f049b2c3fcbae739ac965f97fd6855ca1aab77b8/llvm/include/llvm/CodeGen/GCMetadataPrinter.h
The emitStackmaps function that was moved here calls any implementations in the registry to write out the stackmap [0]. The default stackmap is only used if a GCMetaDataPrinter does not emit a stackmap.
Since users of the GCMetaDataPrinter can use the AsmPrinter arbitrarly and can choose not to use the __LLVMSTACKMAP section it would then lead to the assertion being triggered.

[0] https://github.com/llvm/llvm-project/blob/f049b2c3fcbae739ac965f97fd6855ca1aab77b8/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp#L3765

@zero9178 Thanks very much for the explanation! I had no idea we had a customization point for those.