[flang] Fix a bug with simplified minloc that treated logicals with even values ...
authorSacha Ballantyne <Sacha.Ballantyne@arm.com>
Tue, 28 Feb 2023 16:42:18 +0000 (16:42 +0000)
committerSacha Ballantyne <Sacha.Ballantyne@arm.com>
Tue, 28 Feb 2023 17:15:36 +0000 (17:15 +0000)
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

flang/lib/Optimizer/Transforms/SimplifyIntrinsics.cpp
flang/test/Transforms/simplifyintrinsics.fir

index 9de7ae1..581ba00 100644 (file)
@@ -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();
@@ -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 =
index 8b3086b..36585e2 100644 (file)
@@ -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>