Skip to content

Commit 64bf14a

Browse files
committed
fix regression with vector subscripts in WHERE and add test
1 parent 66effd4 commit 64bf14a

File tree

2 files changed

+56
-6
lines changed

2 files changed

+56
-6
lines changed

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1299,11 +1299,12 @@ void OrderedAssignmentRewriter::saveLeftHandSide(
12991299
// Save vector subscripted LHS address.
13001300
mlir::Location loc = region.getParentOp()->getLoc();
13011301
LhsValueAndCleanUp loweredLhs = generateYieldedLHS(loc, region);
1302-
assert(loweredLhs.vectorSubscriptLoopNest &&
1303-
"expect vector subscript loop nest");
1304-
constructStack.push_back(loweredLhs.vectorSubscriptLoopNest->outerOp);
1302+
// loweredLhs.vectorSubscriptLoopNest is empty inside a WHERE because the
1303+
// WHERE loops are already indexing the vector subscripted designator.
1304+
if (loweredLhs.vectorSubscriptLoopNest)
1305+
constructStack.push_back(loweredLhs.vectorSubscriptLoopNest->outerOp);
13051306
fir::factory::TemporaryStorage *temp = nullptr;
1306-
if (!rhsIsArray(regionAssignOp)) {
1307+
if (loweredLhs.vectorSubscriptLoopNest && !rhsIsArray(regionAssignOp)) {
13071308
// Vector subscripted entity for which the shape must also be saved on top
13081309
// of the element addresses (e.g. the shape may change in each forall
13091310
// iteration and is needed to create the elemental loops).
@@ -1341,8 +1342,10 @@ void OrderedAssignmentRewriter::saveLeftHandSide(
13411342
}
13421343
temp->pushValue(loc, builder, loweredLhs.lhs);
13431344
generateCleanupIfAny(loweredLhs.elementalCleanup);
1344-
constructStack.pop_back();
1345-
builder.setInsertionPointAfter(loweredLhs.vectorSubscriptLoopNest->outerOp);
1345+
if (loweredLhs.vectorSubscriptLoopNest) {
1346+
constructStack.pop_back();
1347+
builder.setInsertionPointAfter(loweredLhs.vectorSubscriptLoopNest->outerOp);
1348+
}
13461349
}
13471350

13481351
/// Lower an ordered assignment tree to fir.do_loop and hlfir.assign given

flang/test/HLFIR/order_assignments/vector-subscripts-codegen.fir

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,3 +212,50 @@ func.func @unordered(%arg0: !fir.ref<!fir.array<100xf32>> , %arg1: !fir.ref<!fir
212212
// CHECK: }
213213
// CHECK: return
214214
// CHECK: }
215+
216+
// Test vector saving subscripted LHS inside WHERE.
217+
!t=!fir.type<_QFwhere_vec_subscriptsTt{x:f32,vec:!fir.array<4xi64>}>
218+
func.func @_QPwhere_vec_subscripts(%arg0: !fir.ref<!fir.array<4x!fir.logical<4>>>, %arg1: !fir.box<!fir.array<?x!t>>) {
219+
%0 = fir.dummy_scope : !fir.dscope
220+
%1:2 = hlfir.declare %arg1 dummy_scope %0 {uniq_name = "_QFwhere_vec_subscriptsEa"} : (!fir.box<!fir.array<?x!t>>, !fir.dscope) -> (!fir.box<!fir.array<?x!t>>, !fir.box<!fir.array<?x!t>>)
221+
%c4 = arith.constant 4 : index
222+
%2 = fir.shape %c4 : (index) -> !fir.shape<1>
223+
%3:2 = hlfir.declare %arg0(%2) dummy_scope %0 {uniq_name = "_QFwhere_vec_subscriptsEmask"} : (!fir.ref<!fir.array<4x!fir.logical<4>>>, !fir.shape<1>, !fir.dscope) -> (!fir.ref<!fir.array<4x!fir.logical<4>>>, !fir.ref<!fir.array<4x!fir.logical<4>>>)
224+
hlfir.where {
225+
hlfir.yield %3#0 : !fir.ref<!fir.array<4x!fir.logical<4>>>
226+
} do {
227+
hlfir.region_assign {
228+
%cst = arith.constant 0.000000e+00 : f32
229+
hlfir.yield %cst : f32
230+
} to {
231+
%c1 = arith.constant 1 : index
232+
%4 = hlfir.designate %1#0 (%c1) : (!fir.box<!fir.array<?x!t>>, index) -> !fir.ref<!t>
233+
%6 = hlfir.designate %4{"vec"} shape %2 : (!fir.ref<!t>, !fir.shape<1>) -> !fir.ref<!fir.array<4xi64>>
234+
hlfir.elemental_addr %2 unordered : !fir.shape<1> {
235+
^bb0(%arg2: index):
236+
%8 = hlfir.designate %6 (%arg2) : (!fir.ref<!fir.array<4xi64>>, index) -> !fir.ref<i64>
237+
%9 = fir.load %8 : !fir.ref<i64>
238+
%10 = hlfir.designate %1#0 (%9) : (!fir.box<!fir.array<?x!t>>, i64) -> !fir.ref<!t>
239+
%11 = hlfir.designate %10{"x"} : (!fir.ref<!t>) -> !fir.ref<f32>
240+
hlfir.yield %11 : !fir.ref<f32>
241+
}
242+
}
243+
}
244+
return
245+
}
246+
// CHECK-LABEL: func.func @_QPwhere_vec_subscripts(
247+
// CHECK: %[[VAL_16:.*]] = fir.call @_FortranACreateDescriptorStack(
248+
// CHECK: fir.do_loop {{.*}}
249+
// CHECK: fir.if %{{.*}} {
250+
// CHECK: fir.call @_FortranAPushDescriptor(
251+
// CHECK: }
252+
// CHECK: }
253+
// CHECK: fir.do_loop {{.*}}
254+
// CHECK: fir.if %{{.*}} {
255+
// CHECK: fir.call @_FortranADescriptorAt(
256+
// CHECK: hlfir.assign
257+
// CHECK: }
258+
// CHECK: }
259+
// CHECK: fir.call @_FortranADestroyDescriptorStack(
260+
// CHECK: return
261+
// CHECK: }

0 commit comments

Comments
 (0)