xref: /llvm-project/mlir/lib/Conversion/MathToEmitC/MathToEmitCPass.cpp (revision c0055ec434cbb132d7776f8b4c39e99b69fa97ea)
1 //===- MathToEmitCPass.cpp - Math to EmitC Pass -----------------*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements a pass to convert the Math dialect to the EmitC dialect.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "mlir/Conversion/MathToEmitC/MathToEmitCPass.h"
14 #include "mlir/Conversion/MathToEmitC/MathToEmitC.h"
15 #include "mlir/Dialect/EmitC/IR/EmitC.h"
16 #include "mlir/Dialect/Math/IR/Math.h"
17 #include "mlir/Pass/Pass.h"
18 #include "mlir/Transforms/DialectConversion.h"
19 
20 namespace mlir {
21 #define GEN_PASS_DEF_CONVERTMATHTOEMITC
22 #include "mlir/Conversion/Passes.h.inc"
23 } // namespace mlir
24 
25 using namespace mlir;
26 namespace {
27 
28 //  Replaces Math operations with `emitc.call_opaque` operations.
29 struct ConvertMathToEmitC
30     : public impl::ConvertMathToEmitCBase<ConvertMathToEmitC> {
31   using ConvertMathToEmitCBase::ConvertMathToEmitCBase;
32 
33 public:
34   void runOnOperation() final;
35 };
36 
37 } // namespace
38 
39 void ConvertMathToEmitC::runOnOperation() {
40   ConversionTarget target(getContext());
41   target.addLegalOp<emitc::CallOpaqueOp>();
42 
43   target.addIllegalOp<math::FloorOp, math::ExpOp, math::RoundOp, math::CosOp,
44                       math::SinOp, math::Atan2Op, math::CeilOp, math::AcosOp,
45                       math::AsinOp, math::AbsFOp, math::PowFOp>();
46 
47   RewritePatternSet patterns(&getContext());
48   populateConvertMathToEmitCPatterns(patterns, languageTarget);
49 
50   if (failed(
51           applyPartialConversion(getOperation(), target, std::move(patterns))))
52     signalPassFailure();
53 }
54