Skip to content

[AIX] Add -msave-reg-params to save arguments to stack #97524

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 1 commit into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions clang/include/clang/Basic/CodeGenOptions.def
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,9 @@ CODEGENOPT(ForceAAPCSBitfieldLoad, 1, 0)
/// Assume that by-value parameters do not alias any other values.
CODEGENOPT(PassByValueIsNoAlias, 1, 0)

/// Whether to store register parameters to stack.
CODEGENOPT(SaveRegParams, 1, 0)

/// Whether to not follow the AAPCS that enforces volatile bit-field access width to be
/// according to the field declaring type width.
CODEGENOPT(AAPCSBitfieldWidth, 1, 1)
Expand Down
5 changes: 5 additions & 0 deletions clang/include/clang/Driver/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -5065,6 +5065,11 @@ def mspe : Flag<["-"], "mspe">, Group<m_ppc_Features_Group>;
def mno_spe : Flag<["-"], "mno-spe">, Group<m_ppc_Features_Group>;
def mefpu2 : Flag<["-"], "mefpu2">, Group<m_ppc_Features_Group>;
} // let Flags = [TargetSpecific]
def msave_reg_params : Flag<["-"], "msave-reg-params">, Group<m_Group>,
Flags<[TargetSpecific]>,
Visibility<[ClangOption, CC1Option]>,
HelpText<"Save arguments passed by registers to ABI-defined stack positions">,
MarshallingInfoFlag<CodeGenOpts<"SaveRegParams">>;
def mabi_EQ_quadword_atomics : Flag<["-"], "mabi=quadword-atomics">,
Group<m_Group>, Visibility<[ClangOption, CC1Option]>,
HelpText<"Enable quadword atomics ABI on AIX (AIX PPC64 only). Uses lqarx/stqcx. instructions.">,
Expand Down
3 changes: 3 additions & 0 deletions clang/lib/CodeGen/CGCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2025,6 +2025,9 @@ static void getTrivialDefaultFunctionAttributes(
FuncAttrs.addAttribute(llvm::Attribute::NoUnwind);
}

if (CodeGenOpts.SaveRegParams && !AttrOnCallSite)
FuncAttrs.addAttribute("save-reg-params");

for (StringRef Attr : CodeGenOpts.DefaultFunctionAttrs) {
StringRef Var, Value;
std::tie(Var, Value) = Attr.split('=');
Expand Down
3 changes: 3 additions & 0 deletions clang/lib/Driver/ToolChains/AIX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,9 @@ void AIX::addClangTargetOptions(
options::OPT_mtocdata))
addTocDataOptions(Args, CC1Args, getDriver());

if (Args.hasArg(options::OPT_msave_reg_params))
CC1Args.push_back("-msave-reg-params");

if (Args.hasFlag(options::OPT_fxl_pragma_pack,
options::OPT_fno_xl_pragma_pack, true))
CC1Args.push_back("-fxl-pragma-pack");
Expand Down
10 changes: 10 additions & 0 deletions clang/test/CodeGen/PowerPC/save-reg-params.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// RUN: %clang_cc1 -triple powerpc64-ibm-aix -emit-llvm -o - %s -msave-reg-params | FileCheck -check-prefix=SAVE %s
// RUN: %clang_cc1 -triple powerpc-ibm-aix -emit-llvm -o - %s -msave-reg-params | FileCheck -check-prefix=SAVE %s
// RUN: %clang_cc1 -triple powerpc64-ibm-aix -emit-llvm -o - %s | FileCheck -check-prefix=NOSAVE %s
// RUN: %clang_cc1 -triple powerpc-ibm-aix -emit-llvm -o - %s | FileCheck -check-prefix=NOSAVE %s

void bar(int);
void foo(int x) { bar(x); }

// SAVE: attributes #{{[0-9]+}} = { {{.+}} "save-reg-params" {{.+}} }
// NOSAVE-NOT: "save-reg-params"
7 changes: 7 additions & 0 deletions clang/test/Driver/aix-save-reg-params.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// RUN: %clang -### -target powerpc-ibm-aix-xcoff -msave-reg-params -c %s -o /dev/null 2>&1 | FileCheck %s
// RUN: %clang -### -target powerpc64-ibm-aix-xcoff -msave-reg-params -c %s -o /dev/null 2>&1 | FileCheck %s
// RUN: %clang -### -target powerpc-ibm-aix-xcoff -c %s -o /dev/null 2>&1 | FileCheck %s --check-prefix=DISABLE
// RUN: %clang -### -target powerpc64-ibm-aix-xcoff -c %s -o /dev/null 2>&1 | FileCheck %s --check-prefix=DISABLE

// CHECK: "-msave-reg-params"
// DISABLE-NOT: "-msave-reg-params"
4 changes: 4 additions & 0 deletions clang/test/Driver/ppc-unsupported.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,8 @@
// RUN: -c %s 2>&1 | FileCheck %s
// RUN: not %clang -target powerpc-unknown-aix -mabi=quadword-atomics \
// RUN: -c %s 2>&1 | FileCheck %s
// RUN: not %clang -target powerpc64le-unknown-linux-gnu -msave-reg-params \
// RUN: -c %s 2>&1 | FileCheck %s
// RUN: not %clang -target powerpc-unknown-unknown -msave-reg-params \
// RUN: -c %s 2>&1 | FileCheck %s
// CHECK: unsupported option
23 changes: 23 additions & 0 deletions llvm/lib/Target/PowerPC/PPCISelLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7214,6 +7214,8 @@ SDValue PPCTargetLowering::LowerFormalArguments_AIX(
// Reserve space for the linkage area on the stack.
const unsigned LinkageSize = Subtarget.getFrameLowering()->getLinkageSize();
CCInfo.AllocateStack(LinkageSize, Align(PtrByteSize));
uint64_t SaveStackPos = CCInfo.getStackSize();
bool SaveParams = MF.getFunction().hasFnAttribute("save-reg-params");
CCInfo.AnalyzeFormalArguments(Ins, CC_AIX);

SmallVector<SDValue, 8> MemOps;
Expand All @@ -7232,6 +7234,27 @@ SDValue PPCTargetLowering::LowerFormalArguments_AIX(
if (VA.isMemLoc() && VA.needsCustom() && ValVT.isFloatingPoint())
continue;

if (SaveParams && VA.isRegLoc() && !Flags.isByVal() && !VA.needsCustom()) {
const TargetRegisterClass *RegClass = getRegClassForSVT(
LocVT.SimpleTy, IsPPC64, Subtarget.hasP8Vector(), Subtarget.hasVSX());
// On PPC64, debugger assumes extended 8-byte values are stored from GPR.
MVT SaveVT = RegClass == &PPC::G8RCRegClass ? MVT::i64 : LocVT;
const Register VReg = MF.addLiveIn(VA.getLocReg(), RegClass);
SDValue Parm = DAG.getCopyFromReg(Chain, dl, VReg, SaveVT);
int FI = MFI.CreateFixedObject(SaveVT.getStoreSize(), SaveStackPos, true);
SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
SDValue StoreReg = DAG.getStore(Chain, dl, Parm, FIN,
MachinePointerInfo(), Align(PtrByteSize));
SaveStackPos = alignTo(SaveStackPos + SaveVT.getStoreSize(), PtrByteSize);
MemOps.push_back(StoreReg);
}

if (SaveParams && (VA.isMemLoc() || Flags.isByVal()) && !VA.needsCustom()) {
unsigned StoreSize =
Flags.isByVal() ? Flags.getByValSize() : LocVT.getStoreSize();
SaveStackPos = alignTo(SaveStackPos + StoreSize, PtrByteSize);
}

auto HandleMemLoc = [&]() {
const unsigned LocSize = LocVT.getStoreSize();
const unsigned ValSize = ValVT.getStoreSize();
Expand Down
Loading
Loading