Skip to content

Commit

Permalink
[flang] Fix a bug with simplified minloc that treated logicals with e…
Browse files Browse the repository at this point in the history
…ven values > 1 as 0

Previously the mask would be loaded as the appropriate integer type and cast to I1 to pass to
fir.if, however this truncates the integer and so would cast 6 to 0. By loading values as logicals
and casting to I1 this problem is avoided.

Reviewed By: Leporacanthicus

Differential Revision: https://reviews.llvm.org/D144974
  • Loading branch information
Sacha Ballantyne authored and Sacha Ballantyne committed Feb 28, 2023
1 parent f94fb26 commit 242bb0b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
13 changes: 7 additions & 6 deletions flang/lib/Optimizer/Transforms/SimplifyIntrinsics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1189,15 +1189,16 @@ void SimplifyIntrinsicsPass::simplifyMinlocReduction(

int maskRank;
fir::KindTy kind = 0;
mlir::Type logicalConvertType = builder.getI1Type();
mlir::Type logicalElemType = builder.getI1Type();
if (isOperandAbsent(mask)) {
maskRank = -1;
} else {
maskRank = getDimCount(mask);
mlir::Type maskElemTy = hlfir::getFortranElementType(maskDef.getType());
fir::LogicalType maskLogiTy = {maskElemTy.dyn_cast<fir::LogicalType>()};
kind = maskLogiTy.getFKind();
logicalConvertType = builder.getIntegerType(kind * 8);
fir::LogicalType logicalFirType = {maskElemTy.dyn_cast<fir::LogicalType>()};
kind = logicalFirType.getFKind();
// Convert fir::LogicalType to mlir::Type
logicalElemType = logicalFirType;
}

mlir::Operation *outputDef = args[0].getDefiningOp();
Expand All @@ -1221,11 +1222,11 @@ void SimplifyIntrinsicsPass::simplifyMinlocReduction(
auto typeGenerator = [rank](fir::FirOpBuilder &builder) {
return genRuntimeMinlocType(builder, rank);
};
auto bodyGenerator = [rank, maskRank, inputType, logicalConvertType,
auto bodyGenerator = [rank, maskRank, inputType, logicalElemType,
outType](fir::FirOpBuilder &builder,
mlir::func::FuncOp &funcOp) {
genRuntimeMinlocBody(builder, funcOp, rank, maskRank, inputType,
logicalConvertType, outType);
logicalElemType, outType);
};

mlir::func::FuncOp newFunc =
Expand Down
8 changes: 4 additions & 4 deletions flang/test/Transforms/simplifyintrinsics.fir
Original file line number Diff line number Diff line change
Expand Up @@ -1767,16 +1767,16 @@ func.func @_QPtestminloc_works1d(%arg0: !fir.ref<!fir.array<10xi32>> {fir.bindc_
// CHECK: %[[FLAG_SET:.*]] = arith.constant 1 : i32
// CHECK: %[[FLAG_EMPTY:.*]] = arith.constant 0 : i32
// CHECK: fir.store %[[FLAG_EMPTY]] to %[[FLAG_ALLOC]] : !fir.ref<i32>
// CHECK: %[[BOX_MASK:.*]] = fir.convert %[[BOX_MASK_NONE]] : (!fir.box<none>) -> !fir.box<!fir.array<?xi32>>
// CHECK: %[[BOX_MASK:.*]] = fir.convert %[[BOX_MASK_NONE]] : (!fir.box<none>) -> !fir.box<!fir.array<?x!fir.logical<4>>>
// CHECK: %[[MAX:.*]] = arith.constant 2147483647 : i32
// CHECK: %[[CINDEX_1:.*]] = arith.constant 1 : index
// CHECK: %[[DIM_INDEX0:.*]] = arith.constant 0 : index
// CHECK: %[[DIMS:.*]]:3 = fir.box_dims %[[BOX_INARR]], %[[DIM_INDEX0]] : (!fir.box<!fir.array<?xi32>>, index) -> (index, index, index)
// CHECK: %[[EXTENT:.*]] = arith.subi %[[DIMS]]#1, %[[CINDEX_1]] : index
// CHECK: %[[DOLOOP:.*]] = fir.do_loop %[[ITER:.*]] = %[[CINDEX_0]] to %[[EXTENT]] step %[[CINDEX_1]] iter_args(%[[MIN:.*]] = %[[MAX]]) -> (i32) {
// CHECK: %[[MASK_ITEM:.*]] = fir.coordinate_of %[[BOX_MASK]], %[[ITER]] : (!fir.box<!fir.array<?xi32>>, index) -> !fir.ref<i32>
// CHECK: %[[MASK_ITEMVAL:.*]] = fir.load %[[MASK_ITEM]] : !fir.ref<i32>
// CHECK: %[[MASK_IF_ITEM:.*]] = fir.convert %[[MASK_ITEMVAL]] : (i32) -> i1
// CHECK: %[[MASK_ITEM:.*]] = fir.coordinate_of %[[BOX_MASK]], %[[ITER]] : (!fir.box<!fir.array<?x!fir.logical<4>>>, index) -> !fir.ref<!fir.logical<4>>
// CHECK: %[[MASK_ITEMVAL:.*]] = fir.load %[[MASK_ITEM]] : !fir.ref<!fir.logical<4>>
// CHECK: %[[MASK_IF_ITEM:.*]] = fir.convert %[[MASK_ITEMVAL]] : (!fir.logical<4>) -> i1
// CHECK: %[[IF_MASK:.*]] = fir.if %[[MASK_IF_ITEM]] -> (i32) {
// CHECK: fir.store %[[FLAG_SET]] to %[[FLAG_ALLOC]] : !fir.ref<i32>
// CHECK: %[[INARR_ITEM:.*]] = fir.coordinate_of %[[BOX_INARR]], %[[ITER]] : (!fir.box<!fir.array<?xi32>>, index) -> !fir.ref<i32>
Expand Down

0 comments on commit 242bb0b

Please sign in to comment.