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 elementwise unary, binary and ternary standard operations to SPIR-V
19 /// operations.
20 template <typename Op, typename SPIRVOp>
21 class ElementwiseOpPattern final : public OpConversionPattern<Op> {
22 public:
23   using OpConversionPattern<Op>::OpConversionPattern;
24 
25   LogicalResult
matchAndRewrite(Op op,typename Op::Adaptor adaptor,ConversionPatternRewriter & rewriter)26   matchAndRewrite(Op op, typename Op::Adaptor adaptor,
27                   ConversionPatternRewriter &rewriter) const override {
28     assert(adaptor.getOperands().size() <= 3);
29     auto dstType = this->getTypeConverter()->convertType(op.getType());
30     if (!dstType)
31       return failure();
32     if (SPIRVOp::template hasTrait<OpTrait::spirv::UnsignedOp>() &&
33         !op.getType().isIndex() && dstType != op.getType()) {
34       return op.emitError(
35           "bitwidth emulation is not implemented yet on unsigned op");
36     }
37     rewriter.template replaceOpWithNewOp<SPIRVOp>(op, dstType,
38                                                   adaptor.getOperands());
39     return success();
40   }
41 };
42 
43 } // namespace spirv
44 } // namespace mlir
45 
46 #endif // MLIR_CONVERSION_SPIRVCOMMON_PATTERN_H
47