Skip to content

Commit 37fcad7

Browse files
authored
[flang][openacc] Fix upperbound computation when lowerbound is default (#68160)
The upperbound is computed from the extent. When the lowerbound is 1 (the default), the upperbounds is equal to `extent - 1`.
1 parent 19991d0 commit 37fcad7

File tree

2 files changed

+17
-20
lines changed

2 files changed

+17
-20
lines changed

flang/lib/Lower/DirectivesCommon.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -799,13 +799,18 @@ genBoundsOps(fir::FirOpBuilder &builder, mlir::Location loc,
799799
}
800800
}
801801
}
802-
// ub = baseLb + extent - 1
803802
if (!ubound) {
804803
mlir::Value ext =
805804
fir::factory::readExtent(builder, loc, dataExv, dimension);
806-
mlir::Value lbExt =
807-
builder.create<mlir::arith::AddIOp>(loc, ext, baseLb);
808-
ubound = builder.create<mlir::arith::SubIOp>(loc, lbExt, one);
805+
if (defaultLb) {
806+
// ub = extent - 1
807+
ubound = builder.create<mlir::arith::SubIOp>(loc, ext, one);
808+
} else {
809+
// ub = baseLb + extent - 1
810+
mlir::Value lbExt =
811+
builder.create<mlir::arith::AddIOp>(loc, ext, baseLb);
812+
ubound = builder.create<mlir::arith::SubIOp>(loc, lbExt, one);
813+
}
809814
}
810815
mlir::Value bound = builder.create<BoundsOp>(
811816
loc, boundTy, lbound, ubound, extent, stride, strideInBytes, baseLb);

flang/test/Lower/OpenACC/acc-enter-data.f90

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,7 @@ subroutine acc_enter_data
178178
!$acc enter data copyin(a(1:,1:5))
179179
!CHECK: %[[ONE:.*]] = arith.constant 1 : index
180180
!CHECK: %[[LB1:.*]] = arith.constant 0 : index
181-
!CHECK: %[[LBEXT:.*]] = arith.addi %c10{{.*}}, %[[ONE]] : index
182-
!CHECK: %[[UB1:.*]] = arith.subi %[[LBEXT]], %[[ONE]] : index
181+
!CHECK: %[[UB1:.*]] = arith.subi %c10{{.*}}, %[[ONE]] : index
183182
!CHECK: %[[BOUND1:.*]] = acc.bounds lowerbound(%[[LB1]] : index) upperbound(%[[UB1]] : index) stride(%[[ONE]] : index) startIdx(%c1{{.*}} : index)
184183
!CHECK: %[[LB2:.*]] = arith.constant 0 : index
185184
!CHECK: %[[UB2:.*]] = arith.constant 4 : index
@@ -203,11 +202,9 @@ subroutine acc_enter_data
203202
!$acc enter data copyin(a(:,:))
204203
!CHECK: %[[LB:.*]] = arith.constant 0 : index
205204
!CHECK: %[[ONE:.*]] = arith.constant 1 : index
206-
!CHECK: %[[LBEXT:.*]] = arith.addi %c10{{.*}}, %[[ONE]] : index
207-
!CHECK: %[[UB:.*]] = arith.subi %[[LBEXT]], %[[ONE]] : index
205+
!CHECK: %[[UB:.*]] = arith.subi %c10{{.*}}, %[[ONE]] : index
208206
!CHECK: %[[BOUND1:.*]] = acc.bounds lowerbound(%[[LB]] : index) upperbound(%[[UB]] : index) stride(%[[ONE]] : index) startIdx(%[[ONE]] : index)
209-
!CHECK: %[[LBEXT:.*]] = arith.addi %c10{{.*}}, %[[ONE]] : index
210-
!CHECK: %[[UB:.*]] = arith.subi %[[LBEXT]], %[[ONE]] : index
207+
!CHECK: %[[UB:.*]] = arith.subi %{{.*}}, %[[ONE]] : index
211208
!CHECK: %[[BOUND2:.*]] = acc.bounds lowerbound(%[[LB]] : index) upperbound(%[[UB]] : index) stride(%[[ONE]] : index) startIdx(%[[ONE]] : index)
212209
!FIR: %[[COPYIN_A:.*]] = acc.copyin varPtr(%[[A]] : !fir.ref<!fir.array<10x10xf32>>) bounds(%[[BOUND1]], %[[BOUND2]]) -> !fir.ref<!fir.array<10x10xf32>> {name = "a(:,:)", structured = false}
213210
!HLFIR: %[[COPYIN_A:.*]] = acc.copyin varPtr(%[[DECLA]]#1 : !fir.ref<!fir.array<10x10xf32>>) bounds(%[[BOUND1]], %[[BOUND2]]) -> !fir.ref<!fir.array<10x10xf32>> {name = "a(:,:)", structured = false}
@@ -402,8 +399,7 @@ subroutine acc_enter_data_assumed(a, b, n, m)
402399
!CHECK: %[[C0:.*]] = arith.constant 0 : index
403400
!FIR: %[[DIMS1:.*]]:3 = fir.box_dims %[[A]], %[[C0]] : (!fir.box<!fir.array<?xf32>>, index) -> (index, index, index)
404401
!HLFIR: %[[DIMS1:.*]]:3 = fir.box_dims %[[DECLA]]#1, %[[C0]] : (!fir.box<!fir.array<?xf32>>, index) -> (index, index, index)
405-
!CHECK: %[[LBEXT:.*]] = arith.addi %[[DIMS1]]#1, %[[ONE]] : index
406-
!CHECK: %[[UB:.*]] = arith.subi %[[LBEXT]], %[[ONE]] : index
402+
!CHECK: %[[UB:.*]] = arith.subi %[[DIMS1]]#1, %[[ONE]] : index
407403
!CHECK: %[[BOUND:.*]] = acc.bounds lowerbound(%[[LB]] : index) upperbound(%[[UB]] : index) stride(%[[DIMS0]]#2 : index) startIdx(%[[ONE]] : index) {strideInBytes = true}
408404
!FIR: %[[BOX_ADDR:.*]] = fir.box_addr %[[A]] : (!fir.box<!fir.array<?xf32>>) -> !fir.ref<!fir.array<?xf32>>
409405
!HLFIR: %[[BOX_ADDR:.*]] = fir.box_addr %[[DECLA]]#1 : (!fir.box<!fir.array<?xf32>>) -> !fir.ref<!fir.array<?xf32>>
@@ -419,8 +415,7 @@ subroutine acc_enter_data_assumed(a, b, n, m)
419415
!CHECK: %[[C0:.*]] = arith.constant 0 : index
420416
!FIR: %[[DIMS1:.*]]:3 = fir.box_dims %[[A]], %[[C0]] : (!fir.box<!fir.array<?xf32>>, index) -> (index, index, index)
421417
!HLFIR: %[[DIMS1:.*]]:3 = fir.box_dims %[[DECLA]]#1, %[[C0]] : (!fir.box<!fir.array<?xf32>>, index) -> (index, index, index)
422-
!CHECK: %[[LBEXT:.*]] = arith.addi %[[DIMS1]]#1, %[[ONE]] : index
423-
!CHECK: %[[UB:.*]] = arith.subi %[[LBEXT]], %[[ONE]] : index
418+
!CHECK: %[[UB:.*]] = arith.subi %[[DIMS1]]#1, %[[ONE]] : index
424419
!CHECK: %[[BOUND:.*]] = acc.bounds lowerbound(%[[LB]] : index) upperbound(%[[UB]] : index) stride(%[[DIMS0]]#2 : index) startIdx(%[[ONE]] : index) {strideInBytes = true}
425420
!FIR: %[[BOX_ADDR:.*]] = fir.box_addr %[[A]] : (!fir.box<!fir.array<?xf32>>) -> !fir.ref<!fir.array<?xf32>>
426421
!HLFIR: %[[BOX_ADDR:.*]] = fir.box_addr %[[DECLA]]#1 : (!fir.box<!fir.array<?xf32>>) -> !fir.ref<!fir.array<?xf32>>
@@ -465,8 +460,7 @@ subroutine acc_enter_data_assumed(a, b, n, m)
465460
!CHECK: %[[C0:.*]] = arith.constant 0 : index
466461
!FIR: %[[DIMS:.*]]:3 = fir.box_dims %[[A]], %[[C0]] : (!fir.box<!fir.array<?xf32>>, index) -> (index, index, index)
467462
!HLFIR: %[[DIMS:.*]]:3 = fir.box_dims %[[DECLA]]#1, %[[C0]] : (!fir.box<!fir.array<?xf32>>, index) -> (index, index, index)
468-
!CHECK: %[[LBEXT:.*]] = arith.addi %[[DIMS]]#1, %[[ONE]] : index
469-
!CHECK: %[[UB:.*]] = arith.subi %[[LBEXT]], %[[ONE]] : index
463+
!CHECK: %[[UB:.*]] = arith.subi %[[DIMS]]#1, %[[ONE]] : index
470464
!CHECK: %[[BOUND:.*]] = acc.bounds lowerbound(%[[LB]] : index) upperbound(%[[UB]] : index) stride(%[[DIMS0]]#2 : index) startIdx(%[[ONE]] : index) {strideInBytes = true}
471465
!FIR: %[[BOX_ADDR:.*]] = fir.box_addr %[[A]] : (!fir.box<!fir.array<?xf32>>) -> !fir.ref<!fir.array<?xf32>>
472466
!HLFIR: %[[BOX_ADDR:.*]] = fir.box_addr %[[DECLA]]#1 : (!fir.box<!fir.array<?xf32>>) -> !fir.ref<!fir.array<?xf32>>
@@ -720,8 +714,7 @@ subroutine acc_enter_data_derived_type()
720714
!HLFIR: %[[ARRAY_COORD:.*]] = hlfir.designate %[[DECLA]]#0{"array"} shape %{{.*}} : (!fir.ref<!fir.type<_QFacc_enter_data_derived_typeTdt{data:f32,array:!fir.array<10xf32>}>>, !fir.shape<1>) -> !fir.ref<!fir.array<10xf32>>
721715
!CHECK: %[[LB:.*]] = arith.constant 0 : index
722716
!CHECK: %[[C1:.*]] = arith.constant 1 : index
723-
!CHECK: %[[LBEXT:.*]] = arith.addi %[[C10]], %[[C1]] : index
724-
!CHECK: %[[UB:.*]] = arith.subi %[[LBEXT]], %[[C1]] : index
717+
!CHECK: %[[UB:.*]] = arith.subi %[[C10]], %[[C1]] : index
725718
!CHECK: %[[BOUND:.*]] = acc.bounds lowerbound(%[[LB]] : index) upperbound(%[[UB]] : index) stride(%[[C1]] : index) startIdx(%[[C1]] : index)
726719
!CHECK: %[[CREATE:.*]] = acc.create varPtr(%[[ARRAY_COORD]] : !fir.ref<!fir.array<10xf32>>) bounds(%[[BOUND]]) -> !fir.ref<!fir.array<10xf32>> {name = "a%array(:)", structured = false}
727720
!CHECK: acc.enter_data dataOperands(%[[CREATE]] : !fir.ref<!fir.array<10xf32>>)
@@ -755,8 +748,7 @@ subroutine acc_enter_data_derived_type()
755748
!HLFIR: %[[ARRAY_COORD:.*]] = hlfir.designate %[[DECLA]]#0{"array"} shape %{{.*}} : (!fir.ref<!fir.type<_QFacc_enter_data_derived_typeTdt{data:f32,array:!fir.array<10xf32>}>>, !fir.shape<1>) -> !fir.ref<!fir.array<10xf32>>
756749
!CHECK: %[[ONE:.*]] = arith.constant 1 : index
757750
!CHECK: %[[LB:.*]] = arith.constant 1 : index
758-
!CHECK: %[[LBEXT:.*]] = arith.addi %[[C10]], %[[ONE]] : index
759-
!CHECK: %[[UB:.*]] = arith.subi %[[LBEXT]], %[[ONE]] : index
751+
!CHECK: %[[UB:.*]] = arith.subi %[[C10]], %[[ONE]] : index
760752
!CHECK: %[[BOUND:.*]] = acc.bounds lowerbound(%[[LB]] : index) upperbound(%[[UB]] : index) stride(%[[ONE]] : index) startIdx(%[[ONE]] : index)
761753
!CHECK: %[[CREATE:.*]] = acc.create varPtr(%[[ARRAY_COORD]] : !fir.ref<!fir.array<10xf32>>) bounds(%[[BOUND]]) -> !fir.ref<!fir.array<10xf32>> {name = "a%array(2:)", structured = false}
762754
!CHECK: acc.enter_data dataOperands(%[[CREATE]] : !fir.ref<!fir.array<10xf32>>)

0 commit comments

Comments
 (0)