MLIR 24.0.0git
BuiltinDialect.cpp
Go to the documentation of this file.
1//===- BuiltinDialect.cpp - MLIR Builtin Dialect --------------------------===//
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 contains the Builtin dialect that contains all of the attributes,
10// operations, and types that are necessary for the validity of the IR.
11//
12//===----------------------------------------------------------------------===//
13
16#include "mlir/IR/Builders.h"
17#include "mlir/IR/BuiltinOps.h"
21#include "mlir/IR/TypeRange.h"
22
23using namespace mlir;
24
25//===----------------------------------------------------------------------===//
26// TableGen'erated dialect
27//===----------------------------------------------------------------------===//
28
29#include "mlir/IR/BuiltinDialect.cpp.inc"
30
31//===----------------------------------------------------------------------===//
32// BuiltinBlobManagerInterface
33//===----------------------------------------------------------------------===//
34
37
38//===----------------------------------------------------------------------===//
39// BuiltinOpAsmDialectInterface
40//===----------------------------------------------------------------------===//
41
42namespace {
43struct BuiltinOpAsmDialectInterface : public OpAsmDialectInterface {
44 BuiltinOpAsmDialectInterface(Dialect *dialect,
46 : OpAsmDialectInterface(dialect), blobManager(mgr) {}
47
48 AliasResult getAlias(Attribute attr, raw_ostream &os) const override {
49 if (llvm::isa<LocationAttr>(attr)) {
50 os << "loc";
51 return AliasResult::OverridableAlias;
52 }
53 if (auto distinct = llvm::dyn_cast<DistinctAttr>(attr))
54 if (!llvm::isa<UnitAttr>(distinct.getReferencedAttr())) {
55 os << "distinct";
56 return AliasResult::OverridableAlias;
57 }
59 }
60
61 AliasResult getAlias(Type type, raw_ostream &os) const final {
62 if (auto tupleType = llvm::dyn_cast<TupleType>(type)) {
63 if (tupleType.size() > 16) {
64 os << "tuple";
65 return AliasResult::OverridableAlias;
66 }
67 }
69 }
70
71 //===------------------------------------------------------------------===//
72 // Resources
73 //===------------------------------------------------------------------===//
74
75 std::string
76 getResourceKey(const AsmDialectResourceHandle &handle) const override {
77 return cast<DenseResourceElementsHandle>(handle).getKey().str();
78 }
79 FailureOr<AsmDialectResourceHandle>
80 declareResource(StringRef key) const final {
81 return blobManager.insert(key);
82 }
83 LogicalResult parseResource(AsmParsedResourceEntry &entry) const final {
84 FailureOr<AsmResourceBlob> blob = entry.parseAsBlob();
85 if (failed(blob))
86 return failure();
87
88 // Update the blob for this entry.
89 blobManager.update(entry.getKey(), std::move(*blob));
90 return success();
91 }
92 void
93 buildResources(Operation *op,
94 const SetVector<AsmDialectResourceHandle> &referencedResources,
95 AsmResourceBuilder &provider) const final {
96 blobManager.buildResources(provider, referencedResources.getArrayRef());
97 }
98
99private:
100 /// The blob manager for the dialect.
101 BuiltinBlobManagerInterface &blobManager;
102};
103} // namespace
104
105void BuiltinDialect::initialize() {
106 registerTypes();
107 registerAttributes();
108 registerLocationAttributes();
109 addOperations<
110#define GET_OP_LIST
111#include "mlir/IR/BuiltinOps.cpp.inc"
112 >();
113
114 auto &blobInterface = addInterface<BuiltinBlobManagerInterface>();
115 addInterface<BuiltinOpAsmDialectInterface>(blobInterface);
117}
118
119//===----------------------------------------------------------------------===//
120// ModuleOp
121//===----------------------------------------------------------------------===//
122
123void ModuleOp::build(OpBuilder &builder, OperationState &state,
124 std::optional<StringRef> name) {
125 state.addRegion()->emplaceBlock();
126 if (name) {
127 state.attributes.push_back(builder.getNamedAttr(
128 getSymNameAttrName(state.name), builder.getStringAttr(*name)));
129 }
130}
131
132/// Construct a module from the given context.
133ModuleOp ModuleOp::create(Location loc, std::optional<StringRef> name) {
134 OpBuilder builder(loc->getContext());
135 return ModuleOp::create(builder, loc, name);
136}
137
138DataLayoutSpecInterface ModuleOp::getDataLayoutSpec() {
139 // Take the first and only (if present) attribute that implements the
140 // interface. This needs a linear search, but is called only once per data
141 // layout object construction that is used for repeated queries.
142 for (NamedAttribute attr :
143 getOperation()->getDiscardableAttrDictionary().getValue())
144 if (auto spec = llvm::dyn_cast<DataLayoutSpecInterface>(attr.getValue()))
145 return spec;
146 return {};
147}
148
149TargetSystemSpecInterface ModuleOp::getTargetSystemSpec() {
150 // Take the first and only (if present) attribute that implements the
151 // interface. This needs a linear search, but is called only once per data
152 // layout object construction that is used for repeated queries.
153 for (NamedAttribute attr :
154 getOperation()->getDiscardableAttrDictionary().getValue())
155 if (auto spec = llvm::dyn_cast<TargetSystemSpecInterface>(attr.getValue()))
156 return spec;
157 return {};
158}
159
160LogicalResult ModuleOp::verify() {
161 // Check that none of the attributes are non-dialect attributes, except for
162 // the symbol related attributes.
163 for (auto attr : (*this)->getDiscardableAttrDictionary().getValue()) {
164 if (!attr.getName().strref().contains('.') &&
165 !llvm::is_contained(
166 ArrayRef<StringRef>{
167 getSymNameAttrName().getValue(),
168 mlir::SymbolOpInterface::getDefaultVisibilityAttrName()},
169 attr.getName().strref()))
170 return emitOpError() << "can only contain attributes with "
171 "dialect-prefixed names, found: '"
172 << attr.getName().getValue() << "'";
173 }
174
175 // Check that there is at most one data layout spec attribute.
176 StringRef layoutSpecAttrName;
177 DataLayoutSpecInterface layoutSpec;
178 for (const NamedAttribute &na :
179 (*this)->getDiscardableAttrDictionary().getValue()) {
180 if (auto spec = llvm::dyn_cast<DataLayoutSpecInterface>(na.getValue())) {
181 if (layoutSpec) {
182 InFlightDiagnostic diag =
183 emitOpError() << "expects at most one data layout attribute";
184 diag.attachNote() << "'" << layoutSpecAttrName
185 << "' is a data layout attribute";
186 diag.attachNote() << "'" << na.getName().getValue()
187 << "' is a data layout attribute";
188 }
189 layoutSpecAttrName = na.getName().strref();
190 layoutSpec = spec;
191 }
192 }
193
194 return success();
195}
196
197//===----------------------------------------------------------------------===//
198// UnrealizedConversionCastOp
199//===----------------------------------------------------------------------===//
200
201LogicalResult
202UnrealizedConversionCastOp::fold(FoldAdaptor adaptor,
203 SmallVectorImpl<OpFoldResult> &foldResults) {
204 OperandRange operands = getInputs();
205 ResultRange results = getOutputs();
206
207 if (operands.getType() == results.getType()) {
208 foldResults.append(operands.begin(), operands.end());
209 return success();
210 }
211
212 if (operands.empty())
213 return failure();
214
215 // Check that the input is a cast with results that all feed into this
216 // operation, and operand types that directly match the result types of this
217 // operation.
218 Value firstInput = operands.front();
219 auto inputOp = firstInput.getDefiningOp<UnrealizedConversionCastOp>();
220 if (!inputOp || inputOp.getResults() != operands ||
221 inputOp.getOperandTypes() != results.getTypes())
222 return failure();
223
224 // If everything matches up, we can fold the passthrough.
225 foldResults.append(inputOp->operand_begin(), inputOp->operand_end());
226 return success();
227}
228
229LogicalResult UnrealizedConversionCastOp::verify() {
230 // TODO: The verifier of external models is not called. This op verifier can
231 // be removed when that is fixed.
232 if (getNumResults() == 0)
233 return emitOpError() << "expected at least one result for cast operation";
234 return success();
235}
236
237//===----------------------------------------------------------------------===//
238// TableGen'd op method definitions
239//===----------------------------------------------------------------------===//
240
241#define GET_OP_CLASSES
242#include "mlir/IR/BuiltinOps.cpp.inc"
return success()
ResourceBlobManagerDialectInterfaceBase< DenseResourceElementsHandle > BuiltinBlobManagerInterface
static std::string diag(const llvm::Value &value)
The possible results of an alias query.
@ NoAlias
The two locations do not alias at all.
This class represents an opaque handle to a dialect resource entry.
This class represents a single parsed resource entry.
Definition AsmState.h:291
virtual FailureOr< AsmResourceBlob > parseAsBlob(BlobAllocatorFn allocator) const =0
Parse the resource entry represented by a binary blob.
virtual StringRef getKey() const =0
Return the key of the resource entry.
This class is used to build resource entries for use by the printer.
Definition AsmState.h:247
Attributes are known-constant values of operations.
Definition Attributes.h:25
MLIRContext * getContext() const
Return the context this attribute belongs to.
StringAttr getStringAttr(const Twine &bytes)
Definition Builders.cpp:271
NamedAttribute getNamedAttr(StringRef name, Attribute val)
Definition Builders.cpp:102
Dialects are groups of MLIR operations, types and attributes, as well as behavior associated with the...
Definition Dialect.h:38
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition Location.h:76
void push_back(NamedAttribute newAttribute)
Add an attribute with the specified name.
This class helps build Operations.
Definition Builders.h:210
This class implements the operand iterators for the Operation class.
Definition ValueRange.h:44
type_range getType() const
Operation is the basic unit of execution within MLIR.
Definition Operation.h:87
Block & emplaceBlock()
Definition Region.h:46
This class provides a base class for dialects implementing the resource blob interface.
This class implements the result iterators for the Operation class.
Definition ValueRange.h:248
type_range getTypes() const
type_range getType() const
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition Types.h:74
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition Value.h:96
Operation * getDefiningOp() const
If this value is the result of an operation, return the operation that defines it.
Definition Value.cpp:18
void addBytecodeInterface(BuiltinDialect *dialect)
Add the interfaces necessary for encoding the builtin dialect components in bytecode.
Include the generated interface declarations.
llvm::SetVector< T, Vector, Set, N > SetVector
Definition LLVM.h:125
This represents an operation in an abstracted form, suitable for use with the builder APIs.
Region * addRegion()
Create a region that should be attached to the operation.