1 //===-- Bridge.cpp -- bridge to lower to MLIR -----------------------------===//
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 #include "flang/Lower/Bridge.h"
14 #include "flang/Lower/Allocatable.h"
15 #include "flang/Lower/CallInterface.h"
16 #include "flang/Lower/Coarray.h"
17 #include "flang/Lower/ConvertExpr.h"
18 #include "flang/Lower/ConvertType.h"
19 #include "flang/Lower/ConvertVariable.h"
20 #include "flang/Lower/HostAssociations.h"
21 #include "flang/Lower/IO.h"
22 #include "flang/Lower/IterationSpace.h"
23 #include "flang/Lower/Mangler.h"
24 #include "flang/Lower/OpenACC.h"
25 #include "flang/Lower/OpenMP.h"
26 #include "flang/Lower/PFTBuilder.h"
27 #include "flang/Lower/Runtime.h"
28 #include "flang/Lower/StatementContext.h"
29 #include "flang/Lower/Support/Utils.h"
30 #include "flang/Lower/Todo.h"
31 #include "flang/Optimizer/Builder/BoxValue.h"
32 #include "flang/Optimizer/Builder/Character.h"
33 #include "flang/Optimizer/Builder/FIRBuilder.h"
34 #include "flang/Optimizer/Builder/Runtime/Character.h"
35 #include "flang/Optimizer/Builder/Runtime/Ragged.h"
36 #include "flang/Optimizer/Dialect/FIRAttr.h"
37 #include "flang/Optimizer/Dialect/FIRDialect.h"
38 #include "flang/Optimizer/Dialect/FIROps.h"
39 #include "flang/Optimizer/Support/FIRContext.h"
40 #include "flang/Optimizer/Support/FatalError.h"
41 #include "flang/Optimizer/Support/InternalNames.h"
42 #include "flang/Optimizer/Transforms/Passes.h"
43 #include "flang/Parser/parse-tree.h"
44 #include "flang/Runtime/iostat.h"
45 #include "flang/Semantics/tools.h"
46 #include "mlir/Dialect/ControlFlow/IR/ControlFlowOps.h"
47 #include "mlir/IR/PatternMatch.h"
48 #include "mlir/Parser/Parser.h"
49 #include "mlir/Transforms/RegionUtils.h"
50 #include "llvm/Support/CommandLine.h"
51 #include "llvm/Support/Debug.h"
52 #include "llvm/Support/ErrorHandling.h"
53 
54 #define DEBUG_TYPE "flang-lower-bridge"
55 
56 static llvm::cl::opt<bool> dumpBeforeFir(
57     "fdebug-dump-pre-fir", llvm::cl::init(false),
58     llvm::cl::desc("dump the Pre-FIR tree prior to FIR generation"));
59 
60 static llvm::cl::opt<bool> forceLoopToExecuteOnce(
61     "always-execute-loop-body", llvm::cl::init(false),
62     llvm::cl::desc("force the body of a loop to execute at least once"));
63 
64 namespace {
65 /// Information for generating a structured or unstructured increment loop.
66 struct IncrementLoopInfo {
67   template <typename T>
68   explicit IncrementLoopInfo(Fortran::semantics::Symbol &sym, const T &lower,
69                              const T &upper, const std::optional<T> &step,
70                              bool isUnordered = false)
71       : loopVariableSym{sym}, lowerExpr{Fortran::semantics::GetExpr(lower)},
72         upperExpr{Fortran::semantics::GetExpr(upper)},
73         stepExpr{Fortran::semantics::GetExpr(step)}, isUnordered{isUnordered} {}
74 
75   IncrementLoopInfo(IncrementLoopInfo &&) = default;
76   IncrementLoopInfo &operator=(IncrementLoopInfo &&x) { return x; }
77 
78   // TODO: change when unstructured loops are also supported
79   bool isStructured() const { return true; }
80 
81   mlir::Type getLoopVariableType() const {
82     assert(loopVariable && "must be set");
83     return fir::unwrapRefType(loopVariable.getType());
84   }
85 
86   // Data members common to both structured and unstructured loops.
87   const Fortran::semantics::Symbol &loopVariableSym;
88   const Fortran::lower::SomeExpr *lowerExpr;
89   const Fortran::lower::SomeExpr *upperExpr;
90   const Fortran::lower::SomeExpr *stepExpr;
91   bool isUnordered; // do concurrent, forall
92   mlir::Value loopVariable = nullptr;
93   mlir::Value stepValue = nullptr; // possible uses in multiple blocks
94 
95   // Data members for structured loops.
96   fir::DoLoopOp doLoop = nullptr;
97 
98   // Data members for unstructured loops.
99   // TODO:
100 };
101 
102 /// Helper class to generate the runtime type info global data. This data
103 /// is required to describe the derived type to the runtime so that it can
104 /// operate over it. It must be ensured this data will be generated for every
105 /// derived type lowered in the current translated unit. However, this data
106 /// cannot be generated before FuncOp have been created for functions since the
107 /// initializers may take their address (e.g for type bound procedures). This
108 /// class allows registering all the required runtime type info while it is not
109 /// possible to create globals, and to generate this data after function
110 /// lowering.
111 class RuntimeTypeInfoConverter {
112   /// Store the location and symbols of derived type info to be generated.
113   /// The location of the derived type instantiation is also stored because
114   /// runtime type descriptor symbol are compiler generated and cannot be mapped
115   /// to user code on their own.
116   struct TypeInfoSymbol {
117     Fortran::semantics::SymbolRef symbol;
118     mlir::Location loc;
119   };
120 
121 public:
122   void registerTypeInfoSymbol(Fortran::lower::AbstractConverter &converter,
123                               mlir::Location loc,
124                               Fortran::semantics::SymbolRef typeInfoSym) {
125     if (seen.contains(typeInfoSym))
126       return;
127     seen.insert(typeInfoSym);
128     if (!skipRegistration) {
129       registeredTypeInfoSymbols.emplace_back(TypeInfoSymbol{typeInfoSym, loc});
130       return;
131     }
132     // Once the registration is closed, symbols cannot be added to the
133     // registeredTypeInfoSymbols list because it may be iterated over.
134     // However, after registration is closed, it is safe to directly generate
135     // the globals because all FuncOps whose addresses may be required by the
136     // initializers have been generated.
137     Fortran::lower::createRuntimeTypeInfoGlobal(converter, loc,
138                                                 typeInfoSym.get());
139   }
140 
141   void createTypeInfoGlobals(Fortran::lower::AbstractConverter &converter) {
142     skipRegistration = true;
143     for (const TypeInfoSymbol &info : registeredTypeInfoSymbols)
144       Fortran::lower::createRuntimeTypeInfoGlobal(converter, info.loc,
145                                                   info.symbol.get());
146     registeredTypeInfoSymbols.clear();
147   }
148 
149 private:
150   /// Store the runtime type descriptors that will be required for the
151   /// derived type that have been converted to FIR derived types.
152   llvm::SmallVector<TypeInfoSymbol> registeredTypeInfoSymbols;
153   /// Create derived type runtime info global immediately without storing the
154   /// symbol in registeredTypeInfoSymbols.
155   bool skipRegistration = false;
156   /// Track symbols symbols processed during and after the registration
157   /// to avoid infinite loops between type conversions and global variable
158   /// creation.
159   llvm::SmallSetVector<Fortran::semantics::SymbolRef, 64> seen;
160 };
161 
162 using IncrementLoopNestInfo = llvm::SmallVector<IncrementLoopInfo>;
163 } // namespace
164 
165 //===----------------------------------------------------------------------===//
166 // FirConverter
167 //===----------------------------------------------------------------------===//
168 
169 namespace {
170 
171 /// Traverse the pre-FIR tree (PFT) to generate the FIR dialect of MLIR.
172 class FirConverter : public Fortran::lower::AbstractConverter {
173 public:
174   explicit FirConverter(Fortran::lower::LoweringBridge &bridge)
175       : bridge{bridge}, foldingContext{bridge.createFoldingContext()} {}
176   virtual ~FirConverter() = default;
177 
178   /// Convert the PFT to FIR.
179   void run(Fortran::lower::pft::Program &pft) {
180     // Preliminary translation pass.
181 
182     // - Lower common blocks from the PFT common block list that contains a
183     // consolidated list of the common blocks (with the initialization if any in
184     // the Program, and with the common block biggest size in all its
185     // appearance). This is done before lowering any scope declarations because
186     // it is not know at the local scope level what MLIR type common blocks
187     // should have to suit all its usage in the compilation unit.
188     lowerCommonBlocks(pft.getCommonBlocks());
189 
190     //  - Declare all functions that have definitions so that definition
191     //    signatures prevail over call site signatures.
192     //  - Define module variables and OpenMP/OpenACC declarative construct so
193     //    that they are available before lowering any function that may use
194     //    them.
195     for (Fortran::lower::pft::Program::Units &u : pft.getUnits()) {
196       std::visit(Fortran::common::visitors{
197                      [&](Fortran::lower::pft::FunctionLikeUnit &f) {
198                        declareFunction(f);
199                      },
200                      [&](Fortran::lower::pft::ModuleLikeUnit &m) {
201                        lowerModuleDeclScope(m);
202                        for (Fortran::lower::pft::FunctionLikeUnit &f :
203                             m.nestedFunctions)
204                          declareFunction(f);
205                      },
206                      [&](Fortran::lower::pft::BlockDataUnit &b) {},
207                      [&](Fortran::lower::pft::CompilerDirectiveUnit &d) {},
208                  },
209                  u);
210     }
211 
212     // Primary translation pass.
213     for (Fortran::lower::pft::Program::Units &u : pft.getUnits()) {
214       std::visit(
215           Fortran::common::visitors{
216               [&](Fortran::lower::pft::FunctionLikeUnit &f) { lowerFunc(f); },
217               [&](Fortran::lower::pft::ModuleLikeUnit &m) { lowerMod(m); },
218               [&](Fortran::lower::pft::BlockDataUnit &b) {},
219               [&](Fortran::lower::pft::CompilerDirectiveUnit &d) {
220                 setCurrentPosition(
221                     d.get<Fortran::parser::CompilerDirective>().source);
222                 mlir::emitWarning(toLocation(),
223                                   "ignoring all compiler directives");
224               },
225           },
226           u);
227     }
228 
229     /// Once all the code has been translated, create runtime type info
230     /// global data structure for the derived types that have been
231     /// processed.
232     createGlobalOutsideOfFunctionLowering(
233         [&]() { runtimeTypeInfoConverter.createTypeInfoGlobals(*this); });
234   }
235 
236   /// Declare a function.
237   void declareFunction(Fortran::lower::pft::FunctionLikeUnit &funit) {
238     setCurrentPosition(funit.getStartingSourceLoc());
239     for (int entryIndex = 0, last = funit.entryPointList.size();
240          entryIndex < last; ++entryIndex) {
241       funit.setActiveEntry(entryIndex);
242       // Calling CalleeInterface ctor will build a declaration
243       // mlir::func::FuncOp with no other side effects.
244       // TODO: when doing some compiler profiling on real apps, it may be worth
245       // to check it's better to save the CalleeInterface instead of recomputing
246       // it later when lowering the body. CalleeInterface ctor should be linear
247       // with the number of arguments, so it is not awful to do it that way for
248       // now, but the linear coefficient might be non negligible. Until
249       // measured, stick to the solution that impacts the code less.
250       Fortran::lower::CalleeInterface{funit, *this};
251     }
252     funit.setActiveEntry(0);
253 
254     // Compute the set of host associated entities from the nested functions.
255     llvm::SetVector<const Fortran::semantics::Symbol *> escapeHost;
256     for (Fortran::lower::pft::FunctionLikeUnit &f : funit.nestedFunctions)
257       collectHostAssociatedVariables(f, escapeHost);
258     funit.setHostAssociatedSymbols(escapeHost);
259 
260     // Declare internal procedures
261     for (Fortran::lower::pft::FunctionLikeUnit &f : funit.nestedFunctions)
262       declareFunction(f);
263   }
264 
265   /// Collects the canonical list of all host associated symbols. These bindings
266   /// must be aggregated into a tuple which can then be added to each of the
267   /// internal procedure declarations and passed at each call site.
268   void collectHostAssociatedVariables(
269       Fortran::lower::pft::FunctionLikeUnit &funit,
270       llvm::SetVector<const Fortran::semantics::Symbol *> &escapees) {
271     const Fortran::semantics::Scope *internalScope =
272         funit.getSubprogramSymbol().scope();
273     assert(internalScope && "internal procedures symbol must create a scope");
274     auto addToListIfEscapee = [&](const Fortran::semantics::Symbol &sym) {
275       const Fortran::semantics::Symbol &ultimate = sym.GetUltimate();
276       const auto *namelistDetails =
277           ultimate.detailsIf<Fortran::semantics::NamelistDetails>();
278       if (ultimate.has<Fortran::semantics::ObjectEntityDetails>() ||
279           Fortran::semantics::IsProcedurePointer(ultimate) ||
280           Fortran::semantics::IsDummy(sym) || namelistDetails) {
281         const Fortran::semantics::Scope &ultimateScope = ultimate.owner();
282         if (ultimateScope.kind() ==
283                 Fortran::semantics::Scope::Kind::MainProgram ||
284             ultimateScope.kind() == Fortran::semantics::Scope::Kind::Subprogram)
285           if (ultimateScope != *internalScope &&
286               ultimateScope.Contains(*internalScope)) {
287             if (namelistDetails) {
288               // So far, namelist symbols are processed on the fly in IO and
289               // the related namelist data structure is not added to the symbol
290               // map, so it cannot be passed to the internal procedures.
291               // Instead, all the symbols of the host namelist used in the
292               // internal procedure must be considered as host associated so
293               // that IO lowering can find them when needed.
294               for (const auto &namelistObject : namelistDetails->objects())
295                 escapees.insert(&*namelistObject);
296             } else {
297               escapees.insert(&ultimate);
298             }
299           }
300       }
301     };
302     Fortran::lower::pft::visitAllSymbols(funit, addToListIfEscapee);
303   }
304 
305   //===--------------------------------------------------------------------===//
306   // AbstractConverter overrides
307   //===--------------------------------------------------------------------===//
308 
309   mlir::Value getSymbolAddress(Fortran::lower::SymbolRef sym) override final {
310     return lookupSymbol(sym).getAddr();
311   }
312 
313   mlir::Value impliedDoBinding(llvm::StringRef name) override final {
314     mlir::Value val = localSymbols.lookupImpliedDo(name);
315     if (!val)
316       fir::emitFatalError(toLocation(), "ac-do-variable has no binding");
317     return val;
318   }
319 
320   void copySymbolBinding(Fortran::lower::SymbolRef src,
321                          Fortran::lower::SymbolRef target) override final {
322     localSymbols.addSymbol(target, lookupSymbol(src).toExtendedValue());
323   }
324 
325   /// Add the symbol binding to the inner-most level of the symbol map and
326   /// return true if it is not already present. Otherwise, return false.
327   bool bindIfNewSymbol(Fortran::lower::SymbolRef sym,
328                        const fir::ExtendedValue &exval) {
329     if (shallowLookupSymbol(sym))
330       return false;
331     bindSymbol(sym, exval);
332     return true;
333   }
334 
335   void bindSymbol(Fortran::lower::SymbolRef sym,
336                   const fir::ExtendedValue &exval) override final {
337     localSymbols.addSymbol(sym, exval, /*forced=*/true);
338   }
339 
340   bool lookupLabelSet(Fortran::lower::SymbolRef sym,
341                       Fortran::lower::pft::LabelSet &labelSet) override final {
342     Fortran::lower::pft::FunctionLikeUnit &owningProc =
343         *getEval().getOwningProcedure();
344     auto iter = owningProc.assignSymbolLabelMap.find(sym);
345     if (iter == owningProc.assignSymbolLabelMap.end())
346       return false;
347     labelSet = iter->second;
348     return true;
349   }
350 
351   Fortran::lower::pft::Evaluation *
352   lookupLabel(Fortran::lower::pft::Label label) override final {
353     Fortran::lower::pft::FunctionLikeUnit &owningProc =
354         *getEval().getOwningProcedure();
355     auto iter = owningProc.labelEvaluationMap.find(label);
356     if (iter == owningProc.labelEvaluationMap.end())
357       return nullptr;
358     return iter->second;
359   }
360 
361   fir::ExtendedValue genExprAddr(const Fortran::lower::SomeExpr &expr,
362                                  Fortran::lower::StatementContext &context,
363                                  mlir::Location *loc = nullptr) override final {
364     return Fortran::lower::createSomeExtendedAddress(
365         loc ? *loc : toLocation(), *this, expr, localSymbols, context);
366   }
367   fir::ExtendedValue
368   genExprValue(const Fortran::lower::SomeExpr &expr,
369                Fortran::lower::StatementContext &context,
370                mlir::Location *loc = nullptr) override final {
371     return Fortran::lower::createSomeExtendedExpression(
372         loc ? *loc : toLocation(), *this, expr, localSymbols, context);
373   }
374   fir::MutableBoxValue
375   genExprMutableBox(mlir::Location loc,
376                     const Fortran::lower::SomeExpr &expr) override final {
377     return Fortran::lower::createMutableBox(loc, *this, expr, localSymbols);
378   }
379   fir::ExtendedValue genExprBox(const Fortran::lower::SomeExpr &expr,
380                                 Fortran::lower::StatementContext &context,
381                                 mlir::Location loc) override final {
382     return Fortran::lower::createBoxValue(loc, *this, expr, localSymbols,
383                                           context);
384   }
385 
386   Fortran::evaluate::FoldingContext &getFoldingContext() override final {
387     return foldingContext;
388   }
389 
390   mlir::Type genType(const Fortran::lower::SomeExpr &expr) override final {
391     return Fortran::lower::translateSomeExprToFIRType(*this, expr);
392   }
393   mlir::Type genType(const Fortran::lower::pft::Variable &var) override final {
394     return Fortran::lower::translateVariableToFIRType(*this, var);
395   }
396   mlir::Type genType(Fortran::lower::SymbolRef sym) override final {
397     return Fortran::lower::translateSymbolToFIRType(*this, sym);
398   }
399   mlir::Type
400   genType(Fortran::common::TypeCategory tc, int kind,
401           llvm::ArrayRef<std::int64_t> lenParameters) override final {
402     return Fortran::lower::getFIRType(&getMLIRContext(), tc, kind,
403                                       lenParameters);
404   }
405   mlir::Type
406   genType(const Fortran::semantics::DerivedTypeSpec &tySpec) override final {
407     return Fortran::lower::translateDerivedTypeToFIRType(*this, tySpec);
408   }
409   mlir::Type genType(Fortran::common::TypeCategory tc) override final {
410     return Fortran::lower::getFIRType(
411         &getMLIRContext(), tc, bridge.getDefaultKinds().GetDefaultKind(tc),
412         llvm::None);
413   }
414 
415   bool createHostAssociateVarClone(
416       const Fortran::semantics::Symbol &sym) override final {
417     mlir::Location loc = genLocation(sym.name());
418     mlir::Type symType = genType(sym);
419     const auto *details = sym.detailsIf<Fortran::semantics::HostAssocDetails>();
420     assert(details && "No host-association found");
421     const Fortran::semantics::Symbol &hsym = details->symbol();
422     Fortran::lower::SymbolBox hsb = lookupSymbol(hsym);
423 
424     auto allocate = [&](llvm::ArrayRef<mlir::Value> shape,
425                         llvm::ArrayRef<mlir::Value> typeParams) -> mlir::Value {
426       mlir::Value allocVal = builder->allocateLocal(
427           loc, symType, mangleName(sym), toStringRef(sym.GetUltimate().name()),
428           /*pinned=*/true, shape, typeParams,
429           sym.GetUltimate().attrs().test(Fortran::semantics::Attr::TARGET));
430       return allocVal;
431     };
432 
433     fir::ExtendedValue hexv = getExtendedValue(hsb);
434     fir::ExtendedValue exv = hexv.match(
435         [&](const fir::BoxValue &box) -> fir::ExtendedValue {
436           const Fortran::semantics::DeclTypeSpec *type = sym.GetType();
437           if (type && type->IsPolymorphic())
438             TODO(loc, "create polymorphic host associated copy");
439           // Create a contiguous temp with the same shape and length as
440           // the original variable described by a fir.box.
441           llvm::SmallVector<mlir::Value> extents =
442               fir::factory::getExtents(*builder, loc, hexv);
443           if (box.isDerivedWithLengthParameters())
444             TODO(loc, "get length parameters from derived type BoxValue");
445           if (box.isCharacter()) {
446             mlir::Value len = fir::factory::readCharLen(*builder, loc, box);
447             mlir::Value temp = allocate(extents, {len});
448             return fir::CharArrayBoxValue{temp, len, extents};
449           }
450           return fir::ArrayBoxValue{allocate(extents, {}), extents};
451         },
452         [&](const fir::MutableBoxValue &box) -> fir::ExtendedValue {
453           // Allocate storage for a pointer/allocatble descriptor.
454           // No shape/lengths to be passed to the alloca.
455           return fir::MutableBoxValue(allocate({}, {}),
456                                       box.nonDeferredLenParams(), {});
457         },
458         [&](const auto &) -> fir::ExtendedValue {
459           mlir::Value temp =
460               allocate(fir::factory::getExtents(*builder, loc, hexv),
461                        fir::getTypeParams(hexv));
462           return fir::substBase(hexv, temp);
463         });
464 
465     return bindIfNewSymbol(sym, exv);
466   }
467 
468   void
469   copyHostAssociateVar(const Fortran::semantics::Symbol &sym) override final {
470     // 1) Fetch the original copy of the variable.
471     assert(sym.has<Fortran::semantics::HostAssocDetails>() &&
472            "No host-association found");
473     const Fortran::semantics::Symbol &hsym = sym.GetUltimate();
474     Fortran::lower::SymbolBox hsb = lookupSymbol(hsym);
475     fir::ExtendedValue hexv = getExtendedValue(hsb);
476 
477     // 2) Create a copy that will mask the original.
478     createHostAssociateVarClone(sym);
479     Fortran::lower::SymbolBox sb = lookupSymbol(sym);
480     fir::ExtendedValue exv = getExtendedValue(sb);
481 
482     // 3) Perform the assignment.
483     mlir::Location loc = genLocation(sym.name());
484     mlir::Type symType = genType(sym);
485     if (auto seqTy = symType.dyn_cast<fir::SequenceType>()) {
486       Fortran::lower::StatementContext stmtCtx;
487       Fortran::lower::createSomeArrayAssignment(*this, exv, hexv, localSymbols,
488                                                 stmtCtx);
489       stmtCtx.finalize();
490     } else if (hexv.getBoxOf<fir::CharBoxValue>()) {
491       fir::factory::CharacterExprHelper{*builder, loc}.createAssign(exv, hexv);
492     } else if (hexv.getBoxOf<fir::MutableBoxValue>()) {
493       TODO(loc, "firstprivatisation of allocatable variables");
494     } else {
495       auto loadVal = builder->create<fir::LoadOp>(loc, fir::getBase(hexv));
496       builder->create<fir::StoreOp>(loc, loadVal, fir::getBase(exv));
497     }
498   }
499 
500   //===--------------------------------------------------------------------===//
501   // Utility methods
502   //===--------------------------------------------------------------------===//
503 
504   mlir::Location getCurrentLocation() override final { return toLocation(); }
505 
506   /// Generate a dummy location.
507   mlir::Location genUnknownLocation() override final {
508     // Note: builder may not be instantiated yet
509     return mlir::UnknownLoc::get(&getMLIRContext());
510   }
511 
512   /// Generate a `Location` from the `CharBlock`.
513   mlir::Location
514   genLocation(const Fortran::parser::CharBlock &block) override final {
515     if (const Fortran::parser::AllCookedSources *cooked =
516             bridge.getCookedSource()) {
517       if (std::optional<std::pair<Fortran::parser::SourcePosition,
518                                   Fortran::parser::SourcePosition>>
519               loc = cooked->GetSourcePositionRange(block)) {
520         // loc is a pair (begin, end); use the beginning position
521         Fortran::parser::SourcePosition &filePos = loc->first;
522         return mlir::FileLineColLoc::get(&getMLIRContext(), filePos.file.path(),
523                                          filePos.line, filePos.column);
524       }
525     }
526     return genUnknownLocation();
527   }
528 
529   fir::FirOpBuilder &getFirOpBuilder() override final { return *builder; }
530 
531   mlir::ModuleOp &getModuleOp() override final { return bridge.getModule(); }
532 
533   mlir::MLIRContext &getMLIRContext() override final {
534     return bridge.getMLIRContext();
535   }
536   std::string
537   mangleName(const Fortran::semantics::Symbol &symbol) override final {
538     return Fortran::lower::mangle::mangleName(symbol);
539   }
540 
541   const fir::KindMapping &getKindMap() override final {
542     return bridge.getKindMap();
543   }
544 
545   mlir::Value hostAssocTupleValue() override final { return hostAssocTuple; }
546 
547   /// Record a binding for the ssa-value of the tuple for this function.
548   void bindHostAssocTuple(mlir::Value val) override final {
549     assert(!hostAssocTuple && val);
550     hostAssocTuple = val;
551   }
552 
553   void registerRuntimeTypeInfo(
554       mlir::Location loc,
555       Fortran::lower::SymbolRef typeInfoSym) override final {
556     runtimeTypeInfoConverter.registerTypeInfoSymbol(*this, loc, typeInfoSym);
557   }
558 
559 private:
560   FirConverter() = delete;
561   FirConverter(const FirConverter &) = delete;
562   FirConverter &operator=(const FirConverter &) = delete;
563 
564   //===--------------------------------------------------------------------===//
565   // Helper member functions
566   //===--------------------------------------------------------------------===//
567 
568   mlir::Value createFIRExpr(mlir::Location loc,
569                             const Fortran::lower::SomeExpr *expr,
570                             Fortran::lower::StatementContext &stmtCtx) {
571     return fir::getBase(genExprValue(*expr, stmtCtx, &loc));
572   }
573 
574   /// Find the symbol in the local map or return null.
575   Fortran::lower::SymbolBox
576   lookupSymbol(const Fortran::semantics::Symbol &sym) {
577     if (Fortran::lower::SymbolBox v = localSymbols.lookupSymbol(sym))
578       return v;
579     return {};
580   }
581 
582   /// Find the symbol in the inner-most level of the local map or return null.
583   Fortran::lower::SymbolBox
584   shallowLookupSymbol(const Fortran::semantics::Symbol &sym) {
585     if (Fortran::lower::SymbolBox v = localSymbols.shallowLookupSymbol(sym))
586       return v;
587     return {};
588   }
589 
590   /// Add the symbol to the local map and return `true`. If the symbol is
591   /// already in the map and \p forced is `false`, the map is not updated.
592   /// Instead the value `false` is returned.
593   bool addSymbol(const Fortran::semantics::SymbolRef sym, mlir::Value val,
594                  bool forced = false) {
595     if (!forced && lookupSymbol(sym))
596       return false;
597     localSymbols.addSymbol(sym, val, forced);
598     return true;
599   }
600 
601   bool addCharSymbol(const Fortran::semantics::SymbolRef sym, mlir::Value val,
602                      mlir::Value len, bool forced = false) {
603     if (!forced && lookupSymbol(sym))
604       return false;
605     // TODO: ensure val type is fir.array<len x fir.char<kind>> like. Insert
606     // cast if needed.
607     localSymbols.addCharSymbol(sym, val, len, forced);
608     return true;
609   }
610 
611   fir::ExtendedValue getExtendedValue(Fortran::lower::SymbolBox sb) {
612     return sb.match(
613         [&](const Fortran::lower::SymbolBox::PointerOrAllocatable &box) {
614           return fir::factory::genMutableBoxRead(*builder, getCurrentLocation(),
615                                                  box);
616         },
617         [&sb](auto &) { return sb.toExtendedValue(); });
618   }
619 
620   /// Generate the address of loop variable \p sym.
621   mlir::Value genLoopVariableAddress(mlir::Location loc,
622                                      const Fortran::semantics::Symbol &sym) {
623     assert(lookupSymbol(sym) && "loop control variable must already be in map");
624     Fortran::lower::StatementContext stmtCtx;
625     return fir::getBase(
626         genExprAddr(Fortran::evaluate::AsGenericExpr(sym).value(), stmtCtx));
627   }
628 
629   static bool isNumericScalarCategory(Fortran::common::TypeCategory cat) {
630     return cat == Fortran::common::TypeCategory::Integer ||
631            cat == Fortran::common::TypeCategory::Real ||
632            cat == Fortran::common::TypeCategory::Complex ||
633            cat == Fortran::common::TypeCategory::Logical;
634   }
635   static bool isLogicalCategory(Fortran::common::TypeCategory cat) {
636     return cat == Fortran::common::TypeCategory::Logical;
637   }
638   static bool isCharacterCategory(Fortran::common::TypeCategory cat) {
639     return cat == Fortran::common::TypeCategory::Character;
640   }
641   static bool isDerivedCategory(Fortran::common::TypeCategory cat) {
642     return cat == Fortran::common::TypeCategory::Derived;
643   }
644 
645   /// Insert a new block before \p block.  Leave the insertion point unchanged.
646   mlir::Block *insertBlock(mlir::Block *block) {
647     mlir::OpBuilder::InsertPoint insertPt = builder->saveInsertionPoint();
648     mlir::Block *newBlock = builder->createBlock(block);
649     builder->restoreInsertionPoint(insertPt);
650     return newBlock;
651   }
652 
653   mlir::Block *blockOfLabel(Fortran::lower::pft::Evaluation &eval,
654                             Fortran::parser::Label label) {
655     const Fortran::lower::pft::LabelEvalMap &labelEvaluationMap =
656         eval.getOwningProcedure()->labelEvaluationMap;
657     const auto iter = labelEvaluationMap.find(label);
658     assert(iter != labelEvaluationMap.end() && "label missing from map");
659     mlir::Block *block = iter->second->block;
660     assert(block && "missing labeled evaluation block");
661     return block;
662   }
663 
664   void genFIRBranch(mlir::Block *targetBlock) {
665     assert(targetBlock && "missing unconditional target block");
666     builder->create<mlir::cf::BranchOp>(toLocation(), targetBlock);
667   }
668 
669   void genFIRConditionalBranch(mlir::Value cond, mlir::Block *trueTarget,
670                                mlir::Block *falseTarget) {
671     assert(trueTarget && "missing conditional branch true block");
672     assert(falseTarget && "missing conditional branch false block");
673     mlir::Location loc = toLocation();
674     mlir::Value bcc = builder->createConvert(loc, builder->getI1Type(), cond);
675     builder->create<mlir::cf::CondBranchOp>(loc, bcc, trueTarget, llvm::None,
676                                             falseTarget, llvm::None);
677   }
678   void genFIRConditionalBranch(mlir::Value cond,
679                                Fortran::lower::pft::Evaluation *trueTarget,
680                                Fortran::lower::pft::Evaluation *falseTarget) {
681     genFIRConditionalBranch(cond, trueTarget->block, falseTarget->block);
682   }
683   void genFIRConditionalBranch(const Fortran::parser::ScalarLogicalExpr &expr,
684                                mlir::Block *trueTarget,
685                                mlir::Block *falseTarget) {
686     Fortran::lower::StatementContext stmtCtx;
687     mlir::Value cond =
688         createFIRExpr(toLocation(), Fortran::semantics::GetExpr(expr), stmtCtx);
689     stmtCtx.finalize();
690     genFIRConditionalBranch(cond, trueTarget, falseTarget);
691   }
692   void genFIRConditionalBranch(const Fortran::parser::ScalarLogicalExpr &expr,
693                                Fortran::lower::pft::Evaluation *trueTarget,
694                                Fortran::lower::pft::Evaluation *falseTarget) {
695     Fortran::lower::StatementContext stmtCtx;
696     mlir::Value cond =
697         createFIRExpr(toLocation(), Fortran::semantics::GetExpr(expr), stmtCtx);
698     stmtCtx.finalize();
699     genFIRConditionalBranch(cond, trueTarget->block, falseTarget->block);
700   }
701 
702   //===--------------------------------------------------------------------===//
703   // Termination of symbolically referenced execution units
704   //===--------------------------------------------------------------------===//
705 
706   /// END of program
707   ///
708   /// Generate the cleanup block before the program exits
709   void genExitRoutine() {
710     if (blockIsUnterminated())
711       builder->create<mlir::func::ReturnOp>(toLocation());
712   }
713   void genFIR(const Fortran::parser::EndProgramStmt &) { genExitRoutine(); }
714 
715   /// END of procedure-like constructs
716   ///
717   /// Generate the cleanup block before the procedure exits
718   void genReturnSymbol(const Fortran::semantics::Symbol &functionSymbol) {
719     const Fortran::semantics::Symbol &resultSym =
720         functionSymbol.get<Fortran::semantics::SubprogramDetails>().result();
721     Fortran::lower::SymbolBox resultSymBox = lookupSymbol(resultSym);
722     mlir::Location loc = toLocation();
723     if (!resultSymBox) {
724       mlir::emitError(loc, "failed lowering function return");
725       return;
726     }
727     mlir::Value resultVal = resultSymBox.match(
728         [&](const fir::CharBoxValue &x) -> mlir::Value {
729           return fir::factory::CharacterExprHelper{*builder, loc}
730               .createEmboxChar(x.getBuffer(), x.getLen());
731         },
732         [&](const auto &) -> mlir::Value {
733           mlir::Value resultRef = resultSymBox.getAddr();
734           mlir::Type resultType = genType(resultSym);
735           mlir::Type resultRefType = builder->getRefType(resultType);
736           // A function with multiple entry points returning different types
737           // tags all result variables with one of the largest types to allow
738           // them to share the same storage.  Convert this to the actual type.
739           if (resultRef.getType() != resultRefType)
740             resultRef = builder->createConvert(loc, resultRefType, resultRef);
741           return builder->create<fir::LoadOp>(loc, resultRef);
742         });
743     builder->create<mlir::func::ReturnOp>(loc, resultVal);
744   }
745 
746   /// Get the return value of a call to \p symbol, which is a subroutine entry
747   /// point that has alternative return specifiers.
748   const mlir::Value
749   getAltReturnResult(const Fortran::semantics::Symbol &symbol) {
750     assert(Fortran::semantics::HasAlternateReturns(symbol) &&
751            "subroutine does not have alternate returns");
752     return getSymbolAddress(symbol);
753   }
754 
755   void genFIRProcedureExit(Fortran::lower::pft::FunctionLikeUnit &funit,
756                            const Fortran::semantics::Symbol &symbol) {
757     if (mlir::Block *finalBlock = funit.finalBlock) {
758       // The current block must end with a terminator.
759       if (blockIsUnterminated())
760         builder->create<mlir::cf::BranchOp>(toLocation(), finalBlock);
761       // Set insertion point to final block.
762       builder->setInsertionPoint(finalBlock, finalBlock->end());
763     }
764     if (Fortran::semantics::IsFunction(symbol)) {
765       genReturnSymbol(symbol);
766     } else if (Fortran::semantics::HasAlternateReturns(symbol)) {
767       mlir::Value retval = builder->create<fir::LoadOp>(
768           toLocation(), getAltReturnResult(symbol));
769       builder->create<mlir::func::ReturnOp>(toLocation(), retval);
770     } else {
771       genExitRoutine();
772     }
773   }
774 
775   //
776   // Statements that have control-flow semantics
777   //
778 
779   /// Generate an If[Then]Stmt condition or its negation.
780   template <typename A>
781   mlir::Value genIfCondition(const A *stmt, bool negate = false) {
782     mlir::Location loc = toLocation();
783     Fortran::lower::StatementContext stmtCtx;
784     mlir::Value condExpr = createFIRExpr(
785         loc,
786         Fortran::semantics::GetExpr(
787             std::get<Fortran::parser::ScalarLogicalExpr>(stmt->t)),
788         stmtCtx);
789     stmtCtx.finalize();
790     mlir::Value cond =
791         builder->createConvert(loc, builder->getI1Type(), condExpr);
792     if (negate)
793       cond = builder->create<mlir::arith::XOrIOp>(
794           loc, cond, builder->createIntegerConstant(loc, cond.getType(), 1));
795     return cond;
796   }
797 
798   mlir::func::FuncOp getFunc(llvm::StringRef name, mlir::FunctionType ty) {
799     if (mlir::func::FuncOp func = builder->getNamedFunction(name)) {
800       assert(func.getFunctionType() == ty);
801       return func;
802     }
803     return builder->createFunction(toLocation(), name, ty);
804   }
805 
806   /// Lowering of CALL statement
807   void genFIR(const Fortran::parser::CallStmt &stmt) {
808     Fortran::lower::StatementContext stmtCtx;
809     Fortran::lower::pft::Evaluation &eval = getEval();
810     setCurrentPosition(stmt.v.source);
811     assert(stmt.typedCall && "Call was not analyzed");
812     // Call statement lowering shares code with function call lowering.
813     mlir::Value res = Fortran::lower::createSubroutineCall(
814         *this, *stmt.typedCall, explicitIterSpace, implicitIterSpace,
815         localSymbols, stmtCtx, /*isUserDefAssignment=*/false);
816     if (!res)
817       return; // "Normal" subroutine call.
818     // Call with alternate return specifiers.
819     // The call returns an index that selects an alternate return branch target.
820     llvm::SmallVector<int64_t> indexList;
821     llvm::SmallVector<mlir::Block *> blockList;
822     int64_t index = 0;
823     for (const Fortran::parser::ActualArgSpec &arg :
824          std::get<std::list<Fortran::parser::ActualArgSpec>>(stmt.v.t)) {
825       const auto &actual = std::get<Fortran::parser::ActualArg>(arg.t);
826       if (const auto *altReturn =
827               std::get_if<Fortran::parser::AltReturnSpec>(&actual.u)) {
828         indexList.push_back(++index);
829         blockList.push_back(blockOfLabel(eval, altReturn->v));
830       }
831     }
832     blockList.push_back(eval.nonNopSuccessor().block); // default = fallthrough
833     stmtCtx.finalize();
834     builder->create<fir::SelectOp>(toLocation(), res, indexList, blockList);
835   }
836 
837   void genFIR(const Fortran::parser::ComputedGotoStmt &stmt) {
838     Fortran::lower::StatementContext stmtCtx;
839     Fortran::lower::pft::Evaluation &eval = getEval();
840     mlir::Value selectExpr =
841         createFIRExpr(toLocation(),
842                       Fortran::semantics::GetExpr(
843                           std::get<Fortran::parser::ScalarIntExpr>(stmt.t)),
844                       stmtCtx);
845     stmtCtx.finalize();
846     llvm::SmallVector<int64_t> indexList;
847     llvm::SmallVector<mlir::Block *> blockList;
848     int64_t index = 0;
849     for (Fortran::parser::Label label :
850          std::get<std::list<Fortran::parser::Label>>(stmt.t)) {
851       indexList.push_back(++index);
852       blockList.push_back(blockOfLabel(eval, label));
853     }
854     blockList.push_back(eval.nonNopSuccessor().block); // default
855     builder->create<fir::SelectOp>(toLocation(), selectExpr, indexList,
856                                    blockList);
857   }
858 
859   void genFIR(const Fortran::parser::ArithmeticIfStmt &stmt) {
860     Fortran::lower::StatementContext stmtCtx;
861     Fortran::lower::pft::Evaluation &eval = getEval();
862     mlir::Value expr = createFIRExpr(
863         toLocation(),
864         Fortran::semantics::GetExpr(std::get<Fortran::parser::Expr>(stmt.t)),
865         stmtCtx);
866     stmtCtx.finalize();
867     mlir::Type exprType = expr.getType();
868     mlir::Location loc = toLocation();
869     if (exprType.isSignlessInteger()) {
870       // Arithmetic expression has Integer type.  Generate a SelectCaseOp
871       // with ranges {(-inf:-1], 0=default, [1:inf)}.
872       mlir::MLIRContext *context = builder->getContext();
873       llvm::SmallVector<mlir::Attribute> attrList;
874       llvm::SmallVector<mlir::Value> valueList;
875       llvm::SmallVector<mlir::Block *> blockList;
876       attrList.push_back(fir::UpperBoundAttr::get(context));
877       valueList.push_back(builder->createIntegerConstant(loc, exprType, -1));
878       blockList.push_back(blockOfLabel(eval, std::get<1>(stmt.t)));
879       attrList.push_back(fir::LowerBoundAttr::get(context));
880       valueList.push_back(builder->createIntegerConstant(loc, exprType, 1));
881       blockList.push_back(blockOfLabel(eval, std::get<3>(stmt.t)));
882       attrList.push_back(mlir::UnitAttr::get(context)); // 0 is the "default"
883       blockList.push_back(blockOfLabel(eval, std::get<2>(stmt.t)));
884       builder->create<fir::SelectCaseOp>(loc, expr, attrList, valueList,
885                                          blockList);
886       return;
887     }
888     // Arithmetic expression has Real type.  Generate
889     //   sum = expr + expr  [ raise an exception if expr is a NaN ]
890     //   if (sum < 0.0) goto L1 else if (sum > 0.0) goto L3 else goto L2
891     auto sum = builder->create<mlir::arith::AddFOp>(loc, expr, expr);
892     auto zero = builder->create<mlir::arith::ConstantOp>(
893         loc, exprType, builder->getFloatAttr(exprType, 0.0));
894     auto cond1 = builder->create<mlir::arith::CmpFOp>(
895         loc, mlir::arith::CmpFPredicate::OLT, sum, zero);
896     mlir::Block *elseIfBlock =
897         builder->getBlock()->splitBlock(builder->getInsertionPoint());
898     genFIRConditionalBranch(cond1, blockOfLabel(eval, std::get<1>(stmt.t)),
899                             elseIfBlock);
900     startBlock(elseIfBlock);
901     auto cond2 = builder->create<mlir::arith::CmpFOp>(
902         loc, mlir::arith::CmpFPredicate::OGT, sum, zero);
903     genFIRConditionalBranch(cond2, blockOfLabel(eval, std::get<3>(stmt.t)),
904                             blockOfLabel(eval, std::get<2>(stmt.t)));
905   }
906 
907   void genFIR(const Fortran::parser::AssignedGotoStmt &stmt) {
908     // Program requirement 1990 8.2.4 -
909     //
910     //   At the time of execution of an assigned GOTO statement, the integer
911     //   variable must be defined with the value of a statement label of a
912     //   branch target statement that appears in the same scoping unit.
913     //   Note that the variable may be defined with a statement label value
914     //   only by an ASSIGN statement in the same scoping unit as the assigned
915     //   GOTO statement.
916 
917     mlir::Location loc = toLocation();
918     Fortran::lower::pft::Evaluation &eval = getEval();
919     const Fortran::lower::pft::SymbolLabelMap &symbolLabelMap =
920         eval.getOwningProcedure()->assignSymbolLabelMap;
921     const Fortran::semantics::Symbol &symbol =
922         *std::get<Fortran::parser::Name>(stmt.t).symbol;
923     auto selectExpr =
924         builder->create<fir::LoadOp>(loc, getSymbolAddress(symbol));
925     auto iter = symbolLabelMap.find(symbol);
926     if (iter == symbolLabelMap.end()) {
927       // Fail for a nonconforming program unit that does not have any ASSIGN
928       // statements.  The front end should check for this.
929       mlir::emitError(loc, "(semantics issue) no assigned goto targets");
930       exit(1);
931     }
932     auto labelSet = iter->second;
933     llvm::SmallVector<int64_t> indexList;
934     llvm::SmallVector<mlir::Block *> blockList;
935     auto addLabel = [&](Fortran::parser::Label label) {
936       indexList.push_back(label);
937       blockList.push_back(blockOfLabel(eval, label));
938     };
939     // Add labels from an explicit list.  The list may have duplicates.
940     for (Fortran::parser::Label label :
941          std::get<std::list<Fortran::parser::Label>>(stmt.t)) {
942       if (labelSet.count(label) &&
943           std::find(indexList.begin(), indexList.end(), label) ==
944               indexList.end()) { // ignore duplicates
945         addLabel(label);
946       }
947     }
948     // Absent an explicit list, add all possible label targets.
949     if (indexList.empty())
950       for (auto &label : labelSet)
951         addLabel(label);
952     // Add a nop/fallthrough branch to the switch for a nonconforming program
953     // unit that violates the program requirement above.
954     blockList.push_back(eval.nonNopSuccessor().block); // default
955     builder->create<fir::SelectOp>(loc, selectExpr, indexList, blockList);
956   }
957 
958   /// Generate FIR for a DO construct.  There are six variants:
959   ///  - unstructured infinite and while loops
960   ///  - structured and unstructured increment loops
961   ///  - structured and unstructured concurrent loops
962   void genFIR(const Fortran::parser::DoConstruct &doConstruct) {
963     setCurrentPositionAt(doConstruct);
964     // Collect loop nest information.
965     // Generate begin loop code directly for infinite and while loops.
966     Fortran::lower::pft::Evaluation &eval = getEval();
967     Fortran::lower::pft::Evaluation &doStmtEval =
968         eval.getFirstNestedEvaluation();
969     auto *doStmt = doStmtEval.getIf<Fortran::parser::NonLabelDoStmt>();
970     const auto &loopControl =
971         std::get<std::optional<Fortran::parser::LoopControl>>(doStmt->t);
972     IncrementLoopNestInfo incrementLoopNestInfo;
973     if (const auto *bounds = std::get_if<Fortran::parser::LoopControl::Bounds>(
974             &loopControl->u)) {
975       // Non-concurrent increment loop.
976       incrementLoopNestInfo.emplace_back(*bounds->name.thing.symbol,
977                                          bounds->lower, bounds->upper,
978                                          bounds->step);
979       // TODO: unstructured loop
980     } else {
981       TODO(toLocation(), "infinite/unstructured loop/concurrent loop");
982     }
983 
984     // Increment loop begin code.  (TODO: Infinite/while code was already
985     // generated.)
986     genFIRIncrementLoopBegin(incrementLoopNestInfo);
987 
988     // Loop body code - NonLabelDoStmt and EndDoStmt code is generated here.
989     // Their genFIR calls are nops except for block management in some cases.
990     for (Fortran::lower::pft::Evaluation &e : eval.getNestedEvaluations())
991       genFIR(e, /*unstructuredContext=*/false);
992 
993     // Loop end code. (TODO: infinite/while loop)
994     genFIRIncrementLoopEnd(incrementLoopNestInfo);
995   }
996 
997   /// Generate FIR to begin a structured or unstructured increment loop nest.
998   void genFIRIncrementLoopBegin(IncrementLoopNestInfo &incrementLoopNestInfo) {
999     assert(!incrementLoopNestInfo.empty() && "empty loop nest");
1000     mlir::Location loc = toLocation();
1001     auto genControlValue = [&](const Fortran::lower::SomeExpr *expr,
1002                                const IncrementLoopInfo &info) {
1003       mlir::Type controlType = info.isStructured() ? builder->getIndexType()
1004                                                    : info.getLoopVariableType();
1005       Fortran::lower::StatementContext stmtCtx;
1006       if (expr)
1007         return builder->createConvert(loc, controlType,
1008                                       createFIRExpr(loc, expr, stmtCtx));
1009       return builder->createIntegerConstant(loc, controlType, 1); // step
1010     };
1011     for (IncrementLoopInfo &info : incrementLoopNestInfo) {
1012       info.loopVariable = genLoopVariableAddress(loc, info.loopVariableSym);
1013       mlir::Value lowerValue = genControlValue(info.lowerExpr, info);
1014       mlir::Value upperValue = genControlValue(info.upperExpr, info);
1015       info.stepValue = genControlValue(info.stepExpr, info);
1016 
1017       // Structured loop - generate fir.do_loop.
1018       if (info.isStructured()) {
1019         info.doLoop = builder->create<fir::DoLoopOp>(
1020             loc, lowerValue, upperValue, info.stepValue, info.isUnordered,
1021             /*finalCountValue=*/!info.isUnordered);
1022         builder->setInsertionPointToStart(info.doLoop.getBody());
1023         // Update the loop variable value, as it may have non-index references.
1024         mlir::Value value = builder->createConvert(
1025             loc, info.getLoopVariableType(), info.doLoop.getInductionVar());
1026         builder->create<fir::StoreOp>(loc, value, info.loopVariable);
1027         // TODO: Mask expr
1028         // TODO: handle Locality Spec
1029         continue;
1030       }
1031       // TODO: Unstructured loop handling
1032     }
1033   }
1034 
1035   /// Generate FIR to end a structured or unstructured increment loop nest.
1036   void genFIRIncrementLoopEnd(IncrementLoopNestInfo &incrementLoopNestInfo) {
1037     assert(!incrementLoopNestInfo.empty() && "empty loop nest");
1038     mlir::Location loc = toLocation();
1039     for (auto it = incrementLoopNestInfo.rbegin(),
1040               rend = incrementLoopNestInfo.rend();
1041          it != rend; ++it) {
1042       IncrementLoopInfo &info = *it;
1043       if (info.isStructured()) {
1044         // End fir.do_loop.
1045         if (!info.isUnordered) {
1046           builder->setInsertionPointToEnd(info.doLoop.getBody());
1047           mlir::Value result = builder->create<mlir::arith::AddIOp>(
1048               loc, info.doLoop.getInductionVar(), info.doLoop.getStep());
1049           builder->create<fir::ResultOp>(loc, result);
1050         }
1051         builder->setInsertionPointAfter(info.doLoop);
1052         if (info.isUnordered)
1053           continue;
1054         // The loop control variable may be used after loop execution.
1055         mlir::Value lcv = builder->createConvert(
1056             loc, info.getLoopVariableType(), info.doLoop.getResult(0));
1057         builder->create<fir::StoreOp>(loc, lcv, info.loopVariable);
1058         continue;
1059       }
1060 
1061       // TODO: Unstructured loop
1062     }
1063   }
1064 
1065   /// Generate structured or unstructured FIR for an IF construct.
1066   /// The initial statement may be either an IfStmt or an IfThenStmt.
1067   void genFIR(const Fortran::parser::IfConstruct &) {
1068     mlir::Location loc = toLocation();
1069     Fortran::lower::pft::Evaluation &eval = getEval();
1070     if (eval.lowerAsStructured()) {
1071       // Structured fir.if nest.
1072       fir::IfOp topIfOp, currentIfOp;
1073       for (Fortran::lower::pft::Evaluation &e : eval.getNestedEvaluations()) {
1074         auto genIfOp = [&](mlir::Value cond) {
1075           auto ifOp = builder->create<fir::IfOp>(loc, cond, /*withElse=*/true);
1076           builder->setInsertionPointToStart(&ifOp.getThenRegion().front());
1077           return ifOp;
1078         };
1079         if (auto *s = e.getIf<Fortran::parser::IfThenStmt>()) {
1080           topIfOp = currentIfOp = genIfOp(genIfCondition(s, e.negateCondition));
1081         } else if (auto *s = e.getIf<Fortran::parser::IfStmt>()) {
1082           topIfOp = currentIfOp = genIfOp(genIfCondition(s, e.negateCondition));
1083         } else if (auto *s = e.getIf<Fortran::parser::ElseIfStmt>()) {
1084           builder->setInsertionPointToStart(
1085               &currentIfOp.getElseRegion().front());
1086           currentIfOp = genIfOp(genIfCondition(s));
1087         } else if (e.isA<Fortran::parser::ElseStmt>()) {
1088           builder->setInsertionPointToStart(
1089               &currentIfOp.getElseRegion().front());
1090         } else if (e.isA<Fortran::parser::EndIfStmt>()) {
1091           builder->setInsertionPointAfter(topIfOp);
1092         } else {
1093           genFIR(e, /*unstructuredContext=*/false);
1094         }
1095       }
1096       return;
1097     }
1098 
1099     // Unstructured branch sequence.
1100     for (Fortran::lower::pft::Evaluation &e : eval.getNestedEvaluations()) {
1101       auto genIfBranch = [&](mlir::Value cond) {
1102         if (e.lexicalSuccessor == e.controlSuccessor) // empty block -> exit
1103           genFIRConditionalBranch(cond, e.parentConstruct->constructExit,
1104                                   e.controlSuccessor);
1105         else // non-empty block
1106           genFIRConditionalBranch(cond, e.lexicalSuccessor, e.controlSuccessor);
1107       };
1108       if (auto *s = e.getIf<Fortran::parser::IfThenStmt>()) {
1109         maybeStartBlock(e.block);
1110         genIfBranch(genIfCondition(s, e.negateCondition));
1111       } else if (auto *s = e.getIf<Fortran::parser::IfStmt>()) {
1112         maybeStartBlock(e.block);
1113         genIfBranch(genIfCondition(s, e.negateCondition));
1114       } else if (auto *s = e.getIf<Fortran::parser::ElseIfStmt>()) {
1115         startBlock(e.block);
1116         genIfBranch(genIfCondition(s));
1117       } else {
1118         genFIR(e);
1119       }
1120     }
1121   }
1122 
1123   void genFIR(const Fortran::parser::CaseConstruct &) {
1124     for (Fortran::lower::pft::Evaluation &e : getEval().getNestedEvaluations())
1125       genFIR(e);
1126   }
1127 
1128   template <typename A>
1129   void genNestedStatement(const Fortran::parser::Statement<A> &stmt) {
1130     setCurrentPosition(stmt.source);
1131     genFIR(stmt.statement);
1132   }
1133 
1134   /// Force the binding of an explicit symbol. This is used to bind and re-bind
1135   /// a concurrent control symbol to its value.
1136   void forceControlVariableBinding(const Fortran::semantics::Symbol *sym,
1137                                    mlir::Value inducVar) {
1138     mlir::Location loc = toLocation();
1139     assert(sym && "There must be a symbol to bind");
1140     mlir::Type toTy = genType(*sym);
1141     // FIXME: this should be a "per iteration" temporary.
1142     mlir::Value tmp = builder->createTemporary(
1143         loc, toTy, toStringRef(sym->name()),
1144         llvm::ArrayRef<mlir::NamedAttribute>{
1145             Fortran::lower::getAdaptToByRefAttr(*builder)});
1146     mlir::Value cast = builder->createConvert(loc, toTy, inducVar);
1147     builder->create<fir::StoreOp>(loc, cast, tmp);
1148     localSymbols.addSymbol(*sym, tmp, /*force=*/true);
1149   }
1150 
1151   /// Process a concurrent header for a FORALL. (Concurrent headers for DO
1152   /// CONCURRENT loops are lowered elsewhere.)
1153   void genFIR(const Fortran::parser::ConcurrentHeader &header) {
1154     llvm::SmallVector<mlir::Value> lows;
1155     llvm::SmallVector<mlir::Value> highs;
1156     llvm::SmallVector<mlir::Value> steps;
1157     if (explicitIterSpace.isOutermostForall()) {
1158       // For the outermost forall, we evaluate the bounds expressions once.
1159       // Contrastingly, if this forall is nested, the bounds expressions are
1160       // assumed to be pure, possibly dependent on outer concurrent control
1161       // variables, possibly variant with respect to arguments, and will be
1162       // re-evaluated.
1163       mlir::Location loc = toLocation();
1164       mlir::Type idxTy = builder->getIndexType();
1165       Fortran::lower::StatementContext &stmtCtx =
1166           explicitIterSpace.stmtContext();
1167       auto lowerExpr = [&](auto &e) {
1168         return fir::getBase(genExprValue(e, stmtCtx));
1169       };
1170       for (const Fortran::parser::ConcurrentControl &ctrl :
1171            std::get<std::list<Fortran::parser::ConcurrentControl>>(header.t)) {
1172         const Fortran::lower::SomeExpr *lo =
1173             Fortran::semantics::GetExpr(std::get<1>(ctrl.t));
1174         const Fortran::lower::SomeExpr *hi =
1175             Fortran::semantics::GetExpr(std::get<2>(ctrl.t));
1176         auto &optStep =
1177             std::get<std::optional<Fortran::parser::ScalarIntExpr>>(ctrl.t);
1178         lows.push_back(builder->createConvert(loc, idxTy, lowerExpr(*lo)));
1179         highs.push_back(builder->createConvert(loc, idxTy, lowerExpr(*hi)));
1180         steps.push_back(
1181             optStep.has_value()
1182                 ? builder->createConvert(
1183                       loc, idxTy,
1184                       lowerExpr(*Fortran::semantics::GetExpr(*optStep)))
1185                 : builder->createIntegerConstant(loc, idxTy, 1));
1186       }
1187     }
1188     auto lambda = [&, lows, highs, steps]() {
1189       // Create our iteration space from the header spec.
1190       mlir::Location loc = toLocation();
1191       mlir::Type idxTy = builder->getIndexType();
1192       llvm::SmallVector<fir::DoLoopOp> loops;
1193       Fortran::lower::StatementContext &stmtCtx =
1194           explicitIterSpace.stmtContext();
1195       auto lowerExpr = [&](auto &e) {
1196         return fir::getBase(genExprValue(e, stmtCtx));
1197       };
1198       const bool outermost = !lows.empty();
1199       std::size_t headerIndex = 0;
1200       for (const Fortran::parser::ConcurrentControl &ctrl :
1201            std::get<std::list<Fortran::parser::ConcurrentControl>>(header.t)) {
1202         const Fortran::semantics::Symbol *ctrlVar =
1203             std::get<Fortran::parser::Name>(ctrl.t).symbol;
1204         mlir::Value lb;
1205         mlir::Value ub;
1206         mlir::Value by;
1207         if (outermost) {
1208           assert(headerIndex < lows.size());
1209           if (headerIndex == 0)
1210             explicitIterSpace.resetInnerArgs();
1211           lb = lows[headerIndex];
1212           ub = highs[headerIndex];
1213           by = steps[headerIndex++];
1214         } else {
1215           const Fortran::lower::SomeExpr *lo =
1216               Fortran::semantics::GetExpr(std::get<1>(ctrl.t));
1217           const Fortran::lower::SomeExpr *hi =
1218               Fortran::semantics::GetExpr(std::get<2>(ctrl.t));
1219           auto &optStep =
1220               std::get<std::optional<Fortran::parser::ScalarIntExpr>>(ctrl.t);
1221           lb = builder->createConvert(loc, idxTy, lowerExpr(*lo));
1222           ub = builder->createConvert(loc, idxTy, lowerExpr(*hi));
1223           by = optStep.has_value()
1224                    ? builder->createConvert(
1225                          loc, idxTy,
1226                          lowerExpr(*Fortran::semantics::GetExpr(*optStep)))
1227                    : builder->createIntegerConstant(loc, idxTy, 1);
1228         }
1229         auto lp = builder->create<fir::DoLoopOp>(
1230             loc, lb, ub, by, /*unordered=*/true,
1231             /*finalCount=*/false, explicitIterSpace.getInnerArgs());
1232         if (!loops.empty() || !outermost)
1233           builder->create<fir::ResultOp>(loc, lp.getResults());
1234         explicitIterSpace.setInnerArgs(lp.getRegionIterArgs());
1235         builder->setInsertionPointToStart(lp.getBody());
1236         forceControlVariableBinding(ctrlVar, lp.getInductionVar());
1237         loops.push_back(lp);
1238       }
1239       if (outermost)
1240         explicitIterSpace.setOuterLoop(loops[0]);
1241       explicitIterSpace.appendLoops(loops);
1242       if (const auto &mask =
1243               std::get<std::optional<Fortran::parser::ScalarLogicalExpr>>(
1244                   header.t);
1245           mask.has_value()) {
1246         mlir::Type i1Ty = builder->getI1Type();
1247         fir::ExtendedValue maskExv =
1248             genExprValue(*Fortran::semantics::GetExpr(mask.value()), stmtCtx);
1249         mlir::Value cond =
1250             builder->createConvert(loc, i1Ty, fir::getBase(maskExv));
1251         auto ifOp = builder->create<fir::IfOp>(
1252             loc, explicitIterSpace.innerArgTypes(), cond,
1253             /*withElseRegion=*/true);
1254         builder->create<fir::ResultOp>(loc, ifOp.getResults());
1255         builder->setInsertionPointToStart(&ifOp.getElseRegion().front());
1256         builder->create<fir::ResultOp>(loc, explicitIterSpace.getInnerArgs());
1257         builder->setInsertionPointToStart(&ifOp.getThenRegion().front());
1258       }
1259     };
1260     // Push the lambda to gen the loop nest context.
1261     explicitIterSpace.pushLoopNest(lambda);
1262   }
1263 
1264   void genFIR(const Fortran::parser::ForallAssignmentStmt &stmt) {
1265     std::visit([&](const auto &x) { genFIR(x); }, stmt.u);
1266   }
1267 
1268   void genFIR(const Fortran::parser::EndForallStmt &) {
1269     cleanupExplicitSpace();
1270   }
1271 
1272   template <typename A>
1273   void prepareExplicitSpace(const A &forall) {
1274     if (!explicitIterSpace.isActive())
1275       analyzeExplicitSpace(forall);
1276     localSymbols.pushScope();
1277     explicitIterSpace.enter();
1278   }
1279 
1280   /// Cleanup all the FORALL context information when we exit.
1281   void cleanupExplicitSpace() {
1282     explicitIterSpace.leave();
1283     localSymbols.popScope();
1284   }
1285 
1286   /// Generate FIR for a FORALL statement.
1287   void genFIR(const Fortran::parser::ForallStmt &stmt) {
1288     prepareExplicitSpace(stmt);
1289     genFIR(std::get<
1290                Fortran::common::Indirection<Fortran::parser::ConcurrentHeader>>(
1291                stmt.t)
1292                .value());
1293     genFIR(std::get<Fortran::parser::UnlabeledStatement<
1294                Fortran::parser::ForallAssignmentStmt>>(stmt.t)
1295                .statement);
1296     cleanupExplicitSpace();
1297   }
1298 
1299   /// Generate FIR for a FORALL construct.
1300   void genFIR(const Fortran::parser::ForallConstruct &forall) {
1301     prepareExplicitSpace(forall);
1302     genNestedStatement(
1303         std::get<
1304             Fortran::parser::Statement<Fortran::parser::ForallConstructStmt>>(
1305             forall.t));
1306     for (const Fortran::parser::ForallBodyConstruct &s :
1307          std::get<std::list<Fortran::parser::ForallBodyConstruct>>(forall.t)) {
1308       std::visit(
1309           Fortran::common::visitors{
1310               [&](const Fortran::parser::WhereConstruct &b) { genFIR(b); },
1311               [&](const Fortran::common::Indirection<
1312                   Fortran::parser::ForallConstruct> &b) { genFIR(b.value()); },
1313               [&](const auto &b) { genNestedStatement(b); }},
1314           s.u);
1315     }
1316     genNestedStatement(
1317         std::get<Fortran::parser::Statement<Fortran::parser::EndForallStmt>>(
1318             forall.t));
1319   }
1320 
1321   /// Lower the concurrent header specification.
1322   void genFIR(const Fortran::parser::ForallConstructStmt &stmt) {
1323     genFIR(std::get<
1324                Fortran::common::Indirection<Fortran::parser::ConcurrentHeader>>(
1325                stmt.t)
1326                .value());
1327   }
1328 
1329   void genFIR(const Fortran::parser::CompilerDirective &) {
1330     TODO(toLocation(), "CompilerDirective lowering");
1331   }
1332 
1333   void genFIR(const Fortran::parser::OpenACCConstruct &acc) {
1334     mlir::OpBuilder::InsertPoint insertPt = builder->saveInsertionPoint();
1335     genOpenACCConstruct(*this, getEval(), acc);
1336     for (Fortran::lower::pft::Evaluation &e : getEval().getNestedEvaluations())
1337       genFIR(e);
1338     builder->restoreInsertionPoint(insertPt);
1339   }
1340 
1341   void genFIR(const Fortran::parser::OpenACCDeclarativeConstruct &accDecl) {
1342     mlir::OpBuilder::InsertPoint insertPt = builder->saveInsertionPoint();
1343     genOpenACCDeclarativeConstruct(*this, getEval(), accDecl);
1344     for (Fortran::lower::pft::Evaluation &e : getEval().getNestedEvaluations())
1345       genFIR(e);
1346     builder->restoreInsertionPoint(insertPt);
1347   }
1348 
1349   void genFIR(const Fortran::parser::OpenMPConstruct &omp) {
1350     mlir::OpBuilder::InsertPoint insertPt = builder->saveInsertionPoint();
1351     localSymbols.pushScope();
1352     Fortran::lower::genOpenMPConstruct(*this, getEval(), omp);
1353 
1354     for (Fortran::lower::pft::Evaluation &e : getEval().getNestedEvaluations())
1355       genFIR(e);
1356     localSymbols.popScope();
1357     builder->restoreInsertionPoint(insertPt);
1358   }
1359 
1360   void genFIR(const Fortran::parser::OpenMPDeclarativeConstruct &ompDecl) {
1361     mlir::OpBuilder::InsertPoint insertPt = builder->saveInsertionPoint();
1362     genOpenMPDeclarativeConstruct(*this, getEval(), ompDecl);
1363     for (Fortran::lower::pft::Evaluation &e : getEval().getNestedEvaluations())
1364       genFIR(e);
1365     builder->restoreInsertionPoint(insertPt);
1366   }
1367 
1368   /// Generate FIR for a SELECT CASE statement.
1369   /// The type may be CHARACTER, INTEGER, or LOGICAL.
1370   void genFIR(const Fortran::parser::SelectCaseStmt &stmt) {
1371     Fortran::lower::pft::Evaluation &eval = getEval();
1372     mlir::MLIRContext *context = builder->getContext();
1373     mlir::Location loc = toLocation();
1374     Fortran::lower::StatementContext stmtCtx;
1375     const Fortran::lower::SomeExpr *expr = Fortran::semantics::GetExpr(
1376         std::get<Fortran::parser::Scalar<Fortran::parser::Expr>>(stmt.t));
1377     bool isCharSelector = isCharacterCategory(expr->GetType()->category());
1378     bool isLogicalSelector = isLogicalCategory(expr->GetType()->category());
1379     auto charValue = [&](const Fortran::lower::SomeExpr *expr) {
1380       fir::ExtendedValue exv = genExprAddr(*expr, stmtCtx, &loc);
1381       return exv.match(
1382           [&](const fir::CharBoxValue &cbv) {
1383             return fir::factory::CharacterExprHelper{*builder, loc}
1384                 .createEmboxChar(cbv.getAddr(), cbv.getLen());
1385           },
1386           [&](auto) {
1387             fir::emitFatalError(loc, "not a character");
1388             return mlir::Value{};
1389           });
1390     };
1391     mlir::Value selector;
1392     if (isCharSelector) {
1393       selector = charValue(expr);
1394     } else {
1395       selector = createFIRExpr(loc, expr, stmtCtx);
1396       if (isLogicalSelector)
1397         selector = builder->createConvert(loc, builder->getI1Type(), selector);
1398     }
1399     mlir::Type selectType = selector.getType();
1400     llvm::SmallVector<mlir::Attribute> attrList;
1401     llvm::SmallVector<mlir::Value> valueList;
1402     llvm::SmallVector<mlir::Block *> blockList;
1403     mlir::Block *defaultBlock = eval.parentConstruct->constructExit->block;
1404     using CaseValue = Fortran::parser::Scalar<Fortran::parser::ConstantExpr>;
1405     auto addValue = [&](const CaseValue &caseValue) {
1406       const Fortran::lower::SomeExpr *expr =
1407           Fortran::semantics::GetExpr(caseValue.thing);
1408       if (isCharSelector)
1409         valueList.push_back(charValue(expr));
1410       else if (isLogicalSelector)
1411         valueList.push_back(builder->createConvert(
1412             loc, selectType, createFIRExpr(toLocation(), expr, stmtCtx)));
1413       else
1414         valueList.push_back(builder->createIntegerConstant(
1415             loc, selectType, *Fortran::evaluate::ToInt64(*expr)));
1416     };
1417     for (Fortran::lower::pft::Evaluation *e = eval.controlSuccessor; e;
1418          e = e->controlSuccessor) {
1419       const auto &caseStmt = e->getIf<Fortran::parser::CaseStmt>();
1420       assert(e->block && "missing CaseStmt block");
1421       const auto &caseSelector =
1422           std::get<Fortran::parser::CaseSelector>(caseStmt->t);
1423       const auto *caseValueRangeList =
1424           std::get_if<std::list<Fortran::parser::CaseValueRange>>(
1425               &caseSelector.u);
1426       if (!caseValueRangeList) {
1427         defaultBlock = e->block;
1428         continue;
1429       }
1430       for (const Fortran::parser::CaseValueRange &caseValueRange :
1431            *caseValueRangeList) {
1432         blockList.push_back(e->block);
1433         if (const auto *caseValue = std::get_if<CaseValue>(&caseValueRange.u)) {
1434           attrList.push_back(fir::PointIntervalAttr::get(context));
1435           addValue(*caseValue);
1436           continue;
1437         }
1438         const auto &caseRange =
1439             std::get<Fortran::parser::CaseValueRange::Range>(caseValueRange.u);
1440         if (caseRange.lower && caseRange.upper) {
1441           attrList.push_back(fir::ClosedIntervalAttr::get(context));
1442           addValue(*caseRange.lower);
1443           addValue(*caseRange.upper);
1444         } else if (caseRange.lower) {
1445           attrList.push_back(fir::LowerBoundAttr::get(context));
1446           addValue(*caseRange.lower);
1447         } else {
1448           attrList.push_back(fir::UpperBoundAttr::get(context));
1449           addValue(*caseRange.upper);
1450         }
1451       }
1452     }
1453     // Skip a logical default block that can never be referenced.
1454     if (isLogicalSelector && attrList.size() == 2)
1455       defaultBlock = eval.parentConstruct->constructExit->block;
1456     attrList.push_back(mlir::UnitAttr::get(context));
1457     blockList.push_back(defaultBlock);
1458 
1459     // Generate a fir::SelectCaseOp.
1460     // Explicit branch code is better for the LOGICAL type.  The CHARACTER type
1461     // does not yet have downstream support, and also uses explicit branch code.
1462     // The -no-structured-fir option can be used to force generation of INTEGER
1463     // type branch code.
1464     if (!isLogicalSelector && !isCharSelector && eval.lowerAsStructured()) {
1465       // Numeric selector is a ssa register, all temps that may have
1466       // been generated while evaluating it can be cleaned-up before the
1467       // fir.select_case.
1468       stmtCtx.finalize();
1469       builder->create<fir::SelectCaseOp>(loc, selector, attrList, valueList,
1470                                          blockList);
1471       return;
1472     }
1473 
1474     // Generate a sequence of case value comparisons and branches.
1475     auto caseValue = valueList.begin();
1476     auto caseBlock = blockList.begin();
1477     for (mlir::Attribute attr : attrList) {
1478       if (attr.isa<mlir::UnitAttr>()) {
1479         genFIRBranch(*caseBlock++);
1480         break;
1481       }
1482       auto genCond = [&](mlir::Value rhs,
1483                          mlir::arith::CmpIPredicate pred) -> mlir::Value {
1484         if (!isCharSelector)
1485           return builder->create<mlir::arith::CmpIOp>(loc, pred, selector, rhs);
1486         fir::factory::CharacterExprHelper charHelper{*builder, loc};
1487         std::pair<mlir::Value, mlir::Value> lhsVal =
1488             charHelper.createUnboxChar(selector);
1489         mlir::Value &lhsAddr = lhsVal.first;
1490         mlir::Value &lhsLen = lhsVal.second;
1491         std::pair<mlir::Value, mlir::Value> rhsVal =
1492             charHelper.createUnboxChar(rhs);
1493         mlir::Value &rhsAddr = rhsVal.first;
1494         mlir::Value &rhsLen = rhsVal.second;
1495         return fir::runtime::genCharCompare(*builder, loc, pred, lhsAddr,
1496                                             lhsLen, rhsAddr, rhsLen);
1497       };
1498       mlir::Block *newBlock = insertBlock(*caseBlock);
1499       if (attr.isa<fir::ClosedIntervalAttr>()) {
1500         mlir::Block *newBlock2 = insertBlock(*caseBlock);
1501         mlir::Value cond =
1502             genCond(*caseValue++, mlir::arith::CmpIPredicate::sge);
1503         genFIRConditionalBranch(cond, newBlock, newBlock2);
1504         builder->setInsertionPointToEnd(newBlock);
1505         mlir::Value cond2 =
1506             genCond(*caseValue++, mlir::arith::CmpIPredicate::sle);
1507         genFIRConditionalBranch(cond2, *caseBlock++, newBlock2);
1508         builder->setInsertionPointToEnd(newBlock2);
1509         continue;
1510       }
1511       mlir::arith::CmpIPredicate pred;
1512       if (attr.isa<fir::PointIntervalAttr>()) {
1513         pred = mlir::arith::CmpIPredicate::eq;
1514       } else if (attr.isa<fir::LowerBoundAttr>()) {
1515         pred = mlir::arith::CmpIPredicate::sge;
1516       } else {
1517         assert(attr.isa<fir::UpperBoundAttr>() && "unexpected predicate");
1518         pred = mlir::arith::CmpIPredicate::sle;
1519       }
1520       mlir::Value cond = genCond(*caseValue++, pred);
1521       genFIRConditionalBranch(cond, *caseBlock++, newBlock);
1522       builder->setInsertionPointToEnd(newBlock);
1523     }
1524     assert(caseValue == valueList.end() && caseBlock == blockList.end() &&
1525            "select case list mismatch");
1526     // Clean-up the selector at the end of the construct if it is a temporary
1527     // (which is possible with characters).
1528     mlir::OpBuilder::InsertPoint insertPt = builder->saveInsertionPoint();
1529     builder->setInsertionPointToEnd(eval.parentConstruct->constructExit->block);
1530     stmtCtx.finalize();
1531     builder->restoreInsertionPoint(insertPt);
1532   }
1533 
1534   fir::ExtendedValue
1535   genAssociateSelector(const Fortran::lower::SomeExpr &selector,
1536                        Fortran::lower::StatementContext &stmtCtx) {
1537     return isArraySectionWithoutVectorSubscript(selector)
1538                ? Fortran::lower::createSomeArrayBox(*this, selector,
1539                                                     localSymbols, stmtCtx)
1540                : genExprAddr(selector, stmtCtx);
1541   }
1542 
1543   void genFIR(const Fortran::parser::AssociateConstruct &) {
1544     Fortran::lower::StatementContext stmtCtx;
1545     Fortran::lower::pft::Evaluation &eval = getEval();
1546     for (Fortran::lower::pft::Evaluation &e : eval.getNestedEvaluations()) {
1547       if (auto *stmt = e.getIf<Fortran::parser::AssociateStmt>()) {
1548         if (eval.lowerAsUnstructured())
1549           maybeStartBlock(e.block);
1550         localSymbols.pushScope();
1551         for (const Fortran::parser::Association &assoc :
1552              std::get<std::list<Fortran::parser::Association>>(stmt->t)) {
1553           Fortran::semantics::Symbol &sym =
1554               *std::get<Fortran::parser::Name>(assoc.t).symbol;
1555           const Fortran::lower::SomeExpr &selector =
1556               *sym.get<Fortran::semantics::AssocEntityDetails>().expr();
1557           localSymbols.addSymbol(sym, genAssociateSelector(selector, stmtCtx));
1558         }
1559       } else if (e.getIf<Fortran::parser::EndAssociateStmt>()) {
1560         if (eval.lowerAsUnstructured())
1561           maybeStartBlock(e.block);
1562         stmtCtx.finalize();
1563         localSymbols.popScope();
1564       } else {
1565         genFIR(e);
1566       }
1567     }
1568   }
1569 
1570   void genFIR(const Fortran::parser::BlockConstruct &blockConstruct) {
1571     setCurrentPositionAt(blockConstruct);
1572     TODO(toLocation(), "BlockConstruct lowering");
1573   }
1574   void genFIR(const Fortran::parser::BlockStmt &) {
1575     TODO(toLocation(), "BlockStmt lowering");
1576   }
1577   void genFIR(const Fortran::parser::EndBlockStmt &) {
1578     TODO(toLocation(), "EndBlockStmt lowering");
1579   }
1580 
1581   void genFIR(const Fortran::parser::ChangeTeamConstruct &construct) {
1582     TODO(toLocation(), "ChangeTeamConstruct lowering");
1583   }
1584   void genFIR(const Fortran::parser::ChangeTeamStmt &stmt) {
1585     TODO(toLocation(), "ChangeTeamStmt lowering");
1586   }
1587   void genFIR(const Fortran::parser::EndChangeTeamStmt &stmt) {
1588     TODO(toLocation(), "EndChangeTeamStmt lowering");
1589   }
1590 
1591   void genFIR(const Fortran::parser::CriticalConstruct &criticalConstruct) {
1592     setCurrentPositionAt(criticalConstruct);
1593     TODO(toLocation(), "CriticalConstruct lowering");
1594   }
1595   void genFIR(const Fortran::parser::CriticalStmt &) {
1596     TODO(toLocation(), "CriticalStmt lowering");
1597   }
1598   void genFIR(const Fortran::parser::EndCriticalStmt &) {
1599     TODO(toLocation(), "EndCriticalStmt lowering");
1600   }
1601 
1602   void genFIR(const Fortran::parser::SelectRankConstruct &selectRankConstruct) {
1603     setCurrentPositionAt(selectRankConstruct);
1604     TODO(toLocation(), "SelectRankConstruct lowering");
1605   }
1606   void genFIR(const Fortran::parser::SelectRankStmt &) {
1607     TODO(toLocation(), "SelectRankStmt lowering");
1608   }
1609   void genFIR(const Fortran::parser::SelectRankCaseStmt &) {
1610     TODO(toLocation(), "SelectRankCaseStmt lowering");
1611   }
1612 
1613   void genFIR(const Fortran::parser::SelectTypeConstruct &selectTypeConstruct) {
1614     setCurrentPositionAt(selectTypeConstruct);
1615     TODO(toLocation(), "SelectTypeConstruct lowering");
1616   }
1617   void genFIR(const Fortran::parser::SelectTypeStmt &) {
1618     TODO(toLocation(), "SelectTypeStmt lowering");
1619   }
1620   void genFIR(const Fortran::parser::TypeGuardStmt &) {
1621     TODO(toLocation(), "TypeGuardStmt lowering");
1622   }
1623 
1624   //===--------------------------------------------------------------------===//
1625   // IO statements (see io.h)
1626   //===--------------------------------------------------------------------===//
1627 
1628   void genFIR(const Fortran::parser::BackspaceStmt &stmt) {
1629     mlir::Value iostat = genBackspaceStatement(*this, stmt);
1630     genIoConditionBranches(getEval(), stmt.v, iostat);
1631   }
1632   void genFIR(const Fortran::parser::CloseStmt &stmt) {
1633     mlir::Value iostat = genCloseStatement(*this, stmt);
1634     genIoConditionBranches(getEval(), stmt.v, iostat);
1635   }
1636   void genFIR(const Fortran::parser::EndfileStmt &stmt) {
1637     mlir::Value iostat = genEndfileStatement(*this, stmt);
1638     genIoConditionBranches(getEval(), stmt.v, iostat);
1639   }
1640   void genFIR(const Fortran::parser::FlushStmt &stmt) {
1641     mlir::Value iostat = genFlushStatement(*this, stmt);
1642     genIoConditionBranches(getEval(), stmt.v, iostat);
1643   }
1644   void genFIR(const Fortran::parser::InquireStmt &stmt) {
1645     mlir::Value iostat = genInquireStatement(*this, stmt);
1646     if (const auto *specs =
1647             std::get_if<std::list<Fortran::parser::InquireSpec>>(&stmt.u))
1648       genIoConditionBranches(getEval(), *specs, iostat);
1649   }
1650   void genFIR(const Fortran::parser::OpenStmt &stmt) {
1651     mlir::Value iostat = genOpenStatement(*this, stmt);
1652     genIoConditionBranches(getEval(), stmt.v, iostat);
1653   }
1654   void genFIR(const Fortran::parser::PrintStmt &stmt) {
1655     genPrintStatement(*this, stmt);
1656   }
1657   void genFIR(const Fortran::parser::ReadStmt &stmt) {
1658     mlir::Value iostat = genReadStatement(*this, stmt);
1659     genIoConditionBranches(getEval(), stmt.controls, iostat);
1660   }
1661   void genFIR(const Fortran::parser::RewindStmt &stmt) {
1662     mlir::Value iostat = genRewindStatement(*this, stmt);
1663     genIoConditionBranches(getEval(), stmt.v, iostat);
1664   }
1665   void genFIR(const Fortran::parser::WaitStmt &stmt) {
1666     mlir::Value iostat = genWaitStatement(*this, stmt);
1667     genIoConditionBranches(getEval(), stmt.v, iostat);
1668   }
1669   void genFIR(const Fortran::parser::WriteStmt &stmt) {
1670     mlir::Value iostat = genWriteStatement(*this, stmt);
1671     genIoConditionBranches(getEval(), stmt.controls, iostat);
1672   }
1673 
1674   template <typename A>
1675   void genIoConditionBranches(Fortran::lower::pft::Evaluation &eval,
1676                               const A &specList, mlir::Value iostat) {
1677     if (!iostat)
1678       return;
1679 
1680     mlir::Block *endBlock = nullptr;
1681     mlir::Block *eorBlock = nullptr;
1682     mlir::Block *errBlock = nullptr;
1683     for (const auto &spec : specList) {
1684       std::visit(Fortran::common::visitors{
1685                      [&](const Fortran::parser::EndLabel &label) {
1686                        endBlock = blockOfLabel(eval, label.v);
1687                      },
1688                      [&](const Fortran::parser::EorLabel &label) {
1689                        eorBlock = blockOfLabel(eval, label.v);
1690                      },
1691                      [&](const Fortran::parser::ErrLabel &label) {
1692                        errBlock = blockOfLabel(eval, label.v);
1693                      },
1694                      [](const auto &) {}},
1695                  spec.u);
1696     }
1697     if (!endBlock && !eorBlock && !errBlock)
1698       return;
1699 
1700     mlir::Location loc = toLocation();
1701     mlir::Type indexType = builder->getIndexType();
1702     mlir::Value selector = builder->createConvert(loc, indexType, iostat);
1703     llvm::SmallVector<int64_t> indexList;
1704     llvm::SmallVector<mlir::Block *> blockList;
1705     if (eorBlock) {
1706       indexList.push_back(Fortran::runtime::io::IostatEor);
1707       blockList.push_back(eorBlock);
1708     }
1709     if (endBlock) {
1710       indexList.push_back(Fortran::runtime::io::IostatEnd);
1711       blockList.push_back(endBlock);
1712     }
1713     if (errBlock) {
1714       indexList.push_back(0);
1715       blockList.push_back(eval.nonNopSuccessor().block);
1716       // ERR label statement is the default successor.
1717       blockList.push_back(errBlock);
1718     } else {
1719       // Fallthrough successor statement is the default successor.
1720       blockList.push_back(eval.nonNopSuccessor().block);
1721     }
1722     builder->create<fir::SelectOp>(loc, selector, indexList, blockList);
1723   }
1724 
1725   //===--------------------------------------------------------------------===//
1726   // Memory allocation and deallocation
1727   //===--------------------------------------------------------------------===//
1728 
1729   void genFIR(const Fortran::parser::AllocateStmt &stmt) {
1730     Fortran::lower::genAllocateStmt(*this, stmt, toLocation());
1731   }
1732 
1733   void genFIR(const Fortran::parser::DeallocateStmt &stmt) {
1734     Fortran::lower::genDeallocateStmt(*this, stmt, toLocation());
1735   }
1736 
1737   /// Nullify pointer object list
1738   ///
1739   /// For each pointer object, reset the pointer to a disassociated status.
1740   /// We do this by setting each pointer to null.
1741   void genFIR(const Fortran::parser::NullifyStmt &stmt) {
1742     mlir::Location loc = toLocation();
1743     for (auto &pointerObject : stmt.v) {
1744       const Fortran::lower::SomeExpr *expr =
1745           Fortran::semantics::GetExpr(pointerObject);
1746       assert(expr);
1747       fir::MutableBoxValue box = genExprMutableBox(loc, *expr);
1748       fir::factory::disassociateMutableBox(*builder, loc, box);
1749     }
1750   }
1751 
1752   //===--------------------------------------------------------------------===//
1753 
1754   void genFIR(const Fortran::parser::EventPostStmt &stmt) {
1755     genEventPostStatement(*this, stmt);
1756   }
1757 
1758   void genFIR(const Fortran::parser::EventWaitStmt &stmt) {
1759     genEventWaitStatement(*this, stmt);
1760   }
1761 
1762   void genFIR(const Fortran::parser::FormTeamStmt &stmt) {
1763     genFormTeamStatement(*this, getEval(), stmt);
1764   }
1765 
1766   void genFIR(const Fortran::parser::LockStmt &stmt) {
1767     genLockStatement(*this, stmt);
1768   }
1769 
1770   fir::ExtendedValue
1771   genInitializerExprValue(const Fortran::lower::SomeExpr &expr,
1772                           Fortran::lower::StatementContext &stmtCtx) {
1773     return Fortran::lower::createSomeInitializerExpression(
1774         toLocation(), *this, expr, localSymbols, stmtCtx);
1775   }
1776 
1777   /// Return true if the current context is a conditionalized and implied
1778   /// iteration space.
1779   bool implicitIterationSpace() { return !implicitIterSpace.empty(); }
1780 
1781   /// Return true if context is currently an explicit iteration space. A scalar
1782   /// assignment expression may be contextually within a user-defined iteration
1783   /// space, transforming it into an array expression.
1784   bool explicitIterationSpace() { return explicitIterSpace.isActive(); }
1785 
1786   /// Generate an array assignment.
1787   /// This is an assignment expression with rank > 0. The assignment may or may
1788   /// not be in a WHERE and/or FORALL context.
1789   void genArrayAssignment(const Fortran::evaluate::Assignment &assign,
1790                           Fortran::lower::StatementContext &stmtCtx) {
1791     if (isWholeAllocatable(assign.lhs)) {
1792       // Assignment to allocatables may require the lhs to be
1793       // deallocated/reallocated. See Fortran 2018 10.2.1.3 p3
1794       Fortran::lower::createAllocatableArrayAssignment(
1795           *this, assign.lhs, assign.rhs, explicitIterSpace, implicitIterSpace,
1796           localSymbols, stmtCtx);
1797       return;
1798     }
1799 
1800     if (!implicitIterationSpace() && !explicitIterationSpace()) {
1801       // No masks and the iteration space is implied by the array, so create a
1802       // simple array assignment.
1803       Fortran::lower::createSomeArrayAssignment(*this, assign.lhs, assign.rhs,
1804                                                 localSymbols, stmtCtx);
1805       return;
1806     }
1807 
1808     // If there is an explicit iteration space, generate an array assignment
1809     // with a user-specified iteration space and possibly with masks. These
1810     // assignments may *appear* to be scalar expressions, but the scalar
1811     // expression is evaluated at all points in the user-defined space much like
1812     // an ordinary array assignment. More specifically, the semantics inside the
1813     // FORALL much more closely resembles that of WHERE than a scalar
1814     // assignment.
1815     // Otherwise, generate a masked array assignment. The iteration space is
1816     // implied by the lhs array expression.
1817     Fortran::lower::createAnyMaskedArrayAssignment(
1818         *this, assign.lhs, assign.rhs, explicitIterSpace, implicitIterSpace,
1819         localSymbols,
1820         explicitIterationSpace() ? explicitIterSpace.stmtContext()
1821                                  : implicitIterSpace.stmtContext());
1822   }
1823 
1824   static bool
1825   isArraySectionWithoutVectorSubscript(const Fortran::lower::SomeExpr &expr) {
1826     return expr.Rank() > 0 && Fortran::evaluate::IsVariable(expr) &&
1827            !Fortran::evaluate::UnwrapWholeSymbolDataRef(expr) &&
1828            !Fortran::evaluate::HasVectorSubscript(expr);
1829   }
1830 
1831 #if !defined(NDEBUG)
1832   static bool isFuncResultDesignator(const Fortran::lower::SomeExpr &expr) {
1833     const Fortran::semantics::Symbol *sym =
1834         Fortran::evaluate::GetFirstSymbol(expr);
1835     return sym && sym->IsFuncResult();
1836   }
1837 #endif
1838 
1839   static bool isWholeAllocatable(const Fortran::lower::SomeExpr &expr) {
1840     const Fortran::semantics::Symbol *sym =
1841         Fortran::evaluate::UnwrapWholeSymbolOrComponentDataRef(expr);
1842     return sym && Fortran::semantics::IsAllocatable(*sym);
1843   }
1844 
1845   /// Shared for both assignments and pointer assignments.
1846   void genAssignment(const Fortran::evaluate::Assignment &assign) {
1847     Fortran::lower::StatementContext stmtCtx;
1848     mlir::Location loc = toLocation();
1849     if (explicitIterationSpace()) {
1850       Fortran::lower::createArrayLoads(*this, explicitIterSpace, localSymbols);
1851       explicitIterSpace.genLoopNest();
1852     }
1853     std::visit(
1854         Fortran::common::visitors{
1855             // [1] Plain old assignment.
1856             [&](const Fortran::evaluate::Assignment::Intrinsic &) {
1857               const Fortran::semantics::Symbol *sym =
1858                   Fortran::evaluate::GetLastSymbol(assign.lhs);
1859 
1860               if (!sym)
1861                 TODO(loc, "assignment to pointer result of function reference");
1862 
1863               std::optional<Fortran::evaluate::DynamicType> lhsType =
1864                   assign.lhs.GetType();
1865               assert(lhsType && "lhs cannot be typeless");
1866               // Assignment to polymorphic allocatables may require changing the
1867               // variable dynamic type (See Fortran 2018 10.2.1.3 p3).
1868               if (lhsType->IsPolymorphic() && isWholeAllocatable(assign.lhs))
1869                 TODO(loc, "assignment to polymorphic allocatable");
1870 
1871               // Note: No ad-hoc handling for pointers is required here. The
1872               // target will be assigned as per 2018 10.2.1.3 p2. genExprAddr
1873               // on a pointer returns the target address and not the address of
1874               // the pointer variable.
1875 
1876               if (assign.lhs.Rank() > 0 || explicitIterationSpace()) {
1877                 // Array assignment
1878                 // See Fortran 2018 10.2.1.3 p5, p6, and p7
1879                 genArrayAssignment(assign, stmtCtx);
1880                 return;
1881               }
1882 
1883               // Scalar assignment
1884               const bool isNumericScalar =
1885                   isNumericScalarCategory(lhsType->category());
1886               fir::ExtendedValue rhs = isNumericScalar
1887                                            ? genExprValue(assign.rhs, stmtCtx)
1888                                            : genExprAddr(assign.rhs, stmtCtx);
1889               bool lhsIsWholeAllocatable = isWholeAllocatable(assign.lhs);
1890               llvm::Optional<fir::factory::MutableBoxReallocation> lhsRealloc;
1891               llvm::Optional<fir::MutableBoxValue> lhsMutableBox;
1892               auto lhs = [&]() -> fir::ExtendedValue {
1893                 if (lhsIsWholeAllocatable) {
1894                   lhsMutableBox = genExprMutableBox(loc, assign.lhs);
1895                   llvm::SmallVector<mlir::Value> lengthParams;
1896                   if (const fir::CharBoxValue *charBox = rhs.getCharBox())
1897                     lengthParams.push_back(charBox->getLen());
1898                   else if (fir::isDerivedWithLengthParameters(rhs))
1899                     TODO(loc, "assignment to derived type allocatable with "
1900                               "length parameters");
1901                   lhsRealloc = fir::factory::genReallocIfNeeded(
1902                       *builder, loc, *lhsMutableBox,
1903                       /*shape=*/llvm::None, lengthParams);
1904                   return lhsRealloc->newValue;
1905                 }
1906                 return genExprAddr(assign.lhs, stmtCtx);
1907               }();
1908 
1909               if (isNumericScalar) {
1910                 // Fortran 2018 10.2.1.3 p8 and p9
1911                 // Conversions should have been inserted by semantic analysis,
1912                 // but they can be incorrect between the rhs and lhs. Correct
1913                 // that here.
1914                 mlir::Value addr = fir::getBase(lhs);
1915                 mlir::Value val = fir::getBase(rhs);
1916                 // A function with multiple entry points returning different
1917                 // types tags all result variables with one of the largest
1918                 // types to allow them to share the same storage.  Assignment
1919                 // to a result variable of one of the other types requires
1920                 // conversion to the actual type.
1921                 mlir::Type toTy = genType(assign.lhs);
1922                 mlir::Value cast =
1923                     builder->convertWithSemantics(loc, toTy, val);
1924                 if (fir::dyn_cast_ptrEleTy(addr.getType()) != toTy) {
1925                   assert(isFuncResultDesignator(assign.lhs) && "type mismatch");
1926                   addr = builder->createConvert(
1927                       toLocation(), builder->getRefType(toTy), addr);
1928                 }
1929                 builder->create<fir::StoreOp>(loc, cast, addr);
1930               } else if (isCharacterCategory(lhsType->category())) {
1931                 // Fortran 2018 10.2.1.3 p10 and p11
1932                 fir::factory::CharacterExprHelper{*builder, loc}.createAssign(
1933                     lhs, rhs);
1934               } else if (isDerivedCategory(lhsType->category())) {
1935                 // Fortran 2018 10.2.1.3 p13 and p14
1936                 // Recursively gen an assignment on each element pair.
1937                 fir::factory::genRecordAssignment(*builder, loc, lhs, rhs);
1938               } else {
1939                 llvm_unreachable("unknown category");
1940               }
1941               if (lhsIsWholeAllocatable)
1942                 fir::factory::finalizeRealloc(
1943                     *builder, loc, lhsMutableBox.getValue(),
1944                     /*lbounds=*/llvm::None, /*takeLboundsIfRealloc=*/false,
1945                     lhsRealloc.getValue());
1946             },
1947 
1948             // [2] User defined assignment. If the context is a scalar
1949             // expression then call the procedure.
1950             [&](const Fortran::evaluate::ProcedureRef &procRef) {
1951               Fortran::lower::StatementContext &ctx =
1952                   explicitIterationSpace() ? explicitIterSpace.stmtContext()
1953                                            : stmtCtx;
1954               Fortran::lower::createSubroutineCall(
1955                   *this, procRef, explicitIterSpace, implicitIterSpace,
1956                   localSymbols, ctx, /*isUserDefAssignment=*/true);
1957             },
1958 
1959             // [3] Pointer assignment with possibly empty bounds-spec. R1035: a
1960             // bounds-spec is a lower bound value.
1961             [&](const Fortran::evaluate::Assignment::BoundsSpec &lbExprs) {
1962               if (IsProcedure(assign.rhs))
1963                 TODO(loc, "procedure pointer assignment");
1964               std::optional<Fortran::evaluate::DynamicType> lhsType =
1965                   assign.lhs.GetType();
1966               std::optional<Fortran::evaluate::DynamicType> rhsType =
1967                   assign.rhs.GetType();
1968               // Polymorphic lhs/rhs may need more care. See F2018 10.2.2.3.
1969               if ((lhsType && lhsType->IsPolymorphic()) ||
1970                   (rhsType && rhsType->IsPolymorphic()))
1971                 TODO(loc, "pointer assignment involving polymorphic entity");
1972 
1973               // FIXME: in the explicit space context, we want to use
1974               // ScalarArrayExprLowering here.
1975               fir::MutableBoxValue lhs = genExprMutableBox(loc, assign.lhs);
1976               llvm::SmallVector<mlir::Value> lbounds;
1977               for (const Fortran::evaluate::ExtentExpr &lbExpr : lbExprs)
1978                 lbounds.push_back(
1979                     fir::getBase(genExprValue(toEvExpr(lbExpr), stmtCtx)));
1980               Fortran::lower::associateMutableBox(*this, loc, lhs, assign.rhs,
1981                                                   lbounds, stmtCtx);
1982               if (explicitIterationSpace()) {
1983                 mlir::ValueRange inners = explicitIterSpace.getInnerArgs();
1984                 if (!inners.empty()) {
1985                   // TODO: should force a copy-in/copy-out here.
1986                   // e.g., obj%ptr(i+1) => obj%ptr(i)
1987                   builder->create<fir::ResultOp>(loc, inners);
1988                 }
1989               }
1990             },
1991 
1992             // [4] Pointer assignment with bounds-remapping. R1036: a
1993             // bounds-remapping is a pair, lower bound and upper bound.
1994             [&](const Fortran::evaluate::Assignment::BoundsRemapping
1995                     &boundExprs) {
1996               std::optional<Fortran::evaluate::DynamicType> lhsType =
1997                   assign.lhs.GetType();
1998               std::optional<Fortran::evaluate::DynamicType> rhsType =
1999                   assign.rhs.GetType();
2000               // Polymorphic lhs/rhs may need more care. See F2018 10.2.2.3.
2001               if ((lhsType && lhsType->IsPolymorphic()) ||
2002                   (rhsType && rhsType->IsPolymorphic()))
2003                 TODO(loc, "pointer assignment involving polymorphic entity");
2004 
2005               // FIXME: in the explicit space context, we want to use
2006               // ScalarArrayExprLowering here.
2007               fir::MutableBoxValue lhs = genExprMutableBox(loc, assign.lhs);
2008               if (Fortran::evaluate::UnwrapExpr<Fortran::evaluate::NullPointer>(
2009                       assign.rhs)) {
2010                 fir::factory::disassociateMutableBox(*builder, loc, lhs);
2011                 return;
2012               }
2013               llvm::SmallVector<mlir::Value> lbounds;
2014               llvm::SmallVector<mlir::Value> ubounds;
2015               for (const std::pair<Fortran::evaluate::ExtentExpr,
2016                                    Fortran::evaluate::ExtentExpr> &pair :
2017                    boundExprs) {
2018                 const Fortran::evaluate::ExtentExpr &lbExpr = pair.first;
2019                 const Fortran::evaluate::ExtentExpr &ubExpr = pair.second;
2020                 lbounds.push_back(
2021                     fir::getBase(genExprValue(toEvExpr(lbExpr), stmtCtx)));
2022                 ubounds.push_back(
2023                     fir::getBase(genExprValue(toEvExpr(ubExpr), stmtCtx)));
2024               }
2025               // Do not generate a temp in case rhs is an array section.
2026               fir::ExtendedValue rhs =
2027                   isArraySectionWithoutVectorSubscript(assign.rhs)
2028                       ? Fortran::lower::createSomeArrayBox(
2029                             *this, assign.rhs, localSymbols, stmtCtx)
2030                       : genExprAddr(assign.rhs, stmtCtx);
2031               fir::factory::associateMutableBoxWithRemap(*builder, loc, lhs,
2032                                                          rhs, lbounds, ubounds);
2033               if (explicitIterationSpace()) {
2034                 mlir::ValueRange inners = explicitIterSpace.getInnerArgs();
2035                 if (!inners.empty()) {
2036                   // TODO: should force a copy-in/copy-out here.
2037                   // e.g., obj%ptr(i+1) => obj%ptr(i)
2038                   builder->create<fir::ResultOp>(loc, inners);
2039                 }
2040               }
2041             },
2042         },
2043         assign.u);
2044     if (explicitIterationSpace())
2045       Fortran::lower::createArrayMergeStores(*this, explicitIterSpace);
2046   }
2047 
2048   void genFIR(const Fortran::parser::WhereConstruct &c) {
2049     implicitIterSpace.growStack();
2050     genNestedStatement(
2051         std::get<
2052             Fortran::parser::Statement<Fortran::parser::WhereConstructStmt>>(
2053             c.t));
2054     for (const auto &body :
2055          std::get<std::list<Fortran::parser::WhereBodyConstruct>>(c.t))
2056       genFIR(body);
2057     for (const auto &e :
2058          std::get<std::list<Fortran::parser::WhereConstruct::MaskedElsewhere>>(
2059              c.t))
2060       genFIR(e);
2061     if (const auto &e =
2062             std::get<std::optional<Fortran::parser::WhereConstruct::Elsewhere>>(
2063                 c.t);
2064         e.has_value())
2065       genFIR(*e);
2066     genNestedStatement(
2067         std::get<Fortran::parser::Statement<Fortran::parser::EndWhereStmt>>(
2068             c.t));
2069   }
2070   void genFIR(const Fortran::parser::WhereBodyConstruct &body) {
2071     std::visit(
2072         Fortran::common::visitors{
2073             [&](const Fortran::parser::Statement<
2074                 Fortran::parser::AssignmentStmt> &stmt) {
2075               genNestedStatement(stmt);
2076             },
2077             [&](const Fortran::parser::Statement<Fortran::parser::WhereStmt>
2078                     &stmt) { genNestedStatement(stmt); },
2079             [&](const Fortran::common::Indirection<
2080                 Fortran::parser::WhereConstruct> &c) { genFIR(c.value()); },
2081         },
2082         body.u);
2083   }
2084   void genFIR(const Fortran::parser::WhereConstructStmt &stmt) {
2085     implicitIterSpace.append(Fortran::semantics::GetExpr(
2086         std::get<Fortran::parser::LogicalExpr>(stmt.t)));
2087   }
2088   void genFIR(const Fortran::parser::WhereConstruct::MaskedElsewhere &ew) {
2089     genNestedStatement(
2090         std::get<
2091             Fortran::parser::Statement<Fortran::parser::MaskedElsewhereStmt>>(
2092             ew.t));
2093     for (const auto &body :
2094          std::get<std::list<Fortran::parser::WhereBodyConstruct>>(ew.t))
2095       genFIR(body);
2096   }
2097   void genFIR(const Fortran::parser::MaskedElsewhereStmt &stmt) {
2098     implicitIterSpace.append(Fortran::semantics::GetExpr(
2099         std::get<Fortran::parser::LogicalExpr>(stmt.t)));
2100   }
2101   void genFIR(const Fortran::parser::WhereConstruct::Elsewhere &ew) {
2102     genNestedStatement(
2103         std::get<Fortran::parser::Statement<Fortran::parser::ElsewhereStmt>>(
2104             ew.t));
2105     for (const auto &body :
2106          std::get<std::list<Fortran::parser::WhereBodyConstruct>>(ew.t))
2107       genFIR(body);
2108   }
2109   void genFIR(const Fortran::parser::ElsewhereStmt &stmt) {
2110     implicitIterSpace.append(nullptr);
2111   }
2112   void genFIR(const Fortran::parser::EndWhereStmt &) {
2113     implicitIterSpace.shrinkStack();
2114   }
2115 
2116   void genFIR(const Fortran::parser::WhereStmt &stmt) {
2117     Fortran::lower::StatementContext stmtCtx;
2118     const auto &assign = std::get<Fortran::parser::AssignmentStmt>(stmt.t);
2119     implicitIterSpace.growStack();
2120     implicitIterSpace.append(Fortran::semantics::GetExpr(
2121         std::get<Fortran::parser::LogicalExpr>(stmt.t)));
2122     genAssignment(*assign.typedAssignment->v);
2123     implicitIterSpace.shrinkStack();
2124   }
2125 
2126   void genFIR(const Fortran::parser::PointerAssignmentStmt &stmt) {
2127     genAssignment(*stmt.typedAssignment->v);
2128   }
2129 
2130   void genFIR(const Fortran::parser::AssignmentStmt &stmt) {
2131     genAssignment(*stmt.typedAssignment->v);
2132   }
2133 
2134   void genFIR(const Fortran::parser::SyncAllStmt &stmt) {
2135     genSyncAllStatement(*this, stmt);
2136   }
2137 
2138   void genFIR(const Fortran::parser::SyncImagesStmt &stmt) {
2139     genSyncImagesStatement(*this, stmt);
2140   }
2141 
2142   void genFIR(const Fortran::parser::SyncMemoryStmt &stmt) {
2143     genSyncMemoryStatement(*this, stmt);
2144   }
2145 
2146   void genFIR(const Fortran::parser::SyncTeamStmt &stmt) {
2147     genSyncTeamStatement(*this, stmt);
2148   }
2149 
2150   void genFIR(const Fortran::parser::UnlockStmt &stmt) {
2151     genUnlockStatement(*this, stmt);
2152   }
2153 
2154   void genFIR(const Fortran::parser::AssignStmt &stmt) {
2155     const Fortran::semantics::Symbol &symbol =
2156         *std::get<Fortran::parser::Name>(stmt.t).symbol;
2157     mlir::Location loc = toLocation();
2158     mlir::Value labelValue = builder->createIntegerConstant(
2159         loc, genType(symbol), std::get<Fortran::parser::Label>(stmt.t));
2160     builder->create<fir::StoreOp>(loc, labelValue, getSymbolAddress(symbol));
2161   }
2162 
2163   void genFIR(const Fortran::parser::FormatStmt &) {
2164     // do nothing.
2165 
2166     // FORMAT statements have no semantics. They may be lowered if used by a
2167     // data transfer statement.
2168   }
2169 
2170   void genFIR(const Fortran::parser::PauseStmt &stmt) {
2171     genPauseStatement(*this, stmt);
2172   }
2173 
2174   // call FAIL IMAGE in runtime
2175   void genFIR(const Fortran::parser::FailImageStmt &stmt) {
2176     genFailImageStatement(*this);
2177   }
2178 
2179   // call STOP, ERROR STOP in runtime
2180   void genFIR(const Fortran::parser::StopStmt &stmt) {
2181     genStopStatement(*this, stmt);
2182   }
2183 
2184   void genFIR(const Fortran::parser::ReturnStmt &stmt) {
2185     Fortran::lower::pft::FunctionLikeUnit *funit =
2186         getEval().getOwningProcedure();
2187     assert(funit && "not inside main program, function or subroutine");
2188     if (funit->isMainProgram()) {
2189       genExitRoutine();
2190       return;
2191     }
2192     mlir::Location loc = toLocation();
2193     if (stmt.v) {
2194       // Alternate return statement - If this is a subroutine where some
2195       // alternate entries have alternate returns, but the active entry point
2196       // does not, ignore the alternate return value.  Otherwise, assign it
2197       // to the compiler-generated result variable.
2198       const Fortran::semantics::Symbol &symbol = funit->getSubprogramSymbol();
2199       if (Fortran::semantics::HasAlternateReturns(symbol)) {
2200         Fortran::lower::StatementContext stmtCtx;
2201         const Fortran::lower::SomeExpr *expr =
2202             Fortran::semantics::GetExpr(*stmt.v);
2203         assert(expr && "missing alternate return expression");
2204         mlir::Value altReturnIndex = builder->createConvert(
2205             loc, builder->getIndexType(), createFIRExpr(loc, expr, stmtCtx));
2206         builder->create<fir::StoreOp>(loc, altReturnIndex,
2207                                       getAltReturnResult(symbol));
2208       }
2209     }
2210     // Branch to the last block of the SUBROUTINE, which has the actual return.
2211     if (!funit->finalBlock) {
2212       mlir::OpBuilder::InsertPoint insPt = builder->saveInsertionPoint();
2213       funit->finalBlock = builder->createBlock(&builder->getRegion());
2214       builder->restoreInsertionPoint(insPt);
2215     }
2216     builder->create<mlir::cf::BranchOp>(loc, funit->finalBlock);
2217   }
2218 
2219   void genFIR(const Fortran::parser::CycleStmt &) {
2220     genFIRBranch(getEval().controlSuccessor->block);
2221   }
2222   void genFIR(const Fortran::parser::ExitStmt &) {
2223     genFIRBranch(getEval().controlSuccessor->block);
2224   }
2225   void genFIR(const Fortran::parser::GotoStmt &) {
2226     genFIRBranch(getEval().controlSuccessor->block);
2227   }
2228 
2229   // Nop statements - No code, or code is generated at the construct level.
2230   void genFIR(const Fortran::parser::AssociateStmt &) {}       // nop
2231   void genFIR(const Fortran::parser::CaseStmt &) {}            // nop
2232   void genFIR(const Fortran::parser::ContinueStmt &) {}        // nop
2233   void genFIR(const Fortran::parser::ElseIfStmt &) {}          // nop
2234   void genFIR(const Fortran::parser::ElseStmt &) {}            // nop
2235   void genFIR(const Fortran::parser::EndAssociateStmt &) {}    // nop
2236   void genFIR(const Fortran::parser::EndDoStmt &) {}           // nop
2237   void genFIR(const Fortran::parser::EndFunctionStmt &) {}     // nop
2238   void genFIR(const Fortran::parser::EndIfStmt &) {}           // nop
2239   void genFIR(const Fortran::parser::EndMpSubprogramStmt &) {} // nop
2240   void genFIR(const Fortran::parser::EndSelectStmt &) {}       // nop
2241   void genFIR(const Fortran::parser::EndSubroutineStmt &) {}   // nop
2242   void genFIR(const Fortran::parser::EntryStmt &) {}           // nop
2243   void genFIR(const Fortran::parser::IfStmt &) {}              // nop
2244   void genFIR(const Fortran::parser::IfThenStmt &) {}          // nop
2245   void genFIR(const Fortran::parser::NonLabelDoStmt &) {}      // nop
2246 
2247   void genFIR(const Fortran::parser::OmpEndLoopDirective &) {
2248     TODO(toLocation(), "OmpEndLoopDirective lowering");
2249   }
2250 
2251   void genFIR(const Fortran::parser::NamelistStmt &) {
2252     TODO(toLocation(), "NamelistStmt lowering");
2253   }
2254 
2255   /// Generate FIR for the Evaluation `eval`.
2256   void genFIR(Fortran::lower::pft::Evaluation &eval,
2257               bool unstructuredContext = true) {
2258     if (unstructuredContext) {
2259       // When transitioning from unstructured to structured code,
2260       // the structured code could be a target that starts a new block.
2261       maybeStartBlock(eval.isConstruct() && eval.lowerAsStructured()
2262                           ? eval.getFirstNestedEvaluation().block
2263                           : eval.block);
2264     }
2265 
2266     setCurrentEval(eval);
2267     setCurrentPosition(eval.position);
2268     eval.visit([&](const auto &stmt) { genFIR(stmt); });
2269 
2270     if (unstructuredContext && blockIsUnterminated()) {
2271       // Exit from an unstructured IF or SELECT construct block.
2272       Fortran::lower::pft::Evaluation *successor{};
2273       if (eval.isActionStmt())
2274         successor = eval.controlSuccessor;
2275       else if (eval.isConstruct() &&
2276                eval.getLastNestedEvaluation()
2277                    .lexicalSuccessor->isIntermediateConstructStmt())
2278         successor = eval.constructExit;
2279       if (successor && successor->block)
2280         genFIRBranch(successor->block);
2281     }
2282   }
2283 
2284   /// Map mlir function block arguments to the corresponding Fortran dummy
2285   /// variables. When the result is passed as a hidden argument, the Fortran
2286   /// result is also mapped. The symbol map is used to hold this mapping.
2287   void mapDummiesAndResults(Fortran::lower::pft::FunctionLikeUnit &funit,
2288                             const Fortran::lower::CalleeInterface &callee) {
2289     assert(builder && "require a builder object at this point");
2290     using PassBy = Fortran::lower::CalleeInterface::PassEntityBy;
2291     auto mapPassedEntity = [&](const auto arg) -> void {
2292       if (arg.passBy == PassBy::AddressAndLength) {
2293         // TODO: now that fir call has some attributes regarding character
2294         // return, PassBy::AddressAndLength should be retired.
2295         mlir::Location loc = toLocation();
2296         fir::factory::CharacterExprHelper charHelp{*builder, loc};
2297         mlir::Value box =
2298             charHelp.createEmboxChar(arg.firArgument, arg.firLength);
2299         addSymbol(arg.entity->get(), box);
2300       } else {
2301         if (arg.entity.has_value()) {
2302           addSymbol(arg.entity->get(), arg.firArgument);
2303         } else {
2304           assert(funit.parentHasHostAssoc());
2305           funit.parentHostAssoc().internalProcedureBindings(*this,
2306                                                             localSymbols);
2307         }
2308       }
2309     };
2310     for (const Fortran::lower::CalleeInterface::PassedEntity &arg :
2311          callee.getPassedArguments())
2312       mapPassedEntity(arg);
2313 
2314     // Allocate local skeleton instances of dummies from other entry points.
2315     // Most of these locals will not survive into final generated code, but
2316     // some will.  It is illegal to reference them at run time if they do.
2317     for (const Fortran::semantics::Symbol *arg :
2318          funit.nonUniversalDummyArguments) {
2319       if (lookupSymbol(*arg))
2320         continue;
2321       mlir::Type type = genType(*arg);
2322       // TODO: Account for VALUE arguments (and possibly other variants).
2323       type = builder->getRefType(type);
2324       addSymbol(*arg, builder->create<fir::UndefOp>(toLocation(), type));
2325     }
2326     if (std::optional<Fortran::lower::CalleeInterface::PassedEntity>
2327             passedResult = callee.getPassedResult()) {
2328       mapPassedEntity(*passedResult);
2329       // FIXME: need to make sure things are OK here. addSymbol may not be OK
2330       if (funit.primaryResult &&
2331           passedResult->entity->get() != *funit.primaryResult)
2332         addSymbol(*funit.primaryResult,
2333                   getSymbolAddress(passedResult->entity->get()));
2334     }
2335   }
2336 
2337   /// Instantiate variable \p var and add it to the symbol map.
2338   /// See ConvertVariable.cpp.
2339   void instantiateVar(const Fortran::lower::pft::Variable &var,
2340                       Fortran::lower::AggregateStoreMap &storeMap) {
2341     Fortran::lower::instantiateVariable(*this, var, localSymbols, storeMap);
2342   }
2343 
2344   /// Prepare to translate a new function
2345   void startNewFunction(Fortran::lower::pft::FunctionLikeUnit &funit) {
2346     assert(!builder && "expected nullptr");
2347     Fortran::lower::CalleeInterface callee(funit, *this);
2348     mlir::func::FuncOp func = callee.addEntryBlockAndMapArguments();
2349     builder = new fir::FirOpBuilder(func, bridge.getKindMap());
2350     assert(builder && "FirOpBuilder did not instantiate");
2351     builder->setInsertionPointToStart(&func.front());
2352     func.setVisibility(mlir::SymbolTable::Visibility::Public);
2353 
2354     mapDummiesAndResults(funit, callee);
2355 
2356     // Note: not storing Variable references because getOrderedSymbolTable
2357     // below returns a temporary.
2358     llvm::SmallVector<Fortran::lower::pft::Variable> deferredFuncResultList;
2359 
2360     // Backup actual argument for entry character results
2361     // with different lengths. It needs to be added to the non
2362     // primary results symbol before mapSymbolAttributes is called.
2363     Fortran::lower::SymbolBox resultArg;
2364     if (std::optional<Fortran::lower::CalleeInterface::PassedEntity>
2365             passedResult = callee.getPassedResult())
2366       resultArg = lookupSymbol(passedResult->entity->get());
2367 
2368     Fortran::lower::AggregateStoreMap storeMap;
2369     // The front-end is currently not adding module variables referenced
2370     // in a module procedure as host associated. As a result we need to
2371     // instantiate all module variables here if this is a module procedure.
2372     // It is likely that the front-end behavior should change here.
2373     // This also applies to internal procedures inside module procedures.
2374     if (auto *module = Fortran::lower::pft::getAncestor<
2375             Fortran::lower::pft::ModuleLikeUnit>(funit))
2376       for (const Fortran::lower::pft::Variable &var :
2377            module->getOrderedSymbolTable())
2378         instantiateVar(var, storeMap);
2379 
2380     mlir::Value primaryFuncResultStorage;
2381     for (const Fortran::lower::pft::Variable &var :
2382          funit.getOrderedSymbolTable()) {
2383       // Always instantiate aggregate storage blocks.
2384       if (var.isAggregateStore()) {
2385         instantiateVar(var, storeMap);
2386         continue;
2387       }
2388       const Fortran::semantics::Symbol &sym = var.getSymbol();
2389       if (funit.parentHasHostAssoc()) {
2390         // Never instantitate host associated variables, as they are already
2391         // instantiated from an argument tuple. Instead, just bind the symbol to
2392         // the reference to the host variable, which must be in the map.
2393         const Fortran::semantics::Symbol &ultimate = sym.GetUltimate();
2394         if (funit.parentHostAssoc().isAssociated(ultimate)) {
2395           Fortran::lower::SymbolBox hostBox =
2396               localSymbols.lookupSymbol(ultimate);
2397           assert(hostBox && "host association is not in map");
2398           localSymbols.addSymbol(sym, hostBox.toExtendedValue());
2399           continue;
2400         }
2401       }
2402       if (!sym.IsFuncResult() || !funit.primaryResult) {
2403         instantiateVar(var, storeMap);
2404       } else if (&sym == funit.primaryResult) {
2405         instantiateVar(var, storeMap);
2406         primaryFuncResultStorage = getSymbolAddress(sym);
2407       } else {
2408         deferredFuncResultList.push_back(var);
2409       }
2410     }
2411 
2412     // If this is a host procedure with host associations, then create the tuple
2413     // of pointers for passing to the internal procedures.
2414     if (!funit.getHostAssoc().empty())
2415       funit.getHostAssoc().hostProcedureBindings(*this, localSymbols);
2416 
2417     /// TODO: should use same mechanism as equivalence?
2418     /// One blocking point is character entry returns that need special handling
2419     /// since they are not locally allocated but come as argument. CHARACTER(*)
2420     /// is not something that fit wells with equivalence lowering.
2421     for (const Fortran::lower::pft::Variable &altResult :
2422          deferredFuncResultList) {
2423       if (std::optional<Fortran::lower::CalleeInterface::PassedEntity>
2424               passedResult = callee.getPassedResult())
2425         addSymbol(altResult.getSymbol(), resultArg.getAddr());
2426       Fortran::lower::StatementContext stmtCtx;
2427       Fortran::lower::mapSymbolAttributes(*this, altResult, localSymbols,
2428                                           stmtCtx, primaryFuncResultStorage);
2429     }
2430 
2431     // Create most function blocks in advance.
2432     createEmptyBlocks(funit.evaluationList);
2433 
2434     // Reinstate entry block as the current insertion point.
2435     builder->setInsertionPointToEnd(&func.front());
2436 
2437     if (callee.hasAlternateReturns()) {
2438       // Create a local temp to hold the alternate return index.
2439       // Give it an integer index type and the subroutine name (for dumps).
2440       // Attach it to the subroutine symbol in the localSymbols map.
2441       // Initialize it to zero, the "fallthrough" alternate return value.
2442       const Fortran::semantics::Symbol &symbol = funit.getSubprogramSymbol();
2443       mlir::Location loc = toLocation();
2444       mlir::Type idxTy = builder->getIndexType();
2445       mlir::Value altResult =
2446           builder->createTemporary(loc, idxTy, toStringRef(symbol.name()));
2447       addSymbol(symbol, altResult);
2448       mlir::Value zero = builder->createIntegerConstant(loc, idxTy, 0);
2449       builder->create<fir::StoreOp>(loc, zero, altResult);
2450     }
2451 
2452     if (Fortran::lower::pft::Evaluation *alternateEntryEval =
2453             funit.getEntryEval())
2454       genFIRBranch(alternateEntryEval->lexicalSuccessor->block);
2455   }
2456 
2457   /// Create global blocks for the current function.  This eliminates the
2458   /// distinction between forward and backward targets when generating
2459   /// branches.  A block is "global" if it can be the target of a GOTO or
2460   /// other source code branch.  A block that can only be targeted by a
2461   /// compiler generated branch is "local".  For example, a DO loop preheader
2462   /// block containing loop initialization code is global.  A loop header
2463   /// block, which is the target of the loop back edge, is local.  Blocks
2464   /// belong to a region.  Any block within a nested region must be replaced
2465   /// with a block belonging to that region.  Branches may not cross region
2466   /// boundaries.
2467   void createEmptyBlocks(
2468       std::list<Fortran::lower::pft::Evaluation> &evaluationList) {
2469     mlir::Region *region = &builder->getRegion();
2470     for (Fortran::lower::pft::Evaluation &eval : evaluationList) {
2471       if (eval.isNewBlock)
2472         eval.block = builder->createBlock(region);
2473       if (eval.isConstruct() || eval.isDirective()) {
2474         if (eval.lowerAsUnstructured()) {
2475           createEmptyBlocks(eval.getNestedEvaluations());
2476         } else if (eval.hasNestedEvaluations()) {
2477           // A structured construct that is a target starts a new block.
2478           Fortran::lower::pft::Evaluation &constructStmt =
2479               eval.getFirstNestedEvaluation();
2480           if (constructStmt.isNewBlock)
2481             constructStmt.block = builder->createBlock(region);
2482         }
2483       }
2484     }
2485   }
2486 
2487   /// Return the predicate: "current block does not have a terminator branch".
2488   bool blockIsUnterminated() {
2489     mlir::Block *currentBlock = builder->getBlock();
2490     return currentBlock->empty() ||
2491            !currentBlock->back().hasTrait<mlir::OpTrait::IsTerminator>();
2492   }
2493 
2494   /// Unconditionally switch code insertion to a new block.
2495   void startBlock(mlir::Block *newBlock) {
2496     assert(newBlock && "missing block");
2497     // Default termination for the current block is a fallthrough branch to
2498     // the new block.
2499     if (blockIsUnterminated())
2500       genFIRBranch(newBlock);
2501     // Some blocks may be re/started more than once, and might not be empty.
2502     // If the new block already has (only) a terminator, set the insertion
2503     // point to the start of the block.  Otherwise set it to the end.
2504     builder->setInsertionPointToStart(newBlock);
2505     if (blockIsUnterminated())
2506       builder->setInsertionPointToEnd(newBlock);
2507   }
2508 
2509   /// Conditionally switch code insertion to a new block.
2510   void maybeStartBlock(mlir::Block *newBlock) {
2511     if (newBlock)
2512       startBlock(newBlock);
2513   }
2514 
2515   /// Emit return and cleanup after the function has been translated.
2516   void endNewFunction(Fortran::lower::pft::FunctionLikeUnit &funit) {
2517     setCurrentPosition(Fortran::lower::pft::stmtSourceLoc(funit.endStmt));
2518     if (funit.isMainProgram())
2519       genExitRoutine();
2520     else
2521       genFIRProcedureExit(funit, funit.getSubprogramSymbol());
2522     funit.finalBlock = nullptr;
2523     LLVM_DEBUG(llvm::dbgs() << "*** Lowering result:\n\n"
2524                             << *builder->getFunction() << '\n');
2525     // FIXME: Simplification should happen in a normal pass, not here.
2526     mlir::IRRewriter rewriter(*builder);
2527     (void)mlir::simplifyRegions(rewriter,
2528                                 {builder->getRegion()}); // remove dead code
2529     delete builder;
2530     builder = nullptr;
2531     hostAssocTuple = mlir::Value{};
2532     localSymbols.clear();
2533   }
2534 
2535   /// Helper to generate GlobalOps when the builder is not positioned in any
2536   /// region block. This is required because the FirOpBuilder assumes it is
2537   /// always positioned inside a region block when creating globals, the easiest
2538   /// way comply is to create a dummy function and to throw it afterwards.
2539   void createGlobalOutsideOfFunctionLowering(
2540       const std::function<void()> &createGlobals) {
2541     // FIXME: get rid of the bogus function context and instantiate the
2542     // globals directly into the module.
2543     mlir::MLIRContext *context = &getMLIRContext();
2544     mlir::func::FuncOp func = fir::FirOpBuilder::createFunction(
2545         mlir::UnknownLoc::get(context), getModuleOp(),
2546         fir::NameUniquer::doGenerated("Sham"),
2547         mlir::FunctionType::get(context, llvm::None, llvm::None));
2548     func.addEntryBlock();
2549     builder = new fir::FirOpBuilder(func, bridge.getKindMap());
2550     createGlobals();
2551     if (mlir::Region *region = func.getCallableRegion())
2552       region->dropAllReferences();
2553     func.erase();
2554     delete builder;
2555     builder = nullptr;
2556     localSymbols.clear();
2557   }
2558   /// Instantiate the data from a BLOCK DATA unit.
2559   void lowerBlockData(Fortran::lower::pft::BlockDataUnit &bdunit) {
2560     createGlobalOutsideOfFunctionLowering([&]() {
2561       Fortran::lower::AggregateStoreMap fakeMap;
2562       for (const auto &[_, sym] : bdunit.symTab) {
2563         if (sym->has<Fortran::semantics::ObjectEntityDetails>()) {
2564           Fortran::lower::pft::Variable var(*sym, true);
2565           instantiateVar(var, fakeMap);
2566         }
2567       }
2568     });
2569   }
2570 
2571   /// Create fir::Global for all the common blocks that appear in the program.
2572   void
2573   lowerCommonBlocks(const Fortran::semantics::CommonBlockList &commonBlocks) {
2574     createGlobalOutsideOfFunctionLowering(
2575         [&]() { Fortran::lower::defineCommonBlocks(*this, commonBlocks); });
2576   }
2577 
2578   /// Lower a procedure (nest).
2579   void lowerFunc(Fortran::lower::pft::FunctionLikeUnit &funit) {
2580     if (!funit.isMainProgram()) {
2581       const Fortran::semantics::Symbol &procSymbol =
2582           funit.getSubprogramSymbol();
2583       if (procSymbol.owner().IsSubmodule()) {
2584         TODO(toLocation(), "support submodules");
2585         return;
2586       }
2587     }
2588     setCurrentPosition(funit.getStartingSourceLoc());
2589     for (int entryIndex = 0, last = funit.entryPointList.size();
2590          entryIndex < last; ++entryIndex) {
2591       funit.setActiveEntry(entryIndex);
2592       startNewFunction(funit); // the entry point for lowering this procedure
2593       for (Fortran::lower::pft::Evaluation &eval : funit.evaluationList)
2594         genFIR(eval);
2595       endNewFunction(funit);
2596     }
2597     funit.setActiveEntry(0);
2598     for (Fortran::lower::pft::FunctionLikeUnit &f : funit.nestedFunctions)
2599       lowerFunc(f); // internal procedure
2600   }
2601 
2602   /// Lower module variable definitions to fir::globalOp and OpenMP/OpenACC
2603   /// declarative construct.
2604   void lowerModuleDeclScope(Fortran::lower::pft::ModuleLikeUnit &mod) {
2605     setCurrentPosition(mod.getStartingSourceLoc());
2606     createGlobalOutsideOfFunctionLowering([&]() {
2607       for (const Fortran::lower::pft::Variable &var :
2608            mod.getOrderedSymbolTable()) {
2609         // Only define the variables owned by this module.
2610         const Fortran::semantics::Scope *owningScope = var.getOwningScope();
2611         if (!owningScope || mod.getScope() == *owningScope)
2612           Fortran::lower::defineModuleVariable(*this, var);
2613       }
2614       for (auto &eval : mod.evaluationList)
2615         genFIR(eval);
2616     });
2617   }
2618 
2619   /// Lower functions contained in a module.
2620   void lowerMod(Fortran::lower::pft::ModuleLikeUnit &mod) {
2621     for (Fortran::lower::pft::FunctionLikeUnit &f : mod.nestedFunctions)
2622       lowerFunc(f);
2623   }
2624 
2625   void setCurrentPosition(const Fortran::parser::CharBlock &position) {
2626     if (position != Fortran::parser::CharBlock{})
2627       currentPosition = position;
2628   }
2629 
2630   /// Set current position at the location of \p parseTreeNode. Note that the
2631   /// position is updated automatically when visiting statements, but not when
2632   /// entering higher level nodes like constructs or procedures. This helper is
2633   /// intended to cover the latter cases.
2634   template <typename A>
2635   void setCurrentPositionAt(const A &parseTreeNode) {
2636     setCurrentPosition(Fortran::parser::FindSourceLocation(parseTreeNode));
2637   }
2638 
2639   //===--------------------------------------------------------------------===//
2640   // Utility methods
2641   //===--------------------------------------------------------------------===//
2642 
2643   /// Convert a parser CharBlock to a Location
2644   mlir::Location toLocation(const Fortran::parser::CharBlock &cb) {
2645     return genLocation(cb);
2646   }
2647 
2648   mlir::Location toLocation() { return toLocation(currentPosition); }
2649   void setCurrentEval(Fortran::lower::pft::Evaluation &eval) {
2650     evalPtr = &eval;
2651   }
2652   Fortran::lower::pft::Evaluation &getEval() {
2653     assert(evalPtr);
2654     return *evalPtr;
2655   }
2656 
2657   std::optional<Fortran::evaluate::Shape>
2658   getShape(const Fortran::lower::SomeExpr &expr) {
2659     return Fortran::evaluate::GetShape(foldingContext, expr);
2660   }
2661 
2662   //===--------------------------------------------------------------------===//
2663   // Analysis on a nested explicit iteration space.
2664   //===--------------------------------------------------------------------===//
2665 
2666   void analyzeExplicitSpace(const Fortran::parser::ConcurrentHeader &header) {
2667     explicitIterSpace.pushLevel();
2668     for (const Fortran::parser::ConcurrentControl &ctrl :
2669          std::get<std::list<Fortran::parser::ConcurrentControl>>(header.t)) {
2670       const Fortran::semantics::Symbol *ctrlVar =
2671           std::get<Fortran::parser::Name>(ctrl.t).symbol;
2672       explicitIterSpace.addSymbol(ctrlVar);
2673     }
2674     if (const auto &mask =
2675             std::get<std::optional<Fortran::parser::ScalarLogicalExpr>>(
2676                 header.t);
2677         mask.has_value())
2678       analyzeExplicitSpace(*Fortran::semantics::GetExpr(*mask));
2679   }
2680   template <bool LHS = false, typename A>
2681   void analyzeExplicitSpace(const Fortran::evaluate::Expr<A> &e) {
2682     explicitIterSpace.exprBase(&e, LHS);
2683   }
2684   void analyzeExplicitSpace(const Fortran::evaluate::Assignment *assign) {
2685     auto analyzeAssign = [&](const Fortran::lower::SomeExpr &lhs,
2686                              const Fortran::lower::SomeExpr &rhs) {
2687       analyzeExplicitSpace</*LHS=*/true>(lhs);
2688       analyzeExplicitSpace(rhs);
2689     };
2690     std::visit(
2691         Fortran::common::visitors{
2692             [&](const Fortran::evaluate::ProcedureRef &procRef) {
2693               // Ensure the procRef expressions are the one being visited.
2694               assert(procRef.arguments().size() == 2);
2695               const Fortran::lower::SomeExpr *lhs =
2696                   procRef.arguments()[0].value().UnwrapExpr();
2697               const Fortran::lower::SomeExpr *rhs =
2698                   procRef.arguments()[1].value().UnwrapExpr();
2699               assert(lhs && rhs &&
2700                      "user defined assignment arguments must be expressions");
2701               analyzeAssign(*lhs, *rhs);
2702             },
2703             [&](const auto &) { analyzeAssign(assign->lhs, assign->rhs); }},
2704         assign->u);
2705     explicitIterSpace.endAssign();
2706   }
2707   void analyzeExplicitSpace(const Fortran::parser::ForallAssignmentStmt &stmt) {
2708     std::visit([&](const auto &s) { analyzeExplicitSpace(s); }, stmt.u);
2709   }
2710   void analyzeExplicitSpace(const Fortran::parser::AssignmentStmt &s) {
2711     analyzeExplicitSpace(s.typedAssignment->v.operator->());
2712   }
2713   void analyzeExplicitSpace(const Fortran::parser::PointerAssignmentStmt &s) {
2714     analyzeExplicitSpace(s.typedAssignment->v.operator->());
2715   }
2716   void analyzeExplicitSpace(const Fortran::parser::WhereConstruct &c) {
2717     analyzeExplicitSpace(
2718         std::get<
2719             Fortran::parser::Statement<Fortran::parser::WhereConstructStmt>>(
2720             c.t)
2721             .statement);
2722     for (const Fortran::parser::WhereBodyConstruct &body :
2723          std::get<std::list<Fortran::parser::WhereBodyConstruct>>(c.t))
2724       analyzeExplicitSpace(body);
2725     for (const Fortran::parser::WhereConstruct::MaskedElsewhere &e :
2726          std::get<std::list<Fortran::parser::WhereConstruct::MaskedElsewhere>>(
2727              c.t))
2728       analyzeExplicitSpace(e);
2729     if (const auto &e =
2730             std::get<std::optional<Fortran::parser::WhereConstruct::Elsewhere>>(
2731                 c.t);
2732         e.has_value())
2733       analyzeExplicitSpace(e.operator->());
2734   }
2735   void analyzeExplicitSpace(const Fortran::parser::WhereConstructStmt &ws) {
2736     const Fortran::lower::SomeExpr *exp = Fortran::semantics::GetExpr(
2737         std::get<Fortran::parser::LogicalExpr>(ws.t));
2738     addMaskVariable(exp);
2739     analyzeExplicitSpace(*exp);
2740   }
2741   void analyzeExplicitSpace(
2742       const Fortran::parser::WhereConstruct::MaskedElsewhere &ew) {
2743     analyzeExplicitSpace(
2744         std::get<
2745             Fortran::parser::Statement<Fortran::parser::MaskedElsewhereStmt>>(
2746             ew.t)
2747             .statement);
2748     for (const Fortran::parser::WhereBodyConstruct &e :
2749          std::get<std::list<Fortran::parser::WhereBodyConstruct>>(ew.t))
2750       analyzeExplicitSpace(e);
2751   }
2752   void analyzeExplicitSpace(const Fortran::parser::WhereBodyConstruct &body) {
2753     std::visit(Fortran::common::visitors{
2754                    [&](const Fortran::common::Indirection<
2755                        Fortran::parser::WhereConstruct> &wc) {
2756                      analyzeExplicitSpace(wc.value());
2757                    },
2758                    [&](const auto &s) { analyzeExplicitSpace(s.statement); }},
2759                body.u);
2760   }
2761   void analyzeExplicitSpace(const Fortran::parser::MaskedElsewhereStmt &stmt) {
2762     const Fortran::lower::SomeExpr *exp = Fortran::semantics::GetExpr(
2763         std::get<Fortran::parser::LogicalExpr>(stmt.t));
2764     addMaskVariable(exp);
2765     analyzeExplicitSpace(*exp);
2766   }
2767   void
2768   analyzeExplicitSpace(const Fortran::parser::WhereConstruct::Elsewhere *ew) {
2769     for (const Fortran::parser::WhereBodyConstruct &e :
2770          std::get<std::list<Fortran::parser::WhereBodyConstruct>>(ew->t))
2771       analyzeExplicitSpace(e);
2772   }
2773   void analyzeExplicitSpace(const Fortran::parser::WhereStmt &stmt) {
2774     const Fortran::lower::SomeExpr *exp = Fortran::semantics::GetExpr(
2775         std::get<Fortran::parser::LogicalExpr>(stmt.t));
2776     addMaskVariable(exp);
2777     analyzeExplicitSpace(*exp);
2778     const std::optional<Fortran::evaluate::Assignment> &assign =
2779         std::get<Fortran::parser::AssignmentStmt>(stmt.t).typedAssignment->v;
2780     assert(assign.has_value() && "WHERE has no statement");
2781     analyzeExplicitSpace(assign.operator->());
2782   }
2783   void analyzeExplicitSpace(const Fortran::parser::ForallStmt &forall) {
2784     analyzeExplicitSpace(
2785         std::get<
2786             Fortran::common::Indirection<Fortran::parser::ConcurrentHeader>>(
2787             forall.t)
2788             .value());
2789     analyzeExplicitSpace(std::get<Fortran::parser::UnlabeledStatement<
2790                              Fortran::parser::ForallAssignmentStmt>>(forall.t)
2791                              .statement);
2792     analyzeExplicitSpacePop();
2793   }
2794   void
2795   analyzeExplicitSpace(const Fortran::parser::ForallConstructStmt &forall) {
2796     analyzeExplicitSpace(
2797         std::get<
2798             Fortran::common::Indirection<Fortran::parser::ConcurrentHeader>>(
2799             forall.t)
2800             .value());
2801   }
2802   void analyzeExplicitSpace(const Fortran::parser::ForallConstruct &forall) {
2803     analyzeExplicitSpace(
2804         std::get<
2805             Fortran::parser::Statement<Fortran::parser::ForallConstructStmt>>(
2806             forall.t)
2807             .statement);
2808     for (const Fortran::parser::ForallBodyConstruct &s :
2809          std::get<std::list<Fortran::parser::ForallBodyConstruct>>(forall.t)) {
2810       std::visit(Fortran::common::visitors{
2811                      [&](const Fortran::common::Indirection<
2812                          Fortran::parser::ForallConstruct> &b) {
2813                        analyzeExplicitSpace(b.value());
2814                      },
2815                      [&](const Fortran::parser::WhereConstruct &w) {
2816                        analyzeExplicitSpace(w);
2817                      },
2818                      [&](const auto &b) { analyzeExplicitSpace(b.statement); }},
2819                  s.u);
2820     }
2821     analyzeExplicitSpacePop();
2822   }
2823 
2824   void analyzeExplicitSpacePop() { explicitIterSpace.popLevel(); }
2825 
2826   void addMaskVariable(Fortran::lower::FrontEndExpr exp) {
2827     // Note: use i8 to store bool values. This avoids round-down behavior found
2828     // with sequences of i1. That is, an array of i1 will be truncated in size
2829     // and be too small. For example, a buffer of type fir.array<7xi1> will have
2830     // 0 size.
2831     mlir::Type i64Ty = builder->getIntegerType(64);
2832     mlir::TupleType ty = fir::factory::getRaggedArrayHeaderType(*builder);
2833     mlir::Type buffTy = ty.getType(1);
2834     mlir::Type shTy = ty.getType(2);
2835     mlir::Location loc = toLocation();
2836     mlir::Value hdr = builder->createTemporary(loc, ty);
2837     // FIXME: Is there a way to create a `zeroinitializer` in LLVM-IR dialect?
2838     // For now, explicitly set lazy ragged header to all zeros.
2839     // auto nilTup = builder->createNullConstant(loc, ty);
2840     // builder->create<fir::StoreOp>(loc, nilTup, hdr);
2841     mlir::Type i32Ty = builder->getIntegerType(32);
2842     mlir::Value zero = builder->createIntegerConstant(loc, i32Ty, 0);
2843     mlir::Value zero64 = builder->createIntegerConstant(loc, i64Ty, 0);
2844     mlir::Value flags = builder->create<fir::CoordinateOp>(
2845         loc, builder->getRefType(i64Ty), hdr, zero);
2846     builder->create<fir::StoreOp>(loc, zero64, flags);
2847     mlir::Value one = builder->createIntegerConstant(loc, i32Ty, 1);
2848     mlir::Value nullPtr1 = builder->createNullConstant(loc, buffTy);
2849     mlir::Value var = builder->create<fir::CoordinateOp>(
2850         loc, builder->getRefType(buffTy), hdr, one);
2851     builder->create<fir::StoreOp>(loc, nullPtr1, var);
2852     mlir::Value two = builder->createIntegerConstant(loc, i32Ty, 2);
2853     mlir::Value nullPtr2 = builder->createNullConstant(loc, shTy);
2854     mlir::Value shape = builder->create<fir::CoordinateOp>(
2855         loc, builder->getRefType(shTy), hdr, two);
2856     builder->create<fir::StoreOp>(loc, nullPtr2, shape);
2857     implicitIterSpace.addMaskVariable(exp, var, shape, hdr);
2858     explicitIterSpace.outermostContext().attachCleanup(
2859         [builder = this->builder, hdr, loc]() {
2860           fir::runtime::genRaggedArrayDeallocate(loc, *builder, hdr);
2861         });
2862   }
2863 
2864   void createRuntimeTypeInfoGlobals() {}
2865 
2866   //===--------------------------------------------------------------------===//
2867 
2868   Fortran::lower::LoweringBridge &bridge;
2869   Fortran::evaluate::FoldingContext foldingContext;
2870   fir::FirOpBuilder *builder = nullptr;
2871   Fortran::lower::pft::Evaluation *evalPtr = nullptr;
2872   Fortran::lower::SymMap localSymbols;
2873   Fortran::parser::CharBlock currentPosition;
2874   RuntimeTypeInfoConverter runtimeTypeInfoConverter;
2875 
2876   /// WHERE statement/construct mask expression stack.
2877   Fortran::lower::ImplicitIterSpace implicitIterSpace;
2878 
2879   /// FORALL context
2880   Fortran::lower::ExplicitIterSpace explicitIterSpace;
2881 
2882   /// Tuple of host assoicated variables.
2883   mlir::Value hostAssocTuple;
2884 };
2885 
2886 } // namespace
2887 
2888 Fortran::evaluate::FoldingContext
2889 Fortran::lower::LoweringBridge::createFoldingContext() const {
2890   return {getDefaultKinds(), getIntrinsicTable()};
2891 }
2892 
2893 void Fortran::lower::LoweringBridge::lower(
2894     const Fortran::parser::Program &prg,
2895     const Fortran::semantics::SemanticsContext &semanticsContext) {
2896   std::unique_ptr<Fortran::lower::pft::Program> pft =
2897       Fortran::lower::createPFT(prg, semanticsContext);
2898   if (dumpBeforeFir)
2899     Fortran::lower::dumpPFT(llvm::errs(), *pft);
2900   FirConverter converter{*this};
2901   converter.run(*pft);
2902 }
2903 
2904 void Fortran::lower::LoweringBridge::parseSourceFile(llvm::SourceMgr &srcMgr) {
2905   mlir::OwningOpRef<mlir::ModuleOp> owningRef =
2906       mlir::parseSourceFile<mlir::ModuleOp>(srcMgr, &context);
2907   module.reset(new mlir::ModuleOp(owningRef.get().getOperation()));
2908   owningRef.release();
2909 }
2910 
2911 Fortran::lower::LoweringBridge::LoweringBridge(
2912     mlir::MLIRContext &context,
2913     const Fortran::common::IntrinsicTypeDefaultKinds &defaultKinds,
2914     const Fortran::evaluate::IntrinsicProcTable &intrinsics,
2915     const Fortran::parser::AllCookedSources &cooked, llvm::StringRef triple,
2916     fir::KindMapping &kindMap)
2917     : defaultKinds{defaultKinds}, intrinsics{intrinsics}, cooked{&cooked},
2918       context{context}, kindMap{kindMap} {
2919   // Register the diagnostic handler.
2920   context.getDiagEngine().registerHandler([](mlir::Diagnostic &diag) {
2921     llvm::raw_ostream &os = llvm::errs();
2922     switch (diag.getSeverity()) {
2923     case mlir::DiagnosticSeverity::Error:
2924       os << "error: ";
2925       break;
2926     case mlir::DiagnosticSeverity::Remark:
2927       os << "info: ";
2928       break;
2929     case mlir::DiagnosticSeverity::Warning:
2930       os << "warning: ";
2931       break;
2932     default:
2933       break;
2934     }
2935     if (!diag.getLocation().isa<mlir::UnknownLoc>())
2936       os << diag.getLocation() << ": ";
2937     os << diag << '\n';
2938     os.flush();
2939     return mlir::success();
2940   });
2941 
2942   // Create the module and attach the attributes.
2943   module = std::make_unique<mlir::ModuleOp>(
2944       mlir::ModuleOp::create(mlir::UnknownLoc::get(&context)));
2945   assert(module.get() && "module was not created");
2946   fir::setTargetTriple(*module.get(), triple);
2947   fir::setKindMapping(*module.get(), kindMap);
2948 }
2949