Skip to content

Commit 67830fe

Browse files
committed
[mlir][IR] Insert operations before SingleBlock's terminator
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 ba6a681 commit 67830fe

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
@@ -931,6 +931,10 @@ struct SingleBlock : public TraitBase<ConcreteType, SingleBlock> {
931931
}
932932
template <typename OpT = ConcreteType>
933933
enable_if_single_region<OpT> insert(Block::iterator insertPt, Operation *op) {
934+
Block *body = getBody();
935+
// Insert op before the block's terminator if it has one
936+
if (insertPt == body->end() && body->hasTerminator())
937+
insertPt = Block::iterator(body->getTerminator());
934938
getBody()->getOperations().insert(insertPt, op);
935939
}
936940
};
@@ -995,37 +999,6 @@ struct SingleBlockImplicitTerminator {
995999
::mlir::impl::ensureRegionTerminator(region, builder, loc,
9961000
buildTerminator);
9971001
}
998-
999-
//===------------------------------------------------------------------===//
1000-
// Single Region Utilities
1001-
//===------------------------------------------------------------------===//
1002-
1003-
template <typename OpT, typename T = void>
1004-
using enable_if_single_region =
1005-
std::enable_if_t<OpT::template hasTrait<OneRegion>(), T>;
1006-
1007-
/// Insert the operation into the back of the body, before the terminator.
1008-
template <typename OpT = ConcreteType>
1009-
enable_if_single_region<OpT> push_back(Operation *op) {
1010-
Block *body = static_cast<SingleBlock<ConcreteType> *>(this)->getBody();
1011-
insert(Block::iterator(body->getTerminator()), op);
1012-
}
1013-
1014-
/// Insert the operation at the given insertion point. Note: The operation
1015-
/// is never inserted after the terminator, even if the insertion point is
1016-
/// end().
1017-
template <typename OpT = ConcreteType>
1018-
enable_if_single_region<OpT> insert(Operation *insertPt, Operation *op) {
1019-
insert(Block::iterator(insertPt), op);
1020-
}
1021-
template <typename OpT = ConcreteType>
1022-
enable_if_single_region<OpT> insert(Block::iterator insertPt,
1023-
Operation *op) {
1024-
Block *body = static_cast<SingleBlock<ConcreteType> *>(this)->getBody();
1025-
if (insertPt == body->end())
1026-
insertPt = Block::iterator(body->getTerminator());
1027-
body->getOperations().insert(insertPt, op);
1028-
}
10291002
};
10301003
};
10311004

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)