Skip to content

Passing through cir.func attributes to func.func operation in CIRtoMLIR lowering #1618

@felixdaas

Description

@felixdaas

In the CIRtoMLIR lowering, when lowering a cir.func operation with

class CIRFuncOpLowering : public mlir::OpConversionPattern<cir::FuncOp> {
public:
using OpConversionPattern<cir::FuncOp>::OpConversionPattern;
mlir::LogicalResult
matchAndRewrite(cir::FuncOp op, OpAdaptor adaptor,
mlir::ConversionPatternRewriter &rewriter) const override {
auto fnType = op.getFunctionType();
mlir::TypeConverter::SignatureConversion signatureConversion(
fnType.getNumInputs());
for (const auto &argType : enumerate(fnType.getInputs())) {
auto convertedType = typeConverter->convertType(argType.value());
if (!convertedType)
return mlir::failure();
signatureConversion.addInputs(argType.index(), convertedType);
}
mlir::Type resultType =
getTypeConverter()->convertType(fnType.getReturnType());
auto fn = rewriter.create<mlir::func::FuncOp>(
op.getLoc(), op.getName(),
rewriter.getFunctionType(signatureConversion.getConvertedTypes(),
resultType ? mlir::TypeRange(resultType)
: mlir::TypeRange()));
if (failed(rewriter.convertRegionTypes(&op.getBody(), *typeConverter,
&signatureConversion)))
return mlir::failure();
rewriter.inlineRegionBefore(op.getBody(), fn.getBody(), fn.end());
rewriter.eraseOp(op);
return mlir::LogicalResult::success();
}
};
no attributes are passed/set to the created func.func operation. Because of this the lowering fails with the following error if the the c-code includes function declarations:

error: 'func.func' op symbol declaration cannot have public visibility

I would suggest fixing this by passing the sym_visibility and maybe also the arg_attrs, res_attrs attributes of the cir.func to the created func.func (arg_attrs, res_attrs don't fix this particular issue and I'm not sure if they are currently used for anything, maybe someone else can explain what the purpose of the attributes is)

Here's a minimal example that triggers the issue:

test.c

#include "example.h"

int main(int argc, char *argv[]) {
    // Variables
    int number = 15;

    number = add10(number);

    return 0;
} 

example.h

#ifndef ADD10_H
#define ADD10_H

int add10(int x);

#endif

example.c

#include "example.h"

int add10(int x) {
    return x + 10;
}

trying to lower throughmlir here leads to the describe error:

clang -fclangir -S -Xclang -emit-mlir=core -fno-clangir-direct-lowering test.c
loc(fused["./example.h":4:1, "./example.h":4:16]): error: 'func.func' op symbol declaration cannot have public visibility
fatal error: error in backend: The pass manager failed to lower CIR to MLIR standard dialects!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions