Skip to content

[PEI][PowerPC] Fix false alarm of stack size limit #65559

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions llvm/include/llvm/CodeGen/TargetFrameLowering.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ class TargetFrameLowering {
///
Align getStackAlign() const { return StackAlignment; }

/// getStackThreshold - Return the maximum stack size
///
virtual uint64_t getStackThreshold() const { return UINT_MAX; }

/// alignSPAdjust - This method aligns the stack adjustment to the correct
/// alignment.
///
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/CodeGen/PrologEpilogInserter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ bool PEI::runOnMachineFunction(MachineFunction &MF) {
MachineFrameInfo &MFI = MF.getFrameInfo();
uint64_t StackSize = MFI.getStackSize();

unsigned Threshold = UINT_MAX;
uint64_t Threshold = TFI->getStackThreshold();
if (MF.getFunction().hasFnAttribute("warn-stack-size")) {
bool Failed = MF.getFunction()
.getFnAttribute("warn-stack-size")
Expand Down
14 changes: 14 additions & 0 deletions llvm/lib/Target/PowerPC/PPCFrameLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2740,3 +2740,17 @@ bool PPCFrameLowering::enableShrinkWrapping(const MachineFunction &MF) const {
return false;
return !MF.getSubtarget<PPCSubtarget>().is32BitELFABI();
}

uint64_t PPCFrameLowering::getStackThreshold() const {
// On PPC64, we use `stux r1, <scratch_reg>, r1` to extend the stack;
// use `add r1, r1, <scratch_reg>` to release the stack frame.
// Scratch register contains a signed 64-bit number, which is negative
// when extending the stack and is positive when releasing the stack frame.
// To make `stux` and `add` paired, the absolute value of the number contained
// in the scratch register should be the same. Thus the maximum stack size
// is (2^63)-1, i.e., LONG_MAX.
if (Subtarget.isPPC64())
return LONG_MAX;

return TargetFrameLowering::getStackThreshold();
}
2 changes: 2 additions & 0 deletions llvm/lib/Target/PowerPC/PPCFrameLowering.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ class PPCFrameLowering: public TargetFrameLowering {
/// function prologue/epilogue.
bool canUseAsPrologue(const MachineBasicBlock &MBB) const override;
bool canUseAsEpilogue(const MachineBasicBlock &MBB) const override;

uint64_t getStackThreshold() const override;
};
} // End llvm namespace

Expand Down
16 changes: 16 additions & 0 deletions llvm/test/CodeGen/PowerPC/warn-stack-size.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
; RUN: llc -mtriple=powerpc64-unknown-unknown < %s 2>&1 | FileCheck %s
; RUN: llc -mtriple=powerpc64le-unknown-unknown < %s 2>&1 | FileCheck %s

; CHECK-NOT: warning: {{.*}} stack frame size ({{.*}}) exceeds limit (4294967295) in function 'large_stack'
define ptr @large_stack() {
%s = alloca [281474976710656 x i8], align 1
%e = getelementptr i8, ptr %s, i64 0
ret ptr %e
}

; CHECK: warning: {{.*}} stack frame size ({{.*}}) exceeds limit (4294967295) in function 'warn_on_large_stack'
define ptr @warn_on_large_stack() "warn-stack-size"="4294967295" {
%s = alloca [281474976710656 x i8], align 1
%e = getelementptr i8, ptr %s, i64 0
ret ptr %e
}