Skip to content

Commit

Permalink
[CIR] Add minimal support for Darwin aarch64 triples
Browse files Browse the repository at this point in the history
  • Loading branch information
lanza committed Jun 20, 2024
1 parent 7812ed3 commit e5d840b
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 9 deletions.
1 change: 1 addition & 0 deletions clang/lib/CIR/ABIInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace cir {

class ABIArgInfo;
class CIRGenCXXABI;
class CIRGenFunctionInfo;
class CIRGenTypes;
Expand Down
5 changes: 5 additions & 0 deletions clang/lib/CIR/CIRGenItaniumCXXABI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,11 @@ CIRGenCXXABI *cir::CreateCIRGenItaniumCXXABI(CIRGenModule &CGM) {
assert(CGM.getASTContext().getTargetInfo().getTriple().getArch() !=
llvm::Triple::le32 &&
"le32 NYI");
LLVM_FALLTHROUGH;
case TargetCXXABI::GenericAArch64:
case TargetCXXABI::AppleARM64:
// TODO: this isn't quite right, clang uses AppleARM64CXXABI which inherits
// from ARMCXXABI. We'll have to follow suit.
return new CIRGenItaniumCXXABI(CGM);

default:
Expand Down
2 changes: 2 additions & 0 deletions clang/lib/CIR/CIRGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ using llvm::StringRef;
static CIRGenCXXABI *createCXXABI(CIRGenModule &CGM) {
switch (CGM.getASTContext().getCXXABIKind()) {
case TargetCXXABI::GenericItanium:
case TargetCXXABI::GenericAArch64:
case TargetCXXABI::AppleARM64:
return CreateCIRGenItaniumCXXABI(CGM);
default:
llvm_unreachable("invalid C++ ABI kind");
Expand Down
81 changes: 72 additions & 9 deletions clang/lib/CIR/TargetInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,69 @@
using namespace cir;
using namespace clang;

static bool testIfIsVoidTy(QualType Ty) {
const auto *BT = Ty->getAs<BuiltinType>();
if (!BT)
return false;

BuiltinType::Kind k = BT->getKind();
return k == BuiltinType::Void;
}

//===----------------------------------------------------------------------===//
// AArch64 ABI Implementation
//===----------------------------------------------------------------------===//

namespace {

class AArch64ABIInfo : public ABIInfo {
public:
enum ABIKind { AAPCS = 0, DarwinPCS, Win64 };

private:
ABIKind Kind;

public:
AArch64ABIInfo(CIRGenTypes &CGT, ABIKind Kind) : ABIInfo(CGT), Kind(Kind) {}

private:
ABIKind getABIKind() const { return Kind; }
bool isDarwinPCS() const { return Kind == DarwinPCS; }

ABIArgInfo classifyReturnType(QualType RetTy, bool IsVariadic) const;
ABIArgInfo classifyArgumentType(QualType RetTy, bool IsVariadic,
unsigned CallingConvention) const;

void computeInfo(CIRGenFunctionInfo &FI) const override {
// Top leevl CIR has unlimited arguments and return types. Lowering for ABI
// specific concerns should happen during a lowering phase. Assume
// everything is direct for now.
for (CIRGenFunctionInfo::arg_iterator it = FI.arg_begin(),
ie = FI.arg_end();
it != ie; ++it) {
if (testIfIsVoidTy(it->type))
it->info = ABIArgInfo::getIgnore();
else
it->info = ABIArgInfo::getDirect(CGT.ConvertType(it->type));
}
auto RetTy = FI.getReturnType();
if (testIfIsVoidTy(RetTy))
FI.getReturnInfo() = ABIArgInfo::getIgnore();
else
FI.getReturnInfo() = ABIArgInfo::getDirect(CGT.ConvertType(RetTy));

return;
}
};

class AArch64TargetCIRGenInfo : public TargetCIRGenInfo {
public:
AArch64TargetCIRGenInfo(CIRGenTypes &CGT, AArch64ABIInfo::ABIKind Kind)
: TargetCIRGenInfo(std::make_unique<AArch64ABIInfo>(CGT, Kind)) {}
};

} // namespace

namespace {

/// The AVX ABI leel for X86 targets.
Expand Down Expand Up @@ -114,15 +177,6 @@ ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty,
assert(false && "NYI");
}

static bool testIfIsVoidTy(QualType Ty) {
const auto *BT = Ty->getAs<BuiltinType>();
if (!BT)
return false;

BuiltinType::Kind k = BT->getKind();
return k == BuiltinType::Void;
}

void X86_64ABIInfo::computeInfo(CIRGenFunctionInfo &FI) const {
// Top leevl CIR has unlimited arguments and return types. Lowering for ABI
// specific concerns should happen during a lowering phase. Assume everything
Expand Down Expand Up @@ -423,6 +477,15 @@ const TargetCIRGenInfo &CIRGenModule::getTargetCIRGenInfo() {
switch (Triple.getArch()) {
default:
assert(false && "Target not yet supported!");
case llvm::Triple::aarch64: {
AArch64ABIInfo::ABIKind Kind = AArch64ABIInfo::AAPCS;
assert(getTarget().getABI() == "aapcs" ||
getTarget().getABI() == "darwinpcs" &&
"Only Darwin supported for aarch64");
Kind = AArch64ABIInfo::DarwinPCS;
return SetCIRGenInfo(new AArch64TargetCIRGenInfo(genTypes, Kind));
}

case llvm::Triple::x86_64: {
StringRef ABI = getTarget().getABI();
X86AVXABILevel AVXLevel = (ABI == "avx512" ? X86AVXABILevel::AVX512
Expand Down
1 change: 1 addition & 0 deletions clang/test/CIR/driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// RUN: %clang -target x86_64-unknown-linux-gnu -fclangir -c %s -o %t.o
// RUN: llvm-objdump -d %t.o | FileCheck %s -check-prefix=OBJ
// RUN: %clang -target x86_64-unknown-linux-gnu -fclangir -disable-cir-passes -S -emit-cir %s -o %t.cir
// RUN: %clang -target arm64-apple-macosx12.0.0 -fclangir -S -emit-cir %s -o %t.cir
// RUN: FileCheck --input-file=%t.cir %s -check-prefix=CIR
// XFAIL: *

Expand Down

0 comments on commit e5d840b

Please sign in to comment.