1 //===- TosaToArithPass.cpp - Lowering Tosa to Linalg Dialect -----------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This transformation pass legalizes Tosa operations to the Arith dialect.
11 //===----------------------------------------------------------------------===//
13 #include "mlir/Conversion/TosaToArith/TosaToArith.h"
15 #include "mlir/Dialect/Arith/IR/Arith.h"
16 #include "mlir/Dialect/Tosa/IR/TosaOps.h"
17 #include "mlir/Dialect/Tosa/Transforms/Passes.h"
18 #include "mlir/IR/PatternMatch.h"
19 #include "mlir/Pass/PassManager.h"
20 #include "mlir/Transforms/DialectConversion.h"
21 #include "mlir/Transforms/GreedyPatternRewriteDriver.h"
24 #define GEN_PASS_DEF_TOSATOARITH
25 #include "mlir/Conversion/Passes.h.inc"
32 struct TosaToArith
: public impl::TosaToArithBase
<TosaToArith
> {
34 TosaToArith(TosaToArithOptions
&options
) : TosaToArithBase(options
) {}
36 void runOnOperation() override
{
37 RewritePatternSet
patterns(&getContext());
38 ConversionTarget
target(getContext());
39 target
.addIllegalOp
<tosa::ConstOp
>();
40 target
.addLegalDialect
<arith::ArithDialect
>();
42 mlir::tosa::populateTosaToArithConversionPatterns(&patterns
);
44 if (this->includeApplyRescale
) {
45 mlir::tosa::populateTosaRescaleToArithConversionPatterns(&patterns
,
47 target
.addIllegalOp
<tosa::ApplyScaleOp
>();
50 if (failed(applyPartialConversion(getOperation(), target
,
51 std::move(patterns
))))
57 std::unique_ptr
<Pass
> mlir::tosa::createTosaToArith(bool includeApplyRescale
,
58 bool use32BitApplyRescale
) {
59 TosaToArithOptions options
= {includeApplyRescale
, use32BitApplyRescale
};
60 return std::make_unique
<TosaToArith
>(options
);