This is an archive of the discontinued LLVM Phabricator instance.

[AsmPrinter] Allow .cfi_restore_state to be put at the end of a function
Needs ReviewPublic

Authored by sinan on Jan 31 2023, 5:36 AM.

Details

Summary

If cfi_restore_state is inserted into an empty block placed at the end of the function, this directive would be ignored when generating asm (because of this patch https://reviews.llvm.org/D32246, but cfi_restore_state looks fine, since it does not describe any target address), resulting in a cfi_remember_state/restore_ state mismatch and then crashes in applications like bolt(assertion in BinaryBasicBlock::getCFIStateAtInstr). This patch will allow .cfi_restore_state to be put at the end of the function.

reproduce(mismatch.ll at https://reviews.llvm.org/F26307823):
llc -mtriple=aarch64 mismatch.ll -o mismatch.s
then you can find there are four cfi_remember_state but only three cfi_restore_state.

So, the whole story for this test case is that CFIFixup inserts directives

  1. insert .cfi_remember_state at bb.0, and insert .cfi_restore_state at bb.4
  2. insert .cfi_remember_state at bb.4, and insert .cfi_restore_state at bb.6
  3. insert .cfi_remember_state at bb.6, and insert .cfi_restore_state at bb.7
  4. insert .cfi_remember_state at bb.7, and insert .cfi_restore_state at bb.8

and, .cfi_restore_state at bb.8 is ignored when emitting asm and then the mismatch happens.

Diff Detail

Event Timeline

sinan created this revision.Jan 31 2023, 5:36 AM
Herald added a project: Restricted Project. · View Herald TranscriptJan 31 2023, 5:36 AM
sinan requested review of this revision.Jan 31 2023, 5:36 AM
Herald added a project: Restricted Project. · View Herald TranscriptJan 31 2023, 5:36 AM
sinan added a comment.Jan 31 2023, 5:37 AM

Another way to avoid this mismatch is to set such blocks as 'Reachable' in CFIFixup, but it looks a bit different from the 'Unreachable block' case in CFIFixup to me and I think we still need a restore_state in the empty block. Any idea?

sinan added a comment.Feb 15 2023, 6:41 PM

gentle ping ...