2020-01-20 11:14:37 +08:00
|
|
|
//===- Visitors.cpp - MLIR Visitor Utilities ------------------------------===//
|
2019-08-30 04:04:22 +08:00
|
|
|
//
|
2020-01-26 11:58:30 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
2019-12-24 01:35:36 +08:00
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2019-08-30 04:04:22 +08:00
|
|
|
//
|
2019-12-24 01:35:36 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2019-08-30 04:04:22 +08:00
|
|
|
|
|
|
|
#include "mlir/IR/Visitors.h"
|
|
|
|
#include "mlir/IR/Operation.h"
|
|
|
|
|
|
|
|
using namespace mlir;
|
|
|
|
|
2020-11-04 19:02:08 +08:00
|
|
|
/// Walk all of the regions/blocks/operations nested under and including the
|
2021-03-06 05:47:36 +08:00
|
|
|
/// given operation. Regions, blocks and operations at the same nesting level
|
|
|
|
/// are visited in lexicographical order. The walk order for enclosing regions,
|
|
|
|
/// blocks and operations with respect to their nested ones is specified by
|
|
|
|
/// 'order'. These methods are invoked for void-returning callbacks. A callback
|
|
|
|
/// on a block or operation is allowed to erase that block or operation only if
|
|
|
|
/// the walk is in post-order. See non-void method for pre-order erasure.
|
2021-03-06 05:46:56 +08:00
|
|
|
void detail::walk(Operation *op, function_ref<void(Region *)> callback,
|
|
|
|
WalkOrder order) {
|
2021-03-06 05:47:36 +08:00
|
|
|
// We don't use early increment for regions because they can't be erased from
|
|
|
|
// a callback.
|
2020-11-04 19:02:08 +08:00
|
|
|
for (auto ®ion : op->getRegions()) {
|
2021-03-06 05:46:56 +08:00
|
|
|
if (order == WalkOrder::PreOrder)
|
|
|
|
callback(®ion);
|
2020-11-04 19:02:08 +08:00
|
|
|
for (auto &block : region) {
|
|
|
|
for (auto &nestedOp : block)
|
2021-03-06 05:46:56 +08:00
|
|
|
walk(&nestedOp, callback, order);
|
2020-11-04 19:02:08 +08:00
|
|
|
}
|
2021-03-06 05:46:56 +08:00
|
|
|
if (order == WalkOrder::PostOrder)
|
|
|
|
callback(®ion);
|
2020-11-04 19:02:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-06 05:46:56 +08:00
|
|
|
void detail::walk(Operation *op, function_ref<void(Block *)> callback,
|
|
|
|
WalkOrder order) {
|
2020-11-04 19:02:08 +08:00
|
|
|
for (auto ®ion : op->getRegions()) {
|
2021-03-06 05:47:36 +08:00
|
|
|
// Early increment here in the case where the block is erased.
|
|
|
|
for (auto &block : llvm::make_early_inc_range(region)) {
|
2021-03-06 05:46:56 +08:00
|
|
|
if (order == WalkOrder::PreOrder)
|
|
|
|
callback(&block);
|
2020-11-04 19:02:08 +08:00
|
|
|
for (auto &nestedOp : block)
|
2021-03-06 05:46:56 +08:00
|
|
|
walk(&nestedOp, callback, order);
|
|
|
|
if (order == WalkOrder::PostOrder)
|
|
|
|
callback(&block);
|
2020-11-04 19:02:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-06 05:46:56 +08:00
|
|
|
void detail::walk(Operation *op, function_ref<void(Operation *)> callback,
|
|
|
|
WalkOrder order) {
|
|
|
|
if (order == WalkOrder::PreOrder)
|
|
|
|
callback(op);
|
|
|
|
|
2020-07-07 16:35:23 +08:00
|
|
|
// TODO: This walk should be iterative over the operations.
|
2020-11-04 19:02:08 +08:00
|
|
|
for (auto ®ion : op->getRegions()) {
|
|
|
|
for (auto &block : region) {
|
2019-08-30 04:04:22 +08:00
|
|
|
// Early increment here in the case where the operation is erased.
|
|
|
|
for (auto &nestedOp : llvm::make_early_inc_range(block))
|
2021-03-06 05:46:56 +08:00
|
|
|
walk(&nestedOp, callback, order);
|
2020-11-04 19:02:08 +08:00
|
|
|
}
|
|
|
|
}
|
2021-03-06 05:46:56 +08:00
|
|
|
|
|
|
|
if (order == WalkOrder::PostOrder)
|
|
|
|
callback(op);
|
2020-10-29 21:48:07 +08:00
|
|
|
}
|
|
|
|
|
2020-11-04 19:02:08 +08:00
|
|
|
/// Walk all of the regions/blocks/operations nested under and including the
|
2021-03-06 05:47:36 +08:00
|
|
|
/// given operation. These functions walk operations until an interrupt result
|
|
|
|
/// is returned by the callback. Walks on regions, blocks and operations may
|
|
|
|
/// also be skipped if the callback returns a skip result. Regions, blocks and
|
|
|
|
/// operations at the same nesting level are visited in lexicographical order.
|
|
|
|
/// The walk order for enclosing regions, blocks and operations with respect to
|
|
|
|
/// their nested ones is specified by 'order'. A callback on a block or
|
|
|
|
/// operation is allowed to erase that block or operation if either:
|
|
|
|
/// * the walk is in post-order, or
|
|
|
|
/// * the walk is in pre-order and the walk is skipped after the erasure.
|
2020-11-04 19:02:08 +08:00
|
|
|
WalkResult detail::walk(Operation *op,
|
2021-03-06 05:46:56 +08:00
|
|
|
function_ref<WalkResult(Region *)> callback,
|
|
|
|
WalkOrder order) {
|
2021-03-06 05:47:36 +08:00
|
|
|
// We don't use early increment for regions because they can't be erased from
|
|
|
|
// a callback.
|
2020-11-04 19:02:08 +08:00
|
|
|
for (auto ®ion : op->getRegions()) {
|
2021-03-06 05:47:36 +08:00
|
|
|
if (order == WalkOrder::PreOrder) {
|
|
|
|
WalkResult result = callback(®ion);
|
|
|
|
if (result.wasSkipped())
|
|
|
|
continue;
|
|
|
|
if (result.wasInterrupted())
|
2021-03-06 05:46:56 +08:00
|
|
|
return WalkResult::interrupt();
|
2021-03-06 05:47:36 +08:00
|
|
|
}
|
2020-11-04 19:02:08 +08:00
|
|
|
for (auto &block : region) {
|
|
|
|
for (auto &nestedOp : block)
|
2021-03-06 05:46:56 +08:00
|
|
|
walk(&nestedOp, callback, order);
|
2020-11-04 19:02:08 +08:00
|
|
|
}
|
2021-03-06 05:47:36 +08:00
|
|
|
if (order == WalkOrder::PostOrder) {
|
2021-03-06 05:46:56 +08:00
|
|
|
if (callback(®ion).wasInterrupted())
|
|
|
|
return WalkResult::interrupt();
|
2021-03-06 05:47:36 +08:00
|
|
|
// We don't check if this region was skipped because its walk already
|
|
|
|
// finished and the walk will continue with the next region.
|
|
|
|
}
|
2020-11-04 19:02:08 +08:00
|
|
|
}
|
|
|
|
return WalkResult::advance();
|
|
|
|
}
|
|
|
|
|
|
|
|
WalkResult detail::walk(Operation *op,
|
2021-03-06 05:46:56 +08:00
|
|
|
function_ref<WalkResult(Block *)> callback,
|
|
|
|
WalkOrder order) {
|
2020-11-04 19:02:08 +08:00
|
|
|
for (auto ®ion : op->getRegions()) {
|
2021-03-06 05:47:36 +08:00
|
|
|
// Early increment here in the case where the block is erased.
|
|
|
|
for (auto &block : llvm::make_early_inc_range(region)) {
|
|
|
|
if (order == WalkOrder::PreOrder) {
|
|
|
|
WalkResult result = callback(&block);
|
|
|
|
if (result.wasSkipped())
|
|
|
|
continue;
|
|
|
|
if (result.wasInterrupted())
|
2021-03-06 05:46:56 +08:00
|
|
|
return WalkResult::interrupt();
|
2021-03-06 05:47:36 +08:00
|
|
|
}
|
2020-11-04 19:02:08 +08:00
|
|
|
for (auto &nestedOp : block)
|
2021-03-06 05:46:56 +08:00
|
|
|
walk(&nestedOp, callback, order);
|
2021-03-06 05:47:36 +08:00
|
|
|
if (order == WalkOrder::PostOrder) {
|
2021-03-06 05:46:56 +08:00
|
|
|
if (callback(&block).wasInterrupted())
|
|
|
|
return WalkResult::interrupt();
|
2021-03-06 05:47:36 +08:00
|
|
|
// We don't check if this block was skipped because its walk already
|
|
|
|
// finished and the walk will continue with the next block.
|
|
|
|
}
|
2020-11-04 19:02:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return WalkResult::advance();
|
|
|
|
}
|
|
|
|
|
|
|
|
WalkResult detail::walk(Operation *op,
|
2021-03-06 05:46:56 +08:00
|
|
|
function_ref<WalkResult(Operation *)> callback,
|
|
|
|
WalkOrder order) {
|
2021-03-06 05:47:36 +08:00
|
|
|
if (order == WalkOrder::PreOrder) {
|
|
|
|
WalkResult result = callback(op);
|
|
|
|
// If skipped, caller will continue the walk on the next operation.
|
|
|
|
if (result.wasSkipped())
|
|
|
|
return WalkResult::advance();
|
|
|
|
if (result.wasInterrupted())
|
2021-03-06 05:46:56 +08:00
|
|
|
return WalkResult::interrupt();
|
2021-03-06 05:47:36 +08:00
|
|
|
}
|
2021-03-06 05:46:56 +08:00
|
|
|
|
2020-07-07 16:35:23 +08:00
|
|
|
// TODO: This walk should be iterative over the operations.
|
2019-08-31 03:47:24 +08:00
|
|
|
for (auto ®ion : op->getRegions()) {
|
|
|
|
for (auto &block : region) {
|
|
|
|
// Early increment here in the case where the operation is erased.
|
2020-11-04 19:02:08 +08:00
|
|
|
for (auto &nestedOp : llvm::make_early_inc_range(block)) {
|
2021-03-06 05:46:56 +08:00
|
|
|
if (walk(&nestedOp, callback, order).wasInterrupted())
|
2019-08-31 03:47:24 +08:00
|
|
|
return WalkResult::interrupt();
|
2020-11-04 19:02:08 +08:00
|
|
|
}
|
2019-08-31 03:47:24 +08:00
|
|
|
}
|
|
|
|
}
|
2021-03-06 05:46:56 +08:00
|
|
|
|
|
|
|
if (order == WalkOrder::PostOrder)
|
|
|
|
return callback(op);
|
|
|
|
return WalkResult::advance();
|
2019-08-31 03:47:24 +08:00
|
|
|
}
|