Skip to content

Commit

Permalink
[Flang] Remove kind from CountOp (#75466)
Browse files Browse the repository at this point in the history
The kind is already represented in the return type of the operation.
Like we did for minloc, this removes the kind parameter from CountOp.
  • Loading branch information
davemgreen committed Dec 15, 2023
1 parent 0ed0b74 commit 34eee5d
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 34 deletions.
7 changes: 3 additions & 4 deletions flang/include/flang/Optimizer/HLFIR/HLFIROps.td
Original file line number Diff line number Diff line change
Expand Up @@ -383,22 +383,21 @@ def hlfir_AnyOp : hlfir_Op<"any", [DeclareOpInterfaceMethods<MemoryEffectsOpInte
let hasVerifier = 1;
}

def hlfir_CountOp : hlfir_Op<"count", [AttrSizedOperandSegments, DeclareOpInterfaceMethods<MemoryEffectsOpInterface>]> {
def hlfir_CountOp : hlfir_Op<"count", [DeclareOpInterfaceMethods<MemoryEffectsOpInterface>]> {
let summary = "COUNT transformational intrinsic";
let description = [{
Takes a logical and counts the number of true values.
}];

let arguments = (ins
AnyFortranLogicalArrayObject:$mask,
Optional<AnyIntegerType>:$dim,
Optional<AnyIntegerType>:$kind
Optional<AnyIntegerType>:$dim
);

let results = (outs AnyFortranValue);

let assemblyFormat = [{
$mask (`dim` $dim^)? (`kind` $kind^)? attr-dict `:` functional-type(operands, results)
$mask (`dim` $dim^)? attr-dict `:` functional-type(operands, results)
}];

let hasVerifier = 1;
Expand Down
3 changes: 1 addition & 2 deletions flang/lib/Lower/HlfirIntrinsics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -373,9 +373,8 @@ mlir::Value HlfirCountLowering::lowerImpl(
mlir::Value dim = operands[1];
if (dim)
dim = hlfir::loadTrivialScalar(loc, builder, hlfir::Entity{dim});
mlir::Value kind = operands[2];
mlir::Type resultType = computeResultType(array, stmtResultType);
return createOp<hlfir::CountOp>(resultType, array, dim, kind);
return createOp<hlfir::CountOp>(resultType, array, dim);
}

mlir::Value HlfirCharExtremumLowering::lowerImpl(
Expand Down
18 changes: 13 additions & 5 deletions flang/lib/Optimizer/HLFIR/Transforms/LowerHLFIRIntrinsics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,14 @@ class HlfirIntrinsicConversion : public mlir::OpRewritePattern<OP> {
}
};

// Given an integer or array of integer type, calculate the Kind parameter from
// the width for use in runtime intrinsic calls.
static unsigned getKindForType(mlir::Type ty) {
mlir::Type eltty = hlfir::getFortranElementType(ty);
unsigned width = eltty.cast<mlir::IntegerType>().getWidth();
return width / 8;
}

template <class OP>
class HlfirReductionIntrinsicConversion : public HlfirIntrinsicConversion<OP> {
using HlfirIntrinsicConversion<OP>::HlfirIntrinsicConversion;
Expand Down Expand Up @@ -208,10 +216,8 @@ class HlfirReductionIntrinsicConversion : public HlfirIntrinsicConversion<OP> {
inArgs.push_back({operation.getArray(), operation.getArray().getType()});
inArgs.push_back({operation.getDim(), i32});
inArgs.push_back({operation.getMask(), logicalType});
mlir::Type T = hlfir::getFortranElementType(operation.getType());
unsigned width = T.cast<mlir::IntegerType>().getWidth();
mlir::Value kind =
builder.createIntegerConstant(operation->getLoc(), i32, width / 8);
mlir::Value kind = builder.createIntegerConstant(
operation->getLoc(), i32, getKindForType(operation.getType()));
inArgs.push_back({kind, i32});
inArgs.push_back({operation.getBack(), i32});
auto *argLowering = fir::getIntrinsicArgumentLowering(opName);
Expand Down Expand Up @@ -313,7 +319,9 @@ struct CountOpConversion : public HlfirIntrinsicConversion<hlfir::CountOp> {
llvm::SmallVector<IntrinsicArgument, 3> inArgs;
inArgs.push_back({count.getMask(), logicalType});
inArgs.push_back({count.getDim(), i32});
inArgs.push_back({count.getKind(), i32});
mlir::Value kind = builder.createIntegerConstant(
count->getLoc(), i32, getKindForType(count.getType()));
inArgs.push_back({kind, i32});

auto *argLowering = fir::getIntrinsicArgumentLowering("count");
llvm::SmallVector<fir::ExtendedValue, 3> args =
Expand Down
21 changes: 8 additions & 13 deletions flang/test/HLFIR/count-lowering-default-int-kinds.fir
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ module attributes {fir.defaultkind = "a1c4d8i8l4r4", fir.kindmap = ""} {
}
}
// CHECK-LABEL: func.func @test_i8
// CHECK: %[[KIND:.*]] = arith.constant 8 : index
// CHECK: %[[KIND_ARG:.*]] = fir.convert %[[KIND]] : (index) -> i32
// CHECK: fir.call @_FortranACountDim(%{{.*}}, %{{.*}}, %{{.*}}, %[[KIND_ARG]], %{{.*}}, %{{.*}}) : (!fir.ref<!fir.box<none>>, !fir.box<none>, i32, i32, !fir.ref<i8>, i32) -> none
// CHECK: %[[KIND:.*]] = arith.constant 8 : i32
// CHECK: fir.call @_FortranACountDim(%{{.*}}, %{{.*}}, %{{.*}}, %[[KIND]], %{{.*}}, %{{.*}}) : (!fir.ref<!fir.box<none>>, !fir.box<none>, i32, i32, !fir.ref<i8>, i32) -> none

module attributes {fir.defaultkind = "a1c4d8i4l4r4", fir.kindmap = ""} {
func.func @test_i4(%arg0: !fir.box<!fir.array<?x?x!fir.logical<4>>> {fir.bindc_name = "x"}, %arg1: i64) {
Expand All @@ -19,9 +18,8 @@ module attributes {fir.defaultkind = "a1c4d8i4l4r4", fir.kindmap = ""} {
}
}
// CHECK-LABEL: func.func @test_i4
// CHECK: %[[KIND:.*]] = arith.constant 4 : index
// CHECK: %[[KIND_ARG:.*]] = fir.convert %[[KIND]] : (index) -> i32
// CHECK: fir.call @_FortranACountDim(%{{.*}}, %{{.*}}, %{{.*}}, %[[KIND_ARG]], %{{.*}}, %{{.*}}) : (!fir.ref<!fir.box<none>>, !fir.box<none>, i32, i32, !fir.ref<i8>, i32) -> none
// CHECK: %[[KIND:.*]] = arith.constant 4 : i32
// CHECK: fir.call @_FortranACountDim(%{{.*}}, %{{.*}}, %{{.*}}, %[[KIND]], %{{.*}}, %{{.*}}) : (!fir.ref<!fir.box<none>>, !fir.box<none>, i32, i32, !fir.ref<i8>, i32) -> none

module attributes {fir.defaultkind = "a1c4d8i2l4r4", fir.kindmap = ""} {
func.func @test_i2(%arg0: !fir.box<!fir.array<?x?x!fir.logical<4>>> {fir.bindc_name = "x"}, %arg1: i64) {
Expand All @@ -30,9 +28,8 @@ module attributes {fir.defaultkind = "a1c4d8i2l4r4", fir.kindmap = ""} {
}
}
// CHECK-LABEL: func.func @test_i2
// CHECK: %[[KIND:.*]] = arith.constant 2 : index
// CHECK: %[[KIND_ARG:.*]] = fir.convert %[[KIND]] : (index) -> i32
// CHECK: fir.call @_FortranACountDim(%{{.*}}, %{{.*}}, %{{.*}}, %[[KIND_ARG]], %{{.*}}, %{{.*}}) : (!fir.ref<!fir.box<none>>, !fir.box<none>, i32, i32, !fir.ref<i8>, i32) -> none
// CHECK: %[[KIND:.*]] = arith.constant 2 : i32
// CHECK: fir.call @_FortranACountDim(%{{.*}}, %{{.*}}, %{{.*}}, %[[KIND]], %{{.*}}, %{{.*}}) : (!fir.ref<!fir.box<none>>, !fir.box<none>, i32, i32, !fir.ref<i8>, i32) -> none

module attributes {fir.defaultkind = "a1c4d8i1l4r4", fir.kindmap = ""} {
func.func @test_i1(%arg0: !fir.box<!fir.array<?x?x!fir.logical<4>>> {fir.bindc_name = "x"}, %arg1: i64) {
Expand All @@ -41,7 +38,5 @@ module attributes {fir.defaultkind = "a1c4d8i1l4r4", fir.kindmap = ""} {
}
}
// CHECK-LABEL: func.func @test_i1
// CHECK: arith.constant 1 : index
// CHECK: %[[KIND:.*]] = arith.constant 1 : index
// CHECK: %[[KIND_ARG:.*]] = fir.convert %[[KIND]] : (index) -> i32
// CHECK: fir.call @_FortranACountDim(%{{.*}}, %{{.*}}, %{{.*}}, %[[KIND_ARG]], %{{.*}}, %{{.*}}) : (!fir.ref<!fir.box<none>>, !fir.box<none>, i32, i32, !fir.ref<i8>, i32) -> none
// CHECK: %[[KIND:.*]] = arith.constant 1 : i32
// CHECK: fir.call @_FortranACountDim(%{{.*}}, %{{.*}}, %{{.*}}, %[[KIND]], %{{.*}}, %{{.*}}) : (!fir.ref<!fir.box<none>>, !fir.box<none>, i32, i32, !fir.ref<i8>, i32) -> none
13 changes: 5 additions & 8 deletions flang/test/HLFIR/count-lowering.fir
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ func.func @_QPcount2(%arg0: !fir.box<!fir.array<?x?x!fir.logical<4>>> {fir.bindc
// CHECK: %[[ARG0:.*]]: !fir.box<!fir.array<?x?x!fir.logical<4>>>
// CHECK: %[[ARG1:.*]]: !fir.box<!fir.array<?xi32>
// CHECK: %[[ARG2:.*]]: !fir.ref<i32>
// CHECK-DAG: %[[KIND:.*]] = arith.constant 4 : i32
// CHECK-DAG: %[[MASK:.*]]:2 = hlfir.declare %[[ARG0]]
// CHECK-DAG: %[[DIM_VAR:.*]]:2 = hlfir.declare %[[ARG2]]
// CHECK-DAG: %[[RES:.*]]:2 = hlfir.declare %[[ARG1]]

// CHECK-DAG: %[[RET_BOX:.*]] = fir.alloca !fir.box<!fir.heap<!fir.array<?xi32>>>
// CHECK-DAG: %[[KIND:.*]] = arith.constant 4 : index
// CHECK-DAG: %[[RET_ADDR:.*]] = fir.zero_bits !fir.heap<!fir.array<?xi32>>
// CHECK-DAG: %[[C0:.*]] = arith.constant 0 : index
// CHECK-DAG: %[[RET_SHAPE:.*]] = fir.shape %[[C0]] : (index) -> !fir.shape<1>
Expand All @@ -49,9 +49,8 @@ func.func @_QPcount2(%arg0: !fir.box<!fir.array<?x?x!fir.logical<4>>> {fir.bindc
// CHECK-DAG: %[[DIM:.*]] = fir.load %[[DIM_VAR]]#0 : !fir.ref<i32>
// CHECK-DAG: %[[RET_ARG:.*]] = fir.convert %[[RET_BOX]]
// CHECK-DAG: %[[MASK_ARG:.*]] = fir.convert %[[MASK]]#1
// CHECK-DAG: %[[KIND_ARG:.*]] = fir.convert %[[KIND]] : (index) -> i32

// CHECK: %[[NONE:.*]] = fir.call @_FortranACountDim(%[[RET_ARG]], %[[MASK_ARG]], %[[DIM]], %[[KIND_ARG]], %[[LOC_STR:.*]], %[[LOC_N:.*]]) : (!fir.ref<!fir.box<none>>, !fir.box<none>, i32, i32, !fir.ref<i8>, i32) -> none
// CHECK: %[[NONE:.*]] = fir.call @_FortranACountDim(%[[RET_ARG]], %[[MASK_ARG]], %[[DIM]], %[[KIND]], %[[LOC_STR:.*]], %[[LOC_N:.*]]) : (!fir.ref<!fir.box<none>>, !fir.box<none>, i32, i32, !fir.ref<i8>, i32) -> none
// CHECK: %[[RET:.*]] = fir.load %[[RET_BOX]]
// CHECK: %[[BOX_DIMS:.*]]:3 = fir.box_dims %[[RET]]
// CHECK-NEXT: %[[ADDR:.*]] = fir.box_addr %[[RET]]
Expand Down Expand Up @@ -82,7 +81,7 @@ func.func @_QPcount3(%arg0: !fir.ref<!fir.array<2xi32>> {fir.bindc_name = "s"})
// CHECK-LABEL: func.func @_QPcount3(
// CHECK: %[[ARG0:.*]]: !fir.ref<!fir.array<2xi32>>
// CHECK-DAG: %[[RET_BOX:.*]] = fir.alloca !fir.box<!fir.heap<!fir.array<?xi32>>>
// CHECK-DAG: %[[KIND:.*]] = arith.constant 4 : index
// CHECK-DAG: %[[KIND:.*]] = arith.constant 4 : i32
// CHECK-DAG: %[[RET_ADDR:.*]] = fir.zero_bits !fir.heap<!fir.array<?xi32>>
// CHECK-DAG: %[[C0:.*]] = arith.constant 0 : index
// CHECK-DAG: %[[RET_SHAPE:.*]] = fir.shape %[[C0]] : (index) -> !fir.shape<1>
Expand All @@ -98,9 +97,8 @@ func.func @_QPcount3(%arg0: !fir.ref<!fir.array<2xi32>> {fir.bindc_name = "s"})

// CHECK-DAG: %[[RET_ARG:.*]] = fir.convert %[[RET_BOX]]
// CHECK-DAG: %[[MASK_ARG:.*]] = fir.convert %[[MASK_BOX]] : (!fir.box<!fir.array<2x2x!fir.logical<4>>>) -> !fir.box<none>
// CHECK-DAG: %[[KIND_ARG:.*]] = fir.convert %[[KIND]] : (index) -> i32

// CHECK: %[[NONE:.*]] = fir.call @_FortranACountDim(%[[RET_ARG]], %[[MASK_ARG]], %[[DIM]], %[[KIND_ARG]], %[[LOC_STR:.*]], %[[LOC_N:.*]])
// CHECK: %[[NONE:.*]] = fir.call @_FortranACountDim(%[[RET_ARG]], %[[MASK_ARG]], %[[DIM]], %[[KIND]], %[[LOC_STR:.*]], %[[LOC_N:.*]])
// CHECK: %[[RET:.*]] = fir.load %[[RET_BOX]]
// CHECK: %[[BOX_DIMS:.*]]:3 = fir.box_dims %[[RET]]
// CHECK-NEXT: %[[ADDR:.*]] = fir.box_addr %[[RET]]
Expand All @@ -117,9 +115,8 @@ func.func @_QPcount4(%arg0: !fir.box<!fir.array<?x?x!fir.logical<4>>> {fir.bindc
%0:2 = hlfir.declare %arg0 {uniq_name = "_QFcount4Ea"} : (!fir.box<!fir.array<?x?x!fir.logical<4>>>) -> (!fir.box<!fir.array<?x?x!fir.logical<4>>>, !fir.box<!fir.array<?x?x!fir.logical<4>>>)
%1:2 = hlfir.declare %arg2 {uniq_name = "_QFcount4Ed"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
%2:2 = hlfir.declare %arg1 {uniq_name = "_QFcount4Es"} : (!fir.box<!fir.array<?xi32>>) -> (!fir.box<!fir.array<?xi32>>, !fir.box<!fir.array<?xi32>>)
%c8_i32 = arith.constant 8 : i32
%3 = fir.load %1#0 : !fir.ref<i32>
%4 = hlfir.count %0#0 dim %3 kind %c8_i32 {fastmath = #arith.fastmath<contract>} : (!fir.box<!fir.array<?x?x!fir.logical<4>>>, i32, i32) -> !hlfir.expr<?xi64>
%4 = hlfir.count %0#0 dim %3 {fastmath = #arith.fastmath<contract>} : (!fir.box<!fir.array<?x?x!fir.logical<4>>>, i32) -> !hlfir.expr<?xi64>
%5 = hlfir.shape_of %4 : (!hlfir.expr<?xi64>) -> !fir.shape<1>
%6 = hlfir.elemental %5 : (!fir.shape<1>) -> !hlfir.expr<?xi32> {
^bb0(%arg3: index):
Expand Down
3 changes: 1 addition & 2 deletions flang/test/Lower/HLFIR/count.f90
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,8 @@ subroutine count4(a, s, d)
! CHECK-DAG: %[[MASK:.*]]:2 = hlfir.declare %[[ARG0]]
! CHECK-DAG: %[[DIM_REF:.*]]:2 = hlfir.declare %[[ARG2]]
! CHECK-DAG: %[[OUT:.*]]:2 = hlfir.declare %[[ARG1]]
! CHECK-DAG: %[[C8:.*]] = arith.constant 8 : i32
! CHECK-DAG: %[[DIM:.*]] = fir.load %[[DIM_REF]]#0 : !fir.ref<i32>
! CHECK-DAG: %[[EXPR:.*]] = hlfir.count %[[MASK]]#0 dim %[[DIM]] kind %[[C8]] : (!fir.box<!fir.array<?x?x!fir.logical<4>>>, i32, i32) -> !hlfir.expr<?xi64>
! CHECK-DAG: %[[EXPR:.*]] = hlfir.count %[[MASK]]#0 dim %[[DIM]] : (!fir.box<!fir.array<?x?x!fir.logical<4>>>, i32) -> !hlfir.expr<?xi64>
! CHECK-DAG: %[[RES_SHAPE:.*]] = hlfir.shape_of %[[EXPR]]
! CHECK-DAG: %[[RES:.*]] = hlfir.elemental %[[RES_SHAPE]] unordered : (!fir.shape<1>) -> !hlfir.expr<?xi32>
! CHECK-DAG: hlfir.assign %[[RES]] to %[[OUT]]#0
Expand Down

0 comments on commit 34eee5d

Please sign in to comment.