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