Skip to content

Commit 6b780af

Browse files
authored
[flang] canonicalize shape in pointer remapping lowering (#157472)
Fix for #153221. Canonicalize the new shape of the pointer when lowering pointer assignment with bounds remapping. This is done by using the existing helper that generates a compare to zero + select like in the other situation where shapes are lowered. Note that this only needs to be done for the extents here because lower bounds are canonicalized in LBOUND inquiries and descriptor creation (embox/rebox codegen) based on the extent value.
1 parent 5ec99c7 commit 6b780af

File tree

4 files changed

+36
-18
lines changed

4 files changed

+36
-18
lines changed

flang/lib/Optimizer/Builder/MutableBox.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -603,21 +603,23 @@ void fir::factory::associateMutableBoxWithRemap(
603603
mlir::ValueRange lbounds, mlir::ValueRange ubounds) {
604604
// Compute new extents
605605
llvm::SmallVector<mlir::Value> extents;
606-
auto idxTy = builder.getIndexType();
606+
mlir::Type idxTy = builder.getIndexType();
607+
mlir::Value zero = builder.createIntegerConstant(loc, idxTy, 0);
607608
if (!lbounds.empty()) {
608609
auto one = builder.createIntegerConstant(loc, idxTy, 1);
609610
for (auto [lb, ub] : llvm::zip(lbounds, ubounds)) {
610-
auto lbi = builder.createConvert(loc, idxTy, lb);
611-
auto ubi = builder.createConvert(loc, idxTy, ub);
612-
auto diff = mlir::arith::SubIOp::create(builder, loc, idxTy, ubi, lbi);
611+
612+
mlir::Value lbi = builder.createConvert(loc, idxTy, lb);
613+
mlir::Value ubi = builder.createConvert(loc, idxTy, ub);
613614
extents.emplace_back(
614-
mlir::arith::AddIOp::create(builder, loc, idxTy, diff, one));
615+
fir::factory::computeExtent(builder, loc, lbi, ubi, zero, one));
615616
}
616617
} else {
617618
// lbounds are default. Upper bounds and extents are the same.
618-
for (auto ub : ubounds) {
619-
auto cast = builder.createConvert(loc, idxTy, ub);
620-
extents.emplace_back(cast);
619+
for (mlir::Value ub : ubounds) {
620+
mlir::Value cast = builder.createConvert(loc, idxTy, ub);
621+
extents.emplace_back(
622+
fir::factory::genMaxWithZero(builder, loc, cast, zero));
621623
}
622624
}
623625
const auto newRank = extents.size();

flang/test/Lower/HLFIR/allocatable-and-pointer-status-change.f90

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,16 @@ subroutine pointer_remapping(p, ziel)
5858
! CHECK: %[[VAL_14:.*]] = fir.convert %[[VAL_9]] : (i64) -> index
5959
! CHECK: %[[VAL_15:.*]] = arith.subi %[[VAL_14]], %[[VAL_13]] : index
6060
! CHECK: %[[VAL_16:.*]] = arith.addi %[[VAL_15]], %[[VAL_12]] : index
61+
! CHECK: %[[cmp0:.*]] = arith.cmpi sgt, %[[VAL_16]], %c0{{.*}} : index
62+
! CHECK: %[[ext0:.*]] = arith.select %[[cmp0]], %[[VAL_16]], %c0{{.*}} : index
6163
! CHECK: %[[VAL_17:.*]] = fir.convert %[[VAL_10]] : (i64) -> index
6264
! CHECK: %[[VAL_18:.*]] = fir.convert %[[VAL_11]] : (i64) -> index
6365
! CHECK: %[[VAL_19:.*]] = arith.subi %[[VAL_18]], %[[VAL_17]] : index
6466
! CHECK: %[[VAL_20:.*]] = arith.addi %[[VAL_19]], %[[VAL_12]] : index
67+
! CHECK: %[[cmp1:.*]] = arith.cmpi sgt, %[[VAL_20]], %c0{{.*}} : index
68+
! CHECK: %[[ext1:.*]] = arith.select %[[cmp1]], %[[VAL_20]], %c0{{.*}} : index
6569
! CHECK: %[[VAL_21:.*]] = fir.convert %[[VAL_7]]#0 : (!fir.ref<!fir.array<10x20x30xf32>>) -> !fir.ref<!fir.array<?x?xf32>>
66-
! CHECK: %[[VAL_22:.*]] = fir.shape_shift %[[VAL_8]], %[[VAL_16]], %[[VAL_10]], %[[VAL_20]] : (i64, index, i64, index) -> !fir.shapeshift<2>
70+
! CHECK: %[[VAL_22:.*]] = fir.shape_shift %[[VAL_8]], %[[ext0]], %[[VAL_10]], %[[ext1]] : (i64, index, i64, index) -> !fir.shapeshift<2>
6771
! CHECK: %[[VAL_23:.*]] = fir.embox %[[VAL_21]](%[[VAL_22]]) : (!fir.ref<!fir.array<?x?xf32>>, !fir.shapeshift<2>) -> !fir.box<!fir.ptr<!fir.array<?x?xf32>>>
6872
! CHECK: fir.store %[[VAL_23]] to %[[VAL_2]]#0 : !fir.ref<!fir.box<!fir.ptr<!fir.array<?x?xf32>>>>
6973
end subroutine

flang/test/Lower/HLFIR/issue80884.f90

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ subroutine issue80884(p, targ)
2626
! CHECK: %[[VAL_13:.*]] = fir.convert %[[VAL_5]] : (i64) -> index
2727
! CHECK: %[[VAL_14:.*]] = arith.subi %[[VAL_13]], %[[VAL_12]] : index
2828
! CHECK: %[[VAL_15:.*]] = arith.addi %[[VAL_14]], %[[VAL_11]] : index
29+
! CHECK: %[[cmp0:.*]] = arith.cmpi sgt, %[[VAL_15]], %c0{{.*}} : index
30+
! CHECK: %[[ext0:.*]] = arith.select %[[cmp0]], %[[VAL_15]], %c0{{.*}} : index
2931
! CHECK: %[[VAL_16:.*]] = fir.convert %[[VAL_10]] : (!fir.ref<!fir.array<10x10xf32>>) -> !fir.ref<!fir.array<?xf32>>
30-
! CHECK: %[[VAL_17:.*]] = fir.shape_shift %[[VAL_4]], %[[VAL_15]] : (i64, index) -> !fir.shapeshift<1>
32+
! CHECK: %[[VAL_17:.*]] = fir.shape_shift %[[VAL_4]], %[[ext0]] : (i64, index) -> !fir.shapeshift<1>
3133
! CHECK: %[[VAL_18:.*]] = fir.embox %[[VAL_16]](%[[VAL_17]]) : (!fir.ref<!fir.array<?xf32>>, !fir.shapeshift<1>) -> !fir.box<!fir.ptr<!fir.array<?xf32>>>
3234
! CHECK: fir.store %[[VAL_18]] to %[[VAL_2]]#0 : !fir.ref<!fir.box<!fir.ptr<!fir.array<?xf32>>>>

flang/test/Lower/pointer-assignments.f90

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,15 @@ subroutine test_array_remap(p, x)
113113
! CHECK-DAG: %[[c2_idx:.*]] = fir.convert %c2{{.*}} : (i64) -> index
114114
! CHECK-DAG: %[[c11_idx:.*]] = fir.convert %c11{{.*}} : (i64) -> index
115115
! CHECK-DAG: %[[diff0:.*]] = arith.subi %[[c11_idx]], %[[c2_idx]] : index
116-
! CHECK-DAG: %[[ext0:.*]] = arith.addi %[[diff0:.*]], %c1{{.*}} : index
116+
! CHECK-DAG: %[[raw_ext0:.*]] = arith.addi %[[diff0:.*]], %c1{{.*}} : index
117+
! CHECK-DAG: %[[cmp0:.*]] = arith.cmpi sgt, %[[raw_ext0]], %c0{{.*}} : index
118+
! CHECK-DAG: %[[ext0:.*]] = arith.select %[[cmp0]], %[[raw_ext0]], %c0{{.*}} : index
117119
! CHECK-DAG: %[[c3_idx:.*]] = fir.convert %c3{{.*}} : (i64) -> index
118120
! CHECK-DAG: %[[c12_idx:.*]] = fir.convert %c12{{.*}} : (i64) -> index
119121
! CHECK-DAG: %[[diff1:.*]] = arith.subi %[[c12_idx]], %[[c3_idx]] : index
120-
! CHECK-DAG: %[[ext1:.*]] = arith.addi %[[diff1]], %c1{{.*}} : index
122+
! CHECK-DAG: %[[raw_ext1:.*]] = arith.addi %[[diff1]], %c1{{.*}} : index
123+
! CHECK-DAG: %[[cmp1:.*]] = arith.cmpi sgt, %[[raw_ext1]], %c0{{.*}} : index
124+
! CHECK-DAG: %[[ext1:.*]] = arith.select %[[cmp1]], %[[raw_ext1]], %c0{{.*}} : index
121125
! CHECK-DAG: %[[addrCast:.*]] = fir.convert %[[x]] : (!fir.ref<!fir.array<100xf32>>) -> !fir.ref<!fir.array<?x?xf32>>
122126
! CHECK: %[[shape:.*]] = fir.shape_shift %c2{{.*}}, %[[ext0]], %c3{{.*}}, %[[ext1]]
123127
! CHECK: %[[box:.*]] = fir.embox %[[addrCast]](%[[shape]]) : (!fir.ref<!fir.array<?x?xf32>>, !fir.shapeshift<2>) -> !fir.box<!fir.ptr<!fir.array<?x?xf32>>>
@@ -132,9 +136,9 @@ subroutine test_array_char_remap(p, x)
132136
character(*), target :: x(100)
133137
character(:), pointer :: p(:, :)
134138
! CHECK: subi
135-
! CHECK: %[[ext0:.*]] = arith.addi
139+
! CHECK: %[[ext0:.*]] = arith.select
136140
! CHECK: subi
137-
! CHECK: %[[ext1:.*]] = arith.addi
141+
! CHECK: %[[ext1:.*]] = arith.select
138142
! CHECK: %[[shape:.*]] = fir.shape_shift %c2{{.*}}, %[[ext0]], %c3{{.*}}, %[[ext1]]
139143
! CHECK: %[[box:.*]] = fir.embox %{{.*}}(%[[shape]]) typeparams %[[unbox]]#1 : (!fir.ref<!fir.array<?x?x!fir.char<1,?>>>, !fir.shapeshift<2>, index) -> !fir.box<!fir.ptr<!fir.array<?x?x!fir.char<1,?>>>>
140144
! CHECK: fir.store %[[box]] to %[[p]]
@@ -218,9 +222,9 @@ subroutine test_array_non_contig_remap(p, x)
218222
real, target :: x(:)
219223
real, pointer :: p(:, :)
220224
! CHECK: subi
221-
! CHECK: %[[ext0:.*]] = arith.addi
225+
! CHECK: %[[ext0:.*]] = arith.select
222226
! CHECK: subi
223-
! CHECK: %[[ext1:.*]] = arith.addi
227+
! CHECK: %[[ext1:.*]] = arith.select
224228
! CHECK: %[[shape:.*]] = fir.shape_shift %{{.*}}, %[[ext0]], %{{.*}}, %[[ext1]]
225229
! CHECK: %[[rebox:.*]] = fir.rebox %[[x]](%[[shape]]) : (!fir.box<!fir.array<?xf32>>, !fir.shapeshift<2>) -> !fir.box<!fir.ptr<!fir.array<?x?xf32>>>
226230
! CHECK: fir.store %[[rebox]] to %[[p]] : !fir.ref<!fir.box<!fir.ptr<!fir.array<?x?xf32>>>>
@@ -250,13 +254,17 @@ subroutine test_array_non_contig_remap(p, x)
250254
! CHECK: %[[VAL_18:.*]] = fir.convert %[[VAL_4]] : (i64) -> index
251255
! CHECK: %[[VAL_19:.*]] = arith.subi %[[VAL_18]], %[[VAL_17]] : index
252256
! CHECK: %[[VAL_20:.*]] = arith.addi %[[VAL_19]], %[[VAL_16]] : index
257+
! CHECK: %[[cmp0:.*]] = arith.cmpi sgt, %[[VAL_20]], %c0{{.*}} : index
258+
! CHECK: %[[ext0:.*]] = arith.select %[[cmp0]], %[[VAL_20]], %c0{{.*}} : index
253259
! CHECK: %[[VAL_21:.*]] = fir.convert %[[VAL_5]] : (i64) -> index
254260
! CHECK: %[[VAL_22:.*]] = fir.convert %[[VAL_6]] : (i64) -> index
255261
! CHECK: %[[VAL_23:.*]] = arith.subi %[[VAL_22]], %[[VAL_21]] : index
256262
! CHECK: %[[VAL_24:.*]] = arith.addi %[[VAL_23]], %[[VAL_16]] : index
263+
! CHECK: %[[cmp1:.*]] = arith.cmpi sgt, %[[VAL_24]], %c0{{.*}} : index
264+
! CHECK: %[[ext1:.*]] = arith.select %[[cmp1]], %[[VAL_24]], %c0{{.*}} : index
257265
! CHECK: %[[VAL_25:.*]] = fir.convert %[[VAL_3]] : (i64) -> index
258266
! CHECK: %[[VAL_26:.*]] = fir.convert %[[VAL_5]] : (i64) -> index
259-
! CHECK: %[[VAL_27:.*]] = fir.shape_shift %[[VAL_25]], %[[VAL_20]], %[[VAL_26]], %[[VAL_24]] : (index, index, index, index) -> !fir.shapeshift<2>
267+
! CHECK: %[[VAL_27:.*]] = fir.shape_shift %[[VAL_25]], %[[ext0]], %[[VAL_26]], %[[ext1]] : (index, index, index, index) -> !fir.shapeshift<2>
260268
! CHECK: %[[VAL_28:.*]] = fir.rebox %[[VAL_15]](%[[VAL_27]]) : (!fir.box<!fir.array<100xf32>>, !fir.shapeshift<2>) -> !fir.box<!fir.ptr<!fir.array<?x?xf32>>>
261269
! CHECK: fir.store %[[VAL_28]] to %[[VAL_0]] : !fir.ref<!fir.box<!fir.ptr<!fir.array<?x?xf32>>>>
262270
! CHECK: return
@@ -333,7 +341,9 @@ subroutine issue857_array_remap(rhs)
333341
! CHECK: %[[c101:.*]] = fir.convert %c101_i64 : (i64) -> index
334342
! CHECK: %[[c200:.*]] = fir.convert %c200_i64 : (i64) -> index
335343
! CHECK: %[[sub:.*]] = arith.subi %[[c200]], %[[c101]] : index
336-
! CHECK: %[[extent:.*]] = arith.addi %[[sub]], %c1{{.*}} : index
344+
! CHECK: %[[raw_extent:.*]] = arith.addi %[[sub]], %c1{{.*}} : index
345+
! CHECK: %[[cmp:.*]] = arith.cmpi sgt, %[[raw_extent]], %c0{{.*}} : index
346+
! CHECK: %[[extent:.*]] = arith.select %[[cmp]], %[[raw_extent]], %c0{{.*}} : index
337347
! CHECK: %[[addr:.*]] = fir.box_addr %{{.*}} : (!fir.box<!fir.ptr<!fir.array<?x?x!fir.type<_QFissue857_array_remapTt{i:i32}>>>>) -> !fir.ptr<!fir.array<?x?x!fir.type<_QFissue857_array_remapTt{i:i32}>>>
338348
! CHECK: %[[addr_cast:.*]] = fir.convert %[[addr]] : (!fir.ptr<!fir.array<?x?x!fir.type<_QFissue857_array_remapTt{i:i32}>>>) -> !fir.ptr<!fir.array<?x!fir.type<_QFissue857_array_remapTt{i:i32}>>>
339349
! CHECK: fir.store %[[addr_cast]] to %[[lhs_addr]] : !fir.ref<!fir.ptr<!fir.array<?x!fir.type<_QFissue857_array_remapTt{i:i32}>>>>

0 commit comments

Comments
 (0)