31 void populateConvertToEmitCConversionPatterns(
41 dialect->addInterfaces<FuncToEmitCDialectInterface>();
55 matchAndRewrite(func::CallOp callOp, OpAdaptor adaptor,
58 if (callOp.getNumResults() > 1)
60 callOp,
"only functions with zero or one result can be converted");
63 adaptor.getOperands(),
75 matchAndRewrite(func::FuncOp funcOp, OpAdaptor adaptor,
77 FunctionType fnType = funcOp.getFunctionType();
79 if (fnType.getNumResults() > 1)
81 funcOp,
"only functions with zero or one result can be converted");
84 fnType.getNumInputs());
85 for (
const auto &argType :
enumerate(fnType.getInputs())) {
86 auto convertedType = getTypeConverter()->convertType(argType.value());
89 "argument type conversion failed");
90 signatureConverter.addInputs(argType.index(), convertedType);
94 if (fnType.getNumResults() == 1) {
95 resultType = getTypeConverter()->convertType(fnType.getResult(0));
98 "result type conversion failed");
102 emitc::FuncOp newFuncOp = rewriter.
create<emitc::FuncOp>(
103 funcOp.getLoc(), funcOp.getName(),
105 signatureConverter.getConvertedTypes(),
109 for (
const auto &namedAttr : funcOp->getAttrs()) {
110 if (namedAttr.getName() != funcOp.getFunctionTypeAttrName() &&
112 newFuncOp->
setAttr(namedAttr.getName(), namedAttr.getValue());
116 if (funcOp.isDeclaration()) {
118 newFuncOp.setSpecifiersAttr(specifiers);
123 if (funcOp.isPrivate() && !funcOp.isDeclaration()) {
125 newFuncOp.setSpecifiersAttr(specifiers);
128 if (!funcOp.isDeclaration()) {
132 &newFuncOp.getBody(), *getTypeConverter(), &signatureConverter)))
146 matchAndRewrite(func::ReturnOp returnOp, OpAdaptor adaptor,
148 if (returnOp.getNumOperands() > 1)
150 returnOp,
"only zero or one operand is supported");
154 returnOp.getNumOperands() ? adaptor.getOperands()[0] :
nullptr);
168 patterns.add<CallOpConversion, FuncOpConversion, ReturnOpConversion>(
MLIRContext * getContext() const
ArrayAttr getStrArrayAttr(ArrayRef< StringRef > values)
This class implements a pattern rewriter for use with ConversionPatterns.
FailureOr< Block * > convertRegionTypes(Region *region, const TypeConverter &converter, TypeConverter::SignatureConversion *entryConversion=nullptr)
Apply a signature conversion to each block in the given region.
void eraseOp(Operation *op) override
PatternRewriter hook for erasing a dead operation.
This class describes a specific conversion target.
ConvertToEmitCPatternInterface(Dialect *dialect)
The DialectRegistry maps a dialect namespace to a constructor for the matching dialect.
bool addExtension(TypeID extensionID, std::unique_ptr< DialectExtensionBase > extension)
Add the given extension to the registry.
MLIRContext is the top-level object for a collection of MLIR operations.
Operation * create(const OperationState &state)
Creates an operation given the fields represented as an OperationState.
OpConversionPattern is a wrapper around ConversionPattern that allows for matching and rewriting agai...
void setAttr(StringAttr name, Attribute value)
If the an attribute exists with the specified name, change it to the new value.
std::enable_if_t<!std::is_convertible< CallbackT, Twine >::value, LogicalResult > notifyMatchFailure(Location loc, CallbackT &&reasonCallback)
Used to notify the listener that the IR failed to be rewritten because of a match failure,...
void inlineRegionBefore(Region ®ion, Region &parent, Region::iterator before)
Move the blocks that belong to "region" before the given position in another region "parent".
OpTy replaceOpWithNewOp(Operation *op, Args &&...args)
Replace the results of the given (original) op with a new op that is created without verification (re...
static StringRef getSymbolAttrName()
Return the name of the attribute used for symbol names.
This class provides all of the information necessary to convert a type signature.
This class provides an abstraction over the various different ranges of value types.
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
constexpr void enumerate(std::tuple< Tys... > &tuple, CallbackT &&callback)
Include the generated interface declarations.
void registerConvertFuncToEmitCInterface(DialectRegistry ®istry)
const FrozenRewritePatternSet & patterns
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...
void populateFuncToEmitCPatterns(const TypeConverter &typeConverter, RewritePatternSet &patterns)