1 //===- Operation.cpp - Operation support code -----------------------------===//
2 //
3 // Part of the MLIR 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/BlockAndValueMapping.h"
11 #include "mlir/IR/Diagnostics.h"
12 #include "mlir/IR/Dialect.h"
13 #include "mlir/IR/Function.h"
14 #include "mlir/IR/MLIRContext.h"
15 #include "mlir/IR/OpDefinition.h"
16 #include "mlir/IR/OpImplementation.h"
17 #include "mlir/IR/PatternMatch.h"
18 #include "mlir/IR/StandardTypes.h"
19 #include "mlir/IR/TypeUtilities.h"
20 #include "llvm/Support/CommandLine.h"
21 #include <numeric>
22 
23 using namespace mlir;
24 
25 static llvm::cl::opt<bool> printOpOnDiagnostic(
26     "mlir-print-op-on-diagnostic",
27     llvm::cl::desc("When a diagnostic is emitted on an operation, also print "
28                    "the operation as an attached note"));
29 
30 OpAsmParser::~OpAsmParser() {}
31 
32 //===----------------------------------------------------------------------===//
33 // OperationName
34 //===----------------------------------------------------------------------===//
35 
36 /// Form the OperationName for an op with the specified string.  This either is
37 /// a reference to an AbstractOperation if one is known, or a uniqued Identifier
38 /// if not.
39 OperationName::OperationName(StringRef name, MLIRContext *context) {
40   if (auto *op = AbstractOperation::lookup(name, context))
41     representation = op;
42   else
43     representation = Identifier::get(name, context);
44 }
45 
46 /// Return the name of the dialect this operation is registered to.
47 StringRef OperationName::getDialect() const {
48   return getStringRef().split('.').first;
49 }
50 
51 /// Return the name of this operation.  This always succeeds.
52 StringRef OperationName::getStringRef() const {
53   if (auto *op = representation.dyn_cast<const AbstractOperation *>())
54     return op->name;
55   return representation.get<Identifier>().strref();
56 }
57 
58 const AbstractOperation *OperationName::getAbstractOperation() const {
59   return representation.dyn_cast<const AbstractOperation *>();
60 }
61 
62 OperationName OperationName::getFromOpaquePointer(void *pointer) {
63   return OperationName(RepresentationUnion::getFromOpaqueValue(pointer));
64 }
65 
66 //===----------------------------------------------------------------------===//
67 // Operation
68 //===----------------------------------------------------------------------===//
69 
70 /// Create a new Operation with the specific fields.
71 Operation *Operation::create(Location location, OperationName name,
72                              ArrayRef<Type> resultTypes,
73                              ArrayRef<Value> operands,
74                              ArrayRef<NamedAttribute> attributes,
75                              ArrayRef<Block *> successors, unsigned numRegions,
76                              bool resizableOperandList) {
77   return create(location, name, resultTypes, operands,
78                 NamedAttributeList(attributes), successors, numRegions,
79                 resizableOperandList);
80 }
81 
82 /// Create a new Operation from operation state.
83 Operation *Operation::create(const OperationState &state) {
84   return Operation::create(state.location, state.name, state.types,
85                            state.operands, NamedAttributeList(state.attributes),
86                            state.successors, state.regions,
87                            state.resizableOperandList);
88 }
89 
90 /// Create a new Operation with the specific fields.
91 Operation *Operation::create(Location location, OperationName name,
92                              ArrayRef<Type> resultTypes,
93                              ArrayRef<Value> operands,
94                              NamedAttributeList attributes,
95                              ArrayRef<Block *> successors, RegionRange regions,
96                              bool resizableOperandList) {
97   unsigned numRegions = regions.size();
98   Operation *op = create(location, name, resultTypes, operands, attributes,
99                          successors, numRegions, resizableOperandList);
100   for (unsigned i = 0; i < numRegions; ++i)
101     if (regions[i])
102       op->getRegion(i).takeBody(*regions[i]);
103   return op;
104 }
105 
106 /// Overload of create that takes an existing NamedAttributeList to avoid
107 /// unnecessarily uniquing a list of attributes.
108 Operation *Operation::create(Location location, OperationName name,
109                              ArrayRef<Type> resultTypes,
110                              ArrayRef<Value> operands,
111                              NamedAttributeList attributes,
112                              ArrayRef<Block *> successors, unsigned numRegions,
113                              bool resizableOperandList) {
114   unsigned numSuccessors = successors.size();
115 
116   // We only need to allocate additional memory for a subset of results.
117   unsigned numTrailingResults = OpResult::getNumTrailing(resultTypes.size());
118 
119   // Input operands are nullptr-separated for each successor, the null operands
120   // aren't actually stored.
121   unsigned numOperands = operands.size() - numSuccessors;
122 
123   // Compute the byte size for the operation and the operand storage.
124   auto byteSize = totalSizeToAlloc<detail::TrailingOpResult, BlockOperand,
125                                    Region, detail::OperandStorage>(
126       numTrailingResults, numSuccessors, numRegions,
127       /*detail::OperandStorage*/ 1);
128   byteSize += llvm::alignTo(detail::OperandStorage::additionalAllocSize(
129                                 numOperands, resizableOperandList),
130                             alignof(Operation));
131   void *rawMem = malloc(byteSize);
132 
133   // Create the new Operation.
134   auto op = ::new (rawMem) Operation(location, name, resultTypes, numSuccessors,
135                                      numRegions, attributes);
136 
137   assert((numSuccessors == 0 || !op->isKnownNonTerminator()) &&
138          "unexpected successors in a non-terminator operation");
139 
140   // Initialize the trailing results.
141   if (LLVM_UNLIKELY(numTrailingResults > 0)) {
142     // We initialize the trailing results with their result number. This makes
143     // 'getResultNumber' checks much more efficient. The main purpose for these
144     // results is to give an anchor to the main operation anyways, so this is
145     // purely an optimization.
146     auto *trailingResultIt = op->getTrailingObjects<detail::TrailingOpResult>();
147     for (unsigned i = 0; i != numTrailingResults; ++i, ++trailingResultIt)
148       trailingResultIt->trailingResultNumber = i;
149   }
150 
151   // Initialize the regions.
152   for (unsigned i = 0; i != numRegions; ++i)
153     new (&op->getRegion(i)) Region(op);
154 
155   // Initialize the results and operands.
156   new (&op->getOperandStorage())
157       detail::OperandStorage(numOperands, resizableOperandList);
158   auto opOperands = op->getOpOperands();
159 
160   // Initialize normal operands.
161   unsigned operandIt = 0, operandE = operands.size();
162   unsigned nextOperand = 0;
163   for (; operandIt != operandE; ++operandIt) {
164     // Null operands are used as sentinels between successor operand lists. If
165     // we encounter one here, break and handle the successor operands lists
166     // separately below.
167     if (!operands[operandIt])
168       break;
169     new (&opOperands[nextOperand++]) OpOperand(op, operands[operandIt]);
170   }
171 
172   unsigned currentSuccNum = 0;
173   if (operandIt == operandE) {
174     // Verify that the amount of sentinel operands is equivalent to the number
175     // of successors.
176     assert(currentSuccNum == numSuccessors);
177     return op;
178   }
179 
180   assert(!op->isKnownNonTerminator() &&
181          "Unexpected nullptr in operand list when creating non-terminator.");
182   auto instBlockOperands = op->getBlockOperands();
183   unsigned *succOperandCount = nullptr;
184 
185   for (; operandIt != operandE; ++operandIt) {
186     // If we encounter a sentinel branch to the next operand update the count
187     // variable.
188     if (!operands[operandIt]) {
189       assert(currentSuccNum < numSuccessors);
190 
191       new (&instBlockOperands[currentSuccNum])
192           BlockOperand(op, successors[currentSuccNum]);
193       succOperandCount =
194           &instBlockOperands[currentSuccNum].numSuccessorOperands;
195       ++currentSuccNum;
196       continue;
197     }
198     new (&opOperands[nextOperand++]) OpOperand(op, operands[operandIt]);
199     ++(*succOperandCount);
200   }
201 
202   // Verify that the amount of sentinel operands is equivalent to the number of
203   // successors.
204   assert(currentSuccNum == numSuccessors);
205 
206   return op;
207 }
208 
209 Operation::Operation(Location location, OperationName name,
210                      ArrayRef<Type> resultTypes, unsigned numSuccessors,
211                      unsigned numRegions, const NamedAttributeList &attributes)
212     : location(location), numSuccs(numSuccessors), numRegions(numRegions),
213       hasSingleResult(false), name(name), attrs(attributes) {
214   if (!resultTypes.empty()) {
215     // If there is a single result it is stored in-place, otherwise use a tuple.
216     hasSingleResult = resultTypes.size() == 1;
217     if (hasSingleResult)
218       resultType = resultTypes.front();
219     else
220       resultType = TupleType::get(resultTypes, location->getContext());
221   }
222 }
223 
224 // Operations are deleted through the destroy() member because they are
225 // allocated via malloc.
226 Operation::~Operation() {
227   assert(block == nullptr && "operation destroyed but still in a block");
228 
229   // Explicitly run the destructors for the operands and results.
230   getOperandStorage().~OperandStorage();
231 
232   // Explicitly run the destructors for the successors.
233   for (auto &successor : getBlockOperands())
234     successor.~BlockOperand();
235 
236   // Explicitly destroy the regions.
237   for (auto &region : getRegions())
238     region.~Region();
239 }
240 
241 /// Destroy this operation or one of its subclasses.
242 void Operation::destroy() {
243   this->~Operation();
244   free(this);
245 }
246 
247 /// Return the context this operation is associated with.
248 MLIRContext *Operation::getContext() { return location->getContext(); }
249 
250 /// Return the dialect this operation is associated with, or nullptr if the
251 /// associated dialect is not registered.
252 Dialect *Operation::getDialect() {
253   if (auto *abstractOp = getAbstractOperation())
254     return &abstractOp->dialect;
255 
256   // If this operation hasn't been registered or doesn't have abstract
257   // operation, try looking up the dialect name in the context.
258   return getContext()->getRegisteredDialect(getName().getDialect());
259 }
260 
261 Region *Operation::getParentRegion() {
262   return block ? block->getParent() : nullptr;
263 }
264 
265 Operation *Operation::getParentOp() {
266   return block ? block->getParentOp() : nullptr;
267 }
268 
269 /// Return true if this operation is a proper ancestor of the `other`
270 /// operation.
271 bool Operation::isProperAncestor(Operation *other) {
272   while ((other = other->getParentOp()))
273     if (this == other)
274       return true;
275   return false;
276 }
277 
278 /// Replace any uses of 'from' with 'to' within this operation.
279 void Operation::replaceUsesOfWith(Value from, Value to) {
280   if (from == to)
281     return;
282   for (auto &operand : getOpOperands())
283     if (operand.get() == from)
284       operand.set(to);
285 }
286 
287 /// Replace the current operands of this operation with the ones provided in
288 /// 'operands'. If the operands list is not resizable, the size of 'operands'
289 /// must be less than or equal to the current number of operands.
290 void Operation::setOperands(ValueRange operands) {
291   getOperandStorage().setOperands(this, operands);
292 }
293 
294 //===----------------------------------------------------------------------===//
295 // Diagnostics
296 //===----------------------------------------------------------------------===//
297 
298 /// Emit an error about fatal conditions with this operation, reporting up to
299 /// any diagnostic handlers that may be listening.
300 InFlightDiagnostic Operation::emitError(const Twine &message) {
301   InFlightDiagnostic diag = mlir::emitError(getLoc(), message);
302   if (printOpOnDiagnostic) {
303     // Print out the operation explicitly here so that we can print the generic
304     // form.
305     // TODO(riverriddle) It would be nice if we could instead provide the
306     // specific printing flags when adding the operation as an argument to the
307     // diagnostic.
308     std::string printedOp;
309     {
310       llvm::raw_string_ostream os(printedOp);
311       print(os, OpPrintingFlags().printGenericOpForm().useLocalScope());
312     }
313     diag.attachNote(getLoc()) << "see current operation: " << printedOp;
314   }
315   return diag;
316 }
317 
318 /// Emit a warning about this operation, reporting up to any diagnostic
319 /// handlers that may be listening.
320 InFlightDiagnostic Operation::emitWarning(const Twine &message) {
321   InFlightDiagnostic diag = mlir::emitWarning(getLoc(), message);
322   if (printOpOnDiagnostic)
323     diag.attachNote(getLoc()) << "see current operation: " << *this;
324   return diag;
325 }
326 
327 /// Emit a remark about this operation, reporting up to any diagnostic
328 /// handlers that may be listening.
329 InFlightDiagnostic Operation::emitRemark(const Twine &message) {
330   InFlightDiagnostic diag = mlir::emitRemark(getLoc(), message);
331   if (printOpOnDiagnostic)
332     diag.attachNote(getLoc()) << "see current operation: " << *this;
333   return diag;
334 }
335 
336 //===----------------------------------------------------------------------===//
337 // Operation Ordering
338 //===----------------------------------------------------------------------===//
339 
340 constexpr unsigned Operation::kInvalidOrderIdx;
341 constexpr unsigned Operation::kOrderStride;
342 
343 /// Given an operation 'other' that is within the same parent block, return
344 /// whether the current operation is before 'other' in the operation list
345 /// of the parent block.
346 /// Note: This function has an average complexity of O(1), but worst case may
347 /// take O(N) where N is the number of operations within the parent block.
348 bool Operation::isBeforeInBlock(Operation *other) {
349   assert(block && "Operations without parent blocks have no order.");
350   assert(other && other->block == block &&
351          "Expected other operation to have the same parent block.");
352   // If the order of the block is already invalid, directly recompute the
353   // parent.
354   if (!block->isOpOrderValid()) {
355     block->recomputeOpOrder();
356   } else {
357     // Update the order either operation if necessary.
358     updateOrderIfNecessary();
359     other->updateOrderIfNecessary();
360   }
361 
362   return orderIndex < other->orderIndex;
363 }
364 
365 /// Update the order index of this operation of this operation if necessary,
366 /// potentially recomputing the order of the parent block.
367 void Operation::updateOrderIfNecessary() {
368   assert(block && "expected valid parent");
369 
370   // If the order is valid for this operation there is nothing to do.
371   if (hasValidOrder())
372     return;
373   Operation *blockFront = &block->front();
374   Operation *blockBack = &block->back();
375 
376   // This method is expected to only be invoked on blocks with more than one
377   // operation.
378   assert(blockFront != blockBack && "expected more than one operation");
379 
380   // If the operation is at the end of the block.
381   if (this == blockBack) {
382     Operation *prevNode = getPrevNode();
383     if (!prevNode->hasValidOrder())
384       return block->recomputeOpOrder();
385 
386     // Add the stride to the previous operation.
387     orderIndex = prevNode->orderIndex + kOrderStride;
388     return;
389   }
390 
391   // If this is the first operation try to use the next operation to compute the
392   // ordering.
393   if (this == blockFront) {
394     Operation *nextNode = getNextNode();
395     if (!nextNode->hasValidOrder())
396       return block->recomputeOpOrder();
397     // There is no order to give this operation.
398     if (nextNode->orderIndex == 0)
399       return block->recomputeOpOrder();
400 
401     // If we can't use the stride, just take the middle value left. This is safe
402     // because we know there is at least one valid index to assign to.
403     if (nextNode->orderIndex <= kOrderStride)
404       orderIndex = (nextNode->orderIndex / 2);
405     else
406       orderIndex = kOrderStride;
407     return;
408   }
409 
410   // Otherwise, this operation is between two others. Place this operation in
411   // the middle of the previous and next if possible.
412   Operation *prevNode = getPrevNode(), *nextNode = getNextNode();
413   if (!prevNode->hasValidOrder() || !nextNode->hasValidOrder())
414     return block->recomputeOpOrder();
415   unsigned prevOrder = prevNode->orderIndex, nextOrder = nextNode->orderIndex;
416 
417   // Check to see if there is a valid order between the two.
418   if (prevOrder + 1 == nextOrder)
419     return block->recomputeOpOrder();
420   orderIndex = prevOrder + 1 + ((nextOrder - prevOrder) / 2);
421 }
422 
423 //===----------------------------------------------------------------------===//
424 // ilist_traits for Operation
425 //===----------------------------------------------------------------------===//
426 
427 auto llvm::ilist_detail::SpecificNodeAccess<
428     typename llvm::ilist_detail::compute_node_options<
429         ::mlir::Operation>::type>::getNodePtr(pointer N) -> node_type * {
430   return NodeAccess::getNodePtr<OptionsT>(N);
431 }
432 
433 auto llvm::ilist_detail::SpecificNodeAccess<
434     typename llvm::ilist_detail::compute_node_options<
435         ::mlir::Operation>::type>::getNodePtr(const_pointer N)
436     -> const node_type * {
437   return NodeAccess::getNodePtr<OptionsT>(N);
438 }
439 
440 auto llvm::ilist_detail::SpecificNodeAccess<
441     typename llvm::ilist_detail::compute_node_options<
442         ::mlir::Operation>::type>::getValuePtr(node_type *N) -> pointer {
443   return NodeAccess::getValuePtr<OptionsT>(N);
444 }
445 
446 auto llvm::ilist_detail::SpecificNodeAccess<
447     typename llvm::ilist_detail::compute_node_options<
448         ::mlir::Operation>::type>::getValuePtr(const node_type *N)
449     -> const_pointer {
450   return NodeAccess::getValuePtr<OptionsT>(N);
451 }
452 
453 void llvm::ilist_traits<::mlir::Operation>::deleteNode(Operation *op) {
454   op->destroy();
455 }
456 
457 Block *llvm::ilist_traits<::mlir::Operation>::getContainingBlock() {
458   size_t Offset(size_t(&((Block *)nullptr->*Block::getSublistAccess(nullptr))));
459   iplist<Operation> *Anchor(static_cast<iplist<Operation> *>(this));
460   return reinterpret_cast<Block *>(reinterpret_cast<char *>(Anchor) - Offset);
461 }
462 
463 /// This is a trait method invoked when a operation is added to a block.  We
464 /// keep the block pointer up to date.
465 void llvm::ilist_traits<::mlir::Operation>::addNodeToList(Operation *op) {
466   assert(!op->getBlock() && "already in a operation block!");
467   op->block = getContainingBlock();
468 
469   // Invalidate the order on the operation.
470   op->orderIndex = Operation::kInvalidOrderIdx;
471 }
472 
473 /// This is a trait method invoked when a operation is removed from a block.
474 /// We keep the block pointer up to date.
475 void llvm::ilist_traits<::mlir::Operation>::removeNodeFromList(Operation *op) {
476   assert(op->block && "not already in a operation block!");
477   op->block = nullptr;
478 }
479 
480 /// This is a trait method invoked when a operation is moved from one block
481 /// to another.  We keep the block pointer up to date.
482 void llvm::ilist_traits<::mlir::Operation>::transferNodesFromList(
483     ilist_traits<Operation> &otherList, op_iterator first, op_iterator last) {
484   Block *curParent = getContainingBlock();
485 
486   // Invalidate the ordering of the parent block.
487   curParent->invalidateOpOrder();
488 
489   // If we are transferring operations within the same block, the block
490   // pointer doesn't need to be updated.
491   if (curParent == otherList.getContainingBlock())
492     return;
493 
494   // Update the 'block' member of each operation.
495   for (; first != last; ++first)
496     first->block = curParent;
497 }
498 
499 /// Remove this operation (and its descendants) from its Block and delete
500 /// all of them.
501 void Operation::erase() {
502   if (auto *parent = getBlock())
503     parent->getOperations().erase(this);
504   else
505     destroy();
506 }
507 
508 /// Unlink this operation from its current block and insert it right before
509 /// `existingOp` which may be in the same or another block in the same
510 /// function.
511 void Operation::moveBefore(Operation *existingOp) {
512   moveBefore(existingOp->getBlock(), existingOp->getIterator());
513 }
514 
515 /// Unlink this operation from its current basic block and insert it right
516 /// before `iterator` in the specified basic block.
517 void Operation::moveBefore(Block *block,
518                            llvm::iplist<Operation>::iterator iterator) {
519   block->getOperations().splice(iterator, getBlock()->getOperations(),
520                                 getIterator());
521 }
522 
523 /// This drops all operand uses from this operation, which is an essential
524 /// step in breaking cyclic dependences between references when they are to
525 /// be deleted.
526 void Operation::dropAllReferences() {
527   for (auto &op : getOpOperands())
528     op.drop();
529 
530   for (auto &region : getRegions())
531     region.dropAllReferences();
532 
533   for (auto &dest : getBlockOperands())
534     dest.drop();
535 }
536 
537 /// This drops all uses of any values defined by this operation or its nested
538 /// regions, wherever they are located.
539 void Operation::dropAllDefinedValueUses() {
540   dropAllUses();
541 
542   for (auto &region : getRegions())
543     for (auto &block : region)
544       block.dropAllDefinedValueUses();
545 }
546 
547 /// Return the number of results held by this operation.
548 unsigned Operation::getNumResults() {
549   if (!resultType)
550     return 0;
551   return hasSingleResult ? 1 : resultType.cast<TupleType>().size();
552 }
553 
554 void Operation::setSuccessor(Block *block, unsigned index) {
555   assert(index < getNumSuccessors());
556   getBlockOperands()[index].set(block);
557 }
558 
559 auto Operation::getNonSuccessorOperands() -> operand_range {
560   return getOperands().take_front(hasSuccessors() ? getSuccessorOperandIndex(0)
561                                                   : getNumOperands());
562 }
563 
564 /// Get the index of the first operand of the successor at the provided
565 /// index.
566 unsigned Operation::getSuccessorOperandIndex(unsigned index) {
567   assert(!isKnownNonTerminator() && "only terminators may have successors");
568   assert(index < getNumSuccessors());
569 
570   // Count the number of operands for each of the successors after, and
571   // including, the one at 'index'. This is based upon the assumption that all
572   // non successor operands are placed at the beginning of the operand list.
573   auto blockOperands = getBlockOperands().drop_front(index);
574   unsigned postSuccessorOpCount =
575       std::accumulate(blockOperands.begin(), blockOperands.end(), 0u,
576                       [](unsigned cur, const BlockOperand &operand) {
577                         return cur + operand.numSuccessorOperands;
578                       });
579   return getNumOperands() - postSuccessorOpCount;
580 }
581 
582 Optional<std::pair<unsigned, unsigned>>
583 Operation::decomposeSuccessorOperandIndex(unsigned operandIndex) {
584   assert(!isKnownNonTerminator() && "only terminators may have successors");
585   assert(operandIndex < getNumOperands());
586   unsigned currentOperandIndex = getNumOperands();
587   auto blockOperands = getBlockOperands();
588   for (unsigned i = 0, e = getNumSuccessors(); i < e; i++) {
589     unsigned successorIndex = e - i - 1;
590     currentOperandIndex -= blockOperands[successorIndex].numSuccessorOperands;
591     if (currentOperandIndex <= operandIndex)
592       return std::make_pair(successorIndex, operandIndex - currentOperandIndex);
593   }
594   return None;
595 }
596 
597 auto Operation::getSuccessorOperands(unsigned index) -> operand_range {
598   unsigned succOperandIndex = getSuccessorOperandIndex(index);
599   return getOperands().slice(succOperandIndex, getNumSuccessorOperands(index));
600 }
601 
602 /// Attempt to fold this operation using the Op's registered foldHook.
603 LogicalResult Operation::fold(ArrayRef<Attribute> operands,
604                               SmallVectorImpl<OpFoldResult> &results) {
605   // If we have a registered operation definition matching this one, use it to
606   // try to constant fold the operation.
607   auto *abstractOp = getAbstractOperation();
608   if (abstractOp && succeeded(abstractOp->foldHook(this, operands, results)))
609     return success();
610 
611   // Otherwise, fall back on the dialect hook to handle it.
612   Dialect *dialect = getDialect();
613   if (!dialect)
614     return failure();
615 
616   SmallVector<Attribute, 8> constants;
617   if (failed(dialect->constantFoldHook(this, operands, constants)))
618     return failure();
619   results.assign(constants.begin(), constants.end());
620   return success();
621 }
622 
623 /// Emit an error with the op name prefixed, like "'dim' op " which is
624 /// convenient for verifiers.
625 InFlightDiagnostic Operation::emitOpError(const Twine &message) {
626   return emitError() << "'" << getName() << "' op " << message;
627 }
628 
629 //===----------------------------------------------------------------------===//
630 // Operation Cloning
631 //===----------------------------------------------------------------------===//
632 
633 /// Create a deep copy of this operation but keep the operation regions empty.
634 /// Operands are remapped using `mapper` (if present), and `mapper` is updated
635 /// to contain the results.
636 Operation *Operation::cloneWithoutRegions(BlockAndValueMapping &mapper) {
637   SmallVector<Value, 8> operands;
638   SmallVector<Block *, 2> successors;
639 
640   operands.reserve(getNumOperands() + getNumSuccessors());
641 
642   if (getNumSuccessors() == 0) {
643     // Non-branching operations can just add all the operands.
644     for (auto opValue : getOperands())
645       operands.push_back(mapper.lookupOrDefault(opValue));
646   } else {
647     // We add the operands separated by nullptr's for each successor.
648     unsigned firstSuccOperand =
649         getNumSuccessors() ? getSuccessorOperandIndex(0) : getNumOperands();
650     auto opOperands = getOpOperands();
651 
652     unsigned i = 0;
653     for (; i != firstSuccOperand; ++i)
654       operands.push_back(mapper.lookupOrDefault(opOperands[i].get()));
655 
656     successors.reserve(getNumSuccessors());
657     for (unsigned succ = 0, e = getNumSuccessors(); succ != e; ++succ) {
658       successors.push_back(mapper.lookupOrDefault(getSuccessor(succ)));
659 
660       // Add sentinel to delineate successor operands.
661       operands.push_back(nullptr);
662 
663       // Remap the successors operands.
664       for (auto operand : getSuccessorOperands(succ))
665         operands.push_back(mapper.lookupOrDefault(operand));
666     }
667   }
668 
669   SmallVector<Type, 8> resultTypes(getResultTypes());
670   unsigned numRegions = getNumRegions();
671   auto *newOp =
672       Operation::create(getLoc(), getName(), resultTypes, operands, attrs,
673                         successors, numRegions, hasResizableOperandsList());
674 
675   // Remember the mapping of any results.
676   for (unsigned i = 0, e = getNumResults(); i != e; ++i)
677     mapper.map(getResult(i), newOp->getResult(i));
678 
679   return newOp;
680 }
681 
682 Operation *Operation::cloneWithoutRegions() {
683   BlockAndValueMapping mapper;
684   return cloneWithoutRegions(mapper);
685 }
686 
687 /// Create a deep copy of this operation, remapping any operands that use
688 /// values outside of the operation using the map that is provided (leaving
689 /// them alone if no entry is present).  Replaces references to cloned
690 /// sub-operations to the corresponding operation that is copied, and adds
691 /// those mappings to the map.
692 Operation *Operation::clone(BlockAndValueMapping &mapper) {
693   auto *newOp = cloneWithoutRegions(mapper);
694 
695   // Clone the regions.
696   for (unsigned i = 0; i != numRegions; ++i)
697     getRegion(i).cloneInto(&newOp->getRegion(i), mapper);
698 
699   return newOp;
700 }
701 
702 Operation *Operation::clone() {
703   BlockAndValueMapping mapper;
704   return clone(mapper);
705 }
706 
707 //===----------------------------------------------------------------------===//
708 // OpState trait class.
709 //===----------------------------------------------------------------------===//
710 
711 // The fallback for the parser is to reject the custom assembly form.
712 ParseResult OpState::parse(OpAsmParser &parser, OperationState &result) {
713   return parser.emitError(parser.getNameLoc(), "has no custom assembly form");
714 }
715 
716 // The fallback for the printer is to print in the generic assembly form.
717 void OpState::print(OpAsmPrinter &p) { p.printGenericOp(getOperation()); }
718 
719 /// Emit an error about fatal conditions with this operation, reporting up to
720 /// any diagnostic handlers that may be listening.
721 InFlightDiagnostic OpState::emitError(const Twine &message) {
722   return getOperation()->emitError(message);
723 }
724 
725 /// Emit an error with the op name prefixed, like "'dim' op " which is
726 /// convenient for verifiers.
727 InFlightDiagnostic OpState::emitOpError(const Twine &message) {
728   return getOperation()->emitOpError(message);
729 }
730 
731 /// Emit a warning about this operation, reporting up to any diagnostic
732 /// handlers that may be listening.
733 InFlightDiagnostic OpState::emitWarning(const Twine &message) {
734   return getOperation()->emitWarning(message);
735 }
736 
737 /// Emit a remark about this operation, reporting up to any diagnostic
738 /// handlers that may be listening.
739 InFlightDiagnostic OpState::emitRemark(const Twine &message) {
740   return getOperation()->emitRemark(message);
741 }
742 
743 //===----------------------------------------------------------------------===//
744 // Op Trait implementations
745 //===----------------------------------------------------------------------===//
746 
747 LogicalResult OpTrait::impl::verifyZeroOperands(Operation *op) {
748   if (op->getNumOperands() != 0)
749     return op->emitOpError() << "requires zero operands";
750   return success();
751 }
752 
753 LogicalResult OpTrait::impl::verifyOneOperand(Operation *op) {
754   if (op->getNumOperands() != 1)
755     return op->emitOpError() << "requires a single operand";
756   return success();
757 }
758 
759 LogicalResult OpTrait::impl::verifyNOperands(Operation *op,
760                                              unsigned numOperands) {
761   if (op->getNumOperands() != numOperands) {
762     return op->emitOpError() << "expected " << numOperands
763                              << " operands, but found " << op->getNumOperands();
764   }
765   return success();
766 }
767 
768 LogicalResult OpTrait::impl::verifyAtLeastNOperands(Operation *op,
769                                                     unsigned numOperands) {
770   if (op->getNumOperands() < numOperands)
771     return op->emitOpError()
772            << "expected " << numOperands << " or more operands";
773   return success();
774 }
775 
776 /// If this is a vector type, or a tensor type, return the scalar element type
777 /// that it is built around, otherwise return the type unmodified.
778 static Type getTensorOrVectorElementType(Type type) {
779   if (auto vec = type.dyn_cast<VectorType>())
780     return vec.getElementType();
781 
782   // Look through tensor<vector<...>> to find the underlying element type.
783   if (auto tensor = type.dyn_cast<TensorType>())
784     return getTensorOrVectorElementType(tensor.getElementType());
785   return type;
786 }
787 
788 LogicalResult OpTrait::impl::verifyOperandsAreIntegerLike(Operation *op) {
789   for (auto opType : op->getOperandTypes()) {
790     auto type = getTensorOrVectorElementType(opType);
791     if (!type.isIntOrIndex())
792       return op->emitOpError() << "requires an integer or index type";
793   }
794   return success();
795 }
796 
797 LogicalResult OpTrait::impl::verifyOperandsAreFloatLike(Operation *op) {
798   for (auto opType : op->getOperandTypes()) {
799     auto type = getTensorOrVectorElementType(opType);
800     if (!type.isa<FloatType>())
801       return op->emitOpError("requires a float type");
802   }
803   return success();
804 }
805 
806 LogicalResult OpTrait::impl::verifySameTypeOperands(Operation *op) {
807   // Zero or one operand always have the "same" type.
808   unsigned nOperands = op->getNumOperands();
809   if (nOperands < 2)
810     return success();
811 
812   auto type = op->getOperand(0).getType();
813   for (auto opType : llvm::drop_begin(op->getOperandTypes(), 1))
814     if (opType != type)
815       return op->emitOpError() << "requires all operands to have the same type";
816   return success();
817 }
818 
819 LogicalResult OpTrait::impl::verifyZeroResult(Operation *op) {
820   if (op->getNumResults() != 0)
821     return op->emitOpError() << "requires zero results";
822   return success();
823 }
824 
825 LogicalResult OpTrait::impl::verifyOneResult(Operation *op) {
826   if (op->getNumResults() != 1)
827     return op->emitOpError() << "requires one result";
828   return success();
829 }
830 
831 LogicalResult OpTrait::impl::verifyNResults(Operation *op,
832                                             unsigned numOperands) {
833   if (op->getNumResults() != numOperands)
834     return op->emitOpError() << "expected " << numOperands << " results";
835   return success();
836 }
837 
838 LogicalResult OpTrait::impl::verifyAtLeastNResults(Operation *op,
839                                                    unsigned numOperands) {
840   if (op->getNumResults() < numOperands)
841     return op->emitOpError()
842            << "expected " << numOperands << " or more results";
843   return success();
844 }
845 
846 LogicalResult OpTrait::impl::verifySameOperandsShape(Operation *op) {
847   if (failed(verifyAtLeastNOperands(op, 1)))
848     return failure();
849 
850   auto type = op->getOperand(0).getType();
851   for (auto opType : llvm::drop_begin(op->getOperandTypes(), 1)) {
852     if (failed(verifyCompatibleShape(opType, type)))
853       return op->emitOpError() << "requires the same shape for all operands";
854   }
855   return success();
856 }
857 
858 LogicalResult OpTrait::impl::verifySameOperandsAndResultShape(Operation *op) {
859   if (failed(verifyAtLeastNOperands(op, 1)) ||
860       failed(verifyAtLeastNResults(op, 1)))
861     return failure();
862 
863   auto type = op->getOperand(0).getType();
864   for (auto resultType : op->getResultTypes()) {
865     if (failed(verifyCompatibleShape(resultType, type)))
866       return op->emitOpError()
867              << "requires the same shape for all operands and results";
868   }
869   for (auto opType : llvm::drop_begin(op->getOperandTypes(), 1)) {
870     if (failed(verifyCompatibleShape(opType, type)))
871       return op->emitOpError()
872              << "requires the same shape for all operands and results";
873   }
874   return success();
875 }
876 
877 LogicalResult OpTrait::impl::verifySameOperandsElementType(Operation *op) {
878   if (failed(verifyAtLeastNOperands(op, 1)))
879     return failure();
880   auto elementType = getElementTypeOrSelf(op->getOperand(0));
881 
882   for (auto operand : llvm::drop_begin(op->getOperands(), 1)) {
883     if (getElementTypeOrSelf(operand) != elementType)
884       return op->emitOpError("requires the same element type for all operands");
885   }
886 
887   return success();
888 }
889 
890 LogicalResult
891 OpTrait::impl::verifySameOperandsAndResultElementType(Operation *op) {
892   if (failed(verifyAtLeastNOperands(op, 1)) ||
893       failed(verifyAtLeastNResults(op, 1)))
894     return failure();
895 
896   auto elementType = getElementTypeOrSelf(op->getResult(0));
897 
898   // Verify result element type matches first result's element type.
899   for (auto result : llvm::drop_begin(op->getResults(), 1)) {
900     if (getElementTypeOrSelf(result) != elementType)
901       return op->emitOpError(
902           "requires the same element type for all operands and results");
903   }
904 
905   // Verify operand's element type matches first result's element type.
906   for (auto operand : op->getOperands()) {
907     if (getElementTypeOrSelf(operand) != elementType)
908       return op->emitOpError(
909           "requires the same element type for all operands and results");
910   }
911 
912   return success();
913 }
914 
915 LogicalResult OpTrait::impl::verifySameOperandsAndResultType(Operation *op) {
916   if (failed(verifyAtLeastNOperands(op, 1)) ||
917       failed(verifyAtLeastNResults(op, 1)))
918     return failure();
919 
920   auto type = op->getResult(0).getType();
921   auto elementType = getElementTypeOrSelf(type);
922   for (auto resultType : llvm::drop_begin(op->getResultTypes(), 1)) {
923     if (getElementTypeOrSelf(resultType) != elementType ||
924         failed(verifyCompatibleShape(resultType, type)))
925       return op->emitOpError()
926              << "requires the same type for all operands and results";
927   }
928   for (auto opType : op->getOperandTypes()) {
929     if (getElementTypeOrSelf(opType) != elementType ||
930         failed(verifyCompatibleShape(opType, type)))
931       return op->emitOpError()
932              << "requires the same type for all operands and results";
933   }
934   return success();
935 }
936 
937 static LogicalResult verifySuccessor(Operation *op, unsigned succNo) {
938   Operation::operand_range operands = op->getSuccessorOperands(succNo);
939   unsigned operandCount = op->getNumSuccessorOperands(succNo);
940   Block *destBB = op->getSuccessor(succNo);
941   if (operandCount != destBB->getNumArguments())
942     return op->emitError() << "branch has " << operandCount
943                            << " operands for successor #" << succNo
944                            << ", but target block has "
945                            << destBB->getNumArguments();
946 
947   auto operandIt = operands.begin();
948   for (unsigned i = 0, e = operandCount; i != e; ++i, ++operandIt) {
949     if ((*operandIt).getType() != destBB->getArgument(i).getType())
950       return op->emitError() << "type mismatch for bb argument #" << i
951                              << " of successor #" << succNo;
952   }
953 
954   return success();
955 }
956 
957 static LogicalResult verifyTerminatorSuccessors(Operation *op) {
958   auto *parent = op->getParentRegion();
959 
960   // Verify that the operands lines up with the BB arguments in the successor.
961   for (unsigned i = 0, e = op->getNumSuccessors(); i != e; ++i) {
962     auto *succ = op->getSuccessor(i);
963     if (succ->getParent() != parent)
964       return op->emitError("reference to block defined in another region");
965     if (failed(verifySuccessor(op, i)))
966       return failure();
967   }
968   return success();
969 }
970 
971 LogicalResult OpTrait::impl::verifyIsTerminator(Operation *op) {
972   Block *block = op->getBlock();
973   // Verify that the operation is at the end of the respective parent block.
974   if (!block || &block->back() != op)
975     return op->emitOpError("must be the last operation in the parent block");
976 
977   // Verify the state of the successor blocks.
978   if (op->getNumSuccessors() != 0 && failed(verifyTerminatorSuccessors(op)))
979     return failure();
980   return success();
981 }
982 
983 LogicalResult OpTrait::impl::verifyResultsAreBoolLike(Operation *op) {
984   for (auto resultType : op->getResultTypes()) {
985     auto elementType = getTensorOrVectorElementType(resultType);
986     bool isBoolType = elementType.isInteger(1);
987     if (!isBoolType)
988       return op->emitOpError() << "requires a bool result type";
989   }
990 
991   return success();
992 }
993 
994 LogicalResult OpTrait::impl::verifyResultsAreFloatLike(Operation *op) {
995   for (auto resultType : op->getResultTypes())
996     if (!getTensorOrVectorElementType(resultType).isa<FloatType>())
997       return op->emitOpError() << "requires a floating point type";
998 
999   return success();
1000 }
1001 
1002 LogicalResult OpTrait::impl::verifyResultsAreIntegerLike(Operation *op) {
1003   for (auto resultType : op->getResultTypes())
1004     if (!getTensorOrVectorElementType(resultType).isIntOrIndex())
1005       return op->emitOpError() << "requires an integer or index type";
1006   return success();
1007 }
1008 
1009 static LogicalResult verifyValueSizeAttr(Operation *op, StringRef attrName,
1010                                          bool isOperand) {
1011   auto sizeAttr = op->getAttrOfType<DenseIntElementsAttr>(attrName);
1012   if (!sizeAttr)
1013     return op->emitOpError("requires 1D vector attribute '") << attrName << "'";
1014 
1015   auto sizeAttrType = sizeAttr.getType().dyn_cast<VectorType>();
1016   if (!sizeAttrType || sizeAttrType.getRank() != 1)
1017     return op->emitOpError("requires 1D vector attribute '") << attrName << "'";
1018 
1019   if (llvm::any_of(sizeAttr.getIntValues(), [](const APInt &element) {
1020         return !element.isNonNegative();
1021       }))
1022     return op->emitOpError("'")
1023            << attrName << "' attribute cannot have negative elements";
1024 
1025   size_t totalCount = std::accumulate(
1026       sizeAttr.begin(), sizeAttr.end(), 0,
1027       [](unsigned all, APInt one) { return all + one.getZExtValue(); });
1028 
1029   if (isOperand && totalCount != op->getNumOperands())
1030     return op->emitOpError("operand count (")
1031            << op->getNumOperands() << ") does not match with the total size ("
1032            << totalCount << ") specified in attribute '" << attrName << "'";
1033   else if (!isOperand && totalCount != op->getNumResults())
1034     return op->emitOpError("result count (")
1035            << op->getNumResults() << ") does not match with the total size ("
1036            << totalCount << ") specified in attribute '" << attrName << "'";
1037   return success();
1038 }
1039 
1040 LogicalResult OpTrait::impl::verifyOperandSizeAttr(Operation *op,
1041                                                    StringRef attrName) {
1042   return verifyValueSizeAttr(op, attrName, /*isOperand=*/true);
1043 }
1044 
1045 LogicalResult OpTrait::impl::verifyResultSizeAttr(Operation *op,
1046                                                   StringRef attrName) {
1047   return verifyValueSizeAttr(op, attrName, /*isOperand=*/false);
1048 }
1049 
1050 //===----------------------------------------------------------------------===//
1051 // BinaryOp implementation
1052 //===----------------------------------------------------------------------===//
1053 
1054 // These functions are out-of-line implementations of the methods in BinaryOp,
1055 // which avoids them being template instantiated/duplicated.
1056 
1057 void impl::buildBinaryOp(Builder *builder, OperationState &result, Value lhs,
1058                          Value rhs) {
1059   assert(lhs.getType() == rhs.getType());
1060   result.addOperands({lhs, rhs});
1061   result.types.push_back(lhs.getType());
1062 }
1063 
1064 ParseResult impl::parseOneResultSameOperandTypeOp(OpAsmParser &parser,
1065                                                   OperationState &result) {
1066   SmallVector<OpAsmParser::OperandType, 2> ops;
1067   Type type;
1068   return failure(parser.parseOperandList(ops) ||
1069                  parser.parseOptionalAttrDict(result.attributes) ||
1070                  parser.parseColonType(type) ||
1071                  parser.resolveOperands(ops, type, result.operands) ||
1072                  parser.addTypeToList(type, result.types));
1073 }
1074 
1075 void impl::printOneResultOp(Operation *op, OpAsmPrinter &p) {
1076   assert(op->getNumResults() == 1 && "op should have one result");
1077 
1078   // If not all the operand and result types are the same, just use the
1079   // generic assembly form to avoid omitting information in printing.
1080   auto resultType = op->getResult(0).getType();
1081   if (llvm::any_of(op->getOperandTypes(),
1082                    [&](Type type) { return type != resultType; })) {
1083     p.printGenericOp(op);
1084     return;
1085   }
1086 
1087   p << op->getName() << ' ';
1088   p.printOperands(op->getOperands());
1089   p.printOptionalAttrDict(op->getAttrs());
1090   // Now we can output only one type for all operands and the result.
1091   p << " : " << resultType;
1092 }
1093 
1094 //===----------------------------------------------------------------------===//
1095 // CastOp implementation
1096 //===----------------------------------------------------------------------===//
1097 
1098 void impl::buildCastOp(Builder *builder, OperationState &result, Value source,
1099                        Type destType) {
1100   result.addOperands(source);
1101   result.addTypes(destType);
1102 }
1103 
1104 ParseResult impl::parseCastOp(OpAsmParser &parser, OperationState &result) {
1105   OpAsmParser::OperandType srcInfo;
1106   Type srcType, dstType;
1107   return failure(parser.parseOperand(srcInfo) ||
1108                  parser.parseOptionalAttrDict(result.attributes) ||
1109                  parser.parseColonType(srcType) ||
1110                  parser.resolveOperand(srcInfo, srcType, result.operands) ||
1111                  parser.parseKeywordType("to", dstType) ||
1112                  parser.addTypeToList(dstType, result.types));
1113 }
1114 
1115 void impl::printCastOp(Operation *op, OpAsmPrinter &p) {
1116   p << op->getName() << ' ' << op->getOperand(0);
1117   p.printOptionalAttrDict(op->getAttrs());
1118   p << " : " << op->getOperand(0).getType() << " to "
1119     << op->getResult(0).getType();
1120 }
1121 
1122 Value impl::foldCastOp(Operation *op) {
1123   // Identity cast
1124   if (op->getOperand(0).getType() == op->getResult(0).getType())
1125     return op->getOperand(0);
1126   return nullptr;
1127 }
1128 
1129 //===----------------------------------------------------------------------===//
1130 // Misc. utils
1131 //===----------------------------------------------------------------------===//
1132 
1133 /// Insert an operation, generated by `buildTerminatorOp`, at the end of the
1134 /// region's only block if it does not have a terminator already. If the region
1135 /// is empty, insert a new block first. `buildTerminatorOp` should return the
1136 /// terminator operation to insert.
1137 void impl::ensureRegionTerminator(
1138     Region &region, Location loc,
1139     function_ref<Operation *()> buildTerminatorOp) {
1140   if (region.empty())
1141     region.push_back(new Block);
1142 
1143   Block &block = region.back();
1144   if (!block.empty() && block.back().isKnownTerminator())
1145     return;
1146 
1147   block.push_back(buildTerminatorOp());
1148 }
1149