MLIR 22.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
16#include "llvm/ADT/StringExtras.h"
17#include "llvm/Support/FormatVariadic.h"
18#include <string>
19
20namespace mlir {
21namespace spirv {
22
23//===----------------------------------------------------------------------===//
24// Utility Functions
25//===----------------------------------------------------------------------===//
26
27/// Converts a SPIR-V Decoration enum value to its snake_case string
28/// representation for use in MLIR attributes.
29inline std::string getDecorationString(spirv::Decoration decor) {
30 return llvm::convertToSnakeFromCamelCase(stringifyDecoration(decor));
31}
32
33/// Converts elementwise unary, binary and ternary standard operations to SPIR-V
34/// operations.
35template <typename Op, typename SPIRVOp>
36struct ElementwiseOpPattern : public OpConversionPattern<Op> {
37 using OpConversionPattern<Op>::OpConversionPattern;
38
39 LogicalResult
40 matchAndRewrite(Op op, typename Op::Adaptor adaptor,
41 ConversionPatternRewriter &rewriter) const override {
42 assert(adaptor.getOperands().size() <= 3);
43 Type dstType = this->getTypeConverter()->convertType(op.getType());
44 if (!dstType) {
45 return rewriter.notifyMatchFailure(
46 op->getLoc(),
47 llvm::formatv("failed to convert type {0} for SPIR-V", op.getType()));
48 }
49
50 if (SPIRVOp::template hasTrait<OpTrait::spirv::UnsignedOp>() &&
51 !getElementTypeOrSelf(op.getType()).isIndex() &&
52 dstType != op.getType()) {
53 op.dump();
54 return op.emitError("bitwidth emulation is not implemented yet on "
55 "unsigned op pattern version");
56 }
57 rewriter.template replaceOpWithNewOp<SPIRVOp>(op, dstType,
58 adaptor.getOperands());
59 return success();
60 }
61};
62
63} // namespace spirv
64} // namespace mlir
65
66#endif // MLIR_CONVERSION_SPIRVCOMMON_PATTERN_H
return success()
void dump()
Dump this operation.
InFlightDiagnostic emitError(const Twine &message={})
Emit an error about fatal conditions with this operation, reporting up to any diagnostic handlers tha...
Location getLoc()
The source location the operation was defined or derived from.
This provides public APIs that all operations should have.
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:54
std::string getDecorationString(spirv::Decoration decor)
Converts a SPIR-V Decoration enum value to its snake_case string representation for use in MLIR attri...
Definition Pattern.h:29
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:36
LogicalResult matchAndRewrite(Op op, typename Op::Adaptor adaptor, ConversionPatternRewriter &rewriter) const override
Definition Pattern.h:40