Skip to content

Commit e1004dd

Browse files
committed
Fixed CFI, Comments, More Tests
1 parent 176419d commit e1004dd

File tree

2 files changed

+653
-25
lines changed

2 files changed

+653
-25
lines changed

llvm/lib/Target/RISCV/RISCVFrameLowering.cpp

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1319,19 +1319,22 @@ void RISCVFrameLowering::emitEpilogue(MachineFunction &MF,
13191319
emitCFIForCSI<CFIRestoreRegisterEmitter>(
13201320
MBB, MBBI, getPushOrLibCallsSavedInfo(MF, CSI));
13211321

1322-
// Update CFA offset. After CM_POP SP should be equal to CFA, so CFA
1323-
// offset should be a zero.
1322+
// Update CFA Offset. If this is a QCI interrupt function, there will be a
1323+
// leftover offset which is deallocated by `QC.C.MILEAVERET`, otherwise
1324+
// getQCIInterruptStackSize() will be 0.
13241325
unsigned CFIIndex =
1325-
MF.addFrameInst(MCCFIInstruction::cfiDefCfaOffset(nullptr, 0));
1326+
MF.addFrameInst(MCCFIInstruction::cfiDefCfaOffset(nullptr, RVFI->getQCIInterruptStackSize()));
13261327
BuildMI(MBB, MBBI, DL, TII->get(TargetOpcode::CFI_INSTRUCTION))
13271328
.addCFIIndex(CFIIndex)
13281329
.setMIFlag(MachineInstr::FrameDestroy);
13291330
}
13301331
}
13311332

1332-
// Deallocate stack if StackSize isn't a zero yet
1333+
// Deallocate stack if StackSize isn't a zero yet. If this is a QCI interrupt
1334+
// function, there will be a leftover offset which is deallocated by
1335+
// `QC.C.MILEAVERET`, otherwise getQCIInterruptStackSize() will be 0.
13331336
if (StackSize != 0)
1334-
deallocateStack(MF, MBB, MBBI, DL, StackSize, RealStackSize - StackSize);
1337+
deallocateStack(MF, MBB, MBBI, DL, StackSize, RVFI->getQCIInterruptStackSize());
13351338

13361339
// Emit epilogue for shadow call stack.
13371340
emitSCSEpilogue(MF, MBB, MBBI, DL);
@@ -1898,8 +1901,12 @@ bool RISCVFrameLowering::assignCalleeSavedSpillSlots(
18981901
if (RVFI->isPushable(MF)) {
18991902
// Determine how many GPRs we need to push and save it to RVFI.
19001903
unsigned PushedRegNum = getNumPushPopRegs(CSI);
1901-
unsigned PushMoreThan = RVFI->useQCIInterrupt(MF) ? 2 : 0;
1902-
if (PushedRegNum > PushMoreThan) {
1904+
1905+
// `QC.C.MIENTER(.NEST)` will save `ra` and `s0`, so we should only push if
1906+
// we want to push more than 2 registers. Otherwise, we should push if we
1907+
// want to push more than 0 registers.
1908+
unsigned OnlyPushIfMoreThan = RVFI->useQCIInterrupt(MF) ? 2 : 0;
1909+
if (PushedRegNum > OnlyPushIfMoreThan) {
19031910
RVFI->setRVPushRegs(PushedRegNum);
19041911
RVFI->setRVPushStackSize(alignTo((STI.getXLen() / 8) * PushedRegNum, 16));
19051912
}

0 commit comments

Comments
 (0)