24 using OpRewritePattern<tosa::VariableOp>::OpRewritePattern;
26 LogicalResult matchAndRewrite(tosa::VariableOp op,
27 PatternRewriter &rewriter)
const final {
29 auto newVariable = mlir::ml_program::GlobalOp::create(
30 rewriter, op.getLoc(), op.getName(), variableType,
true,
31 op.getInitialValueAttr(),
nullptr);
32 newVariable.setPrivate();
33 rewriter.replaceOp(op, newVariable);
38class VariableWriteOpConverter
41 using OpRewritePattern<tosa::VariableWriteOp>::OpRewritePattern;
43 LogicalResult matchAndRewrite(tosa::VariableWriteOp op,
44 PatternRewriter &rewriter)
const final {
45 auto globalSymbolRef =
46 SymbolRefAttr::get(rewriter.getContext(), op.getName());
47 auto newVariableWrite = ml_program::GlobalStoreOp::create(
48 rewriter, op.getLoc(), globalSymbolRef, op.getInput1());
49 rewriter.replaceOp(op, newVariableWrite);
54class VariableReadOpConverter :
public OpRewritePattern<tosa::VariableReadOp> {
56 using OpRewritePattern<tosa::VariableReadOp>::OpRewritePattern;
58 LogicalResult matchAndRewrite(tosa::VariableReadOp op,
59 PatternRewriter &rewriter)
const final {
60 auto globalSymbolRef =
61 SymbolRefAttr::get(rewriter.getContext(), op.getName());
62 auto newVariableRead = ml_program::GlobalLoadOp::create(
63 rewriter, op.getLoc(), op.getType(), globalSymbolRef);
64 rewriter.replaceOp(op, newVariableRead);
74 patterns->add<VariableOpConverter, VariableWriteOpConverter,
75 VariableReadOpConverter>(
patterns->getContext());
RankedTensorType getVariableType(VariableOp variableOp)
void populateTosaToMLProgramConversionPatterns(RewritePatternSet *patterns)
Include the generated interface declarations.
const FrozenRewritePatternSet & patterns
OpRewritePattern is a wrapper around RewritePattern that allows for matching and rewriting against an...