1995c3984SLei Zhang //===- MathToSPIRV.cpp - Math to SPIR-V Patterns --------------------------===//
2995c3984SLei Zhang //
3995c3984SLei Zhang // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4995c3984SLei Zhang // See https://llvm.org/LICENSE.txt for license information.
5995c3984SLei Zhang // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6995c3984SLei Zhang //
7995c3984SLei Zhang //===----------------------------------------------------------------------===//
8995c3984SLei Zhang //
9995c3984SLei Zhang // This file implements patterns to convert Math dialect to SPIR-V dialect.
10995c3984SLei Zhang //
11995c3984SLei Zhang //===----------------------------------------------------------------------===//
12995c3984SLei Zhang 
13*a54f4eaeSMogball #include "../SPIRVCommon/Pattern.h"
14995c3984SLei Zhang #include "mlir/Dialect/Math/IR/Math.h"
15995c3984SLei Zhang #include "mlir/Dialect/SPIRV/IR/SPIRVDialect.h"
16995c3984SLei Zhang #include "mlir/Dialect/SPIRV/IR/SPIRVOps.h"
17995c3984SLei Zhang #include "mlir/Dialect/SPIRV/Transforms/SPIRVConversion.h"
18995c3984SLei Zhang #include "llvm/Support/Debug.h"
19995c3984SLei Zhang 
20995c3984SLei Zhang #define DEBUG_TYPE "math-to-spirv-pattern"
21995c3984SLei Zhang 
22995c3984SLei Zhang using namespace mlir;
23995c3984SLei Zhang 
24995c3984SLei Zhang //===----------------------------------------------------------------------===//
25995c3984SLei Zhang // Operation conversion
26995c3984SLei Zhang //===----------------------------------------------------------------------===//
27995c3984SLei Zhang 
28995c3984SLei Zhang // Note that DRR cannot be used for the patterns in this file: we may need to
29995c3984SLei Zhang // convert type along the way, which requires ConversionPattern. DRR generates
30995c3984SLei Zhang // normal RewritePattern.
31995c3984SLei Zhang 
32995c3984SLei Zhang namespace {
33995c3984SLei Zhang /// Converts math.log1p to SPIR-V ops.
34995c3984SLei Zhang ///
35995c3984SLei Zhang /// SPIR-V does not have a direct operations for log(1+x). Explicitly lower to
36995c3984SLei Zhang /// these operations.
37995c3984SLei Zhang class Log1pOpPattern final : public OpConversionPattern<math::Log1pOp> {
38995c3984SLei Zhang public:
39995c3984SLei Zhang   using OpConversionPattern<math::Log1pOp>::OpConversionPattern;
40995c3984SLei Zhang 
41995c3984SLei Zhang   LogicalResult
42b54c724bSRiver Riddle   matchAndRewrite(math::Log1pOp operation, OpAdaptor adaptor,
43995c3984SLei Zhang                   ConversionPatternRewriter &rewriter) const override {
44b54c724bSRiver Riddle     assert(adaptor.getOperands().size() == 1);
45995c3984SLei Zhang     Location loc = operation.getLoc();
46995c3984SLei Zhang     auto type =
47995c3984SLei Zhang         this->getTypeConverter()->convertType(operation.operand().getType());
48995c3984SLei Zhang     auto one = spirv::ConstantOp::getOne(type, operation.getLoc(), rewriter);
49b54c724bSRiver Riddle     auto onePlus =
50b54c724bSRiver Riddle         rewriter.create<spirv::FAddOp>(loc, one, adaptor.getOperands()[0]);
51995c3984SLei Zhang     rewriter.replaceOpWithNewOp<spirv::GLSLLogOp>(operation, type, onePlus);
52995c3984SLei Zhang     return success();
53995c3984SLei Zhang   }
54995c3984SLei Zhang };
55995c3984SLei Zhang } // namespace
56995c3984SLei Zhang 
57995c3984SLei Zhang //===----------------------------------------------------------------------===//
58995c3984SLei Zhang // Pattern population
59995c3984SLei Zhang //===----------------------------------------------------------------------===//
60995c3984SLei Zhang 
61995c3984SLei Zhang namespace mlir {
62995c3984SLei Zhang void populateMathToSPIRVPatterns(SPIRVTypeConverter &typeConverter,
63995c3984SLei Zhang                                  RewritePatternSet &patterns) {
64*a54f4eaeSMogball   patterns.add<
65*a54f4eaeSMogball       Log1pOpPattern,
66*a54f4eaeSMogball       spirv::UnaryAndBinaryOpPattern<math::AbsOp, spirv::GLSLFAbsOp>,
67*a54f4eaeSMogball       spirv::UnaryAndBinaryOpPattern<math::CeilOp, spirv::GLSLCeilOp>,
68*a54f4eaeSMogball       spirv::UnaryAndBinaryOpPattern<math::CosOp, spirv::GLSLCosOp>,
69*a54f4eaeSMogball       spirv::UnaryAndBinaryOpPattern<math::ExpOp, spirv::GLSLExpOp>,
70*a54f4eaeSMogball       spirv::UnaryAndBinaryOpPattern<math::FloorOp, spirv::GLSLFloorOp>,
71*a54f4eaeSMogball       spirv::UnaryAndBinaryOpPattern<math::LogOp, spirv::GLSLLogOp>,
72*a54f4eaeSMogball       spirv::UnaryAndBinaryOpPattern<math::PowFOp, spirv::GLSLPowOp>,
73*a54f4eaeSMogball       spirv::UnaryAndBinaryOpPattern<math::RsqrtOp, spirv::GLSLInverseSqrtOp>,
74*a54f4eaeSMogball       spirv::UnaryAndBinaryOpPattern<math::SinOp, spirv::GLSLSinOp>,
75*a54f4eaeSMogball       spirv::UnaryAndBinaryOpPattern<math::SqrtOp, spirv::GLSLSqrtOp>,
76*a54f4eaeSMogball       spirv::UnaryAndBinaryOpPattern<math::TanhOp, spirv::GLSLTanhOp>>(
77995c3984SLei Zhang       typeConverter, patterns.getContext());
78995c3984SLei Zhang }
79995c3984SLei Zhang 
80995c3984SLei Zhang } // namespace mlir
81