MLIR  20.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 
13 #include "mlir/IR/TypeUtilities.h"
15 #include "llvm/Support/FormatVariadic.h"
16 
17 namespace mlir {
18 namespace spirv {
19 
20 /// Converts elementwise unary, binary and ternary standard operations to SPIR-V
21 /// operations.
22 template <typename Op, typename SPIRVOp>
25 
26  LogicalResult
27  matchAndRewrite(Op op, typename Op::Adaptor adaptor,
28  ConversionPatternRewriter &rewriter) const override {
29  assert(adaptor.getOperands().size() <= 3);
30  Type dstType = this->getTypeConverter()->convertType(op.getType());
31  if (!dstType) {
32  return rewriter.notifyMatchFailure(
33  op->getLoc(),
34  llvm::formatv("failed to convert type {0} for SPIR-V", op.getType()));
35  }
36 
37  if (SPIRVOp::template hasTrait<OpTrait::spirv::UnsignedOp>() &&
38  !getElementTypeOrSelf(op.getType()).isIndex() &&
39  dstType != op.getType()) {
40  op.dump();
41  return op.emitError("bitwidth emulation is not implemented yet on "
42  "unsigned op pattern version");
43  }
44  rewriter.template replaceOpWithNewOp<SPIRVOp>(op, dstType,
45  adaptor.getOperands());
46  return success();
47  }
48 };
49 
50 } // namespace spirv
51 } // namespace mlir
52 
53 #endif // MLIR_CONVERSION_SPIRVCOMMON_PATTERN_H
This class implements a pattern rewriter for use with ConversionPatterns.
const TypeConverter * getTypeConverter() const
Return the type converter held by this pattern, or nullptr if the pattern does not require type conve...
OpConversionPattern is a wrapper around ConversionPattern that allows for matching and rewriting agai...
void dump()
Dump this operation.
Definition: OpDefinition.h:122
InFlightDiagnostic emitError(const Twine &message={})
Emit an error about fatal conditions with this operation, reporting up to any diagnostic handlers tha...
Definition: Operation.cpp:826
Location getLoc()
The source location the operation was defined or derived from.
Definition: OpDefinition.h:125
This provides public APIs that all operations should have.
std::enable_if_t<!std::is_convertible< CallbackT, Twine >::value, LogicalResult > notifyMatchFailure(Location loc, CallbackT &&reasonCallback)
Used to notify the listener that the IR failed to be rewritten because of a match failure,...
Definition: PatternMatch.h:724
LogicalResult convertType(Type t, SmallVectorImpl< Type > &results) const
Convert the given type.
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition: Types.h:74
bool isIndex() const
Definition: Types.cpp:64
Include the generated interface declarations.
Type getElementTypeOrSelf(Type type)
Return the element type or return the type itself.
Converts elementwise unary, binary and ternary standard operations to SPIR-V operations.
Definition: Pattern.h:23
LogicalResult matchAndRewrite(Op op, typename Op::Adaptor adaptor, ConversionPatternRewriter &rewriter) const override
Definition: Pattern.h:27