Skip to content

Commit 9aead99

Browse files
committed
Catch some cases where the call frame size stored in MachineBasicBlocks was not set or updated.
1 parent 60543c0 commit 9aead99

File tree

5 files changed

+60
-3
lines changed

5 files changed

+60
-3
lines changed

llvm/include/llvm/CodeGen/TargetInstrInfo.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2269,6 +2269,12 @@ class TargetInstrInfo : public MCInstrInfo {
22692269
/// a call sequence. Zero-sized call frames are possible.
22702270
std::optional<unsigned> getCallFrameSizeAt(MachineInstr &MI) const;
22712271

2272+
/// Get the call frame size just before MII. Contains no value if MII is not
2273+
/// in a call sequence. Zero-sized call frames are possible.
2274+
std::optional<unsigned>
2275+
getCallFrameSizeAt(MachineBasicBlock &MBB,
2276+
MachineBasicBlock::iterator MII) const;
2277+
22722278
/// Fills in the necessary MachineOperands to refer to a frame index.
22732279
/// The best way to understand this is to print `asm(""::"m"(x));` after
22742280
/// finalize-isel. Example:

llvm/lib/CodeGen/TargetInstrInfo.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "llvm/CodeGen/TargetInstrInfo.h"
1414
#include "llvm/ADT/StringExtras.h"
1515
#include "llvm/BinaryFormat/Dwarf.h"
16+
#include "llvm/CodeGen/MachineBasicBlock.h"
1617
#include "llvm/CodeGen/MachineCombinerPattern.h"
1718
#include "llvm/CodeGen/MachineFrameInfo.h"
1819
#include "llvm/CodeGen/MachineInstrBuilder.h"
@@ -1626,9 +1627,14 @@ TargetInstrInfo::describeLoadedValue(const MachineInstr &MI,
16261627

16271628
std::optional<unsigned>
16281629
TargetInstrInfo::getCallFrameSizeAt(MachineInstr &MI) const {
1630+
return this->getCallFrameSizeAt(*MI.getParent(), MI.getIterator());
1631+
}
1632+
1633+
std::optional<unsigned>
1634+
TargetInstrInfo::getCallFrameSizeAt(MachineBasicBlock &MBB,
1635+
MachineBasicBlock::iterator MII) const {
16291636
// Search backwards from MI for the most recent call frame instruction.
1630-
MachineBasicBlock *MBB = MI.getParent();
1631-
for (auto &AdjI : reverse(make_range(MBB->instr_begin(), MI.getIterator()))) {
1637+
for (auto &AdjI : reverse(make_range(MBB.begin(), MII))) {
16321638
if (AdjI.getOpcode() == getCallFrameSetupOpcode())
16331639
return getFrameTotalSize(AdjI);
16341640
if (AdjI.getOpcode() == getCallFrameDestroyOpcode())
@@ -1637,7 +1643,7 @@ TargetInstrInfo::getCallFrameSizeAt(MachineInstr &MI) const {
16371643

16381644
// If none was found, use the call frame size from the start of the basic
16391645
// block.
1640-
return MBB->getCallFrameSize();
1646+
return MBB.getCallFrameSize();
16411647
}
16421648

16431649
/// Both DefMI and UseMI must be valid. By default, call directly to the

llvm/lib/Target/AMDGPU/AMDGPURegisterBankInfo.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -816,6 +816,15 @@ bool AMDGPURegisterBankInfo::executeInWaterfallLoop(
816816
MachineBasicBlock *BodyBB = MF->CreateMachineBasicBlock();
817817
MachineBasicBlock *RemainderBB = MF->CreateMachineBasicBlock();
818818
MachineBasicBlock *RestoreExecBB = MF->CreateMachineBasicBlock();
819+
820+
// Set call-frame sizes in each new BB, in case we are in a call sequence.
821+
std::optional<unsigned> CallFrameSizeBefore =
822+
TII->getCallFrameSizeAt(MBB, Range.begin());
823+
LoopBB->setCallFrameSize(CallFrameSizeBefore);
824+
BodyBB->setCallFrameSize(CallFrameSizeBefore);
825+
RemainderBB->setCallFrameSize(CallFrameSizeBefore);
826+
RestoreExecBB->setCallFrameSize(CallFrameSizeBefore);
827+
819828
MachineFunction::iterator MBBI(MBB);
820829
++MBBI;
821830
MF->insert(MBBI, LoopBB);

llvm/lib/Target/X86/X86FrameLowering.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -790,6 +790,12 @@ void X86FrameLowering::emitStackProbeInlineGenericLoop(
790790
MachineBasicBlock *testMBB = MF.CreateMachineBasicBlock(LLVM_BB);
791791
MachineBasicBlock *tailMBB = MF.CreateMachineBasicBlock(LLVM_BB);
792792

793+
// Set call-frame sizes in each new BB, in case we are in a call sequence.
794+
std::optional<unsigned> CallFrameSizeBefore =
795+
TII.getCallFrameSizeAt(MBB, MBBI);
796+
testMBB->setCallFrameSize(CallFrameSizeBefore);
797+
tailMBB->setCallFrameSize(CallFrameSizeBefore);
798+
793799
MachineFunction::iterator MBBIter = ++MBB.getIterator();
794800
MF.insert(MBBIter, testMBB);
795801
MF.insert(MBBIter, tailMBB);
@@ -932,6 +938,13 @@ void X86FrameLowering::emitStackProbeInlineWindowsCoreCLR64(
932938
MachineBasicBlock *LoopMBB = MF.CreateMachineBasicBlock(LLVM_BB);
933939
MachineBasicBlock *ContinueMBB = MF.CreateMachineBasicBlock(LLVM_BB);
934940

941+
// Set call-frame sizes in each new BB, in case we are in a call sequence.
942+
std::optional<unsigned> CallFrameSizeBefore =
943+
TII.getCallFrameSizeAt(MBB, MBBI);
944+
RoundMBB->setCallFrameSize(CallFrameSizeBefore);
945+
LoopMBB->setCallFrameSize(CallFrameSizeBefore);
946+
ContinueMBB->setCallFrameSize(CallFrameSizeBefore);
947+
935948
MachineFunction::iterator MBBIter = std::next(MBB.getIterator());
936949
MF.insert(MBBIter, RoundMBB);
937950
MF.insert(MBBIter, LoopMBB);
@@ -1286,6 +1299,19 @@ void X86FrameLowering::BuildStackAlignAND(MachineBasicBlock &MBB,
12861299
: Is64Bit ? X86::R11D
12871300
: X86::EAX;
12881301

1302+
std::optional<unsigned> CallFrameSizeBefore =
1303+
TII.getCallFrameSizeAt(MBB, MBBI);
1304+
// entryMBB takes the place of MBB, so give it its stored call frame size.
1305+
entryMBB->setCallFrameSize(MBB.getCallFrameSize());
1306+
// MBB contains only a tail of its original instructions, so update its
1307+
// stored call frame size.
1308+
MBB.setCallFrameSize(CallFrameSizeBefore);
1309+
// For the other basic blocks, take the state at the insertion point as
1310+
// well.
1311+
headMBB->setCallFrameSize(CallFrameSizeBefore);
1312+
bodyMBB->setCallFrameSize(CallFrameSizeBefore);
1313+
footMBB->setCallFrameSize(CallFrameSizeBefore);
1314+
12891315
// Setup entry block
12901316
{
12911317

llvm/lib/Target/X86/X86ISelLowering.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35337,6 +35337,16 @@ X86TargetLowering::EmitLoweredProbedAlloca(MachineInstr &MI,
3533735337
MachineBasicBlock *tailMBB = MF->CreateMachineBasicBlock(LLVM_BB);
3533835338
MachineBasicBlock *blockMBB = MF->CreateMachineBasicBlock(LLVM_BB);
3533935339

35340+
// Set call-frame sizes in each new BB, in case we are in a call sequence.
35341+
// MBB is split at std::next(MachineBasicBlock::iterator(MI) and these blocks
35342+
// are inserted in between, so they should all have the call frame size at
35343+
// the split point.
35344+
std::optional<unsigned> CallFrameSizeAtSplit =
35345+
TII->getCallFrameSizeAt(*MBB, std::next(MachineBasicBlock::iterator(MI)));
35346+
testMBB->setCallFrameSize(CallFrameSizeAtSplit);
35347+
blockMBB->setCallFrameSize(CallFrameSizeAtSplit);
35348+
tailMBB->setCallFrameSize(CallFrameSizeAtSplit);
35349+
3534035350
MachineFunction::iterator MBBIter = ++MBB->getIterator();
3534135351
MF->insert(MBBIter, testMBB);
3534235352
MF->insert(MBBIter, blockMBB);

0 commit comments

Comments
 (0)