Skip to content

Commit 996092d

Browse files
authored
[flang] probably convert Fortran logical to i1 in expanding hlfir.maxloc/hlfir.minloc opcodes (#129791)
If mask is a scalar, it always converts to !fir.box<!fir.array<1xi1>>. The wrong value may be picked up when passing to the function on the big endian platform. This patch is to do the conversion based on the original type of the mask and convert the value to i1 after the load.
1 parent 5b9e1a5 commit 996092d

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

flang/lib/Optimizer/Transforms/SimplifyIntrinsics.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -821,7 +821,8 @@ static void genRuntimeMinMaxlocBody(fir::FirOpBuilder &builder,
821821
// if mask is a logical scalar, we can check its value before the main loop
822822
// and either ignore the fact it is there or exit early.
823823
if (maskRank == 0) {
824-
mlir::Type logical = builder.getI1Type();
824+
mlir::Type i1Type = builder.getI1Type();
825+
mlir::Type logical = maskElemType;
825826
mlir::IndexType idxTy = builder.getIndexType();
826827

827828
fir::SequenceType::Shape singleElement(1, 1);
@@ -834,8 +835,9 @@ static void genRuntimeMinMaxlocBody(fir::FirOpBuilder &builder,
834835
mlir::Value condAddr =
835836
builder.create<fir::CoordinateOp>(loc, logicalRefTy, array, indx);
836837
mlir::Value cond = builder.create<fir::LoadOp>(loc, condAddr);
838+
mlir::Value condI1 = builder.create<fir::ConvertOp>(loc, i1Type, cond);
837839

838-
fir::IfOp ifOp = builder.create<fir::IfOp>(loc, elementType, cond,
840+
fir::IfOp ifOp = builder.create<fir::IfOp>(loc, elementType, condI1,
839841
/*withElseRegion=*/true);
840842

841843
builder.setInsertionPointToStart(&ifOp.getElseRegion().front());

flang/test/Transforms/simplifyintrinsics.fir

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1996,10 +1996,11 @@ func.func @_QPtestminloc_works1d_scalarmask_f64(%arg0: !fir.ref<!fir.array<10xf6
19961996
// CHECK: %[[OUTARR_IDX0:.*]] = arith.constant 0 : index
19971997
// CHECK: %[[OUTARR_ITEM0:.*]] = fir.coordinate_of %[[BOX_OUTARR]], %[[OUTARR_IDX0]] : (!fir.box<!fir.heap<!fir.array<1xi32>>>, index) -> !fir.ref<i32>
19981998
// CHECK: fir.store %[[INIT_OUT_IDX]] to %[[OUTARR_ITEM0]] : !fir.ref<i32>
1999-
// CHECK: %[[BOX_MASK:.*]] = fir.convert %[[BOX_MASK_NONE]] : (!fir.box<none>) -> !fir.box<!fir.array<1xi1>>
1999+
// CHECK: %[[BOX_MASK:.*]] = fir.convert %[[BOX_MASK_NONE]] : (!fir.box<none>) -> !fir.box<!fir.array<1x!fir.logical<4>>>
20002000
// CHECK: %[[MASK_IDX0:.*]] = arith.constant 0 : index
2001-
// CHECK: %[[MASK_ITEM:.*]] = fir.coordinate_of %[[BOX_MASK]], %[[MASK_IDX0]] : (!fir.box<!fir.array<1xi1>>, index) -> !fir.ref<i1>
2002-
// CHECK: %[[MASK:.*]] = fir.load %[[MASK_ITEM]] : !fir.ref<i1>
2001+
// CHECK: %[[MASK_ITEM:.*]] = fir.coordinate_of %[[BOX_MASK]], %[[MASK_IDX0]] : (!fir.box<!fir.array<1x!fir.logical<4>>>, index) -> !fir.ref<!fir.logical<4>>
2002+
// CHECK: %[[MASK_LOGICAL:.*]] = fir.load %[[MASK_ITEM]] : !fir.ref<!fir.logical<4>>
2003+
// CHECK: %[[MASK:.*]] = fir.convert %[[MASK_LOGICAL]] : (!fir.logical<4>) -> i1
20032004
// CHECK: %[[INIT_RES:.*]] = fir.if %[[MASK]] -> (f64) {
20042005
// CHECK: %[[C_INDEX0:.*]] = arith.constant 0 : index
20052006
// CHECK: %[[BOX_INARR:.*]] = fir.convert %[[BOX_INARR_NONE]] : (!fir.box<none>) -> !fir.box<!fir.array<?xf64>>
@@ -2573,10 +2574,11 @@ func.func @_QPtestmaxloc_works1d_scalarmask_f64(%arg0: !fir.ref<!fir.array<10xf6
25732574
// CHECK: %[[OUTARR_IDX0:.*]] = arith.constant 0 : index
25742575
// CHECK: %[[OUTARR_ITEM0:.*]] = fir.coordinate_of %[[BOX_OUTARR]], %[[OUTARR_IDX0]] : (!fir.box<!fir.heap<!fir.array<1xi32>>>, index) -> !fir.ref<i32>
25752576
// CHECK: fir.store %[[INIT_OUT_IDX]] to %[[OUTARR_ITEM0]] : !fir.ref<i32>
2576-
// CHECK: %[[BOX_MASK:.*]] = fir.convert %[[BOX_MASK_NONE]] : (!fir.box<none>) -> !fir.box<!fir.array<1xi1>>
2577+
// CHECK: %[[BOX_MASK:.*]] = fir.convert %[[BOX_MASK_NONE]] : (!fir.box<none>) -> !fir.box<!fir.array<1x!fir.logical<4>>>
25772578
// CHECK: %[[MASK_IDX0:.*]] = arith.constant 0 : index
2578-
// CHECK: %[[MASK_ITEM:.*]] = fir.coordinate_of %[[BOX_MASK]], %[[MASK_IDX0]] : (!fir.box<!fir.array<1xi1>>, index) -> !fir.ref<i1>
2579-
// CHECK: %[[MASK:.*]] = fir.load %[[MASK_ITEM]] : !fir.ref<i1>
2579+
// CHECK: %[[MASK_ITEM:.*]] = fir.coordinate_of %[[BOX_MASK]], %[[MASK_IDX0]] : (!fir.box<!fir.array<1x!fir.logical<4>>>, index) -> !fir.ref<!fir.logical<4>>
2580+
// CHECK: %[[MASK_LOGICAL:.*]] = fir.load %[[MASK_ITEM]] : !fir.ref<!fir.logical<4>>
2581+
// CHECK: %[[MASK:.*]] = fir.convert %[[MASK_LOGICAL]] : (!fir.logical<4>) -> i1
25802582
// CHECK: %[[INIT_RES:.*]] = fir.if %[[MASK]] -> (f64) {
25812583
// CHECK: %[[C_INDEX0:.*]] = arith.constant 0 : index
25822584
// CHECK: %[[BOX_INARR:.*]] = fir.convert %[[BOX_INARR_NONE]] : (!fir.box<none>) -> !fir.box<!fir.array<?xf64>>

0 commit comments

Comments
 (0)