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     Fortran::lower::genOpenMPConstruct(*this, getEval(), omp);
1405 
1406     // If loop is part of an OpenMP Construct then the OpenMP dialect
1407     // workshare loop operation has already been created. Only the
1408     // body needs to be created here and the do_loop can be skipped.
1409     Fortran::lower::pft::Evaluation *curEval =
1410         std::get_if<Fortran::parser::OpenMPLoopConstruct>(&omp.u)
1411             ? &getEval().getFirstNestedEvaluation()
1412             : &getEval();
1413 
1414     for (Fortran::lower::pft::Evaluation &e : curEval->getNestedEvaluations())
1415       genFIR(e);
1416     localSymbols.popScope();
1417     builder->restoreInsertionPoint(insertPt);
1418   }
1419 
1420   void genFIR(const Fortran::parser::OpenMPDeclarativeConstruct &ompDecl) {
1421     mlir::OpBuilder::InsertPoint insertPt = builder->saveInsertionPoint();
1422     genOpenMPDeclarativeConstruct(*this, getEval(), ompDecl);
1423     for (Fortran::lower::pft::Evaluation &e : getEval().getNestedEvaluations())
1424       genFIR(e);
1425     builder->restoreInsertionPoint(insertPt);
1426   }
1427 
1428   /// Generate FIR for a SELECT CASE statement.
1429   /// The type may be CHARACTER, INTEGER, or LOGICAL.
1430   void genFIR(const Fortran::parser::SelectCaseStmt &stmt) {
1431     Fortran::lower::pft::Evaluation &eval = getEval();
1432     mlir::MLIRContext *context = builder->getContext();
1433     mlir::Location loc = toLocation();
1434     Fortran::lower::StatementContext stmtCtx;
1435     const Fortran::lower::SomeExpr *expr = Fortran::semantics::GetExpr(
1436         std::get<Fortran::parser::Scalar<Fortran::parser::Expr>>(stmt.t));
1437     bool isCharSelector = isCharacterCategory(expr->GetType()->category());
1438     bool isLogicalSelector = isLogicalCategory(expr->GetType()->category());
1439     auto charValue = [&](const Fortran::lower::SomeExpr *expr) {
1440       fir::ExtendedValue exv = genExprAddr(*expr, stmtCtx, &loc);
1441       return exv.match(
1442           [&](const fir::CharBoxValue &cbv) {
1443             return fir::factory::CharacterExprHelper{*builder, loc}
1444                 .createEmboxChar(cbv.getAddr(), cbv.getLen());
1445           },
1446           [&](auto) {
1447             fir::emitFatalError(loc, "not a character");
1448             return mlir::Value{};
1449           });
1450     };
1451     mlir::Value selector;
1452     if (isCharSelector) {
1453       selector = charValue(expr);
1454     } else {
1455       selector = createFIRExpr(loc, expr, stmtCtx);
1456       if (isLogicalSelector)
1457         selector = builder->createConvert(loc, builder->getI1Type(), selector);
1458     }
1459     mlir::Type selectType = selector.getType();
1460     llvm::SmallVector<mlir::Attribute> attrList;
1461     llvm::SmallVector<mlir::Value> valueList;
1462     llvm::SmallVector<mlir::Block *> blockList;
1463     mlir::Block *defaultBlock = eval.parentConstruct->constructExit->block;
1464     using CaseValue = Fortran::parser::Scalar<Fortran::parser::ConstantExpr>;
1465     auto addValue = [&](const CaseValue &caseValue) {
1466       const Fortran::lower::SomeExpr *expr =
1467           Fortran::semantics::GetExpr(caseValue.thing);
1468       if (isCharSelector)
1469         valueList.push_back(charValue(expr));
1470       else if (isLogicalSelector)
1471         valueList.push_back(builder->createConvert(
1472             loc, selectType, createFIRExpr(toLocation(), expr, stmtCtx)));
1473       else
1474         valueList.push_back(builder->createIntegerConstant(
1475             loc, selectType, *Fortran::evaluate::ToInt64(*expr)));
1476     };
1477     for (Fortran::lower::pft::Evaluation *e = eval.controlSuccessor; e;
1478          e = e->controlSuccessor) {
1479       const auto &caseStmt = e->getIf<Fortran::parser::CaseStmt>();
1480       assert(e->block && "missing CaseStmt block");
1481       const auto &caseSelector =
1482           std::get<Fortran::parser::CaseSelector>(caseStmt->t);
1483       const auto *caseValueRangeList =
1484           std::get_if<std::list<Fortran::parser::CaseValueRange>>(
1485               &caseSelector.u);
1486       if (!caseValueRangeList) {
1487         defaultBlock = e->block;
1488         continue;
1489       }
1490       for (const Fortran::parser::CaseValueRange &caseValueRange :
1491            *caseValueRangeList) {
1492         blockList.push_back(e->block);
1493         if (const auto *caseValue = std::get_if<CaseValue>(&caseValueRange.u)) {
1494           attrList.push_back(fir::PointIntervalAttr::get(context));
1495           addValue(*caseValue);
1496           continue;
1497         }
1498         const auto &caseRange =
1499             std::get<Fortran::parser::CaseValueRange::Range>(caseValueRange.u);
1500         if (caseRange.lower && caseRange.upper) {
1501           attrList.push_back(fir::ClosedIntervalAttr::get(context));
1502           addValue(*caseRange.lower);
1503           addValue(*caseRange.upper);
1504         } else if (caseRange.lower) {
1505           attrList.push_back(fir::LowerBoundAttr::get(context));
1506           addValue(*caseRange.lower);
1507         } else {
1508           attrList.push_back(fir::UpperBoundAttr::get(context));
1509           addValue(*caseRange.upper);
1510         }
1511       }
1512     }
1513     // Skip a logical default block that can never be referenced.
1514     if (isLogicalSelector && attrList.size() == 2)
1515       defaultBlock = eval.parentConstruct->constructExit->block;
1516     attrList.push_back(mlir::UnitAttr::get(context));
1517     blockList.push_back(defaultBlock);
1518 
1519     // Generate a fir::SelectCaseOp.
1520     // Explicit branch code is better for the LOGICAL type.  The CHARACTER type
1521     // does not yet have downstream support, and also uses explicit branch code.
1522     // The -no-structured-fir option can be used to force generation of INTEGER
1523     // type branch code.
1524     if (!isLogicalSelector && !isCharSelector && eval.lowerAsStructured()) {
1525       // Numeric selector is a ssa register, all temps that may have
1526       // been generated while evaluating it can be cleaned-up before the
1527       // fir.select_case.
1528       stmtCtx.finalize();
1529       builder->create<fir::SelectCaseOp>(loc, selector, attrList, valueList,
1530                                          blockList);
1531       return;
1532     }
1533 
1534     // Generate a sequence of case value comparisons and branches.
1535     auto caseValue = valueList.begin();
1536     auto caseBlock = blockList.begin();
1537     for (mlir::Attribute attr : attrList) {
1538       if (attr.isa<mlir::UnitAttr>()) {
1539         genFIRBranch(*caseBlock++);
1540         break;
1541       }
1542       auto genCond = [&](mlir::Value rhs,
1543                          mlir::arith::CmpIPredicate pred) -> mlir::Value {
1544         if (!isCharSelector)
1545           return builder->create<mlir::arith::CmpIOp>(loc, pred, selector, rhs);
1546         fir::factory::CharacterExprHelper charHelper{*builder, loc};
1547         std::pair<mlir::Value, mlir::Value> lhsVal =
1548             charHelper.createUnboxChar(selector);
1549         mlir::Value &lhsAddr = lhsVal.first;
1550         mlir::Value &lhsLen = lhsVal.second;
1551         std::pair<mlir::Value, mlir::Value> rhsVal =
1552             charHelper.createUnboxChar(rhs);
1553         mlir::Value &rhsAddr = rhsVal.first;
1554         mlir::Value &rhsLen = rhsVal.second;
1555         return fir::runtime::genCharCompare(*builder, loc, pred, lhsAddr,
1556                                             lhsLen, rhsAddr, rhsLen);
1557       };
1558       mlir::Block *newBlock = insertBlock(*caseBlock);
1559       if (attr.isa<fir::ClosedIntervalAttr>()) {
1560         mlir::Block *newBlock2 = insertBlock(*caseBlock);
1561         mlir::Value cond =
1562             genCond(*caseValue++, mlir::arith::CmpIPredicate::sge);
1563         genFIRConditionalBranch(cond, newBlock, newBlock2);
1564         builder->setInsertionPointToEnd(newBlock);
1565         mlir::Value cond2 =
1566             genCond(*caseValue++, mlir::arith::CmpIPredicate::sle);
1567         genFIRConditionalBranch(cond2, *caseBlock++, newBlock2);
1568         builder->setInsertionPointToEnd(newBlock2);
1569         continue;
1570       }
1571       mlir::arith::CmpIPredicate pred;
1572       if (attr.isa<fir::PointIntervalAttr>()) {
1573         pred = mlir::arith::CmpIPredicate::eq;
1574       } else if (attr.isa<fir::LowerBoundAttr>()) {
1575         pred = mlir::arith::CmpIPredicate::sge;
1576       } else {
1577         assert(attr.isa<fir::UpperBoundAttr>() && "unexpected predicate");
1578         pred = mlir::arith::CmpIPredicate::sle;
1579       }
1580       mlir::Value cond = genCond(*caseValue++, pred);
1581       genFIRConditionalBranch(cond, *caseBlock++, newBlock);
1582       builder->setInsertionPointToEnd(newBlock);
1583     }
1584     assert(caseValue == valueList.end() && caseBlock == blockList.end() &&
1585            "select case list mismatch");
1586     // Clean-up the selector at the end of the construct if it is a temporary
1587     // (which is possible with characters).
1588     mlir::OpBuilder::InsertPoint insertPt = builder->saveInsertionPoint();
1589     builder->setInsertionPointToEnd(eval.parentConstruct->constructExit->block);
1590     stmtCtx.finalize();
1591     builder->restoreInsertionPoint(insertPt);
1592   }
1593 
1594   fir::ExtendedValue
1595   genAssociateSelector(const Fortran::lower::SomeExpr &selector,
1596                        Fortran::lower::StatementContext &stmtCtx) {
1597     return Fortran::lower::isArraySectionWithoutVectorSubscript(selector)
1598                ? Fortran::lower::createSomeArrayBox(*this, selector,
1599                                                     localSymbols, stmtCtx)
1600                : genExprAddr(selector, stmtCtx);
1601   }
1602 
1603   void genFIR(const Fortran::parser::AssociateConstruct &) {
1604     Fortran::lower::StatementContext stmtCtx;
1605     Fortran::lower::pft::Evaluation &eval = getEval();
1606     for (Fortran::lower::pft::Evaluation &e : eval.getNestedEvaluations()) {
1607       if (auto *stmt = e.getIf<Fortran::parser::AssociateStmt>()) {
1608         if (eval.lowerAsUnstructured())
1609           maybeStartBlock(e.block);
1610         localSymbols.pushScope();
1611         for (const Fortran::parser::Association &assoc :
1612              std::get<std::list<Fortran::parser::Association>>(stmt->t)) {
1613           Fortran::semantics::Symbol &sym =
1614               *std::get<Fortran::parser::Name>(assoc.t).symbol;
1615           const Fortran::lower::SomeExpr &selector =
1616               *sym.get<Fortran::semantics::AssocEntityDetails>().expr();
1617           localSymbols.addSymbol(sym, genAssociateSelector(selector, stmtCtx));
1618         }
1619       } else if (e.getIf<Fortran::parser::EndAssociateStmt>()) {
1620         if (eval.lowerAsUnstructured())
1621           maybeStartBlock(e.block);
1622         stmtCtx.finalize();
1623         localSymbols.popScope();
1624       } else {
1625         genFIR(e);
1626       }
1627     }
1628   }
1629 
1630   void genFIR(const Fortran::parser::BlockConstruct &blockConstruct) {
1631     setCurrentPositionAt(blockConstruct);
1632     TODO(toLocation(), "BlockConstruct lowering");
1633   }
1634   void genFIR(const Fortran::parser::BlockStmt &) {
1635     TODO(toLocation(), "BlockStmt lowering");
1636   }
1637   void genFIR(const Fortran::parser::EndBlockStmt &) {
1638     TODO(toLocation(), "EndBlockStmt lowering");
1639   }
1640 
1641   void genFIR(const Fortran::parser::ChangeTeamConstruct &construct) {
1642     TODO(toLocation(), "ChangeTeamConstruct lowering");
1643   }
1644   void genFIR(const Fortran::parser::ChangeTeamStmt &stmt) {
1645     TODO(toLocation(), "ChangeTeamStmt lowering");
1646   }
1647   void genFIR(const Fortran::parser::EndChangeTeamStmt &stmt) {
1648     TODO(toLocation(), "EndChangeTeamStmt lowering");
1649   }
1650 
1651   void genFIR(const Fortran::parser::CriticalConstruct &criticalConstruct) {
1652     setCurrentPositionAt(criticalConstruct);
1653     TODO(toLocation(), "CriticalConstruct lowering");
1654   }
1655   void genFIR(const Fortran::parser::CriticalStmt &) {
1656     TODO(toLocation(), "CriticalStmt lowering");
1657   }
1658   void genFIR(const Fortran::parser::EndCriticalStmt &) {
1659     TODO(toLocation(), "EndCriticalStmt lowering");
1660   }
1661 
1662   void genFIR(const Fortran::parser::SelectRankConstruct &selectRankConstruct) {
1663     setCurrentPositionAt(selectRankConstruct);
1664     TODO(toLocation(), "SelectRankConstruct lowering");
1665   }
1666   void genFIR(const Fortran::parser::SelectRankStmt &) {
1667     TODO(toLocation(), "SelectRankStmt lowering");
1668   }
1669   void genFIR(const Fortran::parser::SelectRankCaseStmt &) {
1670     TODO(toLocation(), "SelectRankCaseStmt lowering");
1671   }
1672 
1673   void genFIR(const Fortran::parser::SelectTypeConstruct &selectTypeConstruct) {
1674     setCurrentPositionAt(selectTypeConstruct);
1675     TODO(toLocation(), "SelectTypeConstruct lowering");
1676   }
1677   void genFIR(const Fortran::parser::SelectTypeStmt &) {
1678     TODO(toLocation(), "SelectTypeStmt lowering");
1679   }
1680   void genFIR(const Fortran::parser::TypeGuardStmt &) {
1681     TODO(toLocation(), "TypeGuardStmt lowering");
1682   }
1683 
1684   //===--------------------------------------------------------------------===//
1685   // IO statements (see io.h)
1686   //===--------------------------------------------------------------------===//
1687 
1688   void genFIR(const Fortran::parser::BackspaceStmt &stmt) {
1689     mlir::Value iostat = genBackspaceStatement(*this, stmt);
1690     genIoConditionBranches(getEval(), stmt.v, iostat);
1691   }
1692   void genFIR(const Fortran::parser::CloseStmt &stmt) {
1693     mlir::Value iostat = genCloseStatement(*this, stmt);
1694     genIoConditionBranches(getEval(), stmt.v, iostat);
1695   }
1696   void genFIR(const Fortran::parser::EndfileStmt &stmt) {
1697     mlir::Value iostat = genEndfileStatement(*this, stmt);
1698     genIoConditionBranches(getEval(), stmt.v, iostat);
1699   }
1700   void genFIR(const Fortran::parser::FlushStmt &stmt) {
1701     mlir::Value iostat = genFlushStatement(*this, stmt);
1702     genIoConditionBranches(getEval(), stmt.v, iostat);
1703   }
1704   void genFIR(const Fortran::parser::InquireStmt &stmt) {
1705     mlir::Value iostat = genInquireStatement(*this, stmt);
1706     if (const auto *specs =
1707             std::get_if<std::list<Fortran::parser::InquireSpec>>(&stmt.u))
1708       genIoConditionBranches(getEval(), *specs, iostat);
1709   }
1710   void genFIR(const Fortran::parser::OpenStmt &stmt) {
1711     mlir::Value iostat = genOpenStatement(*this, stmt);
1712     genIoConditionBranches(getEval(), stmt.v, iostat);
1713   }
1714   void genFIR(const Fortran::parser::PrintStmt &stmt) {
1715     genPrintStatement(*this, stmt);
1716   }
1717   void genFIR(const Fortran::parser::ReadStmt &stmt) {
1718     mlir::Value iostat = genReadStatement(*this, stmt);
1719     genIoConditionBranches(getEval(), stmt.controls, iostat);
1720   }
1721   void genFIR(const Fortran::parser::RewindStmt &stmt) {
1722     mlir::Value iostat = genRewindStatement(*this, stmt);
1723     genIoConditionBranches(getEval(), stmt.v, iostat);
1724   }
1725   void genFIR(const Fortran::parser::WaitStmt &stmt) {
1726     mlir::Value iostat = genWaitStatement(*this, stmt);
1727     genIoConditionBranches(getEval(), stmt.v, iostat);
1728   }
1729   void genFIR(const Fortran::parser::WriteStmt &stmt) {
1730     mlir::Value iostat = genWriteStatement(*this, stmt);
1731     genIoConditionBranches(getEval(), stmt.controls, iostat);
1732   }
1733 
1734   template <typename A>
1735   void genIoConditionBranches(Fortran::lower::pft::Evaluation &eval,
1736                               const A &specList, mlir::Value iostat) {
1737     if (!iostat)
1738       return;
1739 
1740     mlir::Block *endBlock = nullptr;
1741     mlir::Block *eorBlock = nullptr;
1742     mlir::Block *errBlock = nullptr;
1743     for (const auto &spec : specList) {
1744       std::visit(Fortran::common::visitors{
1745                      [&](const Fortran::parser::EndLabel &label) {
1746                        endBlock = blockOfLabel(eval, label.v);
1747                      },
1748                      [&](const Fortran::parser::EorLabel &label) {
1749                        eorBlock = blockOfLabel(eval, label.v);
1750                      },
1751                      [&](const Fortran::parser::ErrLabel &label) {
1752                        errBlock = blockOfLabel(eval, label.v);
1753                      },
1754                      [](const auto &) {}},
1755                  spec.u);
1756     }
1757     if (!endBlock && !eorBlock && !errBlock)
1758       return;
1759 
1760     mlir::Location loc = toLocation();
1761     mlir::Type indexType = builder->getIndexType();
1762     mlir::Value selector = builder->createConvert(loc, indexType, iostat);
1763     llvm::SmallVector<int64_t> indexList;
1764     llvm::SmallVector<mlir::Block *> blockList;
1765     if (eorBlock) {
1766       indexList.push_back(Fortran::runtime::io::IostatEor);
1767       blockList.push_back(eorBlock);
1768     }
1769     if (endBlock) {
1770       indexList.push_back(Fortran::runtime::io::IostatEnd);
1771       blockList.push_back(endBlock);
1772     }
1773     if (errBlock) {
1774       indexList.push_back(0);
1775       blockList.push_back(eval.nonNopSuccessor().block);
1776       // ERR label statement is the default successor.
1777       blockList.push_back(errBlock);
1778     } else {
1779       // Fallthrough successor statement is the default successor.
1780       blockList.push_back(eval.nonNopSuccessor().block);
1781     }
1782     builder->create<fir::SelectOp>(loc, selector, indexList, blockList);
1783   }
1784 
1785   //===--------------------------------------------------------------------===//
1786   // Memory allocation and deallocation
1787   //===--------------------------------------------------------------------===//
1788 
1789   void genFIR(const Fortran::parser::AllocateStmt &stmt) {
1790     Fortran::lower::genAllocateStmt(*this, stmt, toLocation());
1791   }
1792 
1793   void genFIR(const Fortran::parser::DeallocateStmt &stmt) {
1794     Fortran::lower::genDeallocateStmt(*this, stmt, toLocation());
1795   }
1796 
1797   /// Nullify pointer object list
1798   ///
1799   /// For each pointer object, reset the pointer to a disassociated status.
1800   /// We do this by setting each pointer to null.
1801   void genFIR(const Fortran::parser::NullifyStmt &stmt) {
1802     mlir::Location loc = toLocation();
1803     for (auto &pointerObject : stmt.v) {
1804       const Fortran::lower::SomeExpr *expr =
1805           Fortran::semantics::GetExpr(pointerObject);
1806       assert(expr);
1807       fir::MutableBoxValue box = genExprMutableBox(loc, *expr);
1808       fir::factory::disassociateMutableBox(*builder, loc, box);
1809     }
1810   }
1811 
1812   //===--------------------------------------------------------------------===//
1813 
1814   void genFIR(const Fortran::parser::EventPostStmt &stmt) {
1815     genEventPostStatement(*this, stmt);
1816   }
1817 
1818   void genFIR(const Fortran::parser::EventWaitStmt &stmt) {
1819     genEventWaitStatement(*this, stmt);
1820   }
1821 
1822   void genFIR(const Fortran::parser::FormTeamStmt &stmt) {
1823     genFormTeamStatement(*this, getEval(), stmt);
1824   }
1825 
1826   void genFIR(const Fortran::parser::LockStmt &stmt) {
1827     genLockStatement(*this, stmt);
1828   }
1829 
1830   fir::ExtendedValue
1831   genInitializerExprValue(const Fortran::lower::SomeExpr &expr,
1832                           Fortran::lower::StatementContext &stmtCtx) {
1833     return Fortran::lower::createSomeInitializerExpression(
1834         toLocation(), *this, expr, localSymbols, stmtCtx);
1835   }
1836 
1837   /// Return true if the current context is a conditionalized and implied
1838   /// iteration space.
1839   bool implicitIterationSpace() { return !implicitIterSpace.empty(); }
1840 
1841   /// Return true if context is currently an explicit iteration space. A scalar
1842   /// assignment expression may be contextually within a user-defined iteration
1843   /// space, transforming it into an array expression.
1844   bool explicitIterationSpace() { return explicitIterSpace.isActive(); }
1845 
1846   /// Generate an array assignment.
1847   /// This is an assignment expression with rank > 0. The assignment may or may
1848   /// not be in a WHERE and/or FORALL context.
1849   /// In a FORALL context, the assignment may be a pointer assignment and the \p
1850   /// lbounds and \p ubounds parameters should only be used in such a pointer
1851   /// assignment case. (If both are None then the array assignment cannot be a
1852   /// pointer assignment.)
1853   void genArrayAssignment(
1854       const Fortran::evaluate::Assignment &assign,
1855       Fortran::lower::StatementContext &stmtCtx,
1856       llvm::Optional<llvm::SmallVector<mlir::Value>> lbounds = llvm::None,
1857       llvm::Optional<llvm::SmallVector<mlir::Value>> ubounds = llvm::None) {
1858     if (Fortran::lower::isWholeAllocatable(assign.lhs)) {
1859       // Assignment to allocatables may require the lhs to be
1860       // deallocated/reallocated. See Fortran 2018 10.2.1.3 p3
1861       Fortran::lower::createAllocatableArrayAssignment(
1862           *this, assign.lhs, assign.rhs, explicitIterSpace, implicitIterSpace,
1863           localSymbols, stmtCtx);
1864       return;
1865     }
1866 
1867     if (lbounds.hasValue()) {
1868       // Array of POINTER entities, with elemental assignment.
1869       if (!Fortran::lower::isWholePointer(assign.lhs))
1870         fir::emitFatalError(toLocation(), "pointer assignment to non-pointer");
1871 
1872       Fortran::lower::createArrayOfPointerAssignment(
1873           *this, assign.lhs, assign.rhs, explicitIterSpace, implicitIterSpace,
1874           lbounds.getValue(), ubounds, localSymbols, stmtCtx);
1875       return;
1876     }
1877 
1878     if (!implicitIterationSpace() && !explicitIterationSpace()) {
1879       // No masks and the iteration space is implied by the array, so create a
1880       // simple array assignment.
1881       Fortran::lower::createSomeArrayAssignment(*this, assign.lhs, assign.rhs,
1882                                                 localSymbols, stmtCtx);
1883       return;
1884     }
1885 
1886     // If there is an explicit iteration space, generate an array assignment
1887     // with a user-specified iteration space and possibly with masks. These
1888     // assignments may *appear* to be scalar expressions, but the scalar
1889     // expression is evaluated at all points in the user-defined space much like
1890     // an ordinary array assignment. More specifically, the semantics inside the
1891     // FORALL much more closely resembles that of WHERE than a scalar
1892     // assignment.
1893     // Otherwise, generate a masked array assignment. The iteration space is
1894     // implied by the lhs array expression.
1895     Fortran::lower::createAnyMaskedArrayAssignment(
1896         *this, assign.lhs, assign.rhs, explicitIterSpace, implicitIterSpace,
1897         localSymbols,
1898         explicitIterationSpace() ? explicitIterSpace.stmtContext()
1899                                  : implicitIterSpace.stmtContext());
1900   }
1901 
1902 #if !defined(NDEBUG)
1903   static bool isFuncResultDesignator(const Fortran::lower::SomeExpr &expr) {
1904     const Fortran::semantics::Symbol *sym =
1905         Fortran::evaluate::GetFirstSymbol(expr);
1906     return sym && sym->IsFuncResult();
1907   }
1908 #endif
1909 
1910   inline fir::MutableBoxValue
1911   genExprMutableBox(mlir::Location loc,
1912                     const Fortran::lower::SomeExpr &expr) override final {
1913     return Fortran::lower::createMutableBox(loc, *this, expr, localSymbols);
1914   }
1915 
1916   /// Shared for both assignments and pointer assignments.
1917   void genAssignment(const Fortran::evaluate::Assignment &assign) {
1918     Fortran::lower::StatementContext stmtCtx;
1919     mlir::Location loc = toLocation();
1920     if (explicitIterationSpace()) {
1921       Fortran::lower::createArrayLoads(*this, explicitIterSpace, localSymbols);
1922       explicitIterSpace.genLoopNest();
1923     }
1924     std::visit(
1925         Fortran::common::visitors{
1926             // [1] Plain old assignment.
1927             [&](const Fortran::evaluate::Assignment::Intrinsic &) {
1928               const Fortran::semantics::Symbol *sym =
1929                   Fortran::evaluate::GetLastSymbol(assign.lhs);
1930 
1931               if (!sym)
1932                 TODO(loc, "assignment to pointer result of function reference");
1933 
1934               std::optional<Fortran::evaluate::DynamicType> lhsType =
1935                   assign.lhs.GetType();
1936               assert(lhsType && "lhs cannot be typeless");
1937               // Assignment to polymorphic allocatables may require changing the
1938               // variable dynamic type (See Fortran 2018 10.2.1.3 p3).
1939               if (lhsType->IsPolymorphic() &&
1940                   Fortran::lower::isWholeAllocatable(assign.lhs))
1941                 TODO(loc, "assignment to polymorphic allocatable");
1942 
1943               // Note: No ad-hoc handling for pointers is required here. The
1944               // target will be assigned as per 2018 10.2.1.3 p2. genExprAddr
1945               // on a pointer returns the target address and not the address of
1946               // the pointer variable.
1947 
1948               if (assign.lhs.Rank() > 0 || explicitIterationSpace()) {
1949                 // Array assignment
1950                 // See Fortran 2018 10.2.1.3 p5, p6, and p7
1951                 genArrayAssignment(assign, stmtCtx);
1952                 return;
1953               }
1954 
1955               // Scalar assignment
1956               const bool isNumericScalar =
1957                   isNumericScalarCategory(lhsType->category());
1958               fir::ExtendedValue rhs = isNumericScalar
1959                                            ? genExprValue(assign.rhs, stmtCtx)
1960                                            : genExprAddr(assign.rhs, stmtCtx);
1961               const bool lhsIsWholeAllocatable =
1962                   Fortran::lower::isWholeAllocatable(assign.lhs);
1963               llvm::Optional<fir::factory::MutableBoxReallocation> lhsRealloc;
1964               llvm::Optional<fir::MutableBoxValue> lhsMutableBox;
1965               auto lhs = [&]() -> fir::ExtendedValue {
1966                 if (lhsIsWholeAllocatable) {
1967                   lhsMutableBox = genExprMutableBox(loc, assign.lhs);
1968                   llvm::SmallVector<mlir::Value> lengthParams;
1969                   if (const fir::CharBoxValue *charBox = rhs.getCharBox())
1970                     lengthParams.push_back(charBox->getLen());
1971                   else if (fir::isDerivedWithLenParameters(rhs))
1972                     TODO(loc, "assignment to derived type allocatable with "
1973                               "length parameters");
1974                   lhsRealloc = fir::factory::genReallocIfNeeded(
1975                       *builder, loc, *lhsMutableBox,
1976                       /*shape=*/llvm::None, lengthParams);
1977                   return lhsRealloc->newValue;
1978                 }
1979                 return genExprAddr(assign.lhs, stmtCtx);
1980               }();
1981 
1982               if (isNumericScalar) {
1983                 // Fortran 2018 10.2.1.3 p8 and p9
1984                 // Conversions should have been inserted by semantic analysis,
1985                 // but they can be incorrect between the rhs and lhs. Correct
1986                 // that here.
1987                 mlir::Value addr = fir::getBase(lhs);
1988                 mlir::Value val = fir::getBase(rhs);
1989                 // A function with multiple entry points returning different
1990                 // types tags all result variables with one of the largest
1991                 // types to allow them to share the same storage.  Assignment
1992                 // to a result variable of one of the other types requires
1993                 // conversion to the actual type.
1994                 mlir::Type toTy = genType(assign.lhs);
1995                 mlir::Value cast =
1996                     builder->convertWithSemantics(loc, toTy, val);
1997                 if (fir::dyn_cast_ptrEleTy(addr.getType()) != toTy) {
1998                   assert(isFuncResultDesignator(assign.lhs) && "type mismatch");
1999                   addr = builder->createConvert(
2000                       toLocation(), builder->getRefType(toTy), addr);
2001                 }
2002                 builder->create<fir::StoreOp>(loc, cast, addr);
2003               } else if (isCharacterCategory(lhsType->category())) {
2004                 // Fortran 2018 10.2.1.3 p10 and p11
2005                 fir::factory::CharacterExprHelper{*builder, loc}.createAssign(
2006                     lhs, rhs);
2007               } else if (isDerivedCategory(lhsType->category())) {
2008                 // Fortran 2018 10.2.1.3 p13 and p14
2009                 // Recursively gen an assignment on each element pair.
2010                 fir::factory::genRecordAssignment(*builder, loc, lhs, rhs);
2011               } else {
2012                 llvm_unreachable("unknown category");
2013               }
2014               if (lhsIsWholeAllocatable)
2015                 fir::factory::finalizeRealloc(
2016                     *builder, loc, lhsMutableBox.getValue(),
2017                     /*lbounds=*/llvm::None, /*takeLboundsIfRealloc=*/false,
2018                     lhsRealloc.getValue());
2019             },
2020 
2021             // [2] User defined assignment. If the context is a scalar
2022             // expression then call the procedure.
2023             [&](const Fortran::evaluate::ProcedureRef &procRef) {
2024               Fortran::lower::StatementContext &ctx =
2025                   explicitIterationSpace() ? explicitIterSpace.stmtContext()
2026                                            : stmtCtx;
2027               Fortran::lower::createSubroutineCall(
2028                   *this, procRef, explicitIterSpace, implicitIterSpace,
2029                   localSymbols, ctx, /*isUserDefAssignment=*/true);
2030             },
2031 
2032             // [3] Pointer assignment with possibly empty bounds-spec. R1035: a
2033             // bounds-spec is a lower bound value.
2034             [&](const Fortran::evaluate::Assignment::BoundsSpec &lbExprs) {
2035               if (Fortran::evaluate::IsProcedure(assign.rhs))
2036                 TODO(loc, "procedure pointer assignment");
2037               std::optional<Fortran::evaluate::DynamicType> lhsType =
2038                   assign.lhs.GetType();
2039               std::optional<Fortran::evaluate::DynamicType> rhsType =
2040                   assign.rhs.GetType();
2041               // Polymorphic lhs/rhs may need more care. See F2018 10.2.2.3.
2042               if ((lhsType && lhsType->IsPolymorphic()) ||
2043                   (rhsType && rhsType->IsPolymorphic()))
2044                 TODO(loc, "pointer assignment involving polymorphic entity");
2045 
2046               llvm::SmallVector<mlir::Value> lbounds;
2047               for (const Fortran::evaluate::ExtentExpr &lbExpr : lbExprs)
2048                 lbounds.push_back(
2049                     fir::getBase(genExprValue(toEvExpr(lbExpr), stmtCtx)));
2050               if (explicitIterationSpace()) {
2051                 // Pointer assignment in FORALL context. Copy the rhs box value
2052                 // into the lhs box variable.
2053                 genArrayAssignment(assign, stmtCtx, lbounds);
2054                 return;
2055               }
2056               fir::MutableBoxValue lhs = genExprMutableBox(loc, assign.lhs);
2057               Fortran::lower::associateMutableBox(*this, loc, lhs, assign.rhs,
2058                                                   lbounds, stmtCtx);
2059             },
2060 
2061             // [4] Pointer assignment with bounds-remapping. R1036: a
2062             // bounds-remapping is a pair, lower bound and upper bound.
2063             [&](const Fortran::evaluate::Assignment::BoundsRemapping
2064                     &boundExprs) {
2065               std::optional<Fortran::evaluate::DynamicType> lhsType =
2066                   assign.lhs.GetType();
2067               std::optional<Fortran::evaluate::DynamicType> rhsType =
2068                   assign.rhs.GetType();
2069               // Polymorphic lhs/rhs may need more care. See F2018 10.2.2.3.
2070               if ((lhsType && lhsType->IsPolymorphic()) ||
2071                   (rhsType && rhsType->IsPolymorphic()))
2072                 TODO(loc, "pointer assignment involving polymorphic entity");
2073 
2074               llvm::SmallVector<mlir::Value> lbounds;
2075               llvm::SmallVector<mlir::Value> ubounds;
2076               for (const std::pair<Fortran::evaluate::ExtentExpr,
2077                                    Fortran::evaluate::ExtentExpr> &pair :
2078                    boundExprs) {
2079                 const Fortran::evaluate::ExtentExpr &lbExpr = pair.first;
2080                 const Fortran::evaluate::ExtentExpr &ubExpr = pair.second;
2081                 lbounds.push_back(
2082                     fir::getBase(genExprValue(toEvExpr(lbExpr), stmtCtx)));
2083                 ubounds.push_back(
2084                     fir::getBase(genExprValue(toEvExpr(ubExpr), stmtCtx)));
2085               }
2086               if (explicitIterationSpace()) {
2087                 // Pointer assignment in FORALL context. Copy the rhs box value
2088                 // into the lhs box variable.
2089                 genArrayAssignment(assign, stmtCtx, lbounds, ubounds);
2090                 return;
2091               }
2092               fir::MutableBoxValue lhs = genExprMutableBox(loc, assign.lhs);
2093               if (Fortran::evaluate::UnwrapExpr<Fortran::evaluate::NullPointer>(
2094                       assign.rhs)) {
2095                 fir::factory::disassociateMutableBox(*builder, loc, lhs);
2096                 return;
2097               }
2098               // Do not generate a temp in case rhs is an array section.
2099               fir::ExtendedValue rhs =
2100                   Fortran::lower::isArraySectionWithoutVectorSubscript(
2101                       assign.rhs)
2102                       ? Fortran::lower::createSomeArrayBox(
2103                             *this, assign.rhs, localSymbols, stmtCtx)
2104                       : genExprAddr(assign.rhs, stmtCtx);
2105               fir::factory::associateMutableBoxWithRemap(*builder, loc, lhs,
2106                                                          rhs, lbounds, ubounds);
2107               if (explicitIterationSpace()) {
2108                 mlir::ValueRange inners = explicitIterSpace.getInnerArgs();
2109                 if (!inners.empty())
2110                   builder->create<fir::ResultOp>(loc, inners);
2111               }
2112             },
2113         },
2114         assign.u);
2115     if (explicitIterationSpace())
2116       Fortran::lower::createArrayMergeStores(*this, explicitIterSpace);
2117   }
2118 
2119   void genFIR(const Fortran::parser::WhereConstruct &c) {
2120     implicitIterSpace.growStack();
2121     genNestedStatement(
2122         std::get<
2123             Fortran::parser::Statement<Fortran::parser::WhereConstructStmt>>(
2124             c.t));
2125     for (const auto &body :
2126          std::get<std::list<Fortran::parser::WhereBodyConstruct>>(c.t))
2127       genFIR(body);
2128     for (const auto &e :
2129          std::get<std::list<Fortran::parser::WhereConstruct::MaskedElsewhere>>(
2130              c.t))
2131       genFIR(e);
2132     if (const auto &e =
2133             std::get<std::optional<Fortran::parser::WhereConstruct::Elsewhere>>(
2134                 c.t);
2135         e.has_value())
2136       genFIR(*e);
2137     genNestedStatement(
2138         std::get<Fortran::parser::Statement<Fortran::parser::EndWhereStmt>>(
2139             c.t));
2140   }
2141   void genFIR(const Fortran::parser::WhereBodyConstruct &body) {
2142     std::visit(
2143         Fortran::common::visitors{
2144             [&](const Fortran::parser::Statement<
2145                 Fortran::parser::AssignmentStmt> &stmt) {
2146               genNestedStatement(stmt);
2147             },
2148             [&](const Fortran::parser::Statement<Fortran::parser::WhereStmt>
2149                     &stmt) { genNestedStatement(stmt); },
2150             [&](const Fortran::common::Indirection<
2151                 Fortran::parser::WhereConstruct> &c) { genFIR(c.value()); },
2152         },
2153         body.u);
2154   }
2155   void genFIR(const Fortran::parser::WhereConstructStmt &stmt) {
2156     implicitIterSpace.append(Fortran::semantics::GetExpr(
2157         std::get<Fortran::parser::LogicalExpr>(stmt.t)));
2158   }
2159   void genFIR(const Fortran::parser::WhereConstruct::MaskedElsewhere &ew) {
2160     genNestedStatement(
2161         std::get<
2162             Fortran::parser::Statement<Fortran::parser::MaskedElsewhereStmt>>(
2163             ew.t));
2164     for (const auto &body :
2165          std::get<std::list<Fortran::parser::WhereBodyConstruct>>(ew.t))
2166       genFIR(body);
2167   }
2168   void genFIR(const Fortran::parser::MaskedElsewhereStmt &stmt) {
2169     implicitIterSpace.append(Fortran::semantics::GetExpr(
2170         std::get<Fortran::parser::LogicalExpr>(stmt.t)));
2171   }
2172   void genFIR(const Fortran::parser::WhereConstruct::Elsewhere &ew) {
2173     genNestedStatement(
2174         std::get<Fortran::parser::Statement<Fortran::parser::ElsewhereStmt>>(
2175             ew.t));
2176     for (const auto &body :
2177          std::get<std::list<Fortran::parser::WhereBodyConstruct>>(ew.t))
2178       genFIR(body);
2179   }
2180   void genFIR(const Fortran::parser::ElsewhereStmt &stmt) {
2181     implicitIterSpace.append(nullptr);
2182   }
2183   void genFIR(const Fortran::parser::EndWhereStmt &) {
2184     implicitIterSpace.shrinkStack();
2185   }
2186 
2187   void genFIR(const Fortran::parser::WhereStmt &stmt) {
2188     Fortran::lower::StatementContext stmtCtx;
2189     const auto &assign = std::get<Fortran::parser::AssignmentStmt>(stmt.t);
2190     implicitIterSpace.growStack();
2191     implicitIterSpace.append(Fortran::semantics::GetExpr(
2192         std::get<Fortran::parser::LogicalExpr>(stmt.t)));
2193     genAssignment(*assign.typedAssignment->v);
2194     implicitIterSpace.shrinkStack();
2195   }
2196 
2197   void genFIR(const Fortran::parser::PointerAssignmentStmt &stmt) {
2198     genAssignment(*stmt.typedAssignment->v);
2199   }
2200 
2201   void genFIR(const Fortran::parser::AssignmentStmt &stmt) {
2202     genAssignment(*stmt.typedAssignment->v);
2203   }
2204 
2205   void genFIR(const Fortran::parser::SyncAllStmt &stmt) {
2206     genSyncAllStatement(*this, stmt);
2207   }
2208 
2209   void genFIR(const Fortran::parser::SyncImagesStmt &stmt) {
2210     genSyncImagesStatement(*this, stmt);
2211   }
2212 
2213   void genFIR(const Fortran::parser::SyncMemoryStmt &stmt) {
2214     genSyncMemoryStatement(*this, stmt);
2215   }
2216 
2217   void genFIR(const Fortran::parser::SyncTeamStmt &stmt) {
2218     genSyncTeamStatement(*this, stmt);
2219   }
2220 
2221   void genFIR(const Fortran::parser::UnlockStmt &stmt) {
2222     genUnlockStatement(*this, stmt);
2223   }
2224 
2225   void genFIR(const Fortran::parser::AssignStmt &stmt) {
2226     const Fortran::semantics::Symbol &symbol =
2227         *std::get<Fortran::parser::Name>(stmt.t).symbol;
2228     mlir::Location loc = toLocation();
2229     mlir::Value labelValue = builder->createIntegerConstant(
2230         loc, genType(symbol), std::get<Fortran::parser::Label>(stmt.t));
2231     builder->create<fir::StoreOp>(loc, labelValue, getSymbolAddress(symbol));
2232   }
2233 
2234   void genFIR(const Fortran::parser::FormatStmt &) {
2235     // do nothing.
2236 
2237     // FORMAT statements have no semantics. They may be lowered if used by a
2238     // data transfer statement.
2239   }
2240 
2241   void genFIR(const Fortran::parser::PauseStmt &stmt) {
2242     genPauseStatement(*this, stmt);
2243   }
2244 
2245   // call FAIL IMAGE in runtime
2246   void genFIR(const Fortran::parser::FailImageStmt &stmt) {
2247     genFailImageStatement(*this);
2248   }
2249 
2250   // call STOP, ERROR STOP in runtime
2251   void genFIR(const Fortran::parser::StopStmt &stmt) {
2252     genStopStatement(*this, stmt);
2253   }
2254 
2255   void genFIR(const Fortran::parser::ReturnStmt &stmt) {
2256     Fortran::lower::pft::FunctionLikeUnit *funit =
2257         getEval().getOwningProcedure();
2258     assert(funit && "not inside main program, function or subroutine");
2259     if (funit->isMainProgram()) {
2260       genExitRoutine();
2261       return;
2262     }
2263     mlir::Location loc = toLocation();
2264     if (stmt.v) {
2265       // Alternate return statement - If this is a subroutine where some
2266       // alternate entries have alternate returns, but the active entry point
2267       // does not, ignore the alternate return value.  Otherwise, assign it
2268       // to the compiler-generated result variable.
2269       const Fortran::semantics::Symbol &symbol = funit->getSubprogramSymbol();
2270       if (Fortran::semantics::HasAlternateReturns(symbol)) {
2271         Fortran::lower::StatementContext stmtCtx;
2272         const Fortran::lower::SomeExpr *expr =
2273             Fortran::semantics::GetExpr(*stmt.v);
2274         assert(expr && "missing alternate return expression");
2275         mlir::Value altReturnIndex = builder->createConvert(
2276             loc, builder->getIndexType(), createFIRExpr(loc, expr, stmtCtx));
2277         builder->create<fir::StoreOp>(loc, altReturnIndex,
2278                                       getAltReturnResult(symbol));
2279       }
2280     }
2281     // Branch to the last block of the SUBROUTINE, which has the actual return.
2282     if (!funit->finalBlock) {
2283       mlir::OpBuilder::InsertPoint insPt = builder->saveInsertionPoint();
2284       funit->finalBlock = builder->createBlock(&builder->getRegion());
2285       builder->restoreInsertionPoint(insPt);
2286     }
2287     builder->create<mlir::cf::BranchOp>(loc, funit->finalBlock);
2288   }
2289 
2290   void genFIR(const Fortran::parser::CycleStmt &) {
2291     genFIRBranch(getEval().controlSuccessor->block);
2292   }
2293   void genFIR(const Fortran::parser::ExitStmt &) {
2294     genFIRBranch(getEval().controlSuccessor->block);
2295   }
2296   void genFIR(const Fortran::parser::GotoStmt &) {
2297     genFIRBranch(getEval().controlSuccessor->block);
2298   }
2299 
2300   // Nop statements - No code, or code is generated at the construct level.
2301   void genFIR(const Fortran::parser::AssociateStmt &) {}       // nop
2302   void genFIR(const Fortran::parser::CaseStmt &) {}            // nop
2303   void genFIR(const Fortran::parser::ContinueStmt &) {}        // nop
2304   void genFIR(const Fortran::parser::ElseIfStmt &) {}          // nop
2305   void genFIR(const Fortran::parser::ElseStmt &) {}            // nop
2306   void genFIR(const Fortran::parser::EndAssociateStmt &) {}    // nop
2307   void genFIR(const Fortran::parser::EndDoStmt &) {}           // nop
2308   void genFIR(const Fortran::parser::EndFunctionStmt &) {}     // nop
2309   void genFIR(const Fortran::parser::EndIfStmt &) {}           // nop
2310   void genFIR(const Fortran::parser::EndMpSubprogramStmt &) {} // nop
2311   void genFIR(const Fortran::parser::EndSelectStmt &) {}       // nop
2312   void genFIR(const Fortran::parser::EndSubroutineStmt &) {}   // nop
2313   void genFIR(const Fortran::parser::EntryStmt &) {}           // nop
2314   void genFIR(const Fortran::parser::IfStmt &) {}              // nop
2315   void genFIR(const Fortran::parser::IfThenStmt &) {}          // nop
2316   void genFIR(const Fortran::parser::NonLabelDoStmt &) {}      // nop
2317   void genFIR(const Fortran::parser::OmpEndLoopDirective &) {} // nop
2318 
2319   void genFIR(const Fortran::parser::NamelistStmt &) {
2320     TODO(toLocation(), "NamelistStmt lowering");
2321   }
2322 
2323   /// Generate FIR for the Evaluation `eval`.
2324   void genFIR(Fortran::lower::pft::Evaluation &eval,
2325               bool unstructuredContext = true) {
2326     if (unstructuredContext) {
2327       // When transitioning from unstructured to structured code,
2328       // the structured code could be a target that starts a new block.
2329       maybeStartBlock(eval.isConstruct() && eval.lowerAsStructured()
2330                           ? eval.getFirstNestedEvaluation().block
2331                           : eval.block);
2332     }
2333 
2334     setCurrentEval(eval);
2335     setCurrentPosition(eval.position);
2336     eval.visit([&](const auto &stmt) { genFIR(stmt); });
2337 
2338     if (unstructuredContext && blockIsUnterminated()) {
2339       // Exit from an unstructured IF or SELECT construct block.
2340       Fortran::lower::pft::Evaluation *successor{};
2341       if (eval.isActionStmt())
2342         successor = eval.controlSuccessor;
2343       else if (eval.isConstruct() &&
2344                eval.getLastNestedEvaluation()
2345                    .lexicalSuccessor->isIntermediateConstructStmt())
2346         successor = eval.constructExit;
2347       if (successor && successor->block)
2348         genFIRBranch(successor->block);
2349     }
2350   }
2351 
2352   /// Map mlir function block arguments to the corresponding Fortran dummy
2353   /// variables. When the result is passed as a hidden argument, the Fortran
2354   /// result is also mapped. The symbol map is used to hold this mapping.
2355   void mapDummiesAndResults(Fortran::lower::pft::FunctionLikeUnit &funit,
2356                             const Fortran::lower::CalleeInterface &callee) {
2357     assert(builder && "require a builder object at this point");
2358     using PassBy = Fortran::lower::CalleeInterface::PassEntityBy;
2359     auto mapPassedEntity = [&](const auto arg) {
2360       if (arg.passBy == PassBy::AddressAndLength) {
2361         // TODO: now that fir call has some attributes regarding character
2362         // return, PassBy::AddressAndLength should be retired.
2363         mlir::Location loc = toLocation();
2364         fir::factory::CharacterExprHelper charHelp{*builder, loc};
2365         mlir::Value box =
2366             charHelp.createEmboxChar(arg.firArgument, arg.firLength);
2367         addSymbol(arg.entity->get(), box);
2368       } else {
2369         if (arg.entity.has_value()) {
2370           addSymbol(arg.entity->get(), arg.firArgument);
2371         } else {
2372           assert(funit.parentHasHostAssoc());
2373           funit.parentHostAssoc().internalProcedureBindings(*this,
2374                                                             localSymbols);
2375         }
2376       }
2377     };
2378     for (const Fortran::lower::CalleeInterface::PassedEntity &arg :
2379          callee.getPassedArguments())
2380       mapPassedEntity(arg);
2381 
2382     // Allocate local skeleton instances of dummies from other entry points.
2383     // Most of these locals will not survive into final generated code, but
2384     // some will.  It is illegal to reference them at run time if they do.
2385     for (const Fortran::semantics::Symbol *arg :
2386          funit.nonUniversalDummyArguments) {
2387       if (lookupSymbol(*arg))
2388         continue;
2389       mlir::Type type = genType(*arg);
2390       // TODO: Account for VALUE arguments (and possibly other variants).
2391       type = builder->getRefType(type);
2392       addSymbol(*arg, builder->create<fir::UndefOp>(toLocation(), type));
2393     }
2394     if (std::optional<Fortran::lower::CalleeInterface::PassedEntity>
2395             passedResult = callee.getPassedResult()) {
2396       mapPassedEntity(*passedResult);
2397       // FIXME: need to make sure things are OK here. addSymbol may not be OK
2398       if (funit.primaryResult &&
2399           passedResult->entity->get() != *funit.primaryResult)
2400         addSymbol(*funit.primaryResult,
2401                   getSymbolAddress(passedResult->entity->get()));
2402     }
2403   }
2404 
2405   /// Instantiate variable \p var and add it to the symbol map.
2406   /// See ConvertVariable.cpp.
2407   void instantiateVar(const Fortran::lower::pft::Variable &var,
2408                       Fortran::lower::AggregateStoreMap &storeMap) {
2409     Fortran::lower::instantiateVariable(*this, var, localSymbols, storeMap);
2410   }
2411 
2412   /// Prepare to translate a new function
2413   void startNewFunction(Fortran::lower::pft::FunctionLikeUnit &funit) {
2414     assert(!builder && "expected nullptr");
2415     Fortran::lower::CalleeInterface callee(funit, *this);
2416     mlir::func::FuncOp func = callee.addEntryBlockAndMapArguments();
2417     builder = new fir::FirOpBuilder(func, bridge.getKindMap());
2418     assert(builder && "FirOpBuilder did not instantiate");
2419     builder->setInsertionPointToStart(&func.front());
2420     func.setVisibility(mlir::SymbolTable::Visibility::Public);
2421 
2422     mapDummiesAndResults(funit, callee);
2423 
2424     // Note: not storing Variable references because getOrderedSymbolTable
2425     // below returns a temporary.
2426     llvm::SmallVector<Fortran::lower::pft::Variable> deferredFuncResultList;
2427 
2428     // Backup actual argument for entry character results
2429     // with different lengths. It needs to be added to the non
2430     // primary results symbol before mapSymbolAttributes is called.
2431     Fortran::lower::SymbolBox resultArg;
2432     if (std::optional<Fortran::lower::CalleeInterface::PassedEntity>
2433             passedResult = callee.getPassedResult())
2434       resultArg = lookupSymbol(passedResult->entity->get());
2435 
2436     Fortran::lower::AggregateStoreMap storeMap;
2437     // The front-end is currently not adding module variables referenced
2438     // in a module procedure as host associated. As a result we need to
2439     // instantiate all module variables here if this is a module procedure.
2440     // It is likely that the front-end behavior should change here.
2441     // This also applies to internal procedures inside module procedures.
2442     if (auto *module = Fortran::lower::pft::getAncestor<
2443             Fortran::lower::pft::ModuleLikeUnit>(funit))
2444       for (const Fortran::lower::pft::Variable &var :
2445            module->getOrderedSymbolTable())
2446         instantiateVar(var, storeMap);
2447 
2448     mlir::Value primaryFuncResultStorage;
2449     for (const Fortran::lower::pft::Variable &var :
2450          funit.getOrderedSymbolTable()) {
2451       // Always instantiate aggregate storage blocks.
2452       if (var.isAggregateStore()) {
2453         instantiateVar(var, storeMap);
2454         continue;
2455       }
2456       const Fortran::semantics::Symbol &sym = var.getSymbol();
2457       if (funit.parentHasHostAssoc()) {
2458         // Never instantitate host associated variables, as they are already
2459         // instantiated from an argument tuple. Instead, just bind the symbol to
2460         // the reference to the host variable, which must be in the map.
2461         const Fortran::semantics::Symbol &ultimate = sym.GetUltimate();
2462         if (funit.parentHostAssoc().isAssociated(ultimate)) {
2463           Fortran::lower::SymbolBox hostBox =
2464               localSymbols.lookupSymbol(ultimate);
2465           assert(hostBox && "host association is not in map");
2466           localSymbols.addSymbol(sym, hostBox.toExtendedValue());
2467           continue;
2468         }
2469       }
2470       if (!sym.IsFuncResult() || !funit.primaryResult) {
2471         instantiateVar(var, storeMap);
2472       } else if (&sym == funit.primaryResult) {
2473         instantiateVar(var, storeMap);
2474         primaryFuncResultStorage = getSymbolAddress(sym);
2475       } else {
2476         deferredFuncResultList.push_back(var);
2477       }
2478     }
2479 
2480     // If this is a host procedure with host associations, then create the tuple
2481     // of pointers for passing to the internal procedures.
2482     if (!funit.getHostAssoc().empty())
2483       funit.getHostAssoc().hostProcedureBindings(*this, localSymbols);
2484 
2485     /// TODO: should use same mechanism as equivalence?
2486     /// One blocking point is character entry returns that need special handling
2487     /// since they are not locally allocated but come as argument. CHARACTER(*)
2488     /// is not something that fit wells with equivalence lowering.
2489     for (const Fortran::lower::pft::Variable &altResult :
2490          deferredFuncResultList) {
2491       if (std::optional<Fortran::lower::CalleeInterface::PassedEntity>
2492               passedResult = callee.getPassedResult())
2493         addSymbol(altResult.getSymbol(), resultArg.getAddr());
2494       Fortran::lower::StatementContext stmtCtx;
2495       Fortran::lower::mapSymbolAttributes(*this, altResult, localSymbols,
2496                                           stmtCtx, primaryFuncResultStorage);
2497     }
2498 
2499     // Create most function blocks in advance.
2500     createEmptyBlocks(funit.evaluationList);
2501 
2502     // Reinstate entry block as the current insertion point.
2503     builder->setInsertionPointToEnd(&func.front());
2504 
2505     if (callee.hasAlternateReturns()) {
2506       // Create a local temp to hold the alternate return index.
2507       // Give it an integer index type and the subroutine name (for dumps).
2508       // Attach it to the subroutine symbol in the localSymbols map.
2509       // Initialize it to zero, the "fallthrough" alternate return value.
2510       const Fortran::semantics::Symbol &symbol = funit.getSubprogramSymbol();
2511       mlir::Location loc = toLocation();
2512       mlir::Type idxTy = builder->getIndexType();
2513       mlir::Value altResult =
2514           builder->createTemporary(loc, idxTy, toStringRef(symbol.name()));
2515       addSymbol(symbol, altResult);
2516       mlir::Value zero = builder->createIntegerConstant(loc, idxTy, 0);
2517       builder->create<fir::StoreOp>(loc, zero, altResult);
2518     }
2519 
2520     if (Fortran::lower::pft::Evaluation *alternateEntryEval =
2521             funit.getEntryEval())
2522       genFIRBranch(alternateEntryEval->lexicalSuccessor->block);
2523   }
2524 
2525   /// Create global blocks for the current function.  This eliminates the
2526   /// distinction between forward and backward targets when generating
2527   /// branches.  A block is "global" if it can be the target of a GOTO or
2528   /// other source code branch.  A block that can only be targeted by a
2529   /// compiler generated branch is "local".  For example, a DO loop preheader
2530   /// block containing loop initialization code is global.  A loop header
2531   /// block, which is the target of the loop back edge, is local.  Blocks
2532   /// belong to a region.  Any block within a nested region must be replaced
2533   /// with a block belonging to that region.  Branches may not cross region
2534   /// boundaries.
2535   void createEmptyBlocks(
2536       std::list<Fortran::lower::pft::Evaluation> &evaluationList) {
2537     mlir::Region *region = &builder->getRegion();
2538     for (Fortran::lower::pft::Evaluation &eval : evaluationList) {
2539       if (eval.isNewBlock)
2540         eval.block = builder->createBlock(region);
2541       if (eval.isConstruct() || eval.isDirective()) {
2542         if (eval.lowerAsUnstructured()) {
2543           createEmptyBlocks(eval.getNestedEvaluations());
2544         } else if (eval.hasNestedEvaluations()) {
2545           // A structured construct that is a target starts a new block.
2546           Fortran::lower::pft::Evaluation &constructStmt =
2547               eval.getFirstNestedEvaluation();
2548           if (constructStmt.isNewBlock)
2549             constructStmt.block = builder->createBlock(region);
2550         }
2551       }
2552     }
2553   }
2554 
2555   /// Return the predicate: "current block does not have a terminator branch".
2556   bool blockIsUnterminated() {
2557     mlir::Block *currentBlock = builder->getBlock();
2558     return currentBlock->empty() ||
2559            !currentBlock->back().hasTrait<mlir::OpTrait::IsTerminator>();
2560   }
2561 
2562   /// Unconditionally switch code insertion to a new block.
2563   void startBlock(mlir::Block *newBlock) {
2564     assert(newBlock && "missing block");
2565     // Default termination for the current block is a fallthrough branch to
2566     // the new block.
2567     if (blockIsUnterminated())
2568       genFIRBranch(newBlock);
2569     // Some blocks may be re/started more than once, and might not be empty.
2570     // If the new block already has (only) a terminator, set the insertion
2571     // point to the start of the block.  Otherwise set it to the end.
2572     builder->setInsertionPointToStart(newBlock);
2573     if (blockIsUnterminated())
2574       builder->setInsertionPointToEnd(newBlock);
2575   }
2576 
2577   /// Conditionally switch code insertion to a new block.
2578   void maybeStartBlock(mlir::Block *newBlock) {
2579     if (newBlock)
2580       startBlock(newBlock);
2581   }
2582 
2583   /// Emit return and cleanup after the function has been translated.
2584   void endNewFunction(Fortran::lower::pft::FunctionLikeUnit &funit) {
2585     setCurrentPosition(Fortran::lower::pft::stmtSourceLoc(funit.endStmt));
2586     if (funit.isMainProgram())
2587       genExitRoutine();
2588     else
2589       genFIRProcedureExit(funit, funit.getSubprogramSymbol());
2590     funit.finalBlock = nullptr;
2591     LLVM_DEBUG(llvm::dbgs() << "*** Lowering result:\n\n"
2592                             << *builder->getFunction() << '\n');
2593     // FIXME: Simplification should happen in a normal pass, not here.
2594     mlir::IRRewriter rewriter(*builder);
2595     (void)mlir::simplifyRegions(rewriter,
2596                                 {builder->getRegion()}); // remove dead code
2597     delete builder;
2598     builder = nullptr;
2599     hostAssocTuple = mlir::Value{};
2600     localSymbols.clear();
2601   }
2602 
2603   /// Helper to generate GlobalOps when the builder is not positioned in any
2604   /// region block. This is required because the FirOpBuilder assumes it is
2605   /// always positioned inside a region block when creating globals, the easiest
2606   /// way comply is to create a dummy function and to throw it afterwards.
2607   void createGlobalOutsideOfFunctionLowering(
2608       const std::function<void()> &createGlobals) {
2609     // FIXME: get rid of the bogus function context and instantiate the
2610     // globals directly into the module.
2611     mlir::MLIRContext *context = &getMLIRContext();
2612     mlir::func::FuncOp func = fir::FirOpBuilder::createFunction(
2613         mlir::UnknownLoc::get(context), getModuleOp(),
2614         fir::NameUniquer::doGenerated("Sham"),
2615         mlir::FunctionType::get(context, llvm::None, llvm::None));
2616     func.addEntryBlock();
2617     builder = new fir::FirOpBuilder(func, bridge.getKindMap());
2618     createGlobals();
2619     if (mlir::Region *region = func.getCallableRegion())
2620       region->dropAllReferences();
2621     func.erase();
2622     delete builder;
2623     builder = nullptr;
2624     localSymbols.clear();
2625   }
2626   /// Instantiate the data from a BLOCK DATA unit.
2627   void lowerBlockData(Fortran::lower::pft::BlockDataUnit &bdunit) {
2628     createGlobalOutsideOfFunctionLowering([&]() {
2629       Fortran::lower::AggregateStoreMap fakeMap;
2630       for (const auto &[_, sym] : bdunit.symTab) {
2631         if (sym->has<Fortran::semantics::ObjectEntityDetails>()) {
2632           Fortran::lower::pft::Variable var(*sym, true);
2633           instantiateVar(var, fakeMap);
2634         }
2635       }
2636     });
2637   }
2638 
2639   /// Create fir::Global for all the common blocks that appear in the program.
2640   void
2641   lowerCommonBlocks(const Fortran::semantics::CommonBlockList &commonBlocks) {
2642     createGlobalOutsideOfFunctionLowering(
2643         [&]() { Fortran::lower::defineCommonBlocks(*this, commonBlocks); });
2644   }
2645 
2646   /// Lower a procedure (nest).
2647   void lowerFunc(Fortran::lower::pft::FunctionLikeUnit &funit) {
2648     if (!funit.isMainProgram()) {
2649       const Fortran::semantics::Symbol &procSymbol =
2650           funit.getSubprogramSymbol();
2651       if (procSymbol.owner().IsSubmodule()) {
2652         TODO(toLocation(), "support submodules");
2653         return;
2654       }
2655     }
2656     setCurrentPosition(funit.getStartingSourceLoc());
2657     for (int entryIndex = 0, last = funit.entryPointList.size();
2658          entryIndex < last; ++entryIndex) {
2659       funit.setActiveEntry(entryIndex);
2660       startNewFunction(funit); // the entry point for lowering this procedure
2661       for (Fortran::lower::pft::Evaluation &eval : funit.evaluationList)
2662         genFIR(eval);
2663       endNewFunction(funit);
2664     }
2665     funit.setActiveEntry(0);
2666     for (Fortran::lower::pft::FunctionLikeUnit &f : funit.nestedFunctions)
2667       lowerFunc(f); // internal procedure
2668   }
2669 
2670   /// Lower module variable definitions to fir::globalOp and OpenMP/OpenACC
2671   /// declarative construct.
2672   void lowerModuleDeclScope(Fortran::lower::pft::ModuleLikeUnit &mod) {
2673     setCurrentPosition(mod.getStartingSourceLoc());
2674     createGlobalOutsideOfFunctionLowering([&]() {
2675       for (const Fortran::lower::pft::Variable &var :
2676            mod.getOrderedSymbolTable()) {
2677         // Only define the variables owned by this module.
2678         const Fortran::semantics::Scope *owningScope = var.getOwningScope();
2679         if (!owningScope || mod.getScope() == *owningScope)
2680           Fortran::lower::defineModuleVariable(*this, var);
2681       }
2682       for (auto &eval : mod.evaluationList)
2683         genFIR(eval);
2684     });
2685   }
2686 
2687   /// Lower functions contained in a module.
2688   void lowerMod(Fortran::lower::pft::ModuleLikeUnit &mod) {
2689     for (Fortran::lower::pft::FunctionLikeUnit &f : mod.nestedFunctions)
2690       lowerFunc(f);
2691   }
2692 
2693   void setCurrentPosition(const Fortran::parser::CharBlock &position) {
2694     if (position != Fortran::parser::CharBlock{})
2695       currentPosition = position;
2696   }
2697 
2698   /// Set current position at the location of \p parseTreeNode. Note that the
2699   /// position is updated automatically when visiting statements, but not when
2700   /// entering higher level nodes like constructs or procedures. This helper is
2701   /// intended to cover the latter cases.
2702   template <typename A>
2703   void setCurrentPositionAt(const A &parseTreeNode) {
2704     setCurrentPosition(Fortran::parser::FindSourceLocation(parseTreeNode));
2705   }
2706 
2707   //===--------------------------------------------------------------------===//
2708   // Utility methods
2709   //===--------------------------------------------------------------------===//
2710 
2711   /// Convert a parser CharBlock to a Location
2712   mlir::Location toLocation(const Fortran::parser::CharBlock &cb) {
2713     return genLocation(cb);
2714   }
2715 
2716   mlir::Location toLocation() { return toLocation(currentPosition); }
2717   void setCurrentEval(Fortran::lower::pft::Evaluation &eval) {
2718     evalPtr = &eval;
2719   }
2720   Fortran::lower::pft::Evaluation &getEval() {
2721     assert(evalPtr);
2722     return *evalPtr;
2723   }
2724 
2725   std::optional<Fortran::evaluate::Shape>
2726   getShape(const Fortran::lower::SomeExpr &expr) {
2727     return Fortran::evaluate::GetShape(foldingContext, expr);
2728   }
2729 
2730   //===--------------------------------------------------------------------===//
2731   // Analysis on a nested explicit iteration space.
2732   //===--------------------------------------------------------------------===//
2733 
2734   void analyzeExplicitSpace(const Fortran::parser::ConcurrentHeader &header) {
2735     explicitIterSpace.pushLevel();
2736     for (const Fortran::parser::ConcurrentControl &ctrl :
2737          std::get<std::list<Fortran::parser::ConcurrentControl>>(header.t)) {
2738       const Fortran::semantics::Symbol *ctrlVar =
2739           std::get<Fortran::parser::Name>(ctrl.t).symbol;
2740       explicitIterSpace.addSymbol(ctrlVar);
2741     }
2742     if (const auto &mask =
2743             std::get<std::optional<Fortran::parser::ScalarLogicalExpr>>(
2744                 header.t);
2745         mask.has_value())
2746       analyzeExplicitSpace(*Fortran::semantics::GetExpr(*mask));
2747   }
2748   template <bool LHS = false, typename A>
2749   void analyzeExplicitSpace(const Fortran::evaluate::Expr<A> &e) {
2750     explicitIterSpace.exprBase(&e, LHS);
2751   }
2752   void analyzeExplicitSpace(const Fortran::evaluate::Assignment *assign) {
2753     auto analyzeAssign = [&](const Fortran::lower::SomeExpr &lhs,
2754                              const Fortran::lower::SomeExpr &rhs) {
2755       analyzeExplicitSpace</*LHS=*/true>(lhs);
2756       analyzeExplicitSpace(rhs);
2757     };
2758     std::visit(
2759         Fortran::common::visitors{
2760             [&](const Fortran::evaluate::ProcedureRef &procRef) {
2761               // Ensure the procRef expressions are the one being visited.
2762               assert(procRef.arguments().size() == 2);
2763               const Fortran::lower::SomeExpr *lhs =
2764                   procRef.arguments()[0].value().UnwrapExpr();
2765               const Fortran::lower::SomeExpr *rhs =
2766                   procRef.arguments()[1].value().UnwrapExpr();
2767               assert(lhs && rhs &&
2768                      "user defined assignment arguments must be expressions");
2769               analyzeAssign(*lhs, *rhs);
2770             },
2771             [&](const auto &) { analyzeAssign(assign->lhs, assign->rhs); }},
2772         assign->u);
2773     explicitIterSpace.endAssign();
2774   }
2775   void analyzeExplicitSpace(const Fortran::parser::ForallAssignmentStmt &stmt) {
2776     std::visit([&](const auto &s) { analyzeExplicitSpace(s); }, stmt.u);
2777   }
2778   void analyzeExplicitSpace(const Fortran::parser::AssignmentStmt &s) {
2779     analyzeExplicitSpace(s.typedAssignment->v.operator->());
2780   }
2781   void analyzeExplicitSpace(const Fortran::parser::PointerAssignmentStmt &s) {
2782     analyzeExplicitSpace(s.typedAssignment->v.operator->());
2783   }
2784   void analyzeExplicitSpace(const Fortran::parser::WhereConstruct &c) {
2785     analyzeExplicitSpace(
2786         std::get<
2787             Fortran::parser::Statement<Fortran::parser::WhereConstructStmt>>(
2788             c.t)
2789             .statement);
2790     for (const Fortran::parser::WhereBodyConstruct &body :
2791          std::get<std::list<Fortran::parser::WhereBodyConstruct>>(c.t))
2792       analyzeExplicitSpace(body);
2793     for (const Fortran::parser::WhereConstruct::MaskedElsewhere &e :
2794          std::get<std::list<Fortran::parser::WhereConstruct::MaskedElsewhere>>(
2795              c.t))
2796       analyzeExplicitSpace(e);
2797     if (const auto &e =
2798             std::get<std::optional<Fortran::parser::WhereConstruct::Elsewhere>>(
2799                 c.t);
2800         e.has_value())
2801       analyzeExplicitSpace(e.operator->());
2802   }
2803   void analyzeExplicitSpace(const Fortran::parser::WhereConstructStmt &ws) {
2804     const Fortran::lower::SomeExpr *exp = Fortran::semantics::GetExpr(
2805         std::get<Fortran::parser::LogicalExpr>(ws.t));
2806     addMaskVariable(exp);
2807     analyzeExplicitSpace(*exp);
2808   }
2809   void analyzeExplicitSpace(
2810       const Fortran::parser::WhereConstruct::MaskedElsewhere &ew) {
2811     analyzeExplicitSpace(
2812         std::get<
2813             Fortran::parser::Statement<Fortran::parser::MaskedElsewhereStmt>>(
2814             ew.t)
2815             .statement);
2816     for (const Fortran::parser::WhereBodyConstruct &e :
2817          std::get<std::list<Fortran::parser::WhereBodyConstruct>>(ew.t))
2818       analyzeExplicitSpace(e);
2819   }
2820   void analyzeExplicitSpace(const Fortran::parser::WhereBodyConstruct &body) {
2821     std::visit(Fortran::common::visitors{
2822                    [&](const Fortran::common::Indirection<
2823                        Fortran::parser::WhereConstruct> &wc) {
2824                      analyzeExplicitSpace(wc.value());
2825                    },
2826                    [&](const auto &s) { analyzeExplicitSpace(s.statement); }},
2827                body.u);
2828   }
2829   void analyzeExplicitSpace(const Fortran::parser::MaskedElsewhereStmt &stmt) {
2830     const Fortran::lower::SomeExpr *exp = Fortran::semantics::GetExpr(
2831         std::get<Fortran::parser::LogicalExpr>(stmt.t));
2832     addMaskVariable(exp);
2833     analyzeExplicitSpace(*exp);
2834   }
2835   void
2836   analyzeExplicitSpace(const Fortran::parser::WhereConstruct::Elsewhere *ew) {
2837     for (const Fortran::parser::WhereBodyConstruct &e :
2838          std::get<std::list<Fortran::parser::WhereBodyConstruct>>(ew->t))
2839       analyzeExplicitSpace(e);
2840   }
2841   void analyzeExplicitSpace(const Fortran::parser::WhereStmt &stmt) {
2842     const Fortran::lower::SomeExpr *exp = Fortran::semantics::GetExpr(
2843         std::get<Fortran::parser::LogicalExpr>(stmt.t));
2844     addMaskVariable(exp);
2845     analyzeExplicitSpace(*exp);
2846     const std::optional<Fortran::evaluate::Assignment> &assign =
2847         std::get<Fortran::parser::AssignmentStmt>(stmt.t).typedAssignment->v;
2848     assert(assign.has_value() && "WHERE has no statement");
2849     analyzeExplicitSpace(assign.operator->());
2850   }
2851   void analyzeExplicitSpace(const Fortran::parser::ForallStmt &forall) {
2852     analyzeExplicitSpace(
2853         std::get<
2854             Fortran::common::Indirection<Fortran::parser::ConcurrentHeader>>(
2855             forall.t)
2856             .value());
2857     analyzeExplicitSpace(std::get<Fortran::parser::UnlabeledStatement<
2858                              Fortran::parser::ForallAssignmentStmt>>(forall.t)
2859                              .statement);
2860     analyzeExplicitSpacePop();
2861   }
2862   void
2863   analyzeExplicitSpace(const Fortran::parser::ForallConstructStmt &forall) {
2864     analyzeExplicitSpace(
2865         std::get<
2866             Fortran::common::Indirection<Fortran::parser::ConcurrentHeader>>(
2867             forall.t)
2868             .value());
2869   }
2870   void analyzeExplicitSpace(const Fortran::parser::ForallConstruct &forall) {
2871     analyzeExplicitSpace(
2872         std::get<
2873             Fortran::parser::Statement<Fortran::parser::ForallConstructStmt>>(
2874             forall.t)
2875             .statement);
2876     for (const Fortran::parser::ForallBodyConstruct &s :
2877          std::get<std::list<Fortran::parser::ForallBodyConstruct>>(forall.t)) {
2878       std::visit(Fortran::common::visitors{
2879                      [&](const Fortran::common::Indirection<
2880                          Fortran::parser::ForallConstruct> &b) {
2881                        analyzeExplicitSpace(b.value());
2882                      },
2883                      [&](const Fortran::parser::WhereConstruct &w) {
2884                        analyzeExplicitSpace(w);
2885                      },
2886                      [&](const auto &b) { analyzeExplicitSpace(b.statement); }},
2887                  s.u);
2888     }
2889     analyzeExplicitSpacePop();
2890   }
2891 
2892   void analyzeExplicitSpacePop() { explicitIterSpace.popLevel(); }
2893 
2894   void addMaskVariable(Fortran::lower::FrontEndExpr exp) {
2895     // Note: use i8 to store bool values. This avoids round-down behavior found
2896     // with sequences of i1. That is, an array of i1 will be truncated in size
2897     // and be too small. For example, a buffer of type fir.array<7xi1> will have
2898     // 0 size.
2899     mlir::Type i64Ty = builder->getIntegerType(64);
2900     mlir::TupleType ty = fir::factory::getRaggedArrayHeaderType(*builder);
2901     mlir::Type buffTy = ty.getType(1);
2902     mlir::Type shTy = ty.getType(2);
2903     mlir::Location loc = toLocation();
2904     mlir::Value hdr = builder->createTemporary(loc, ty);
2905     // FIXME: Is there a way to create a `zeroinitializer` in LLVM-IR dialect?
2906     // For now, explicitly set lazy ragged header to all zeros.
2907     // auto nilTup = builder->createNullConstant(loc, ty);
2908     // builder->create<fir::StoreOp>(loc, nilTup, hdr);
2909     mlir::Type i32Ty = builder->getIntegerType(32);
2910     mlir::Value zero = builder->createIntegerConstant(loc, i32Ty, 0);
2911     mlir::Value zero64 = builder->createIntegerConstant(loc, i64Ty, 0);
2912     mlir::Value flags = builder->create<fir::CoordinateOp>(
2913         loc, builder->getRefType(i64Ty), hdr, zero);
2914     builder->create<fir::StoreOp>(loc, zero64, flags);
2915     mlir::Value one = builder->createIntegerConstant(loc, i32Ty, 1);
2916     mlir::Value nullPtr1 = builder->createNullConstant(loc, buffTy);
2917     mlir::Value var = builder->create<fir::CoordinateOp>(
2918         loc, builder->getRefType(buffTy), hdr, one);
2919     builder->create<fir::StoreOp>(loc, nullPtr1, var);
2920     mlir::Value two = builder->createIntegerConstant(loc, i32Ty, 2);
2921     mlir::Value nullPtr2 = builder->createNullConstant(loc, shTy);
2922     mlir::Value shape = builder->create<fir::CoordinateOp>(
2923         loc, builder->getRefType(shTy), hdr, two);
2924     builder->create<fir::StoreOp>(loc, nullPtr2, shape);
2925     implicitIterSpace.addMaskVariable(exp, var, shape, hdr);
2926     explicitIterSpace.outermostContext().attachCleanup(
2927         [builder = this->builder, hdr, loc]() {
2928           fir::runtime::genRaggedArrayDeallocate(loc, *builder, hdr);
2929         });
2930   }
2931 
2932   void createRuntimeTypeInfoGlobals() {}
2933 
2934   //===--------------------------------------------------------------------===//
2935 
2936   Fortran::lower::LoweringBridge &bridge;
2937   Fortran::evaluate::FoldingContext foldingContext;
2938   fir::FirOpBuilder *builder = nullptr;
2939   Fortran::lower::pft::Evaluation *evalPtr = nullptr;
2940   Fortran::lower::SymMap localSymbols;
2941   Fortran::parser::CharBlock currentPosition;
2942   RuntimeTypeInfoConverter runtimeTypeInfoConverter;
2943 
2944   /// WHERE statement/construct mask expression stack.
2945   Fortran::lower::ImplicitIterSpace implicitIterSpace;
2946 
2947   /// FORALL context
2948   Fortran::lower::ExplicitIterSpace explicitIterSpace;
2949 
2950   /// Tuple of host assoicated variables.
2951   mlir::Value hostAssocTuple;
2952 };
2953 
2954 } // namespace
2955 
2956 Fortran::evaluate::FoldingContext
2957 Fortran::lower::LoweringBridge::createFoldingContext() const {
2958   return {getDefaultKinds(), getIntrinsicTable()};
2959 }
2960 
2961 void Fortran::lower::LoweringBridge::lower(
2962     const Fortran::parser::Program &prg,
2963     const Fortran::semantics::SemanticsContext &semanticsContext) {
2964   std::unique_ptr<Fortran::lower::pft::Program> pft =
2965       Fortran::lower::createPFT(prg, semanticsContext);
2966   if (dumpBeforeFir)
2967     Fortran::lower::dumpPFT(llvm::errs(), *pft);
2968   FirConverter converter{*this};
2969   converter.run(*pft);
2970 }
2971 
2972 void Fortran::lower::LoweringBridge::parseSourceFile(llvm::SourceMgr &srcMgr) {
2973   mlir::OwningOpRef<mlir::ModuleOp> owningRef =
2974       mlir::parseSourceFile<mlir::ModuleOp>(srcMgr, &context);
2975   module.reset(new mlir::ModuleOp(owningRef.get().getOperation()));
2976   owningRef.release();
2977 }
2978 
2979 Fortran::lower::LoweringBridge::LoweringBridge(
2980     mlir::MLIRContext &context,
2981     const Fortran::common::IntrinsicTypeDefaultKinds &defaultKinds,
2982     const Fortran::evaluate::IntrinsicProcTable &intrinsics,
2983     const Fortran::parser::AllCookedSources &cooked, llvm::StringRef triple,
2984     fir::KindMapping &kindMap)
2985     : defaultKinds{defaultKinds}, intrinsics{intrinsics}, cooked{&cooked},
2986       context{context}, kindMap{kindMap} {
2987   // Register the diagnostic handler.
2988   context.getDiagEngine().registerHandler([](mlir::Diagnostic &diag) {
2989     llvm::raw_ostream &os = llvm::errs();
2990     switch (diag.getSeverity()) {
2991     case mlir::DiagnosticSeverity::Error:
2992       os << "error: ";
2993       break;
2994     case mlir::DiagnosticSeverity::Remark:
2995       os << "info: ";
2996       break;
2997     case mlir::DiagnosticSeverity::Warning:
2998       os << "warning: ";
2999       break;
3000     default:
3001       break;
3002     }
3003     if (!diag.getLocation().isa<mlir::UnknownLoc>())
3004       os << diag.getLocation() << ": ";
3005     os << diag << '\n';
3006     os.flush();
3007     return mlir::success();
3008   });
3009 
3010   // Create the module and attach the attributes.
3011   module = std::make_unique<mlir::ModuleOp>(
3012       mlir::ModuleOp::create(mlir::UnknownLoc::get(&context)));
3013   assert(module.get() && "module was not created");
3014   fir::setTargetTriple(*module.get(), triple);
3015   fir::setKindMapping(*module.get(), kindMap);
3016 }
3017