1 //===- Pattern.h - SPIRV Common Conversion Patterns -----------------------===//
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 #ifndef MLIR_CONVERSION_SPIRVCOMMON_PATTERN_H
10 #define MLIR_CONVERSION_SPIRVCOMMON_PATTERN_H
11 
12 #include "mlir/Dialect/SPIRV/IR/SPIRVOpTraits.h"
13 #include "mlir/Transforms/DialectConversion.h"
14 
15 namespace mlir {
16 namespace spirv {
17 
18 /// Converts unary and binary standard operations to SPIR-V operations.
19 template <typename Op, typename SPIRVOp>
20 class UnaryAndBinaryOpPattern final : public OpConversionPattern<Op> {
21 public:
22   using OpConversionPattern<Op>::OpConversionPattern;
23 
24   LogicalResult
25   matchAndRewrite(Op op, typename Op::Adaptor adaptor,
26                   ConversionPatternRewriter &rewriter) const override {
27     assert(adaptor.getOperands().size() <= 2);
28     auto dstType = this->getTypeConverter()->convertType(op.getType());
29     if (!dstType)
30       return failure();
31     if (SPIRVOp::template hasTrait<OpTrait::spirv::UnsignedOp>() &&
32         dstType != op.getType()) {
33       return op.emitError(
34           "bitwidth emulation is not implemented yet on unsigned op");
35     }
36     rewriter.template replaceOpWithNewOp<SPIRVOp>(op, dstType,
37                                                   adaptor.getOperands());
38     return success();
39   }
40 };
41 
42 } // namespace spirv
43 } // namespace mlir
44 
45 #endif // MLIR_CONVERSION_SPIRVCOMMON_PATTERN_H
46