Skip to content

Commit 3deaa77

Browse files
authored
[flang][OpenMP] simplify getReductionName (llvm#85666)
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 576d81b commit 3deaa77

38 files changed

+163
-213
lines changed

flang/lib/Lower/OpenMP/ReductionProcessor.cpp

Lines changed: 22 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,10 @@ bool ReductionProcessor::supportedIntrinsicProcReduction(
8181
return redType;
8282
}
8383

84-
std::string ReductionProcessor::getReductionName(llvm::StringRef name,
85-
mlir::Type ty, bool isByRef) {
84+
std::string
85+
ReductionProcessor::getReductionName(llvm::StringRef name,
86+
const fir::KindMapping &kindMap,
87+
mlir::Type ty, bool isByRef) {
8688
ty = fir::unwrapRefType(ty);
8789

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

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

13299
std::string ReductionProcessor::getReductionName(
133-
omp::clause::DefinedOperator::IntrinsicOperator intrinsicOp, mlir::Type ty,
134-
bool isByRef) {
100+
omp::clause::DefinedOperator::IntrinsicOperator intrinsicOp,
101+
const fir::KindMapping &kindMap, mlir::Type ty, bool isByRef) {
135102
std::string reductionName;
136103

137104
switch (intrinsicOp) {
@@ -154,17 +121,17 @@ std::string ReductionProcessor::getReductionName(
154121
break;
155122
}
156123

157-
return getReductionName(reductionName, ty, isByRef);
124+
return getReductionName(reductionName, kindMap, ty, isByRef);
158125
}
159126

160127
mlir::Value
161128
ReductionProcessor::getReductionInitValue(mlir::Location loc, mlir::Type type,
162129
ReductionIdentifier redId,
163130
fir::FirOpBuilder &builder) {
164131
type = fir::unwrapRefType(type);
165-
assert((fir::isa_integer(type) || fir::isa_real(type) ||
166-
type.isa<fir::LogicalType>()) &&
167-
"only integer, logical and real types are currently supported");
132+
if (!fir::isa_integer(type) && !fir::isa_real(type) &&
133+
!mlir::isa<fir::LogicalType>(type))
134+
TODO(loc, "Reduction of some types is not supported");
168135
switch (redId) {
169136
case ReductionIdentifier::MAX: {
170137
if (auto ty = type.dyn_cast<mlir::FloatType>()) {
@@ -463,8 +430,7 @@ mlir::omp::DeclareReductionOp ReductionProcessor::createDeclareReduction(
463430
mlir::OpBuilder::InsertionGuard guard(builder);
464431
mlir::ModuleOp module = builder.getModule();
465432

466-
if (reductionOpName.empty())
467-
TODO(loc, "Reduction of some types is not supported");
433+
assert(!reductionOpName.empty());
468434

469435
auto decl =
470436
module.lookupSymbol<mlir::omp::DeclareReductionOp>(reductionOpName);
@@ -601,15 +567,18 @@ void ReductionProcessor::addDeclareReduction(
601567

602568
for (mlir::Value symVal : reductionVars) {
603569
auto redType = mlir::cast<fir::ReferenceType>(symVal.getType());
570+
const auto &kindMap = firOpBuilder.getKindMap();
604571
if (redType.getEleTy().isa<fir::LogicalType>())
605-
decl = createDeclareReduction(
606-
firOpBuilder,
607-
getReductionName(intrinsicOp, firOpBuilder.getI1Type(), isByRef),
608-
redId, redType, currentLocation, isByRef);
572+
decl = createDeclareReduction(firOpBuilder,
573+
getReductionName(intrinsicOp, kindMap,
574+
firOpBuilder.getI1Type(),
575+
isByRef),
576+
redId, redType, currentLocation, isByRef);
609577
else
610578
decl = createDeclareReduction(
611-
firOpBuilder, getReductionName(intrinsicOp, redType, isByRef),
612-
redId, redType, currentLocation, isByRef);
579+
firOpBuilder,
580+
getReductionName(intrinsicOp, kindMap, redType, isByRef), redId,
581+
redType, currentLocation, isByRef);
613582
reductionDeclSymbols.push_back(mlir::SymbolRefAttr::get(
614583
firOpBuilder.getContext(), decl.getSymName()));
615584
}
@@ -631,7 +600,7 @@ void ReductionProcessor::addDeclareReduction(
631600
decl = createDeclareReduction(
632601
firOpBuilder,
633602
getReductionName(getRealName(*reductionIntrinsic).ToString(),
634-
redType, isByRef),
603+
firOpBuilder.getKindMap(), redType, isByRef),
635604
redId, redType, currentLocation, isByRef);
636605
reductionDeclSymbols.push_back(mlir::SymbolRefAttr::get(
637606
firOpBuilder.getContext(), decl.getSymName()));

flang/lib/Lower/OpenMP/ReductionProcessor.h

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

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

8283
static std::string
8384
getReductionName(omp::clause::DefinedOperator::IntrinsicOperator intrinsicOp,
84-
mlir::Type ty, bool isByRef);
85+
const fir::KindMapping &kindMap, mlir::Type ty,
86+
bool isByRef);
8587

8688
/// This function returns the identity value of the operator \p
8789
/// 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.declare_reduction @add_reduction_f_64_byref : !fir.ref<f64>
5+
! CHECK-LABEL: omp.declare_reduction @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.declare_reduction @add_reduction_i_64_byref : !fir.ref<i64>
22+
! CHECK-LABEL: omp.declare_reduction @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.declare_reduction @add_reduction_f_32_byref : !fir.ref<f32>
39+
! CHECK-LABEL: omp.declare_reduction @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.declare_reduction @add_reduction_i_32_byref : !fir.ref<i32>
56+
! CHECK-LABEL: omp.declare_reduction @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)