Skip to content

Commit 02cc833

Browse files
ecnelisesyuxuanchen1997
authored andcommitted
[AIX] Add -msave-reg-params to save arguments to stack (#97524)
Summary: In PowerPC ABI, a few initial arguments are passed through registers, but their places in parameter save area are reserved, arguments passed by memory goes after the reserved location. For debugging purpose, we may want to save copy of the pass-by-reg arguments into correct places on stack. The new option achieves by adding new function level attribute and make argument lowering part aware of it. Test Plan: Reviewers: Subscribers: Tasks: Tags: Differential Revision: https://phabricator.intern.facebook.com/D60250560
1 parent 4ee4313 commit 02cc833

File tree

9 files changed

+925
-0
lines changed

9 files changed

+925
-0
lines changed

clang/include/clang/Basic/CodeGenOptions.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,9 @@ CODEGENOPT(ForceAAPCSBitfieldLoad, 1, 0)
429429
/// Assume that by-value parameters do not alias any other values.
430430
CODEGENOPT(PassByValueIsNoAlias, 1, 0)
431431

432+
/// Whether to store register parameters to stack.
433+
CODEGENOPT(SaveRegParams, 1, 0)
434+
432435
/// Whether to not follow the AAPCS that enforces volatile bit-field access width to be
433436
/// according to the field declaring type width.
434437
CODEGENOPT(AAPCSBitfieldWidth, 1, 1)

clang/include/clang/Driver/Options.td

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5089,6 +5089,11 @@ def mspe : Flag<["-"], "mspe">, Group<m_ppc_Features_Group>;
50895089
def mno_spe : Flag<["-"], "mno-spe">, Group<m_ppc_Features_Group>;
50905090
def mefpu2 : Flag<["-"], "mefpu2">, Group<m_ppc_Features_Group>;
50915091
} // let Flags = [TargetSpecific]
5092+
def msave_reg_params : Flag<["-"], "msave-reg-params">, Group<m_Group>,
5093+
Flags<[TargetSpecific]>,
5094+
Visibility<[ClangOption, CC1Option]>,
5095+
HelpText<"Save arguments passed by registers to ABI-defined stack positions">,
5096+
MarshallingInfoFlag<CodeGenOpts<"SaveRegParams">>;
50925097
def mabi_EQ_quadword_atomics : Flag<["-"], "mabi=quadword-atomics">,
50935098
Group<m_Group>, Visibility<[ClangOption, CC1Option]>,
50945099
HelpText<"Enable quadword atomics ABI on AIX (AIX PPC64 only). Uses lqarx/stqcx. instructions.">,

clang/lib/CodeGen/CGCall.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2025,6 +2025,9 @@ static void getTrivialDefaultFunctionAttributes(
20252025
FuncAttrs.addAttribute(llvm::Attribute::NoUnwind);
20262026
}
20272027

2028+
if (CodeGenOpts.SaveRegParams && !AttrOnCallSite)
2029+
FuncAttrs.addAttribute("save-reg-params");
2030+
20282031
for (StringRef Attr : CodeGenOpts.DefaultFunctionAttrs) {
20292032
StringRef Var, Value;
20302033
std::tie(Var, Value) = Attr.split('=');

clang/lib/Driver/ToolChains/AIX.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,9 @@ void AIX::addClangTargetOptions(
548548
options::OPT_mtocdata))
549549
addTocDataOptions(Args, CC1Args, getDriver());
550550

551+
if (Args.hasArg(options::OPT_msave_reg_params))
552+
CC1Args.push_back("-msave-reg-params");
553+
551554
if (Args.hasFlag(options::OPT_fxl_pragma_pack,
552555
options::OPT_fno_xl_pragma_pack, true))
553556
CC1Args.push_back("-fxl-pragma-pack");
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// RUN: %clang_cc1 -triple powerpc64-ibm-aix -emit-llvm -o - %s -msave-reg-params | FileCheck -check-prefix=SAVE %s
2+
// RUN: %clang_cc1 -triple powerpc-ibm-aix -emit-llvm -o - %s -msave-reg-params | FileCheck -check-prefix=SAVE %s
3+
// RUN: %clang_cc1 -triple powerpc64-ibm-aix -emit-llvm -o - %s | FileCheck -check-prefix=NOSAVE %s
4+
// RUN: %clang_cc1 -triple powerpc-ibm-aix -emit-llvm -o - %s | FileCheck -check-prefix=NOSAVE %s
5+
6+
void bar(int);
7+
void foo(int x) { bar(x); }
8+
9+
// SAVE: attributes #{{[0-9]+}} = { {{.+}} "save-reg-params" {{.+}} }
10+
// NOSAVE-NOT: "save-reg-params"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// RUN: %clang -### -target powerpc-ibm-aix-xcoff -msave-reg-params -c %s -o /dev/null 2>&1 | FileCheck %s
2+
// RUN: %clang -### -target powerpc64-ibm-aix-xcoff -msave-reg-params -c %s -o /dev/null 2>&1 | FileCheck %s
3+
// RUN: %clang -### -target powerpc-ibm-aix-xcoff -c %s -o /dev/null 2>&1 | FileCheck %s --check-prefix=DISABLE
4+
// RUN: %clang -### -target powerpc64-ibm-aix-xcoff -c %s -o /dev/null 2>&1 | FileCheck %s --check-prefix=DISABLE
5+
6+
// CHECK: "-msave-reg-params"
7+
// DISABLE-NOT: "-msave-reg-params"

clang/test/Driver/ppc-unsupported.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,8 @@
1616
// RUN: -c %s 2>&1 | FileCheck %s
1717
// RUN: not %clang -target powerpc-unknown-aix -mabi=quadword-atomics \
1818
// RUN: -c %s 2>&1 | FileCheck %s
19+
// RUN: not %clang -target powerpc64le-unknown-linux-gnu -msave-reg-params \
20+
// RUN: -c %s 2>&1 | FileCheck %s
21+
// RUN: not %clang -target powerpc-unknown-unknown -msave-reg-params \
22+
// RUN: -c %s 2>&1 | FileCheck %s
1923
// CHECK: unsupported option

llvm/lib/Target/PowerPC/PPCISelLowering.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7215,6 +7215,8 @@ SDValue PPCTargetLowering::LowerFormalArguments_AIX(
72157215
// Reserve space for the linkage area on the stack.
72167216
const unsigned LinkageSize = Subtarget.getFrameLowering()->getLinkageSize();
72177217
CCInfo.AllocateStack(LinkageSize, Align(PtrByteSize));
7218+
uint64_t SaveStackPos = CCInfo.getStackSize();
7219+
bool SaveParams = MF.getFunction().hasFnAttribute("save-reg-params");
72187220
CCInfo.AnalyzeFormalArguments(Ins, CC_AIX);
72197221

72207222
SmallVector<SDValue, 8> MemOps;
@@ -7233,6 +7235,27 @@ SDValue PPCTargetLowering::LowerFormalArguments_AIX(
72337235
if (VA.isMemLoc() && VA.needsCustom() && ValVT.isFloatingPoint())
72347236
continue;
72357237

7238+
if (SaveParams && VA.isRegLoc() && !Flags.isByVal() && !VA.needsCustom()) {
7239+
const TargetRegisterClass *RegClass = getRegClassForSVT(
7240+
LocVT.SimpleTy, IsPPC64, Subtarget.hasP8Vector(), Subtarget.hasVSX());
7241+
// On PPC64, debugger assumes extended 8-byte values are stored from GPR.
7242+
MVT SaveVT = RegClass == &PPC::G8RCRegClass ? MVT::i64 : LocVT;
7243+
const Register VReg = MF.addLiveIn(VA.getLocReg(), RegClass);
7244+
SDValue Parm = DAG.getCopyFromReg(Chain, dl, VReg, SaveVT);
7245+
int FI = MFI.CreateFixedObject(SaveVT.getStoreSize(), SaveStackPos, true);
7246+
SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
7247+
SDValue StoreReg = DAG.getStore(Chain, dl, Parm, FIN,
7248+
MachinePointerInfo(), Align(PtrByteSize));
7249+
SaveStackPos = alignTo(SaveStackPos + SaveVT.getStoreSize(), PtrByteSize);
7250+
MemOps.push_back(StoreReg);
7251+
}
7252+
7253+
if (SaveParams && (VA.isMemLoc() || Flags.isByVal()) && !VA.needsCustom()) {
7254+
unsigned StoreSize =
7255+
Flags.isByVal() ? Flags.getByValSize() : LocVT.getStoreSize();
7256+
SaveStackPos = alignTo(SaveStackPos + StoreSize, PtrByteSize);
7257+
}
7258+
72367259
auto HandleMemLoc = [&]() {
72377260
const unsigned LocSize = LocVT.getStoreSize();
72387261
const unsigned ValSize = ValVT.getStoreSize();

0 commit comments

Comments
 (0)