54 lines · cpp
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-exception6//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_CONVERTMATHTOEMITC22#include "mlir/Conversion/Passes.h.inc"23} // namespace mlir24 25using namespace mlir;26namespace {27 28// Replaces Math operations with `emitc.call_opaque` operations.29struct ConvertMathToEmitC30 : public impl::ConvertMathToEmitCBase<ConvertMathToEmitC> {31 using ConvertMathToEmitCBase::ConvertMathToEmitCBase;32 33public:34 void runOnOperation() final;35};36 37} // namespace38 39void 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