Skip to content

Commit 9b1bd69

Browse files
committed
[flang][OpenMP] simplify getReductionName
Re-use fir::getTypeAsString instead of creating something new here. This spells integer names like i32 instead of i_32 so there is a lot of test churn.
1 parent 8eaefa5 commit 9b1bd69

37 files changed

+159
-189
lines changed

flang/lib/Lower/OpenMP/ReductionProcessor.cpp

Lines changed: 21 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,10 @@ bool ReductionProcessor::supportedIntrinsicProcReduction(
8383
return redType;
8484
}
8585

86-
std::string ReductionProcessor::getReductionName(llvm::StringRef name,
87-
mlir::Type ty, bool isByRef) {
86+
std::string
87+
ReductionProcessor::getReductionName(llvm::StringRef name,
88+
const fir::KindMapping &kindMap,
89+
mlir::Type ty, bool isByRef) {
8890
ty = fir::unwrapRefType(ty);
8991

9092
// extra string to distinguish reduction functions for variables passed by
@@ -93,47 +95,12 @@ std::string ReductionProcessor::getReductionName(llvm::StringRef name,
9395
if (isByRef)
9496
byrefAddition = "_byref";
9597

96-
if (fir::isa_trivial(ty))
97-
return (llvm::Twine(name) +
98-
(ty.isIntOrIndex() ? llvm::Twine("_i_") : llvm::Twine("_f_")) +
99-
llvm::Twine(ty.getIntOrFloatBitWidth()) + byrefAddition)
100-
.str();
101-
102-
// creates a name like reduction_i_64_box_ux4x3
103-
if (auto boxTy = mlir::dyn_cast_or_null<fir::BaseBoxType>(ty)) {
104-
// TODO: support for allocatable boxes:
105-
// !fir.box<!fir.heap<!fir.array<...>>>
106-
fir::SequenceType seqTy = fir::unwrapRefType(boxTy.getEleTy())
107-
.dyn_cast_or_null<fir::SequenceType>();
108-
if (!seqTy)
109-
return {};
110-
111-
std::string prefix = getReductionName(
112-
name, fir::unwrapSeqOrBoxedSeqType(ty), /*isByRef=*/false);
113-
if (prefix.empty())
114-
return {};
115-
std::stringstream tyStr;
116-
tyStr << prefix << "_box_";
117-
bool first = true;
118-
for (std::int64_t extent : seqTy.getShape()) {
119-
if (first)
120-
first = false;
121-
else
122-
tyStr << "x";
123-
if (extent == seqTy.getUnknownExtent())
124-
tyStr << 'u'; // I'm not sure that '?' is safe in symbol names
125-
else
126-
tyStr << extent;
127-
}
128-
return (tyStr.str() + byrefAddition).str();
129-
}
130-
131-
return {};
98+
return fir::getTypeAsString(ty, kindMap, (name + byrefAddition).str());
13299
}
133100

134101
std::string ReductionProcessor::getReductionName(
135102
Fortran::parser::DefinedOperator::IntrinsicOperator intrinsicOp,
136-
mlir::Type ty, bool isByRef) {
103+
const fir::KindMapping &kindMap, mlir::Type ty, bool isByRef) {
137104
std::string reductionName;
138105

139106
switch (intrinsicOp) {
@@ -156,17 +123,17 @@ std::string ReductionProcessor::getReductionName(
156123
break;
157124
}
158125

159-
return getReductionName(reductionName, ty, isByRef);
126+
return getReductionName(reductionName, kindMap, ty, isByRef);
160127
}
161128

162129
mlir::Value
163130
ReductionProcessor::getReductionInitValue(mlir::Location loc, mlir::Type type,
164131
ReductionIdentifier redId,
165132
fir::FirOpBuilder &builder) {
166133
type = fir::unwrapRefType(type);
167-
assert((fir::isa_integer(type) || fir::isa_real(type) ||
168-
type.isa<fir::LogicalType>()) &&
169-
"only integer, logical and real types are currently supported");
134+
if (!fir::isa_integer(type) && !fir::isa_real(type) &&
135+
!mlir::isa<fir::LogicalType>(type))
136+
TODO(loc, "Reduction of some types is not supported");
170137
switch (redId) {
171138
case ReductionIdentifier::MAX: {
172139
if (auto ty = type.dyn_cast<mlir::FloatType>()) {
@@ -465,8 +432,7 @@ mlir::omp::ReductionDeclareOp ReductionProcessor::createReductionDecl(
465432
mlir::OpBuilder::InsertionGuard guard(builder);
466433
mlir::ModuleOp module = builder.getModule();
467434

468-
if (reductionOpName.empty())
469-
TODO(loc, "Reduction of some types is not supported");
435+
assert(!reductionOpName.empty());
470436

471437
auto decl =
472438
module.lookupSymbol<mlir::omp::ReductionDeclareOp>(reductionOpName);
@@ -607,15 +573,18 @@ void ReductionProcessor::addReductionDecl(
607573
}
608574
for (mlir::Value symVal : reductionVars) {
609575
auto redType = mlir::cast<fir::ReferenceType>(symVal.getType());
576+
const auto &kindMap = firOpBuilder.getKindMap();
610577
if (redType.getEleTy().isa<fir::LogicalType>())
611-
decl = createReductionDecl(
612-
firOpBuilder,
613-
getReductionName(intrinsicOp, firOpBuilder.getI1Type(), isByRef),
614-
redId, redType, currentLocation, isByRef);
578+
decl = createReductionDecl(firOpBuilder,
579+
getReductionName(intrinsicOp, kindMap,
580+
firOpBuilder.getI1Type(),
581+
isByRef),
582+
redId, redType, currentLocation, isByRef);
615583
else
616584
decl = createReductionDecl(
617-
firOpBuilder, getReductionName(intrinsicOp, redType, isByRef),
618-
redId, redType, currentLocation, isByRef);
585+
firOpBuilder,
586+
getReductionName(intrinsicOp, kindMap, redType, isByRef), redId,
587+
redType, currentLocation, isByRef);
619588
reductionDeclSymbols.push_back(mlir::SymbolRefAttr::get(
620589
firOpBuilder.getContext(), decl.getSymName()));
621590
}
@@ -640,7 +609,7 @@ void ReductionProcessor::addReductionDecl(
640609
decl = createReductionDecl(
641610
firOpBuilder,
642611
getReductionName(getRealName(*reductionIntrinsic).ToString(),
643-
redType, isByRef),
612+
firOpBuilder.getKindMap(), redType, isByRef),
644613
redId, redType, currentLocation, isByRef);
645614
reductionDeclSymbols.push_back(mlir::SymbolRefAttr::get(
646615
firOpBuilder.getContext(), decl.getSymName()));

flang/lib/Lower/OpenMP/ReductionProcessor.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,13 @@ class ReductionProcessor {
7575
static bool
7676
doReductionByRef(const llvm::SmallVectorImpl<mlir::Value> &reductionVars);
7777

78-
static std::string getReductionName(llvm::StringRef name, mlir::Type ty,
79-
bool isByRef);
78+
static std::string getReductionName(llvm::StringRef name,
79+
const fir::KindMapping &kindMap,
80+
mlir::Type ty, bool isByRef);
8081

8182
static std::string getReductionName(
8283
Fortran::parser::DefinedOperator::IntrinsicOperator intrinsicOp,
83-
mlir::Type ty, bool isByRef);
84+
const fir::KindMapping &kindMap, mlir::Type ty, bool isByRef);
8485

8586
/// This function returns the identity value of the operator \p
8687
/// reductionOpName. For example:

flang/test/Lower/OpenMP/FIR/wsloop-reduction-add-byref.f90

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
! RUN: %flang_fc1 -emit-fir -flang-deprecated-no-hlfir -fopenmp -mmlir --force-byref-reduction %s -o - | FileCheck %s
33
! NOTE: Assertions have been autogenerated by utils/generate-test-checks.py
44

5-
! CHECK-LABEL: omp.reduction.declare @add_reduction_f_64_byref : !fir.ref<f64>
5+
! CHECK-LABEL: omp.reduction.declare @add_reduction_byref_f64 : !fir.ref<f64>
66
! CHECK-SAME: init {
77
! CHECK: ^bb0(%[[VAL_0:.*]]: !fir.ref<f64>):
88
! CHECK: %[[C0_1:.*]] = arith.constant 0.000000e+00 : f64
@@ -19,7 +19,7 @@
1919
! CHECK: omp.yield(%[[ARG0]] : !fir.ref<f64>)
2020
! CHECK: }
2121

22-
! CHECK-LABEL: omp.reduction.declare @add_reduction_i_64_byref : !fir.ref<i64>
22+
! CHECK-LABEL: omp.reduction.declare @add_reduction_byref_i64 : !fir.ref<i64>
2323
! CHECK-SAME: init {
2424
! CHECK: ^bb0(%[[VAL_0:.*]]: !fir.ref<i64>):
2525
! CHECK: %[[C0_1:.*]] = arith.constant 0 : i64
@@ -36,7 +36,7 @@
3636
! CHECK: omp.yield(%[[ARG0]] : !fir.ref<i64>)
3737
! CHECK: }
3838

39-
! CHECK-LABEL: omp.reduction.declare @add_reduction_f_32_byref : !fir.ref<f32>
39+
! CHECK-LABEL: omp.reduction.declare @add_reduction_byref_f32 : !fir.ref<f32>
4040
! CHECK-SAME: init {
4141
! CHECK: ^bb0(%[[VAL_0:.*]]: !fir.ref<f32>):
4242
! CHECK: %[[C0_1:.*]] = arith.constant 0.000000e+00 : f32
@@ -53,7 +53,7 @@
5353
! CHECK: omp.yield(%[[ARG0]] : !fir.ref<f32>)
5454
! CHECK: }
5555

56-
! CHECK-LABEL: omp.reduction.declare @add_reduction_i_32_byref : !fir.ref<i32>
56+
! CHECK-LABEL: omp.reduction.declare @add_reduction_byref_i32 : !fir.ref<i32>
5757
! CHECK-SAME: init {
5858
! CHECK: ^bb0(%[[VAL_0:.*]]: !fir.ref<i32>):
5959
! CHECK: %[[C0_1:.*]] = arith.constant 0 : i32
@@ -80,7 +80,7 @@
8080
! CHECK: %[[VAL_4:.*]] = arith.constant 1 : i32
8181
! CHECK: %[[VAL_5:.*]] = arith.constant 100 : i32
8282
! CHECK: %[[VAL_6:.*]] = arith.constant 1 : i32
83-
! CHECK: omp.wsloop byref reduction(@add_reduction_i_32_byref %[[VAL_1]] -> %[[VAL_7:.*]] : !fir.ref<i32>) for (%[[VAL_8:.*]]) : i32 = (%[[VAL_4]]) to (%[[VAL_5]]) inclusive step (%[[VAL_6]]) {
83+
! CHECK: omp.wsloop byref reduction(@add_reduction_byref_i32 %[[VAL_1]] -> %[[VAL_7:.*]] : !fir.ref<i32>) for (%[[VAL_8:.*]]) : i32 = (%[[VAL_4]]) to (%[[VAL_5]]) inclusive step (%[[VAL_6]]) {
8484
! CHECK: fir.store %[[VAL_8]] to %[[VAL_3]] : !fir.ref<i32>
8585
! CHECK: %[[VAL_9:.*]] = fir.load %[[VAL_7]] : !fir.ref<i32>
8686
! CHECK: %[[VAL_10:.*]] = fir.load %[[VAL_3]] : !fir.ref<i32>
@@ -116,7 +116,7 @@ subroutine simple_int_reduction
116116
! CHECK: %[[VAL_4:.*]] = arith.constant 1 : i32
117117
! CHECK: %[[VAL_5:.*]] = arith.constant 100 : i32
118118
! CHECK: %[[VAL_6:.*]] = arith.constant 1 : i32
119-
! CHECK: omp.wsloop byref reduction(@add_reduction_f_32_byref %[[VAL_1]] -> %[[VAL_7:.*]] : !fir.ref<f32>) for (%[[VAL_8:.*]]) : i32 = (%[[VAL_4]]) to (%[[VAL_5]]) inclusive step (%[[VAL_6]]) {
119+
! CHECK: omp.wsloop byref reduction(@add_reduction_byref_f32 %[[VAL_1]] -> %[[VAL_7:.*]] : !fir.ref<f32>) for (%[[VAL_8:.*]]) : i32 = (%[[VAL_4]]) to (%[[VAL_5]]) inclusive step (%[[VAL_6]]) {
120120
! CHECK: fir.store %[[VAL_8]] to %[[VAL_3]] : !fir.ref<i32>
121121
! CHECK: %[[VAL_9:.*]] = fir.load %[[VAL_7]] : !fir.ref<f32>
122122
! CHECK: %[[VAL_10:.*]] = fir.load %[[VAL_3]] : !fir.ref<i32>
@@ -152,7 +152,7 @@ subroutine simple_real_reduction
152152
! CHECK: %[[VAL_4:.*]] = arith.constant 1 : i32
153153
! CHECK: %[[VAL_5:.*]] = arith.constant 100 : i32
154154
! CHECK: %[[VAL_6:.*]] = arith.constant 1 : i32
155-
! CHECK: omp.wsloop byref reduction(@add_reduction_i_32_byref %[[VAL_1]] -> %[[VAL_7:.*]] : !fir.ref<i32>) for (%[[VAL_8:.*]]) : i32 = (%[[VAL_4]]) to (%[[VAL_5]]) inclusive step (%[[VAL_6]]) {
155+
! CHECK: omp.wsloop byref reduction(@add_reduction_byref_i32 %[[VAL_1]] -> %[[VAL_7:.*]] : !fir.ref<i32>) for (%[[VAL_8:.*]]) : i32 = (%[[VAL_4]]) to (%[[VAL_5]]) inclusive step (%[[VAL_6]]) {
156156
! CHECK: fir.store %[[VAL_8]] to %[[VAL_3]] : !fir.ref<i32>
157157
! CHECK: %[[VAL_9:.*]] = fir.load %[[VAL_3]] : !fir.ref<i32>
158158
! CHECK: %[[VAL_10:.*]] = fir.load %[[VAL_7]] : !fir.ref<i32>
@@ -187,7 +187,7 @@ subroutine simple_int_reduction_switch_order
187187
! CHECK: %[[VAL_4:.*]] = arith.constant 1 : i32
188188
! CHECK: %[[VAL_5:.*]] = arith.constant 100 : i32
189189
! CHECK: %[[VAL_6:.*]] = arith.constant 1 : i32
190-
! CHECK: omp.wsloop byref reduction(@add_reduction_f_32_byref %[[VAL_1]] -> %[[VAL_7:.*]] : !fir.ref<f32>) for (%[[VAL_8:.*]]) : i32 = (%[[VAL_4]]) to (%[[VAL_5]]) inclusive step (%[[VAL_6]]) {
190+
! CHECK: omp.wsloop byref reduction(@add_reduction_byref_f32 %[[VAL_1]] -> %[[VAL_7:.*]] : !fir.ref<f32>) for (%[[VAL_8:.*]]) : i32 = (%[[VAL_4]]) to (%[[VAL_5]]) inclusive step (%[[VAL_6]]) {
191191
! CHECK: fir.store %[[VAL_8]] to %[[VAL_3]] : !fir.ref<i32>
192192
! CHECK: %[[VAL_9:.*]] = fir.load %[[VAL_3]] : !fir.ref<i32>
193193
! CHECK: %[[VAL_10:.*]] = fir.convert %[[VAL_9]] : (i32) -> f32
@@ -229,7 +229,7 @@ subroutine simple_real_reduction_switch_order
229229
! CHECK: %[[VAL_8:.*]] = arith.constant 1 : i32
230230
! CHECK: %[[VAL_9:.*]] = arith.constant 100 : i32
231231
! CHECK: %[[VAL_10:.*]] = arith.constant 1 : i32
232-
! CHECK: omp.wsloop byref reduction(@add_reduction_i_32_byref %[[VAL_1]] -> %[[VAL_11:.*]] : !fir.ref<i32>, @add_reduction_i_32_byref %[[VAL_2]] -> %[[VAL_12:.*]] : !fir.ref<i32>, @add_reduction_i_32_byref %[[VAL_3]] -> %[[VAL_13:.*]] : !fir.ref<i32>) for (%[[VAL_14:.*]]) : i32 = (%[[VAL_8]]) to (%[[VAL_9]]) inclusive step (%[[VAL_10]]) {
232+
! CHECK: omp.wsloop byref reduction(@add_reduction_byref_i32 %[[VAL_1]] -> %[[VAL_11:.*]] : !fir.ref<i32>, @add_reduction_byref_i32 %[[VAL_2]] -> %[[VAL_12:.*]] : !fir.ref<i32>, @add_reduction_byref_i32 %[[VAL_3]] -> %[[VAL_13:.*]] : !fir.ref<i32>) for (%[[VAL_14:.*]]) : i32 = (%[[VAL_8]]) to (%[[VAL_9]]) inclusive step (%[[VAL_10]]) {
233233
! CHECK: fir.store %[[VAL_14]] to %[[VAL_7]] : !fir.ref<i32>
234234
! CHECK: %[[VAL_15:.*]] = fir.load %[[VAL_11]] : !fir.ref<i32>
235235
! CHECK: %[[VAL_16:.*]] = fir.load %[[VAL_7]] : !fir.ref<i32>
@@ -282,7 +282,7 @@ subroutine multiple_int_reductions_same_type
282282
! CHECK: %[[VAL_8:.*]] = arith.constant 1 : i32
283283
! CHECK: %[[VAL_9:.*]] = arith.constant 100 : i32
284284
! CHECK: %[[VAL_10:.*]] = arith.constant 1 : i32
285-
! CHECK: omp.wsloop byref reduction(@add_reduction_f_32_byref %[[VAL_1]] -> %[[VAL_11:.*]] : !fir.ref<f32>, @add_reduction_f_32_byref %[[VAL_2]] -> %[[VAL_12:.*]] : !fir.ref<f32>, @add_reduction_f_32_byref %[[VAL_3]] -> %[[VAL_13:.*]] : !fir.ref<f32>) for (%[[VAL_14:.*]]) : i32 = (%[[VAL_8]]) to (%[[VAL_9]]) inclusive step (%[[VAL_10]]) {
285+
! CHECK: omp.wsloop byref reduction(@add_reduction_byref_f32 %[[VAL_1]] -> %[[VAL_11:.*]] : !fir.ref<f32>, @add_reduction_byref_f32 %[[VAL_2]] -> %[[VAL_12:.*]] : !fir.ref<f32>, @add_reduction_byref_f32 %[[VAL_3]] -> %[[VAL_13:.*]] : !fir.ref<f32>) for (%[[VAL_14:.*]]) : i32 = (%[[VAL_8]]) to (%[[VAL_9]]) inclusive step (%[[VAL_10]]) {
286286
! CHECK: fir.store %[[VAL_14]] to %[[VAL_7]] : !fir.ref<i32>
287287
! CHECK: %[[VAL_15:.*]] = fir.load %[[VAL_11]] : !fir.ref<f32>
288288
! CHECK: %[[VAL_16:.*]] = fir.load %[[VAL_7]] : !fir.ref<i32>
@@ -341,7 +341,7 @@ subroutine multiple_real_reductions_same_type
341341
! CHECK: %[[VAL_10:.*]] = arith.constant 1 : i32
342342
! CHECK: %[[VAL_11:.*]] = arith.constant 100 : i32
343343
! CHECK: %[[VAL_12:.*]] = arith.constant 1 : i32
344-
! CHECK: omp.wsloop byref reduction(@add_reduction_i_32_byref %[[VAL_2]] -> %[[VAL_13:.*]] : !fir.ref<i32>, @add_reduction_i_64_byref %[[VAL_3]] -> %[[VAL_14:.*]] : !fir.ref<i64>, @add_reduction_f_32_byref %[[VAL_4]] -> %[[VAL_15:.*]] : !fir.ref<f32>, @add_reduction_f_64_byref %[[VAL_1]] -> %[[VAL_16:.*]] : !fir.ref<f64>) for (%[[VAL_17:.*]]) : i32 = (%[[VAL_10]]) to (%[[VAL_11]]) inclusive step (%[[VAL_12]]) {
344+
! CHECK: omp.wsloop byref reduction(@add_reduction_byref_i32 %[[VAL_2]] -> %[[VAL_13:.*]] : !fir.ref<i32>, @add_reduction_byref_i64 %[[VAL_3]] -> %[[VAL_14:.*]] : !fir.ref<i64>, @add_reduction_byref_f32 %[[VAL_4]] -> %[[VAL_15:.*]] : !fir.ref<f32>, @add_reduction_byref_f64 %[[VAL_1]] -> %[[VAL_16:.*]] : !fir.ref<f64>) for (%[[VAL_17:.*]]) : i32 = (%[[VAL_10]]) to (%[[VAL_11]]) inclusive step (%[[VAL_12]]) {
345345
! CHECK: fir.store %[[VAL_17]] to %[[VAL_9]] : !fir.ref<i32>
346346
! CHECK: %[[VAL_18:.*]] = fir.load %[[VAL_13]] : !fir.ref<i32>
347347
! CHECK: %[[VAL_19:.*]] = fir.load %[[VAL_9]] : !fir.ref<i32>

0 commit comments

Comments
 (0)