diff --git a/mlir/lib/Transforms/CSE.cpp b/mlir/lib/Transforms/CSE.cpp --- a/mlir/lib/Transforms/CSE.cpp +++ b/mlir/lib/Transforms/CSE.cpp @@ -30,6 +30,16 @@ using namespace mlir; +static bool hasNoOrEmptyRegions(Operation *op) { + return op->getNumRegions() == 0 || + llvm::all_of(op->getRegions(), + [](auto ®ion) { return region.empty(); }); +} + +static bool hasSingleRegionWithSingleBlock(Operation *op) { + return op->getNumRegions() == 1 && llvm::hasSingleElement(op->getRegion(0)); +} + namespace { struct SimpleOperationInfo : public llvm::DenseMapInfo { static unsigned getHashValue(const Operation *opC) { diff --git a/mlir/test/Transforms/cse.mlir b/mlir/test/Transforms/cse.mlir --- a/mlir/test/Transforms/cse.mlir +++ b/mlir/test/Transforms/cse.mlir @@ -493,3 +493,17 @@ // CHECK: } // CHECK-NOT: scf.if // CHECK: return %[[if]], %[[if]] + +// Operations with empty region CSE +// CHECK-LABEL: func @cse_ops_with_empty_region +// CHECK: %[[OP0:.*[0]]] = "test.any_cond" +// CHECK-NOT: %[[OP1:.*[0]]] = "test.any_cond" +// CHECK: return %[[OP0]], %[[OP0]] +func.func @cse_ops_with_empty_region() + -> (i32, i32) { + %0 = "test.any_cond"() ({ + }) : () -> i32 + %1 = "test.any_cond"() ({ + }) : () -> i32 + return %0, %1 : i32, i32 +}