[flang-commits] [flang] [flang][OpenMP] Add missing lowering test coverage (PR #221079)

Gheorghe-Teodor Bercea via flang-commits flang-commits at lists.llvm.org
Tue Sep 15 16:50:13 PDT 2026


https://github.com/doru1004 updated https://github.com/llvm/llvm-project/pull/221079

>From 08310fedd865a16d3feea4de67df9c83fb98587e Mon Sep 17 00:00:00 2001
From: Gheorghe-Teodor Bercea <dobercea at amd.com>
Date: Thu, 3 Sep 2026 18:38:41 -0400
Subject: [PATCH] Add missing lowering test coverage

---
 .../Lower/OpenMP/hlfir-to-fir-conv-omp.mlir   |  64 ++++++
 .../Lower/OpenMP/reduction-target-spmd.f90    |  15 ++
 .../test/Lower/OpenMP/target-generic-spmd.f90 | 191 ++++++++++++++++++
 3 files changed, 270 insertions(+)
 create mode 100644 flang/test/Lower/OpenMP/hlfir-to-fir-conv-omp.mlir
 create mode 100644 flang/test/Lower/OpenMP/reduction-target-spmd.f90
 create mode 100644 flang/test/Lower/OpenMP/target-generic-spmd.f90

diff --git a/flang/test/Lower/OpenMP/hlfir-to-fir-conv-omp.mlir b/flang/test/Lower/OpenMP/hlfir-to-fir-conv-omp.mlir
new file mode 100644
index 0000000000000..217d0ca5da113
--- /dev/null
+++ b/flang/test/Lower/OpenMP/hlfir-to-fir-conv-omp.mlir
@@ -0,0 +1,64 @@
+// Tests HLFIR-to-FIR conversion aspects relevant to OpenMP. For example, that
+// the correct alloca block is chosen for OMP regions.
+// RUN: fir-opt --convert-hlfir-to-fir %s -o - | \
+// RUN: FileCheck %s
+
+fir.global internal @_QQro.1xi4.0(dense<42> : tensor<1xi32>) constant : !fir.array<1xi32>
+
+func.func @_QPfoo() {
+  %c1 = arith.constant 1 : index
+  %host_alloc = fir.alloca !fir.array<1xi32> {bindc_name = "arr", uniq_name = "_QFfooEarr"}
+
+  %1 = fir.shape %c1 : (index) -> !fir.shape<1>
+  %host_decl:2 = hlfir.declare %host_alloc(%1) {uniq_name = "_QFfooEarr"} : (!fir.ref<!fir.array<1xi32>>, !fir.shape<1>) -> (!fir.ref<!fir.array<1xi32>>, !fir.ref<!fir.array<1xi32>>)
+  %map_info = omp.map.info var_ptr(%host_decl#1 : !fir.ref<!fir.array<1xi32>>, !fir.array<1xi32>) map_clauses(implicit, tofrom) capture(ByRef) name("arr") -> !fir.ref<!fir.array<1xi32>>
+
+  %c1_3 = arith.constant 1 : i32
+  %c10 = arith.constant 10 : i32
+
+  // CHECK: omp.target
+  omp.target kernel_type(spmd) host_eval(%c1_3 -> %constarg0, %c10 -> %constarg1 : i32, i32) map_entries(%map_info -> %arg1 : !fir.ref<!fir.array<1xi32>>) {
+    %c1_2 = arith.constant 1 : index
+    %21 = fir.shape %c1_2 : (index) -> !fir.shape<1>
+
+    // CHECK: %[[TARGET_DECL:.*]] = fir.declare
+    %target_decl:2 = hlfir.declare %arg1(%21) {uniq_name = "_QFfooEarr"} : (!fir.ref<!fir.array<1xi32>>, !fir.shape<1>) -> (!fir.ref<!fir.array<1xi32>>, !fir.ref<!fir.array<1xi32>>)
+
+    // CHECK: omp.teams
+    omp.teams {
+
+      // CHECK: omp.parallel
+      omp.parallel {
+        // CHECK: %[[TO_BOX_ALLOC:.*]] = fir.alloca !fir.box<!fir.array<1xi32>> {pinned}
+        // CHECK: omp.distribute
+        omp.distribute {
+          // CHECK: omp.wsloop
+          omp.wsloop {
+            // CHECK: omp.loop_nest
+            omp.loop_nest (%arg2) : i32 = (%constarg0) to (%constarg1) inclusive step (%constarg0) {
+              %25 = fir.address_of(@_QQro.1xi4.0) : !fir.ref<!fir.array<1xi32>>
+              %26 = fir.shape %c1_2 : (index) -> !fir.shape<1>
+              %27:2 = hlfir.declare %25(%26) {fortran_attrs = #fir.var_attrs<parameter>, uniq_name = "_QQro.1xi4.0"} : (!fir.ref<!fir.array<1xi32>>, !fir.shape<1>) -> (!fir.ref<!fir.array<1xi32>>, !fir.ref<!fir.array<1xi32>>)
+
+
+              // CHECK: %[[EMBOX:.*]] = fir.embox %[[TARGET_DECL]]
+              // CHECK: fir.store %[[EMBOX]] to %[[TO_BOX_ALLOC]]
+              // CHECK: %[[BOX_ALLOC_CONV:.*]] = fir.convert %[[TO_BOX_ALLOC]] : (!fir.ref<!fir.box<!fir.array<1xi32>>>) -> !fir.ref<!fir.box<none>>
+              // CHECK: fir.call @_FortranAAssignSimple(%[[BOX_ALLOC_CONV]], {{.*}})
+              hlfir.assign %27#0 to %target_decl#0 : !fir.ref<!fir.array<1xi32>>, !fir.ref<!fir.array<1xi32>>
+              // CHECK: omp.yield
+              omp.yield
+            }
+          } {omp.composite}
+        } {omp.composite}
+        // CHECK: omp.terminator
+        omp.terminator
+      } {omp.composite}
+      // CHECK: omp.terminator
+      omp.terminator
+    } {omp.combined}
+    // CHECK: omp.terminator
+    omp.terminator
+  } {omp.combined}
+  return
+}
diff --git a/flang/test/Lower/OpenMP/reduction-target-spmd.f90 b/flang/test/Lower/OpenMP/reduction-target-spmd.f90
new file mode 100644
index 0000000000000..353c540c3bbf3
--- /dev/null
+++ b/flang/test/Lower/OpenMP/reduction-target-spmd.f90
@@ -0,0 +1,15 @@
+! RUN: %flang_fc1 -emit-fir -fopenmp -o - %s | FileCheck %s
+! RUN: bbc -emit-fir -fopenmp -o - %s | FileCheck %s
+
+! CHECK:       omp.teams
+! CHECK-SAME:  reduction(@add_reduction_i32 %{{.*}} -> %{{.*}} : !fir.ref<i32>)
+subroutine myfun()
+  integer :: i, j
+  i = 0
+  j = 0
+  !$omp target teams distribute parallel do reduction(+:i)
+  do j = 1,5
+     i = i + j
+  end do
+  !$omp end target teams distribute parallel do
+end subroutine myfun
diff --git a/flang/test/Lower/OpenMP/target-generic-spmd.f90 b/flang/test/Lower/OpenMP/target-generic-spmd.f90
new file mode 100644
index 0000000000000..d6cd8ae229b3a
--- /dev/null
+++ b/flang/test/Lower/OpenMP/target-generic-spmd.f90
@@ -0,0 +1,191 @@
+! RUN: %flang_fc1 -emit-hlfir -fopenmp %s -o - | FileCheck %s
+
+! CHECK-LABEL: func.func @_QPdistribute_generic() {
+subroutine distribute_generic()
+  ! CHECK: omp.target
+  ! CHECK-NOT: host_eval({{.*}})
+  ! CHECK-SAME: {
+  !$omp target
+  !$omp teams
+  !$omp distribute
+  do i = 1, 10
+    call foo(i)
+  end do
+  !$omp end distribute
+  call bar() !< Prevents this from being Generic-SPMD.
+  !$omp end teams
+  !$omp end target
+
+  ! CHECK: omp.target
+  ! CHECK-NOT: host_eval({{.*}})
+  ! CHECK-SAME: {
+  !$omp target teams
+  !$omp distribute
+  do i = 1, 10
+    call foo(i)
+  end do
+  !$omp end distribute
+  call bar() !< Prevents this from being Generic-SPMD.
+  !$omp end target teams
+
+  ! CHECK: omp.target
+  ! CHECK-NOT: host_eval({{.*}})
+  ! CHECK-SAME: {
+  !$omp target teams
+  !$omp distribute
+  do i = 1, 10
+    call foo(i)
+  end do
+  !$omp end distribute
+
+  !$omp distribute
+  do i = 1, 10
+    call foo(i)
+  end do
+  !$omp end distribute
+  !$omp end target teams
+end subroutine distribute_generic
+
+! CHECK-LABEL: func.func @_QPdistribute_spmd() {
+subroutine distribute_spmd()
+  ! CHECK: omp.target
+  ! CHECK-SAME: host_eval({{.*}})
+  !$omp target
+  !$omp teams
+  !$omp distribute
+  do i = 1, 10
+    call foo(i)
+  end do
+  !$omp end distribute
+  !$omp end teams
+  !$omp end target
+
+  ! CHECK: omp.target
+  ! CHECK-SAME: host_eval({{.*}})
+  !$omp target teams
+  !$omp distribute
+  do i = 1, 10
+    call foo(i)
+  end do
+  !$omp end distribute
+  !$omp end target teams
+end subroutine distribute_spmd
+
+! CHECK-LABEL: func.func @_QPdistribute_simd_generic() {
+subroutine distribute_simd_generic()
+  ! CHECK: omp.target
+  ! CHECK-NOT: host_eval({{.*}})
+  ! CHECK-SAME: {
+  !$omp target
+  !$omp teams
+  !$omp distribute simd
+  do i = 1, 10
+    call foo(i)
+  end do
+  !$omp end distribute simd
+  call bar() !< Prevents this from being Generic-SPMD.
+  !$omp end teams
+  !$omp end target
+
+  ! CHECK: omp.target
+  ! CHECK-NOT: host_eval({{.*}})
+  ! CHECK-SAME: {
+  !$omp target teams
+  !$omp distribute simd
+  do i = 1, 10
+    call foo(i)
+  end do
+  !$omp end distribute simd
+  call bar() !< Prevents this from being Generic-SPMD.
+  !$omp end target teams
+
+  ! CHECK: omp.target
+  ! CHECK-NOT: host_eval({{.*}})
+  ! CHECK-SAME: {
+  !$omp target teams
+  !$omp distribute simd
+  do i = 1, 10
+    call foo(i)
+  end do
+  !$omp end distribute simd
+
+  !$omp distribute simd
+  do i = 1, 10
+    call foo(i)
+  end do
+  !$omp end distribute simd
+  !$omp end target teams
+end subroutine distribute_simd_generic
+
+! CHECK-LABEL: func.func @_QPdistribute_simd_spmd() {
+subroutine distribute_simd_spmd()
+  ! CHECK: omp.target
+  ! CHECK-SAME: host_eval({{.*}})
+  !$omp target
+  !$omp teams
+  !$omp distribute simd
+  do i = 1, 10
+    call foo(i)
+  end do
+  !$omp end distribute simd
+  !$omp end teams
+  !$omp end target
+
+  ! CHECK: omp.target
+  ! CHECK-SAME: host_eval({{.*}})
+  !$omp target teams
+  !$omp distribute simd
+  do i = 1, 10
+    call foo(i)
+  end do
+  !$omp end distribute simd
+  !$omp end target teams
+end subroutine distribute_simd_spmd
+
+! CHECK-LABEL: func.func @_QPteams_distribute_spmd() {
+subroutine teams_distribute_spmd()
+  ! CHECK: omp.target
+  ! CHECK-SAME: host_eval({{.*}})
+  !$omp target
+  !$omp teams distribute
+  do i = 1, 10
+    call foo(i)
+  end do
+  !$omp end teams distribute
+  !$omp end target
+end subroutine teams_distribute_spmd
+
+! CHECK-LABEL: func.func @_QPteams_distribute_simd_spmd() {
+subroutine teams_distribute_simd_spmd()
+  ! CHECK: omp.target
+  ! CHECK-SAME: host_eval({{.*}})
+  !$omp target
+  !$omp teams distribute simd
+  do i = 1, 10
+    call foo(i)
+  end do
+  !$omp end teams distribute simd
+  !$omp end target
+end subroutine teams_distribute_simd_spmd
+
+! CHECK-LABEL: func.func @_QPtarget_teams_distribute_spmd() {
+subroutine target_teams_distribute_spmd()
+  ! CHECK: omp.target
+  ! CHECK-SAME: host_eval({{.*}})
+  !$omp target teams distribute
+  do i = 1, 10
+    call foo(i)
+  end do
+  !$omp end target teams distribute
+end subroutine target_teams_distribute_spmd
+
+! CHECK-LABEL: func.func @_QPtarget_teams_distribute_simd_spmd() {
+subroutine target_teams_distribute_simd_spmd()
+  ! CHECK: omp.target
+  ! CHECK-SAME: host_eval({{.*}})
+  !$omp target teams distribute simd
+  do i = 1, 10
+    call foo(i)
+  end do
+  !$omp end target teams distribute simd
+end subroutine target_teams_distribute_simd_spmd



More information about the flang-commits mailing list