[flang] [llvm] [OpenMP][Flang] Fix dynamic-extent array mapping (PR #107247)
Akash Banerjee via llvm-commits
llvm-commits at lists.llvm.org
Wed Sep 4 08:04:27 PDT 2024
https://github.com/TIFitis updated https://github.com/llvm/llvm-project/pull/107247
>From ddd27b85355b83b072d517d000ac6c743658daef Mon Sep 17 00:00:00 2001
From: Akash Banerjee <Akash.Banerjee at amd.com>
Date: Wed, 4 Sep 2024 15:31:39 +0100
Subject: [PATCH 1/3] [OpenMP][Flang] Fix dynamic-extent array mapping
This patch fixes the mapping and lowering of arrays with dynamic extents and adds a new test for the same.
The fix discards the incomplete the dynamic extent information and replacing it with just the base type. When lowering to llvm later, the bounds information is used instead.
---
flang/lib/Lower/OpenMP/Utils.cpp | 7 ++++
.../offloading/fortran/target-map-dynamic.f90 | 34 +++++++++++++++++++
2 files changed, 41 insertions(+)
create mode 100644 offload/test/offloading/fortran/target-map-dynamic.f90
diff --git a/flang/lib/Lower/OpenMP/Utils.cpp b/flang/lib/Lower/OpenMP/Utils.cpp
index bbf08961bee66a..8073b24a1d5b45 100644
--- a/flang/lib/Lower/OpenMP/Utils.cpp
+++ b/flang/lib/Lower/OpenMP/Utils.cpp
@@ -132,6 +132,13 @@ createMapInfoOp(fir::FirOpBuilder &builder, mlir::Location loc,
mlir::TypeAttr varType = mlir::TypeAttr::get(
llvm::cast<mlir::omp::PointerLikeType>(retTy).getElementType());
+ // For types with unknown extents such as <2x?xi32> we discard the incomplete
+ // type info and only retain the base type. The correct dimensions are later
+ // recovered through the bounds info.
+ if (auto seqType = llvm::dyn_cast<fir::SequenceType>(varType.getValue()))
+ if (seqType.hasDynamicExtents())
+ varType = mlir::TypeAttr::get(seqType.getEleTy());
+
mlir::omp::MapInfoOp op = builder.create<mlir::omp::MapInfoOp>(
loc, retTy, baseAddr, varType, varPtrPtr, members, membersIndex, bounds,
builder.getIntegerAttr(builder.getIntegerType(64, false), mapType),
diff --git a/offload/test/offloading/fortran/target-map-dynamic.f90 b/offload/test/offloading/fortran/target-map-dynamic.f90
new file mode 100644
index 00000000000000..cb082393dcc97a
--- /dev/null
+++ b/offload/test/offloading/fortran/target-map-dynamic.f90
@@ -0,0 +1,34 @@
+! Offloading test checking interaction of allocatables
+! with enter, exit and target
+! REQUIRES: flang, amdgpu
+
+! RUN: %libomptarget-compile-fortran-run-and-check-generic
+
+subroutine test_array_target_enter_data(dims)
+ integer, intent(in) :: dims(2)
+ double precision :: A(2, dims(2))
+ !$omp target enter data map(to: U)
+
+ A(2,2) = 1.0
+ !$omp target
+ A(1,1) = 10
+ A(2,1) = 20
+ A(1,2) = 30
+ A(2,2) = 40
+ !$omp end target
+
+ !$omp target exit data map(from: A)
+
+ print *, A
+end subroutine test_array_target_enter_data
+
+program main
+ integer :: dimensions(2)
+ dimensions(1) = 1
+ dimensions(2) = 2
+
+call test_array_target_enter_data(dimensions)
+end program
+
+
+! CHECK: 10. 20. 30. 40.
>From 1c3ea6daf7805096f68fbdbf2eb5d01eb434e21c Mon Sep 17 00:00:00 2001
From: Akash Banerjee <Akash.Banerjee at amd.com>
Date: Wed, 4 Sep 2024 15:38:44 +0100
Subject: [PATCH 2/3] Fix test description.
---
offload/test/offloading/fortran/target-map-dynamic.f90 | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/offload/test/offloading/fortran/target-map-dynamic.f90 b/offload/test/offloading/fortran/target-map-dynamic.f90
index cb082393dcc97a..8bf6980884a187 100644
--- a/offload/test/offloading/fortran/target-map-dynamic.f90
+++ b/offload/test/offloading/fortran/target-map-dynamic.f90
@@ -1,5 +1,4 @@
-! Offloading test checking interaction of allocatables
-! with enter, exit and target
+! Offloading test checking lowering of arrays with dynamic extents.
! REQUIRES: flang, amdgpu
! RUN: %libomptarget-compile-fortran-run-and-check-generic
>From 5764c74eb21304eb4955d744f068b8a25788c4b0 Mon Sep 17 00:00:00 2001
From: Akash Banerjee <Akash.Banerjee at amd.com>
Date: Wed, 4 Sep 2024 16:03:53 +0100
Subject: [PATCH 3/3] Update Flang frontend tests.
---
flang/test/Lower/OpenMP/array-bounds.f90 | 2 +-
flang/test/Lower/OpenMP/target.f90 | 2 +-
2 files changed, 2 insertions(+), 2 deletions(-)
diff --git a/flang/test/Lower/OpenMP/array-bounds.f90 b/flang/test/Lower/OpenMP/array-bounds.f90
index 9e59d71f560fc8..09498ca6cdde99 100644
--- a/flang/test/Lower/OpenMP/array-bounds.f90
+++ b/flang/test/Lower/OpenMP/array-bounds.f90
@@ -74,7 +74,7 @@ end subroutine assumed_shape_array
!HOST: %[[C4_1:.*]] = arith.subi %c4, %c1{{.*}} : index
!HOST: %[[EXT:.*]] = arith.addi %[[C4_1]], %c1{{.*}} : index
!HOST: %[[BOUNDS:.*]] = omp.map.bounds lower_bound(%c1{{.*}} : index) upper_bound(%c4{{.*}} : index) extent(%[[EXT]] : index) stride(%[[DIMS0]]#2 : index) start_idx(%c1{{.*}} : index) {stride_in_bytes = true}
-!HOST: %[[MAP:.*]] = omp.map.info var_ptr(%[[ARG0_DECL]]#1 : !fir.ref<!fir.array<?xi32>>, !fir.array<?xi32>) map_clauses(tofrom) capture(ByRef) bounds(%[[BOUNDS]]) -> !fir.ref<!fir.array<?xi32>> {name = "arr_read_write(2:5)"}
+!HOST: %[[MAP:.*]] = omp.map.info var_ptr(%[[ARG0_DECL]]#1 : !fir.ref<!fir.array<?xi32>>, i32) map_clauses(tofrom) capture(ByRef) bounds(%[[BOUNDS]]) -> !fir.ref<!fir.array<?xi32>> {name = "arr_read_write(2:5)"}
!HOST: omp.target map_entries(%[[MAP]] -> %{{.*}}, {{.*}} -> {{.*}} : !fir.ref<!fir.array<?xi32>>, !fir.ref<i32>) {
subroutine assumed_size_array(arr_read_write)
integer, intent(inout) :: arr_read_write(*)
diff --git a/flang/test/Lower/OpenMP/target.f90 b/flang/test/Lower/OpenMP/target.f90
index e86a2f9b6098d2..6fccea7e37072a 100644
--- a/flang/test/Lower/OpenMP/target.f90
+++ b/flang/test/Lower/OpenMP/target.f90
@@ -460,7 +460,7 @@ subroutine omp_target_implicit_bounds(n)
integer :: n
integer :: a(n)
!CHECK: %[[VAL_14:.*]] = omp.map.bounds lower_bound(%c0{{.*}} : index) upper_bound(%[[UB]] : index) extent(%[[VAL_7]] : index) stride(%c1{{.*}} : index) start_idx(%c1{{.*}} : index)
- !CHECK: %[[VAL_15:.*]] = omp.map.info var_ptr(%[[VAL_10]]#1 : !fir.ref<!fir.array<?xi32>>, !fir.array<?xi32>) map_clauses(implicit, tofrom) capture(ByRef) bounds(%[[VAL_14]]) -> !fir.ref<!fir.array<?xi32>> {name = "a"}
+ !CHECK: %[[VAL_15:.*]] = omp.map.info var_ptr(%[[VAL_10]]#1 : !fir.ref<!fir.array<?xi32>>, i32) map_clauses(implicit, tofrom) capture(ByRef) bounds(%[[VAL_14]]) -> !fir.ref<!fir.array<?xi32>> {name = "a"}
!CHECK: %[[VAL_16:.*]] = omp.map.info var_ptr(%[[VAL_COPY]] : !fir.ref<i32>, i32) map_clauses(implicit, exit_release_or_enter_alloc) capture(ByCopy) -> !fir.ref<i32> {name = ""}
!CHECK: omp.target map_entries(%[[VAL_15]] -> %[[VAL_17:.*]], %[[VAL_16]] -> %[[VAL_18:.*]] : !fir.ref<!fir.array<?xi32>>, !fir.ref<i32>) {
!CHECK: ^bb0(%[[VAL_17]]: !fir.ref<!fir.array<?xi32>>, %[[VAL_18]]: !fir.ref<i32>):
More information about the llvm-commits
mailing list