Skip to content

Commit 356c33e

Browse files
committed
[PowerPC] Take ABI into account for data layout
Prior to this change, the data layout calculation would not account for explicitly set -mabi=elfv2 on powerpc64-unknown-linux-gnu, a target that defaults to elfv1. Signed-off-by: Jens Reidel <[email protected]>
1 parent 7c8b3f3 commit 356c33e

File tree

3 files changed

+35
-22
lines changed

3 files changed

+35
-22
lines changed

clang/lib/Basic/Targets/PPC.h

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -445,42 +445,30 @@ class LLVM_LIBRARY_VISIBILITY PPC64TargetInfo : public PPCTargetInfo {
445445
LongWidth = LongAlign = PointerWidth = PointerAlign = 64;
446446
IntMaxType = SignedLong;
447447
Int64Type = SignedLong;
448-
std::string DataLayout;
449448

450449
if (Triple.isOSAIX()) {
451450
// TODO: Set appropriate ABI for AIX platform.
452-
DataLayout = "E-m:a-Fi64-i64:64-i128:128-n32:64";
453451
LongDoubleWidth = 64;
454452
LongDoubleAlign = DoubleAlign = 32;
455453
LongDoubleFormat = &llvm::APFloat::IEEEdouble();
456-
} else if ((Triple.getArch() == llvm::Triple::ppc64le)) {
457-
DataLayout = "e-m:e-Fn32-i64:64-i128:128-n32:64";
454+
} else if ((Triple.getArch() == llvm::Triple::ppc64le) ||
455+
Triple.isPPC64ELFv2ABI()) {
458456
ABI = "elfv2";
459457
} else {
460-
DataLayout = "E-m:e";
461-
if (Triple.isPPC64ELFv2ABI()) {
462-
ABI = "elfv2";
463-
DataLayout += "-Fn32";
464-
} else {
465-
ABI = "elfv1";
466-
DataLayout += "-Fi64";
467-
}
468-
DataLayout += "-i64:64-i128:128-n32:64";
458+
ABI = "elfv1";
469459
}
470460

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

476-
if (Triple.isOSAIX() || Triple.isOSLinux())
477-
DataLayout += "-S128-v256:256:256-v512:512:512";
478-
resetDataLayout(DataLayout);
479-
480466
// Newer PPC64 instruction sets support atomics up to 16 bytes.
481467
MaxAtomicPromoteWidth = 128;
482468
// Baseline PPC64 supports inlining atomics up to 8 bytes.
483469
MaxAtomicInlineWidth = 64;
470+
471+
calculateDataLayout();
484472
}
485473

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

486+
void calculateDataLayout() {
487+
std::string DataLayout;
488+
489+
if (getTriple().isOSAIX()) {
490+
DataLayout = "E-m:a-Fi64-i64:64-i128:128-n32:64";
491+
} else if ((getTriple().getArch() == llvm::Triple::ppc64le)) {
492+
DataLayout = "e-m:e-Fn32-i64:64-i128:128-n32:64";
493+
} else {
494+
DataLayout = "E-m:e";
495+
if (ABI == "elfv2") {
496+
DataLayout += "-Fn32";
497+
} else {
498+
DataLayout += "-Fi64";
499+
}
500+
DataLayout += "-i64:64-i128:128-n32:64";
501+
}
502+
503+
if (getTriple().isOSAIX() || getTriple().isOSLinux())
504+
DataLayout += "-S128-v256:256:256-v512:512:512";
505+
resetDataLayout(DataLayout);
506+
}
507+
498508
// PPC64 Linux-specific ABI options.
499509
bool setABI(const std::string &Name) override {
500510
if (Name == "elfv1" || Name == "elfv2") {
501511
ABI = Name;
512+
calculateDataLayout();
502513
return true;
503514
}
504515
return false;

llvm/lib/Target/PowerPC/PPCTargetMachine.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,8 +296,9 @@ PPCTargetMachine::PPCTargetMachine(const Target &T, const Triple &TT,
296296
std::optional<Reloc::Model> RM,
297297
std::optional<CodeModel::Model> CM,
298298
CodeGenOptLevel OL, bool JIT)
299-
: CodeGenTargetMachineImpl(T, TT.computeDataLayout(), TT, CPU,
300-
computeFSAdditions(FS, OL, TT), Options,
299+
: CodeGenTargetMachineImpl(T,
300+
TT.computeDataLayout(Options.MCOptions.ABIName),
301+
TT, CPU, computeFSAdditions(FS, OL, TT), Options,
301302
getEffectiveRelocModel(TT, RM),
302303
getEffectivePPCCodeModel(TT, CM, JIT), OL),
303304
TLOF(createTLOF(getTargetTriple())),

llvm/lib/TargetParser/TargetDataLayout.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ static std::string computeMipsDataLayout(const Triple &TT, StringRef ABIName) {
208208
return Ret;
209209
}
210210

211-
static std::string computePowerDataLayout(const Triple &T) {
211+
static std::string computePowerDataLayout(const Triple &T, StringRef ABIName) {
212212
bool is64Bit = T.isPPC64();
213213
std::string Ret;
214214

@@ -228,7 +228,8 @@ static std::string computePowerDataLayout(const Triple &T) {
228228
// If the target ABI uses function descriptors, then the alignment of function
229229
// pointers depends on the alignment used to emit the descriptor. Otherwise,
230230
// function pointers are aligned to 32 bits because the instructions must be.
231-
if ((T.getArch() == Triple::ppc64 && !T.isPPC64ELFv2ABI())) {
231+
if ((T.getArch() == Triple::ppc64 &&
232+
(!T.isPPC64ELFv2ABI() && ABIName != "elfv2"))) {
232233
Ret += "-Fi64";
233234
} else if (T.isOSAIX()) {
234235
Ret += is64Bit ? "-Fi64" : "-Fi32";
@@ -570,7 +571,7 @@ std::string Triple::computeDataLayout(StringRef ABIName) const {
570571
case Triple::ppcle:
571572
case Triple::ppc64:
572573
case Triple::ppc64le:
573-
return computePowerDataLayout(*this);
574+
return computePowerDataLayout(*this, ABIName);
574575
case Triple::r600:
575576
case Triple::amdgcn:
576577
return computeAMDDataLayout(*this);

0 commit comments

Comments
 (0)