Skip to content
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

wip -- emitFunctionProlog #1229

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
11 changes: 11 additions & 0 deletions clang/include/clang/CIR/MissingFeatures.h
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,17 @@ struct MissingFeatures {
static bool mustProgress() { return false; }

static bool skipTempCopy() { return false; }

static bool addressSpaceInGlobalVar() { return false; }

static bool useARMGuardVarABI() { return false; }

// PtrAuth added a RawAddress type subclassing from Address.
static bool rawAddress() { return false; }

// LLVM has values that can be named (e.g. %x) and MLIR doens't, add this when
// we have a solution
static bool namedValues() { return false; }
};

} // namespace cir
Expand Down
224 changes: 224 additions & 0 deletions clang/lib/CIR/CodeGen/CIRGenCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,13 @@
#include "TargetInfo.h"

#include "clang/AST/Attr.h"
#include "clang/AST/Attrs.inc"
#include "clang/AST/Decl.h"
#include "clang/AST/DeclBase.h"
#include "clang/AST/DeclCXX.h"
#include "clang/AST/GlobalDecl.h"
#include "clang/Basic/Specifiers.h"
#include "clang/CIR/ABIArgInfo.h"
#include "clang/CIR/Dialect/IR/CIRDialect.h"
#include "clang/CIR/Dialect/IR/CIRTypes.h"
#include "clang/CIR/FnInfoOpts.h"
Expand Down Expand Up @@ -1110,6 +1115,191 @@ static CanQual<FunctionProtoType> GetFormalType(const CXXMethodDecl *MD) {
.getAs<FunctionProtoType>();
}

void CIRGenFunction::emitFunctionProlog(const CIRGenFunctionInfo &functionInfo,
cir::FuncOp fn,
const FunctionArgList &args) {
return;
if (CurCodeDecl && CurCodeDecl->hasAttr<NakedAttr>())
// Naked functions don't have prologues.
return;

// If this is an implicit-return-zero function, go ahead and initialize the
// return value. TODO: it might be nice to have a more general mechanism for
// this that didn't require synthesized return statements.
if (const FunctionDecl *fd = dyn_cast_or_null<FunctionDecl>(CurCodeDecl)) {
if (fd->hasImplicitReturnZero())
llvm_unreachable("NYI");
}

// FIXME: We no longer need the types from FunctionArgList; lift up and
// simplify.

ClangToCIRArgMapping cirFunctionArgs(CGM.getASTContext(), functionInfo);
assert(fn.getArguments().size() == cirFunctionArgs.totalCIRArgs());

// If we're using inalloca, all the memory arguments are GEPs off of the last
// parameter, which is a pointer to the complete memory area.
Address argStruct = Address::invalid();
if (cirFunctionArgs.hasInallocaArg())
llvm_unreachable("NYI");

// Name the struct return parameter.
if (cirFunctionArgs.hasSRetArg()) {
llvm_unreachable("NYI");
}

// Track if we received the parameter as a pointer (indirect, byval, or
// inalloca). If already have a pointer, buildParmDecl doesn't need to copy it
// into a local alloca for us.
llvm::SmallVector<ParamValue, 16> argVals;
argVals.reserve(args.size());

// Create a pointer value for every parameter declaration. This usually
// entails copying one or more CIR arguments into an alloca. Don't push any
// cleanups or do anything that might unwind. We do that separately, so we can
// push the cleanups in the correct order for the ABI.
assert(functionInfo.arg_size() == args.size() &&
"Mismatch between function signature & arguments.");
unsigned argNo = 0;
CIRGenFunctionInfo::const_arg_iterator info_it = functionInfo.arg_begin();
for (FunctionArgList::const_iterator i = args.begin(), e = args.end(); i != e;
++i, ++info_it, ++argNo) {
const VarDecl *arg = *i;
const cir::ABIArgInfo &argI = info_it->info;

bool isPromoted =
isa<ParmVarDecl>(arg) && cast<ParmVarDecl>(arg)->isKNRPromoted();
// We are converting from ABIArgInfo type to VarDecl type directly, unless
// the parameter is promoted. In this case we convert to
// CIRGenFunctionInfo::ArgInfo type with subsequent argument demotion.
QualType ty = isPromoted ? info_it->type : arg->getType();
assert(hasScalarEvaluationKind(ty) ==
hasScalarEvaluationKind(arg->getType()));

unsigned firstCIRArg, numCIRArgs;
std::tie(firstCIRArg, numCIRArgs) = cirFunctionArgs.getCIRArgs(argNo);

switch (argI.getKind()) {
case cir::ABIArgInfo::InAlloca:
llvm_unreachable("NYI");
case cir::ABIArgInfo::Indirect:
llvm_unreachable("NYI");
case cir::ABIArgInfo::IndirectAliased:
llvm_unreachable("NYI");
case cir::ABIArgInfo::Extend:
llvm_unreachable("NYI");
case cir::ABIArgInfo::Direct: {
auto ai = fn.getArgument(firstCIRArg);
mlir::Type lTy = convertType(arg->getType());

// Prepare parameter attributes. So far, only attributes for pointer
// parameters are prepared.
if (argI.getDirectOffset() == 0 && isa<cir::PointerType>(lTy) &&
isa<cir::PointerType>(argI.getCoerceToType())) {
llvm_unreachable("NYI");
}

// Prepare the argument value. If we have the trivial case, handle it with
// no muss and fuss.
if (!isa<cir::StructType>(argI.getCoerceToType()) &&
argI.getCoerceToType() == convertType(ty) &&
argI.getDirectOffset() == 0) {
assert(numCIRArgs == 1);

// LLVM expects swifterror parameters to be used in very restricted
// ways. Copy the value into a less-restricted temporary.
mlir::Value v = ai;
if (functionInfo.getExtParameterInfo(argNo).getABI() ==
ParameterABI::SwiftErrorResult) {
llvm_unreachable("NYI");
}

// Ensure the argument is the correct type.
if (v.getType() != argI.getCoerceToType())
llvm_unreachable("NYI");

if (isPromoted)
llvm_unreachable("NYI");

// Because of merging of function types from multiple decls it is
// possible for the type of an argument to not match the corresponding
// type in the function type. Since we are codegenning the callee in
// here, and a cast to the argument type.
mlir::Type lTy = convertType(arg->getType());
if (v.getType() != lTy)
llvm_unreachable("NYI");

argVals.push_back(ParamValue::forDirect(v));
}

// VLST arguments are coerced to VLATs at the function boundary for ABI
// consistency. If this is a VLST that was coerced to a VLAT at the
// function boundary and hte types match up, use the CIR equivalent of
// llvm.vector.extract to convert back to the original VLST.
if (cir::MissingFeatures::vectorType())
llvm_unreachable("NYI");

cir::StructType sTy = dyn_cast<cir::StructType>(argI.getCoerceToType());
if (argI.isDirect() && !argI.getCanBeFlattened() && sTy &&
sTy.getNumElements() > 1) {
llvm_unreachable("NYI");
}

Address alloca =
CreateMemTemp(ty, getContext().getDeclAlign(arg),
getLoc(arg->getLocation()), arg->getName());

// Pointer to store info.
Address ptr = emitAddressAtOffset(*this, alloca, argI);

// Fast-isel and the LLVM optimizer generally like scalar values better
// than FCAs, so we flatten them if this is safe to do for this argument.
if (argI.isDirect() && argI.getCanBeFlattened() && sTy &&
sTy.getNumElements() > 1) {
llvm_unreachable("NYI");
} else {
// Simple cast, just do a coerced store of the argument into the alloca.
assert(numCIRArgs == 1);
auto ai = fn.getArgument(firstCIRArg);
// ai.setName(arg->getName() + ".coerce");
createCoercedStore(
ai, ptr,
llvm::TypeSize::getFixed(
getContext().getTypeSizeInChars(ty).getQuantity() -
argI.getDirectOffset()),
/*dstIsVolatile=*/false);
}

// Match to what buildParmDecl is expecting for this type.
if (CIRGenFunction::hasScalarEvaluationKind(ty)) {
mlir::Value v = emitLoadOfScalar(alloca, false, ty, arg->getBeginLoc());
if (isPromoted)
llvm_unreachable("NYI");
argVals.push_back(ParamValue::forDirect(v));
} else {
llvm_unreachable("NYI");
}
break;
}

case cir::ABIArgInfo::CoerceAndExpand:
llvm_unreachable("NYI");
case cir::ABIArgInfo::Expand:
llvm_unreachable("NYI");
case cir::ABIArgInfo::Ignore:
llvm_unreachable("NYI");
}
}

if (getTarget().getCXXABI().areArgsDestroyedLeftToRightInCallee()) {
for (int i = args.size() - 1; i >= 0; --i)
emitParmDecl(*args[i], argVals[i], i + 1);
} else {
for (unsigned i = 0, e = args.size(); i != e; ++i)
emitParmDecl(*args[i], argVals[i], i + 1);
}
}

/// TODO(cir): this should be shared with LLVM codegen
static void addExtParameterInfosForCall(
llvm::SmallVectorImpl<FunctionProtoType::ExtParameterInfo> &paramInfos,
Expand Down Expand Up @@ -1638,3 +1828,37 @@ void CIRGenModule::getDefaultFunctionAttributes(
// TODO(cir): addMergableDefaultFunctionAttributes(codeGenOpts, funcAttrs);
}
}

void CIRGenFunction::createCoercedStore(mlir::Value src, Address dst,
llvm::TypeSize dstSize,
bool dstIsVolatile) {
if (!dstSize)
return;

mlir::Type srcTy = src.getType();
llvm::TypeSize srcSize = CGM.getDataLayout().getTypeAllocSize(srcTy);

// GEP into structs to try to make typesm match.
// FIXME: This isn't really that useful with opaque types, but it impacts a
// lot of regressiont ests.
if (srcTy != dst.getElementType()) {
llvm_unreachable("NYI");
}

if (srcSize.isScalable() || srcSize <= dstSize) {
if (isa<cir::IntType>(srcTy) &&
isa<cir::PointerType>(dst.getElementType()) &&
srcSize == CGM.getDataLayout().getTypeAllocSize(dst.getElementType())) {
llvm_unreachable("NYI");
} else if (cir::StructType sTy = dyn_cast<cir::StructType>(src.getType())) {
llvm_unreachable("NYI");
} else {
getBuilder().createStore(src.getLoc(), src, dst.withElementType(srcTy),
dstIsVolatile);
}
} else if (isa<cir::IntType>(srcTy)) {
llvm_unreachable("NYI");
} else {
llvm_unreachable("NYI");
}
}
81 changes: 81 additions & 0 deletions clang/lib/CIR/CodeGen/CIRGenDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1263,3 +1263,84 @@ void CIRGenFunction::pushDestroyAndDeferDeactivation(
pushDestroy(cleanupKind, addr, type, destroyer, useEHCleanupForArray);
DeferredDeactivationCleanupStack.push_back({EHStack.stable_begin(), flag});
}

/// Emit an alloca (or GlobalValue depending on target)
/// for the specified parameter and set up LocalDeclMap.
void CIRGenFunction::emitParmDecl(const VarDecl &varDecl, ParamValue arg,
unsigned argNo) {
bool noDebugInfo = false;
// FIXME: Why isn't ImplicitParamDecl a ParmVarDecl?
assert((isa<ParmVarDecl>(varDecl) || isa<ImplicitParamDecl>(varDecl)) &&
"Invalid argument to buildParmDecl");

// Set the name of the parameter's initial value to make IR easier to read.
// Don't modify the names of globals.
if (cir::MissingFeatures::namedValues())
llvm_unreachable("NYI");

QualType ty = varDecl.getType();

// Use better CIR generation for certain implicit parameters.
if ([[maybe_unused]] auto const *ipd =
dyn_cast<ImplicitParamDecl>(&varDecl)) {
llvm_unreachable("NYI");
}

Address declPtr = Address::invalid();
assert(!cir::MissingFeatures::rawAddress());
Address allocaPtr = Address::invalid();
bool doStore = false;
bool isScalar = hasScalarEvaluationKind(ty);
bool useIndirectDebugAddress = false;

// If we already have a pointer to the argument, reuse the input pointer.
if (arg.isIndirect()) {
llvm_unreachable("NYI");
} else {
// Check if the parameter address is controlled by OpenMP runtime.
Address openMPLocalAddr =
getLangOpts().OpenMP
? CGM.getOpenMPRuntime().getAddressOfLocalVariable(*this, &varDecl)
: Address::invalid();
if (getLangOpts().OpenMP && openMPLocalAddr.isValid()) {
llvm_unreachable("NYI");
} else {
// Otherwise, create a temporary to hold the value.
declPtr = CreateMemTemp(ty, getContext().getDeclAlign(&varDecl),
getLoc(varDecl.getLocation()),
varDecl.getName() + ".addr", &allocaPtr);
}
doStore = true;
}

mlir::Value argVal = (doStore ? arg.getDirectValue() : nullptr);

LValue lv = makeAddrLValue(declPtr, ty);
if (isScalar) {
Qualifiers qs = ty.getQualifiers();
if ([[maybe_unused]] Qualifiers::ObjCLifetime lt = qs.getObjCLifetime()) {
llvm_unreachable("NYI");
}
}

// Store the initial value into the alloca.
if (doStore)
emitStoreOfScalar(argVal, lv, /*isInit=*/true);

setAddrOfLocalVar(&varDecl, declPtr);

// Emit debug info for param declarations in non-thunk functions.
if (CIRGenDebugInfo *di = getDebugInfo()) {
llvm_unreachable("NYI");
}

if (varDecl.hasAttr<AnnotateAttr>())
llvm_unreachable("NYI");

// We can only check return value nullability if all arguments to the function
// staisfy their nullability preconditions. This makes it necessary to emit
// null checks for args in the function body itself.
if (requiresReturnValueNullabilityCheck()) {
llvm_unreachable("NYI");
}
}
2 changes: 1 addition & 1 deletion clang/lib/CIR/CodeGen/CIRGenFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1217,7 +1217,7 @@ void CIRGenFunction::StartFunction(GlobalDecl GD, QualType RetTy,
llvm_unreachable("NYI");
}

// TODO: emitFunctionProlog
emitFunctionProlog(*CurFnInfo, cast<cir::FuncOp>(CurFn), Args);

{
// Set the insertion point in the builder to the beginning of the
Expand Down
Loading
Loading