FLANG
ReductionProcessor.h
1//===-- Lower/OpenMP/ReductionProcessor.h -----------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Coding style: https://mlir.llvm.org/getting_started/DeveloperGuide/
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef FORTRAN_LOWER_REDUCTIONPROCESSOR_H
14#define FORTRAN_LOWER_REDUCTIONPROCESSOR_H
15
16#include "flang/Lower/OpenMP/Clauses.h"
17#include "flang/Optimizer/Builder/FIRBuilder.h"
18#include "flang/Optimizer/Dialect/FIRType.h"
19#include "flang/Parser/parse-tree.h"
20#include "flang/Semantics/symbol.h"
21#include "flang/Semantics/type.h"
22#include "mlir/IR/Location.h"
23#include "mlir/IR/Types.h"
24
25namespace mlir {
26namespace omp {
27class DeclareReductionOp;
28} // namespace omp
29} // namespace mlir
30
31namespace Fortran {
32namespace lower {
34} // namespace lower
35namespace semantics {
36class Scope;
38} // namespace semantics
39} // namespace Fortran
40
41namespace Fortran {
42namespace lower {
43namespace omp {
44
46public:
47 // ompOrig: mold/original variable
48 // ompPriv: private allocation (may be null for by-value reductions)
49 using GenInitValueCBTy = std::function<mlir::Value(
50 fir::FirOpBuilder &builder, mlir::Location loc, mlir::Type type,
51 mlir::Value ompOrig, mlir::Value ompPriv)>;
52 using GenCombinerCBTy = std::function<void(
53 fir::FirOpBuilder &builder, mlir::Location loc, mlir::Type type,
54 mlir::Value op1, mlir::Value op2, bool isByRef)>;
55
56 // TODO: Move this enumeration to the OpenMP dialect
57 enum ReductionIdentifier {
58 ID,
59 USER_DEF_OP,
60 ADD,
61 SUBTRACT,
62 MULTIPLY,
63 AND,
64 OR,
65 EQV,
66 NEQV,
67 MAX,
68 MIN,
69 IAND,
70 IOR,
71 IEOR
72 };
73
74 static bool doReductionByRef(mlir::Type reductionType);
75 static bool doReductionByRef(mlir::Value reductionVar);
76
77 static ReductionIdentifier
78 getReductionType(const omp::clause::ProcedureDesignator &pd);
79
80 static ReductionIdentifier
81 getReductionType(omp::clause::DefinedOperator::IntrinsicOperator intrinsicOp);
82
88 static parser::DefinedOperator::IntrinsicOperator
89 toParserIntrinsicOperator(omp::clause::DefinedOperator::IntrinsicOperator op);
90
91 static ReductionIdentifier
92 getReductionType(const fir::ReduceOperationEnum &pd);
93
94 static bool
95 supportedIntrinsicProcReduction(const omp::clause::ProcedureDesignator &pd);
96
101 const semantics::Scope &scope,
102 const omp::clause::ProcedureDesignator &reductionIntrinsic,
103 const semantics::DeclTypeSpec *type);
104
105 static const semantics::SourceName
106 getRealName(const semantics::Symbol *symbol);
107
108 static const semantics::SourceName
109 getRealName(const omp::clause::ProcedureDesignator &pd);
110
111 static std::string getReductionName(llvm::StringRef name,
112 const fir::KindMapping &kindMap,
113 mlir::Type ty, bool isByRef);
114
115 static std::string getReductionName(ReductionIdentifier redId,
116 const fir::KindMapping &kindMap,
117 mlir::Type ty, bool isByRef);
118
128 static std::string
130 const semantics::Symbol &reductionSymbol,
131 mlir::Type reductionType, bool isByRef);
132
137 static int getOperationIdentity(ReductionIdentifier redId,
138 mlir::Location loc);
139
140 static mlir::Value getReductionInitValue(mlir::Location loc, mlir::Type type,
141 ReductionIdentifier redId,
142 fir::FirOpBuilder &builder);
143
144 template <typename FloatOp, typename IntegerOp>
145 static mlir::Value getReductionOperation(fir::FirOpBuilder &builder,
146 mlir::Type type, mlir::Location loc,
147 mlir::Value op1, mlir::Value op2);
148 template <typename FloatOp, typename IntegerOp, typename ComplexOp>
149 static mlir::Value getReductionOperation(fir::FirOpBuilder &builder,
150 mlir::Type type, mlir::Location loc,
151 mlir::Value op1, mlir::Value op2);
152
153 static mlir::Value createScalarCombiner(fir::FirOpBuilder &builder,
154 mlir::Location loc,
155 ReductionIdentifier redId,
156 mlir::Type type, mlir::Value op1,
157 mlir::Value op2);
161 template <typename DeclareRedType>
162 static DeclareRedType createDeclareReductionHelper(
163 AbstractConverter &converter, llvm::StringRef reductionOpName,
164 mlir::Type type, mlir::Location loc, bool isByRef,
165 GenCombinerCBTy genCombinerCB, GenInitValueCBTy genInitValueCB,
166 const semantics::Symbol *sym = nullptr);
167
172 template <typename OpType>
173 static OpType createDeclareReduction(AbstractConverter &builder,
174 llvm::StringRef reductionOpName,
175 const ReductionIdentifier redId,
176 mlir::Type type, mlir::Location loc,
177 bool isByRef);
178
187 template <typename OpType, typename RedOperatorListTy>
188 static bool processReductionArguments(
189 mlir::Location currentLocation, lower::AbstractConverter &converter,
190 const RedOperatorListTy &redOperatorList,
191 llvm::SmallVectorImpl<mlir::Value> &reductionVars,
192 llvm::SmallVectorImpl<bool> &reduceVarByRef,
193 llvm::SmallVectorImpl<mlir::Attribute> &reductionDeclSymbols,
194 const llvm::SmallVectorImpl<const semantics::Symbol *> &reductionSymbols,
195 semantics::SemanticsContext *semaCtx = nullptr,
196 llvm::DenseMap<const semantics::Symbol *, mlir::Value>
197 *reductionVarCache = nullptr);
198};
199
200template <typename FloatOp, typename IntegerOp>
201mlir::Value
202ReductionProcessor::getReductionOperation(fir::FirOpBuilder &builder,
203 mlir::Type type, mlir::Location loc,
204 mlir::Value op1, mlir::Value op2) {
205 type = fir::unwrapRefType(type);
206 assert(type.isIntOrIndexOrFloat() &&
207 "only integer, float and complex types are currently supported");
208 if (type.isIntOrIndex())
209 return IntegerOp::create(builder, loc, op1, op2);
210 return FloatOp::create(builder, loc, op1, op2);
211}
212
213template <typename FloatOp, typename IntegerOp, typename ComplexOp>
214mlir::Value
215ReductionProcessor::getReductionOperation(fir::FirOpBuilder &builder,
216 mlir::Type type, mlir::Location loc,
217 mlir::Value op1, mlir::Value op2) {
218 assert((type.isIntOrIndexOrFloat() || fir::isa_complex(type)) &&
219 "only integer, float and complex types are currently supported");
220 if (type.isIntOrIndex())
221 return IntegerOp::create(builder, loc, op1, op2);
222 if (fir::isa_real(type))
223 return FloatOp::create(builder, loc, op1, op2);
224 return ComplexOp::create(builder, loc, op1, op2);
225}
226
227} // namespace omp
228} // namespace lower
229} // namespace Fortran
230
231#endif // FORTRAN_LOWER_REDUCTIONPROCESSOR_H
Definition AbstractConverter.h:87
Definition ReductionProcessor.h:45
static std::string getScopedUserReductionName(AbstractConverter &converter, const semantics::Symbol &reductionSymbol, mlir::Type reductionType, bool isByRef)
Definition ReductionProcessor.cpp:280
static int getOperationIdentity(ReductionIdentifier redId, mlir::Location loc)
Definition ReductionProcessor.cpp:1464
static OpType createDeclareReduction(AbstractConverter &builder, llvm::StringRef reductionOpName, const ReductionIdentifier redId, mlir::Type type, mlir::Location loc, bool isByRef)
Definition ReductionProcessor.cpp:710
static bool processReductionArguments(mlir::Location currentLocation, lower::AbstractConverter &converter, const RedOperatorListTy &redOperatorList, llvm::SmallVectorImpl< mlir::Value > &reductionVars, llvm::SmallVectorImpl< bool > &reduceVarByRef, llvm::SmallVectorImpl< mlir::Attribute > &reductionDeclSymbols, const llvm::SmallVectorImpl< const semantics::Symbol * > &reductionSymbols, semantics::SemanticsContext *semaCtx=nullptr, llvm::DenseMap< const semantics::Symbol *, mlir::Value > *reductionVarCache=nullptr)
Definition ReductionProcessor.cpp:915
static parser::DefinedOperator::IntrinsicOperator toParserIntrinsicOperator(omp::clause::DefinedOperator::IntrinsicOperator op)
Definition ReductionProcessor.cpp:129
static const semantics::Symbol * findUserDefinedReductionForIntrinsic(const semantics::Scope &scope, const omp::clause::ProcedureDesignator &reductionIntrinsic, const semantics::DeclTypeSpec *type)
Definition ReductionProcessor.cpp:199
static DeclareRedType createDeclareReductionHelper(AbstractConverter &converter, llvm::StringRef reductionOpName, mlir::Type type, mlir::Location loc, bool isByRef, GenCombinerCBTy genCombinerCB, GenInitValueCBTy genInitValueCB, const semantics::Symbol *sym=nullptr)
Definition ReductionProcessor.cpp:648
Definition scope.h:68
Definition semantics.h:67
Definition symbol.h:907
Definition FIRBuilder.h:66
Definition KindMapping.h:48
Definition ParserActions.h:24
Definition bit-population-count.h:20
bool isa_complex(mlir::Type t)
Is t a floating point complex type?
Definition FIRType.h:221
bool isa_real(mlir::Type t)
Is t a real type?
Definition FIRType.h:200
Definition AbstractConverter.h:32