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