Skip to content

Commit 87d77d3

Browse files
authored
[mlir][IR] Insert operations before SingleBlock's terminator (#65959)
Change `SingleBlock::{insert,push_back}` to avoid inserting the argument operation after the block's terminator. This allows removing `SingleBlockImplicitTerminator`'s functions with the same name. Define `Block::hasTerminator` checking whether the block has a terminator operation. Signed-off-by: Victor Perez <[email protected]>
1 parent b64bf89 commit 87d77d3

File tree

3 files changed

+13
-32
lines changed

3 files changed

+13
-32
lines changed

mlir/include/mlir/IR/Block.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,9 @@ class Block : public IRObjectWithUseList<BlockOperand>,
214214
/// the block has a valid terminator operation.
215215
Operation *getTerminator();
216216

217+
/// Check whether this block has a terminator.
218+
bool hasTerminator();
219+
217220
//===--------------------------------------------------------------------===//
218221
// Predecessors and successors.
219222
//===--------------------------------------------------------------------===//

mlir/include/mlir/IR/OpDefinition.h

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -932,6 +932,10 @@ struct SingleBlock : public TraitBase<ConcreteType, SingleBlock> {
932932
}
933933
template <typename OpT = ConcreteType>
934934
enable_if_single_region<OpT> insert(Block::iterator insertPt, Operation *op) {
935+
Block *body = getBody();
936+
// Insert op before the block's terminator if it has one
937+
if (insertPt == body->end() && body->hasTerminator())
938+
insertPt = Block::iterator(body->getTerminator());
935939
getBody()->getOperations().insert(insertPt, op);
936940
}
937941
};
@@ -997,37 +1001,6 @@ struct SingleBlockImplicitTerminator {
9971001
::mlir::impl::ensureRegionTerminator(region, builder, loc,
9981002
buildTerminator);
9991003
}
1000-
1001-
//===------------------------------------------------------------------===//
1002-
// Single Region Utilities
1003-
//===------------------------------------------------------------------===//
1004-
1005-
template <typename OpT, typename T = void>
1006-
using enable_if_single_region =
1007-
std::enable_if_t<OpT::template hasTrait<OneRegion>(), T>;
1008-
1009-
/// Insert the operation into the back of the body, before the terminator.
1010-
template <typename OpT = ConcreteType>
1011-
enable_if_single_region<OpT> push_back(Operation *op) {
1012-
Block *body = static_cast<SingleBlock<ConcreteType> *>(this)->getBody();
1013-
insert(Block::iterator(body->getTerminator()), op);
1014-
}
1015-
1016-
/// Insert the operation at the given insertion point. Note: The operation
1017-
/// is never inserted after the terminator, even if the insertion point is
1018-
/// end().
1019-
template <typename OpT = ConcreteType>
1020-
enable_if_single_region<OpT> insert(Operation *insertPt, Operation *op) {
1021-
insert(Block::iterator(insertPt), op);
1022-
}
1023-
template <typename OpT = ConcreteType>
1024-
enable_if_single_region<OpT> insert(Block::iterator insertPt,
1025-
Operation *op) {
1026-
Block *body = static_cast<SingleBlock<ConcreteType> *>(this)->getBody();
1027-
if (insertPt == body->end())
1028-
insertPt = Block::iterator(body->getTerminator());
1029-
body->getOperations().insert(insertPt, op);
1030-
}
10311004
};
10321005
};
10331006

mlir/lib/IR/Block.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,10 +236,15 @@ void Block::eraseArguments(function_ref<bool(BlockArgument)> shouldEraseFn) {
236236
/// Get the terminator operation of this block. This function asserts that
237237
/// the block has a valid terminator operation.
238238
Operation *Block::getTerminator() {
239-
assert(!empty() && back().mightHaveTrait<OpTrait::IsTerminator>());
239+
assert(hasTerminator());
240240
return &back();
241241
}
242242

243+
/// Check whether this block has a terminator.
244+
bool Block::hasTerminator() {
245+
return !empty() && back().mightHaveTrait<OpTrait::IsTerminator>();
246+
}
247+
243248
// Indexed successor access.
244249
unsigned Block::getNumSuccessors() {
245250
return empty() ? 0 : back().getNumSuccessors();

0 commit comments

Comments
 (0)