Skip to content

Commit e2632fb

Browse files
committed
[NVPTX] Use MBB.begin() instead MBB.front() in NVPTXFrameLowering::emitPrologue
The second argument of `NVPTXFrameLowering::emitPrologue(MachineFunction &MF, MachineBasicBlock &MBB)` is the first MBB of the MF. In that function, it assumes the first MBB always contains instructions, so it gets the first instruction by MachineInstr *MI = &MBB.front();. However, with the reproducer/test case attached, all instructions in the first MBB is cleared in a previous pass for stack coloring. As a consequence, MBB.front() triggers the assertion that the first node is actually a sentinel node. Hence we are using MachineBasicBlock::iterator to iterate over MBB. Fix #52623. Differential Revision: https://reviews.llvm.org/D132663
1 parent 1b56b2b commit e2632fb

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

llvm/lib/Target/NVPTX/NVPTXFrameLowering.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ void NVPTXFrameLowering::emitPrologue(MachineFunction &MF,
3333
MachineBasicBlock &MBB) const {
3434
if (MF.getFrameInfo().hasStackObjects()) {
3535
assert(&MF.front() == &MBB && "Shrink-wrapping not yet supported");
36-
MachineInstr *MI = &MBB.front();
36+
MachineBasicBlock::iterator MBBI = MBB.begin();
3737
MachineRegisterInfo &MR = MF.getRegInfo();
3838

3939
const NVPTXRegisterInfo *NRI =
@@ -55,12 +55,13 @@ void NVPTXFrameLowering::emitPrologue(MachineFunction &MF,
5555
(Is64Bit ? NVPTX::MOV_DEPOT_ADDR_64 : NVPTX::MOV_DEPOT_ADDR);
5656
if (!MR.use_empty(NRI->getFrameRegister(MF))) {
5757
// If %SP is not used, do not bother emitting "cvta.local %SP, %SPL".
58-
MI = BuildMI(MBB, MI, dl,
59-
MF.getSubtarget().getInstrInfo()->get(CvtaLocalOpcode),
60-
NRI->getFrameRegister(MF))
61-
.addReg(NRI->getFrameLocalRegister(MF));
58+
MBBI = BuildMI(MBB, MBBI, dl,
59+
MF.getSubtarget().getInstrInfo()->get(CvtaLocalOpcode),
60+
NRI->getFrameRegister(MF))
61+
.addReg(NRI->getFrameLocalRegister(MF));
6262
}
63-
BuildMI(MBB, MI, dl, MF.getSubtarget().getInstrInfo()->get(MovDepotOpcode),
63+
BuildMI(MBB, MBBI, dl,
64+
MF.getSubtarget().getInstrInfo()->get(MovDepotOpcode),
6465
NRI->getFrameLocalRegister(MF))
6566
.addImm(MF.getFunctionNumber());
6667
}

llvm/test/CodeGen/NVPTX/bug52623.ll

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
; RUN: llc < %s -march=nvptx -mcpu=sm_75 -verify-machineinstrs
2+
; RUN: %if ptxas %{ llc < %s -march=nvptx -mcpu=sm_75 | %ptxas-verify %}
3+
4+
; Check that llc will not crash even when first MBB doesn't contain
5+
; any instruction.
6+
7+
target datalayout = "e-i64:64-i128:128-v16:16-v32:32-n16:32:64"
8+
target triple = "nvptx64"
9+
10+
%printf_args.0.8 = type { i8* }
11+
12+
define internal i32 @__kmpc_get_hardware_thread_id_in_block(i1 %0) {
13+
%2 = alloca %printf_args.0.8, i32 0, align 8
14+
%3 = bitcast %printf_args.0.8* %2 to i8*
15+
br i1 true, label %._crit_edge1, label %._crit_edge
16+
17+
._crit_edge: ; preds = %1, %._crit_edge
18+
%4 = call i32 null(i8* null, i8* %3)
19+
br i1 %0, label %._crit_edge, label %._crit_edge1
20+
21+
._crit_edge1: ; preds = %._crit_edge, %1
22+
ret i32 0
23+
24+
; uselistorder directives
25+
uselistorder label %._crit_edge, { 1, 0 }
26+
}

0 commit comments

Comments
 (0)