Skip to content

Commit b75c8fc

Browse files
committed
[X86] Fix stack probing on x32 (PR41477)
Fix for https://bugs.llvm.org/show_bug.cgi?id=41477. On the x32 ABI with stack probing a dynamic alloca will result in a WIN_ALLOCA_32 with a 32-bit size. The current implementation tries to copy it into RAX, resulting in a physreg copy error. Fix this by copying to EAX instead. Also fix incorrect opcodes or registers used in subs. llvm-svn: 358807
1 parent ce12ea8 commit b75c8fc

File tree

3 files changed

+69
-8
lines changed

3 files changed

+69
-8
lines changed

llvm/lib/Target/X86/X86FrameLowering.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -794,8 +794,8 @@ void X86FrameLowering::emitStackProbeCall(MachineFunction &MF,
794794
.addExternalSymbol(MF.createExternalSymbolName(Symbol));
795795
}
796796

797-
unsigned AX = Is64Bit ? X86::RAX : X86::EAX;
798-
unsigned SP = Is64Bit ? X86::RSP : X86::ESP;
797+
unsigned AX = Uses64BitFramePtr ? X86::RAX : X86::EAX;
798+
unsigned SP = Uses64BitFramePtr ? X86::RSP : X86::ESP;
799799
CI.addReg(AX, RegState::Implicit)
800800
.addReg(SP, RegState::Implicit)
801801
.addReg(AX, RegState::Define | RegState::Implicit)
@@ -809,7 +809,7 @@ void X86FrameLowering::emitStackProbeCall(MachineFunction &MF,
809809
// adjusting %rsp.
810810
// All other platforms do not specify a particular ABI for the stack probe
811811
// function, so we arbitrarily define it to not adjust %esp/%rsp itself.
812-
BuildMI(MBB, MBBI, DL, TII.get(getSUBrrOpcode(Is64Bit)), SP)
812+
BuildMI(MBB, MBBI, DL, TII.get(getSUBrrOpcode(Uses64BitFramePtr)), SP)
813813
.addReg(SP)
814814
.addReg(AX);
815815
}

llvm/lib/Target/X86/X86WinAllocaExpander.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -209,15 +209,18 @@ void X86WinAllocaExpander::lower(MachineInstr* MI, Lowering L) {
209209
return;
210210
}
211211

212+
// These two variables differ on x32, which is a 64-bit target with a
213+
// 32-bit alloca.
212214
bool Is64Bit = STI->is64Bit();
215+
bool Is64BitAlloca = MI->getOpcode() == X86::WIN_ALLOCA_64;
213216
assert(SlotSize == 4 || SlotSize == 8);
214-
unsigned RegA = (SlotSize == 8) ? X86::RAX : X86::EAX;
215217

216218
switch (L) {
217-
case TouchAndSub:
219+
case TouchAndSub: {
218220
assert(Amount >= SlotSize);
219221

220222
// Use a push to touch the top of the stack.
223+
unsigned RegA = Is64Bit ? X86::RAX : X86::EAX;
221224
BuildMI(*MBB, I, DL, TII->get(Is64Bit ? X86::PUSH64r : X86::PUSH32r))
222225
.addReg(RegA, RegState::Undef);
223226
Amount -= SlotSize;
@@ -226,22 +229,26 @@ void X86WinAllocaExpander::lower(MachineInstr* MI, Lowering L) {
226229

227230
// Fall through to make any remaining adjustment.
228231
LLVM_FALLTHROUGH;
232+
}
229233
case Sub:
230234
assert(Amount > 0);
231235
if (Amount == SlotSize) {
232236
// Use push to save size.
237+
unsigned RegA = Is64Bit ? X86::RAX : X86::EAX;
233238
BuildMI(*MBB, I, DL, TII->get(Is64Bit ? X86::PUSH64r : X86::PUSH32r))
234239
.addReg(RegA, RegState::Undef);
235240
} else {
236241
// Sub.
237-
BuildMI(*MBB, I, DL, TII->get(getSubOpcode(Is64Bit, Amount)), StackPtr)
242+
BuildMI(*MBB, I, DL,
243+
TII->get(getSubOpcode(Is64BitAlloca, Amount)), StackPtr)
238244
.addReg(StackPtr)
239245
.addImm(Amount);
240246
}
241247
break;
242248
case Probe:
243249
if (!NoStackArgProbe) {
244250
// The probe lowering expects the amount in RAX/EAX.
251+
unsigned RegA = Is64BitAlloca ? X86::RAX : X86::EAX;
245252
BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::COPY), RegA)
246253
.addReg(MI->getOperand(0).getReg());
247254

@@ -250,8 +257,8 @@ void X86WinAllocaExpander::lower(MachineInstr* MI, Lowering L) {
250257
/*InPrologue=*/false);
251258
} else {
252259
// Sub
253-
BuildMI(*MBB, I, DL, TII->get(Is64Bit ? X86::SUB64rr : X86::SUB32rr),
254-
StackPtr)
260+
BuildMI(*MBB, I, DL,
261+
TII->get(Is64BitAlloca ? X86::SUB64rr : X86::SUB32rr), StackPtr)
255262
.addReg(StackPtr)
256263
.addReg(MI->getOperand(0).getReg());
257264
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
2+
; RUN: llc < %s -mtriple=x86_64-unknown-linux-gnux32 -verify-machineinstrs | FileCheck %s
3+
4+
target datalayout = "e-m:e-p:32:32-i64:64-f80:128-n8:16:32:64-S128"
5+
target triple = "x86_64-unknown-linux-gnux32"
6+
7+
; probe-stack + dynamic size alloca
8+
define void @test1(i32 %size) nounwind #0 {
9+
; CHECK-LABEL: test1:
10+
; CHECK: # %bb.0: # %start
11+
; CHECK-NEXT: pushq %rbp
12+
; CHECK-NEXT: movl %esp, %ebp
13+
; CHECK-NEXT: # kill: def $edi killed $edi def $rdi
14+
; CHECK-NEXT: leal 15(%rdi), %eax
15+
; CHECK-NEXT: andl $-16, %eax
16+
; CHECK-NEXT: callq __rust_probestack
17+
; CHECK-NEXT: subl %eax, %esp
18+
start:
19+
%alloca = alloca i8, i32 %size
20+
unreachable
21+
}
22+
23+
; probe-stack + no-stack-arg-probe + dynamic size alloca
24+
define void @test2(i32 %size) nounwind #1 {
25+
; CHECK-LABEL: test2:
26+
; CHECK: # %bb.0: # %start
27+
; CHECK-NEXT: pushq %rbp
28+
; CHECK-NEXT: movl %esp, %ebp
29+
; CHECK-NEXT: addl $15, %edi
30+
; CHECK-NEXT: andl $-16, %edi
31+
; CHECK-NEXT: subl %edi, %esp
32+
start:
33+
%alloca = alloca i8, i32 %size
34+
unreachable
35+
}
36+
37+
; probe-stack + fixed size alloca not in entry block
38+
define void @test3() nounwind #0 {
39+
; CHECK-LABEL: test3:
40+
; CHECK: # %bb.0: # %start
41+
; CHECK-NEXT: pushq %rbp
42+
; CHECK-NEXT: movl %esp, %ebp
43+
; CHECK-NEXT: pushq %rax
44+
; CHECK-NEXT: subl $1992, %esp # imm = 0x7C8
45+
start:
46+
br label %block
47+
48+
block:
49+
%alloca = alloca i8, i32 2000
50+
unreachable
51+
}
52+
53+
attributes #0 = { "probe-stack"="__rust_probestack" }
54+
attributes #1 = { "probe-stack"="__rust_probestack" "no-stack-arg-probe" }

0 commit comments

Comments
 (0)