MLIR  15.0.0git
Pattern.h
Go to the documentation of this file.
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 
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:
24 
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
Include the generated interface declarations.
TypeConverter * getTypeConverter() const
Return the type converter held by this pattern, or nullptr if the pattern does not require type conve...
LogicalResult convertType(Type t, SmallVectorImpl< Type > &results)
Convert the given type.
Converts elementwise unary, binary and ternary standard operations to SPIR-V operations.
Definition: Pattern.h:21
LogicalResult matchAndRewrite(Op op, typename Op::Adaptor adaptor, ConversionPatternRewriter &rewriter) const override
Definition: Pattern.h:26
LogicalResult success(bool isSuccess=true)
Utility function to generate a LogicalResult.
Definition: LogicalResult.h:56
This class represents an efficient way to signal success or failure.
Definition: LogicalResult.h:26
LogicalResult failure(bool isFailure=true)
Utility function to generate a LogicalResult.
Definition: LogicalResult.h:62
InFlightDiagnostic emitError(const Twine &message={})
Emit an error about fatal conditions with this operation, reporting up to any diagnostic handlers tha...
Definition: Operation.cpp:639
OpConversionPattern is a wrapper around ConversionPattern that allows for matching and rewriting agai...
This class implements a pattern rewriter for use with ConversionPatterns.
This provides public APIs that all operations should have.