Skip to content

Commit 5804e90

Browse files
authored
Merge pull request #1163 from schweitzpgi/ch-bug5
Fix vector subscript in vector subscript (in vector subscri...)
2 parents 59a36a1 + 94efeab commit 5804e90

File tree

2 files changed

+114
-42
lines changed

2 files changed

+114
-42
lines changed

flang/lib/Lower/ConvertExpr.cpp

Lines changed: 11 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3475,32 +3475,6 @@ class ArrayExprLowering {
34753475
}
34763476
}
34773477

3478-
/// Entry point for when an array expression appears on the lhs of an
3479-
/// assignment. In the default case, the rhs is fully evaluated prior to any
3480-
/// of the results being written back to the lhs. (CopyInCopyOut semantics.)
3481-
static fir::ArrayLoadOp lowerArraySubspace(
3482-
Fortran::lower::AbstractConverter &converter,
3483-
Fortran::lower::SymMap &symMap, Fortran::lower::StatementContext &stmtCtx,
3484-
const Fortran::evaluate::Expr<Fortran::evaluate::SomeType> &expr) {
3485-
ArrayExprLowering ael{converter, stmtCtx, symMap,
3486-
ConstituentSemantics::CopyInCopyOut};
3487-
return ael.lowerArraySubspace(expr);
3488-
}
3489-
3490-
fir::ArrayLoadOp lowerArraySubspace(
3491-
const Fortran::evaluate::Expr<Fortran::evaluate::SomeType> &exp) {
3492-
return std::visit(
3493-
[&](const auto &e) {
3494-
auto f = genarr(e);
3495-
auto exv = f(IterationSpace{});
3496-
if (auto *defOp = fir::getBase(exv).getDefiningOp())
3497-
if (auto arrLd = mlir::dyn_cast<fir::ArrayLoadOp>(defOp))
3498-
return arrLd;
3499-
fir::emitFatalError(getLoc(), "array must be loaded");
3500-
},
3501-
exp.u);
3502-
}
3503-
35043478
/// Entry point for when an array expression appears in a context where the
35053479
/// result must be boxed. (BoxValue semantics.)
35063480
static ExtValue lowerBoxedArrayExpression(
@@ -5096,30 +5070,25 @@ class ArrayExprLowering {
50965070
// vector subscript with replicated values.
50975071
assert(!isBoxValue() &&
50985072
"fir.box cannot be created with vector subscripts");
5099-
if (Fortran::evaluate::HasVectorSubscript(toEvExpr(e)))
5100-
TODO(loc, "vector subscript of vector subscript");
51015073
auto base = x.base();
51025074
auto exv = genArrayBase(base);
51035075
auto arrExpr = ignoreEvConvert(e);
5104-
auto arrLoad =
5105-
lowerArraySubspace(converter, symMap, stmtCtx, arrExpr);
5106-
auto arrLd = arrLoad.getResult();
5107-
auto eleTy =
5108-
arrLd.getType().cast<fir::SequenceType>().getEleTy();
5076+
auto saveSemant = semant;
5077+
semant = ConstituentSemantics::RefTransparent;
5078+
auto genArrFetch = genarr(arrExpr);
5079+
semant = saveSemant;
51095080
auto currentPC = pc;
51105081
auto dim = sub.index();
51115082
auto lb =
51125083
fir::factory::readLowerBound(builder, loc, exv, dim, one);
5113-
auto arrLdTypeParams = arrLoad.typeparams();
51145084
pc = [=](IterSpace iters) {
51155085
IterationSpace newIters = currentPC(iters);
5116-
auto iter = newIters.iterVec()[dim];
5117-
// TODO: Next line, delete?
5118-
auto resTy = adjustedArrayElementType(eleTy);
5119-
auto fetch = builder.create<fir::ArrayFetchOp>(
5120-
loc, resTy, arrLd, mlir::ValueRange{iter},
5121-
arrLdTypeParams);
5122-
auto cast = builder.createConvert(loc, idxTy, fetch);
5086+
IterationSpace vecIters(
5087+
newIters,
5088+
llvm::ArrayRef<mlir::Value>{newIters.iterValue(dim)});
5089+
auto fetch = genArrFetch(vecIters);
5090+
auto cast =
5091+
builder.createConvert(loc, idxTy, fir::getBase(fetch));
51235092
auto val =
51245093
builder.create<mlir::SubIOp>(loc, idxTy, cast, lb);
51255094
newIters.setIndexValue(dim, val);
@@ -5128,7 +5097,7 @@ class ArrayExprLowering {
51285097
// Create a slice with the vector size so that the shape
51295098
// of array reference is correctly computed in later phase,
51305099
// even though this is not a triplet.
5131-
auto vectorSubscriptShape = getShape(arrLoad);
5100+
auto vectorSubscriptShape = getShape(arrayOperands.back());
51325101
assert(vectorSubscriptShape.size() == 1);
51335102
trips.push_back(one);
51345103
trips.push_back(vectorSubscriptShape[0]);

flang/test/Lower/array-expression-subscript.f90

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,112 @@ subroutine test1b(a,b,c)
9696
b(c(1:20:2)) = a
9797
end subroutine test1b
9898

99+
! CHECK-LABEL: func @_QPtest1c(
100+
! CHECK-SAME: %[[VAL_0:.*]]: !fir.ref<!fir.array<10xi32>>, %[[VAL_1:.*]]: !fir.ref<!fir.array<10xi32>>, %[[VAL_2:.*]]: !fir.ref<!fir.array<20xi32>>, %[[VAL_3:.*]]: !fir.ref<!fir.array<10xi32>>) {
101+
! CHECK: return
102+
! CHECK: }
99103
subroutine test1c(a,b,c,d)
100104
integer :: a(10), b(10), d(10), c(20)
101105

102106
! flang: parser FAIL (final position)
103107
!a = b(d(c(1:20:2))
104108
end subroutine test1c
109+
110+
111+
! CHECK-LABEL: func @_QPtest2a(
112+
! CHECK-SAME: %[[VAL_0:.*]]: !fir.ref<!fir.array<10xi32>>, %[[VAL_1:.*]]: !fir.ref<!fir.array<10xi32>>, %[[VAL_2:.*]]: !fir.ref<!fir.array<10xi32>>, %[[VAL_3:.*]]: !fir.ref<!fir.array<10xi32>>) {
113+
! CHECK: %[[VAL_4:.*]] = constant 10 : index
114+
! CHECK: %[[VAL_5:.*]] = constant 10 : index
115+
! CHECK: %[[VAL_6:.*]] = constant 10 : index
116+
! CHECK: %[[VAL_7:.*]] = constant 10 : index
117+
! CHECK: %[[VAL_8:.*]] = fir.shape %[[VAL_4]] : (index) -> !fir.shape<1>
118+
! CHECK: %[[VAL_9:.*]] = fir.array_load %[[VAL_0]](%[[VAL_8]]) : (!fir.ref<!fir.array<10xi32>>, !fir.shape<1>) -> !fir.array<10xi32>
119+
! CHECK: %[[VAL_10:.*]] = constant 10 : i64
120+
! CHECK: %[[VAL_11:.*]] = fir.convert %[[VAL_10]] : (i64) -> index
121+
! CHECK: %[[VAL_12:.*]] = constant 1 : index
122+
! CHECK: %[[VAL_13:.*]] = constant 1 : index
123+
! CHECK: %[[VAL_14:.*]] = fir.shape %[[VAL_7]] : (index) -> !fir.shape<1>
124+
! CHECK: %[[VAL_15:.*]] = fir.array_load %[[VAL_3]](%[[VAL_14]]) : (!fir.ref<!fir.array<10xi32>>, !fir.shape<1>) -> !fir.array<10xi32>
125+
! CHECK: %[[VAL_16:.*]] = fir.shape %[[VAL_6]] : (index) -> !fir.shape<1>
126+
! CHECK: %[[VAL_17:.*]] = fir.slice %[[VAL_13]], %[[VAL_7]], %[[VAL_13]] : (index, index, index) -> !fir.slice<1>
127+
! CHECK: %[[VAL_18:.*]] = fir.array_load %[[VAL_2]](%[[VAL_16]]) {{\[}}%[[VAL_17]]] : (!fir.ref<!fir.array<10xi32>>, !fir.shape<1>, !fir.slice<1>) -> !fir.array<10xi32>
128+
! CHECK: %[[VAL_19:.*]] = constant 0 : index
129+
! CHECK: %[[VAL_20:.*]] = subi %[[VAL_7]], %[[VAL_13]] : index
130+
! CHECK: %[[VAL_21:.*]] = addi %[[VAL_20]], %[[VAL_13]] : index
131+
! CHECK: %[[VAL_22:.*]] = divi_signed %[[VAL_21]], %[[VAL_13]] : index
132+
! CHECK: %[[VAL_23:.*]] = cmpi sgt, %[[VAL_22]], %[[VAL_19]] : index
133+
! CHECK: %[[VAL_24:.*]] = select %[[VAL_23]], %[[VAL_22]], %[[VAL_19]] : index
134+
! CHECK: %[[VAL_25:.*]] = fir.shape %[[VAL_5]] : (index) -> !fir.shape<1>
135+
! CHECK: %[[VAL_26:.*]] = fir.slice %[[VAL_12]], %[[VAL_24]], %[[VAL_12]] : (index, index, index) -> !fir.slice<1>
136+
! CHECK: %[[VAL_27:.*]] = fir.array_load %[[VAL_1]](%[[VAL_25]]) {{\[}}%[[VAL_26]]] : (!fir.ref<!fir.array<10xi32>>, !fir.shape<1>, !fir.slice<1>) -> !fir.array<10xi32>
137+
! CHECK: %[[VAL_28:.*]] = constant 1 : index
138+
! CHECK: %[[VAL_29:.*]] = constant 0 : index
139+
! CHECK: %[[VAL_30:.*]] = subi %[[VAL_11]], %[[VAL_28]] : index
140+
! CHECK: %[[VAL_31:.*]] = fir.do_loop %[[VAL_32:.*]] = %[[VAL_29]] to %[[VAL_30]] step %[[VAL_28]] unordered iter_args(%[[VAL_33:.*]] = %[[VAL_9]]) -> (!fir.array<10xi32>) {
141+
! CHECK: %[[VAL_34:.*]] = fir.array_fetch %[[VAL_15]], %[[VAL_32]] : (!fir.array<10xi32>, index) -> i32
142+
! CHECK: %[[VAL_35:.*]] = fir.convert %[[VAL_34]] : (i32) -> index
143+
! CHECK: %[[VAL_36:.*]] = subi %[[VAL_35]], %[[VAL_13]] : index
144+
! CHECK: %[[VAL_37:.*]] = fir.array_fetch %[[VAL_18]], %[[VAL_36]] : (!fir.array<10xi32>, index) -> i32
145+
! CHECK: %[[VAL_38:.*]] = fir.convert %[[VAL_37]] : (i32) -> index
146+
! CHECK: %[[VAL_39:.*]] = subi %[[VAL_38]], %[[VAL_12]] : index
147+
! CHECK: %[[VAL_40:.*]] = fir.array_fetch %[[VAL_27]], %[[VAL_39]] : (!fir.array<10xi32>, index) -> i32
148+
! CHECK: %[[VAL_41:.*]] = fir.array_update %[[VAL_33]], %[[VAL_40]], %[[VAL_32]] : (!fir.array<10xi32>, i32, index) -> !fir.array<10xi32>
149+
! CHECK: fir.result %[[VAL_41]] : !fir.array<10xi32>
150+
! CHECK: }
151+
! CHECK: fir.array_merge_store %[[VAL_9]], %[[VAL_42:.*]] to %[[VAL_0]] : !fir.array<10xi32>, !fir.array<10xi32>, !fir.ref<!fir.array<10xi32>>
152+
! CHECK: return
153+
! CHECK: }
154+
subroutine test2a(a,b,c,d)
155+
integer :: a(10), b(10), c(10), d(10)
156+
157+
a = b(c(d))
158+
end subroutine test2a
159+
160+
! CHECK-LABEL: func @_QPtest2b(
161+
! CHECK-SAME: %[[VAL_0:.*]]: !fir.ref<!fir.array<10xi32>>, %[[VAL_1:.*]]: !fir.ref<!fir.array<10xi32>>, %[[VAL_2:.*]]: !fir.ref<!fir.array<10xi32>>, %[[VAL_3:.*]]: !fir.ref<!fir.array<10xi32>>) {
162+
! CHECK: %[[VAL_4:.*]] = constant 10 : index
163+
! CHECK: %[[VAL_5:.*]] = constant 10 : index
164+
! CHECK: %[[VAL_6:.*]] = constant 10 : index
165+
! CHECK: %[[VAL_7:.*]] = constant 10 : index
166+
! CHECK: %[[VAL_8:.*]] = constant 1 : index
167+
! CHECK: %[[VAL_9:.*]] = constant 1 : index
168+
! CHECK: %[[VAL_10:.*]] = fir.shape %[[VAL_7]] : (index) -> !fir.shape<1>
169+
! CHECK: %[[VAL_11:.*]] = fir.array_load %[[VAL_3]](%[[VAL_10]]) : (!fir.ref<!fir.array<10xi32>>, !fir.shape<1>) -> !fir.array<10xi32>
170+
! CHECK: %[[VAL_12:.*]] = fir.shape %[[VAL_6]] : (index) -> !fir.shape<1>
171+
! CHECK: %[[VAL_13:.*]] = fir.slice %[[VAL_9]], %[[VAL_7]], %[[VAL_9]] : (index, index, index) -> !fir.slice<1>
172+
! CHECK: %[[VAL_14:.*]] = fir.array_load %[[VAL_2]](%[[VAL_12]]) {{\[}}%[[VAL_13]]] : (!fir.ref<!fir.array<10xi32>>, !fir.shape<1>, !fir.slice<1>) -> !fir.array<10xi32>
173+
! CHECK: %[[VAL_15:.*]] = constant 0 : index
174+
! CHECK: %[[VAL_16:.*]] = subi %[[VAL_7]], %[[VAL_9]] : index
175+
! CHECK: %[[VAL_17:.*]] = addi %[[VAL_16]], %[[VAL_9]] : index
176+
! CHECK: %[[VAL_18:.*]] = divi_signed %[[VAL_17]], %[[VAL_9]] : index
177+
! CHECK: %[[VAL_19:.*]] = cmpi sgt, %[[VAL_18]], %[[VAL_15]] : index
178+
! CHECK: %[[VAL_20:.*]] = select %[[VAL_19]], %[[VAL_18]], %[[VAL_15]] : index
179+
! CHECK: %[[VAL_21:.*]] = fir.shape %[[VAL_5]] : (index) -> !fir.shape<1>
180+
! CHECK: %[[VAL_22:.*]] = fir.slice %[[VAL_8]], %[[VAL_20]], %[[VAL_8]] : (index, index, index) -> !fir.slice<1>
181+
! CHECK: %[[VAL_23:.*]] = fir.array_load %[[VAL_1]](%[[VAL_21]]) {{\[}}%[[VAL_22]]] : (!fir.ref<!fir.array<10xi32>>, !fir.shape<1>, !fir.slice<1>) -> !fir.array<10xi32>
182+
! CHECK: %[[VAL_24:.*]] = constant 10 : i64
183+
! CHECK: %[[VAL_25:.*]] = fir.convert %[[VAL_24]] : (i64) -> index
184+
! CHECK: %[[VAL_26:.*]] = fir.shape %[[VAL_4]] : (index) -> !fir.shape<1>
185+
! CHECK: %[[VAL_27:.*]] = fir.array_load %[[VAL_0]](%[[VAL_26]]) : (!fir.ref<!fir.array<10xi32>>, !fir.shape<1>) -> !fir.array<10xi32>
186+
! CHECK: %[[VAL_28:.*]] = constant 1 : index
187+
! CHECK: %[[VAL_29:.*]] = constant 0 : index
188+
! CHECK: %[[VAL_30:.*]] = subi %[[VAL_25]], %[[VAL_28]] : index
189+
! CHECK: %[[VAL_31:.*]] = fir.do_loop %[[VAL_32:.*]] = %[[VAL_29]] to %[[VAL_30]] step %[[VAL_28]] unordered iter_args(%[[VAL_33:.*]] = %[[VAL_23]]) -> (!fir.array<10xi32>) {
190+
! CHECK: %[[VAL_34:.*]] = fir.array_fetch %[[VAL_27]], %[[VAL_32]] : (!fir.array<10xi32>, index) -> i32
191+
! CHECK: %[[VAL_35:.*]] = fir.array_fetch %[[VAL_11]], %[[VAL_32]] : (!fir.array<10xi32>, index) -> i32
192+
! CHECK: %[[VAL_36:.*]] = fir.convert %[[VAL_35]] : (i32) -> index
193+
! CHECK: %[[VAL_37:.*]] = subi %[[VAL_36]], %[[VAL_9]] : index
194+
! CHECK: %[[VAL_38:.*]] = fir.array_fetch %[[VAL_14]], %[[VAL_37]] : (!fir.array<10xi32>, index) -> i32
195+
! CHECK: %[[VAL_39:.*]] = fir.convert %[[VAL_38]] : (i32) -> index
196+
! CHECK: %[[VAL_40:.*]] = subi %[[VAL_39]], %[[VAL_8]] : index
197+
! CHECK: %[[VAL_41:.*]] = fir.array_update %[[VAL_33]], %[[VAL_34]], %[[VAL_40]] : (!fir.array<10xi32>, i32, index) -> !fir.array<10xi32>
198+
! CHECK: fir.result %[[VAL_41]] : !fir.array<10xi32>
199+
! CHECK: }
200+
! CHECK: fir.array_merge_store %[[VAL_23]], %[[VAL_42:.*]] to %[[VAL_1]]{{\[}}%[[VAL_22]]] : !fir.array<10xi32>, !fir.array<10xi32>, !fir.ref<!fir.array<10xi32>>, !fir.slice<1>
201+
! CHECK: return
202+
! CHECK: }
203+
subroutine test2b(a,b,c,d)
204+
integer :: a(10), b(10), c(10), d(10)
205+
206+
b(c(d)) = a
207+
end subroutine test2b

0 commit comments

Comments
 (0)