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 ElementalKernelGenerator = std::function<hlfir::Entity(
362 mlir::Location, fir::FirOpBuilder &, mlir::ValueRange)>;
369hlfir::ElementalOp genElementalOp(
370 mlir::Location loc, fir::FirOpBuilder &builder, mlir::Type elementType,
371 mlir::Value shape, mlir::ValueRange typeParams,
372 const ElementalKernelGenerator &genKernel,
bool isUnordered =
false,
373 mlir::Value polymorphicMold = {}, mlir::Type exprType = mlir::Type{});
377 mlir::Operation *outerOp =
nullptr;
378 mlir::Block *body =
nullptr;
390 mlir::ValueRange extents,
bool isUnordered =
false,
391 bool emitWorkshareLoop =
false,
392 bool couldVectorize =
true);
394 mlir::Value shape,
bool isUnordered =
false,
395 bool emitWorkshareLoop =
false,
396 bool couldVectorize =
true) {
397 return genLoopNest(loc, builder, getIndexExtents(loc, builder, shape),
398 isUnordered, emitWorkshareLoop, couldVectorize);
407using ReductionLoopBodyGenerator = std::function<llvm::SmallVector<mlir::Value>(
429 mlir::ValueRange reductionInits,
const ReductionLoopBodyGenerator &genBody,
430 bool isUnordered =
false);
437hlfir::YieldElementOp inlineElementalOp(mlir::Location loc,
439 hlfir::ElementalOp elemental,
440 mlir::ValueRange oneBasedIndices);
449mlir::Value inlineElementalOp(
451 hlfir::ElementalOpInterface elemental, mlir::ValueRange oneBasedIndices,
452 mlir::IRMapping &mapper,
453 const std::function<
bool(hlfir::ElementalOp)> &mustRecursivelyInline);
465void genNoAliasArrayAssignment(
468 bool temporaryLHS =
false,
471 *scalarCombineAndAssign =
nullptr,
472 mlir::ArrayAttr accessGroups = {});
479void genNoAliasAssignment(
480 mlir::Location loc, fir::FirOpBuilder &builder, hlfir::Entity rhs,
481 hlfir::Entity lhs,
bool emitWorkshareLoop =
false,
482 bool temporaryLHS =
false,
483 std::function<
void(mlir::Location, fir::FirOpBuilder &, hlfir::Entity,
484 hlfir::Entity, mlir::ArrayAttr accessGroups)>
485 *scalarCombineAndAssign =
nullptr,
486 mlir::ArrayAttr accessGroups = {});
487inline void genNoAliasAssignment(
488 mlir::Location loc, fir::FirOpBuilder &builder, hlfir::Entity rhs,
489 hlfir::Entity lhs,
bool emitWorkshareLoop,
bool temporaryLHS,
490 std::function<
void(mlir::Location, fir::FirOpBuilder &, hlfir::Entity,
491 hlfir::Entity, mlir::ArrayAttr)>
492 scalarCombineAndAssign,
493 mlir::ArrayAttr accessGroups = {}) {
494 genNoAliasAssignment(loc, builder, rhs, lhs, emitWorkshareLoop, temporaryLHS,
495 &scalarCombineAndAssign, accessGroups);
502std::pair<hlfir::Entity, bool>
503computeEvaluateOpInNewTemp(mlir::Location, fir::FirOpBuilder &,
504 hlfir::EvaluateInMemoryOp evalInMem,
505 mlir::Value shape, mlir::ValueRange typeParams);
513void computeEvaluateOpIn(mlir::Location, fir::FirOpBuilder &,
514 hlfir::EvaluateInMemoryOp, mlir::Value storage);
516std::pair<fir::ExtendedValue, std::optional<hlfir::CleanupFunction>>
517convertToValue(mlir::Location loc, fir::FirOpBuilder &builder,
518 hlfir::Entity entity);
520std::pair<fir::ExtendedValue, std::optional<hlfir::CleanupFunction>>
521convertToAddress(mlir::Location loc, fir::FirOpBuilder &builder,
522 hlfir::Entity entity, mlir::Type targetType);
524std::pair<fir::ExtendedValue, std::optional<hlfir::CleanupFunction>>
525convertToBox(mlir::Location loc, fir::FirOpBuilder &builder,
526 hlfir::Entity entity, mlir::Type targetType,
unsigned corank = 0);
529hlfir::ElementalOp cloneToElementalOp(mlir::Location loc,
530 fir::FirOpBuilder &builder,
531 hlfir::ElementalAddrOp elementalAddrOp);
538bool elementalOpMustProduceTemp(hlfir::ElementalOp elemental);
549std::pair<hlfir::Entity, bool> createTempFromMold(mlir::Location loc,
550 fir::FirOpBuilder &builder,
554hlfir::Entity createStackTempFromMold(mlir::Location loc,
555 fir::FirOpBuilder &builder,
558hlfir::EntityWithAttributes convertCharacterKind(mlir::Location loc,
559 fir::FirOpBuilder &builder,
560 hlfir::Entity scalarChar,
576std::pair<hlfir::Entity, std::optional<hlfir::CleanupFunction>>
577genTypeAndKindConvert(mlir::Location loc, fir::FirOpBuilder &builder,
578 hlfir::Entity source, mlir::Type toType,
579 bool preserveLowerBounds);
583Entity loadElementAt(mlir::Location loc, fir::FirOpBuilder &builder,
584 Entity entity, mlir::ValueRange oneBasedIndices);
589llvm::SmallVector<mlir::Value, Fortran::common::maxRank>
590genExtentsVector(mlir::Location loc, fir::FirOpBuilder &builder,
Entity entity);
599Entity gen1DSection(mlir::Location loc, fir::FirOpBuilder &builder,
600 Entity array, int64_t dim,
601 mlir::ArrayRef<mlir::Value> extents,
602 mlir::ValueRange oneBasedIndices,
603 mlir::ArrayRef<mlir::Value> typeParams);
608bool designatePreservesContinuity(hlfir::DesignateOp op);
616bool isSimplyContiguous(mlir::Value base,
bool checkWhole =
true);
620bool isInsideHlfirWhereMaskedExpression(mlir::Region ®ion);
This class provides a shared interface for box and class types.
Definition FIRType.h:40
Definition BoxValue.h:480
Definition FIRBuilder.h:59
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:766
mlir::Type unwrapPassByRefType(mlir::Type t)
Definition FIRType.h:307
bool isBoxAddress(mlir::Type t)
Is t an address to fir.box or class type?
Definition FIRType.h:528
unsigned getBoxCorank(mlir::Type boxTy)
Get the corank from a !fir.box type.
Definition FIRType.cpp:488
bool isRecordWithTypeParameters(mlir::Type ty)
Return true iff ty is a RecordType with type parameters.
Definition FIRType.h:445
bool isa_trivial(mlir::Type t)
Definition FIRType.h:232
Definition AbstractConverter.h:32
Structure to describe a loop nest.
Definition HLFIRTools.h:376