19aeb7f03SValentin Clement //===-- VectorSubscripts.cpp -- Vector subscripts tools -------------------===//
29aeb7f03SValentin Clement //
39aeb7f03SValentin Clement // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
49aeb7f03SValentin Clement // See https://llvm.org/LICENSE.txt for license information.
59aeb7f03SValentin Clement // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
69aeb7f03SValentin Clement //
79aeb7f03SValentin Clement //===----------------------------------------------------------------------===//
89aeb7f03SValentin Clement //
99aeb7f03SValentin Clement // Coding style: https://mlir.llvm.org/getting_started/DeveloperGuide/
109aeb7f03SValentin Clement //
119aeb7f03SValentin Clement //===----------------------------------------------------------------------===//
129aeb7f03SValentin Clement 
139aeb7f03SValentin Clement #include "flang/Lower/VectorSubscripts.h"
149aeb7f03SValentin Clement #include "flang/Lower/AbstractConverter.h"
159aeb7f03SValentin Clement #include "flang/Lower/Support/Utils.h"
169aeb7f03SValentin Clement #include "flang/Optimizer/Builder/Character.h"
179aeb7f03SValentin Clement #include "flang/Optimizer/Builder/Complex.h"
189aeb7f03SValentin Clement #include "flang/Optimizer/Builder/FIRBuilder.h"
195b66cc10SValentin Clement #include "flang/Optimizer/Builder/Todo.h"
209aeb7f03SValentin Clement #include "flang/Semantics/expression.h"
219aeb7f03SValentin Clement 
229aeb7f03SValentin Clement namespace {
239aeb7f03SValentin Clement /// Helper class to lower a designator containing vector subscripts into a
249aeb7f03SValentin Clement /// lowered representation that can be worked with.
259aeb7f03SValentin Clement class VectorSubscriptBoxBuilder {
269aeb7f03SValentin Clement public:
VectorSubscriptBoxBuilder(mlir::Location loc,Fortran::lower::AbstractConverter & converter,Fortran::lower::StatementContext & stmtCtx)279aeb7f03SValentin Clement   VectorSubscriptBoxBuilder(mlir::Location loc,
289aeb7f03SValentin Clement                             Fortran::lower::AbstractConverter &converter,
299aeb7f03SValentin Clement                             Fortran::lower::StatementContext &stmtCtx)
309aeb7f03SValentin Clement       : converter{converter}, stmtCtx{stmtCtx}, loc{loc} {}
319aeb7f03SValentin Clement 
gen(const Fortran::lower::SomeExpr & expr)329aeb7f03SValentin Clement   Fortran::lower::VectorSubscriptBox gen(const Fortran::lower::SomeExpr &expr) {
339aeb7f03SValentin Clement     elementType = genDesignator(expr);
349aeb7f03SValentin Clement     return Fortran::lower::VectorSubscriptBox(
359aeb7f03SValentin Clement         std::move(loweredBase), std::move(loweredSubscripts),
369aeb7f03SValentin Clement         std::move(componentPath), substringBounds, elementType);
379aeb7f03SValentin Clement   }
389aeb7f03SValentin Clement 
399aeb7f03SValentin Clement private:
409aeb7f03SValentin Clement   using LoweredVectorSubscript =
419aeb7f03SValentin Clement       Fortran::lower::VectorSubscriptBox::LoweredVectorSubscript;
429aeb7f03SValentin Clement   using LoweredTriplet = Fortran::lower::VectorSubscriptBox::LoweredTriplet;
439aeb7f03SValentin Clement   using LoweredSubscript = Fortran::lower::VectorSubscriptBox::LoweredSubscript;
449aeb7f03SValentin Clement   using MaybeSubstring = Fortran::lower::VectorSubscriptBox::MaybeSubstring;
459aeb7f03SValentin Clement 
469aeb7f03SValentin Clement   /// genDesignator unwraps a Designator<T> and calls `gen` on what the
479aeb7f03SValentin Clement   /// designator actually contains.
489aeb7f03SValentin Clement   template <typename A>
genDesignator(const A &)499aeb7f03SValentin Clement   mlir::Type genDesignator(const A &) {
509aeb7f03SValentin Clement     fir::emitFatalError(loc, "expr must contain a designator");
519aeb7f03SValentin Clement   }
529aeb7f03SValentin Clement   template <typename T>
genDesignator(const Fortran::evaluate::Expr<T> & expr)539aeb7f03SValentin Clement   mlir::Type genDesignator(const Fortran::evaluate::Expr<T> &expr) {
549aeb7f03SValentin Clement     using ExprVariant = decltype(Fortran::evaluate::Expr<T>::u);
559aeb7f03SValentin Clement     using Designator = Fortran::evaluate::Designator<T>;
569aeb7f03SValentin Clement     if constexpr (Fortran::common::HasMember<Designator, ExprVariant>) {
579aeb7f03SValentin Clement       const auto &designator = std::get<Designator>(expr.u);
589aeb7f03SValentin Clement       return std::visit([&](const auto &x) { return gen(x); }, designator.u);
599aeb7f03SValentin Clement     } else {
609aeb7f03SValentin Clement       return std::visit([&](const auto &x) { return genDesignator(x); },
619aeb7f03SValentin Clement                         expr.u);
629aeb7f03SValentin Clement     }
639aeb7f03SValentin Clement   }
649aeb7f03SValentin Clement 
659aeb7f03SValentin Clement   // The gen(X) methods visit X to lower its base and subscripts and return the
669aeb7f03SValentin Clement   // type of X elements.
679aeb7f03SValentin Clement 
gen(const Fortran::evaluate::DataRef & dataRef)689aeb7f03SValentin Clement   mlir::Type gen(const Fortran::evaluate::DataRef &dataRef) {
699aeb7f03SValentin Clement     return std::visit([&](const auto &ref) -> mlir::Type { return gen(ref); },
709aeb7f03SValentin Clement                       dataRef.u);
719aeb7f03SValentin Clement   }
729aeb7f03SValentin Clement 
gen(const Fortran::evaluate::SymbolRef & symRef)739aeb7f03SValentin Clement   mlir::Type gen(const Fortran::evaluate::SymbolRef &symRef) {
749aeb7f03SValentin Clement     // Never visited because expr lowering is used to lowered the ranked
759aeb7f03SValentin Clement     // ArrayRef.
769aeb7f03SValentin Clement     fir::emitFatalError(
779aeb7f03SValentin Clement         loc, "expected at least one ArrayRef with vector susbcripts");
789aeb7f03SValentin Clement   }
799aeb7f03SValentin Clement 
gen(const Fortran::evaluate::Substring & substring)809aeb7f03SValentin Clement   mlir::Type gen(const Fortran::evaluate::Substring &substring) {
819aeb7f03SValentin Clement     // StaticDataObject::Pointer bases are constants and cannot be
829aeb7f03SValentin Clement     // subscripted, so the base must be a DataRef here.
839aeb7f03SValentin Clement     mlir::Type baseElementType =
849aeb7f03SValentin Clement         gen(std::get<Fortran::evaluate::DataRef>(substring.parent()));
859aeb7f03SValentin Clement     fir::FirOpBuilder &builder = converter.getFirOpBuilder();
869aeb7f03SValentin Clement     mlir::Type idxTy = builder.getIndexType();
879aeb7f03SValentin Clement     mlir::Value lb = genScalarValue(substring.lower());
889aeb7f03SValentin Clement     substringBounds.emplace_back(builder.createConvert(loc, idxTy, lb));
899aeb7f03SValentin Clement     if (const auto &ubExpr = substring.upper()) {
909aeb7f03SValentin Clement       mlir::Value ub = genScalarValue(*ubExpr);
919aeb7f03SValentin Clement       substringBounds.emplace_back(builder.createConvert(loc, idxTy, ub));
929aeb7f03SValentin Clement     }
939aeb7f03SValentin Clement     return baseElementType;
949aeb7f03SValentin Clement   }
959aeb7f03SValentin Clement 
gen(const Fortran::evaluate::ComplexPart & complexPart)969aeb7f03SValentin Clement   mlir::Type gen(const Fortran::evaluate::ComplexPart &complexPart) {
979aeb7f03SValentin Clement     auto complexType = gen(complexPart.complex());
989aeb7f03SValentin Clement     fir::FirOpBuilder &builder = converter.getFirOpBuilder();
999aeb7f03SValentin Clement     mlir::Type i32Ty = builder.getI32Type(); // llvm's GEP requires i32
1009aeb7f03SValentin Clement     mlir::Value offset = builder.createIntegerConstant(
1019aeb7f03SValentin Clement         loc, i32Ty,
1029aeb7f03SValentin Clement         complexPart.part() == Fortran::evaluate::ComplexPart::Part::RE ? 0 : 1);
1039aeb7f03SValentin Clement     componentPath.emplace_back(offset);
1049aeb7f03SValentin Clement     return fir::factory::Complex{builder, loc}.getComplexPartType(complexType);
1059aeb7f03SValentin Clement   }
1069aeb7f03SValentin Clement 
gen(const Fortran::evaluate::Component & component)1079aeb7f03SValentin Clement   mlir::Type gen(const Fortran::evaluate::Component &component) {
1089aeb7f03SValentin Clement     auto recTy = gen(component.base()).cast<fir::RecordType>();
1099aeb7f03SValentin Clement     const Fortran::semantics::Symbol &componentSymbol =
1109aeb7f03SValentin Clement         component.GetLastSymbol();
1119aeb7f03SValentin Clement     // Parent components will not be found here, they are not part
1129aeb7f03SValentin Clement     // of the FIR type and cannot be used in the path yet.
1139aeb7f03SValentin Clement     if (componentSymbol.test(Fortran::semantics::Symbol::Flag::ParentComp))
114*331145e6SValentin Clement       TODO(loc, "reference to parent component");
1159aeb7f03SValentin Clement     mlir::Type fldTy = fir::FieldType::get(&converter.getMLIRContext());
1169aeb7f03SValentin Clement     llvm::StringRef componentName = toStringRef(componentSymbol.name());
1179aeb7f03SValentin Clement     // Parameters threading in field_index is not yet very clear. We only
1189aeb7f03SValentin Clement     // have the ones of the ranked array ref at hand, but it looks like
1199aeb7f03SValentin Clement     // the fir.field_index expects the one of the direct base.
1209aeb7f03SValentin Clement     if (recTy.getNumLenParams() != 0)
1219aeb7f03SValentin Clement       TODO(loc, "threading length parameters in field index op");
1229aeb7f03SValentin Clement     fir::FirOpBuilder &builder = converter.getFirOpBuilder();
1239aeb7f03SValentin Clement     componentPath.emplace_back(builder.create<fir::FieldIndexOp>(
1249aeb7f03SValentin Clement         loc, fldTy, componentName, recTy, /*typeParams*/ llvm::None));
1259aeb7f03SValentin Clement     return fir::unwrapSequenceType(recTy.getType(componentName));
1269aeb7f03SValentin Clement   }
1279aeb7f03SValentin Clement 
gen(const Fortran::evaluate::ArrayRef & arrayRef)1289aeb7f03SValentin Clement   mlir::Type gen(const Fortran::evaluate::ArrayRef &arrayRef) {
1299aeb7f03SValentin Clement     auto isTripletOrVector =
1309aeb7f03SValentin Clement         [](const Fortran::evaluate::Subscript &subscript) -> bool {
1319aeb7f03SValentin Clement       return std::visit(
1329aeb7f03SValentin Clement           Fortran::common::visitors{
1339aeb7f03SValentin Clement               [](const Fortran::evaluate::IndirectSubscriptIntegerExpr &expr) {
1349aeb7f03SValentin Clement                 return expr.value().Rank() != 0;
1359aeb7f03SValentin Clement               },
1369aeb7f03SValentin Clement               [&](const Fortran::evaluate::Triplet &) { return true; }},
1379aeb7f03SValentin Clement           subscript.u);
1389aeb7f03SValentin Clement     };
1399aeb7f03SValentin Clement     if (llvm::any_of(arrayRef.subscript(), isTripletOrVector))
1409aeb7f03SValentin Clement       return genRankedArrayRefSubscriptAndBase(arrayRef);
1419aeb7f03SValentin Clement 
1429aeb7f03SValentin Clement     // This is a scalar ArrayRef (only scalar indexes), collect the indexes and
1439aeb7f03SValentin Clement     // visit the base that must contain another arrayRef with the vector
1449aeb7f03SValentin Clement     // subscript.
1459aeb7f03SValentin Clement     mlir::Type elementType = gen(namedEntityToDataRef(arrayRef.base()));
1469aeb7f03SValentin Clement     for (const Fortran::evaluate::Subscript &subscript : arrayRef.subscript()) {
1479aeb7f03SValentin Clement       const auto &expr =
1489aeb7f03SValentin Clement           std::get<Fortran::evaluate::IndirectSubscriptIntegerExpr>(
1499aeb7f03SValentin Clement               subscript.u);
1509aeb7f03SValentin Clement       componentPath.emplace_back(genScalarValue(expr.value()));
1519aeb7f03SValentin Clement     }
1529aeb7f03SValentin Clement     return elementType;
1539aeb7f03SValentin Clement   }
1549aeb7f03SValentin Clement 
1559aeb7f03SValentin Clement   /// Lower the subscripts and base of the ArrayRef that is an array (there must
1569aeb7f03SValentin Clement   /// be one since there is a vector subscript, and there can only be one
1579aeb7f03SValentin Clement   /// according to C925).
genRankedArrayRefSubscriptAndBase(const Fortran::evaluate::ArrayRef & arrayRef)1589aeb7f03SValentin Clement   mlir::Type genRankedArrayRefSubscriptAndBase(
1599aeb7f03SValentin Clement       const Fortran::evaluate::ArrayRef &arrayRef) {
1609aeb7f03SValentin Clement     // Lower the save the base
1619aeb7f03SValentin Clement     Fortran::lower::SomeExpr baseExpr = namedEntityToExpr(arrayRef.base());
1629aeb7f03SValentin Clement     loweredBase = converter.genExprAddr(baseExpr, stmtCtx);
1639aeb7f03SValentin Clement     // Lower and save the subscripts
1649aeb7f03SValentin Clement     fir::FirOpBuilder &builder = converter.getFirOpBuilder();
1659aeb7f03SValentin Clement     mlir::Type idxTy = builder.getIndexType();
1669aeb7f03SValentin Clement     mlir::Value one = builder.createIntegerConstant(loc, idxTy, 1);
1679aeb7f03SValentin Clement     for (const auto &subscript : llvm::enumerate(arrayRef.subscript())) {
1689aeb7f03SValentin Clement       std::visit(
1699aeb7f03SValentin Clement           Fortran::common::visitors{
1709aeb7f03SValentin Clement               [&](const Fortran::evaluate::IndirectSubscriptIntegerExpr &expr) {
1719aeb7f03SValentin Clement                 if (expr.value().Rank() == 0) {
1729aeb7f03SValentin Clement                   // Simple scalar subscript
1739aeb7f03SValentin Clement                   loweredSubscripts.emplace_back(genScalarValue(expr.value()));
1749aeb7f03SValentin Clement                 } else {
1759aeb7f03SValentin Clement                   // Vector subscript.
1769aeb7f03SValentin Clement                   // Remove conversion if any to avoid temp creation that may
1779aeb7f03SValentin Clement                   // have been added by the front-end to avoid the creation of a
1789aeb7f03SValentin Clement                   // temp array value.
1799aeb7f03SValentin Clement                   auto vector = converter.genExprAddr(
1809aeb7f03SValentin Clement                       ignoreEvConvert(expr.value()), stmtCtx);
1819aeb7f03SValentin Clement                   mlir::Value size =
1829aeb7f03SValentin Clement                       fir::factory::readExtent(builder, loc, vector, /*dim=*/0);
1839aeb7f03SValentin Clement                   size = builder.createConvert(loc, idxTy, size);
1849aeb7f03SValentin Clement                   loweredSubscripts.emplace_back(
1859aeb7f03SValentin Clement                       LoweredVectorSubscript{std::move(vector), size});
1869aeb7f03SValentin Clement                 }
1879aeb7f03SValentin Clement               },
1889aeb7f03SValentin Clement               [&](const Fortran::evaluate::Triplet &triplet) {
1899aeb7f03SValentin Clement                 mlir::Value lb, ub;
1909aeb7f03SValentin Clement                 if (const auto &lbExpr = triplet.lower())
1919aeb7f03SValentin Clement                   lb = genScalarValue(*lbExpr);
1929aeb7f03SValentin Clement                 else
1939aeb7f03SValentin Clement                   lb = fir::factory::readLowerBound(builder, loc, loweredBase,
1949aeb7f03SValentin Clement                                                     subscript.index(), one);
1959aeb7f03SValentin Clement                 if (const auto &ubExpr = triplet.upper())
1969aeb7f03SValentin Clement                   ub = genScalarValue(*ubExpr);
1979aeb7f03SValentin Clement                 else
1989aeb7f03SValentin Clement                   ub = fir::factory::readExtent(builder, loc, loweredBase,
1999aeb7f03SValentin Clement                                                 subscript.index());
2009aeb7f03SValentin Clement                 lb = builder.createConvert(loc, idxTy, lb);
2019aeb7f03SValentin Clement                 ub = builder.createConvert(loc, idxTy, ub);
2029aeb7f03SValentin Clement                 mlir::Value stride = genScalarValue(triplet.stride());
2039aeb7f03SValentin Clement                 stride = builder.createConvert(loc, idxTy, stride);
2049aeb7f03SValentin Clement                 loweredSubscripts.emplace_back(LoweredTriplet{lb, ub, stride});
2059aeb7f03SValentin Clement               },
2069aeb7f03SValentin Clement           },
2079aeb7f03SValentin Clement           subscript.value().u);
2089aeb7f03SValentin Clement     }
2099aeb7f03SValentin Clement     return fir::unwrapSequenceType(
2109aeb7f03SValentin Clement         fir::unwrapPassByRefType(fir::getBase(loweredBase).getType()));
2119aeb7f03SValentin Clement   }
2129aeb7f03SValentin Clement 
gen(const Fortran::evaluate::CoarrayRef &)2139aeb7f03SValentin Clement   mlir::Type gen(const Fortran::evaluate::CoarrayRef &) {
2149aeb7f03SValentin Clement     // Is this possible/legal ?
215*331145e6SValentin Clement     TODO(loc, "coarray ref with vector subscript in IO input");
2169aeb7f03SValentin Clement   }
2179aeb7f03SValentin Clement 
2189aeb7f03SValentin Clement   template <typename A>
genScalarValue(const A & expr)2199aeb7f03SValentin Clement   mlir::Value genScalarValue(const A &expr) {
2209aeb7f03SValentin Clement     return fir::getBase(converter.genExprValue(toEvExpr(expr), stmtCtx));
2219aeb7f03SValentin Clement   }
2229aeb7f03SValentin Clement 
2239aeb7f03SValentin Clement   Fortran::evaluate::DataRef
namedEntityToDataRef(const Fortran::evaluate::NamedEntity & namedEntity)2249aeb7f03SValentin Clement   namedEntityToDataRef(const Fortran::evaluate::NamedEntity &namedEntity) {
2259aeb7f03SValentin Clement     if (namedEntity.IsSymbol())
2269aeb7f03SValentin Clement       return Fortran::evaluate::DataRef{namedEntity.GetFirstSymbol()};
2279aeb7f03SValentin Clement     return Fortran::evaluate::DataRef{namedEntity.GetComponent()};
2289aeb7f03SValentin Clement   }
2299aeb7f03SValentin Clement 
2309aeb7f03SValentin Clement   Fortran::lower::SomeExpr
namedEntityToExpr(const Fortran::evaluate::NamedEntity & namedEntity)2319aeb7f03SValentin Clement   namedEntityToExpr(const Fortran::evaluate::NamedEntity &namedEntity) {
2329aeb7f03SValentin Clement     return Fortran::evaluate::AsGenericExpr(namedEntityToDataRef(namedEntity))
2339aeb7f03SValentin Clement         .value();
2349aeb7f03SValentin Clement   }
2359aeb7f03SValentin Clement 
2369aeb7f03SValentin Clement   Fortran::lower::AbstractConverter &converter;
2379aeb7f03SValentin Clement   Fortran::lower::StatementContext &stmtCtx;
2389aeb7f03SValentin Clement   mlir::Location loc;
2399aeb7f03SValentin Clement   /// Elements of VectorSubscriptBox being built.
2409aeb7f03SValentin Clement   fir::ExtendedValue loweredBase;
2419aeb7f03SValentin Clement   llvm::SmallVector<LoweredSubscript, 16> loweredSubscripts;
2429aeb7f03SValentin Clement   llvm::SmallVector<mlir::Value> componentPath;
2439aeb7f03SValentin Clement   MaybeSubstring substringBounds;
2449aeb7f03SValentin Clement   mlir::Type elementType;
2459aeb7f03SValentin Clement };
2469aeb7f03SValentin Clement } // namespace
2479aeb7f03SValentin Clement 
genVectorSubscriptBox(mlir::Location loc,Fortran::lower::AbstractConverter & converter,Fortran::lower::StatementContext & stmtCtx,const Fortran::lower::SomeExpr & expr)2489aeb7f03SValentin Clement Fortran::lower::VectorSubscriptBox Fortran::lower::genVectorSubscriptBox(
2499aeb7f03SValentin Clement     mlir::Location loc, Fortran::lower::AbstractConverter &converter,
2509aeb7f03SValentin Clement     Fortran::lower::StatementContext &stmtCtx,
2519aeb7f03SValentin Clement     const Fortran::lower::SomeExpr &expr) {
2529aeb7f03SValentin Clement   return VectorSubscriptBoxBuilder(loc, converter, stmtCtx).gen(expr);
2539aeb7f03SValentin Clement }
2549aeb7f03SValentin Clement 
2559aeb7f03SValentin Clement template <typename LoopType, typename Generator>
loopOverElementsBase(fir::FirOpBuilder & builder,mlir::Location loc,const Generator & elementalGenerator,mlir::Value initialCondition)2569aeb7f03SValentin Clement mlir::Value Fortran::lower::VectorSubscriptBox::loopOverElementsBase(
2579aeb7f03SValentin Clement     fir::FirOpBuilder &builder, mlir::Location loc,
2589aeb7f03SValentin Clement     const Generator &elementalGenerator,
2599aeb7f03SValentin Clement     [[maybe_unused]] mlir::Value initialCondition) {
2609aeb7f03SValentin Clement   mlir::Value shape = builder.createShape(loc, loweredBase);
2619aeb7f03SValentin Clement   mlir::Value slice = createSlice(builder, loc);
2629aeb7f03SValentin Clement 
2639aeb7f03SValentin Clement   // Create loop nest for triplets and vector subscripts in column
2649aeb7f03SValentin Clement   // major order.
2659aeb7f03SValentin Clement   llvm::SmallVector<mlir::Value> inductionVariables;
2669aeb7f03SValentin Clement   LoopType outerLoop;
2679aeb7f03SValentin Clement   for (auto [lb, ub, step] : genLoopBounds(builder, loc)) {
2689aeb7f03SValentin Clement     LoopType loop;
2699aeb7f03SValentin Clement     if constexpr (std::is_same_v<LoopType, fir::IterWhileOp>) {
2709aeb7f03SValentin Clement       loop =
2719aeb7f03SValentin Clement           builder.create<fir::IterWhileOp>(loc, lb, ub, step, initialCondition);
2729aeb7f03SValentin Clement       initialCondition = loop.getIterateVar();
2739aeb7f03SValentin Clement       if (!outerLoop)
2749aeb7f03SValentin Clement         outerLoop = loop;
2759aeb7f03SValentin Clement       else
2769aeb7f03SValentin Clement         builder.create<fir::ResultOp>(loc, loop.getResult(0));
2779aeb7f03SValentin Clement     } else {
2789aeb7f03SValentin Clement       loop =
2799aeb7f03SValentin Clement           builder.create<fir::DoLoopOp>(loc, lb, ub, step, /*unordered=*/false);
2809aeb7f03SValentin Clement       if (!outerLoop)
2819aeb7f03SValentin Clement         outerLoop = loop;
2829aeb7f03SValentin Clement     }
2839aeb7f03SValentin Clement     builder.setInsertionPointToStart(loop.getBody());
2849aeb7f03SValentin Clement     inductionVariables.push_back(loop.getInductionVar());
2859aeb7f03SValentin Clement   }
2869aeb7f03SValentin Clement   assert(outerLoop && !inductionVariables.empty() &&
2879aeb7f03SValentin Clement          "at least one loop should be created");
2889aeb7f03SValentin Clement 
2899aeb7f03SValentin Clement   fir::ExtendedValue elem =
2909aeb7f03SValentin Clement       getElementAt(builder, loc, shape, slice, inductionVariables);
2919aeb7f03SValentin Clement 
2929aeb7f03SValentin Clement   if constexpr (std::is_same_v<LoopType, fir::IterWhileOp>) {
2939aeb7f03SValentin Clement     auto res = elementalGenerator(elem);
2949aeb7f03SValentin Clement     builder.create<fir::ResultOp>(loc, res);
2959aeb7f03SValentin Clement     builder.setInsertionPointAfter(outerLoop);
2969aeb7f03SValentin Clement     return outerLoop.getResult(0);
2979aeb7f03SValentin Clement   } else {
2989aeb7f03SValentin Clement     elementalGenerator(elem);
2999aeb7f03SValentin Clement     builder.setInsertionPointAfter(outerLoop);
3009aeb7f03SValentin Clement     return {};
3019aeb7f03SValentin Clement   }
3029aeb7f03SValentin Clement }
3039aeb7f03SValentin Clement 
loopOverElements(fir::FirOpBuilder & builder,mlir::Location loc,const ElementalGenerator & elementalGenerator)3049aeb7f03SValentin Clement void Fortran::lower::VectorSubscriptBox::loopOverElements(
3059aeb7f03SValentin Clement     fir::FirOpBuilder &builder, mlir::Location loc,
3069aeb7f03SValentin Clement     const ElementalGenerator &elementalGenerator) {
3079aeb7f03SValentin Clement   mlir::Value initialCondition;
3089aeb7f03SValentin Clement   loopOverElementsBase<fir::DoLoopOp, ElementalGenerator>(
3099aeb7f03SValentin Clement       builder, loc, elementalGenerator, initialCondition);
3109aeb7f03SValentin Clement }
3119aeb7f03SValentin Clement 
loopOverElementsWhile(fir::FirOpBuilder & builder,mlir::Location loc,const ElementalGeneratorWithBoolReturn & elementalGenerator,mlir::Value initialCondition)3129aeb7f03SValentin Clement mlir::Value Fortran::lower::VectorSubscriptBox::loopOverElementsWhile(
3139aeb7f03SValentin Clement     fir::FirOpBuilder &builder, mlir::Location loc,
3149aeb7f03SValentin Clement     const ElementalGeneratorWithBoolReturn &elementalGenerator,
3159aeb7f03SValentin Clement     mlir::Value initialCondition) {
3169aeb7f03SValentin Clement   return loopOverElementsBase<fir::IterWhileOp,
3179aeb7f03SValentin Clement                               ElementalGeneratorWithBoolReturn>(
3189aeb7f03SValentin Clement       builder, loc, elementalGenerator, initialCondition);
3199aeb7f03SValentin Clement }
3209aeb7f03SValentin Clement 
3219aeb7f03SValentin Clement mlir::Value
createSlice(fir::FirOpBuilder & builder,mlir::Location loc)3229aeb7f03SValentin Clement Fortran::lower::VectorSubscriptBox::createSlice(fir::FirOpBuilder &builder,
3239aeb7f03SValentin Clement                                                 mlir::Location loc) {
3249aeb7f03SValentin Clement   mlir::Type idxTy = builder.getIndexType();
3259aeb7f03SValentin Clement   llvm::SmallVector<mlir::Value> triples;
3269aeb7f03SValentin Clement   mlir::Value one = builder.createIntegerConstant(loc, idxTy, 1);
3279aeb7f03SValentin Clement   auto undef = builder.create<fir::UndefOp>(loc, idxTy);
3289aeb7f03SValentin Clement   for (const LoweredSubscript &subscript : loweredSubscripts)
3299aeb7f03SValentin Clement     std::visit(Fortran::common::visitors{
3309aeb7f03SValentin Clement                    [&](const LoweredTriplet &triplet) {
3319aeb7f03SValentin Clement                      triples.emplace_back(triplet.lb);
3329aeb7f03SValentin Clement                      triples.emplace_back(triplet.ub);
3339aeb7f03SValentin Clement                      triples.emplace_back(triplet.stride);
3349aeb7f03SValentin Clement                    },
3359aeb7f03SValentin Clement                    [&](const LoweredVectorSubscript &vector) {
3369aeb7f03SValentin Clement                      triples.emplace_back(one);
3379aeb7f03SValentin Clement                      triples.emplace_back(vector.size);
3389aeb7f03SValentin Clement                      triples.emplace_back(one);
3399aeb7f03SValentin Clement                    },
3409aeb7f03SValentin Clement                    [&](const mlir::Value &i) {
3419aeb7f03SValentin Clement                      triples.emplace_back(i);
3429aeb7f03SValentin Clement                      triples.emplace_back(undef);
3439aeb7f03SValentin Clement                      triples.emplace_back(undef);
3449aeb7f03SValentin Clement                    },
3459aeb7f03SValentin Clement                },
3469aeb7f03SValentin Clement                subscript);
3479aeb7f03SValentin Clement   return builder.create<fir::SliceOp>(loc, triples, componentPath);
3489aeb7f03SValentin Clement }
3499aeb7f03SValentin Clement 
3509aeb7f03SValentin Clement llvm::SmallVector<std::tuple<mlir::Value, mlir::Value, mlir::Value>>
genLoopBounds(fir::FirOpBuilder & builder,mlir::Location loc)3519aeb7f03SValentin Clement Fortran::lower::VectorSubscriptBox::genLoopBounds(fir::FirOpBuilder &builder,
3529aeb7f03SValentin Clement                                                   mlir::Location loc) {
3539aeb7f03SValentin Clement   mlir::Type idxTy = builder.getIndexType();
3549aeb7f03SValentin Clement   mlir::Value one = builder.createIntegerConstant(loc, idxTy, 1);
3559aeb7f03SValentin Clement   mlir::Value zero = builder.createIntegerConstant(loc, idxTy, 0);
3569aeb7f03SValentin Clement   llvm::SmallVector<std::tuple<mlir::Value, mlir::Value, mlir::Value>> bounds;
3579aeb7f03SValentin Clement   size_t dimension = loweredSubscripts.size();
3589aeb7f03SValentin Clement   for (const LoweredSubscript &subscript : llvm::reverse(loweredSubscripts)) {
3599aeb7f03SValentin Clement     --dimension;
3609aeb7f03SValentin Clement     if (std::holds_alternative<mlir::Value>(subscript))
3619aeb7f03SValentin Clement       continue;
3629aeb7f03SValentin Clement     mlir::Value lb, ub, step;
3639aeb7f03SValentin Clement     if (const auto *triplet = std::get_if<LoweredTriplet>(&subscript)) {
3649aeb7f03SValentin Clement       mlir::Value extent = builder.genExtentFromTriplet(
3659aeb7f03SValentin Clement           loc, triplet->lb, triplet->ub, triplet->stride, idxTy);
3669aeb7f03SValentin Clement       mlir::Value baseLb = fir::factory::readLowerBound(
3679aeb7f03SValentin Clement           builder, loc, loweredBase, dimension, one);
3689aeb7f03SValentin Clement       baseLb = builder.createConvert(loc, idxTy, baseLb);
3699aeb7f03SValentin Clement       lb = baseLb;
3709aeb7f03SValentin Clement       ub = builder.create<mlir::arith::SubIOp>(loc, idxTy, extent, one);
3719aeb7f03SValentin Clement       ub = builder.create<mlir::arith::AddIOp>(loc, idxTy, ub, baseLb);
3729aeb7f03SValentin Clement       step = one;
3739aeb7f03SValentin Clement     } else {
3749aeb7f03SValentin Clement       const auto &vector = std::get<LoweredVectorSubscript>(subscript);
3759aeb7f03SValentin Clement       lb = zero;
3769aeb7f03SValentin Clement       ub = builder.create<mlir::arith::SubIOp>(loc, idxTy, vector.size, one);
3779aeb7f03SValentin Clement       step = one;
3789aeb7f03SValentin Clement     }
3799aeb7f03SValentin Clement     bounds.emplace_back(lb, ub, step);
3809aeb7f03SValentin Clement   }
3819aeb7f03SValentin Clement   return bounds;
3829aeb7f03SValentin Clement }
3839aeb7f03SValentin Clement 
getElementAt(fir::FirOpBuilder & builder,mlir::Location loc,mlir::Value shape,mlir::Value slice,mlir::ValueRange inductionVariables)3849aeb7f03SValentin Clement fir::ExtendedValue Fortran::lower::VectorSubscriptBox::getElementAt(
3859aeb7f03SValentin Clement     fir::FirOpBuilder &builder, mlir::Location loc, mlir::Value shape,
3869aeb7f03SValentin Clement     mlir::Value slice, mlir::ValueRange inductionVariables) {
3879aeb7f03SValentin Clement   /// Generate the indexes for the array_coor inside the loops.
3889aeb7f03SValentin Clement   mlir::Type idxTy = builder.getIndexType();
3899aeb7f03SValentin Clement   llvm::SmallVector<mlir::Value> indexes;
3909aeb7f03SValentin Clement   size_t inductionIdx = inductionVariables.size() - 1;
3919aeb7f03SValentin Clement   for (const LoweredSubscript &subscript : loweredSubscripts)
3929aeb7f03SValentin Clement     std::visit(Fortran::common::visitors{
3939aeb7f03SValentin Clement                    [&](const LoweredTriplet &triplet) {
3949aeb7f03SValentin Clement                      indexes.emplace_back(inductionVariables[inductionIdx--]);
3959aeb7f03SValentin Clement                    },
3969aeb7f03SValentin Clement                    [&](const LoweredVectorSubscript &vector) {
3979aeb7f03SValentin Clement                      mlir::Value vecIndex = inductionVariables[inductionIdx--];
3989aeb7f03SValentin Clement                      mlir::Value vecBase = fir::getBase(vector.vector);
3999aeb7f03SValentin Clement                      mlir::Type vecEleTy = fir::unwrapSequenceType(
4009aeb7f03SValentin Clement                          fir::unwrapPassByRefType(vecBase.getType()));
4019aeb7f03SValentin Clement                      mlir::Type refTy = builder.getRefType(vecEleTy);
4029aeb7f03SValentin Clement                      auto vecEltRef = builder.create<fir::CoordinateOp>(
4039aeb7f03SValentin Clement                          loc, refTy, vecBase, vecIndex);
4049aeb7f03SValentin Clement                      auto vecElt =
4059aeb7f03SValentin Clement                          builder.create<fir::LoadOp>(loc, vecEleTy, vecEltRef);
4069aeb7f03SValentin Clement                      indexes.emplace_back(
4079aeb7f03SValentin Clement                          builder.createConvert(loc, idxTy, vecElt));
4089aeb7f03SValentin Clement                    },
4099aeb7f03SValentin Clement                    [&](const mlir::Value &i) {
4109aeb7f03SValentin Clement                      indexes.emplace_back(builder.createConvert(loc, idxTy, i));
4119aeb7f03SValentin Clement                    },
4129aeb7f03SValentin Clement                },
4139aeb7f03SValentin Clement                subscript);
4149aeb7f03SValentin Clement   mlir::Type refTy = builder.getRefType(getElementType());
4159aeb7f03SValentin Clement   auto elementAddr = builder.create<fir::ArrayCoorOp>(
4169aeb7f03SValentin Clement       loc, refTy, fir::getBase(loweredBase), shape, slice, indexes,
4179aeb7f03SValentin Clement       fir::getTypeParams(loweredBase));
4189aeb7f03SValentin Clement   fir::ExtendedValue element = fir::factory::arraySectionElementToExtendedValue(
4199aeb7f03SValentin Clement       builder, loc, loweredBase, elementAddr, slice);
4209aeb7f03SValentin Clement   if (!substringBounds.empty()) {
4219aeb7f03SValentin Clement     const fir::CharBoxValue *charBox = element.getCharBox();
4229aeb7f03SValentin Clement     assert(charBox && "substring requires CharBox base");
4239aeb7f03SValentin Clement     fir::factory::CharacterExprHelper helper{builder, loc};
4249aeb7f03SValentin Clement     return helper.createSubstring(*charBox, substringBounds);
4259aeb7f03SValentin Clement   }
4269aeb7f03SValentin Clement   return element;
4279aeb7f03SValentin Clement }
428