Skip to content

Commit a63846b

Browse files
authored
[flang] Fix array assignment regression introduced by #147371 (#147761)
In some cases fixed shape arrays can be fir.heap/fir.ptr, even after hlfir::derefPointersAndAllocatables() is called.
1 parent c57fe2f commit a63846b

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

flang/lib/Optimizer/HLFIR/Transforms/OptimizedBufferization.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -805,8 +805,8 @@ llvm::LogicalResult BroadcastAssignBufferization::matchAndRewrite(
805805
shape, /*slice=*/mlir::Value{});
806806
} else {
807807
// Array references must have fixed shape, when used in assignments.
808-
auto refTy = mlir::cast<fir::ReferenceType>(lhs.getType());
809-
auto seqTy = mlir::cast<fir::SequenceType>(refTy.getElementType());
808+
auto seqTy =
809+
mlir::cast<fir::SequenceType>(fir::unwrapRefType(lhs.getType()));
810810
llvm::ArrayRef<int64_t> fixedShape = seqTy.getShape();
811811
int64_t flatExtent = 1;
812812
for (int64_t extent : fixedShape)

flang/test/HLFIR/opt-scalar-assign.fir

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,3 +184,34 @@ func.func private @_QFPtest7(%arg0: !fir.ref<tuple<!fir.box<!fir.array<2x2xi32>>
184184
// CHECK: hlfir.assign %[[VAL_0]] to %[[VAL_2]] : i32, !fir.ref<i32>
185185
// CHECK: }
186186
// CHECK: }
187+
188+
func.func @_QPtest8() {
189+
%0 = fir.dummy_scope : !fir.dscope
190+
%1 = fir.alloca !fir.array<120xi8> {uniq_name = "_QFtest8Ea"}
191+
%c0 = arith.constant 0 : index
192+
%2 = fir.coordinate_of %1, %c0 : (!fir.ref<!fir.array<120xi8>>, index) -> !fir.ref<i8>
193+
%3 = fir.convert %2 : (!fir.ref<i8>) -> !fir.ptr<!fir.array<2x10xf32>>
194+
%c2 = arith.constant 2 : index
195+
%c10 = arith.constant 10 : index
196+
%4 = fir.shape %c2, %c10 : (index, index) -> !fir.shape<2>
197+
%5:2 = hlfir.declare %3(%4) {uniq_name = "_QFtest8Ea"} : (!fir.ptr<!fir.array<2x10xf32>>, !fir.shape<2>) -> (!fir.ptr<!fir.array<2x10xf32>>, !fir.ptr<!fir.array<2x10xf32>>)
198+
%c0_0 = arith.constant 0 : index
199+
%6 = fir.coordinate_of %1, %c0_0 : (!fir.ref<!fir.array<120xi8>>, index) -> !fir.ref<i8>
200+
%7 = fir.convert %6 : (!fir.ref<i8>) -> !fir.ptr<!fir.array<30xf32>>
201+
%c30 = arith.constant 30 : index
202+
%8 = fir.shape %c30 : (index) -> !fir.shape<1>
203+
%9:2 = hlfir.declare %7(%8) {uniq_name = "_QFtest8Eb"} : (!fir.ptr<!fir.array<30xf32>>, !fir.shape<1>) -> (!fir.ptr<!fir.array<30xf32>>, !fir.ptr<!fir.array<30xf32>>)
204+
%cst = arith.constant -1.000000e+00 : f32
205+
hlfir.assign %cst to %5#0 : f32, !fir.ptr<!fir.array<2x10xf32>>
206+
return
207+
}
208+
209+
// CHECK-LABEL: func.func @_QPtest8() {
210+
// CHECK: %[[VAL_0:.*]] = arith.constant -1.000000e+00 : f32
211+
// CHECK: %[[VAL_1:.*]]:2 = hlfir.declare %{{.*}}(%{{.*}}) {uniq_name = "_QFtest8Ea"} : (!fir.ptr<!fir.array<2x10xf32>>, !fir.shape<2>) -> (!fir.ptr<!fir.array<2x10xf32>>, !fir.ptr<!fir.array<2x10xf32>>)
212+
// CHECK: %[[VAL_2:.*]] = fir.convert %[[VAL_1]]#0 : (!fir.ptr<!fir.array<2x10xf32>>) -> !fir.ref<!fir.array<20xf32>>
213+
// CHECK: fir.do_loop %[[VAL_3:.*]] = %{{.*}} to %{{.*}} step %{{.*}} unordered {
214+
// CHECK: %[[VAL_4:.*]] = hlfir.designate %[[VAL_2]] (%[[VAL_3]]) : (!fir.ref<!fir.array<20xf32>>, index) -> !fir.ref<f32>
215+
// CHECK: hlfir.assign %[[VAL_0]] to %[[VAL_4]] : f32, !fir.ref<f32>
216+
// CHECK: }
217+
// CHECK: }

0 commit comments

Comments
 (0)