[Mlir-commits] [mlir] [mlir] Fix false positive loops in RegionBranchOpInterface::hasLoop (PR #233784)

Mehdi Amini llvmlistbot at llvm.org
Wed Oct 21 00:04:58 PDT 2026


================
@@ -284,3 +316,53 @@ TEST(RegionBranchOpInterface, NotRecursiveRegions) {
   EXPECT_EQ(getEnclosingRepetitiveRegion(op1), nullptr);
   EXPECT_EQ(getEnclosingRepetitiveRegion(op2), nullptr);
 }
+
+TEST(RegionBranchOpInterface, MultipleExitsToSameRegionIsNotALoop) {
+  // Both terminators of region 0 branch to region 1. Region 1 is reached
+  // twice, but the region graph has no cycle.
+  const char *ir = R"MLIR(
+"cftest.entry_regions_op"() (
+      {
+        "cftest.dummy_op"() : () -> ()
+      ^bb1:
+        "cftest.dummy_op"() : () -> ()
+      },
+      {"cftest.dummy_op"() : () -> ()}
+  ) : () -> ()
+  )MLIR";
+
+  DialectRegistry registry;
+  registry.insert<CFTestDialect>();
+  MLIRContext ctx(registry);
+
+  OwningOpRef<ModuleOp> module = parseSourceString<ModuleOp>(ir, &ctx);
+  ASSERT_TRUE(module);
+  auto regionOp = cast<RegionBranchOpInterface>(
+      &module->getBody()->getOperations().front());
+
+  EXPECT_FALSE(regionOp.isRepetitiveRegion(0));
+  EXPECT_FALSE(regionOp.isRepetitiveRegion(1));
+  EXPECT_FALSE(regionOp.hasLoop());
+}
+
+TEST(RegionBranchOpInterface, LoopAfterEntryRegion) {
+  const char *ir = R"MLIR(
+"cftest.entry_regions_op"() (
+      {"cftest.dummy_op"() : () -> ()},
+      {"cftest.dummy_op"() : () -> ()}
+  ) {loop} : () -> ()
+  )MLIR";
+
+  DialectRegistry registry;
+  registry.insert<CFTestDialect>();
+  MLIRContext ctx(registry);
+
+  OwningOpRef<ModuleOp> module = parseSourceString<ModuleOp>(ir, &ctx);
+  ASSERT_TRUE(module);
+  auto regionOp = cast<RegionBranchOpInterface>(
+      &module->getBody()->getOperations().front());
+
+  EXPECT_FALSE(regionOp.isRepetitiveRegion(0));
+  EXPECT_TRUE(regionOp.isRepetitiveRegion(1));
+  EXPECT_TRUE(regionOp.hasLoop());
+}
----------------
joker-eph wrote:

Wonder if it would be useful to have a test for a cycle among regions not reachable from the entry block?

https://github.com/llvm/llvm-project/pull/233784


More information about the Mlir-commits mailing list