Skip to content

Commit 4b57d03

Browse files
authored
[flang][openacc] Set extent to 0 when it is undefined (llvm#71108)
Set upperbound to lowerbound value and extent to zero when extent is undefined.
1 parent 53ffafb commit 4b57d03

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

flang/lib/Lower/DirectivesCommon.h

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -632,11 +632,18 @@ genBaseBoundsOps(fir::FirOpBuilder &builder, mlir::Location loc,
632632
for (std::size_t dim = 0; dim < dataExv.rank(); ++dim) {
633633
mlir::Value baseLb =
634634
fir::factory::readLowerBound(builder, loc, dataExv, dim, one);
635+
mlir::Value zero = builder.createIntegerConstant(loc, idxTy, 0);
636+
mlir::Value ub;
637+
mlir::Value lb = zero;
635638
mlir::Value ext = fir::factory::readExtent(builder, loc, dataExv, dim);
636-
mlir::Value lb = builder.createIntegerConstant(loc, idxTy, 0);
639+
if (mlir::isa<fir::UndefOp>(ext.getDefiningOp())) {
640+
ext = zero;
641+
ub = lb;
642+
} else {
643+
// ub = extent - 1
644+
ub = builder.create<mlir::arith::SubIOp>(loc, ext, one);
645+
}
637646

638-
// ub = extent - 1
639-
mlir::Value ub = builder.create<mlir::arith::SubIOp>(loc, ext, one);
640647
mlir::Value bound =
641648
builder.create<BoundsOp>(loc, boundTy, lb, ub, ext, one, false, baseLb);
642649
bounds.push_back(bound);
@@ -738,7 +745,7 @@ genBoundsOps(fir::FirOpBuilder &builder, mlir::Location loc,
738745
const auto &strideExpr{std::get<2>(triplet->t)};
739746
if (strideExpr) {
740747
mlir::emitError(loc, "stride cannot be specified on "
741-
"an OpenMP array section");
748+
"an array section");
742749
break;
743750
}
744751
}

flang/test/Lower/OpenACC/acc-bounds.f90

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,20 @@ subroutine acc_derived_type_component_allocatable_array()
8686
! CHECK: return
8787
! CHECK: }
8888

89+
subroutine acc_undefined_extent(a)
90+
real, dimension(1:*) :: a
91+
92+
!$acc kernels present(a)
93+
!$acc end kernels
94+
end subroutine
95+
! CHECK-LABEL: func.func @_QMopenacc_boundsPacc_undefined_extent(
96+
! CHECK-SAME: %[[ARG0:.*]]: !fir.ref<!fir.array<?xf32>> {fir.bindc_name = "a"}) {
97+
! 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>>)
98+
! CHECK: %[[ONE:.*]] = arith.constant 1 : index
99+
! CHECK: %[[ZERO:.*]] = arith.constant 0 : index
100+
! CHECK: %[[BOUND:.*]] = acc.bounds lowerbound(%[[ZERO]] : index) upperbound(%[[ZERO]] : index) extent(%[[ZERO]] : index) stride(%[[ONE]] : index) startIdx(%[[ONE]] : index)
101+
! FIR: %[[PRESENT:.*]] = acc.present varPtr(%[[ARG0]] : !fir.ref<!fir.array<?xf32>>) bounds(%[[BOUND]]) -> !fir.ref<!fir.array<?xf32>> {name = "a"}
102+
! HLFIR: %[[PRESENT:.*]] = acc.present varPtr(%[[DECL_ARG0]]#1 : !fir.ref<!fir.array<?xf32>>) bounds(%[[BOUND]]) -> !fir.ref<!fir.array<?xf32>> {name = "a"}
103+
! CHECK: acc.kernels dataOperands(%[[PRESENT]] : !fir.ref<!fir.array<?xf32>>)
104+
89105
end module

0 commit comments

Comments
 (0)