Skip to content

Commit 0ac21e6

Browse files
[mlir][IR] SingleBlockImplicitTerminator: Declare "inherited" trait in ODS instead of C++
`SingleBlockImplicitTerminator` is now a combination of two traits: `SingleBlock` and `SingleBlockImplicitTerminatorImpl` (the original `SingleBlockImplicitTerminator`). This change makes it possible to check if the `SingleBlock` op trait is implemented. Until now, `Operation::hasTrait<OpTrait::SingleBlock>()` returned `false` for ops that implement `SingleBlockImplicitTerminator`. Differential Revision: https://reviews.llvm.org/D159078
1 parent eb27be9 commit 0ac21e6

File tree

4 files changed

+12
-13
lines changed

4 files changed

+12
-13
lines changed

mlir/include/mlir/IR/OpBase.td

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,13 @@ def ElementwiseMappable : TraitList<[
122122
// Op's regions have a single block.
123123
def SingleBlock : NativeOpTrait<"SingleBlock">, StructuralOpTrait;
124124

125+
class SingleBlockImplicitTerminatorImpl<string op>
126+
: ParamNativeOpTrait<"SingleBlockImplicitTerminator", op, [SingleBlock]>,
127+
StructuralOpTrait;
128+
125129
// Op's regions have a single block with the specified terminator.
126130
class SingleBlockImplicitTerminator<string op>
127-
: ParamNativeOpTrait<"SingleBlockImplicitTerminator", op>,
128-
StructuralOpTrait;
131+
: TraitList<[SingleBlock, SingleBlockImplicitTerminatorImpl<op>]>;
129132

130133
// Op's regions don't have terminator.
131134
def NoTerminator : NativeOpTrait<"NoTerminator">, StructuralOpTrait;

mlir/include/mlir/IR/OpDefinition.h

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -943,9 +943,8 @@ struct SingleBlock : public TraitBase<ConcreteType, SingleBlock> {
943943
template <typename TerminatorOpType>
944944
struct SingleBlockImplicitTerminator {
945945
template <typename ConcreteType>
946-
class Impl : public SingleBlock<ConcreteType> {
946+
class Impl {
947947
private:
948-
using Base = SingleBlock<ConcreteType>;
949948
/// Builds a terminator operation without relying on OpBuilder APIs to avoid
950949
/// cyclic header inclusion.
951950
static Operation *buildTerminator(OpBuilder &builder, Location loc) {
@@ -959,8 +958,6 @@ struct SingleBlockImplicitTerminator {
959958
using ImplicitTerminatorOpT = TerminatorOpType;
960959

961960
static LogicalResult verifyRegionTrait(Operation *op) {
962-
if (failed(Base::verifyTrait(op)))
963-
return failure();
964961
for (unsigned i = 0, e = op->getNumRegions(); i < e; ++i) {
965962
Region &region = op->getRegion(i);
966963
// Empty regions are fine.
@@ -1002,7 +999,6 @@ struct SingleBlockImplicitTerminator {
1002999
//===------------------------------------------------------------------===//
10031000
// Single Region Utilities
10041001
//===------------------------------------------------------------------===//
1005-
using Base::getBody;
10061002

10071003
template <typename OpT, typename T = void>
10081004
using enable_if_single_region =
@@ -1011,7 +1007,8 @@ struct SingleBlockImplicitTerminator {
10111007
/// Insert the operation into the back of the body, before the terminator.
10121008
template <typename OpT = ConcreteType>
10131009
enable_if_single_region<OpT> push_back(Operation *op) {
1014-
insert(Block::iterator(getBody()->getTerminator()), op);
1010+
Block *body = static_cast<SingleBlock<ConcreteType> *>(this)->getBody();
1011+
insert(Block::iterator(body->getTerminator()), op);
10151012
}
10161013

10171014
/// Insert the operation at the given insertion point. Note: The operation
@@ -1024,7 +1021,7 @@ struct SingleBlockImplicitTerminator {
10241021
template <typename OpT = ConcreteType>
10251022
enable_if_single_region<OpT> insert(Block::iterator insertPt,
10261023
Operation *op) {
1027-
auto *body = getBody();
1024+
Block *body = static_cast<SingleBlock<ConcreteType> *>(this)->getBody();
10281025
if (insertPt == body->end())
10291026
insertPt = Block::iterator(body->getTerminator());
10301027
body->getOperations().insert(insertPt, op);

mlir/test/mlir-tblgen/gen-dialect-doc.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def TestTypeDefParams : TypeDef<Test_Dialect, "TestTypeDefParams"> {
8181
// CHECK: Other group
8282
// CHECK: test.b
8383
// CHECK: test.c
84-
// CHECK: Traits: SingleBlockImplicitTerminator<YieldOp>
84+
// CHECK: Traits: SingleBlock, SingleBlockImplicitTerminator<YieldOp>
8585
// CHECK: Interfaces: NoMemoryEffect (MemoryEffectOpInterface)
8686
// CHECK: Effects: MemoryEffects::Effect{}
8787

mlir/tools/mlir-tblgen/OpFormatGen.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -313,11 +313,10 @@ struct OperationFormat {
313313
resultTypes.resize(op.getNumResults(), TypeResolution());
314314

315315
hasImplicitTermTrait = llvm::any_of(op.getTraits(), [](const Trait &trait) {
316-
return trait.getDef().isSubClassOf("SingleBlockImplicitTerminator");
316+
return trait.getDef().isSubClassOf("SingleBlockImplicitTerminatorImpl");
317317
});
318318

319-
hasSingleBlockTrait =
320-
hasImplicitTermTrait || op.getTrait("::mlir::OpTrait::SingleBlock");
319+
hasSingleBlockTrait = op.getTrait("::mlir::OpTrait::SingleBlock");
321320
}
322321

323322
/// Generate the operation parser from this format.

0 commit comments

Comments
 (0)