Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit a30b1b4

Browse files
serge-sans-paillememfrob
serge-sans-paille
authored and
memfrob
committedOct 4, 2022
[stack-clash] Fix probing of dynamic alloca
- Perform the probing in the correct direction. Related to rust-lang/rust#77885 (comment) - The first touch on a dynamic alloca cannot use a mov because it clobbers existing space. Use a xor 0 instead Differential Revision: https://reviews.llvm.org/D90216
1 parent dd5d9bb commit a30b1b4

File tree

3 files changed

+13
-13
lines changed

3 files changed

+13
-13
lines changed
 

‎llvm/lib/Target/X86/X86ISelLowering.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -32316,7 +32316,7 @@ X86TargetLowering::EmitLoweredProbedAlloca(MachineInstr &MI,
3231632316

3231732317
BuildMI(testMBB, DL, TII->get(X86::JCC_1))
3231832318
.addMBB(tailMBB)
32319-
.addImm(X86::COND_LE);
32319+
.addImm(X86::COND_GE);
3232032320
testMBB->addSuccessor(blockMBB);
3232132321
testMBB->addSuccessor(tailMBB);
3232232322

@@ -32332,9 +32332,9 @@ X86TargetLowering::EmitLoweredProbedAlloca(MachineInstr &MI,
3233232332
//
3233332333
// The property we want to enforce is to never have more than [page alloc] between two probes.
3233432334

32335-
const unsigned MovMIOpc =
32336-
TFI.Uses64BitFramePtr ? X86::MOV64mi32 : X86::MOV32mi;
32337-
addRegOffset(BuildMI(blockMBB, DL, TII->get(MovMIOpc)), physSPReg, false, 0)
32335+
const unsigned XORMIOpc =
32336+
TFI.Uses64BitFramePtr ? X86::XOR64mi8 : X86::XOR32mi8;
32337+
addRegOffset(BuildMI(blockMBB, DL, TII->get(XORMIOpc)), physSPReg, false, 0)
3233832338
.addImm(0);
3233932339

3234032340
BuildMI(blockMBB, DL,

‎llvm/test/CodeGen/X86/stack-clash-dynamic-alloca.ll

+6-6
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ attributes #0 = {"probe-stack"="inline-asm"}
2424
; CHECK-X86-64-NEXT: andq $-16, %rcx
2525
; CHECK-X86-64-NEXT: subq %rcx, %rax
2626
; CHECK-X86-64-NEXT: cmpq %rsp, %rax
27-
; CHECK-X86-64-NEXT: jle .LBB0_3
27+
; CHECK-X86-64-NEXT: jge .LBB0_3
2828
; CHECK-X86-64-NEXT: .LBB0_2: # =>This Inner Loop Header: Depth=1
29-
; CHECK-X86-64-NEXT: movq $0, (%rsp)
29+
; CHECK-X86-64-NEXT: xorq $0, (%rsp)
3030
; CHECK-X86-64-NEXT: subq $4096, %rsp # imm = 0x1000
3131
; CHECK-X86-64-NEXT: cmpq %rsp, %rax
32-
; CHECK-X86-64-NEXT: jg .LBB0_2
32+
; CHECK-X86-64-NEXT: jl .LBB0_2
3333
; CHECK-X86-64-NEXT: .LBB0_3:
3434
; CHECK-X86-64-NEXT: movq %rax, %rsp
3535
; CHECK-X86-64-NEXT: movl $1, 4792(%rax)
@@ -54,12 +54,12 @@ attributes #0 = {"probe-stack"="inline-asm"}
5454
; CHECK-X86-32-NEXT: andl $-16, %ecx
5555
; CHECK-X86-32-NEXT: subl %ecx, %eax
5656
; CHECK-X86-32-NEXT: cmpl %esp, %eax
57-
; CHECK-X86-32-NEXT: jle .LBB0_3
57+
; CHECK-X86-32-NEXT: jge .LBB0_3
5858
; CHECK-X86-32-NEXT: .LBB0_2: # =>This Inner Loop Header: Depth=1
59-
; CHECK-X86-32-NEXT: movl $0, (%esp)
59+
; CHECK-X86-32-NEXT: xorl $0, (%esp)
6060
; CHECK-X86-32-NEXT: subl $4096, %esp # imm = 0x1000
6161
; CHECK-X86-32-NEXT: cmpl %esp, %eax
62-
; CHECK-X86-32-NEXT: jg .LBB0_2
62+
; CHECK-X86-32-NEXT: jl .LBB0_2
6363
; CHECK-X86-32-NEXT: .LBB0_3:
6464
; CHECK-X86-32-NEXT: movl %eax, %esp
6565
; CHECK-X86-32-NEXT: movl $1, 4792(%eax)

‎llvm/test/CodeGen/X86/stack-clash-small-alloc-medium-align.ll

+3-3
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,12 @@ define i32 @foo4(i64 %i) local_unnamed_addr #0 {
106106
; CHECK-NEXT: andq $-16, %rcx
107107
; CHECK-NEXT: subq %rcx, %rax
108108
; CHECK-NEXT: cmpq %rsp, %rax
109-
; CHECK-NEXT: jle .LBB3_3
109+
; CHECK-NEXT: jge .LBB3_3
110110
; CHECK-NEXT:.LBB3_2: # =>This Inner Loop Header: Depth=1
111-
; CHECK-NEXT: movq $0, (%rsp)
111+
; CHECK-NEXT: xorq $0, (%rsp)
112112
; CHECK-NEXT: subq $4096, %rsp # imm = 0x1000
113113
; CHECK-NEXT: cmpq %rsp, %rax
114-
; CHECK-NEXT: jg .LBB3_2
114+
; CHECK-NEXT: jl .LBB3_2
115115
; CHECK-NEXT:.LBB3_3:
116116
; CHECK-NEXT: andq $-64, %rax
117117
; CHECK-NEXT: movq %rax, %rsp

0 commit comments

Comments
 (0)
Failed to load comments.