Skip to content

[CIR] cir.call with scalar return type #135552

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

Merged
merged 1 commit into from
Apr 17, 2025
Merged
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
92 changes: 92 additions & 0 deletions clang/include/clang/CIR/ABIArgInfo.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
//==-- ABIArgInfo.h - Abstract info regarding ABI-specific arguments -------==//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// Defines ABIArgInfo and associated types used by CIR to track information
// regarding ABI-coerced types for function arguments and return values. This
// was moved to the common library as it might be used by both CIRGen and
// passes.
//
//===----------------------------------------------------------------------===//

#ifndef CLANG_CIR_ABIARGINFO_H
#define CLANG_CIR_ABIARGINFO_H

#include "mlir/IR/Types.h"
#include "clang/CIR/MissingFeatures.h"

namespace cir {

class ABIArgInfo {
public:
enum Kind : uint8_t {
/// Pass the argument directly using the normal converted CIR type,
/// or by coercing to another specified type stored in 'CoerceToType'). If
/// an offset is specified (in UIntData), then the argument passed is offset
/// by some number of bytes in the memory representation. A dummy argument
/// is emitted before the real argument if the specified type stored in
/// "PaddingType" is not zero.
Direct,

/// Ignore the argument (treat as void). Useful for void and empty
/// structs.
Ignore,

// TODO: more argument kinds will be added as the upstreaming proceeds.
};

private:
mlir::Type typeData;
struct DirectAttrInfo {
unsigned offset;
unsigned align;
};
union {
DirectAttrInfo directAttr;
};
Kind theKind;

public:
ABIArgInfo(Kind k = Direct) : directAttr{0, 0}, theKind(k) {}

static ABIArgInfo getDirect(mlir::Type ty = nullptr) {
ABIArgInfo info(Direct);
info.setCoerceToType(ty);
assert(!cir::MissingFeatures::abiArgInfo());
return info;
}

static ABIArgInfo getIgnore() { return ABIArgInfo(Ignore); }

Kind getKind() const { return theKind; }
bool isDirect() const { return theKind == Direct; }
bool isIgnore() const { return theKind == Ignore; }

bool canHaveCoerceToType() const {
assert(!cir::MissingFeatures::abiArgInfo());
return isDirect();
}

unsigned getDirectOffset() const {
assert(!cir::MissingFeatures::abiArgInfo());
return directAttr.offset;
}

mlir::Type getCoerceToType() const {
assert(canHaveCoerceToType() && "invalid kind!");
return typeData;
}

void setCoerceToType(mlir::Type ty) {
assert(canHaveCoerceToType() && "invalid kind!");
typeData = ty;
}
};

} // namespace cir

#endif // CLANG_CIR_ABIARGINFO_H
8 changes: 5 additions & 3 deletions clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -207,13 +207,15 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
// Call operators
//===--------------------------------------------------------------------===//

cir::CallOp createCallOp(mlir::Location loc, mlir::SymbolRefAttr callee) {
auto op = create<cir::CallOp>(loc, callee);
cir::CallOp createCallOp(mlir::Location loc, mlir::SymbolRefAttr callee,
mlir::Type returnType) {
auto op = create<cir::CallOp>(loc, callee, returnType);
return op;
}

cir::CallOp createCallOp(mlir::Location loc, cir::FuncOp callee) {
return createCallOp(loc, mlir::SymbolRefAttr::get(callee));
return createCallOp(loc, mlir::SymbolRefAttr::get(callee),
callee.getFunctionType().getReturnType());
}

//===--------------------------------------------------------------------===//
Expand Down
6 changes: 5 additions & 1 deletion clang/include/clang/CIR/Dialect/IR/CIROps.td
Original file line number Diff line number Diff line change
Expand Up @@ -1408,10 +1408,14 @@ def CallOp : CIR_CallOpBase<"call", [NoRegionArguments]> {
```
}];

let results = (outs Optional<CIR_AnyType>:$result);
let arguments = commonArgs;

let builders = [OpBuilder<(ins "mlir::SymbolRefAttr":$callee), [{
let builders = [OpBuilder<(ins "mlir::SymbolRefAttr":$callee,
"mlir::Type":$resType), [{
$_state.addAttribute("callee", callee);
if (resType && !isa<VoidType>(resType))
$_state.addTypes(resType);
}]>];
}

Expand Down
5 changes: 5 additions & 0 deletions clang/include/clang/CIR/MissingFeatures.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ struct MissingFeatures {

// Misc
static bool cxxABI() { return false; }
static bool cirgenABIInfo() { return false; }
static bool cirgenTargetInfo() { return false; }
static bool abiArgInfo() { return false; }
static bool tryEmitAsConstant() { return false; }
static bool constructABIArgDirectExtend() { return false; }
static bool opGlobalViewAttr() { return false; }
Expand All @@ -132,6 +135,8 @@ struct MissingFeatures {
static bool fpConstraints() { return false; }
static bool sanitizers() { return false; }
static bool addHeapAllocSiteMetadata() { return false; }
static bool targetCIRGenInfoArch() { return false; }
static bool targetCIRGenInfoOS() { return false; }
static bool targetCodeGenInfoGetNullPointer() { return false; }
static bool loopInfoStack() { return false; }
static bool requiresCleanups() { return false; }
Expand Down
32 changes: 32 additions & 0 deletions clang/lib/CIR/CodeGen/ABIInfo.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//===----- ABIInfo.h - ABI information access & encapsulation ---*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_CLANG_LIB_CIR_ABIINFO_H
#define LLVM_CLANG_LIB_CIR_ABIINFO_H

namespace clang::CIRGen {

class CIRGenFunctionInfo;
class CIRGenTypes;

class ABIInfo {
ABIInfo() = delete;

public:
CIRGenTypes &cgt;

ABIInfo(CIRGenTypes &cgt) : cgt(cgt) {}

virtual ~ABIInfo();

virtual void computeInfo(CIRGenFunctionInfo &funcInfo) const = 0;
};

} // namespace clang::CIRGen

#endif // LLVM_CLANG_LIB_CIR_ABIINFO_H
79 changes: 69 additions & 10 deletions clang/lib/CIR/CodeGen/CIRGenCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@
using namespace clang;
using namespace clang::CIRGen;

CIRGenFunctionInfo *CIRGenFunctionInfo::create() {
// For now we just create an empty CIRGenFunctionInfo.
CIRGenFunctionInfo *fi = new CIRGenFunctionInfo();
CIRGenFunctionInfo *CIRGenFunctionInfo::create(CanQualType resultType) {
void *buffer = operator new(totalSizeToAlloc<ArgInfo>(1));

CIRGenFunctionInfo *fi = new (buffer) CIRGenFunctionInfo();
fi->getArgsBuffer()[0].type = resultType;

return fi;
}

Expand All @@ -29,13 +32,29 @@ CIRGenCallee CIRGenCallee::prepareConcreteCallee(CIRGenFunction &cgf) const {
return *this;
}

static const CIRGenFunctionInfo &arrangeFreeFunctionLikeCall(CIRGenTypes &cgt) {
static const CIRGenFunctionInfo &
arrangeFreeFunctionLikeCall(CIRGenTypes &cgt, CIRGenModule &cgm,
const FunctionType *fnType) {
if (const auto *proto = dyn_cast<FunctionProtoType>(fnType)) {
if (proto->isVariadic())
cgm.errorNYI("call to variadic function");
if (proto->hasExtParameterInfos())
cgm.errorNYI("call to functions with extra parameter info");
} else if (cgm.getTargetCIRGenInfo().isNoProtoCallVariadic(
cast<FunctionNoProtoType>(fnType)))
cgm.errorNYI("call to function without a prototype");

assert(!cir::MissingFeatures::opCallArgs());
return cgt.arrangeCIRFunctionInfo();

CanQualType retType = fnType->getReturnType()
->getCanonicalTypeUnqualified()
.getUnqualifiedType();
return cgt.arrangeCIRFunctionInfo(retType);
}

const CIRGenFunctionInfo &CIRGenTypes::arrangeFreeFunctionCall() {
return arrangeFreeFunctionLikeCall(*this);
const CIRGenFunctionInfo &
CIRGenTypes::arrangeFreeFunctionCall(const FunctionType *fnType) {
return arrangeFreeFunctionLikeCall(*this, cgm, fnType);
}

static cir::CIRCallOpInterface emitCallLikeOp(CIRGenFunction &cgf,
Expand All @@ -54,8 +73,12 @@ static cir::CIRCallOpInterface emitCallLikeOp(CIRGenFunction &cgf,

RValue CIRGenFunction::emitCall(const CIRGenFunctionInfo &funcInfo,
const CIRGenCallee &callee,
ReturnValueSlot returnValue,
cir::CIRCallOpInterface *callOp,
mlir::Location loc) {
QualType retTy = funcInfo.getReturnType();
const cir::ABIArgInfo &retInfo = funcInfo.getReturnInfo();

assert(!cir::MissingFeatures::opCallArgs());
assert(!cir::MissingFeatures::emitLifetimeMarkers());

Expand Down Expand Up @@ -87,9 +110,45 @@ RValue CIRGenFunction::emitCall(const CIRGenFunctionInfo &funcInfo,
assert(!cir::MissingFeatures::opCallMustTail());
assert(!cir::MissingFeatures::opCallReturn());

// For now we just return nothing because we don't have support for return
// values yet.
RValue ret = RValue::get(nullptr);
RValue ret;
switch (retInfo.getKind()) {
case cir::ABIArgInfo::Direct: {
mlir::Type retCIRTy = convertType(retTy);
if (retInfo.getCoerceToType() == retCIRTy &&
retInfo.getDirectOffset() == 0) {
switch (getEvaluationKind(retTy)) {
case cir::TEK_Scalar: {
mlir::ResultRange results = theCall->getOpResults();
assert(results.size() == 1 && "unexpected number of returns");

// If the argument doesn't match, perform a bitcast to coerce it. This
// can happen due to trivial type mismatches.
if (results[0].getType() != retCIRTy)
cgm.errorNYI(loc, "bitcast on function return value");

mlir::Region *region = builder.getBlock()->getParent();
if (region != theCall->getParentRegion())
cgm.errorNYI(loc, "function calls with cleanup");

return RValue::get(results[0]);
}
default:
cgm.errorNYI(loc,
"unsupported evaluation kind of function call result");
}
} else
cgm.errorNYI(loc, "unsupported function call form");

break;
}
case cir::ABIArgInfo::Ignore:
// If we are ignoring an argument that had a result, make sure to construct
// the appropriate return value for our caller.
ret = getUndefRValue(retTy);
break;
default:
cgm.errorNYI(loc, "unsupported return value information");
}

return ret;
}
4 changes: 4 additions & 0 deletions clang/lib/CIR/CodeGen/CIRGenCall.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ struct CallArg {};

class CallArgList : public llvm::SmallVector<CallArg, 8> {};

/// Contains the address where the return value of a function can be stored, and
/// whether the address is volatile or not.
class ReturnValueSlot {};

} // namespace clang::CIRGen

#endif // CLANG_LIB_CODEGEN_CIRGENCALL_H
24 changes: 19 additions & 5 deletions clang/lib/CIR/CodeGen/CIRGenExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -695,23 +695,36 @@ static CIRGenCallee emitDirectCallee(CIRGenModule &cgm, GlobalDecl gd) {
return CIRGenCallee::forDirect(callee, gd);
}

RValue CIRGenFunction::getUndefRValue(QualType ty) {
if (ty->isVoidType())
return RValue::get(nullptr);

cgm.errorNYI("unsupported type for undef rvalue");
return RValue::get(nullptr);
}

RValue CIRGenFunction::emitCall(clang::QualType calleeTy,
const CIRGenCallee &callee,
const clang::CallExpr *e) {
const clang::CallExpr *e,
ReturnValueSlot returnValue) {
// Get the actual function type. The callee type will always be a pointer to
// function type or a block pointer type.
assert(calleeTy->isFunctionPointerType() &&
"Callee must have function pointer type!");

calleeTy = getContext().getCanonicalType(calleeTy);
auto pointeeTy = cast<PointerType>(calleeTy)->getPointeeType();

if (getLangOpts().CPlusPlus)
assert(!cir::MissingFeatures::sanitizers());

const auto *fnType = cast<FunctionType>(pointeeTy);

assert(!cir::MissingFeatures::sanitizers());
assert(!cir::MissingFeatures::opCallArgs());

const CIRGenFunctionInfo &funcInfo = cgm.getTypes().arrangeFreeFunctionCall();
const CIRGenFunctionInfo &funcInfo =
cgm.getTypes().arrangeFreeFunctionCall(fnType);

assert(!cir::MissingFeatures::opCallNoPrototypeFunc());
assert(!cir::MissingFeatures::opCallChainCall());
Expand All @@ -720,7 +733,7 @@ RValue CIRGenFunction::emitCall(clang::QualType calleeTy,

cir::CIRCallOpInterface callOp;
RValue callResult =
emitCall(funcInfo, callee, &callOp, getLoc(e->getExprLoc()));
emitCall(funcInfo, callee, returnValue, &callOp, getLoc(e->getExprLoc()));

assert(!cir::MissingFeatures::generateDebugInfo());

Expand All @@ -746,7 +759,8 @@ CIRGenCallee CIRGenFunction::emitCallee(const clang::Expr *e) {
return {};
}

RValue CIRGenFunction::emitCallExpr(const clang::CallExpr *e) {
RValue CIRGenFunction::emitCallExpr(const clang::CallExpr *e,
ReturnValueSlot returnValue) {
assert(!cir::MissingFeatures::objCBlocks());

if (isa<CXXMemberCallExpr>(e)) {
Expand Down Expand Up @@ -778,7 +792,7 @@ RValue CIRGenFunction::emitCallExpr(const clang::CallExpr *e) {
}
assert(!cir::MissingFeatures::opCallPseudoDtor());

return emitCall(e->getCallee()->getType(), callee, e);
return emitCall(e->getCallee()->getType(), callee, e, returnValue);
}

/// Emit code to compute the specified expression, ignoring the result.
Expand Down
7 changes: 2 additions & 5 deletions clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1519,11 +1519,8 @@ mlir::Value ScalarExprEmitter::VisitCastExpr(CastExpr *ce) {
}

mlir::Value ScalarExprEmitter::VisitCallExpr(const CallExpr *e) {
if (e->getCallReturnType(cgf.getContext())->isReferenceType()) {
cgf.getCIRGenModule().errorNYI(
e->getSourceRange(), "call to function with non-void return type");
return {};
}
if (e->getCallReturnType(cgf.getContext())->isReferenceType())
return emitLoadOfLValue(e);

auto v = cgf.emitCallExpr(e).getScalarVal();
assert(!cir::MissingFeatures::emitLValueAlignmentAssumption());
Expand Down
Loading
Loading