23#define GEN_PASS_DEF_WRAPFUNCINCLASSPASS
24#include "mlir/Dialect/EmitC/Transforms/Passes.h.inc"
27struct WrapFuncInClassPass
28 :
public impl::WrapFuncInClassPassBase<WrapFuncInClassPass> {
29 using WrapFuncInClassPassBase::WrapFuncInClassPassBase;
30 void runOnOperation()
override {
52 auto className = funcOp.getSymNameAttr().str() +
"Class";
53 ClassOp newClassOp = ClassOp::create(rewriter, funcOp.getLoc(), className);
59 auto argAttrs = funcOp.getArgAttrs();
60 for (
auto [idx, val] : llvm::enumerate(funcOp.getArguments())) {
61 StringAttr fieldName =
64 TypeAttr typeAttr = TypeAttr::get(val.getType());
65 fields.push_back({fieldName, typeAttr});
67 FieldOp fieldop = emitc::FieldOp::create(rewriter, funcOp->getLoc(),
68 fieldName, typeAttr,
nullptr);
70 if (argAttrs && idx < argAttrs->size()) {
71 fieldop->setDiscardableAttrs(funcOp.getArgAttrDict(idx));
76 FunctionType funcType = funcOp.getFunctionType();
79 emitc::FuncOp::create(rewriter, loc, (funcName), funcType);
82 newFuncOp.getBody().takeBody(funcOp.getBody());
85 std::vector<Value> newArguments;
86 newArguments.reserve(fields.size());
87 for (
auto &[fieldName, attr] : fields) {
89 emitc::GetFieldOp::create(rewriter, loc, attr.getValue(), fieldName);
90 newArguments.push_back(arg);
93 for (
auto [oldArg, newArg] :
94 llvm::zip(newFuncOp.getArguments(), newArguments)) {
98 llvm::BitVector argsToErase(newFuncOp.getNumArguments(),
true);
99 if (failed(newFuncOp.eraseArguments(argsToErase)))
100 newFuncOp->emitOpError(
"failed to erase all arguments using BitVector");
LogicalResult matchAndRewrite(emitc::FuncOp funcOp, PatternRewriter &rewriter) const override
WrapFuncInClass(MLIRContext *context, StringRef funcName)
StringAttr getStringAttr(const Twine &bytes)
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
MLIRContext is the top-level object for a collection of MLIR operations.
Block * createBlock(Region *parent, Region::iterator insertPt={}, TypeRange argTypes={}, ArrayRef< Location > locs={})
Add new block with 'argTypes' arguments and set the insertion point to the end of it.
void setInsertionPointToStart(Block *block)
Sets the insertion point to the start of the specified block.
void setInsertionPointToEnd(Block *block)
Sets the insertion point to the end of the specified block.
Operation is the basic unit of execution within MLIR.
A special type of RewriterBase that coordinates the application of a rewrite pattern on the current I...
MLIRContext * getContext() const
RewritePatternSet & add(ConstructorArg &&arg, ConstructorArgs &&...args)
Add an instance of each of the pattern types 'Ts' to the pattern list with the given arguments.
virtual void replaceOp(Operation *op, ValueRange newValues)
Replace the results of the given (original) operation with the specified list of values (replacements...
virtual void replaceAllUsesWith(Value from, Value to)
Find uses of from and replace them with to.
::mlir::Pass::Option< std::string > funcName
void populateWrapFuncInClass(RewritePatternSet &patterns, StringRef fName)
Include the generated interface declarations.
void walkAndApplyPatterns(Operation *op, const FrozenRewritePatternSet &patterns, RewriterBase::Listener *listener=nullptr)
A fast walk-based pattern rewrite driver.
OpRewritePattern(MLIRContext *context, PatternBenefit benefit=1, ArrayRef< StringRef > generatedNames={})