MLIR 22.0.0git
LinalgDialect.cpp
Go to the documentation of this file.
1//===- Dialect.cpp - Implementation of the linalg dialect and types -------===//
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// This file implements the Linalg dialect types and dialect.
10//
11//===----------------------------------------------------------------------===//
12
23#include "mlir/Support/LLVM.h"
25
26#include "llvm/ADT/TypeSwitch.h"
27
28using namespace mlir;
29using namespace mlir::linalg;
30
31//===----------------------------------------------------------------------===//
32// LinalgDialect Dialect Interfaces
33//===----------------------------------------------------------------------===//
34
35namespace {
36
37struct LinalgInlinerInterface : public DialectInlinerInterface {
39
40 // We don't have any special restrictions on what can be inlined into
41 // destination regions (e.g. while/conditional bodies). Always allow it.
42 bool isLegalToInline(Region *dest, Region *src, bool wouldBeCloned,
43 IRMapping &valueMapping) const final {
44 return true;
45 }
46 // Operations in Linalg dialect are always legal to inline.
47 bool isLegalToInline(Operation *, Region *, bool, IRMapping &) const final {
48 return true;
49 }
50 // Handle the given inlined terminator by replacing it with a new operation
51 // as necessary. Required when the region has only one block.
52 void handleTerminator(Operation *op, ValueRange valuesToRepl) const final {}
53};
54
55} // namespace
56
57//===----------------------------------------------------------------------===//
58// LinalgDialect
59//===----------------------------------------------------------------------===//
60
61/// Attribute name used to memoize indexing maps for named ops.
62constexpr const ::llvm::StringLiteral
63 LinalgDialect::kMemoizedIndexingMapsAttrName;
64
65/// Trait to check if T provides a `regionBuilder` method.
66template <typename T, typename... Args>
67using has_region_builder = decltype(T::regionBuilder);
68template <typename T>
69using detect_has_region_builder = llvm::is_detected<has_region_builder, T>;
70
71/// SFINAE helper for single C++ class without a `regionBuilder` method (e.g.
72/// an OpInterface).
73template <typename OpType, typename = std::enable_if_t<
76 llvm::StringMap<LinalgDialect::RegionBuilderFunType> &map) {
77 // Do nothing.
78}
79
80template <typename OpType,
81 typename = std::enable_if_t<detect_has_region_builder<OpType>::value>,
82 typename = void>
84 llvm::StringMap<LinalgDialect::RegionBuilderFunType> &map) {
85 map.insert(std::make_pair(
86 OpType::getOperationName(),
87 static_cast<LinalgDialect::RegionBuilderFunType>(OpType::regionBuilder)));
88}
89
90template <typename... OpTypes>
92 llvm::StringMap<LinalgDialect::RegionBuilderFunType> &map) {
94}
95
96void mlir::linalg::LinalgDialect::initialize() {
97 addAttributes<
98#define GET_ATTRDEF_LIST
99#include "mlir/Dialect/Linalg/IR/LinalgOpsAttrDefs.cpp.inc"
100 >();
101 addOperations<
102#define GET_OP_LIST
103#include "mlir/Dialect/Linalg/IR/LinalgOps.cpp.inc"
104 >();
105 addOperations<
106#define GET_OP_LIST
107#include "mlir/Dialect/Linalg/IR/LinalgStructuredOps.cpp.inc"
108 >();
109 addOperations<
110#define GET_OP_LIST
111#include "mlir/Dialect/Linalg/IR/LinalgRelayoutOps.cpp.inc"
112 >();
113
114 // Fill the Linalg-specific OpName to RegionBuilder map.
116#define GET_OP_LIST
117#include "mlir/Dialect/Linalg/IR/LinalgStructuredOps.cpp.inc"
118 >(namedStructuredOpRegionBuilders);
119
120 addInterfaces<LinalgInlinerInterface>();
121
122 declarePromisedInterface<shard::ShardingInterface, GenericOp>();
123 declarePromisedInterfaces<shard::ShardingInterface,
124#define GET_OP_LIST
125#include "mlir/Dialect/Linalg/IR/LinalgStructuredOps.cpp.inc"
126 >();
127 declarePromisedInterface<SubsetOpInterface, CopyOp>();
128 declarePromisedInterface<SubsetInsertionOpInterface, CopyOp>();
129
130 // ValueBoundsOpInterface
131 declarePromisedInterface<ValueBoundsOpInterface, IndexOp>();
132
133 declarePromisedInterface<PartialReductionOpInterface, linalg::GenericOp>();
134
135 // Tiling Interface
136 declarePromisedInterface<TilingInterface, linalg::GenericOp>();
137 declarePromisedInterfaces<TilingInterface,
138#define GET_OP_LIST
139#include "mlir/Dialect/Linalg/IR/LinalgStructuredOps.cpp.inc"
140 >();
141 declarePromisedInterfaces<TilingInterface,
142#define GET_OP_LIST
143#include "mlir/Dialect/Linalg/IR/LinalgRelayoutOps.cpp.inc"
144 >();
145 declarePromisedInterfaces<PartialReductionOpInterface,
146#define GET_OP_LIST
147#include "mlir/Dialect/Linalg/IR/LinalgStructuredOps.cpp.inc"
148 >();
149 declarePromisedInterfaces<bufferization::BufferizableOpInterface,
150#define GET_OP_LIST
151#include "mlir/Dialect/Linalg/IR/LinalgStructuredOps.cpp.inc"
152 >();
153}
154
155LogicalResult LinalgDialect::verifyOperationAttribute(Operation *op,
156 NamedAttribute attr) {
157 if (attr.getName() == LinalgDialect::kMemoizedIndexingMapsAttrName)
158 return success();
159 return op->emitError() << "attribute '" << attr.getName()
160 << "' not supported by the linalg dialect";
161}
162
163#include "mlir/Dialect/Linalg/IR/LinalgOpsEnums.cpp.inc"
164
165#define GET_ATTRDEF_CLASSES
166#include "mlir/Dialect/Linalg/IR/LinalgOpsAttrDefs.cpp.inc"
167
168#include "mlir/Dialect/Linalg/IR/LinalgOpsDialect.cpp.inc"
return success()
static bool isLegalToInline(InlinerInterface &interface, Region *src, Region *insertRegion, bool shouldCloneInlinedRegion, IRMapping &valueMapping)
Utility to check that all of the operations within 'src' can be inlined.
llvm::is_detected< has_region_builder, T > detect_has_region_builder
decltype(T::regionBuilder) has_region_builder
Trait to check if T provides a regionBuilder method.
void addNamedOpBuilders(llvm::StringMap< LinalgDialect::RegionBuilderFunType > &map)
void addNamedOpBuilderImpl(llvm::StringMap< LinalgDialect::RegionBuilderFunType > &map)
SFINAE helper for single C++ class without a regionBuilder method (e.g.
This is the interface that must be implemented by the dialects of operations to be inlined.
DialectInlinerInterface(Dialect *dialect)
NamedAttribute represents a combination of a name and an Attribute value.
Definition Attributes.h:164
StringAttr getName() const
Return the name of the attribute.
Operation is the basic unit of execution within MLIR.
Definition Operation.h:88
InFlightDiagnostic emitError(const Twine &message={})
Emit an error about fatal conditions with this operation, reporting up to any diagnostic handlers tha...
Include the generated interface declarations.