FLANG
HLFIRTools.h
1//===-- HLFIRTools.h -- HLFIR tools -----------------------*- 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_OPTIMIZER_BUILDER_HLFIRTOOLS_H
14#define FORTRAN_OPTIMIZER_BUILDER_HLFIRTOOLS_H
15
16#include "flang/Optimizer/Builder/BoxValue.h"
17#include "flang/Optimizer/Dialect/FIROps.h"
18#include "flang/Optimizer/Dialect/FortranVariableInterface.h"
19#include "flang/Optimizer/HLFIR/HLFIRDialect.h"
20#include "flang/Optimizer/HLFIR/HLFIROps.h"
21#include <optional>
22
23namespace fir {
24class FirOpBuilder;
25}
26
27namespace mlir {
28class IRMapping;
29}
30
31namespace hlfir {
32
33class AssociateOp;
34class ElementalOp;
35class ElementalOpInterface;
36class ElementalAddrOp;
37class EvaluateInMemoryOp;
38class YieldElementOp;
39
42inline bool isFortranVariableWithAttributes(mlir::Value value) {
43 return value.getDefiningOp<fir::FortranVariableOpInterface>();
44}
45
48inline bool isFortranEntityWithAttributes(mlir::Value value) {
49 return isFortranValue(value) || isFortranVariableWithAttributes(value);
50}
51
52class Entity : public mlir::Value {
53public:
54 explicit Entity(mlir::Value value) : mlir::Value(value) {
55 assert(isFortranEntity(value) &&
56 "must be a value representing a Fortran value or variable like");
57 }
58 Entity(fir::FortranVariableOpInterface variable)
59 : mlir::Value(variable.getBase()) {}
60 bool isValue() const { return isFortranValue(*this); }
61 bool isVariable() const { return !isValue(); }
62 bool isMutableBox() const { return hlfir::isBoxAddressType(getType()); }
63 bool isProcedurePointer() const {
64 return hlfir::isFortranProcedurePointerType(getType());
65 }
66 bool isBoxAddressOrValue() const {
67 return hlfir::isBoxAddressOrValueType(getType());
68 }
69
71 bool isProcedure() const { return isFortranProcedureValue(getType()); }
72
74 bool isArray() const { return getRank() != 0; }
75
77 bool isAssumedRank() const { return getRank() == -1; }
78
80 int getRank() const {
81 mlir::Type type = fir::unwrapPassByRefType(fir::unwrapRefType(getType()));
82 if (auto seqTy = mlir::dyn_cast<fir::SequenceType>(type)) {
83 if (seqTy.hasUnknownShape())
84 return -1;
85 return seqTy.getDimension();
86 }
87 if (auto exprType = mlir::dyn_cast<hlfir::ExprType>(type))
88 return exprType.getRank();
89 return 0;
90 }
91 bool isScalar() const { return !isArray(); }
92
93 bool isPolymorphic() const { return hlfir::isPolymorphicType(getType()); }
94
95 mlir::Type getFortranElementType() const {
96 return hlfir::getFortranElementType(getType());
97 }
98 mlir::Type getElementOrSequenceType() const {
99 return hlfir::getFortranElementOrSequenceType(getType());
100 }
101
102 bool hasLengthParameters() const {
103 mlir::Type eleTy = getFortranElementType();
104 return mlir::isa<fir::CharacterType>(eleTy) ||
106 }
107
108 bool isCharacter() const {
109 return mlir::isa<fir::CharacterType>(getFortranElementType());
110 }
111
112 bool hasIntrinsicType() const {
113 mlir::Type eleTy = getFortranElementType();
114 return fir::isa_trivial(eleTy) || mlir::isa<fir::CharacterType>(eleTy);
115 }
116
117 bool isDerivedWithLengthParameters() const {
118 return fir::isRecordWithTypeParameters(getFortranElementType());
119 }
120
121 bool mayHaveNonDefaultLowerBounds() const;
122
123 // Is this entity known to be contiguous at compile time?
124 // Note that when this returns false, the entity may still
125 // turn-out to be contiguous at runtime.
126 bool isSimplyContiguous() const {
127 // If this can be described without a fir.box in FIR, this must
128 // be contiguous.
129 if (!hlfir::isBoxAddressOrValueType(getFirBase().getType()) || isScalar())
130 return true;
131 // Otherwise, if this entity has a visible declaration in FIR,
132 // or is the dereference of an allocatable or contiguous pointer
133 // it is simply contiguous.
134 if (auto varIface = getMaybeDereferencedVariableInterface())
135 return varIface.isAllocatable() || varIface.hasContiguousAttr();
136 return false;
137 }
138
139 fir::FortranVariableOpInterface getIfVariableInterface() const {
140 return this->getDefiningOp<fir::FortranVariableOpInterface>();
141 }
142
143 // Return a "declaration" operation for this variable if visible,
144 // or the "declaration" operation of the allocatable/pointer this
145 // variable was dereferenced from (if it is visible).
146 fir::FortranVariableOpInterface
147 getMaybeDereferencedVariableInterface() const {
148 mlir::Value base = *this;
149 if (auto loadOp = base.getDefiningOp<fir::LoadOp>())
150 base = loadOp.getMemref();
151 return base.getDefiningOp<fir::FortranVariableOpInterface>();
152 }
153
154 bool mayBeOptional() const;
155
156 bool isParameter() const {
157 auto varIface = getIfVariableInterface();
158 return varIface ? varIface.isParameter() : false;
159 }
160
161 bool isAllocatable() const {
162 auto varIface = getIfVariableInterface();
163 return varIface ? varIface.isAllocatable() : false;
164 }
165
166 bool isPointer() const {
167 auto varIface = getIfVariableInterface();
168 return varIface ? varIface.isPointer() : false;
169 }
170
171 // Get the entity as an mlir SSA value containing all the shape, type
172 // parameters and dynamic shape information.
173 mlir::Value getBase() const { return *this; }
174
175 // Get the entity as a FIR base. This may not carry the shape and type
176 // parameters information, and even when it is a box with shape information.
177 // it will not contain the local lower bounds of the entity. This should
178 // be used with care when generating FIR code that does not need this
179 // information, or has access to it in other ways. Its advantage is that
180 // it will never be a fir.box for explicit shape arrays, leading to simpler
181 // FIR code generation.
182 mlir::Value getFirBase() const;
183};
184
190class EntityWithAttributes : public Entity {
191public:
192 explicit EntityWithAttributes(mlir::Value value) : Entity(value) {
193 assert(isFortranEntityWithAttributes(value) &&
194 "must be a value representing a Fortran value or variable");
195 }
196 EntityWithAttributes(fir::FortranVariableOpInterface variable)
197 : Entity(variable) {}
198 fir::FortranVariableOpInterface getIfVariable() const {
199 return getIfVariableInterface();
200 }
201};
202
208using CleanupFunction = std::function<void()>;
209std::pair<fir::ExtendedValue, std::optional<CleanupFunction>>
210translateToExtendedValue(mlir::Location loc, fir::FirOpBuilder &builder,
211 Entity entity, bool contiguousHint = false,
212 bool keepScalarOptionalBoxed = false);
213
218translateToExtendedValue(mlir::Location loc, fir::FirOpBuilder &builder,
219 fir::FortranVariableOpInterface fortranVariable,
220 bool forceHlfirBase = false);
221
223fir::FortranVariableOpInterface
224genDeclare(mlir::Location loc, fir::FirOpBuilder &builder,
225 const fir::ExtendedValue &exv, llvm::StringRef name,
226 fir::FortranVariableFlagsAttr flags,
227 mlir::Value dummyScope = nullptr, mlir::Value storage = nullptr,
228 std::uint64_t storageOffset = 0,
229 cuf::DataAttributeAttr dataAttr = {});
230
234hlfir::AssociateOp
235genAssociateExpr(mlir::Location loc, fir::FirOpBuilder &builder,
236 hlfir::Entity value, mlir::Type variableType,
237 llvm::StringRef name,
238 std::optional<mlir::NamedAttribute> attr = std::nullopt);
239
244mlir::Value genVariableRawAddress(mlir::Location loc,
245 fir::FirOpBuilder &builder,
246 hlfir::Entity var);
247
250mlir::Value genVariableBoxChar(mlir::Location loc, fir::FirOpBuilder &builder,
251 hlfir::Entity var);
252
256hlfir::Entity genVariableBox(mlir::Location loc, fir::FirOpBuilder &builder,
257 hlfir::Entity var,
258 fir::BaseBoxType forceBoxType = {});
259
264Entity loadTrivialScalar(mlir::Location loc, fir::FirOpBuilder &builder,
265 Entity entity);
266
269hlfir::Entity derefPointersAndAllocatables(mlir::Location loc,
270 fir::FirOpBuilder &builder,
271 Entity entity);
272
276hlfir::Entity getElementAt(mlir::Location loc, fir::FirOpBuilder &builder,
277 Entity entity, mlir::ValueRange oneBasedIndices);
279llvm::SmallVector<std::pair<mlir::Value, mlir::Value>>
280genBounds(mlir::Location loc, fir::FirOpBuilder &builder, Entity entity);
283llvm::SmallVector<std::pair<mlir::Value, mlir::Value>>
284genBounds(mlir::Location loc, fir::FirOpBuilder &builder, mlir::Value shape);
285
288llvm::SmallVector<mlir::Value> genLowerbounds(mlir::Location loc,
289 fir::FirOpBuilder &builder,
290 mlir::Value shape, unsigned rank);
291
293mlir::Value genShape(mlir::Location loc, fir::FirOpBuilder &builder,
294 Entity entity);
295
298mlir::Value genExtent(mlir::Location loc, fir::FirOpBuilder &builder,
299 hlfir::Entity entity, unsigned dim);
300
303mlir::Value genLBound(mlir::Location loc, fir::FirOpBuilder &builder,
304 hlfir::Entity entity, unsigned dim);
305
308llvm::SmallVector<mlir::Value> getIndexExtents(mlir::Location loc,
309 fir::FirOpBuilder &builder,
310 mlir::Value shape);
311
314llvm::SmallVector<mlir::Value>
315getExplicitExtentsFromShape(mlir::Value shape, fir::FirOpBuilder &builder);
316
318void genLengthParameters(mlir::Location loc, fir::FirOpBuilder &builder,
319 Entity entity,
320 llvm::SmallVectorImpl<mlir::Value> &result);
321
324mlir::Value genCharLength(mlir::Location loc, fir::FirOpBuilder &builder,
325 Entity entity);
326
327mlir::Value genRank(mlir::Location loc, fir::FirOpBuilder &builder,
328 Entity entity, mlir::Type resultType);
329
334std::pair<mlir::Value, mlir::Value> genVariableFirBaseShapeAndParams(
335 mlir::Location loc, fir::FirOpBuilder &builder, Entity entity,
336 llvm::SmallVectorImpl<mlir::Value> &typeParams);
337
341mlir::Type getVariableElementType(hlfir::Entity variable);
346mlir::Type getEntityElementType(hlfir::Entity entity);
347
348using ElementalKernelGenerator = std::function<hlfir::Entity(
349 mlir::Location, fir::FirOpBuilder &, mlir::ValueRange)>;
356hlfir::ElementalOp genElementalOp(
357 mlir::Location loc, fir::FirOpBuilder &builder, mlir::Type elementType,
358 mlir::Value shape, mlir::ValueRange typeParams,
359 const ElementalKernelGenerator &genKernel, bool isUnordered = false,
360 mlir::Value polymorphicMold = {}, mlir::Type exprType = mlir::Type{});
361
363struct LoopNest {
364 mlir::Operation *outerOp = nullptr;
365 mlir::Block *body = nullptr;
366 llvm::SmallVector<mlir::Value> oneBasedIndices;
367};
368
376LoopNest genLoopNest(mlir::Location loc, fir::FirOpBuilder &builder,
377 mlir::ValueRange extents, bool isUnordered = false,
378 bool emitWorkshareLoop = false,
379 bool couldVectorize = true);
380inline LoopNest genLoopNest(mlir::Location loc, fir::FirOpBuilder &builder,
381 mlir::Value shape, bool isUnordered = false,
382 bool emitWorkshareLoop = false,
383 bool couldVectorize = true) {
384 return genLoopNest(loc, builder, getIndexExtents(loc, builder, shape),
385 isUnordered, emitWorkshareLoop, couldVectorize);
386}
387
394using ReductionLoopBodyGenerator = std::function<llvm::SmallVector<mlir::Value>(
395 mlir::Location, fir::FirOpBuilder &, mlir::ValueRange, mlir::ValueRange)>;
396
414llvm::SmallVector<mlir::Value> genLoopNestWithReductions(
415 mlir::Location loc, fir::FirOpBuilder &builder, mlir::ValueRange extents,
416 mlir::ValueRange reductionInits, const ReductionLoopBodyGenerator &genBody,
417 bool isUnordered = false);
418
424hlfir::YieldElementOp inlineElementalOp(mlir::Location loc,
425 fir::FirOpBuilder &builder,
426 hlfir::ElementalOp elemental,
427 mlir::ValueRange oneBasedIndices);
428
436mlir::Value inlineElementalOp(
437 mlir::Location loc, fir::FirOpBuilder &builder,
438 hlfir::ElementalOpInterface elemental, mlir::ValueRange oneBasedIndices,
439 mlir::IRMapping &mapper,
440 const std::function<bool(hlfir::ElementalOp)> &mustRecursivelyInline);
441
446std::pair<hlfir::Entity, bool>
447computeEvaluateOpInNewTemp(mlir::Location, fir::FirOpBuilder &,
448 hlfir::EvaluateInMemoryOp evalInMem,
449 mlir::Value shape, mlir::ValueRange typeParams);
450
451// Clone the body of the hlfir.eval_in_mem operating on this the provided
452// storage. The provided storage must be a contiguous "raw" memory reference
453// (not a fir.box) big enough to hold the value computed by hlfir.eval_in_mem.
454// No runtime check is inserted by this utility to enforce that. It is also
455// usually invalid to provide some storage that is already addressed directly
456// or indirectly inside the hlfir.eval_in_mem body.
457void computeEvaluateOpIn(mlir::Location, fir::FirOpBuilder &,
458 hlfir::EvaluateInMemoryOp, mlir::Value storage);
459
460std::pair<fir::ExtendedValue, std::optional<hlfir::CleanupFunction>>
461convertToValue(mlir::Location loc, fir::FirOpBuilder &builder,
462 hlfir::Entity entity);
463
464std::pair<fir::ExtendedValue, std::optional<hlfir::CleanupFunction>>
465convertToAddress(mlir::Location loc, fir::FirOpBuilder &builder,
466 hlfir::Entity entity, mlir::Type targetType);
467
468std::pair<fir::ExtendedValue, std::optional<hlfir::CleanupFunction>>
469convertToBox(mlir::Location loc, fir::FirOpBuilder &builder,
470 hlfir::Entity entity, mlir::Type targetType);
471
473hlfir::ElementalOp cloneToElementalOp(mlir::Location loc,
474 fir::FirOpBuilder &builder,
475 hlfir::ElementalAddrOp elementalAddrOp);
476
482bool elementalOpMustProduceTemp(hlfir::ElementalOp elemental);
483
484std::pair<hlfir::Entity, mlir::Value>
485createTempFromMold(mlir::Location loc, fir::FirOpBuilder &builder,
486 hlfir::Entity mold);
487
488// TODO: this does not support polymorphic molds
489hlfir::Entity createStackTempFromMold(mlir::Location loc,
490 fir::FirOpBuilder &builder,
491 hlfir::Entity mold);
492
493hlfir::EntityWithAttributes convertCharacterKind(mlir::Location loc,
494 fir::FirOpBuilder &builder,
495 hlfir::Entity scalarChar,
496 int toKind);
497
511std::pair<hlfir::Entity, std::optional<hlfir::CleanupFunction>>
512genTypeAndKindConvert(mlir::Location loc, fir::FirOpBuilder &builder,
513 hlfir::Entity source, mlir::Type toType,
514 bool preserveLowerBounds);
515
518Entity loadElementAt(mlir::Location loc, fir::FirOpBuilder &builder,
519 Entity entity, mlir::ValueRange oneBasedIndices);
520
525genExtentsVector(mlir::Location loc, fir::FirOpBuilder &builder, Entity entity);
526
534Entity gen1DSection(mlir::Location loc, fir::FirOpBuilder &builder,
535 Entity array, int64_t dim,
536 mlir::ArrayRef<mlir::Value> lbounds,
537 mlir::ArrayRef<mlir::Value> extents,
538 mlir::ValueRange oneBasedIndices,
539 mlir::ArrayRef<mlir::Value> typeParams);
540
544bool designatePreservesContinuity(hlfir::DesignateOp op);
545
552bool isSimplyContiguous(mlir::Value base, bool checkWhole = true);
553
554} // namespace hlfir
555
556#endif // FORTRAN_OPTIMIZER_BUILDER_HLFIRTOOLS_H
This class provides a shared interface for box and class types.
Definition FIRType.h:40
Definition BoxValue.h:478
Definition FIRBuilder.h:55
Definition HLFIRTools.h:190
Definition HLFIRTools.h:52
bool isAssumedRank() const
Is this an assumed ranked entity?
Definition HLFIRTools.h:77
int getRank() const
Return the rank of this entity or -1 if it is an assumed rank.
Definition HLFIRTools.h:80
bool isArray() const
Is this an array or an assumed ranked entity?
Definition HLFIRTools.h:74
bool isProcedure() const
Is this entity a procedure designator?
Definition HLFIRTools.h:71
Definition OpenACC.h:20
Definition AbstractConverter.h:34
mlir::Type unwrapPassByRefType(mlir::Type t)
Definition FIRType.h:289
bool isRecordWithTypeParameters(mlir::Type ty)
Return true iff ty is a RecordType with type parameters.
Definition FIRType.h:424
bool isa_trivial(mlir::Type t)
Definition FIRType.h:214
Definition AbstractConverter.h:29
Structure to describe a loop nest.
Definition HLFIRTools.h:363