diff --git a/mlir/lib/Conversion/FuncToLLVM/FuncToLLVM.cpp b/mlir/lib/Conversion/FuncToLLVM/FuncToLLVM.cpp index 55f0a9ac3bbb2..328c605add65c 100644 --- a/mlir/lib/Conversion/FuncToLLVM/FuncToLLVM.cpp +++ b/mlir/lib/Conversion/FuncToLLVM/FuncToLLVM.cpp @@ -320,13 +320,22 @@ mlir::convertFuncOpToLLVMFuncOp(FunctionOpInterface funcOp, // overriden with an LLVM pointer type for later processing. SmallVector> byValRefNonPtrAttrs; TypeConverter::SignatureConversion result(funcOp.getNumArguments()); - auto llvmType = converter.convertFunctionSignature( - funcOp, varargsAttr && varargsAttr.getValue(), - shouldUseBarePtrCallConv(funcOp, &converter), result, - byValRefNonPtrAttrs); + auto llvmType = dyn_cast_or_null( + converter.convertFunctionSignature( + funcOp, varargsAttr && varargsAttr.getValue(), + shouldUseBarePtrCallConv(funcOp, &converter), result, + byValRefNonPtrAttrs)); if (!llvmType) return rewriter.notifyMatchFailure(funcOp, "signature conversion failed"); + // Check for unsupported variadic functions. + if (!shouldUseBarePtrCallConv(funcOp, &converter)) + if (funcOp->getAttrOfType( + LLVM::LLVMDialect::getEmitCWrapperAttrName())) + if (llvmType.isVarArg()) + return funcOp.emitError("C interface for variadic functions is not " + "supported yet."); + // Create an LLVM function, use external linkage by default until MLIR // functions have linkage. LLVM::Linkage linkage = LLVM::Linkage::External; @@ -342,6 +351,18 @@ mlir::convertFuncOpToLLVMFuncOp(FunctionOpInterface funcOp, linkage = attr.getLinkage(); } + // Check for invalid attributes. + StringRef readnoneAttrName = LLVM::LLVMDialect::getReadnoneAttrName(); + if (funcOp->hasAttr(readnoneAttrName)) { + auto attr = funcOp->getAttrOfType(readnoneAttrName); + if (!attr) { + funcOp->emitError() << "Contains " << readnoneAttrName + << " attribute not of type UnitAttr"; + return rewriter.notifyMatchFailure( + funcOp, "Contains readnone attribute not of type UnitAttr"); + } + } + SmallVector attributes; filterFuncAttributes(funcOp, attributes); auto newFuncOp = rewriter.create( @@ -352,15 +373,7 @@ mlir::convertFuncOpToLLVMFuncOp(FunctionOpInterface funcOp, .setVisibility(funcOp.getVisibility()); // Create a memory effect attribute corresponding to readnone. - StringRef readnoneAttrName = LLVM::LLVMDialect::getReadnoneAttrName(); if (funcOp->hasAttr(readnoneAttrName)) { - auto attr = funcOp->getAttrOfType(readnoneAttrName); - if (!attr) { - funcOp->emitError() << "Contains " << readnoneAttrName - << " attribute not of type UnitAttr"; - return rewriter.notifyMatchFailure( - funcOp, "Contains readnone attribute not of type UnitAttr"); - } auto memoryAttr = LLVM::MemoryEffectsAttr::get( rewriter.getContext(), {LLVM::ModRefInfo::NoModRef, LLVM::ModRefInfo::NoModRef, @@ -447,10 +460,6 @@ mlir::convertFuncOpToLLVMFuncOp(FunctionOpInterface funcOp, if (!shouldUseBarePtrCallConv(funcOp, &converter)) { if (funcOp->getAttrOfType( LLVM::LLVMDialect::getEmitCWrapperAttrName())) { - if (newFuncOp.isVarArg()) - return funcOp.emitError("C interface for variadic functions is not " - "supported yet."); - if (newFuncOp.isExternal()) wrapExternalFunction(rewriter, funcOp->getLoc(), converter, funcOp, newFuncOp);