Skip to content

Commit

Permalink
[PowerPC] Correctly use ELFv2 ABI on all OS's that use the ELFv2 ABI
Browse files Browse the repository at this point in the history
Add a member function isPPC64ELFv2ABI() to determine what ABI is used on the
64-bit PowerPC big endian operating environment.

Reviewed By: nemanjai, dim, pkubaj

Differential Revision: https://reviews.llvm.org/D144321
  • Loading branch information
brad0 committed Feb 20, 2023
1 parent 522d9f5 commit 4b09cb2
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 6 deletions.
5 changes: 4 additions & 1 deletion clang/lib/Basic/Targets/PPC.h
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,10 @@ class LLVM_LIBRARY_VISIBILITY PPC64TargetInfo : public PPCTargetInfo {
ABI = "elfv2";
} else {
DataLayout = "E-m:e-i64:64-n32:64";
ABI = "elfv1";
if (Triple.isPPC64ELFv2ABI())
ABI = "elfv2";
else
ABI = "elfv1";
}

if (Triple.isOSFreeBSD() || Triple.isOSOpenBSD() || Triple.isMusl()) {
Expand Down
3 changes: 1 addition & 2 deletions clang/lib/Driver/ToolChains/Clang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2006,8 +2006,7 @@ void Clang::AddPPCTargetArgs(const ArgList &Args,
if (T.isOSBinFormatELF()) {
switch (getToolChain().getArch()) {
case llvm::Triple::ppc64: {
if ((T.isOSFreeBSD() && T.getOSMajorVersion() >= 13) ||
T.isOSOpenBSD() || T.isMusl())
if (T.isPPC64ELFv2ABI())
ABIName = "elfv2";
else
ABIName = "elfv1";
Expand Down
1 change: 1 addition & 0 deletions clang/test/Driver/ppc-abi.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
// RUN: %clang -target powerpc64-unknown-freebsd12 %s -### 2>&1 | FileCheck --check-prefix=CHECK-ELFv1 %s
// RUN: %clang -target powerpc64-unknown-freebsd13 %s -### 2>&1 | FileCheck --check-prefix=CHECK-ELFv2-BE %s
// RUN: %clang -target powerpc64-unknown-freebsd14 %s -### 2>&1 | FileCheck --check-prefix=CHECK-ELFv2-BE %s
// RUN: %clang -target powerpc64-unknown-freebsd %s -### 2>&1 | FileCheck --check-prefix=CHECK-ELFv2-BE %s
// RUN: %clang -target powerpc64le-unknown-freebsd13 %s -### 2>&1 | FileCheck --check-prefix=CHECK-ELFv2 %s
// RUN: %clang -target powerpc64-unknown-openbsd %s -### 2>&1 | FileCheck --check-prefix=CHECK-ELFv2-BE-PIE %s
// RUN: %clang -target powerpc64-linux-musl %s -### 2>&1 | FileCheck --check-prefix=CHECK-ELFv2-BE-PIE %s
Expand Down
8 changes: 8 additions & 0 deletions llvm/include/llvm/TargetParser/Triple.h
Original file line number Diff line number Diff line change
Expand Up @@ -882,6 +882,14 @@ class Triple {
return getArch() == Triple::ppc64 || getArch() == Triple::ppc64le;
}

/// Tests whether the target 64-bit PowerPC big endian ABI is ELFv2.
bool isPPC64ELFv2ABI() const {
return (getArch() == Triple::ppc64 &&
((getOS() == Triple::FreeBSD &&
(getOSMajorVersion() >= 13 || getOSVersion().empty())) ||
getOS() == Triple::OpenBSD || isMusl()));
}

/// Tests whether the target is 32-bit RISC-V.
bool isRISCV32() const { return getArch() == Triple::riscv32; }

Expand Down
5 changes: 4 additions & 1 deletion llvm/lib/Target/PowerPC/PPCTargetMachine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,10 @@ static PPCTargetMachine::PPCABI computeTargetABI(const Triple &TT,
case Triple::ppc64le:
return PPCTargetMachine::PPC_ABI_ELFv2;
case Triple::ppc64:
return PPCTargetMachine::PPC_ABI_ELFv1;
if (TT.isPPC64ELFv2ABI())
return PPCTargetMachine::PPC_ABI_ELFv2;
else
return PPCTargetMachine::PPC_ABI_ELFv1;
default:
return PPCTargetMachine::PPC_ABI_UNKNOWN;
}
Expand Down
7 changes: 6 additions & 1 deletion llvm/test/CodeGen/PowerPC/ppc64-elf-abi.ll
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@
; RUN: llc -verify-machineinstrs -mtriple=powerpc64le-unknown-linux-gnu -target-abi elfv1 < %s | FileCheck %s -check-prefix=CHECK-ELFv1
; RUN: llc -verify-machineinstrs -mtriple=powerpc64le-unknown-linux-gnu -target-abi elfv2 < %s | FileCheck %s -check-prefix=CHECK-ELFv2

; RUN: llc -verify-machineinstrs -mtriple=powerpc64-unknown-freebsd < %s | FileCheck %s -check-prefix=CHECK-ELFv1
; RUN: llc -verify-machineinstrs -mtriple=powerpc64-linux-musl < %s | FileCheck %s -check-prefix=CHECK-ELFv2

; RUN: llc -verify-machineinstrs -mtriple=powerpc64-unknown-freebsd12 < %s | FileCheck %s -check-prefix=CHECK-ELFv1
; RUN: llc -verify-machineinstrs -mtriple=powerpc64-unknown-freebsd < %s | FileCheck %s -check-prefix=CHECK-ELFv2
; RUN: llc -verify-machineinstrs -mtriple=powerpc64-unknown-freebsd -target-abi elfv1 < %s | FileCheck %s -check-prefix=CHECK-ELFv1
; RUN: llc -verify-machineinstrs -mtriple=powerpc64-unknown-freebsd -target-abi elfv2 < %s | FileCheck %s -check-prefix=CHECK-ELFv2

; RUN: llc -verify-machineinstrs -mtriple=powerpc64-unknown-openbsd < %s | FileCheck %s -check-prefix=CHECK-ELFv2

; CHECK-ELFv2: .abiversion 2
; CHECK-ELFv1-NOT: .abiversion 2
2 changes: 1 addition & 1 deletion llvm/test/CodeGen/PowerPC/pr47373.ll
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
; RUN: llc -mtriple=powerpc64-unknown-freebsd13.0 -verify-machineinstrs \
; RUN: llc -mtriple=powerpc64-unknown-freebsd12.0 -verify-machineinstrs \
; RUN: -mcpu=ppc64 -ppc-asm-full-reg-names < %s | FileCheck %s
@a = local_unnamed_addr global ptr null, align 8

Expand Down

0 comments on commit 4b09cb2

Please sign in to comment.