Skip to content

Commit

Permalink
[CIR][LowerToLLVM][CXXABI] Lower cir.va.arg (llvm#573)
Browse files Browse the repository at this point in the history
lowering var_arg op for ARM64 architecture. This is CIR lowering.

This PR modified LoweringPrepare CXXABI code to make
LoweringPrepareArm64CXXABI class inherit more generic
LoweringPrepareItaniumCXXABI, this way lowering var_arg would be only
meaningful for arm64 targets and for other arch its no op for now.

The ABI doc and detailed algorithm description can be found in this
official doc.

[](https://github.com/ARM-software/abi-aa/blob/617079d8a0d45bec83d351974849483cf0cc66d5/aapcs64/aapcs64.rst#appendix-variable-argument-lists)
  • Loading branch information
ghehg authored and lanza committed Oct 1, 2024
1 parent 559ca3a commit 34d9461
Show file tree
Hide file tree
Showing 11 changed files with 709 additions and 32 deletions.
13 changes: 13 additions & 0 deletions clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,19 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
return createCast(mlir::cir::CastKind::int_to_ptr, src, newTy);
}

mlir::Value createGetMemberOp(mlir::Location &loc, mlir::Value structPtr,
const char *fldName, unsigned idx) {

assert(structPtr.getType().isa<mlir::cir::PointerType>());
auto structBaseTy =
structPtr.getType().cast<mlir::cir::PointerType>().getPointee();
assert(structBaseTy.isa<mlir::cir::StructType>());
auto fldTy = structBaseTy.cast<mlir::cir::StructType>().getMembers()[idx];
auto fldPtrTy = ::mlir::cir::PointerType::get(getContext(), fldTy);
return create<mlir::cir::GetMemberOp>(loc, fldPtrTy, structPtr, fldName,
idx);
}

mlir::Value createPtrToInt(mlir::Value src, mlir::Type newTy) {
return createCast(mlir::cir::CastKind::ptr_to_int, src, newTy);
}
Expand Down
9 changes: 9 additions & 0 deletions clang/lib/CIR/Dialect/IR/MissingFeatures.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ struct MissingFeatures {
// C++ ABI support
static bool cxxABI() { return false; }
static bool setCallingConv() { return false; }
static bool handleBigEndian() { return false; }
static bool handleAArch64Indirect() { return false; }
static bool classifyArgumentTypeForAArch64() { return false; }
static bool supportgetCoerceToTypeForAArch64() { return false; }
static bool supportTySizeQueryForAArch64() { return false; }
static bool supportTyAlignQueryForAArch64() { return false; }
static bool supportisHomogeneousAggregateQueryForAArch64() { return false; }
static bool supportisEndianQueryForAArch64() { return false; }
static bool supportisAggregateTypeForABIAArch64() { return false; }

// Address space related
static bool addressSpace() { return false; }
Expand Down
1 change: 1 addition & 0 deletions clang/lib/CIR/Dialect/Transforms/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ add_clang_library(MLIRCIRTransforms
LifetimeCheck.cpp
LoweringPrepare.cpp
LoweringPrepareItaniumCXXABI.cpp
LoweringPrepareAArch64CXXABI.cpp
MergeCleanups.cpp
DropAST.cpp
IdiomRecognizer.cpp
Expand Down
40 changes: 34 additions & 6 deletions clang/lib/CIR/Dialect/Transforms/LoweringPrepare.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
#include "clang/AST/CharUnits.h"
#include "clang/AST/Mangle.h"
#include "clang/Basic/Module.h"
#include "clang/Basic/TargetInfo.h"
#include "clang/CIR/Dialect/Builder/CIRBaseBuilder.h"
#include "clang/CIR/Dialect/IR/CIRDataLayout.h"
#include "clang/CIR/Dialect/IR/CIRDialect.h"
#include "clang/CIR/Dialect/Passes.h"
#include "clang/CIR/Interfaces/ASTAttrInterfaces.h"
Expand Down Expand Up @@ -70,6 +72,7 @@ struct LoweringPreparePass : public LoweringPrepareBase<LoweringPreparePass> {

void runOnOp(Operation *op);
void lowerThreeWayCmpOp(CmpThreeWayOp op);
void lowerVAArgOp(VAArgOp op);
void lowerGlobalOp(GlobalOp op);
void lowerDynamicCastOp(DynamicCastOp op);
void lowerStdFindOp(StdFindOp op);
Expand Down Expand Up @@ -108,15 +111,24 @@ struct LoweringPreparePass : public LoweringPrepareBase<LoweringPreparePass> {

void setASTContext(clang::ASTContext *c) {
astCtx = c;
auto abiStr = c->getTargetInfo().getABI();
switch (c->getCXXABIKind()) {
case clang::TargetCXXABI::GenericItanium:
cxxABI.reset(::cir::LoweringPrepareCXXABI::createItaniumABI());
break;
case clang::TargetCXXABI::GenericAArch64:
case clang::TargetCXXABI::AppleARM64:
// TODO: this isn't quite right, clang uses AppleARM64CXXABI which
// inherits from ARMCXXABI. We'll have to follow suit.
cxxABI.reset(::cir::LoweringPrepareCXXABI::createItaniumABI());
// TODO: This is temporary solution. ABIKind info should be
// propagated from the targetInfo managed by ABI lowering
// query system.
assert(abiStr == "aapcs" || abiStr == "darwinpcs" ||
abiStr == "aapcs-soft");
cxxABI.reset(::cir::LoweringPrepareCXXABI::createAArch64ABI(
abiStr == "aapcs"
? ::cir::AArch64ABIKind::AAPCS
: (abiStr == "darwinpccs" ? ::cir::AArch64ABIKind::DarwinPCS
: ::cir::AArch64ABIKind::AAPCSSoft)));
break;

default:
llvm_unreachable("NYI");
}
Expand Down Expand Up @@ -320,6 +332,19 @@ static void canonicalizeIntrinsicThreeWayCmp(CIRBaseBuilderTy &builder,
op.erase();
}

void LoweringPreparePass::lowerVAArgOp(VAArgOp op) {
CIRBaseBuilderTy builder(getContext());
builder.setInsertionPoint(op);
::cir::CIRDataLayout datalayout(theModule);

auto res = cxxABI->lowerVAArg(builder, op, datalayout);
if (res) {
op.replaceAllUsesWith(res);
op.erase();
}
return;
}

void LoweringPreparePass::lowerThreeWayCmpOp(CmpThreeWayOp op) {
CIRBaseBuilderTy builder(getContext());
builder.setInsertionPointAfter(op);
Expand Down Expand Up @@ -603,6 +628,8 @@ void LoweringPreparePass::lowerIterEndOp(IterEndOp op) {
void LoweringPreparePass::runOnOp(Operation *op) {
if (auto threeWayCmp = dyn_cast<CmpThreeWayOp>(op)) {
lowerThreeWayCmpOp(threeWayCmp);
} else if (auto vaArgOp = dyn_cast<VAArgOp>(op)) {
lowerVAArgOp(vaArgOp);
} else if (auto getGlobal = dyn_cast<GlobalOp>(op)) {
lowerGlobalOp(getGlobal);
} else if (auto dynamicCast = dyn_cast<DynamicCastOp>(op)) {
Expand Down Expand Up @@ -635,8 +662,9 @@ void LoweringPreparePass::runOnOperation() {

SmallVector<Operation *> opsToTransform;
op->walk([&](Operation *op) {
if (isa<CmpThreeWayOp, GlobalOp, DynamicCastOp, StdFindOp, IterEndOp,
IterBeginOp, ArrayCtor, ArrayDtor, mlir::cir::FuncOp>(op))
if (isa<CmpThreeWayOp, VAArgOp, GlobalOp, DynamicCastOp, StdFindOp,
IterEndOp, IterBeginOp, ArrayCtor, ArrayDtor, mlir::cir::FuncOp>(
op))
opsToTransform.push_back(op);
});

Expand Down
Loading

0 comments on commit 34d9461

Please sign in to comment.