Skip to content

Commit a6352a6

Browse files
authored
Merge pull request #1148 from schweitzpgi/ch-bug1
Fix mask evaluation bug.
2 parents 2e86d83 + fa8a65e commit a6352a6

File tree

6 files changed

+1045
-901
lines changed

6 files changed

+1045
-901
lines changed

flang/lib/Lower/ConvertExpr.cpp

Lines changed: 15 additions & 17 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.
@@ -4675,16 +4674,9 @@ class ArrayExprLowering {
46754674
static ExtValue convertAdjustedType(fir::FirOpBuilder &builder,
46764675
mlir::Location loc, mlir::Type toType,
46774676
const ExtValue &exv) {
4678-
auto lenFromBufferType = [&](mlir::Type ty) {
4679-
return builder.create<mlir::ConstantIndexOp>(
4680-
loc, fir::dyn_cast_ptrEleTy(ty).cast<fir::CharacterType>().getLen());
4681-
};
46824677
return exv.match(
46834678
[&](const fir::CharBoxValue &cb) -> ExtValue {
4684-
auto typeParams = fir::getTypeParams(exv);
4685-
auto len = typeParams.size() > 0
4686-
? typeParams[0]
4687-
: lenFromBufferType(cb.getBuffer().getType());
4679+
auto len = cb.getLen();
46884680
auto mem =
46894681
builder.create<fir::AllocaOp>(loc, toType, mlir::ValueRange{len});
46904682
fir::CharBoxValue result(mem, len);
@@ -4706,7 +4698,9 @@ class ArrayExprLowering {
47064698
return [=](IterSpace iters) -> ExtValue {
47074699
auto exv = lambda(iters);
47084700
auto val = fir::getBase(exv);
4709-
if (elementTypeWasAdjusted(val.getType()))
4701+
auto valTy = val.getType();
4702+
if (elementTypeWasAdjusted(valTy) &&
4703+
!(fir::isa_ref_type(valTy) && fir::isa_integer(ty)))
47104704
return convertAdjustedType(builder, loc, ty, exv);
47114705
return builder.createConvert(loc, ty, val);
47124706
};
@@ -6401,7 +6395,11 @@ class ArrayExprLowering {
64016395
: converter{converter}, builder{converter.getFirOpBuilder()},
64026396
stmtCtx{stmtCtx}, symMap{symMap},
64036397
explicitSpace(expSpace->isActive() ? expSpace : nullptr),
6404-
implicitSpace(impSpace->empty() ? nullptr : impSpace), semant{sem} {}
6398+
implicitSpace(impSpace->empty() ? nullptr : impSpace), semant{sem} {
6399+
// Generate any mask expressions, as necessary. This is the compute step
6400+
// that creates the effective masks. See 10.2.3.2 in particular.
6401+
genMasks();
6402+
}
64056403

64066404
mlir::Location getLoc() { return converter.getCurrentLocation(); }
64076405

flang/test/Lower/array-expression.f90

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -816,13 +816,12 @@ subroutine test19g(a,b,i)
816816
! CHECK: %[[VAL_25:.*]] = subi %[[VAL_14]], %[[VAL_23]] : index
817817
! CHECK: %[[VAL_26:.*]] = fir.do_loop %[[VAL_27:.*]] = %[[VAL_24]] to %[[VAL_25]] step %[[VAL_23]] unordered iter_args(%[[VAL_28:.*]] = %[[VAL_12]]) -> (!fir.array<70x!fir.char<4,?>>) {
818818
! CHECK: %[[VAL_29:.*]] = fir.array_access %[[VAL_20]], %[[VAL_27]] : (!fir.array<140x!fir.char<2,13>>, index) -> !fir.ref<!fir.char<2,13>>
819-
! CHECK: %[[VAL_30:.*]] = constant 13 : index
820-
! CHECK: %[[VAL_31:.*]] = fir.alloca !fir.char<4,?>(%[[VAL_30]] : index)
821-
! CHECK: %[[VAL_32:.*]] = cmpi slt, %[[VAL_30]], %[[VAL_4]] : index
822-
! CHECK: %[[VAL_33:.*]] = select %[[VAL_32]], %[[VAL_30]], %[[VAL_4]] : index
819+
! CHECK: %[[VAL_31:.*]] = fir.alloca !fir.char<4,?>(%[[VAL_4]] : index)
820+
! CHECK: %[[VAL_32:.*]] = cmpi slt, %[[VAL_4]], %[[VAL_4]] : index
821+
! CHECK: %[[VAL_33:.*]] = select %[[VAL_32]], %[[VAL_4]], %[[VAL_4]] : index
823822
! CHECK: fir.char_convert %[[VAL_29]] for %[[VAL_33]] to %[[VAL_31]] : !fir.ref<!fir.char<2,13>>, index, !fir.ref<!fir.char<4,?>>
824823
! CHECK: %[[VAL_34:.*]] = constant 1 : index
825-
! CHECK: %[[VAL_35:.*]] = subi %[[VAL_30]], %[[VAL_34]] : index
824+
! CHECK: %[[VAL_35:.*]] = subi %[[VAL_4]], %[[VAL_34]] : index
826825
! CHECK: %[[VAL_36:.*]] = constant 32 : i32
827826
! CHECK: %[[VAL_37:.*]] = fir.undefined !fir.char<4>
828827
! CHECK: %[[VAL_38:.*]] = fir.insert_value %[[VAL_37]], %[[VAL_36]], [0 : index] : (!fir.char<4>, i32) -> !fir.char<4>

0 commit comments

Comments
 (0)