[PATCH] D142975: [AsmPrinter] Allow .cfi_restore_state to be put at the end of a function

Sinan Lin via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 31 05:36:34 PST 2023


sinan created this revision.
sinan added reviewers: aprantl, chill, MaskRay.
Herald added subscribers: hiraditya, kristof.beyls.
Herald added a project: All.
sinan requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D142975

Files:
  llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp


Index: llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
===================================================================
--- llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
+++ llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
@@ -1290,13 +1290,16 @@
   auto I = std::next(MI.getIterator());
   while (I != MBB->end() && I->isTransient())
     ++I;
-  if (I == MBB->instr_end() &&
-      MBB->getReverseIterator() == MBB->getParent()->rbegin())
-    return;
 
   const std::vector<MCCFIInstruction> &Instrs = MF->getFrameInstructions();
   unsigned CFIIndex = MI.getOperand(0).getCFIIndex();
   const MCCFIInstruction &CFI = Instrs[CFIIndex];
+
+  if (I == MBB->instr_end() &&
+      MBB->getReverseIterator() == MBB->getParent()->rbegin() &&
+      CFI.getOperation() != MCCFIInstruction::OpRestoreState)
+    return;
+
   emitCFIInstruction(CFI);
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D142975.493569.patch
Type: text/x-patch
Size: 839 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230131/3c4a3e8a/attachment.bin>


More information about the llvm-commits mailing list