27#define GEN_PASS_DEF_TOSAARITHCONSTANTTOTOSACONSTPASS
28#include "mlir/Dialect/Tosa/Transforms/Passes.h.inc"
44static bool isSupportedElementType(
Type elementType) {
45 if (isa<FloatType>(elementType))
48 if (
auto intType = dyn_cast<IntegerType>(elementType))
49 return intType.isSignless() || intType.isUnsigned();
51 if (isa<quant::QuantizedType>(elementType))
54 if (isa<tosa::mxint8Type>(elementType))
64 LogicalResult matchAndRewrite(arith::ConstantOp constOp,
67 auto resultType = dyn_cast<RankedTensorType>(constOp.getResult().getType());
68 if (!resultType || !resultType.hasStaticShape())
71 if (!isSupportedElementType(resultType.getElementType()))
75 auto elementsAttr = dyn_cast<ElementsAttr>(attr);
79 auto attrType = dyn_cast<RankedTensorType>(elementsAttr.getType());
80 if (!attrType || !attrType.hasStaticShape())
82 if (attrType != resultType)
85 auto newConst = tosa::ConstOp::create(rewriter, constOp.getLoc(),
86 resultType, elementsAttr);
87 rewriter.
replaceOp(constOp, newConst.getResult());
92struct TosaArithConstantToTosaConstPass
94 TosaArithConstantToTosaConstPass> {
98 registry.
insert<arith::ArithDialect, tosa::TosaDialect>();
101 void runOnOperation()
override {
104 patterns.add<ArithConstantToTosaConst>(ctx);
Attributes are known-constant values of operations.
The DialectRegistry maps a dialect namespace to a constructor for the matching dialect.
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...
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Include the generated interface declarations.
LogicalResult applyPatternsGreedily(Region ®ion, const FrozenRewritePatternSet &patterns, GreedyRewriteConfig config=GreedyRewriteConfig(), bool *changed=nullptr)
Rewrite ops in the given region, which must be isolated from above, by repeatedly applying the highes...
const FrozenRewritePatternSet & patterns
OpRewritePattern is a wrapper around RewritePattern that allows for matching and rewriting against an...
OpRewritePattern(MLIRContext *context, PatternBenefit benefit=1, ArrayRef< StringRef > generatedNames={})
Patterns must specify the root operation name they match against, and can also specify the benefit of...