Index: flang/lib/Optimizer/HLFIR/Transforms/LowerHLFIRIntrinsics.cpp =================================================================== --- flang/lib/Optimizer/HLFIR/Transforms/LowerHLFIRIntrinsics.cpp +++ flang/lib/Optimizer/HLFIR/Transforms/LowerHLFIRIntrinsics.cpp @@ -154,6 +154,9 @@ std::optional resultEntity; if (fir::isa_trivial(firBaseTy)) { + // Some intrinsics return i1 when the original operation + // produces fir.logical<>, so we may need to cast it. + firBase = builder.createConvert(loc, op->getResult(0).getType(), firBase); resultEntity = hlfir::EntityWithAttributes{firBase}; } else { resultEntity = Index: flang/test/HLFIR/dot_product-lowering.fir =================================================================== --- flang/test/HLFIR/dot_product-lowering.fir +++ flang/test/HLFIR/dot_product-lowering.fir @@ -20,8 +20,8 @@ // CHECK-DAG: %[[LHS_ARG:.*]] = fir.convert %[[LHS_VAR]]#1 : (!fir.box>) -> !fir.box // CHECK-DAG: %[[RHS_ARG:.*]] = fir.convert %[[RHS_VAR]]#1 : (!fir.box>) -> !fir.box -// CHECK: %[[NONE:.*]] = fir.call @_FortranADotProductInteger4(%[[LHS_ARG]], %[[RHS_ARG]], %[[LOC_STR:.*]], %[[LOC_N:.*]]) -// CHECK-NEXT: hlfir.assign %[[NONE]] to %[[RES_VAR]]#0 : i32, !fir.ref +// CHECK: %[[RES_VAL:.*]] = fir.call @_FortranADotProductInteger4(%[[LHS_ARG]], %[[RHS_ARG]], %[[LOC_STR:.*]], %[[LOC_N:.*]]) +// CHECK-NEXT: hlfir.assign %[[RES_VAL]] to %[[RES_VAR]]#0 : i32, !fir.ref // CHECK-NEXT: return // CHECK-NEXT: } @@ -44,8 +44,9 @@ // CHECK-DAG: %[[LHS_ARG:.*]] = fir.convert %[[LHS_VAR]]#1 : (!fir.box>>) -> !fir.box // CHECK-DAG: %[[RHS_ARG:.*]] = fir.convert %[[RHS_VAR]]#1 : (!fir.box>>) -> !fir.box -// CHECK: %[[NONE:.*]] = fir.call @_FortranADotProductLogical(%[[LHS_ARG]], %[[RHS_ARG]], %[[LOC_STR:.*]], %[[LOC_N:.*]]) -// CHECK-NEXT: hlfir.assign %[[NONE]] to %[[RES_VAR]]#0 : i1, !fir.ref> +// CHECK: %[[RES_VAL:.*]] = fir.call @_FortranADotProductLogical(%[[LHS_ARG]], %[[RHS_ARG]], %[[LOC_STR:.*]], %[[LOC_N:.*]]) +// CHECK-NEXT: %[[CAST:.*]] = fir.convert %[[RES_VAL]] : (i1) -> !fir.logical<4> +// CHECK-NEXT: hlfir.assign %[[CAST]] to %[[RES_VAR]]#0 : !fir.logical<4>, !fir.ref> // CHECK-NEXT: return // CHECK-NEXT: } @@ -74,7 +75,33 @@ // CHECK-DAG: %[[LHS_ARG:.*]] = fir.convert %[[LHS_BOX]] : (!fir.box>) -> !fir.box // CHECK-DAG: %[[RHS_ARG:.*]] = fir.convert %[[RHS_BOX]] : (!fir.box>) -> !fir.box -// CHECK: %[[NONE:.*]] = fir.call @_FortranADotProductInteger4(%[[LHS_ARG]], %[[RHS_ARG]], %[[LOC_STR:.*]], %[[LOC_N:.*]]) -// CHECK-NEXT: hlfir.assign %[[NONE]] to %[[RES_VAR]]#0 : i32, !fir.ref +// CHECK: %[[RES_VAL:.*]] = fir.call @_FortranADotProductInteger4(%[[LHS_ARG]], %[[RHS_ARG]], %[[LOC_STR:.*]], %[[LOC_N:.*]]) +// CHECK-NEXT: hlfir.assign %[[RES_VAL]] to %[[RES_VAR]]#0 : i32, !fir.ref // CHECK-NEXT: return -// CHECK-NEXT: } \ No newline at end of file +// CHECK-NEXT: } + +// Test that DOT_PRODUCT's i1 result is casted to !fir.logical. +// Otherwise, the using fir.store becomes invalid. +func.func @_QPdot_product4(%arg0: !fir.box>> {fir.bindc_name = "lhs"}, %arg1: !fir.box>> {fir.bindc_name = "rhs"}) { + %temp = fir.alloca !fir.logical<4> + %0:2 = hlfir.declare %arg0 {uniq_name = "_QFdot_product2Elhs"} : (!fir.box>>) -> (!fir.box>>, !fir.box>>) + %1:2 = hlfir.declare %arg1 {uniq_name = "_QFdot_product2Erhs"} : (!fir.box>>) -> (!fir.box>>, !fir.box>>) + %2 = hlfir.dot_product %0#0 %1#0 {fastmath = #arith.fastmath} : (!fir.box>>, !fir.box>>) -> !fir.logical<4> + fir.store %2 to %temp : !fir.ref> + return +} +// CHECK-LABEL: func.func @_QPdot_product4( +// CHECK-SAME: %[[VAL_0:.*]]: !fir.box>> {fir.bindc_name = "lhs"}, +// CHECK-SAME: %[[VAL_1:.*]]: !fir.box>> {fir.bindc_name = "rhs"}) { +// CHECK: %[[VAL_2:.*]] = fir.alloca !fir.logical<4> +// CHECK: %[[VAL_3:.*]]:2 = hlfir.declare %[[VAL_0]] {uniq_name = "_QFdot_product2Elhs"} : (!fir.box>>) -> (!fir.box>>, !fir.box>>) +// CHECK: %[[VAL_4:.*]]:2 = hlfir.declare %[[VAL_1]] {uniq_name = "_QFdot_product2Erhs"} : (!fir.box>>) -> (!fir.box>>, !fir.box>>) +// CHECK: %[[VAL_5:.*]] = fir.absent !fir.box> +// CHECK: %[[VAL_7:.*]] = arith.constant 84 : index +// CHECK: %[[VAL_9:.*]] = fir.convert %[[VAL_3]]#1 : (!fir.box>>) -> !fir.box +// CHECK: %[[VAL_10:.*]] = fir.convert %[[VAL_4]]#1 : (!fir.box>>) -> !fir.box +// CHECK: %[[VAL_12:.*]] = fir.call @_FortranADotProductLogical(%[[VAL_9]], %[[VAL_10]], %{{.*}}, %{{.*}}) fastmath : (!fir.box, !fir.box, !fir.ref, i32) -> i1 +// CHECK: %[[VAL_13:.*]] = fir.convert %[[VAL_12]] : (i1) -> !fir.logical<4> +// CHECK: fir.store %[[VAL_13]] to %[[VAL_2]] : !fir.ref> +// CHECK: return +// CHECK: }