Fix interaction of CFI instructions with MachineOutliner.

1. When checking if a candidate contains a CFI instruction, actually
iterate over all of the instructions, instead of stopping halfway
through.
2. Make sure copied CFI directives refer to the correct instruction.

Fixes https://github.com/llvm/llvm-project/issues/55842

Differential Revision: https://reviews.llvm.org/D126930
This commit is contained in:
Eli Friedman 2022-06-10 13:37:49 -07:00
parent fbaa8b9ae5
commit 0ff51d5dde
5 changed files with 85 additions and 39 deletions

View File

@ -665,18 +665,21 @@ MachineFunction *MachineOutliner::createOutlinedFunction(
++I) {
if (I->isDebugInstr())
continue;
MachineInstr *NewMI = MF.CloneMachineInstr(&*I);
if (I->isCFIInstruction()) {
unsigned CFIIndex = NewMI->getOperand(0).getCFIIndex();
MCCFIInstruction CFI = Instrs[CFIIndex];
(void)MF.addFrameInst(CFI);
}
NewMI->dropMemRefs(MF);
// Don't keep debug information for outlined instructions.
NewMI->setDebugLoc(DebugLoc());
auto DL = DebugLoc();
if (I->isCFIInstruction()) {
unsigned CFIIndex = I->getOperand(0).getCFIIndex();
MCCFIInstruction CFI = Instrs[CFIIndex];
BuildMI(MBB, MBB.end(), DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
.addCFIIndex(MF.addFrameInst(CFI));
} else {
MachineInstr *NewMI = MF.CloneMachineInstr(&*I);
NewMI->dropMemRefs(MF);
NewMI->setDebugLoc(DL);
MBB.insert(MBB.end(), NewMI);
}
}
// Set normal properties for a late MachineFunction.
MF.getProperties().reset(MachineFunctionProperties::Property::IsSSA);

View File

@ -64,6 +64,7 @@
#include "llvm/InitializePasses.h"
#include "llvm/MC/LaneBitmask.h"
#include "llvm/MC/MCAsmInfo.h"
#include "llvm/MC/MCDwarf.h"
#include "llvm/MC/MCInstrDesc.h"
#include "llvm/MC/MCRegisterInfo.h"
#include "llvm/MC/MCTargetOptions.h"
@ -2212,6 +2213,11 @@ MachineVerifier::visitMachineOperand(const MachineOperand *MO, unsigned MONum) {
}
break;
case MachineOperand::MO_CFIIndex:
if (MO->getCFIIndex() >= MF->getFrameInstructions().size())
report("CFI instruction has invalid index", MO, MONum);
break;
default:
break;
}

View File

@ -7012,12 +7012,10 @@ outliner::OutlinedFunction AArch64InstrInfo::getOutliningCandidateInfo(
// We check to see if CFI Instructions are present, and if they are
// we find the number of CFI Instructions in the candidates.
unsigned CFICount = 0;
MachineBasicBlock::iterator MBBI = RepeatedSequenceLocs[0].front();
for (unsigned Loc = RepeatedSequenceLocs[0].getStartIdx();
Loc < RepeatedSequenceLocs[0].getEndIdx() + 1; Loc++) {
if (MBBI->isCFIInstruction())
for (auto &I : make_range(RepeatedSequenceLocs[0].front(),
std::next(RepeatedSequenceLocs[0].back()))) {
if (I.isCFIInstruction())
CFICount++;
MBBI++;
}
// We compare the number of found CFI Instructions to the number of CFI

View File

@ -9487,12 +9487,10 @@ outliner::OutlinedFunction X86InstrInfo::getOutliningCandidateInfo(
// We check to see if CFI Instructions are present, and if they are
// we find the number of CFI Instructions in the candidates.
unsigned CFICount = 0;
MachineBasicBlock::iterator MBBI = RepeatedSequenceLocs[0].front();
for (unsigned Loc = RepeatedSequenceLocs[0].getStartIdx();
Loc < RepeatedSequenceLocs[0].getEndIdx() + 1; Loc++) {
if (MBBI->isCFIInstruction())
for (auto &I : make_range(RepeatedSequenceLocs[0].front(),
std::next(RepeatedSequenceLocs[0].back()))) {
if (I.isCFIInstruction())
CFICount++;
MBBI++;
}
// We compare the number of found CFI Instructions to the number of CFI

View File

@ -1,15 +1,17 @@
# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py
# RUN: llc -mtriple=aarch64-apple-unknown -run-pass=machine-outliner -verify-machineinstrs %s -o - | FileCheck %s
# RUN: llc -mtriple=aarch64-apple-unknown -run-pass=machine-outliner -verify-machineinstrs -enable-machine-outliner=always %s -o - | FileCheck %s
# Outlining CFI instructions is unsafe if it is not tail called, but otherwise,
# it requires fixups. Show that we don't include CFI instructions in non
# tail call outlined sequences right now.
# tail call outlined sequences right now. Show that we count CFI instructions
# correctly in the presence of debug info.
--- |
define void @foo() #0 { ret void }
define void @bar() #0 { ret void }
define void @baz() #0 { ret void }
attributes #0 = { noredzone }
...
---
name: foo
@ -19,15 +21,28 @@ body: |
liveins: $lr
; CHECK-LABEL: name: foo
; CHECK: liveins: $lr
; CHECK: $w9 = ORRWri $wzr, 1
; CHECK: $w10 = ORRWri $wzr, 2
; CHECK: $w11 = ORRWri $wzr, 3
; CHECK: frame-setup CFI_INSTRUCTION def_cfa $w29, 16
; CHECK: $w20 = ORRWri $wzr, 1
; CHECK: TCRETURNdi @OUTLINED_FUNCTION_0, 0, implicit $sp, implicit-def $w12, implicit-def $w13, implicit-def $w14, implicit-def $w15, implicit $wzr, implicit $sp
; CHECK-NEXT: {{ $}}
; CHECK-NEXT: $w9 = ORRWri $wzr, 1
; CHECK-NEXT: $w10 = ORRWri $wzr, 2
; CHECK-NEXT: $w11 = ORRWri $wzr, 3
; CHECK-NEXT: DBG_VALUE $w9, $noreg
; CHECK-NEXT: DBG_VALUE $w9, $noreg
; CHECK-NEXT: DBG_VALUE $w9, $noreg
; CHECK-NEXT: DBG_VALUE $w9, $noreg
; CHECK-NEXT: DBG_VALUE $w9, $noreg
; CHECK-NEXT: DBG_VALUE $w9, $noreg
; CHECK-NEXT: frame-setup CFI_INSTRUCTION def_cfa $w29, 16
; CHECK-NEXT: $w20 = ORRWri $wzr, 1
; CHECK-NEXT: TCRETURNdi @OUTLINED_FUNCTION_0, 0, implicit $sp, implicit-def $w12, implicit-def $w13, implicit-def $w14, implicit-def $w15, implicit $wzr, implicit $sp
$w9 = ORRWri $wzr, 1
$w10 = ORRWri $wzr, 2
$w11 = ORRWri $wzr, 3
DBG_VALUE $w9, $noreg
DBG_VALUE $w9, $noreg
DBG_VALUE $w9, $noreg
DBG_VALUE $w9, $noreg
DBG_VALUE $w9, $noreg
DBG_VALUE $w9, $noreg
frame-setup CFI_INSTRUCTION def_cfa $w29, 16
$w20 = ORRWri $wzr, 1
$w12 = ORRWri $wzr, 1
@ -44,15 +59,28 @@ body: |
liveins: $lr
; CHECK-LABEL: name: bar
; CHECK: liveins: $lr
; CHECK: $w9 = ORRWri $wzr, 1
; CHECK: $w10 = ORRWri $wzr, 2
; CHECK: $w11 = ORRWri $wzr, 3
; CHECK: frame-setup CFI_INSTRUCTION def_cfa $w29, 16
; CHECK: $w21 = ORRWri $wzr, 1
; CHECK: TCRETURNdi @OUTLINED_FUNCTION_0, 0, implicit $sp, implicit-def $w12, implicit-def $w13, implicit-def $w14, implicit-def $w15, implicit $wzr, implicit $sp
; CHECK-NEXT: {{ $}}
; CHECK-NEXT: $w9 = ORRWri $wzr, 1
; CHECK-NEXT: $w10 = ORRWri $wzr, 2
; CHECK-NEXT: $w11 = ORRWri $wzr, 3
; CHECK-NEXT: DBG_VALUE $w9, $noreg
; CHECK-NEXT: DBG_VALUE $w9, $noreg
; CHECK-NEXT: DBG_VALUE $w9, $noreg
; CHECK-NEXT: DBG_VALUE $w9, $noreg
; CHECK-NEXT: DBG_VALUE $w9, $noreg
; CHECK-NEXT: DBG_VALUE $w9, $noreg
; CHECK-NEXT: frame-setup CFI_INSTRUCTION def_cfa $w29, 16
; CHECK-NEXT: $w21 = ORRWri $wzr, 1
; CHECK-NEXT: TCRETURNdi @OUTLINED_FUNCTION_0, 0, implicit $sp, implicit-def $w12, implicit-def $w13, implicit-def $w14, implicit-def $w15, implicit $wzr, implicit $sp
$w9 = ORRWri $wzr, 1
$w10 = ORRWri $wzr, 2
$w11 = ORRWri $wzr, 3
DBG_VALUE $w9, $noreg
DBG_VALUE $w9, $noreg
DBG_VALUE $w9, $noreg
DBG_VALUE $w9, $noreg
DBG_VALUE $w9, $noreg
DBG_VALUE $w9, $noreg
frame-setup CFI_INSTRUCTION def_cfa $w29, 16
$w21 = ORRWri $wzr, 1
$w12 = ORRWri $wzr, 1
@ -69,15 +97,28 @@ body: |
liveins: $lr
; CHECK-LABEL: name: baz
; CHECK: liveins: $lr
; CHECK: $w9 = ORRWri $wzr, 1
; CHECK: $w10 = ORRWri $wzr, 2
; CHECK: $w11 = ORRWri $wzr, 3
; CHECK: frame-setup CFI_INSTRUCTION def_cfa $w29, 16
; CHECK: $w22 = ORRWri $wzr, 1
; CHECK: TCRETURNdi @OUTLINED_FUNCTION_0, 0, implicit $sp, implicit-def $w12, implicit-def $w13, implicit-def $w14, implicit-def $w15, implicit $wzr, implicit $sp
; CHECK-NEXT: {{ $}}
; CHECK-NEXT: $w9 = ORRWri $wzr, 1
; CHECK-NEXT: $w10 = ORRWri $wzr, 2
; CHECK-NEXT: $w11 = ORRWri $wzr, 3
; CHECK-NEXT: DBG_VALUE $w9, $noreg
; CHECK-NEXT: DBG_VALUE $w9, $noreg
; CHECK-NEXT: DBG_VALUE $w9, $noreg
; CHECK-NEXT: DBG_VALUE $w9, $noreg
; CHECK-NEXT: DBG_VALUE $w9, $noreg
; CHECK-NEXT: DBG_VALUE $w9, $noreg
; CHECK-NEXT: frame-setup CFI_INSTRUCTION def_cfa $w29, 16
; CHECK-NEXT: $w22 = ORRWri $wzr, 1
; CHECK-NEXT: TCRETURNdi @OUTLINED_FUNCTION_0, 0, implicit $sp, implicit-def $w12, implicit-def $w13, implicit-def $w14, implicit-def $w15, implicit $wzr, implicit $sp
$w9 = ORRWri $wzr, 1
$w10 = ORRWri $wzr, 2
$w11 = ORRWri $wzr, 3
DBG_VALUE $w9, $noreg
DBG_VALUE $w9, $noreg
DBG_VALUE $w9, $noreg
DBG_VALUE $w9, $noreg
DBG_VALUE $w9, $noreg
DBG_VALUE $w9, $noreg
frame-setup CFI_INSTRUCTION def_cfa $w29, 16
$w22 = ORRWri $wzr, 1
$w12 = ORRWri $wzr, 1