19 using namespace emitc;
23 #define GEN_PASS_DEF_WRAPFUNCINCLASSPASS
24 #include "mlir/Dialect/EmitC/Transforms/Passes.h.inc"
27 struct WrapFuncInClassPass
28 :
public impl::WrapFuncInClassPassBase<WrapFuncInClassPass> {
29 using WrapFuncInClassPassBase::WrapFuncInClassPassBase;
30 void runOnOperation()
override {
52 auto className = funcOp.getSymNameAttr().str() +
"Class";
53 ClassOp newClassOp = rewriter.
create<ClassOp>(funcOp.getLoc(), className);
59 auto argAttrs = funcOp.getArgAttrs();
64 fieldName = rewriter.
getStringAttr(
"fieldName" + std::to_string(idx));
65 if (argAttrs && idx < argAttrs->size())
66 argAttr = (*argAttrs)[idx];
69 fields.push_back({fieldName, typeAttr});
70 rewriter.
create<emitc::FieldOp>(funcOp.getLoc(), fieldName, typeAttr,
75 FunctionType funcType = funcOp.getFunctionType();
78 rewriter.
create<emitc::FuncOp>(loc, (
"execute"), funcType);
81 newFuncOp.getBody().takeBody(funcOp.getBody());
84 std::vector<Value> newArguments;
85 newArguments.reserve(fields.size());
86 for (
auto &[fieldName, attr] : fields) {
88 rewriter.
create<emitc::GetFieldOp>(loc, attr.getValue(), fieldName);
89 newArguments.push_back(arg);
92 for (
auto [oldArg, newArg] :
93 llvm::zip(newFuncOp.getArguments(), newArguments)) {
97 llvm::BitVector argsToErase(newFuncOp.getNumArguments(),
true);
98 if (failed(newFuncOp.eraseArguments(argsToErase)))
99 newFuncOp->emitOpError(
"failed to erase all arguments using BitVector");
106 StringRef attributeName;
110 StringRef namedAttribute) {
static MLIRContext * getContext(OpFoldResult val)
WrapFuncInClass(MLIRContext *context, StringRef attrName)
LogicalResult matchAndRewrite(emitc::FuncOp funcOp, PatternRewriter &rewriter) const override
Attributes are known-constant values of operations.
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 * create(const OperationState &state)
Creates an operation given the fields represented as an OperationState.
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...
virtual void replaceOp(Operation *op, ValueRange newValues)
Replace the results of the given (original) operation with the specified list of values (replacements...
void replaceAllUsesWith(Value from, Value to)
Find uses of from and replace them with to.
constexpr void enumerate(std::tuple< Tys... > &tuple, CallbackT &&callback)
void populateFuncPatterns(RewritePatternSet &patterns, StringRef namedAttribute)
Populates 'patterns' with func-related patterns.
Include the generated interface declarations.
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 walkAndApplyPatterns(Operation *op, const FrozenRewritePatternSet &patterns, RewriterBase::Listener *listener=nullptr)
A fast walk-based pattern rewrite driver.
OpRewritePattern is a wrapper around RewritePattern that allows for matching and rewriting against an...