1//===- Operation.cpp - Operation support code -----------------------------===//
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#include "mlir/IR/Operation.h"
10#include "mlir/IR/Attributes.h"
11#include "mlir/IR/BuiltinAttributes.h"
12#include "mlir/IR/BuiltinTypes.h"
13#include "mlir/IR/Dialect.h"
14#include "mlir/IR/IRMapping.h"
15#include "mlir/IR/Matchers.h"
16#include "mlir/IR/OpImplementation.h"
17#include "mlir/IR/OperationSupport.h"
18#include "mlir/IR/PatternMatch.h"
19#include "mlir/IR/TypeUtilities.h"
20#include "mlir/Interfaces/FoldInterfaces.h"
21#include "llvm/ADT/SmallVector.h"
22#include "llvm/ADT/StringExtras.h"
23#include "llvm/Support/ErrorHandling.h"
24#include <numeric>
25#include <optional>
26
27using namespace mlir;
28
29//===----------------------------------------------------------------------===//
30// Operation
31//===----------------------------------------------------------------------===//
32
33/// Create a new Operation from operation state.
34Operation *Operation::create(const OperationState &state) {
35 Operation *op =
36 create(state.location, state.name, state.types, state.operands,
37 state.attributes.getDictionary(state.getContext()),
38 state.properties, state.successors, state.regions);
39 if (LLVM_UNLIKELY(state.propertiesAttr)) {
40 assert(!state.properties);
41 LogicalResult result =
42 op->setPropertiesFromAttribute(attr: state.propertiesAttr,
43 /*diagnostic=*/emitError: nullptr);
44 assert(result.succeeded() && "invalid properties in op creation");
45 (void)result;
46 }
47 return op;
48}
49
50/// Create a new Operation with the specific fields.
51Operation *Operation::create(Location location, OperationName name,
52 TypeRange resultTypes, ValueRange operands,
53 NamedAttrList &&attributes,
54 OpaqueProperties properties, BlockRange successors,
55 RegionRange regions) {
56 unsigned numRegions = regions.size();
57 Operation *op =
58 create(location, name, resultTypes, operands, attributes: std::move(attributes),
59 properties, successors, numRegions);
60 for (unsigned i = 0; i < numRegions; ++i)
61 if (regions[i])
62 op->getRegion(index: i).takeBody(other&: *regions[i]);
63 return op;
64}
65
66/// Create a new Operation with the specific fields.
67Operation *Operation::create(Location location, OperationName name,
68 TypeRange resultTypes, ValueRange operands,
69 NamedAttrList &&attributes,
70 OpaqueProperties properties, BlockRange successors,
71 unsigned numRegions) {
72 // Populate default attributes.
73 name.populateDefaultAttrs(attrs&: attributes);
74
75 return create(location, name, resultTypes, operands,
76 attributes.getDictionary(location.getContext()), properties,
77 successors, numRegions);
78}
79
80/// Overload of create that takes an existing DictionaryAttr to avoid
81/// unnecessarily uniquing a list of attributes.
82Operation *Operation::create(Location location, OperationName name,
83 TypeRange resultTypes, ValueRange operands,
84 DictionaryAttr attributes,
85 OpaqueProperties properties, BlockRange successors,
86 unsigned numRegions) {
87 assert(llvm::all_of(resultTypes, [](Type t) { return t; }) &&
88 "unexpected null result type");
89
90 // We only need to allocate additional memory for a subset of results.
91 unsigned numTrailingResults = OpResult::getNumTrailing(numResults: resultTypes.size());
92 unsigned numInlineResults = OpResult::getNumInline(numResults: resultTypes.size());
93 unsigned numSuccessors = successors.size();
94 unsigned numOperands = operands.size();
95 unsigned numResults = resultTypes.size();
96 int opPropertiesAllocSize = llvm::alignTo<8>(Value: name.getOpPropertyByteSize());
97
98 // If the operation is known to have no operands, don't allocate an operand
99 // storage.
100 bool needsOperandStorage =
101 operands.empty() ? !name.hasTrait<OpTrait::ZeroOperands>() : true;
102
103 // Compute the byte size for the operation and the operand storage. This takes
104 // into account the size of the operation, its trailing objects, and its
105 // prefixed objects.
106 size_t byteSize =
107 totalSizeToAlloc<detail::OperandStorage, detail::OpProperties,
108 BlockOperand, Region, OpOperand>(
109 Counts: needsOperandStorage ? 1 : 0, Counts: opPropertiesAllocSize, Counts: numSuccessors,
110 Counts: numRegions, Counts: numOperands);
111 size_t prefixByteSize = llvm::alignTo(
112 Value: Operation::prefixAllocSize(numOutOfLineResults: numTrailingResults, numInlineResults),
113 Align: alignof(Operation));
114 char *mallocMem = reinterpret_cast<char *>(malloc(size: byteSize + prefixByteSize));
115 void *rawMem = mallocMem + prefixByteSize;
116
117 // Create the new Operation.
118 Operation *op = ::new (rawMem) Operation(
119 location, name, numResults, numSuccessors, numRegions,
120 opPropertiesAllocSize, attributes, properties, needsOperandStorage);
121
122 assert((numSuccessors == 0 || op->mightHaveTrait<OpTrait::IsTerminator>()) &&
123 "unexpected successors in a non-terminator operation");
124
125 // Initialize the results.
126 auto resultTypeIt = resultTypes.begin();
127 for (unsigned i = 0; i < numInlineResults; ++i, ++resultTypeIt)
128 new (op->getInlineOpResult(resultNumber: i)) detail::InlineOpResult(*resultTypeIt, i);
129 for (unsigned i = 0; i < numTrailingResults; ++i, ++resultTypeIt) {
130 new (op->getOutOfLineOpResult(resultNumber: i))
131 detail::OutOfLineOpResult(*resultTypeIt, i);
132 }
133
134 // Initialize the regions.
135 for (unsigned i = 0; i != numRegions; ++i)
136 new (&op->getRegion(index: i)) Region(op);
137
138 // Initialize the operands.
139 if (needsOperandStorage) {
140 new (&op->getOperandStorage()) detail::OperandStorage(
141 op, op->getTrailingObjects<OpOperand>(), operands);
142 }
143
144 // Initialize the successors.
145 auto blockOperands = op->getBlockOperands();
146 for (unsigned i = 0; i != numSuccessors; ++i)
147 new (&blockOperands[i]) BlockOperand(op, successors[i]);
148
149 // This must be done after properties are initalized.
150 op->setAttrs(attributes);
151
152 return op;
153}
154
155Operation::Operation(Location location, OperationName name, unsigned numResults,
156 unsigned numSuccessors, unsigned numRegions,
157 int fullPropertiesStorageSize, DictionaryAttr attributes,
158 OpaqueProperties properties, bool hasOperandStorage)
159 : location(location), numResults(numResults), numSuccs(numSuccessors),
160 numRegions(numRegions), hasOperandStorage(hasOperandStorage),
161 propertiesStorageSize((fullPropertiesStorageSize + 7) / 8), name(name) {
162 assert(attributes && "unexpected null attribute dictionary");
163 assert(fullPropertiesStorageSize <= propertiesCapacity &&
164 "Properties size overflow");
165#ifndef NDEBUG
166 if (!getDialect() && !getContext()->allowsUnregisteredDialects())
167 llvm::report_fatal_error(
168 reason: name.getStringRef() +
169 " created with unregistered dialect. If this is intended, please call "
170 "allowUnregisteredDialects() on the MLIRContext, or use "
171 "-allow-unregistered-dialect with the MLIR tool used.");
172#endif
173 if (fullPropertiesStorageSize)
174 name.initOpProperties(storage: getPropertiesStorage(), init: properties);
175}
176
177// Operations are deleted through the destroy() member because they are
178// allocated via malloc.
179Operation::~Operation() {
180 assert(block == nullptr && "operation destroyed but still in a block");
181#ifndef NDEBUG
182 if (!use_empty()) {
183 {
184 InFlightDiagnostic diag =
185 emitOpError(message: "operation destroyed but still has uses");
186 for (Operation *user : getUsers())
187 diag.attachNote(noteLoc: user->getLoc()) << "- use: " << *user << "\n";
188 }
189 llvm::report_fatal_error(reason: "operation destroyed but still has uses");
190 }
191#endif
192 // Explicitly run the destructors for the operands.
193 if (hasOperandStorage)
194 getOperandStorage().~OperandStorage();
195
196 // Explicitly run the destructors for the successors.
197 for (auto &successor : getBlockOperands())
198 successor.~BlockOperand();
199
200 // Explicitly destroy the regions.
201 for (auto &region : getRegions())
202 region.~Region();
203 if (propertiesStorageSize)
204 name.destroyOpProperties(properties: getPropertiesStorage());
205}
206
207/// Destroy this operation or one of its subclasses.
208void Operation::destroy() {
209 // Operations may have additional prefixed allocation, which needs to be
210 // accounted for here when computing the address to free.
211 char *rawMem = reinterpret_cast<char *>(this) -
212 llvm::alignTo(Value: prefixAllocSize(), Align: alignof(Operation));
213 this->~Operation();
214 free(ptr: rawMem);
215}
216
217/// Return true if this operation is a proper ancestor of the `other`
218/// operation.
219bool Operation::isProperAncestor(Operation *other) {
220 while ((other = other->getParentOp()))
221 if (this == other)
222 return true;
223 return false;
224}
225
226/// Replace any uses of 'from' with 'to' within this operation.
227void Operation::replaceUsesOfWith(Value from, Value to) {
228 if (from == to)
229 return;
230 for (auto &operand : getOpOperands())
231 if (operand.get() == from)
232 operand.set(to);
233}
234
235/// Replace the current operands of this operation with the ones provided in
236/// 'operands'.
237void Operation::setOperands(ValueRange operands) {
238 if (LLVM_LIKELY(hasOperandStorage))
239 return getOperandStorage().setOperands(owner: this, values: operands);
240 assert(operands.empty() && "setting operands without an operand storage");
241}
242
243/// Replace the operands beginning at 'start' and ending at 'start' + 'length'
244/// with the ones provided in 'operands'. 'operands' may be smaller or larger
245/// than the range pointed to by 'start'+'length'.
246void Operation::setOperands(unsigned start, unsigned length,
247 ValueRange operands) {
248 assert((start + length) <= getNumOperands() &&
249 "invalid operand range specified");
250 if (LLVM_LIKELY(hasOperandStorage))
251 return getOperandStorage().setOperands(owner: this, start, length, operands);
252 assert(operands.empty() && "setting operands without an operand storage");
253}
254
255/// Insert the given operands into the operand list at the given 'index'.
256void Operation::insertOperands(unsigned index, ValueRange operands) {
257 if (LLVM_LIKELY(hasOperandStorage))
258 return setOperands(start: index, /*length=*/0, operands);
259 assert(operands.empty() && "inserting operands without an operand storage");
260}
261
262//===----------------------------------------------------------------------===//
263// Diagnostics
264//===----------------------------------------------------------------------===//
265
266/// Emit an error about fatal conditions with this operation, reporting up to
267/// any diagnostic handlers that may be listening.
268InFlightDiagnostic Operation::emitError(const Twine &message) {
269 InFlightDiagnostic diag = mlir::emitError(loc: getLoc(), message);
270 if (getContext()->shouldPrintOpOnDiagnostic()) {
271 diag.attachNote(noteLoc: getLoc())
272 .append(arg: "see current operation: ")
273 .appendOp(op&: *this, flags: OpPrintingFlags().printGenericOpForm());
274 }
275 return diag;
276}
277
278/// Emit a warning about this operation, reporting up to any diagnostic
279/// handlers that may be listening.
280InFlightDiagnostic Operation::emitWarning(const Twine &message) {
281 InFlightDiagnostic diag = mlir::emitWarning(loc: getLoc(), message);
282 if (getContext()->shouldPrintOpOnDiagnostic())
283 diag.attachNote(noteLoc: getLoc()) << "see current operation: " << *this;
284 return diag;
285}
286
287/// Emit a remark about this operation, reporting up to any diagnostic
288/// handlers that may be listening.
289InFlightDiagnostic Operation::emitRemark(const Twine &message) {
290 InFlightDiagnostic diag = mlir::emitRemark(loc: getLoc(), message);
291 if (getContext()->shouldPrintOpOnDiagnostic())
292 diag.attachNote(noteLoc: getLoc()) << "see current operation: " << *this;
293 return diag;
294}
295
296DictionaryAttr Operation::getAttrDictionary() {
297 if (getPropertiesStorageSize()) {
298 NamedAttrList attrsList = attrs;
299 getName().populateInherentAttrs(op: this, attrs&: attrsList);
300 return attrsList.getDictionary(getContext());
301 }
302 return attrs;
303}
304
305void Operation::setAttrs(DictionaryAttr newAttrs) {
306 assert(newAttrs && "expected valid attribute dictionary");
307 if (getPropertiesStorageSize()) {
308 // We're spliting the providing DictionaryAttr by removing the inherentAttr
309 // which will be stored in the properties.
310 SmallVector<NamedAttribute> discardableAttrs;
311 discardableAttrs.reserve(N: newAttrs.size());
312 for (NamedAttribute attr : newAttrs) {
313 if (getInherentAttr(attr.getName()))
314 setInherentAttr(attr.getName(), attr.getValue());
315 else
316 discardableAttrs.push_back(attr);
317 }
318 if (discardableAttrs.size() != newAttrs.size())
319 newAttrs = DictionaryAttr::get(getContext(), discardableAttrs);
320 }
321 attrs = newAttrs;
322}
323void Operation::setAttrs(ArrayRef<NamedAttribute> newAttrs) {
324 if (getPropertiesStorageSize()) {
325 // We're spliting the providing array of attributes by removing the inherentAttr
326 // which will be stored in the properties.
327 SmallVector<NamedAttribute> discardableAttrs;
328 discardableAttrs.reserve(N: newAttrs.size());
329 for (NamedAttribute attr : newAttrs) {
330 if (getInherentAttr(attr.getName()))
331 setInherentAttr(attr.getName(), attr.getValue());
332 else
333 discardableAttrs.push_back(Elt: attr);
334 }
335 attrs = DictionaryAttr::get(getContext(), discardableAttrs);
336 return;
337 }
338 attrs = DictionaryAttr::get(getContext(), newAttrs);
339}
340
341std::optional<Attribute> Operation::getInherentAttr(StringRef name) {
342 return getName().getInherentAttr(op: this, name);
343}
344
345void Operation::setInherentAttr(StringAttr name, Attribute value) {
346 getName().setInherentAttr(op: this, name: name, value);
347}
348
349Attribute Operation::getPropertiesAsAttribute() {
350 std::optional<RegisteredOperationName> info = getRegisteredInfo();
351 if (LLVM_UNLIKELY(!info))
352 return *getPropertiesStorage().as<Attribute *>();
353 return info->getOpPropertiesAsAttribute(op: this);
354}
355LogicalResult Operation::setPropertiesFromAttribute(
356 Attribute attr, function_ref<InFlightDiagnostic()> emitError) {
357 std::optional<RegisteredOperationName> info = getRegisteredInfo();
358 if (LLVM_UNLIKELY(!info)) {
359 *getPropertiesStorage().as<Attribute *>() = attr;
360 return success();
361 }
362 return info->setOpPropertiesFromAttribute(
363 opName: this->getName(), properties: this->getPropertiesStorage(), attr, emitError);
364}
365
366void Operation::copyProperties(OpaqueProperties rhs) {
367 name.copyOpProperties(lhs: getPropertiesStorage(), rhs);
368}
369
370llvm::hash_code Operation::hashProperties() {
371 return name.hashOpProperties(properties: getPropertiesStorage());
372}
373
374//===----------------------------------------------------------------------===//
375// Operation Ordering
376//===----------------------------------------------------------------------===//
377
378constexpr unsigned Operation::kInvalidOrderIdx;
379constexpr unsigned Operation::kOrderStride;
380
381/// Given an operation 'other' that is within the same parent block, return
382/// whether the current operation is before 'other' in the operation list
383/// of the parent block.
384/// Note: This function has an average complexity of O(1), but worst case may
385/// take O(N) where N is the number of operations within the parent block.
386bool Operation::isBeforeInBlock(Operation *other) {
387 assert(block && "Operations without parent blocks have no order.");
388 assert(other && other->block == block &&
389 "Expected other operation to have the same parent block.");
390 // If the order of the block is already invalid, directly recompute the
391 // parent.
392 if (!block->isOpOrderValid()) {
393 block->recomputeOpOrder();
394 } else {
395 // Update the order either operation if necessary.
396 updateOrderIfNecessary();
397 other->updateOrderIfNecessary();
398 }
399
400 return orderIndex < other->orderIndex;
401}
402
403/// Update the order index of this operation of this operation if necessary,
404/// potentially recomputing the order of the parent block.
405void Operation::updateOrderIfNecessary() {
406 assert(block && "expected valid parent");
407
408 // If the order is valid for this operation there is nothing to do.
409 if (hasValidOrder())
410 return;
411 Operation *blockFront = &block->front();
412 Operation *blockBack = &block->back();
413
414 // This method is expected to only be invoked on blocks with more than one
415 // operation.
416 assert(blockFront != blockBack && "expected more than one operation");
417
418 // If the operation is at the end of the block.
419 if (this == blockBack) {
420 Operation *prevNode = getPrevNode();
421 if (!prevNode->hasValidOrder())
422 return block->recomputeOpOrder();
423
424 // Add the stride to the previous operation.
425 orderIndex = prevNode->orderIndex + kOrderStride;
426 return;
427 }
428
429 // If this is the first operation try to use the next operation to compute the
430 // ordering.
431 if (this == blockFront) {
432 Operation *nextNode = getNextNode();
433 if (!nextNode->hasValidOrder())
434 return block->recomputeOpOrder();
435 // There is no order to give this operation.
436 if (nextNode->orderIndex == 0)
437 return block->recomputeOpOrder();
438
439 // If we can't use the stride, just take the middle value left. This is safe
440 // because we know there is at least one valid index to assign to.
441 if (nextNode->orderIndex <= kOrderStride)
442 orderIndex = (nextNode->orderIndex / 2);
443 else
444 orderIndex = kOrderStride;
445 return;
446 }
447
448 // Otherwise, this operation is between two others. Place this operation in
449 // the middle of the previous and next if possible.
450 Operation *prevNode = getPrevNode(), *nextNode = getNextNode();
451 if (!prevNode->hasValidOrder() || !nextNode->hasValidOrder())
452 return block->recomputeOpOrder();
453 unsigned prevOrder = prevNode->orderIndex, nextOrder = nextNode->orderIndex;
454
455 // Check to see if there is a valid order between the two.
456 if (prevOrder + 1 == nextOrder)
457 return block->recomputeOpOrder();
458 orderIndex = prevOrder + ((nextOrder - prevOrder) / 2);
459}
460
461//===----------------------------------------------------------------------===//
462// ilist_traits for Operation
463//===----------------------------------------------------------------------===//
464
465auto llvm::ilist_detail::SpecificNodeAccess<
466 typename llvm::ilist_detail::compute_node_options<
467 ::mlir::Operation>::type>::getNodePtr(pointer n) -> node_type * {
468 return NodeAccess::getNodePtr<OptionsT>(N: n);
469}
470
471auto llvm::ilist_detail::SpecificNodeAccess<
472 typename llvm::ilist_detail::compute_node_options<
473 ::mlir::Operation>::type>::getNodePtr(const_pointer n)
474 -> const node_type * {
475 return NodeAccess::getNodePtr<OptionsT>(N: n);
476}
477
478auto llvm::ilist_detail::SpecificNodeAccess<
479 typename llvm::ilist_detail::compute_node_options<
480 ::mlir::Operation>::type>::getValuePtr(node_type *n) -> pointer {
481 return NodeAccess::getValuePtr<OptionsT>(N: n);
482}
483
484auto llvm::ilist_detail::SpecificNodeAccess<
485 typename llvm::ilist_detail::compute_node_options<
486 ::mlir::Operation>::type>::getValuePtr(const node_type *n)
487 -> const_pointer {
488 return NodeAccess::getValuePtr<OptionsT>(N: n);
489}
490
491void llvm::ilist_traits<::mlir::Operation>::deleteNode(Operation *op) {
492 op->destroy();
493}
494
495Block *llvm::ilist_traits<::mlir::Operation>::getContainingBlock() {
496 size_t offset(size_t(&((Block *)nullptr->*Block::getSublistAccess(nullptr))));
497 iplist<Operation> *anchor(static_cast<iplist<Operation> *>(this));
498 return reinterpret_cast<Block *>(reinterpret_cast<char *>(anchor) - offset);
499}
500
501/// This is a trait method invoked when an operation is added to a block. We
502/// keep the block pointer up to date.
503void llvm::ilist_traits<::mlir::Operation>::addNodeToList(Operation *op) {
504 assert(!op->getBlock() && "already in an operation block!");
505 op->block = getContainingBlock();
506
507 // Invalidate the order on the operation.
508 op->orderIndex = Operation::kInvalidOrderIdx;
509}
510
511/// This is a trait method invoked when an operation is removed from a block.
512/// We keep the block pointer up to date.
513void llvm::ilist_traits<::mlir::Operation>::removeNodeFromList(Operation *op) {
514 assert(op->block && "not already in an operation block!");
515 op->block = nullptr;
516}
517
518/// This is a trait method invoked when an operation is moved from one block
519/// to another. We keep the block pointer up to date.
520void llvm::ilist_traits<::mlir::Operation>::transferNodesFromList(
521 ilist_traits<Operation> &otherList, op_iterator first, op_iterator last) {
522 Block *curParent = getContainingBlock();
523
524 // Invalidate the ordering of the parent block.
525 curParent->invalidateOpOrder();
526
527 // If we are transferring operations within the same block, the block
528 // pointer doesn't need to be updated.
529 if (curParent == otherList.getContainingBlock())
530 return;
531
532 // Update the 'block' member of each operation.
533 for (; first != last; ++first)
534 first->block = curParent;
535}
536
537/// Remove this operation (and its descendants) from its Block and delete
538/// all of them.
539void Operation::erase() {
540 if (auto *parent = getBlock())
541 parent->getOperations().erase(IT: this);
542 else
543 destroy();
544}
545
546/// Remove the operation from its parent block, but don't delete it.
547void Operation::remove() {
548 if (Block *parent = getBlock())
549 parent->getOperations().remove(IT: this);
550}
551
552/// Unlink this operation from its current block and insert it right before
553/// `existingOp` which may be in the same or another block in the same
554/// function.
555void Operation::moveBefore(Operation *existingOp) {
556 moveBefore(existingOp->getBlock(), existingOp->getIterator());
557}
558
559/// Unlink this operation from its current basic block and insert it right
560/// before `iterator` in the specified basic block.
561void Operation::moveBefore(Block *block,
562 llvm::iplist<Operation>::iterator iterator) {
563 block->getOperations().splice(iterator, getBlock()->getOperations(),
564 getIterator());
565}
566
567/// Unlink this operation from its current block and insert it right after
568/// `existingOp` which may be in the same or another block in the same function.
569void Operation::moveAfter(Operation *existingOp) {
570 moveAfter(existingOp->getBlock(), existingOp->getIterator());
571}
572
573/// Unlink this operation from its current block and insert it right after
574/// `iterator` in the specified block.
575void Operation::moveAfter(Block *block,
576 llvm::iplist<Operation>::iterator iterator) {
577 assert(iterator != block->end() && "cannot move after end of block");
578 moveBefore(block, iterator: std::next(x: iterator));
579}
580
581/// This drops all operand uses from this operation, which is an essential
582/// step in breaking cyclic dependences between references when they are to
583/// be deleted.
584void Operation::dropAllReferences() {
585 for (auto &op : getOpOperands())
586 op.drop();
587
588 for (auto &region : getRegions())
589 region.dropAllReferences();
590
591 for (auto &dest : getBlockOperands())
592 dest.drop();
593}
594
595/// This drops all uses of any values defined by this operation or its nested
596/// regions, wherever they are located.
597void Operation::dropAllDefinedValueUses() {
598 dropAllUses();
599
600 for (auto &region : getRegions())
601 for (auto &block : region)
602 block.dropAllDefinedValueUses();
603}
604
605void Operation::setSuccessor(Block *block, unsigned index) {
606 assert(index < getNumSuccessors());
607 getBlockOperands()[index].set(block);
608}
609
610#ifndef NDEBUG
611/// Assert that the folded results (in case of values) have the same type as
612/// the results of the given op.
613static void checkFoldResultTypes(Operation *op,
614 SmallVectorImpl<OpFoldResult> &results) {
615 if (results.empty())
616 return;
617
618 for (auto [ofr, opResult] : llvm::zip_equal(t&: results, u: op->getResults())) {
619 if (auto value = dyn_cast<Value>(Val&: ofr)) {
620 if (value.getType() != opResult.getType()) {
621 op->emitOpError() << "folder produced a value of incorrect type: "
622 << opResult.getType()
623 << ", expected: " << value.getType();
624 assert(false && "incorrect fold result type");
625 }
626 }
627 }
628}
629#endif // NDEBUG
630
631/// Attempt to fold this operation using the Op's registered foldHook.
632LogicalResult Operation::fold(ArrayRef<Attribute> operands,
633 SmallVectorImpl<OpFoldResult> &results) {
634 // If we have a registered operation definition matching this one, use it to
635 // try to constant fold the operation.
636 if (succeeded(result: name.foldHook(op: this, operands, results))) {
637#ifndef NDEBUG
638 checkFoldResultTypes(op: this, results);
639#endif // NDEBUG
640 return success();
641 }
642
643 // Otherwise, fall back on the dialect hook to handle it.
644 Dialect *dialect = getDialect();
645 if (!dialect)
646 return failure();
647
648 auto *interface = dyn_cast<DialectFoldInterface>(Val: dialect);
649 if (!interface)
650 return failure();
651
652 LogicalResult status = interface->fold(op: this, operands, results);
653#ifndef NDEBUG
654 if (succeeded(result: status))
655 checkFoldResultTypes(op: this, results);
656#endif // NDEBUG
657 return status;
658}
659
660LogicalResult Operation::fold(SmallVectorImpl<OpFoldResult> &results) {
661 // Check if any operands are constants.
662 SmallVector<Attribute> constants;
663 constants.assign(NumElts: getNumOperands(), Elt: Attribute());
664 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
665 matchPattern(value: getOperand(idx: i), pattern: m_Constant(bind_value: &constants[i]));
666 return fold(operands: constants, results);
667}
668
669/// Emit an error with the op name prefixed, like "'dim' op " which is
670/// convenient for verifiers.
671InFlightDiagnostic Operation::emitOpError(const Twine &message) {
672 return emitError() << "'" << getName() << "' op " << message;
673}
674
675//===----------------------------------------------------------------------===//
676// Operation Cloning
677//===----------------------------------------------------------------------===//
678
679Operation::CloneOptions::CloneOptions()
680 : cloneRegionsFlag(false), cloneOperandsFlag(false) {}
681
682Operation::CloneOptions::CloneOptions(bool cloneRegions, bool cloneOperands)
683 : cloneRegionsFlag(cloneRegions), cloneOperandsFlag(cloneOperands) {}
684
685Operation::CloneOptions Operation::CloneOptions::all() {
686 return CloneOptions().cloneRegions().cloneOperands();
687}
688
689Operation::CloneOptions &Operation::CloneOptions::cloneRegions(bool enable) {
690 cloneRegionsFlag = enable;
691 return *this;
692}
693
694Operation::CloneOptions &Operation::CloneOptions::cloneOperands(bool enable) {
695 cloneOperandsFlag = enable;
696 return *this;
697}
698
699/// Create a deep copy of this operation but keep the operation regions empty.
700/// Operands are remapped using `mapper` (if present), and `mapper` is updated
701/// to contain the results. The `mapResults` flag specifies whether the results
702/// of the cloned operation should be added to the map.
703Operation *Operation::cloneWithoutRegions(IRMapping &mapper) {
704 return clone(mapper, options: CloneOptions::all().cloneRegions(enable: false));
705}
706
707Operation *Operation::cloneWithoutRegions() {
708 IRMapping mapper;
709 return cloneWithoutRegions(mapper);
710}
711
712/// Create a deep copy of this operation, remapping any operands that use
713/// values outside of the operation using the map that is provided (leaving
714/// them alone if no entry is present). Replaces references to cloned
715/// sub-operations to the corresponding operation that is copied, and adds
716/// those mappings to the map.
717Operation *Operation::clone(IRMapping &mapper, CloneOptions options) {
718 SmallVector<Value, 8> operands;
719 SmallVector<Block *, 2> successors;
720
721 // Remap the operands.
722 if (options.shouldCloneOperands()) {
723 operands.reserve(N: getNumOperands());
724 for (auto opValue : getOperands())
725 operands.push_back(Elt: mapper.lookupOrDefault(from: opValue));
726 }
727
728 // Remap the successors.
729 successors.reserve(N: getNumSuccessors());
730 for (Block *successor : getSuccessors())
731 successors.push_back(Elt: mapper.lookupOrDefault(from: successor));
732
733 // Create the new operation.
734 auto *newOp = create(getLoc(), getName(), getResultTypes(), operands, attrs,
735 getPropertiesStorage(), successors, getNumRegions());
736 mapper.map(this, newOp);
737
738 // Clone the regions.
739 if (options.shouldCloneRegions()) {
740 for (unsigned i = 0; i != numRegions; ++i)
741 getRegion(index: i).cloneInto(&newOp->getRegion(i), mapper);
742 }
743
744 // Remember the mapping of any results.
745 for (unsigned i = 0, e = getNumResults(); i != e; ++i)
746 mapper.map(getResult(idx: i), newOp->getResult(i));
747
748 return newOp;
749}
750
751Operation *Operation::clone(CloneOptions options) {
752 IRMapping mapper;
753 return clone(mapper, options);
754}
755
756//===----------------------------------------------------------------------===//
757// OpState trait class.
758//===----------------------------------------------------------------------===//
759
760// The fallback for the parser is to try for a dialect operation parser.
761// Otherwise, reject the custom assembly form.
762ParseResult OpState::parse(OpAsmParser &parser, OperationState &result) {
763 if (auto parseFn = result.name.getDialect()->getParseOperationHook(
764 opName: result.name.getStringRef()))
765 return (*parseFn)(parser, result);
766 return parser.emitError(loc: parser.getNameLoc(), message: "has no custom assembly form");
767}
768
769// The fallback for the printer is to try for a dialect operation printer.
770// Otherwise, it prints the generic form.
771void OpState::print(Operation *op, OpAsmPrinter &p, StringRef defaultDialect) {
772 if (auto printFn = op->getDialect()->getOperationPrinter(op)) {
773 printOpName(op, p, defaultDialect);
774 printFn(op, p);
775 } else {
776 p.printGenericOp(op);
777 }
778}
779
780/// Print an operation name, eliding the dialect prefix if necessary and doesn't
781/// lead to ambiguities.
782void OpState::printOpName(Operation *op, OpAsmPrinter &p,
783 StringRef defaultDialect) {
784 StringRef name = op->getName().getStringRef();
785 if (name.starts_with(Prefix: (defaultDialect + ".").str()) && name.count(C: '.') == 1)
786 name = name.drop_front(N: defaultDialect.size() + 1);
787 p.getStream() << name;
788}
789
790/// Parse properties as a Attribute.
791ParseResult OpState::genericParseProperties(OpAsmParser &parser,
792 Attribute &result) {
793 if (parser.parseLess() || parser.parseAttribute(result) ||
794 parser.parseGreater())
795 return failure();
796 return success();
797}
798
799/// Print the properties as a Attribute.
800void OpState::genericPrintProperties(OpAsmPrinter &p, Attribute properties) {
801 p << "<" << properties << ">";
802}
803
804/// Emit an error about fatal conditions with this operation, reporting up to
805/// any diagnostic handlers that may be listening.
806InFlightDiagnostic OpState::emitError(const Twine &message) {
807 return getOperation()->emitError(message);
808}
809
810/// Emit an error with the op name prefixed, like "'dim' op " which is
811/// convenient for verifiers.
812InFlightDiagnostic OpState::emitOpError(const Twine &message) {
813 return getOperation()->emitOpError(message);
814}
815
816/// Emit a warning about this operation, reporting up to any diagnostic
817/// handlers that may be listening.
818InFlightDiagnostic OpState::emitWarning(const Twine &message) {
819 return getOperation()->emitWarning(message);
820}
821
822/// Emit a remark about this operation, reporting up to any diagnostic
823/// handlers that may be listening.
824InFlightDiagnostic OpState::emitRemark(const Twine &message) {
825 return getOperation()->emitRemark(message);
826}
827
828//===----------------------------------------------------------------------===//
829// Op Trait implementations
830//===----------------------------------------------------------------------===//
831
832LogicalResult
833OpTrait::impl::foldCommutative(Operation *op, ArrayRef<Attribute> operands,
834 SmallVectorImpl<OpFoldResult> &results) {
835 // Nothing to fold if there are not at least 2 operands.
836 if (op->getNumOperands() < 2)
837 return failure();
838 // Move all constant operands to the end.
839 OpOperand *operandsBegin = op->getOpOperands().begin();
840 auto isNonConstant = [&](OpOperand &o) {
841 return !static_cast<bool>(operands[std::distance(first: operandsBegin, last: &o)]);
842 };
843 auto *firstConstantIt = llvm::find_if_not(Range: op->getOpOperands(), P: isNonConstant);
844 auto *newConstantIt = std::stable_partition(
845 first: firstConstantIt, last: op->getOpOperands().end(), pred: isNonConstant);
846 // Return success if the op was modified.
847 return success(isSuccess: firstConstantIt != newConstantIt);
848}
849
850OpFoldResult OpTrait::impl::foldIdempotent(Operation *op) {
851 if (op->getNumOperands() == 1) {
852 auto *argumentOp = op->getOperand(idx: 0).getDefiningOp();
853 if (argumentOp && op->getName() == argumentOp->getName()) {
854 // Replace the outer operation output with the inner operation.
855 return op->getOperand(idx: 0);
856 }
857 } else if (op->getOperand(idx: 0) == op->getOperand(idx: 1)) {
858 return op->getOperand(idx: 0);
859 }
860
861 return {};
862}
863
864OpFoldResult OpTrait::impl::foldInvolution(Operation *op) {
865 auto *argumentOp = op->getOperand(idx: 0).getDefiningOp();
866 if (argumentOp && op->getName() == argumentOp->getName()) {
867 // Replace the outer involutions output with inner's input.
868 return argumentOp->getOperand(idx: 0);
869 }
870
871 return {};
872}
873
874LogicalResult OpTrait::impl::verifyZeroOperands(Operation *op) {
875 if (op->getNumOperands() != 0)
876 return op->emitOpError() << "requires zero operands";
877 return success();
878}
879
880LogicalResult OpTrait::impl::verifyOneOperand(Operation *op) {
881 if (op->getNumOperands() != 1)
882 return op->emitOpError() << "requires a single operand";
883 return success();
884}
885
886LogicalResult OpTrait::impl::verifyNOperands(Operation *op,
887 unsigned numOperands) {
888 if (op->getNumOperands() != numOperands) {
889 return op->emitOpError() << "expected " << numOperands
890 << " operands, but found " << op->getNumOperands();
891 }
892 return success();
893}
894
895LogicalResult OpTrait::impl::verifyAtLeastNOperands(Operation *op,
896 unsigned numOperands) {
897 if (op->getNumOperands() < numOperands)
898 return op->emitOpError()
899 << "expected " << numOperands << " or more operands, but found "
900 << op->getNumOperands();
901 return success();
902}
903
904/// If this is a vector type, or a tensor type, return the scalar element type
905/// that it is built around, otherwise return the type unmodified.
906static Type getTensorOrVectorElementType(Type type) {
907 if (auto vec = llvm::dyn_cast<VectorType>(type))
908 return vec.getElementType();
909
910 // Look through tensor<vector<...>> to find the underlying element type.
911 if (auto tensor = llvm::dyn_cast<TensorType>(Val&: type))
912 return getTensorOrVectorElementType(type: tensor.getElementType());
913 return type;
914}
915
916LogicalResult OpTrait::impl::verifyIsIdempotent(Operation *op) {
917 // FIXME: Add back check for no side effects on operation.
918 // Currently adding it would cause the shared library build
919 // to fail since there would be a dependency of IR on SideEffectInterfaces
920 // which is cyclical.
921 return success();
922}
923
924LogicalResult OpTrait::impl::verifyIsInvolution(Operation *op) {
925 // FIXME: Add back check for no side effects on operation.
926 // Currently adding it would cause the shared library build
927 // to fail since there would be a dependency of IR on SideEffectInterfaces
928 // which is cyclical.
929 return success();
930}
931
932LogicalResult
933OpTrait::impl::verifyOperandsAreSignlessIntegerLike(Operation *op) {
934 for (auto opType : op->getOperandTypes()) {
935 auto type = getTensorOrVectorElementType(type: opType);
936 if (!type.isSignlessIntOrIndex())
937 return op->emitOpError() << "requires an integer or index type";
938 }
939 return success();
940}
941
942LogicalResult OpTrait::impl::verifyOperandsAreFloatLike(Operation *op) {
943 for (auto opType : op->getOperandTypes()) {
944 auto type = getTensorOrVectorElementType(type: opType);
945 if (!llvm::isa<FloatType>(Val: type))
946 return op->emitOpError(message: "requires a float type");
947 }
948 return success();
949}
950
951LogicalResult OpTrait::impl::verifySameTypeOperands(Operation *op) {
952 // Zero or one operand always have the "same" type.
953 unsigned nOperands = op->getNumOperands();
954 if (nOperands < 2)
955 return success();
956
957 auto type = op->getOperand(idx: 0).getType();
958 for (auto opType : llvm::drop_begin(RangeOrContainer: op->getOperandTypes(), N: 1))
959 if (opType != type)
960 return op->emitOpError() << "requires all operands to have the same type";
961 return success();
962}
963
964LogicalResult OpTrait::impl::verifyZeroRegions(Operation *op) {
965 if (op->getNumRegions() != 0)
966 return op->emitOpError() << "requires zero regions";
967 return success();
968}
969
970LogicalResult OpTrait::impl::verifyOneRegion(Operation *op) {
971 if (op->getNumRegions() != 1)
972 return op->emitOpError() << "requires one region";
973 return success();
974}
975
976LogicalResult OpTrait::impl::verifyNRegions(Operation *op,
977 unsigned numRegions) {
978 if (op->getNumRegions() != numRegions)
979 return op->emitOpError() << "expected " << numRegions << " regions";
980 return success();
981}
982
983LogicalResult OpTrait::impl::verifyAtLeastNRegions(Operation *op,
984 unsigned numRegions) {
985 if (op->getNumRegions() < numRegions)
986 return op->emitOpError() << "expected " << numRegions << " or more regions";
987 return success();
988}
989
990LogicalResult OpTrait::impl::verifyZeroResults(Operation *op) {
991 if (op->getNumResults() != 0)
992 return op->emitOpError() << "requires zero results";
993 return success();
994}
995
996LogicalResult OpTrait::impl::verifyOneResult(Operation *op) {
997 if (op->getNumResults() != 1)
998 return op->emitOpError() << "requires one result";
999 return success();
1000}
1001
1002LogicalResult OpTrait::impl::verifyNResults(Operation *op,
1003 unsigned numOperands) {
1004 if (op->getNumResults() != numOperands)
1005 return op->emitOpError() << "expected " << numOperands << " results";
1006 return success();
1007}
1008
1009LogicalResult OpTrait::impl::verifyAtLeastNResults(Operation *op,
1010 unsigned numOperands) {
1011 if (op->getNumResults() < numOperands)
1012 return op->emitOpError()
1013 << "expected " << numOperands << " or more results";
1014 return success();
1015}
1016
1017LogicalResult OpTrait::impl::verifySameOperandsShape(Operation *op) {
1018 if (failed(result: verifyAtLeastNOperands(op, numOperands: 1)))
1019 return failure();
1020
1021 if (failed(result: verifyCompatibleShapes(types: op->getOperandTypes())))
1022 return op->emitOpError() << "requires the same shape for all operands";
1023
1024 return success();
1025}
1026
1027LogicalResult OpTrait::impl::verifySameOperandsAndResultShape(Operation *op) {
1028 if (failed(result: verifyAtLeastNOperands(op, numOperands: 1)) ||
1029 failed(result: verifyAtLeastNResults(op, numOperands: 1)))
1030 return failure();
1031
1032 SmallVector<Type, 8> types(op->getOperandTypes());
1033 types.append(RHS: llvm::to_vector<4>(Range: op->getResultTypes()));
1034
1035 if (failed(result: verifyCompatibleShapes(types)))
1036 return op->emitOpError()
1037 << "requires the same shape for all operands and results";
1038
1039 return success();
1040}
1041
1042LogicalResult OpTrait::impl::verifySameOperandsElementType(Operation *op) {
1043 if (failed(result: verifyAtLeastNOperands(op, numOperands: 1)))
1044 return failure();
1045 auto elementType = getElementTypeOrSelf(val: op->getOperand(idx: 0));
1046
1047 for (auto operand : llvm::drop_begin(RangeOrContainer: op->getOperands(), N: 1)) {
1048 if (getElementTypeOrSelf(val: operand) != elementType)
1049 return op->emitOpError(message: "requires the same element type for all operands");
1050 }
1051
1052 return success();
1053}
1054
1055LogicalResult
1056OpTrait::impl::verifySameOperandsAndResultElementType(Operation *op) {
1057 if (failed(result: verifyAtLeastNOperands(op, numOperands: 1)) ||
1058 failed(result: verifyAtLeastNResults(op, numOperands: 1)))
1059 return failure();
1060
1061 auto elementType = getElementTypeOrSelf(val: op->getResult(idx: 0));
1062
1063 // Verify result element type matches first result's element type.
1064 for (auto result : llvm::drop_begin(RangeOrContainer: op->getResults(), N: 1)) {
1065 if (getElementTypeOrSelf(val: result) != elementType)
1066 return op->emitOpError(
1067 message: "requires the same element type for all operands and results");
1068 }
1069
1070 // Verify operand's element type matches first result's element type.
1071 for (auto operand : op->getOperands()) {
1072 if (getElementTypeOrSelf(val: operand) != elementType)
1073 return op->emitOpError(
1074 message: "requires the same element type for all operands and results");
1075 }
1076
1077 return success();
1078}
1079
1080LogicalResult OpTrait::impl::verifySameOperandsAndResultType(Operation *op) {
1081 if (failed(result: verifyAtLeastNOperands(op, numOperands: 1)) ||
1082 failed(result: verifyAtLeastNResults(op, numOperands: 1)))
1083 return failure();
1084
1085 auto type = op->getResult(idx: 0).getType();
1086 auto elementType = getElementTypeOrSelf(type);
1087 Attribute encoding = nullptr;
1088 if (auto rankedType = dyn_cast<RankedTensorType>(type))
1089 encoding = rankedType.getEncoding();
1090 for (auto resultType : llvm::drop_begin(RangeOrContainer: op->getResultTypes())) {
1091 if (getElementTypeOrSelf(type: resultType) != elementType ||
1092 failed(result: verifyCompatibleShape(type1: resultType, type2: type)))
1093 return op->emitOpError()
1094 << "requires the same type for all operands and results";
1095 if (encoding)
1096 if (auto rankedType = dyn_cast<RankedTensorType>(resultType);
1097 encoding != rankedType.getEncoding())
1098 return op->emitOpError()
1099 << "requires the same encoding for all operands and results";
1100 }
1101 for (auto opType : op->getOperandTypes()) {
1102 if (getElementTypeOrSelf(type: opType) != elementType ||
1103 failed(result: verifyCompatibleShape(type1: opType, type2: type)))
1104 return op->emitOpError()
1105 << "requires the same type for all operands and results";
1106 if (encoding)
1107 if (auto rankedType = dyn_cast<RankedTensorType>(opType);
1108 encoding != rankedType.getEncoding())
1109 return op->emitOpError()
1110 << "requires the same encoding for all operands and results";
1111 }
1112 return success();
1113}
1114
1115LogicalResult OpTrait::impl::verifySameOperandsAndResultRank(Operation *op) {
1116 if (failed(result: verifyAtLeastNOperands(op, numOperands: 1)))
1117 return failure();
1118
1119 // delegate function that returns true if type is a shaped type with known
1120 // rank
1121 auto hasRank = [](const Type type) {
1122 if (auto shaped_type = dyn_cast<ShapedType>(type))
1123 return shaped_type.hasRank();
1124
1125 return false;
1126 };
1127
1128 auto rankedOperandTypes =
1129 llvm::make_filter_range(Range: op->getOperandTypes(), Pred: hasRank);
1130 auto rankedResultTypes =
1131 llvm::make_filter_range(Range: op->getResultTypes(), Pred: hasRank);
1132
1133 // If all operands and results are unranked, then no further verification.
1134 if (rankedOperandTypes.empty() && rankedResultTypes.empty())
1135 return success();
1136
1137 // delegate function that returns rank of shaped type with known rank
1138 auto getRank = [](const Type type) {
1139 return type.cast<ShapedType>().getRank();
1140 };
1141
1142 auto rank = !rankedOperandTypes.empty() ? getRank(*rankedOperandTypes.begin())
1143 : getRank(*rankedResultTypes.begin());
1144
1145 for (const auto type : rankedOperandTypes) {
1146 if (rank != getRank(type)) {
1147 return op->emitOpError(message: "operands don't have matching ranks");
1148 }
1149 }
1150
1151 for (const auto type : rankedResultTypes) {
1152 if (rank != getRank(type)) {
1153 return op->emitOpError(message: "result type has different rank than operands");
1154 }
1155 }
1156
1157 return success();
1158}
1159
1160LogicalResult OpTrait::impl::verifyIsTerminator(Operation *op) {
1161 Block *block = op->getBlock();
1162 // Verify that the operation is at the end of the respective parent block.
1163 if (!block || &block->back() != op)
1164 return op->emitOpError(message: "must be the last operation in the parent block");
1165 return success();
1166}
1167
1168static LogicalResult verifyTerminatorSuccessors(Operation *op) {
1169 auto *parent = op->getParentRegion();
1170
1171 // Verify that the operands lines up with the BB arguments in the successor.
1172 for (Block *succ : op->getSuccessors())
1173 if (succ->getParent() != parent)
1174 return op->emitError(message: "reference to block defined in another region");
1175 return success();
1176}
1177
1178LogicalResult OpTrait::impl::verifyZeroSuccessors(Operation *op) {
1179 if (op->getNumSuccessors() != 0) {
1180 return op->emitOpError(message: "requires 0 successors but found ")
1181 << op->getNumSuccessors();
1182 }
1183 return success();
1184}
1185
1186LogicalResult OpTrait::impl::verifyOneSuccessor(Operation *op) {
1187 if (op->getNumSuccessors() != 1) {
1188 return op->emitOpError(message: "requires 1 successor but found ")
1189 << op->getNumSuccessors();
1190 }
1191 return verifyTerminatorSuccessors(op);
1192}
1193LogicalResult OpTrait::impl::verifyNSuccessors(Operation *op,
1194 unsigned numSuccessors) {
1195 if (op->getNumSuccessors() != numSuccessors) {
1196 return op->emitOpError(message: "requires ")
1197 << numSuccessors << " successors but found "
1198 << op->getNumSuccessors();
1199 }
1200 return verifyTerminatorSuccessors(op);
1201}
1202LogicalResult OpTrait::impl::verifyAtLeastNSuccessors(Operation *op,
1203 unsigned numSuccessors) {
1204 if (op->getNumSuccessors() < numSuccessors) {
1205 return op->emitOpError(message: "requires at least ")
1206 << numSuccessors << " successors but found "
1207 << op->getNumSuccessors();
1208 }
1209 return verifyTerminatorSuccessors(op);
1210}
1211
1212LogicalResult OpTrait::impl::verifyResultsAreBoolLike(Operation *op) {
1213 for (auto resultType : op->getResultTypes()) {
1214 auto elementType = getTensorOrVectorElementType(type: resultType);
1215 bool isBoolType = elementType.isInteger(width: 1);
1216 if (!isBoolType)
1217 return op->emitOpError() << "requires a bool result type";
1218 }
1219
1220 return success();
1221}
1222
1223LogicalResult OpTrait::impl::verifyResultsAreFloatLike(Operation *op) {
1224 for (auto resultType : op->getResultTypes())
1225 if (!llvm::isa<FloatType>(Val: getTensorOrVectorElementType(type: resultType)))
1226 return op->emitOpError() << "requires a floating point type";
1227
1228 return success();
1229}
1230
1231LogicalResult
1232OpTrait::impl::verifyResultsAreSignlessIntegerLike(Operation *op) {
1233 for (auto resultType : op->getResultTypes())
1234 if (!getTensorOrVectorElementType(type: resultType).isSignlessIntOrIndex())
1235 return op->emitOpError() << "requires an integer or index type";
1236 return success();
1237}
1238
1239LogicalResult OpTrait::impl::verifyValueSizeAttr(Operation *op,
1240 StringRef attrName,
1241 StringRef valueGroupName,
1242 size_t expectedCount) {
1243 auto sizeAttr = op->getAttrOfType<DenseI32ArrayAttr>(attrName);
1244 if (!sizeAttr)
1245 return op->emitOpError(message: "requires dense i32 array attribute '")
1246 << attrName << "'";
1247
1248 ArrayRef<int32_t> sizes = sizeAttr.asArrayRef();
1249 if (llvm::any_of(Range&: sizes, P: [](int32_t element) { return element < 0; }))
1250 return op->emitOpError(message: "'")
1251 << attrName << "' attribute cannot have negative elements";
1252
1253 size_t totalCount =
1254 std::accumulate(first: sizes.begin(), last: sizes.end(), init: 0,
1255 binary_op: [](unsigned all, int32_t one) { return all + one; });
1256
1257 if (totalCount != expectedCount)
1258 return op->emitOpError()
1259 << valueGroupName << " count (" << expectedCount
1260 << ") does not match with the total size (" << totalCount
1261 << ") specified in attribute '" << attrName << "'";
1262 return success();
1263}
1264
1265LogicalResult OpTrait::impl::verifyOperandSizeAttr(Operation *op,
1266 StringRef attrName) {
1267 return verifyValueSizeAttr(op, attrName, valueGroupName: "operand", expectedCount: op->getNumOperands());
1268}
1269
1270LogicalResult OpTrait::impl::verifyResultSizeAttr(Operation *op,
1271 StringRef attrName) {
1272 return verifyValueSizeAttr(op, attrName, valueGroupName: "result", expectedCount: op->getNumResults());
1273}
1274
1275LogicalResult OpTrait::impl::verifyNoRegionArguments(Operation *op) {
1276 for (Region &region : op->getRegions()) {
1277 if (region.empty())
1278 continue;
1279
1280 if (region.getNumArguments() != 0) {
1281 if (op->getNumRegions() > 1)
1282 return op->emitOpError(message: "region #")
1283 << region.getRegionNumber() << " should have no arguments";
1284 return op->emitOpError(message: "region should have no arguments");
1285 }
1286 }
1287 return success();
1288}
1289
1290LogicalResult OpTrait::impl::verifyElementwise(Operation *op) {
1291 auto isMappableType = [](Type type) {
1292 return llvm::isa<VectorType, TensorType>(Val: type);
1293 };
1294 auto resultMappableTypes = llvm::to_vector<1>(
1295 Range: llvm::make_filter_range(Range: op->getResultTypes(), Pred: isMappableType));
1296 auto operandMappableTypes = llvm::to_vector<2>(
1297 Range: llvm::make_filter_range(Range: op->getOperandTypes(), Pred: isMappableType));
1298
1299 // If the op only has scalar operand/result types, then we have nothing to
1300 // check.
1301 if (resultMappableTypes.empty() && operandMappableTypes.empty())
1302 return success();
1303
1304 if (!resultMappableTypes.empty() && operandMappableTypes.empty())
1305 return op->emitOpError(message: "if a result is non-scalar, then at least one "
1306 "operand must be non-scalar");
1307
1308 assert(!operandMappableTypes.empty());
1309
1310 if (resultMappableTypes.empty())
1311 return op->emitOpError(message: "if an operand is non-scalar, then there must be at "
1312 "least one non-scalar result");
1313
1314 if (resultMappableTypes.size() != op->getNumResults())
1315 return op->emitOpError(
1316 message: "if an operand is non-scalar, then all results must be non-scalar");
1317
1318 SmallVector<Type, 4> types = llvm::to_vector<2>(
1319 Range: llvm::concat<Type>(Ranges&: operandMappableTypes, Ranges&: resultMappableTypes));
1320 TypeID expectedBaseTy = types.front().getTypeID();
1321 if (!llvm::all_of(Range&: types,
1322 P: [&](Type t) { return t.getTypeID() == expectedBaseTy; }) ||
1323 failed(result: verifyCompatibleShapes(types))) {
1324 return op->emitOpError() << "all non-scalar operands/results must have the "
1325 "same shape and base type";
1326 }
1327
1328 return success();
1329}
1330
1331/// Check for any values used by operations regions attached to the
1332/// specified "IsIsolatedFromAbove" operation defined outside of it.
1333LogicalResult OpTrait::impl::verifyIsIsolatedFromAbove(Operation *isolatedOp) {
1334 assert(isolatedOp->hasTrait<OpTrait::IsIsolatedFromAbove>() &&
1335 "Intended to check IsolatedFromAbove ops");
1336
1337 // List of regions to analyze. Each region is processed independently, with
1338 // respect to the common `limit` region, so we can look at them in any order.
1339 // Therefore, use a simple vector and push/pop back the current region.
1340 SmallVector<Region *, 8> pendingRegions;
1341 for (auto &region : isolatedOp->getRegions()) {
1342 pendingRegions.push_back(Elt: &region);
1343
1344 // Traverse all operations in the region.
1345 while (!pendingRegions.empty()) {
1346 for (Operation &op : pendingRegions.pop_back_val()->getOps()) {
1347 for (Value operand : op.getOperands()) {
1348 // Check that any value that is used by an operation is defined in the
1349 // same region as either an operation result.
1350 auto *operandRegion = operand.getParentRegion();
1351 if (!operandRegion)
1352 return op.emitError(message: "operation's operand is unlinked");
1353 if (!region.isAncestor(other: operandRegion)) {
1354 return op.emitOpError(message: "using value defined outside the region")
1355 .attachNote(noteLoc: isolatedOp->getLoc())
1356 << "required by region isolation constraints";
1357 }
1358 }
1359
1360 // Schedule any regions in the operation for further checking. Don't
1361 // recurse into other IsolatedFromAbove ops, because they will check
1362 // themselves.
1363 if (op.getNumRegions() &&
1364 !op.hasTrait<OpTrait::IsIsolatedFromAbove>()) {
1365 for (Region &subRegion : op.getRegions())
1366 pendingRegions.push_back(Elt: &subRegion);
1367 }
1368 }
1369 }
1370 }
1371
1372 return success();
1373}
1374
1375bool OpTrait::hasElementwiseMappableTraits(Operation *op) {
1376 return op->hasTrait<Elementwise>() && op->hasTrait<Scalarizable>() &&
1377 op->hasTrait<Vectorizable>() && op->hasTrait<Tensorizable>();
1378}
1379
1380//===----------------------------------------------------------------------===//
1381// Misc. utils
1382//===----------------------------------------------------------------------===//
1383
1384/// Insert an operation, generated by `buildTerminatorOp`, at the end of the
1385/// region's only block if it does not have a terminator already. If the region
1386/// is empty, insert a new block first. `buildTerminatorOp` should return the
1387/// terminator operation to insert.
1388void impl::ensureRegionTerminator(
1389 Region &region, OpBuilder &builder, Location loc,
1390 function_ref<Operation *(OpBuilder &, Location)> buildTerminatorOp) {
1391 OpBuilder::InsertionGuard guard(builder);
1392 if (region.empty())
1393 builder.createBlock(parent: &region);
1394
1395 Block &block = region.back();
1396 if (!block.empty() && block.back().hasTrait<OpTrait::IsTerminator>())
1397 return;
1398
1399 builder.setInsertionPointToEnd(&block);
1400 builder.insert(op: buildTerminatorOp(builder, loc));
1401}
1402
1403/// Create a simple OpBuilder and forward to the OpBuilder version of this
1404/// function.
1405void impl::ensureRegionTerminator(
1406 Region &region, Builder &builder, Location loc,
1407 function_ref<Operation *(OpBuilder &, Location)> buildTerminatorOp) {
1408 OpBuilder opBuilder(builder.getContext());
1409 ensureRegionTerminator(region, builder&: opBuilder, loc, buildTerminatorOp);
1410}
1411

source code of mlir/lib/IR/Operation.cpp