Skip to content

[PowerPC] Take ABI into account for data layout #149725

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
45 changes: 28 additions & 17 deletions clang/lib/Basic/Targets/PPC.h
Original file line number Diff line number Diff line change
Expand Up @@ -445,42 +445,30 @@ class LLVM_LIBRARY_VISIBILITY PPC64TargetInfo : public PPCTargetInfo {
LongWidth = LongAlign = PointerWidth = PointerAlign = 64;
IntMaxType = SignedLong;
Int64Type = SignedLong;
std::string DataLayout;

if (Triple.isOSAIX()) {
// TODO: Set appropriate ABI for AIX platform.
DataLayout = "E-m:a-Fi64-i64:64-i128:128-n32:64";
LongDoubleWidth = 64;
LongDoubleAlign = DoubleAlign = 32;
LongDoubleFormat = &llvm::APFloat::IEEEdouble();
} else if ((Triple.getArch() == llvm::Triple::ppc64le)) {
DataLayout = "e-m:e-Fn32-i64:64-i128:128-n32:64";
} else if ((Triple.getArch() == llvm::Triple::ppc64le) ||
Triple.isPPC64ELFv2ABI()) {
ABI = "elfv2";
} else {
DataLayout = "E-m:e";
if (Triple.isPPC64ELFv2ABI()) {
ABI = "elfv2";
DataLayout += "-Fn32";
} else {
ABI = "elfv1";
DataLayout += "-Fi64";
}
DataLayout += "-i64:64-i128:128-n32:64";
ABI = "elfv1";
}

if (Triple.isOSFreeBSD() || Triple.isOSOpenBSD() || Triple.isMusl()) {
LongDoubleWidth = LongDoubleAlign = 64;
LongDoubleFormat = &llvm::APFloat::IEEEdouble();
}

if (Triple.isOSAIX() || Triple.isOSLinux())
DataLayout += "-S128-v256:256:256-v512:512:512";
resetDataLayout(DataLayout);

// Newer PPC64 instruction sets support atomics up to 16 bytes.
MaxAtomicPromoteWidth = 128;
// Baseline PPC64 supports inlining atomics up to 8 bytes.
MaxAtomicInlineWidth = 64;

calculateDataLayout();
}

void setMaxAtomicWidth() override {
Expand All @@ -495,10 +483,33 @@ class LLVM_LIBRARY_VISIBILITY PPC64TargetInfo : public PPCTargetInfo {
return TargetInfo::CharPtrBuiltinVaList;
}

void calculateDataLayout() {
std::string DataLayout;

if (getTriple().isOSAIX()) {
DataLayout = "E-m:a-Fi64-i64:64-i128:128-n32:64";
} else if ((getTriple().getArch() == llvm::Triple::ppc64le)) {
DataLayout = "e-m:e-Fn32-i64:64-i128:128-n32:64";
} else {
DataLayout = "E-m:e";
if (ABI == "elfv2") {
DataLayout += "-Fn32";
} else {
DataLayout += "-Fi64";
}
DataLayout += "-i64:64-i128:128-n32:64";
}

if (getTriple().isOSAIX() || getTriple().isOSLinux())
DataLayout += "-S128-v256:256:256-v512:512:512";
resetDataLayout(DataLayout);
}

// PPC64 Linux-specific ABI options.
bool setABI(const std::string &Name) override {
if (Name == "elfv1" || Name == "elfv2") {
ABI = Name;
calculateDataLayout();
return true;
}
return false;
Expand Down
9 changes: 6 additions & 3 deletions llvm/lib/Target/PowerPC/PPCTargetMachine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,10 @@ static bool isLittleEndianTriple(const Triple &T) {
}

/// Return the datalayout string of a subtarget.
static std::string getDataLayoutString(const Triple &T) {
static std::string computeDataLayout(const Triple &T,
const TargetOptions &Options) {
bool is64Bit = T.getArch() == Triple::ppc64 || T.getArch() == Triple::ppc64le;
StringRef ABIName = Options.MCOptions.getABIName();
std::string Ret;

// Most PPC* platforms are big endian, PPC(64)LE is little endian.
Expand All @@ -174,7 +176,8 @@ static std::string getDataLayoutString(const Triple &T) {
// If the target ABI uses function descriptors, then the alignment of function
// pointers depends on the alignment used to emit the descriptor. Otherwise,
// function pointers are aligned to 32 bits because the instructions must be.
if ((T.getArch() == Triple::ppc64 && !T.isPPC64ELFv2ABI())) {
if ((T.getArch() == Triple::ppc64 &&
(!T.isPPC64ELFv2ABI() && ABIName != "elfv2"))) {
Ret += "-Fi64";
} else if (T.isOSAIX()) {
Ret += is64Bit ? "-Fi64" : "-Fi32";
Expand Down Expand Up @@ -348,7 +351,7 @@ PPCTargetMachine::PPCTargetMachine(const Target &T, const Triple &TT,
std::optional<Reloc::Model> RM,
std::optional<CodeModel::Model> CM,
CodeGenOptLevel OL, bool JIT)
: CodeGenTargetMachineImpl(T, getDataLayoutString(TT), TT, CPU,
: CodeGenTargetMachineImpl(T, computeDataLayout(TT, Options), TT, CPU,
computeFSAdditions(FS, OL, TT), Options,
getEffectiveRelocModel(TT, RM),
getEffectivePPCCodeModel(TT, CM, JIT), OL),
Expand Down
Loading