Skip to content

[flang][openacc] Set extent to 0 when it is undefined #71108

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 2, 2023

Conversation

clementval
Copy link
Contributor

Set upperbound to lowerbound value and extent to zero when extent is undefined.

@llvmbot llvmbot added flang Flang issues not falling into any other category flang:fir-hlfir openacc labels Nov 2, 2023
@llvmbot
Copy link
Member

llvmbot commented Nov 2, 2023

@llvm/pr-subscribers-openacc

@llvm/pr-subscribers-flang-fir-hlfir

Author: Valentin Clement (バレンタイン クレメン) (clementval)

Changes

Set upperbound to lowerbound value and extent to zero when extent is undefined.


Full diff: https://github.com/llvm/llvm-project/pull/71108.diff

2 Files Affected:

  • (modified) flang/lib/Lower/DirectivesCommon.h (+11-4)
  • (modified) flang/test/Lower/OpenACC/acc-bounds.f90 (+16)
diff --git a/flang/lib/Lower/DirectivesCommon.h b/flang/lib/Lower/DirectivesCommon.h
index 86787961382441f..2ea4f53e94081f6 100644
--- a/flang/lib/Lower/DirectivesCommon.h
+++ b/flang/lib/Lower/DirectivesCommon.h
@@ -632,11 +632,18 @@ genBaseBoundsOps(fir::FirOpBuilder &builder, mlir::Location loc,
   for (std::size_t dim = 0; dim < dataExv.rank(); ++dim) {
     mlir::Value baseLb =
         fir::factory::readLowerBound(builder, loc, dataExv, dim, one);
+    mlir::Value zero = builder.createIntegerConstant(loc, idxTy, 0);
+    mlir::Value ub;
+    mlir::Value lb = zero;
     mlir::Value ext = fir::factory::readExtent(builder, loc, dataExv, dim);
-    mlir::Value lb = builder.createIntegerConstant(loc, idxTy, 0);
+    if (mlir::isa<fir::UndefOp>(ext.getDefiningOp())) {
+      ext = zero;
+      ub = lb;
+    } else {
+      // ub = extent - 1
+      ub = builder.create<mlir::arith::SubIOp>(loc, ext, one);
+    }
 
-    // ub = extent - 1
-    mlir::Value ub = builder.create<mlir::arith::SubIOp>(loc, ext, one);
     mlir::Value bound =
         builder.create<BoundsOp>(loc, boundTy, lb, ub, ext, one, false, baseLb);
     bounds.push_back(bound);
@@ -738,7 +745,7 @@ genBoundsOps(fir::FirOpBuilder &builder, mlir::Location loc,
           const auto &strideExpr{std::get<2>(triplet->t)};
           if (strideExpr) {
             mlir::emitError(loc, "stride cannot be specified on "
-                                 "an OpenMP array section");
+                                 "an array section");
             break;
           }
         }
diff --git a/flang/test/Lower/OpenACC/acc-bounds.f90 b/flang/test/Lower/OpenACC/acc-bounds.f90
index c63c9aacf5c2c16..86fb4d8c5284e45 100644
--- a/flang/test/Lower/OpenACC/acc-bounds.f90
+++ b/flang/test/Lower/OpenACC/acc-bounds.f90
@@ -86,4 +86,20 @@ subroutine acc_derived_type_component_allocatable_array()
 ! CHECK: return
 ! CHECK: }
 
+  subroutine acc_undefined_extent(a)
+    real, dimension(1:*) :: a
+
+    !$acc kernels present(a)
+    !$acc end kernels
+  end subroutine
+! CHECK-LABEL: func.func @_QMopenacc_boundsPacc_undefined_extent(
+! CHECK-SAME: %[[ARG0:.*]]: !fir.ref<!fir.array<?xf32>> {fir.bindc_name = "a"}) {
+! HLFIR: %[[DECL_ARG0:.*]]:2 = hlfir.declare %[[ARG0]](%{{.*}}) {uniq_name = "_QMopenacc_boundsFacc_undefined_extentEa"} : (!fir.ref<!fir.array<?xf32>>, !fir.shape<1>) -> (!fir.box<!fir.array<?xf32>>, !fir.ref<!fir.array<?xf32>>)
+! CHECK: %[[ONE:.*]] = arith.constant 1 : index
+! CHECK: %[[ZERO:.*]] = arith.constant 0 : index
+! CHECK: %[[BOUND:.*]] = acc.bounds lowerbound(%[[ZERO]] : index) upperbound(%[[ZERO]] : index) extent(%[[ZERO]] : index) stride(%[[ONE]] : index) startIdx(%[[ONE]] : index)
+! FIR:   %[[PRESENT:.*]] = acc.present varPtr(%[[ARG0]] : !fir.ref<!fir.array<?xf32>>) bounds(%[[BOUND]]) -> !fir.ref<!fir.array<?xf32>> {name = "a"}
+! HLFIR: %[[PRESENT:.*]] = acc.present varPtr(%[[DECL_ARG0]]#1 : !fir.ref<!fir.array<?xf32>>) bounds(%[[BOUND]]) -> !fir.ref<!fir.array<?xf32>> {name = "a"}
+! CHECK: acc.kernels dataOperands(%[[PRESENT]] : !fir.ref<!fir.array<?xf32>>)
+
 end module

Copy link
Contributor

@vzakhari vzakhari left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@clementval clementval merged commit 4b57d03 into llvm:main Nov 2, 2023
@clementval clementval deleted the acc_bound_undefined_extent branch November 2, 2023 22:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:fir-hlfir flang Flang issues not falling into any other category openacc
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants