@@ -655,6 +655,28 @@ class CIRFuncLowering : public mlir::OpConversionPattern<mlir::cir::FuncOp> {
655655public:
656656 using OpConversionPattern<mlir::cir::FuncOp>::OpConversionPattern;
657657
658+ // / Returns the name used for the linkage attribute. This *must* correspond to
659+ // / the name of the attribute in ODS.
660+ static StringRef getLinkageAttrNameString () { return " linkage" ; }
661+
662+ // / Only retain those attributes that are not constructed by
663+ // / `LLVMFuncOp::build`. If `filterArgAttrs` is set, also filter out argument
664+ // / attributes.
665+ static void filterFuncAttributes (mlir::cir::FuncOp func,
666+ bool filterArgAndResAttrs,
667+ SmallVectorImpl<mlir::NamedAttribute> &result) {
668+ for (const auto &attr : func->getAttrs ()) {
669+ if (attr.getName () == mlir::SymbolTable::getSymbolAttrName () ||
670+ attr.getName () == func.getFunctionTypeAttrName () ||
671+ attr.getName () == getLinkageAttrNameString () ||
672+ (filterArgAndResAttrs &&
673+ (attr.getName () == func.getArgAttrsAttrName () ||
674+ attr.getName () == func.getResAttrsAttrName ())))
675+ continue ;
676+ result.push_back (attr);
677+ }
678+ }
679+
658680 mlir::LogicalResult
659681 matchAndRewrite (mlir::cir::FuncOp op, OpAdaptor adaptor,
660682 mlir::ConversionPatternRewriter &rewriter) const override {
@@ -690,9 +712,14 @@ class CIRFuncLowering : public mlir::OpConversionPattern<mlir::cir::FuncOp> {
690712 Loc = FusedLoc.getLocations ()[0 ];
691713 }
692714 assert (Loc.isa <mlir::FileLineColLoc>() && " expected single location here" );
715+
693716 auto linkage = convertLinkage (op.getLinkage ());
694- auto fn = rewriter.create <mlir::LLVM::LLVMFuncOp>(Loc, op.getName (),
695- llvmFnTy, linkage);
717+ SmallVector<mlir::NamedAttribute, 4 > attributes;
718+ filterFuncAttributes (op, /* filterArgAndResAttrs=*/ false , attributes);
719+
720+ auto fn = rewriter.create <mlir::LLVM::LLVMFuncOp>(
721+ Loc, op.getName (), llvmFnTy, linkage, false , mlir::LLVM::CConv::C,
722+ attributes);
696723
697724 rewriter.inlineRegionBefore (op.getBody (), fn.getBody (), fn.end ());
698725 if (failed (rewriter.convertRegionTypes (&fn.getBody (), *typeConverter,
0 commit comments