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());
72 bool isProcedure()
const {
return isFortranProcedureValue(getType()); }
83 if (
auto seqTy = mlir::dyn_cast<fir::SequenceType>(type)) {
84 if (seqTy.hasUnknownShape())
86 return seqTy.getDimension();
88 if (
auto exprType = mlir::dyn_cast<hlfir::ExprType>(type))
89 return exprType.getRank();
92 bool isScalar()
const {
return !
isArray(); }
94 bool isPolymorphic()
const {
return hlfir::isPolymorphicType(getType()); }
96 mlir::Type getFortranElementType()
const {
97 return hlfir::getFortranElementType(getType());
99 mlir::Type getElementOrSequenceType()
const {
100 return hlfir::getFortranElementOrSequenceType(getType());
104 if (isBoxAddressOrValue())
105 return llvm::cast<fir::BaseBoxType>(fir::unwrapRefType(getType()));
108 return fir::BoxType::get(getElementOrSequenceType(), isVolatile, corank);
111 bool hasLengthParameters()
const {
112 mlir::Type eleTy = getFortranElementType();
113 return mlir::isa<fir::CharacterType>(eleTy) ||
117 bool isCharacter()
const {
118 return mlir::isa<fir::CharacterType>(getFortranElementType());
121 bool hasIntrinsicType()
const {
122 mlir::Type eleTy = getFortranElementType();
126 bool isDerivedWithLengthParameters()
const {
130 bool mayHaveNonDefaultLowerBounds()
const;
135 bool isSimplyContiguous()
const {
138 if (!hlfir::isBoxAddressOrValueType(getFirBase().getType()) || isScalar())
143 if (
auto varIface = getMaybeDereferencedVariableInterface())
144 return varIface.isAllocatable() || varIface.hasContiguousAttr();
148 fir::FortranVariableOpInterface getIfVariableInterface()
const {
149 return this->getDefiningOp<fir::FortranVariableOpInterface>();
155 fir::FortranVariableOpInterface
156 getMaybeDereferencedVariableInterface()
const {
157 mlir::Value base = *
this;
158 if (
auto loadOp = base.getDefiningOp<fir::LoadOp>())
159 base = loadOp.getMemref();
160 return base.getDefiningOp<fir::FortranVariableOpInterface>();
163 bool mayBeOptional()
const;
165 bool isParameter()
const {
166 auto varIface = getIfVariableInterface();
167 return varIface ? varIface.isParameter() :
false;
170 bool isAllocatable()
const {
171 auto varIface = getIfVariableInterface();
172 return varIface ? varIface.isAllocatable() :
false;
175 bool isPointer()
const {
176 auto varIface = getIfVariableInterface();
177 return varIface ? varIface.isPointer() :
false;
182 mlir::Value getBase()
const {
return *
this; }
191 mlir::Value getFirBase()
const;
199class EntityWithAttributes :
public Entity {
201 explicit EntityWithAttributes(mlir::Value value) : Entity(value) {
202 assert(isFortranEntityWithAttributes(value) &&
203 "must be a value representing a Fortran value or variable");
205 EntityWithAttributes(fir::FortranVariableOpInterface variable)
206 : Entity(variable) {}
207 fir::FortranVariableOpInterface getIfVariable()
const {
208 return getIfVariableInterface();
217using CleanupFunction = std::function<void()>;
218std::pair<fir::ExtendedValue, std::optional<CleanupFunction>>
220 Entity entity,
bool contiguousHint =
false,
221 bool keepScalarOptionalBoxed =
false);
228 fir::FortranVariableOpInterface fortranVariable,
229 bool forceHlfirBase =
false);
232fir::FortranVariableOpInterface
235 fir::FortranVariableFlagsAttr flags,
236 mlir::Value dummyScope =
nullptr, mlir::Value storage =
nullptr,
237 std::uint64_t storageOffset = 0,
238 cuf::DataAttributeAttr dataAttr = {},
unsigned dummyArgNo = 0);
246 llvm::StringRef name,
247 std::optional<mlir::NamedAttribute> attr = std::nullopt);
253mlir::Value genVariableRawAddress(mlir::Location loc,
273Entity loadTrivialScalar(mlir::Location loc, fir::FirOpBuilder &builder,
278hlfir::Entity derefPointersAndAllocatables(mlir::Location loc,
279 fir::FirOpBuilder &builder,
285hlfir::Entity getElementAt(mlir::Location loc, fir::FirOpBuilder &builder,
286 Entity entity, mlir::ValueRange oneBasedIndices);
288llvm::SmallVector<std::pair<mlir::Value, mlir::Value>>
289genBounds(mlir::Location loc, fir::FirOpBuilder &builder,
Entity entity);
292llvm::SmallVector<std::pair<mlir::Value, mlir::Value>>
293genBounds(mlir::Location loc, fir::FirOpBuilder &builder, mlir::Value shape);
297llvm::SmallVector<mlir::Value> genLowerbounds(mlir::Location loc,
298 fir::FirOpBuilder &builder,
299 mlir::Value shape,
unsigned rank);
302mlir::Value genShape(mlir::Location loc, fir::FirOpBuilder &builder,
307mlir::Value genExtent(mlir::Location loc, fir::FirOpBuilder &builder,
308 hlfir::Entity entity,
unsigned dim);
312mlir::Value genLBound(mlir::Location loc, fir::FirOpBuilder &builder,
313 hlfir::Entity entity,
unsigned dim);
317llvm::SmallVector<mlir::Value> getIndexExtents(mlir::Location loc,
318 fir::FirOpBuilder &builder,
323llvm::SmallVector<mlir::Value>
324getExplicitExtentsFromShape(mlir::Value shape, fir::FirOpBuilder &builder);
327void genLengthParameters(mlir::Location loc, fir::FirOpBuilder &builder,
329 llvm::SmallVectorImpl<mlir::Value> &result);
333mlir::Value genCharLength(mlir::Location loc, fir::FirOpBuilder &builder,
338std::optional<std::int64_t> getCharLengthIfConst(
Entity entity);
340mlir::Value genRank(mlir::Location loc, fir::FirOpBuilder &builder,
341 Entity entity, mlir::Type resultType);
347std::pair<mlir::Value, mlir::Value> genVariableFirBaseShapeAndParams(
348 mlir::Location loc, fir::FirOpBuilder &builder,
Entity entity,
349 llvm::SmallVectorImpl<mlir::Value> &typeParams);
354mlir::Type getVariableElementType(hlfir::Entity variable);
359mlir::Type getEntityElementType(hlfir::Entity entity);
361using ElementalKernelResult =
362 std::pair<hlfir::Entity, std::optional<hlfir::CleanupFunction>>;
363using ElementalKernelGenerator = std::function<ElementalKernelResult(
364 mlir::Location, fir::FirOpBuilder &, mlir::ValueRange)>;
372hlfir::ElementalOp genElementalOp(
373 mlir::Location loc, fir::FirOpBuilder &builder, mlir::Type elementType,
374 mlir::Value shape, mlir::ValueRange typeParams,
375 const ElementalKernelGenerator &genKernel,
bool isUnordered =
false,
376 mlir::Value polymorphicMold = {}, mlir::Type exprType = mlir::Type{});
380 mlir::Operation *outerOp =
nullptr;
381 mlir::Block *body =
nullptr;
393 mlir::ValueRange extents,
bool isUnordered =
false,
394 bool emitWorkshareLoop =
false,
395 bool couldVectorize =
true);
397 mlir::Value shape,
bool isUnordered =
false,
398 bool emitWorkshareLoop =
false,
399 bool couldVectorize =
true) {
400 return genLoopNest(loc, builder, getIndexExtents(loc, builder, shape),
401 isUnordered, emitWorkshareLoop, couldVectorize);
410using ReductionLoopBodyGenerator = std::function<llvm::SmallVector<mlir::Value>(
432 mlir::ValueRange reductionInits,
const ReductionLoopBodyGenerator &genBody,
433 bool isUnordered =
false);
441hlfir::YieldElementOp inlineElementalOp(mlir::Location loc,
443 hlfir::ElementalOp elemental,
444 mlir::ValueRange oneBasedIndices);
457mlir::Value inlineElementalOp(
459 hlfir::ElementalOpInterface elemental, mlir::ValueRange oneBasedIndices,
460 mlir::IRMapping &mapper,
461 const std::function<
bool(hlfir::ElementalOp)> &mustRecursivelyInline,
474void genNoAliasArrayAssignment(
477 bool temporaryLHS =
false,
480 *scalarCombineAndAssign =
nullptr,
481 mlir::ArrayAttr accessGroups = {});
488void genNoAliasAssignment(
489 mlir::Location loc, fir::FirOpBuilder &builder, hlfir::Entity rhs,
490 hlfir::Entity lhs,
bool emitWorkshareLoop =
false,
491 bool temporaryLHS =
false,
492 std::function<
void(mlir::Location, fir::FirOpBuilder &, hlfir::Entity,
493 hlfir::Entity, mlir::ArrayAttr accessGroups)>
494 *scalarCombineAndAssign =
nullptr,
495 mlir::ArrayAttr accessGroups = {});
496inline void genNoAliasAssignment(
497 mlir::Location loc, fir::FirOpBuilder &builder, hlfir::Entity rhs,
498 hlfir::Entity lhs,
bool emitWorkshareLoop,
bool temporaryLHS,
499 std::function<
void(mlir::Location, fir::FirOpBuilder &, hlfir::Entity,
500 hlfir::Entity, mlir::ArrayAttr)>
501 scalarCombineAndAssign,
502 mlir::ArrayAttr accessGroups = {}) {
503 genNoAliasAssignment(loc, builder, rhs, lhs, emitWorkshareLoop, temporaryLHS,
504 &scalarCombineAndAssign, accessGroups);
511std::pair<hlfir::Entity, bool>
512computeEvaluateOpInNewTemp(mlir::Location, fir::FirOpBuilder &,
513 hlfir::EvaluateInMemoryOp evalInMem,
514 mlir::Value shape, mlir::ValueRange typeParams);
522void computeEvaluateOpIn(mlir::Location, fir::FirOpBuilder &,
523 hlfir::EvaluateInMemoryOp, mlir::Value storage);
525std::pair<fir::ExtendedValue, std::optional<hlfir::CleanupFunction>>
526convertToValue(mlir::Location loc, fir::FirOpBuilder &builder,
527 hlfir::Entity entity);
529std::pair<fir::ExtendedValue, std::optional<hlfir::CleanupFunction>>
530convertToAddress(mlir::Location loc, fir::FirOpBuilder &builder,
531 hlfir::Entity entity, mlir::Type targetType);
533std::pair<fir::ExtendedValue, std::optional<hlfir::CleanupFunction>>
534convertToBox(mlir::Location loc, fir::FirOpBuilder &builder,
535 hlfir::Entity entity, mlir::Type targetType,
unsigned corank = 0);
538hlfir::ElementalOp cloneToElementalOp(mlir::Location loc,
539 fir::FirOpBuilder &builder,
540 hlfir::ElementalAddrOp elementalAddrOp);
547bool elementalOpMustProduceTemp(hlfir::ElementalOp elemental);
558std::pair<hlfir::Entity, bool> createTempFromMold(mlir::Location loc,
559 fir::FirOpBuilder &builder,
563hlfir::Entity createStackTempFromMold(mlir::Location loc,
564 fir::FirOpBuilder &builder,
567hlfir::EntityWithAttributes convertCharacterKind(mlir::Location loc,
568 fir::FirOpBuilder &builder,
569 hlfir::Entity scalarChar,
585std::pair<hlfir::Entity, std::optional<hlfir::CleanupFunction>>
586genTypeAndKindConvert(mlir::Location loc, fir::FirOpBuilder &builder,
587 hlfir::Entity source, mlir::Type toType,
588 bool preserveLowerBounds);
592Entity loadElementAt(mlir::Location loc, fir::FirOpBuilder &builder,
593 Entity entity, mlir::ValueRange oneBasedIndices);
598llvm::SmallVector<mlir::Value, Fortran::common::maxRank>
599genExtentsVector(mlir::Location loc, fir::FirOpBuilder &builder,
Entity entity);
608Entity gen1DSection(mlir::Location loc, fir::FirOpBuilder &builder,
609 Entity array, int64_t dim,
610 mlir::ArrayRef<mlir::Value> extents,
611 mlir::ValueRange oneBasedIndices,
612 mlir::ArrayRef<mlir::Value> typeParams);
617bool designatePreservesContinuity(hlfir::DesignateOp op);
625bool isSimplyContiguous(mlir::Value base,
bool checkWhole =
true);
629bool isInsideHlfirWhereMaskedExpression(mlir::Region ®ion);
This class provides a shared interface for box and class types.
Definition FIRType.h:40
Definition BoxValue.h:469
Definition FIRBuilder.h:66
Definition HLFIRTools.h:52
bool isAssumedRank() const
Is this an assumed ranked entity?
Definition HLFIRTools.h:78
int getRank() const
Return the rank of this entity or -1 if it is an assumed rank.
Definition HLFIRTools.h:81
bool isArray() const
Is this an array or an assumed ranked entity?
Definition HLFIRTools.h:75
bool isProcedure() const
Is this entity a procedure designator?
Definition HLFIRTools.h:72
fir::BaseBoxType getBoxType() const
Return the fir.class or fir.box type needed to describe this entity.
Definition HLFIRTools.h:103
Definition AbstractConverter.h:37
bool isa_volatile_type(mlir::Type t)
Definition FIRType.cpp:768
mlir::Type unwrapPassByRefType(mlir::Type t)
Definition FIRType.h:314
bool isBoxAddress(mlir::Type t)
Is t an address to fir.box or class type?
Definition FIRType.h:535
unsigned getBoxCorank(mlir::Type boxTy)
Get the corank from a !fir.box type.
Definition FIRType.cpp:490
bool isRecordWithTypeParameters(mlir::Type ty)
Return true iff ty is a RecordType with type parameters.
Definition FIRType.h:452
bool isa_trivial(mlir::Type t)
Definition FIRType.h:239
Definition AbstractConverter.h:32
Structure to describe a loop nest.
Definition HLFIRTools.h:379