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
20namespace mlir {
21#define GEN_PASS_DEF_CONVERTMATHTOEMITC
22#include "mlir/Conversion/Passes.h.inc"
23} // namespace mlir
24
25using namespace mlir;
26namespace {
27
28// Replaces Math operations with `emitc.call_opaque` operations.
29struct ConvertMathToEmitC
30 : public impl::ConvertMathToEmitCBase<ConvertMathToEmitC> {
31 using ConvertMathToEmitCBase::ConvertMathToEmitCBase;
32
33public:
34 void runOnOperation() final;
35};
36
37} // namespace
38
39void ConvertMathToEmitC::runOnOperation() {
40 ConversionTarget target(getContext());
41 target.addLegalOp<emitc::CallOpaqueOp>();
42
43 target.addIllegalOp<
44 math::FloorOp, math::ExpOp, math::RoundOp, math::CosOp, math::SinOp,
45 math::Atan2Op, math::CeilOp, math::AcosOp, math::AsinOp, math::AbsFOp,
46 math::PowFOp, math::TanOp, math::AtanOp, math::SinhOp, math::CoshOp,
47 math::TanhOp, math::AsinhOp, math::AcoshOp, math::AtanhOp, math::Exp2Op,
48 math::ExpM1Op, math::LogOp, math::Log2Op, math::Log10Op, math::Log1pOp,
49 math::CbrtOp, math::ErfOp, math::ErfcOp, math::TruncOp, math::CopySignOp,
50 math::FmaOp, math::IsNaNOp, math::IsInfOp, math::IsFiniteOp,
51 math::IsNormalOp>();
52
53 RewritePatternSet patterns(&getContext());
54 populateConvertMathToEmitCPatterns(patterns, languageTarget);
55
56 if (failed(
57 Result: applyPartialConversion(op: getOperation(), target, patterns: std::move(patterns))))
58 signalPassFailure();
59}
60