Skip to content

Commit d9efcb5

Browse files
author
bzEq
authored
[PEI][PowerPC] Fix false alarm of stack size limit (#65559)
PPC64 allows stack size up to ((2^63)-1) bytes. Currently llc reports ``` warning: stack frame size (4294967568) exceeds limit (4294967295) in function 'main' ``` if the stack allocated is larger than 4G.
1 parent 785e573 commit d9efcb5

File tree

5 files changed

+37
-2
lines changed

5 files changed

+37
-2
lines changed

llvm/include/llvm/CodeGen/TargetFrameLowering.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ class TargetFrameLowering {
102102
///
103103
Align getStackAlign() const { return StackAlignment; }
104104

105+
/// getStackThreshold - Return the maximum stack size
106+
///
107+
virtual uint64_t getStackThreshold() const { return UINT_MAX; }
108+
105109
/// alignSPAdjust - This method aligns the stack adjustment to the correct
106110
/// alignment.
107111
///

llvm/lib/CodeGen/PrologEpilogInserter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ bool PEI::runOnMachineFunction(MachineFunction &MF) {
293293
MachineFrameInfo &MFI = MF.getFrameInfo();
294294
uint64_t StackSize = MFI.getStackSize();
295295

296-
unsigned Threshold = UINT_MAX;
296+
uint64_t Threshold = TFI->getStackThreshold();
297297
if (MF.getFunction().hasFnAttribute("warn-stack-size")) {
298298
bool Failed = MF.getFunction()
299299
.getFnAttribute("warn-stack-size")

llvm/lib/Target/PowerPC/PPCFrameLowering.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2740,3 +2740,17 @@ bool PPCFrameLowering::enableShrinkWrapping(const MachineFunction &MF) const {
27402740
return false;
27412741
return !MF.getSubtarget<PPCSubtarget>().is32BitELFABI();
27422742
}
2743+
2744+
uint64_t PPCFrameLowering::getStackThreshold() const {
2745+
// On PPC64, we use `stux r1, r1, <scratch_reg>` to extend the stack;
2746+
// use `add r1, r1, <scratch_reg>` to release the stack frame.
2747+
// Scratch register contains a signed 64-bit number, which is negative
2748+
// when extending the stack and is positive when releasing the stack frame.
2749+
// To make `stux` and `add` paired, the absolute value of the number contained
2750+
// in the scratch register should be the same. Thus the maximum stack size
2751+
// is (2^63)-1, i.e., LONG_MAX.
2752+
if (Subtarget.isPPC64())
2753+
return LONG_MAX;
2754+
2755+
return TargetFrameLowering::getStackThreshold();
2756+
}

llvm/lib/Target/PowerPC/PPCFrameLowering.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,8 @@ class PPCFrameLowering: public TargetFrameLowering {
173173
/// function prologue/epilogue.
174174
bool canUseAsPrologue(const MachineBasicBlock &MBB) const override;
175175
bool canUseAsEpilogue(const MachineBasicBlock &MBB) const override;
176+
177+
uint64_t getStackThreshold() const override;
176178
};
177179
} // End llvm namespace
178180

llvm/test/CodeGen/PowerPC/huge-frame-size.ll

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
21
; RUN: llc -verify-machineinstrs -mtriple=powerpc64le-linux-gnu < %s \
32
; RUN: 2>&1 | FileCheck --check-prefix=CHECK-LE %s
43
; RUN: llc -verify-machineinstrs -mtriple=powerpc64-ibm-aix-xcoff < %s \
54
; RUN: 2>&1 | FileCheck --check-prefix=CHECK-BE %s
65

6+
; CHECK-NOT: warning: {{.*}} stack frame size ({{.*}}) exceeds limit (4294967295) in function 'foo'
7+
; CHECK-NOT: warning: {{.*}} stack frame size ({{.*}}) exceeds limit (4294967295) in function 'large_stack'
8+
; CHECK: warning: {{.*}} stack frame size ({{.*}}) exceeds limit (4294967295) in function 'warn_on_large_stack'
9+
710
declare void @bar(ptr)
811

912
define void @foo(i8 %x) {
@@ -54,3 +57,15 @@ entry:
5457
store volatile i8 %x, ptr %d
5558
ret void
5659
}
60+
61+
define ptr @large_stack() {
62+
%s = alloca [281474976710656 x i8], align 1
63+
%e = getelementptr i8, ptr %s, i64 0
64+
ret ptr %e
65+
}
66+
67+
define ptr @warn_on_large_stack() "warn-stack-size"="4294967295" {
68+
%s = alloca [281474976710656 x i8], align 1
69+
%e = getelementptr i8, ptr %s, i64 0
70+
ret ptr %e
71+
}

0 commit comments

Comments
 (0)