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