Skip to content

Commit fa8a65e

Browse files
committed
Fix mask evaluation order bug.
1 parent 2aabe23 commit fa8a65e

File tree

5 files changed

+1037
-887
lines changed

5 files changed

+1037
-887
lines changed

flang/lib/Lower/ConvertExpr.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4077,11 +4077,16 @@ class ArrayExprLowering {
40774077
return header;
40784078
}
40794079

4080+
/// Lower mask expressions with implied iteration spaces from the variants of
4081+
/// WHERE syntax. Since it is legal for mask expressions to have side-effects
4082+
/// and modify values that will be used for the lhs, rhs, or both of
4083+
/// subsequent assignments, the mask must be evaluated before the assignment
4084+
/// is processed.
40804085
/// Mask expressions are array expressions too.
40814086
void genMasks() {
4082-
auto loc = getLoc();
40834087
// Lower the mask expressions, if any.
40844088
if (implicitSpaceHasMasks()) {
4089+
auto loc = getLoc();
40854090
// Mask expressions are array expressions too.
40864091
for (const auto *e : implicitSpace->getExprs())
40874092
if (e && !implicitSpace->isLowered(e)) {
@@ -4140,7 +4145,6 @@ class ArrayExprLowering {
41404145
auto ldExt = builder.create<fir::LoadOp>(loc, coor);
41414146
extents.push_back(builder.createConvert(loc, idxTy, ldExt));
41424147
}
4143-
destShape = extents;
41444148
// Construct shape of buffer.
41454149
auto shapeOp = builder.genShape(loc, extents);
41464150

@@ -4221,11 +4225,6 @@ class ArrayExprLowering {
42214225
std::pair<IterationSpace, mlir::OpBuilder::InsertPoint>
42224226
genIterSpace(mlir::Type resultType) {
42234227
auto loc = getLoc();
4224-
4225-
// Generate any mask expressions, as necessary. This is the compute step
4226-
// that creates the effective masks. See 10.2.3.2 in particular.
4227-
genMasks();
4228-
42294228
auto shape = genIterationShape();
42304229
if (!destination) {
42314230
// Allocate storage for the result if it is not already provided.
@@ -6378,7 +6377,11 @@ class ArrayExprLowering {
63786377
: converter{converter}, builder{converter.getFirOpBuilder()},
63796378
stmtCtx{stmtCtx}, symMap{symMap},
63806379
explicitSpace(expSpace->isActive() ? expSpace : nullptr),
6381-
implicitSpace(impSpace->empty() ? nullptr : impSpace), semant{sem} {}
6380+
implicitSpace(impSpace->empty() ? nullptr : impSpace), semant{sem} {
6381+
// Generate any mask expressions, as necessary. This is the compute step
6382+
// that creates the effective masks. See 10.2.3.2 in particular.
6383+
genMasks();
6384+
}
63826385

63836386
mlir::Location getLoc() { return converter.getCurrentLocation(); }
63846387

0 commit comments

Comments
 (0)