Skip to content

Commit e976eaf

Browse files
authored
[flang] Fix optimization of array assignments after #146408 (#147371)
Host associated variables were not being handled properly. For array references, get the fixed shape extents from the value type instead, that works correctly in all cases.
1 parent eb2b63c commit e976eaf

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -805,13 +805,12 @@ 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());
810+
llvm::ArrayRef<int64_t> fixedShape = seqTy.getShape();
808811
int64_t flatExtent = 1;
809-
for (const mlir::Value &extent : extents) {
810-
mlir::Operation *op = extent.getDefiningOp();
811-
assert(op && "no defining operation for constant array extent");
812-
flatExtent *= fir::toInt(mlir::cast<mlir::arith::ConstantOp>(*op));
813-
}
814-
812+
for (int64_t extent : fixedShape)
813+
flatExtent *= extent;
815814
flatArrayType =
816815
fir::ReferenceType::get(fir::SequenceType::get({flatExtent}, eleTy));
817816
flatArray = builder.createConvert(loc, flatArrayType, flatArray);

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,3 +155,32 @@ func.func @_QPtest6(%arg0: !fir.ref<!fir.box<!fir.heap<!fir.array<?x?xi32>>>> {f
155155
// CHECK: }
156156
// CHECK: return
157157
// CHECK: }
158+
159+
func.func @_QQmain() {
160+
return
161+
}
162+
163+
func.func private @_QFPtest7(%arg0: !fir.ref<tuple<!fir.box<!fir.array<2x2xi32>>>> {fir.host_assoc}) attributes {fir.host_symbol = @_QQmain, llvm.linkage = #llvm.linkage<internal>} {
164+
%0 = fir.dummy_scope : !fir.dscope
165+
%c0_i32 = arith.constant 0 : i32
166+
%1 = fir.coordinate_of %arg0, %c0_i32 : (!fir.ref<tuple<!fir.box<!fir.array<2x2xi32>>>>, i32) -> !fir.ref<!fir.box<!fir.array<2x2xi32>>>
167+
%2 = fir.load %1 : !fir.ref<!fir.box<!fir.array<2x2xi32>>>
168+
%3 = fir.box_addr %2 : (!fir.box<!fir.array<2x2xi32>>) -> !fir.ref<!fir.array<2x2xi32>>
169+
%c0 = arith.constant 0 : index
170+
%4:3 = fir.box_dims %2, %c0 : (!fir.box<!fir.array<2x2xi32>>, index) -> (index, index, index)
171+
%c1 = arith.constant 1 : index
172+
%5:3 = fir.box_dims %2, %c1 : (!fir.box<!fir.array<2x2xi32>>, index) -> (index, index, index)
173+
%6 = fir.shape %4#1, %5#1 : (index, index) -> !fir.shape<2>
174+
%7:2 = hlfir.declare %3(%6) {fortran_attrs = #fir.var_attrs<host_assoc>, uniq_name = "_QFEa"} : (!fir.ref<!fir.array<2x2xi32>>, !fir.shape<2>) -> (!fir.ref<!fir.array<2x2xi32>>, !fir.ref<!fir.array<2x2xi32>>)
175+
%c0_i32_0 = arith.constant 0 : i32
176+
hlfir.assign %c0_i32_0 to %7#0 : i32, !fir.ref<!fir.array<2x2xi32>>
177+
return
178+
}
179+
180+
// CHECK-LABEL: func.func private @_QFPtest7({{.*}}) {{.*}} {
181+
// CHECK: %[[VAL_0:.*]] = arith.constant 0 : i32
182+
// CHECK: fir.do_loop %[[VAL_1:.*]] = %{{.*}} to %{{.*}} step %{{.*}} unordered {
183+
// CHECK: %[[VAL_2:.*]] = hlfir.designate %{{.*}} (%[[VAL_1]]) : (!fir.ref<!fir.array<4xi32>>, index) -> !fir.ref<i32>
184+
// CHECK: hlfir.assign %[[VAL_0]] to %[[VAL_2]] : i32, !fir.ref<i32>
185+
// CHECK: }
186+
// CHECK: }

0 commit comments

Comments
 (0)