13#ifndef FORTRAN_OPTIMIZER_BUILDER_HLFIRTOOLS_H
14#define FORTRAN_OPTIMIZER_BUILDER_HLFIRTOOLS_H
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"
35class ElementalOpInterface;
37class EvaluateInMemoryOp;
42inline bool isFortranVariableWithAttributes(mlir::Value value) {
43 return value.getDefiningOp<fir::FortranVariableOpInterface>();
48inline bool isFortranEntityWithAttributes(mlir::Value value) {
49 return isFortranValue(value) || isFortranVariableWithAttributes(value);
52class Entity :
public mlir::Value {
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");
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());
66 bool isBoxAddressOrValue()
const {
67 return hlfir::isBoxAddressOrValueType(getType());
71 bool isProcedure()
const {
return isFortranProcedureValue(getType()); }
82 if (
auto seqTy = mlir::dyn_cast<fir::SequenceType>(type)) {
83 if (seqTy.hasUnknownShape())
85 return seqTy.getDimension();
87 if (
auto exprType = mlir::dyn_cast<hlfir::ExprType>(type))
88 return exprType.getRank();
91 bool isScalar()
const {
return !
isArray(); }
93 bool isPolymorphic()
const {
return hlfir::isPolymorphicType(getType()); }
95 mlir::Type getFortranElementType()
const {
96 return hlfir::getFortranElementType(getType());
98 mlir::Type getElementOrSequenceType()
const {
99 return hlfir::getFortranElementOrSequenceType(getType());
102 bool hasLengthParameters()
const {
103 mlir::Type eleTy = getFortranElementType();
104 return mlir::isa<fir::CharacterType>(eleTy) ||
108 bool isCharacter()
const {
109 return mlir::isa<fir::CharacterType>(getFortranElementType());
112 bool hasIntrinsicType()
const {
113 mlir::Type eleTy = getFortranElementType();
117 bool isDerivedWithLengthParameters()
const {
121 bool mayHaveNonDefaultLowerBounds()
const;
126 bool isSimplyContiguous()
const {
129 if (!hlfir::isBoxAddressOrValueType(getFirBase().getType()) || isScalar())
134 if (
auto varIface = getMaybeDereferencedVariableInterface())
135 return varIface.isAllocatable() || varIface.hasContiguousAttr();
139 fir::FortranVariableOpInterface getIfVariableInterface()
const {
140 return this->getDefiningOp<fir::FortranVariableOpInterface>();
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>();
154 bool mayBeOptional()
const;
156 bool isParameter()
const {
157 auto varIface = getIfVariableInterface();
158 return varIface ? varIface.isParameter() :
false;
161 bool isAllocatable()
const {
162 auto varIface = getIfVariableInterface();
163 return varIface ? varIface.isAllocatable() :
false;
166 bool isPointer()
const {
167 auto varIface = getIfVariableInterface();
168 return varIface ? varIface.isPointer() :
false;
173 mlir::Value getBase()
const {
return *
this; }
182 mlir::Value getFirBase()
const;
190class EntityWithAttributes :
public Entity {
192 explicit EntityWithAttributes(mlir::Value value) : Entity(value) {
193 assert(isFortranEntityWithAttributes(value) &&
194 "must be a value representing a Fortran value or variable");
196 EntityWithAttributes(fir::FortranVariableOpInterface variable)
197 : Entity(variable) {}
198 fir::FortranVariableOpInterface getIfVariable()
const {
199 return getIfVariableInterface();
208using CleanupFunction = std::function<void()>;
209std::pair<fir::ExtendedValue, std::optional<CleanupFunction>>
211 Entity entity,
bool contiguousHint =
false,
212 bool keepScalarOptionalBoxed =
false);
219 fir::FortranVariableOpInterface fortranVariable,
220 bool forceHlfirBase =
false);
223fir::FortranVariableOpInterface
226 fir::FortranVariableFlagsAttr flags,
227 mlir::Value dummyScope =
nullptr, mlir::Value storage =
nullptr,
228 std::uint64_t storageOffset = 0,
229 cuf::DataAttributeAttr dataAttr = {});
237 llvm::StringRef name,
238 std::optional<mlir::NamedAttribute> attr = std::nullopt);
244mlir::Value genVariableRawAddress(mlir::Location loc,
264Entity loadTrivialScalar(mlir::Location loc, fir::FirOpBuilder &builder,
269hlfir::Entity derefPointersAndAllocatables(mlir::Location loc,
270 fir::FirOpBuilder &builder,
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);
288llvm::SmallVector<mlir::Value> genLowerbounds(mlir::Location loc,
289 fir::FirOpBuilder &builder,
290 mlir::Value shape,
unsigned rank);
293mlir::Value genShape(mlir::Location loc, fir::FirOpBuilder &builder,
298mlir::Value genExtent(mlir::Location loc, fir::FirOpBuilder &builder,
299 hlfir::Entity entity,
unsigned dim);
303mlir::Value genLBound(mlir::Location loc, fir::FirOpBuilder &builder,
304 hlfir::Entity entity,
unsigned dim);
308llvm::SmallVector<mlir::Value> getIndexExtents(mlir::Location loc,
309 fir::FirOpBuilder &builder,
314llvm::SmallVector<mlir::Value>
315getExplicitExtentsFromShape(mlir::Value shape, fir::FirOpBuilder &builder);
318void genLengthParameters(mlir::Location loc, fir::FirOpBuilder &builder,
320 llvm::SmallVectorImpl<mlir::Value> &result);
324mlir::Value genCharLength(mlir::Location loc, fir::FirOpBuilder &builder,
327mlir::Value genRank(mlir::Location loc, fir::FirOpBuilder &builder,
328 Entity entity, mlir::Type resultType);
334std::pair<mlir::Value, mlir::Value> genVariableFirBaseShapeAndParams(
335 mlir::Location loc, fir::FirOpBuilder &builder,
Entity entity,
336 llvm::SmallVectorImpl<mlir::Value> &typeParams);
341mlir::Type getVariableElementType(hlfir::Entity variable);
346mlir::Type getEntityElementType(hlfir::Entity entity);
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{});
364 mlir::Operation *outerOp =
nullptr;
365 mlir::Block *body =
nullptr;
377 mlir::ValueRange extents,
bool isUnordered =
false,
378 bool emitWorkshareLoop =
false,
379 bool couldVectorize =
true);
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);
394using ReductionLoopBodyGenerator = std::function<llvm::SmallVector<mlir::Value>(
416 mlir::ValueRange reductionInits,
const ReductionLoopBodyGenerator &genBody,
417 bool isUnordered =
false);
424hlfir::YieldElementOp inlineElementalOp(mlir::Location loc,
426 hlfir::ElementalOp elemental,
427 mlir::ValueRange oneBasedIndices);
436mlir::Value inlineElementalOp(
438 hlfir::ElementalOpInterface elemental, mlir::ValueRange oneBasedIndices,
439 mlir::IRMapping &mapper,
440 const std::function<
bool(hlfir::ElementalOp)> &mustRecursivelyInline);
446std::pair<hlfir::Entity, bool>
448 hlfir::EvaluateInMemoryOp evalInMem,
449 mlir::Value shape, mlir::ValueRange typeParams);
458 hlfir::EvaluateInMemoryOp, mlir::Value storage);
460std::pair<fir::ExtendedValue, std::optional<hlfir::CleanupFunction>>
464std::pair<fir::ExtendedValue, std::optional<hlfir::CleanupFunction>>
468std::pair<fir::ExtendedValue, std::optional<hlfir::CleanupFunction>>
473hlfir::ElementalOp cloneToElementalOp(mlir::Location loc,
475 hlfir::ElementalAddrOp elementalAddrOp);
482bool elementalOpMustProduceTemp(hlfir::ElementalOp elemental);
484std::pair<hlfir::Entity, mlir::Value>
511std::pair<hlfir::Entity, std::optional<hlfir::CleanupFunction>>
514 bool preserveLowerBounds);
519 Entity entity, mlir::ValueRange oneBasedIndices);
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);
544bool designatePreservesContinuity(hlfir::DesignateOp op);
552bool isSimplyContiguous(mlir::Value base,
bool checkWhole =
true);
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 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