Skip to content

Commit 34eee5d

Browse files
authored
[Flang] Remove kind from CountOp (#75466)
The kind is already represented in the return type of the operation. Like we did for minloc, this removes the kind parameter from CountOp.
1 parent 0ed0b74 commit 34eee5d

File tree

6 files changed

+31
-34
lines changed

6 files changed

+31
-34
lines changed

flang/include/flang/Optimizer/HLFIR/HLFIROps.td

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -383,22 +383,21 @@ def hlfir_AnyOp : hlfir_Op<"any", [DeclareOpInterfaceMethods<MemoryEffectsOpInte
383383
let hasVerifier = 1;
384384
}
385385

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

392392
let arguments = (ins
393393
AnyFortranLogicalArrayObject:$mask,
394-
Optional<AnyIntegerType>:$dim,
395-
Optional<AnyIntegerType>:$kind
394+
Optional<AnyIntegerType>:$dim
396395
);
397396

398397
let results = (outs AnyFortranValue);
399398

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

404403
let hasVerifier = 1;

flang/lib/Lower/HlfirIntrinsics.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -373,9 +373,8 @@ mlir::Value HlfirCountLowering::lowerImpl(
373373
mlir::Value dim = operands[1];
374374
if (dim)
375375
dim = hlfir::loadTrivialScalar(loc, builder, hlfir::Entity{dim});
376-
mlir::Value kind = operands[2];
377376
mlir::Type resultType = computeResultType(array, stmtResultType);
378-
return createOp<hlfir::CountOp>(resultType, array, dim, kind);
377+
return createOp<hlfir::CountOp>(resultType, array, dim);
379378
}
380379

381380
mlir::Value HlfirCharExtremumLowering::lowerImpl(

flang/lib/Optimizer/HLFIR/Transforms/LowerHLFIRIntrinsics.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,14 @@ class HlfirIntrinsicConversion : public mlir::OpRewritePattern<OP> {
181181
}
182182
};
183183

184+
// Given an integer or array of integer type, calculate the Kind parameter from
185+
// the width for use in runtime intrinsic calls.
186+
static unsigned getKindForType(mlir::Type ty) {
187+
mlir::Type eltty = hlfir::getFortranElementType(ty);
188+
unsigned width = eltty.cast<mlir::IntegerType>().getWidth();
189+
return width / 8;
190+
}
191+
184192
template <class OP>
185193
class HlfirReductionIntrinsicConversion : public HlfirIntrinsicConversion<OP> {
186194
using HlfirIntrinsicConversion<OP>::HlfirIntrinsicConversion;
@@ -208,10 +216,8 @@ class HlfirReductionIntrinsicConversion : public HlfirIntrinsicConversion<OP> {
208216
inArgs.push_back({operation.getArray(), operation.getArray().getType()});
209217
inArgs.push_back({operation.getDim(), i32});
210218
inArgs.push_back({operation.getMask(), logicalType});
211-
mlir::Type T = hlfir::getFortranElementType(operation.getType());
212-
unsigned width = T.cast<mlir::IntegerType>().getWidth();
213-
mlir::Value kind =
214-
builder.createIntegerConstant(operation->getLoc(), i32, width / 8);
219+
mlir::Value kind = builder.createIntegerConstant(
220+
operation->getLoc(), i32, getKindForType(operation.getType()));
215221
inArgs.push_back({kind, i32});
216222
inArgs.push_back({operation.getBack(), i32});
217223
auto *argLowering = fir::getIntrinsicArgumentLowering(opName);
@@ -313,7 +319,9 @@ struct CountOpConversion : public HlfirIntrinsicConversion<hlfir::CountOp> {
313319
llvm::SmallVector<IntrinsicArgument, 3> inArgs;
314320
inArgs.push_back({count.getMask(), logicalType});
315321
inArgs.push_back({count.getDim(), i32});
316-
inArgs.push_back({count.getKind(), i32});
322+
mlir::Value kind = builder.createIntegerConstant(
323+
count->getLoc(), i32, getKindForType(count.getType()));
324+
inArgs.push_back({kind, i32});
317325

318326
auto *argLowering = fir::getIntrinsicArgumentLowering("count");
319327
llvm::SmallVector<fir::ExtendedValue, 3> args =

flang/test/HLFIR/count-lowering-default-int-kinds.fir

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@ module attributes {fir.defaultkind = "a1c4d8i8l4r4", fir.kindmap = ""} {
88
}
99
}
1010
// CHECK-LABEL: func.func @test_i8
11-
// CHECK: %[[KIND:.*]] = arith.constant 8 : index
12-
// CHECK: %[[KIND_ARG:.*]] = fir.convert %[[KIND]] : (index) -> i32
13-
// CHECK: fir.call @_FortranACountDim(%{{.*}}, %{{.*}}, %{{.*}}, %[[KIND_ARG]], %{{.*}}, %{{.*}}) : (!fir.ref<!fir.box<none>>, !fir.box<none>, i32, i32, !fir.ref<i8>, i32) -> none
11+
// CHECK: %[[KIND:.*]] = arith.constant 8 : i32
12+
// CHECK: fir.call @_FortranACountDim(%{{.*}}, %{{.*}}, %{{.*}}, %[[KIND]], %{{.*}}, %{{.*}}) : (!fir.ref<!fir.box<none>>, !fir.box<none>, i32, i32, !fir.ref<i8>, i32) -> none
1413

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

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

3734
module attributes {fir.defaultkind = "a1c4d8i1l4r4", fir.kindmap = ""} {
3835
func.func @test_i1(%arg0: !fir.box<!fir.array<?x?x!fir.logical<4>>> {fir.bindc_name = "x"}, %arg1: i64) {
@@ -41,7 +38,5 @@ module attributes {fir.defaultkind = "a1c4d8i1l4r4", fir.kindmap = ""} {
4138
}
4239
}
4340
// CHECK-LABEL: func.func @test_i1
44-
// CHECK: arith.constant 1 : index
45-
// CHECK: %[[KIND:.*]] = arith.constant 1 : index
46-
// CHECK: %[[KIND_ARG:.*]] = fir.convert %[[KIND]] : (index) -> i32
47-
// CHECK: fir.call @_FortranACountDim(%{{.*}}, %{{.*}}, %{{.*}}, %[[KIND_ARG]], %{{.*}}, %{{.*}}) : (!fir.ref<!fir.box<none>>, !fir.box<none>, i32, i32, !fir.ref<i8>, i32) -> none
41+
// CHECK: %[[KIND:.*]] = arith.constant 1 : i32
42+
// CHECK: fir.call @_FortranACountDim(%{{.*}}, %{{.*}}, %{{.*}}, %[[KIND]], %{{.*}}, %{{.*}}) : (!fir.ref<!fir.box<none>>, !fir.box<none>, i32, i32, !fir.ref<i8>, i32) -> none

flang/test/HLFIR/count-lowering.fir

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ func.func @_QPcount2(%arg0: !fir.box<!fir.array<?x?x!fir.logical<4>>> {fir.bindc
3434
// CHECK: %[[ARG0:.*]]: !fir.box<!fir.array<?x?x!fir.logical<4>>>
3535
// CHECK: %[[ARG1:.*]]: !fir.box<!fir.array<?xi32>
3636
// CHECK: %[[ARG2:.*]]: !fir.ref<i32>
37+
// CHECK-DAG: %[[KIND:.*]] = arith.constant 4 : i32
3738
// CHECK-DAG: %[[MASK:.*]]:2 = hlfir.declare %[[ARG0]]
3839
// CHECK-DAG: %[[DIM_VAR:.*]]:2 = hlfir.declare %[[ARG2]]
3940
// CHECK-DAG: %[[RES:.*]]:2 = hlfir.declare %[[ARG1]]
4041

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

54-
// 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
53+
// 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
5554
// CHECK: %[[RET:.*]] = fir.load %[[RET_BOX]]
5655
// CHECK: %[[BOX_DIMS:.*]]:3 = fir.box_dims %[[RET]]
5756
// CHECK-NEXT: %[[ADDR:.*]] = fir.box_addr %[[RET]]
@@ -82,7 +81,7 @@ func.func @_QPcount3(%arg0: !fir.ref<!fir.array<2xi32>> {fir.bindc_name = "s"})
8281
// CHECK-LABEL: func.func @_QPcount3(
8382
// CHECK: %[[ARG0:.*]]: !fir.ref<!fir.array<2xi32>>
8483
// CHECK-DAG: %[[RET_BOX:.*]] = fir.alloca !fir.box<!fir.heap<!fir.array<?xi32>>>
85-
// CHECK-DAG: %[[KIND:.*]] = arith.constant 4 : index
84+
// CHECK-DAG: %[[KIND:.*]] = arith.constant 4 : i32
8685
// CHECK-DAG: %[[RET_ADDR:.*]] = fir.zero_bits !fir.heap<!fir.array<?xi32>>
8786
// CHECK-DAG: %[[C0:.*]] = arith.constant 0 : index
8887
// CHECK-DAG: %[[RET_SHAPE:.*]] = fir.shape %[[C0]] : (index) -> !fir.shape<1>
@@ -98,9 +97,8 @@ func.func @_QPcount3(%arg0: !fir.ref<!fir.array<2xi32>> {fir.bindc_name = "s"})
9897

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

103-
// CHECK: %[[NONE:.*]] = fir.call @_FortranACountDim(%[[RET_ARG]], %[[MASK_ARG]], %[[DIM]], %[[KIND_ARG]], %[[LOC_STR:.*]], %[[LOC_N:.*]])
101+
// CHECK: %[[NONE:.*]] = fir.call @_FortranACountDim(%[[RET_ARG]], %[[MASK_ARG]], %[[DIM]], %[[KIND]], %[[LOC_STR:.*]], %[[LOC_N:.*]])
104102
// CHECK: %[[RET:.*]] = fir.load %[[RET_BOX]]
105103
// CHECK: %[[BOX_DIMS:.*]]:3 = fir.box_dims %[[RET]]
106104
// CHECK-NEXT: %[[ADDR:.*]] = fir.box_addr %[[RET]]
@@ -117,9 +115,8 @@ func.func @_QPcount4(%arg0: !fir.box<!fir.array<?x?x!fir.logical<4>>> {fir.bindc
117115
%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>>>)
118116
%1:2 = hlfir.declare %arg2 {uniq_name = "_QFcount4Ed"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
119117
%2:2 = hlfir.declare %arg1 {uniq_name = "_QFcount4Es"} : (!fir.box<!fir.array<?xi32>>) -> (!fir.box<!fir.array<?xi32>>, !fir.box<!fir.array<?xi32>>)
120-
%c8_i32 = arith.constant 8 : i32
121118
%3 = fir.load %1#0 : !fir.ref<i32>
122-
%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>
119+
%4 = hlfir.count %0#0 dim %3 {fastmath = #arith.fastmath<contract>} : (!fir.box<!fir.array<?x?x!fir.logical<4>>>, i32) -> !hlfir.expr<?xi64>
123120
%5 = hlfir.shape_of %4 : (!hlfir.expr<?xi64>) -> !fir.shape<1>
124121
%6 = hlfir.elemental %5 : (!fir.shape<1>) -> !hlfir.expr<?xi32> {
125122
^bb0(%arg3: index):

flang/test/Lower/HLFIR/count.f90

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,8 @@ subroutine count4(a, s, d)
7070
! CHECK-DAG: %[[MASK:.*]]:2 = hlfir.declare %[[ARG0]]
7171
! CHECK-DAG: %[[DIM_REF:.*]]:2 = hlfir.declare %[[ARG2]]
7272
! CHECK-DAG: %[[OUT:.*]]:2 = hlfir.declare %[[ARG1]]
73-
! CHECK-DAG: %[[C8:.*]] = arith.constant 8 : i32
7473
! CHECK-DAG: %[[DIM:.*]] = fir.load %[[DIM_REF]]#0 : !fir.ref<i32>
75-
! 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>
74+
! CHECK-DAG: %[[EXPR:.*]] = hlfir.count %[[MASK]]#0 dim %[[DIM]] : (!fir.box<!fir.array<?x?x!fir.logical<4>>>, i32) -> !hlfir.expr<?xi64>
7675
! CHECK-DAG: %[[RES_SHAPE:.*]] = hlfir.shape_of %[[EXPR]]
7776
! CHECK-DAG: %[[RES:.*]] = hlfir.elemental %[[RES_SHAPE]] unordered : (!fir.shape<1>) -> !hlfir.expr<?xi32>
7877
! CHECK-DAG: hlfir.assign %[[RES]] to %[[OUT]]#0

0 commit comments

Comments
 (0)