[flang-commits] [flang] [flang][OpenMP] Skip multi-block `teams` regions when processing `loop` directives (PR #132687)

Kareem Ergawy via flang-commits flang-commits at lists.llvm.org
Mon Mar 24 00:47:52 PDT 2025


https://github.com/ergawy created https://github.com/llvm/llvm-project/pull/132687

Fixes a regression when the generic `loop` directive conversion pass encounters a multi-block `teams` region. At the moment, we skip such regions.

>From 7da9a50ee9959fbc1ad59e70b482132f5ce04f6a Mon Sep 17 00:00:00 2001
From: ergawy <kareem.ergawy at amd.com>
Date: Mon, 24 Mar 2025 02:45:40 -0500
Subject: [PATCH] [flang][OpenMP] Skip multi-block `teams` regions when
 processing `loop` directives

Fixes a regression when the generic `loop` directive conversion pass
encounters a multi-block `teams` region. At the moment, we skip such
regions.
---
 .../OpenMP/GenericLoopConversion.cpp          |  4 ++--
 flang/test/Lower/OpenMP/loop-directive.f90    | 21 +++++++++++++++++++
 2 files changed, 23 insertions(+), 2 deletions(-)

diff --git a/flang/lib/Optimizer/OpenMP/GenericLoopConversion.cpp b/flang/lib/Optimizer/OpenMP/GenericLoopConversion.cpp
index 2ca1de1e1cbd6..74ad6330b11a7 100644
--- a/flang/lib/Optimizer/OpenMP/GenericLoopConversion.cpp
+++ b/flang/lib/Optimizer/OpenMP/GenericLoopConversion.cpp
@@ -398,8 +398,8 @@ class ReductionsHoistingPattern
 
   static mlir::omp::LoopOp
   tryToFindNestedLoopWithReduction(mlir::omp::TeamsOp teamsOp) {
-    assert(!teamsOp.getRegion().empty() &&
-           teamsOp.getRegion().getBlocks().size() == 1);
+    if (teamsOp.getRegion().getBlocks().size() != 1)
+      return nullptr;
 
     mlir::Block &teamsBlock = *teamsOp.getRegion().begin();
     auto loopOpIter = llvm::find_if(teamsBlock, [](mlir::Operation &op) {
diff --git a/flang/test/Lower/OpenMP/loop-directive.f90 b/flang/test/Lower/OpenMP/loop-directive.f90
index 8cf2e98332d7d..954985e2d64f1 100644
--- a/flang/test/Lower/OpenMP/loop-directive.f90
+++ b/flang/test/Lower/OpenMP/loop-directive.f90
@@ -337,3 +337,24 @@ subroutine loop_teams_loop_reduction
     x = x + i
   end do
 end subroutine
+
+
+! Tests a regression when the pass encounters a multi-block `teams` region.
+subroutine multi_block_teams
+  implicit none
+  integer :: i
+
+  ! CHECK: omp.target {{.*}} {
+  ! CHECK:   omp.teams {
+  ! CHECK:   ^bb1:
+  ! CHECK:     cf.br ^bb2
+  ! CHECK:   ^bb2:
+  ! CHECK:     omp.terminator
+  ! CHECK:   }
+  ! CHECK: }
+  !$omp target teams
+  select case (i)
+  case(1)
+  end select
+  !$omp end target teams
+end subroutine



More information about the flang-commits mailing list