1 //===-- ConvertVariable.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/ConvertVariable.h"
14 #include "flang/Lower/AbstractConverter.h"
15 #include "flang/Lower/Allocatable.h"
16 #include "flang/Lower/BoxAnalyzer.h"
17 #include "flang/Lower/CallInterface.h"
18 #include "flang/Lower/ConvertExpr.h"
19 #include "flang/Lower/IntrinsicCall.h"
20 #include "flang/Lower/Mangler.h"
21 #include "flang/Lower/PFTBuilder.h"
22 #include "flang/Lower/StatementContext.h"
23 #include "flang/Lower/Support/Utils.h"
24 #include "flang/Lower/SymbolMap.h"
25 #include "flang/Lower/Todo.h"
26 #include "flang/Optimizer/Builder/Character.h"
27 #include "flang/Optimizer/Builder/FIRBuilder.h"
28 #include "flang/Optimizer/Builder/Runtime/Derived.h"
29 #include "flang/Optimizer/Dialect/FIRAttr.h"
30 #include "flang/Optimizer/Dialect/FIRDialect.h"
31 #include "flang/Optimizer/Dialect/FIROps.h"
32 #include "flang/Optimizer/Support/FIRContext.h"
33 #include "flang/Optimizer/Support/FatalError.h"
34 #include "flang/Semantics/runtime-type-info.h"
35 #include "flang/Semantics/tools.h"
36 #include "llvm/Support/Debug.h"
37 
38 #define DEBUG_TYPE "flang-lower-variable"
39 
40 /// Helper to lower a scalar expression using a specific symbol mapping.
41 static mlir::Value genScalarValue(Fortran::lower::AbstractConverter &converter,
42                                   mlir::Location loc,
43                                   const Fortran::lower::SomeExpr &expr,
44                                   Fortran::lower::SymMap &symMap,
45                                   Fortran::lower::StatementContext &context) {
46   // This does not use the AbstractConverter member function to override the
47   // symbol mapping to be used expression lowering.
48   return fir::getBase(Fortran::lower::createSomeExtendedExpression(
49       loc, converter, expr, symMap, context));
50 }
51 
52 /// Does this variable have a default initialization?
53 static bool hasDefaultInitialization(const Fortran::semantics::Symbol &sym) {
54   if (sym.has<Fortran::semantics::ObjectEntityDetails>() && sym.size())
55     if (!Fortran::semantics::IsAllocatableOrPointer(sym))
56       if (const Fortran::semantics::DeclTypeSpec *declTypeSpec = sym.GetType())
57         if (const Fortran::semantics::DerivedTypeSpec *derivedTypeSpec =
58                 declTypeSpec->AsDerived())
59           return derivedTypeSpec->HasDefaultInitialization();
60   return false;
61 }
62 
63 //===----------------------------------------------------------------===//
64 // Global variables instantiation (not for alias and common)
65 //===----------------------------------------------------------------===//
66 
67 /// Helper to generate expression value inside global initializer.
68 static fir::ExtendedValue
69 genInitializerExprValue(Fortran::lower::AbstractConverter &converter,
70                         mlir::Location loc,
71                         const Fortran::lower::SomeExpr &expr,
72                         Fortran::lower::StatementContext &stmtCtx) {
73   // Data initializer are constant value and should not depend on other symbols
74   // given the front-end fold parameter references. In any case, the "current"
75   // map of the converter should not be used since it holds mapping to
76   // mlir::Value from another mlir region. If these value are used by accident
77   // in the initializer, this will lead to segfaults in mlir code.
78   Fortran::lower::SymMap emptyMap;
79   return Fortran::lower::createSomeInitializerExpression(loc, converter, expr,
80                                                          emptyMap, stmtCtx);
81 }
82 
83 /// Can this symbol constant be placed in read-only memory?
84 static bool isConstant(const Fortran::semantics::Symbol &sym) {
85   return sym.attrs().test(Fortran::semantics::Attr::PARAMETER) ||
86          sym.test(Fortran::semantics::Symbol::Flag::ReadOnly);
87 }
88 
89 /// Is this a compiler generated symbol to describe derived types ?
90 static bool isRuntimeTypeInfoData(const Fortran::semantics::Symbol &sym) {
91   // So far, use flags to detect if this symbol were generated during
92   // semantics::BuildRuntimeDerivedTypeTables(). Scope cannot be used since the
93   // symbols are injected in the user scopes defining the described derived
94   // types. A robustness improvement for this test could be to get hands on the
95   // semantics::RuntimeDerivedTypeTables and to check if the symbol names
96   // belongs to this structure.
97   return sym.test(Fortran::semantics::Symbol::Flag::CompilerCreated) &&
98          sym.test(Fortran::semantics::Symbol::Flag::ReadOnly);
99 }
100 
101 static fir::GlobalOp defineGlobal(Fortran::lower::AbstractConverter &converter,
102                                   const Fortran::lower::pft::Variable &var,
103                                   llvm::StringRef globalName,
104                                   mlir::StringAttr linkage);
105 
106 /// Create the global op declaration without any initializer
107 static fir::GlobalOp declareGlobal(Fortran::lower::AbstractConverter &converter,
108                                    const Fortran::lower::pft::Variable &var,
109                                    llvm::StringRef globalName,
110                                    mlir::StringAttr linkage) {
111   fir::FirOpBuilder &builder = converter.getFirOpBuilder();
112   if (fir::GlobalOp global = builder.getNamedGlobal(globalName))
113     return global;
114   // Always define linkonce data since it may be optimized out from the module
115   // that actually owns the variable if it does not refers to it.
116   if (linkage == builder.createLinkOnceODRLinkage() ||
117       linkage == builder.createLinkOnceLinkage())
118     return defineGlobal(converter, var, globalName, linkage);
119   const Fortran::semantics::Symbol &sym = var.getSymbol();
120   mlir::Location loc = converter.genLocation(sym.name());
121   // Resolve potential host and module association before checking that this
122   // symbol is an object of a function pointer.
123   const Fortran::semantics::Symbol &ultimate = sym.GetUltimate();
124   if (!ultimate.has<Fortran::semantics::ObjectEntityDetails>() &&
125       !ultimate.has<Fortran::semantics::ProcEntityDetails>())
126     mlir::emitError(loc, "lowering global declaration: symbol '")
127         << toStringRef(sym.name()) << "' has unexpected details\n";
128   return builder.createGlobal(loc, converter.genType(var), globalName, linkage,
129                               mlir::Attribute{}, isConstant(ultimate));
130 }
131 
132 /// Temporary helper to catch todos in initial data target lowering.
133 static bool
134 hasDerivedTypeWithLengthParameters(const Fortran::semantics::Symbol &sym) {
135   if (const Fortran::semantics::DeclTypeSpec *declTy = sym.GetType())
136     if (const Fortran::semantics::DerivedTypeSpec *derived =
137             declTy->AsDerived())
138       return Fortran::semantics::CountLenParameters(*derived) > 0;
139   return false;
140 }
141 
142 static mlir::Type unwrapElementType(mlir::Type type) {
143   if (mlir::Type ty = fir::dyn_cast_ptrOrBoxEleTy(type))
144     type = ty;
145   if (auto seqType = type.dyn_cast<fir::SequenceType>())
146     type = seqType.getEleTy();
147   return type;
148 }
149 
150 fir::ExtendedValue Fortran::lower::genExtAddrInInitializer(
151     Fortran::lower::AbstractConverter &converter, mlir::Location loc,
152     const Fortran::lower::SomeExpr &addr) {
153   Fortran::lower::SymMap globalOpSymMap;
154   Fortran::lower::AggregateStoreMap storeMap;
155   Fortran::lower::StatementContext stmtCtx;
156   if (const Fortran::semantics::Symbol *sym =
157           Fortran::evaluate::GetFirstSymbol(addr)) {
158     // Length parameters processing will need care in global initializer
159     // context.
160     if (hasDerivedTypeWithLengthParameters(*sym))
161       TODO(loc, "initial-data-target with derived type length parameters");
162 
163     auto var = Fortran::lower::pft::Variable(*sym, /*global=*/true);
164     Fortran::lower::instantiateVariable(converter, var, globalOpSymMap,
165                                         storeMap);
166   }
167   return Fortran::lower::createInitializerAddress(loc, converter, addr,
168                                                   globalOpSymMap, stmtCtx);
169 }
170 
171 /// create initial-data-target fir.box in a global initializer region.
172 mlir::Value Fortran::lower::genInitialDataTarget(
173     Fortran::lower::AbstractConverter &converter, mlir::Location loc,
174     mlir::Type boxType, const Fortran::lower::SomeExpr &initialTarget) {
175   Fortran::lower::SymMap globalOpSymMap;
176   Fortran::lower::AggregateStoreMap storeMap;
177   Fortran::lower::StatementContext stmtCtx;
178   fir::FirOpBuilder &builder = converter.getFirOpBuilder();
179   if (Fortran::evaluate::UnwrapExpr<Fortran::evaluate::NullPointer>(
180           initialTarget))
181     return fir::factory::createUnallocatedBox(builder, loc, boxType,
182                                               /*nonDeferredParams=*/llvm::None);
183   // Pointer initial data target, and NULL(mold).
184   if (const Fortran::semantics::Symbol *sym =
185           Fortran::evaluate::GetFirstSymbol(initialTarget)) {
186     // Length parameters processing will need care in global initializer
187     // context.
188     if (hasDerivedTypeWithLengthParameters(*sym))
189       TODO(loc, "initial-data-target with derived type length parameters");
190 
191     auto var = Fortran::lower::pft::Variable(*sym, /*global=*/true);
192     Fortran::lower::instantiateVariable(converter, var, globalOpSymMap,
193                                         storeMap);
194   }
195   mlir::Value box;
196   if (initialTarget.Rank() > 0) {
197     box = fir::getBase(Fortran::lower::createSomeArrayBox(
198         converter, initialTarget, globalOpSymMap, stmtCtx));
199   } else {
200     fir::ExtendedValue addr = Fortran::lower::createInitializerAddress(
201         loc, converter, initialTarget, globalOpSymMap, stmtCtx);
202     box = builder.createBox(loc, addr);
203   }
204   // box is a fir.box<T>, not a fir.box<fir.ptr<T>> as it should to be used
205   // for pointers. A fir.convert should not be used here, because it would
206   // not actually set the pointer attribute in the descriptor.
207   // In a normal context, fir.rebox would be used to set the pointer attribute
208   // while copying the projection from another fir.box. But fir.rebox cannot be
209   // used in initializer because its current codegen expects that the input
210   // fir.box is in memory, which is not the case in initializers.
211   // So, just replace the fir.embox that created addr with one with
212   // fir.box<fir.ptr<T>> result type.
213   // Note that the descriptor cannot have been created with fir.rebox because
214   // the initial-data-target cannot be a fir.box itself (it cannot be
215   // assumed-shape, deferred-shape, or polymorphic as per C765). However the
216   // case where the initial data target is a derived type with length parameters
217   // will most likely be a bit trickier, hence the TODO above.
218 
219   mlir::Operation *op = box.getDefiningOp();
220   if (!op || !mlir::isa<fir::EmboxOp>(*op))
221     fir::emitFatalError(
222         loc, "fir.box must be created with embox in global initializers");
223   mlir::Type targetEleTy = unwrapElementType(box.getType());
224   if (!fir::isa_char(targetEleTy))
225     return builder.create<fir::EmboxOp>(loc, boxType, op->getOperands(),
226                                         op->getAttrs());
227 
228   // Handle the character case length particularities: embox takes a length
229   // value argument when the result type has unknown length, but not when the
230   // result type has constant length. The type of the initial target must be
231   // constant length, but the one of the pointer may not be. In this case, a
232   // length operand must be added.
233   auto targetLen = targetEleTy.cast<fir::CharacterType>().getLen();
234   auto ptrLen = unwrapElementType(boxType).cast<fir::CharacterType>().getLen();
235   if (ptrLen == targetLen)
236     // Nothing to do
237     return builder.create<fir::EmboxOp>(loc, boxType, op->getOperands(),
238                                         op->getAttrs());
239   auto embox = mlir::cast<fir::EmboxOp>(*op);
240   auto ptrType = boxType.cast<fir::BoxType>().getEleTy();
241   mlir::Value memref = builder.createConvert(loc, ptrType, embox.getMemref());
242   if (targetLen == fir::CharacterType::unknownLen())
243     // Drop the length argument.
244     return builder.create<fir::EmboxOp>(loc, boxType, memref, embox.getShape(),
245                                         embox.getSlice());
246   // targetLen is constant and ptrLen is unknown. Add a length argument.
247   mlir::Value targetLenValue =
248       builder.createIntegerConstant(loc, builder.getIndexType(), targetLen);
249   return builder.create<fir::EmboxOp>(loc, boxType, memref, embox.getShape(),
250                                       embox.getSlice(),
251                                       mlir::ValueRange{targetLenValue});
252 }
253 
254 static mlir::Value genDefaultInitializerValue(
255     Fortran::lower::AbstractConverter &converter, mlir::Location loc,
256     const Fortran::semantics::Symbol &sym, mlir::Type symTy,
257     Fortran::lower::StatementContext &stmtCtx) {
258   fir::FirOpBuilder &builder = converter.getFirOpBuilder();
259   mlir::Type scalarType = symTy;
260   fir::SequenceType sequenceType;
261   if (auto ty = symTy.dyn_cast<fir::SequenceType>()) {
262     sequenceType = ty;
263     scalarType = ty.getEleTy();
264   }
265   // Build a scalar default value of the symbol type, looping through the
266   // components to build each component initial value.
267   auto recTy = scalarType.cast<fir::RecordType>();
268   auto fieldTy = fir::FieldType::get(scalarType.getContext());
269   mlir::Value initialValue = builder.create<fir::UndefOp>(loc, scalarType);
270   const Fortran::semantics::DeclTypeSpec *declTy = sym.GetType();
271   assert(declTy && "var with default initialization must have a type");
272   Fortran::semantics::OrderedComponentIterator components(
273       declTy->derivedTypeSpec());
274   for (const auto &component : components) {
275     // Skip parent components, the sub-components of parent types are part of
276     // components and will be looped through right after.
277     if (component.test(Fortran::semantics::Symbol::Flag::ParentComp))
278       continue;
279     mlir::Value componentValue;
280     llvm::StringRef name = toStringRef(component.name());
281     mlir::Type componentTy = recTy.getType(name);
282     assert(componentTy && "component not found in type");
283     if (const auto *object{
284             component.detailsIf<Fortran::semantics::ObjectEntityDetails>()}) {
285       if (const auto &init = object->init()) {
286         // Component has explicit initialization.
287         if (Fortran::semantics::IsPointer(component))
288           // Initial data target.
289           componentValue =
290               genInitialDataTarget(converter, loc, componentTy, *init);
291         else
292           // Initial value.
293           componentValue = fir::getBase(
294               genInitializerExprValue(converter, loc, *init, stmtCtx));
295       } else if (Fortran::semantics::IsAllocatableOrPointer(component)) {
296         // Pointer or allocatable without initialization.
297         // Create deallocated/disassociated value.
298         // From a standard point of view, pointer without initialization do not
299         // need to be disassociated, but for sanity and simplicity, do it in
300         // global constructor since this has no runtime cost.
301         componentValue = fir::factory::createUnallocatedBox(
302             builder, loc, componentTy, llvm::None);
303       } else if (hasDefaultInitialization(component)) {
304         // Component type has default initialization.
305         componentValue = genDefaultInitializerValue(converter, loc, component,
306                                                     componentTy, stmtCtx);
307       } else {
308         // Component has no initial value.
309         componentValue = builder.create<fir::UndefOp>(loc, componentTy);
310       }
311     } else if (const auto *proc{
312                    component
313                        .detailsIf<Fortran::semantics::ProcEntityDetails>()}) {
314       if (proc->init().has_value())
315         TODO(loc, "procedure pointer component default initialization");
316       else
317         componentValue = builder.create<fir::UndefOp>(loc, componentTy);
318     }
319     assert(componentValue && "must have been computed");
320     componentValue = builder.createConvert(loc, componentTy, componentValue);
321     // FIXME: type parameters must come from the derived-type-spec
322     auto field = builder.create<fir::FieldIndexOp>(
323         loc, fieldTy, name, scalarType,
324         /*typeParams=*/mlir::ValueRange{} /*TODO*/);
325     initialValue = builder.create<fir::InsertValueOp>(
326         loc, recTy, initialValue, componentValue,
327         builder.getArrayAttr(field.getAttributes()));
328   }
329 
330   if (sequenceType) {
331     // For arrays, duplicate the scalar value to all elements with an
332     // fir.insert_range covering the whole array.
333     auto arrayInitialValue = builder.create<fir::UndefOp>(loc, sequenceType);
334     llvm::SmallVector<int64_t> rangeBounds;
335     for (int64_t extent : sequenceType.getShape()) {
336       if (extent == fir::SequenceType::getUnknownExtent())
337         TODO(loc,
338              "default initial value of array component with length parameters");
339       rangeBounds.push_back(0);
340       rangeBounds.push_back(extent - 1);
341     }
342     return builder.create<fir::InsertOnRangeOp>(
343         loc, sequenceType, arrayInitialValue, initialValue,
344         builder.getIndexVectorAttr(rangeBounds));
345   }
346   return initialValue;
347 }
348 
349 /// Does this global already have an initializer ?
350 static bool globalIsInitialized(fir::GlobalOp global) {
351   return !global.getRegion().empty() || global.getInitVal();
352 }
353 
354 /// Call \p genInit to generate code inside \p global initializer region.
355 static void
356 createGlobalInitialization(fir::FirOpBuilder &builder, fir::GlobalOp global,
357                            std::function<void(fir::FirOpBuilder &)> genInit) {
358   mlir::Region &region = global.getRegion();
359   region.push_back(new mlir::Block);
360   mlir::Block &block = region.back();
361   auto insertPt = builder.saveInsertionPoint();
362   builder.setInsertionPointToStart(&block);
363   genInit(builder);
364   builder.restoreInsertionPoint(insertPt);
365 }
366 
367 /// Create the global op and its init if it has one
368 static fir::GlobalOp defineGlobal(Fortran::lower::AbstractConverter &converter,
369                                   const Fortran::lower::pft::Variable &var,
370                                   llvm::StringRef globalName,
371                                   mlir::StringAttr linkage) {
372   fir::FirOpBuilder &builder = converter.getFirOpBuilder();
373   const Fortran::semantics::Symbol &sym = var.getSymbol();
374   mlir::Location loc = converter.genLocation(sym.name());
375   bool isConst = isConstant(sym);
376   fir::GlobalOp global = builder.getNamedGlobal(globalName);
377   mlir::Type symTy = converter.genType(var);
378 
379   if (global && globalIsInitialized(global))
380     return global;
381   // If this is an array, check to see if we can use a dense attribute
382   // with a tensor mlir type.  This optimization currently only supports
383   // rank-1 Fortran arrays of integer, real, or logical. The tensor
384   // type does not support nested structures which are needed for
385   // complex numbers.
386   // To get multidimensional arrays to work, we will have to use column major
387   // array ordering with the tensor type (so it matches column major ordering
388   // with the Fortran fir.array).  By default, tensor types assume row major
389   // ordering. How to create this tensor type is to be determined.
390   if (symTy.isa<fir::SequenceType>() && sym.Rank() == 1 &&
391       !Fortran::semantics::IsAllocatableOrPointer(sym)) {
392     mlir::Type eleTy = symTy.cast<fir::SequenceType>().getEleTy();
393     if (eleTy.isa<mlir::IntegerType, mlir::FloatType, fir::LogicalType>()) {
394       const auto *details =
395           sym.detailsIf<Fortran::semantics::ObjectEntityDetails>();
396       if (details->init()) {
397         global = Fortran::lower::createDenseGlobal(
398             loc, symTy, globalName, linkage, isConst, details->init().value(),
399             converter);
400         if (global) {
401           global.setVisibility(mlir::SymbolTable::Visibility::Public);
402           return global;
403         }
404       }
405     }
406   }
407   if (!global)
408     global = builder.createGlobal(loc, symTy, globalName, linkage,
409                                   mlir::Attribute{}, isConst);
410   if (Fortran::semantics::IsAllocatableOrPointer(sym)) {
411     const auto *details =
412         sym.detailsIf<Fortran::semantics::ObjectEntityDetails>();
413     if (details && details->init()) {
414       auto expr = *details->init();
415       createGlobalInitialization(builder, global, [&](fir::FirOpBuilder &b) {
416         mlir::Value box =
417             Fortran::lower::genInitialDataTarget(converter, loc, symTy, expr);
418         b.create<fir::HasValueOp>(loc, box);
419       });
420     } else {
421       // Create unallocated/disassociated descriptor if no explicit init
422       createGlobalInitialization(builder, global, [&](fir::FirOpBuilder &b) {
423         mlir::Value box =
424             fir::factory::createUnallocatedBox(b, loc, symTy, llvm::None);
425         b.create<fir::HasValueOp>(loc, box);
426       });
427     }
428 
429   } else if (const auto *details =
430                  sym.detailsIf<Fortran::semantics::ObjectEntityDetails>()) {
431     if (details->init()) {
432       createGlobalInitialization(
433           builder, global, [&](fir::FirOpBuilder &builder) {
434             Fortran::lower::StatementContext stmtCtx(
435                 /*cleanupProhibited=*/true);
436             fir::ExtendedValue initVal = genInitializerExprValue(
437                 converter, loc, details->init().value(), stmtCtx);
438             mlir::Value castTo =
439                 builder.createConvert(loc, symTy, fir::getBase(initVal));
440             builder.create<fir::HasValueOp>(loc, castTo);
441           });
442     } else if (hasDefaultInitialization(sym)) {
443       createGlobalInitialization(
444           builder, global, [&](fir::FirOpBuilder &builder) {
445             Fortran::lower::StatementContext stmtCtx(
446                 /*cleanupProhibited=*/true);
447             mlir::Value initVal =
448                 genDefaultInitializerValue(converter, loc, sym, symTy, stmtCtx);
449             mlir::Value castTo = builder.createConvert(loc, symTy, initVal);
450             builder.create<fir::HasValueOp>(loc, castTo);
451           });
452     }
453   } else if (sym.has<Fortran::semantics::CommonBlockDetails>()) {
454     mlir::emitError(loc, "COMMON symbol processed elsewhere");
455   } else {
456     TODO(loc, "global"); // Procedure pointer or something else
457   }
458   // Creates undefined initializer for globals without initializers
459   if (!globalIsInitialized(global))
460     createGlobalInitialization(
461         builder, global, [&](fir::FirOpBuilder &builder) {
462           builder.create<fir::HasValueOp>(
463               loc, builder.create<fir::UndefOp>(loc, symTy));
464         });
465   // Set public visibility to prevent global definition to be optimized out
466   // even if they have no initializer and are unused in this compilation unit.
467   global.setVisibility(mlir::SymbolTable::Visibility::Public);
468   return global;
469 }
470 
471 /// Return linkage attribute for \p var.
472 static mlir::StringAttr
473 getLinkageAttribute(fir::FirOpBuilder &builder,
474                     const Fortran::lower::pft::Variable &var) {
475   // Runtime type info for a same derived type is identical in each compilation
476   // unit. It desired to avoid having to link against module that only define a
477   // type. Therefore the runtime type info is generated everywhere it is needed
478   // with `linkonce_odr` LLVM linkage.
479   if (var.hasSymbol() && isRuntimeTypeInfoData(var.getSymbol()))
480     return builder.createLinkOnceODRLinkage();
481   if (var.isModuleVariable())
482     return {}; // external linkage
483   // Otherwise, the variable is owned by a procedure and must not be visible in
484   // other compilation units.
485   return builder.createInternalLinkage();
486 }
487 
488 /// Instantiate a global variable. If it hasn't already been processed, add
489 /// the global to the ModuleOp as a new uniqued symbol and initialize it with
490 /// the correct value. It will be referenced on demand using `fir.addr_of`.
491 static void instantiateGlobal(Fortran::lower::AbstractConverter &converter,
492                               const Fortran::lower::pft::Variable &var,
493                               Fortran::lower::SymMap &symMap) {
494   const Fortran::semantics::Symbol &sym = var.getSymbol();
495   assert(!var.isAlias() && "must be handled in instantiateAlias");
496   fir::FirOpBuilder &builder = converter.getFirOpBuilder();
497   std::string globalName = Fortran::lower::mangle::mangleName(sym);
498   mlir::Location loc = converter.genLocation(sym.name());
499   fir::GlobalOp global = builder.getNamedGlobal(globalName);
500   mlir::StringAttr linkage = getLinkageAttribute(builder, var);
501   if (var.isModuleVariable()) {
502     // A module global was or will be defined when lowering the module. Emit
503     // only a declaration if the global does not exist at that point.
504     global = declareGlobal(converter, var, globalName, linkage);
505   } else {
506     global = defineGlobal(converter, var, globalName, linkage);
507   }
508   auto addrOf = builder.create<fir::AddrOfOp>(loc, global.resultType(),
509                                               global.getSymbol());
510   Fortran::lower::StatementContext stmtCtx;
511   mapSymbolAttributes(converter, var, symMap, stmtCtx, addrOf);
512 }
513 
514 //===----------------------------------------------------------------===//
515 // Local variables instantiation (not for alias)
516 //===----------------------------------------------------------------===//
517 
518 /// Create a stack slot for a local variable. Precondition: the insertion
519 /// point of the builder must be in the entry block, which is currently being
520 /// constructed.
521 static mlir::Value createNewLocal(Fortran::lower::AbstractConverter &converter,
522                                   mlir::Location loc,
523                                   const Fortran::lower::pft::Variable &var,
524                                   mlir::Value preAlloc,
525                                   llvm::ArrayRef<mlir::Value> shape = {},
526                                   llvm::ArrayRef<mlir::Value> lenParams = {}) {
527   if (preAlloc)
528     return preAlloc;
529   fir::FirOpBuilder &builder = converter.getFirOpBuilder();
530   std::string nm = Fortran::lower::mangle::mangleName(var.getSymbol());
531   mlir::Type ty = converter.genType(var);
532   const Fortran::semantics::Symbol &ultimateSymbol =
533       var.getSymbol().GetUltimate();
534   llvm::StringRef symNm = toStringRef(ultimateSymbol.name());
535   bool isTarg = var.isTarget();
536   // Let the builder do all the heavy lifting.
537   return builder.allocateLocal(loc, ty, nm, symNm, shape, lenParams, isTarg);
538 }
539 
540 /// Must \p var be default initialized at runtime when entering its scope.
541 static bool
542 mustBeDefaultInitializedAtRuntime(const Fortran::lower::pft::Variable &var) {
543   if (!var.hasSymbol())
544     return false;
545   const Fortran::semantics::Symbol &sym = var.getSymbol();
546   if (var.isGlobal())
547     // Global variables are statically initialized.
548     return false;
549   if (Fortran::semantics::IsDummy(sym) && !Fortran::semantics::IsIntentOut(sym))
550     return false;
551   // Local variables (including function results), and intent(out) dummies must
552   // be default initialized at runtime if their type has default initialization.
553   return hasDefaultInitialization(sym);
554 }
555 
556 /// Call default initialization runtime routine to initialize \p var.
557 static void
558 defaultInitializeAtRuntime(Fortran::lower::AbstractConverter &converter,
559                            const Fortran::lower::pft::Variable &var,
560                            Fortran::lower::SymMap &symMap) {
561   fir::FirOpBuilder &builder = converter.getFirOpBuilder();
562   mlir::Location loc = converter.getCurrentLocation();
563   const Fortran::semantics::Symbol &sym = var.getSymbol();
564   fir::ExtendedValue exv = symMap.lookupSymbol(sym).toExtendedValue();
565   if (Fortran::semantics::IsOptional(sym)) {
566     // 15.5.2.12 point 3, absent optional dummies are not initialized.
567     // Creating descriptor/passing null descriptor to the runtime would
568     // create runtime crashes.
569     auto isPresent = builder.create<fir::IsPresentOp>(loc, builder.getI1Type(),
570                                                       fir::getBase(exv));
571     builder.genIfThen(loc, isPresent)
572         .genThen([&]() {
573           auto box = builder.createBox(loc, exv);
574           fir::runtime::genDerivedTypeInitialize(builder, loc, box);
575         })
576         .end();
577   } else {
578     mlir::Value box = builder.createBox(loc, exv);
579     fir::runtime::genDerivedTypeInitialize(builder, loc, box);
580   }
581 }
582 
583 /// Instantiate a local variable. Precondition: Each variable will be visited
584 /// such that if its properties depend on other variables, the variables upon
585 /// which its properties depend will already have been visited.
586 static void instantiateLocal(Fortran::lower::AbstractConverter &converter,
587                              const Fortran::lower::pft::Variable &var,
588                              Fortran::lower::SymMap &symMap) {
589   assert(!var.isAlias());
590   Fortran::lower::StatementContext stmtCtx;
591   mapSymbolAttributes(converter, var, symMap, stmtCtx);
592   if (mustBeDefaultInitializedAtRuntime(var))
593     defaultInitializeAtRuntime(converter, var, symMap);
594 }
595 
596 //===----------------------------------------------------------------===//
597 // Aliased (EQUIVALENCE) variables instantiation
598 //===----------------------------------------------------------------===//
599 
600 /// Insert \p aggregateStore instance into an AggregateStoreMap.
601 static void insertAggregateStore(Fortran::lower::AggregateStoreMap &storeMap,
602                                  const Fortran::lower::pft::Variable &var,
603                                  mlir::Value aggregateStore) {
604   std::size_t off = var.getAggregateStore().getOffset();
605   Fortran::lower::AggregateStoreKey key = {var.getOwningScope(), off};
606   storeMap[key] = aggregateStore;
607 }
608 
609 /// Retrieve the aggregate store instance of \p alias from an
610 /// AggregateStoreMap.
611 static mlir::Value
612 getAggregateStore(Fortran::lower::AggregateStoreMap &storeMap,
613                   const Fortran::lower::pft::Variable &alias) {
614   Fortran::lower::AggregateStoreKey key = {alias.getOwningScope(),
615                                            alias.getAlias()};
616   auto iter = storeMap.find(key);
617   assert(iter != storeMap.end());
618   return iter->second;
619 }
620 
621 /// Build the name for the storage of a global equivalence.
622 static std::string mangleGlobalAggregateStore(
623     const Fortran::lower::pft::Variable::AggregateStore &st) {
624   return Fortran::lower::mangle::mangleName(st.getNamingSymbol());
625 }
626 
627 /// Build the type for the storage of an equivalence.
628 static mlir::Type
629 getAggregateType(Fortran::lower::AbstractConverter &converter,
630                  const Fortran::lower::pft::Variable::AggregateStore &st) {
631   if (const Fortran::semantics::Symbol *initSym = st.getInitialValueSymbol())
632     return converter.genType(*initSym);
633   mlir::IntegerType byteTy = converter.getFirOpBuilder().getIntegerType(8);
634   return fir::SequenceType::get(std::get<1>(st.interval), byteTy);
635 }
636 
637 /// Define a GlobalOp for the storage of a global equivalence described
638 /// by \p aggregate. The global is named \p aggName and is created with
639 /// the provided \p linkage.
640 /// If any of the equivalence members are initialized, an initializer is
641 /// created for the equivalence.
642 /// This is to be used when lowering the scope that owns the equivalence
643 /// (as opposed to simply using it through host or use association).
644 /// This is not to be used for equivalence of common block members (they
645 /// already have the common block GlobalOp for them, see defineCommonBlock).
646 static fir::GlobalOp defineGlobalAggregateStore(
647     Fortran::lower::AbstractConverter &converter,
648     const Fortran::lower::pft::Variable::AggregateStore &aggregate,
649     llvm::StringRef aggName, mlir::StringAttr linkage) {
650   assert(aggregate.isGlobal() && "not a global interval");
651   fir::FirOpBuilder &builder = converter.getFirOpBuilder();
652   fir::GlobalOp global = builder.getNamedGlobal(aggName);
653   if (global && globalIsInitialized(global))
654     return global;
655   mlir::Location loc = converter.getCurrentLocation();
656   mlir::Type aggTy = getAggregateType(converter, aggregate);
657   if (!global)
658     global = builder.createGlobal(loc, aggTy, aggName, linkage);
659 
660   if (const Fortran::semantics::Symbol *initSym =
661           aggregate.getInitialValueSymbol())
662     if (const auto *objectDetails =
663             initSym->detailsIf<Fortran::semantics::ObjectEntityDetails>())
664       if (objectDetails->init()) {
665         createGlobalInitialization(
666             builder, global, [&](fir::FirOpBuilder &builder) {
667               Fortran::lower::StatementContext stmtCtx;
668               mlir::Value initVal = fir::getBase(genInitializerExprValue(
669                   converter, loc, objectDetails->init().value(), stmtCtx));
670               builder.create<fir::HasValueOp>(loc, initVal);
671             });
672         return global;
673       }
674   // Equivalence has no Fortran initial value. Create an undefined FIR initial
675   // value to ensure this is consider an object definition in the IR regardless
676   // of the linkage.
677   createGlobalInitialization(builder, global, [&](fir::FirOpBuilder &builder) {
678     Fortran::lower::StatementContext stmtCtx;
679     mlir::Value initVal = builder.create<fir::UndefOp>(loc, aggTy);
680     builder.create<fir::HasValueOp>(loc, initVal);
681   });
682   return global;
683 }
684 
685 /// Declare a GlobalOp for the storage of a global equivalence described
686 /// by \p aggregate. The global is named \p aggName and is created with
687 /// the provided \p linkage.
688 /// No initializer is built for the created GlobalOp.
689 /// This is to be used when lowering the scope that uses members of an
690 /// equivalence it through host or use association.
691 /// This is not to be used for equivalence of common block members (they
692 /// already have the common block GlobalOp for them, see defineCommonBlock).
693 static fir::GlobalOp declareGlobalAggregateStore(
694     Fortran::lower::AbstractConverter &converter, mlir::Location loc,
695     const Fortran::lower::pft::Variable::AggregateStore &aggregate,
696     llvm::StringRef aggName, mlir::StringAttr linkage) {
697   assert(aggregate.isGlobal() && "not a global interval");
698   fir::FirOpBuilder &builder = converter.getFirOpBuilder();
699   if (fir::GlobalOp global = builder.getNamedGlobal(aggName))
700     return global;
701   mlir::Type aggTy = getAggregateType(converter, aggregate);
702   return builder.createGlobal(loc, aggTy, aggName, linkage);
703 }
704 
705 /// This is an aggregate store for a set of EQUIVALENCED variables. Create the
706 /// storage on the stack or global memory and add it to the map.
707 static void
708 instantiateAggregateStore(Fortran::lower::AbstractConverter &converter,
709                           const Fortran::lower::pft::Variable &var,
710                           Fortran::lower::AggregateStoreMap &storeMap) {
711   assert(var.isAggregateStore() && "not an interval");
712   fir::FirOpBuilder &builder = converter.getFirOpBuilder();
713   mlir::IntegerType i8Ty = builder.getIntegerType(8);
714   mlir::Location loc = converter.getCurrentLocation();
715   std::string aggName = mangleGlobalAggregateStore(var.getAggregateStore());
716   if (var.isGlobal()) {
717     fir::GlobalOp global;
718     auto &aggregate = var.getAggregateStore();
719     mlir::StringAttr linkage = getLinkageAttribute(builder, var);
720     if (var.isModuleVariable()) {
721       // A module global was or will be defined when lowering the module. Emit
722       // only a declaration if the global does not exist at that point.
723       global = declareGlobalAggregateStore(converter, loc, aggregate, aggName,
724                                            linkage);
725     } else {
726       global =
727           defineGlobalAggregateStore(converter, aggregate, aggName, linkage);
728     }
729     auto addr = builder.create<fir::AddrOfOp>(loc, global.resultType(),
730                                               global.getSymbol());
731     auto size = std::get<1>(var.getInterval());
732     fir::SequenceType::Shape shape(1, size);
733     auto seqTy = fir::SequenceType::get(shape, i8Ty);
734     mlir::Type refTy = builder.getRefType(seqTy);
735     mlir::Value aggregateStore = builder.createConvert(loc, refTy, addr);
736     insertAggregateStore(storeMap, var, aggregateStore);
737     return;
738   }
739   // This is a local aggregate, allocate an anonymous block of memory.
740   auto size = std::get<1>(var.getInterval());
741   fir::SequenceType::Shape shape(1, size);
742   auto seqTy = fir::SequenceType::get(shape, i8Ty);
743   mlir::Value local =
744       builder.allocateLocal(loc, seqTy, aggName, "", llvm::None, llvm::None,
745                             /*target=*/false);
746   insertAggregateStore(storeMap, var, local);
747 }
748 
749 /// Cast an alias address (variable part of an equivalence) to fir.ptr so that
750 /// the optimizer is conservative and avoids doing copy elision in assignment
751 /// involving equivalenced variables.
752 /// TODO: Represent the equivalence aliasing constraint in another way to avoid
753 /// pessimizing array assignments involving equivalenced variables.
754 static mlir::Value castAliasToPointer(fir::FirOpBuilder &builder,
755                                       mlir::Location loc, mlir::Type aliasType,
756                                       mlir::Value aliasAddr) {
757   return builder.createConvert(loc, fir::PointerType::get(aliasType),
758                                aliasAddr);
759 }
760 
761 /// Instantiate a member of an equivalence. Compute its address in its
762 /// aggregate storage and lower its attributes.
763 static void instantiateAlias(Fortran::lower::AbstractConverter &converter,
764                              const Fortran::lower::pft::Variable &var,
765                              Fortran::lower::SymMap &symMap,
766                              Fortran::lower::AggregateStoreMap &storeMap) {
767   fir::FirOpBuilder &builder = converter.getFirOpBuilder();
768   assert(var.isAlias());
769   const Fortran::semantics::Symbol &sym = var.getSymbol();
770   const mlir::Location loc = converter.genLocation(sym.name());
771   mlir::IndexType idxTy = builder.getIndexType();
772   std::size_t aliasOffset = var.getAlias();
773   mlir::Value store = getAggregateStore(storeMap, var);
774   mlir::IntegerType i8Ty = builder.getIntegerType(8);
775   mlir::Type i8Ptr = builder.getRefType(i8Ty);
776   mlir::Value offset = builder.createIntegerConstant(
777       loc, idxTy, sym.GetUltimate().offset() - aliasOffset);
778   auto ptr = builder.create<fir::CoordinateOp>(loc, i8Ptr, store,
779                                                mlir::ValueRange{offset});
780   mlir::Value preAlloc =
781       castAliasToPointer(builder, loc, converter.genType(sym), ptr);
782   Fortran::lower::StatementContext stmtCtx;
783   mapSymbolAttributes(converter, var, symMap, stmtCtx, preAlloc);
784   // Default initialization is possible for equivalence members: see
785   // F2018 19.5.3.4. Note that if several equivalenced entities have
786   // default initialization, they must have the same type, and the standard
787   // allows the storage to be default initialized several times (this has
788   // no consequences other than wasting some execution time). For now,
789   // do not try optimizing this to single default initializations of
790   // the equivalenced storages. Keep lowering simple.
791   if (mustBeDefaultInitializedAtRuntime(var))
792     defaultInitializeAtRuntime(converter, var, symMap);
793 }
794 
795 //===--------------------------------------------------------------===//
796 // COMMON blocks instantiation
797 //===--------------------------------------------------------------===//
798 
799 /// Does any member of the common block has an initializer ?
800 static bool
801 commonBlockHasInit(const Fortran::semantics::MutableSymbolVector &cmnBlkMems) {
802   for (const Fortran::semantics::MutableSymbolRef &mem : cmnBlkMems) {
803     if (const auto *memDet =
804             mem->detailsIf<Fortran::semantics::ObjectEntityDetails>())
805       if (memDet->init())
806         return true;
807   }
808   return false;
809 }
810 
811 /// Build a tuple type for a common block based on the common block
812 /// members and the common block size.
813 /// This type is only needed to build common block initializers where
814 /// the initial value is the collection of the member initial values.
815 static mlir::TupleType getTypeOfCommonWithInit(
816     Fortran::lower::AbstractConverter &converter,
817     const Fortran::semantics::MutableSymbolVector &cmnBlkMems,
818     std::size_t commonSize) {
819   fir::FirOpBuilder &builder = converter.getFirOpBuilder();
820   llvm::SmallVector<mlir::Type> members;
821   std::size_t counter = 0;
822   for (const Fortran::semantics::MutableSymbolRef &mem : cmnBlkMems) {
823     if (const auto *memDet =
824             mem->detailsIf<Fortran::semantics::ObjectEntityDetails>()) {
825       if (mem->offset() > counter) {
826         fir::SequenceType::Shape len = {
827             static_cast<fir::SequenceType::Extent>(mem->offset() - counter)};
828         mlir::IntegerType byteTy = builder.getIntegerType(8);
829         auto memTy = fir::SequenceType::get(len, byteTy);
830         members.push_back(memTy);
831         counter = mem->offset();
832       }
833       if (memDet->init()) {
834         mlir::Type memTy = converter.genType(*mem);
835         members.push_back(memTy);
836         counter = mem->offset() + mem->size();
837       }
838     }
839   }
840   if (counter < commonSize) {
841     fir::SequenceType::Shape len = {
842         static_cast<fir::SequenceType::Extent>(commonSize - counter)};
843     mlir::IntegerType byteTy = builder.getIntegerType(8);
844     auto memTy = fir::SequenceType::get(len, byteTy);
845     members.push_back(memTy);
846   }
847   return mlir::TupleType::get(builder.getContext(), members);
848 }
849 
850 /// Common block members may have aliases. They are not in the common block
851 /// member list from the symbol. We need to know about these aliases if they
852 /// have initializer to generate the common initializer.
853 /// This function takes care of adding aliases with initializer to the member
854 /// list.
855 static Fortran::semantics::MutableSymbolVector
856 getCommonMembersWithInitAliases(const Fortran::semantics::Symbol &common) {
857   const auto &commonDetails =
858       common.get<Fortran::semantics::CommonBlockDetails>();
859   auto members = commonDetails.objects();
860 
861   // The number and size of equivalence and common is expected to be small, so
862   // no effort is given to optimize this loop of complexity equivalenced
863   // common members * common members
864   for (const Fortran::semantics::EquivalenceSet &set :
865        common.owner().equivalenceSets())
866     for (const Fortran::semantics::EquivalenceObject &obj : set) {
867       if (!obj.symbol.test(Fortran::semantics::Symbol::Flag::CompilerCreated)) {
868         if (const auto &details =
869                 obj.symbol
870                     .detailsIf<Fortran::semantics::ObjectEntityDetails>()) {
871           const Fortran::semantics::Symbol *com =
872               FindCommonBlockContaining(obj.symbol);
873           if (!details->init() || com != &common)
874             continue;
875           // This is an alias with an init that belongs to the list
876           if (std::find(members.begin(), members.end(), obj.symbol) ==
877               members.end())
878             members.emplace_back(obj.symbol);
879         }
880       }
881     }
882   return members;
883 }
884 
885 /// Return the fir::GlobalOp that was created of COMMON block \p common.
886 /// It is an error if the fir::GlobalOp was not created before this is
887 /// called (it cannot be created on the flight because it is not known here
888 /// what mlir type the GlobalOp should have to satisfy all the
889 /// appearances in the program).
890 static fir::GlobalOp
891 getCommonBlockGlobal(Fortran::lower::AbstractConverter &converter,
892                      const Fortran::semantics::Symbol &common) {
893   fir::FirOpBuilder &builder = converter.getFirOpBuilder();
894   std::string commonName = Fortran::lower::mangle::mangleName(common);
895   fir::GlobalOp global = builder.getNamedGlobal(commonName);
896   // Common blocks are lowered before any subprograms to deal with common
897   // whose size may not be the same in every subprograms.
898   if (!global)
899     fir::emitFatalError(converter.genLocation(common.name()),
900                         "COMMON block was not lowered before its usage");
901   return global;
902 }
903 
904 /// Create the fir::GlobalOp for COMMON block \p common. If \p common has an
905 /// initial value, it is not created yet. Instead, the common block list
906 /// members is returned to later create the initial value in
907 /// finalizeCommonBlockDefinition.
908 static std::optional<std::tuple<
909     fir::GlobalOp, Fortran::semantics::MutableSymbolVector, mlir::Location>>
910 declareCommonBlock(Fortran::lower::AbstractConverter &converter,
911                    const Fortran::semantics::Symbol &common,
912                    std::size_t commonSize) {
913   fir::FirOpBuilder &builder = converter.getFirOpBuilder();
914   std::string commonName = Fortran::lower::mangle::mangleName(common);
915   fir::GlobalOp global = builder.getNamedGlobal(commonName);
916   if (global)
917     return std::nullopt;
918   Fortran::semantics::MutableSymbolVector cmnBlkMems =
919       getCommonMembersWithInitAliases(common);
920   mlir::Location loc = converter.genLocation(common.name());
921   mlir::StringAttr linkage = builder.createCommonLinkage();
922   if (!commonBlockHasInit(cmnBlkMems)) {
923     // A COMMON block sans initializers is initialized to zero.
924     // mlir::Vector types must have a strictly positive size, so at least
925     // temporarily, force a zero size COMMON block to have one byte.
926     const auto sz =
927         static_cast<fir::SequenceType::Extent>(commonSize > 0 ? commonSize : 1);
928     fir::SequenceType::Shape shape = {sz};
929     mlir::IntegerType i8Ty = builder.getIntegerType(8);
930     auto commonTy = fir::SequenceType::get(shape, i8Ty);
931     auto vecTy = mlir::VectorType::get(sz, i8Ty);
932     mlir::Attribute zero = builder.getIntegerAttr(i8Ty, 0);
933     auto init = mlir::DenseElementsAttr::get(vecTy, llvm::makeArrayRef(zero));
934     builder.createGlobal(loc, commonTy, commonName, linkage, init);
935     // No need to add any initial value later.
936     return std::nullopt;
937   }
938   // COMMON block with initializer (note that initialized blank common are
939   // accepted as an extension by semantics). Sort members by offset before
940   // generating the type and initializer.
941   std::sort(cmnBlkMems.begin(), cmnBlkMems.end(),
942             [](auto &s1, auto &s2) { return s1->offset() < s2->offset(); });
943   mlir::TupleType commonTy =
944       getTypeOfCommonWithInit(converter, cmnBlkMems, commonSize);
945   // Create the global object, the initial value will be added later.
946   global = builder.createGlobal(loc, commonTy, commonName);
947   return std::make_tuple(global, std::move(cmnBlkMems), loc);
948 }
949 
950 /// Add initial value to a COMMON block fir::GlobalOp \p global given the list
951 /// \p cmnBlkMems of the common block member symbols that contains symbols with
952 /// an initial value.
953 static void finalizeCommonBlockDefinition(
954     mlir::Location loc, Fortran::lower::AbstractConverter &converter,
955     fir::GlobalOp global,
956     const Fortran::semantics::MutableSymbolVector &cmnBlkMems) {
957   fir::FirOpBuilder &builder = converter.getFirOpBuilder();
958   mlir::TupleType commonTy = global.getType().cast<mlir::TupleType>();
959   auto initFunc = [&](fir::FirOpBuilder &builder) {
960     mlir::IndexType idxTy = builder.getIndexType();
961     mlir::Value cb = builder.create<fir::UndefOp>(loc, commonTy);
962     unsigned tupIdx = 0;
963     std::size_t offset = 0;
964     LLVM_DEBUG(llvm::dbgs() << "block {\n");
965     for (const Fortran::semantics::MutableSymbolRef &mem : cmnBlkMems) {
966       if (const auto *memDet =
967               mem->detailsIf<Fortran::semantics::ObjectEntityDetails>()) {
968         if (mem->offset() > offset) {
969           ++tupIdx;
970           offset = mem->offset();
971         }
972         if (memDet->init()) {
973           LLVM_DEBUG(llvm::dbgs()
974                      << "offset: " << mem->offset() << " is " << *mem << '\n');
975           Fortran::lower::StatementContext stmtCtx;
976           auto initExpr = memDet->init().value();
977           fir::ExtendedValue initVal =
978               Fortran::semantics::IsPointer(*mem)
979                   ? Fortran::lower::genInitialDataTarget(
980                         converter, loc, converter.genType(*mem), initExpr)
981                   : genInitializerExprValue(converter, loc, initExpr, stmtCtx);
982           mlir::IntegerAttr offVal = builder.getIntegerAttr(idxTy, tupIdx);
983           mlir::Value castVal = builder.createConvert(
984               loc, commonTy.getType(tupIdx), fir::getBase(initVal));
985           cb = builder.create<fir::InsertValueOp>(loc, commonTy, cb, castVal,
986                                                   builder.getArrayAttr(offVal));
987           ++tupIdx;
988           offset = mem->offset() + mem->size();
989         }
990       }
991     }
992     LLVM_DEBUG(llvm::dbgs() << "}\n");
993     builder.create<fir::HasValueOp>(loc, cb);
994   };
995   createGlobalInitialization(builder, global, initFunc);
996 }
997 
998 void Fortran::lower::defineCommonBlocks(
999     Fortran::lower::AbstractConverter &converter,
1000     const Fortran::semantics::CommonBlockList &commonBlocks) {
1001   // Common blocks may depend on another common block address (if they contain
1002   // pointers with initial targets). To cover this case, create all common block
1003   // fir::Global before creating the initial values (if any).
1004   std::vector<std::tuple<fir::GlobalOp, Fortran::semantics::MutableSymbolVector,
1005                          mlir::Location>>
1006       delayedInitializations;
1007   for (const auto &[common, size] : commonBlocks)
1008     if (auto delayedInit = declareCommonBlock(converter, common, size))
1009       delayedInitializations.emplace_back(std::move(*delayedInit));
1010   for (auto &[global, cmnBlkMems, loc] : delayedInitializations)
1011     finalizeCommonBlockDefinition(loc, converter, global, cmnBlkMems);
1012 }
1013 
1014 /// The COMMON block is a global structure. `var` will be at some offset
1015 /// within the COMMON block. Adds the address of `var` (COMMON + offset) to
1016 /// the symbol map.
1017 static void instantiateCommon(Fortran::lower::AbstractConverter &converter,
1018                               const Fortran::semantics::Symbol &common,
1019                               const Fortran::lower::pft::Variable &var,
1020                               Fortran::lower::SymMap &symMap) {
1021   fir::FirOpBuilder &builder = converter.getFirOpBuilder();
1022   const Fortran::semantics::Symbol &varSym = var.getSymbol();
1023   mlir::Location loc = converter.genLocation(varSym.name());
1024 
1025   mlir::Value commonAddr;
1026   if (Fortran::lower::SymbolBox symBox = symMap.lookupSymbol(common))
1027     commonAddr = symBox.getAddr();
1028   if (!commonAddr) {
1029     // introduce a local AddrOf and add it to the map
1030     fir::GlobalOp global = getCommonBlockGlobal(converter, common);
1031     commonAddr = builder.create<fir::AddrOfOp>(loc, global.resultType(),
1032                                                global.getSymbol());
1033 
1034     symMap.addSymbol(common, commonAddr);
1035   }
1036   std::size_t byteOffset = varSym.GetUltimate().offset();
1037   mlir::IntegerType i8Ty = builder.getIntegerType(8);
1038   mlir::Type i8Ptr = builder.getRefType(i8Ty);
1039   mlir::Type seqTy = builder.getRefType(builder.getVarLenSeqTy(i8Ty));
1040   mlir::Value base = builder.createConvert(loc, seqTy, commonAddr);
1041   mlir::Value offs =
1042       builder.createIntegerConstant(loc, builder.getIndexType(), byteOffset);
1043   auto varAddr = builder.create<fir::CoordinateOp>(loc, i8Ptr, base,
1044                                                    mlir::ValueRange{offs});
1045   mlir::Type symType = converter.genType(var.getSymbol());
1046   mlir::Value local;
1047   if (Fortran::semantics::FindEquivalenceSet(var.getSymbol()) != nullptr)
1048     local = castAliasToPointer(builder, loc, symType, varAddr);
1049   else
1050     local = builder.createConvert(loc, builder.getRefType(symType), varAddr);
1051   Fortran::lower::StatementContext stmtCtx;
1052   mapSymbolAttributes(converter, var, symMap, stmtCtx, local);
1053 }
1054 
1055 //===--------------------------------------------------------------===//
1056 // Lower Variables specification expressions and attributes
1057 //===--------------------------------------------------------------===//
1058 
1059 /// Helper to decide if a dummy argument must be tracked in an BoxValue.
1060 static bool lowerToBoxValue(const Fortran::semantics::Symbol &sym,
1061                             mlir::Value dummyArg) {
1062   // Only dummy arguments coming as fir.box can be tracked in an BoxValue.
1063   if (!dummyArg || !dummyArg.getType().isa<fir::BoxType>())
1064     return false;
1065   // Non contiguous arrays must be tracked in an BoxValue.
1066   if (sym.Rank() > 0 && !sym.attrs().test(Fortran::semantics::Attr::CONTIGUOUS))
1067     return true;
1068   // Assumed rank and optional fir.box cannot yet be read while lowering the
1069   // specifications.
1070   if (Fortran::evaluate::IsAssumedRank(sym) ||
1071       Fortran::semantics::IsOptional(sym))
1072     return true;
1073   // Polymorphic entity should be tracked through a fir.box that has the
1074   // dynamic type info.
1075   if (const Fortran::semantics::DeclTypeSpec *type = sym.GetType())
1076     if (type->IsPolymorphic())
1077       return true;
1078   return false;
1079 }
1080 
1081 /// Compute extent from lower and upper bound.
1082 static mlir::Value computeExtent(fir::FirOpBuilder &builder, mlir::Location loc,
1083                                  mlir::Value lb, mlir::Value ub) {
1084   mlir::IndexType idxTy = builder.getIndexType();
1085   // Let the folder deal with the common `ub - <const> + 1` case.
1086   auto diff = builder.create<mlir::arith::SubIOp>(loc, idxTy, ub, lb);
1087   mlir::Value one = builder.createIntegerConstant(loc, idxTy, 1);
1088   auto rawExtent = builder.create<mlir::arith::AddIOp>(loc, idxTy, diff, one);
1089   return Fortran::lower::genMaxWithZero(builder, loc, rawExtent);
1090 }
1091 
1092 /// Lower explicit lower bounds into \p result. Does nothing if this is not an
1093 /// array, or if the lower bounds are deferred, or all implicit or one.
1094 static void lowerExplicitLowerBounds(
1095     Fortran::lower::AbstractConverter &converter, mlir::Location loc,
1096     const Fortran::lower::BoxAnalyzer &box,
1097     llvm::SmallVectorImpl<mlir::Value> &result, Fortran::lower::SymMap &symMap,
1098     Fortran::lower::StatementContext &stmtCtx) {
1099   if (!box.isArray() || box.lboundIsAllOnes())
1100     return;
1101   fir::FirOpBuilder &builder = converter.getFirOpBuilder();
1102   mlir::IndexType idxTy = builder.getIndexType();
1103   if (box.isStaticArray()) {
1104     for (int64_t lb : box.staticLBound())
1105       result.emplace_back(builder.createIntegerConstant(loc, idxTy, lb));
1106     return;
1107   }
1108   for (const Fortran::semantics::ShapeSpec *spec : box.dynamicBound()) {
1109     if (auto low = spec->lbound().GetExplicit()) {
1110       auto expr = Fortran::lower::SomeExpr{*low};
1111       mlir::Value lb = builder.createConvert(
1112           loc, idxTy, genScalarValue(converter, loc, expr, symMap, stmtCtx));
1113       result.emplace_back(lb);
1114     }
1115   }
1116   assert(result.empty() || result.size() == box.dynamicBound().size());
1117 }
1118 
1119 /// Lower explicit extents into \p result if this is an explicit-shape or
1120 /// assumed-size array. Does nothing if this is not an explicit-shape or
1121 /// assumed-size array.
1122 static void
1123 lowerExplicitExtents(Fortran::lower::AbstractConverter &converter,
1124                      mlir::Location loc, const Fortran::lower::BoxAnalyzer &box,
1125                      llvm::SmallVectorImpl<mlir::Value> &lowerBounds,
1126                      llvm::SmallVectorImpl<mlir::Value> &result,
1127                      Fortran::lower::SymMap &symMap,
1128                      Fortran::lower::StatementContext &stmtCtx) {
1129   if (!box.isArray())
1130     return;
1131   fir::FirOpBuilder &builder = converter.getFirOpBuilder();
1132   mlir::IndexType idxTy = builder.getIndexType();
1133   if (box.isStaticArray()) {
1134     for (int64_t extent : box.staticShape())
1135       result.emplace_back(builder.createIntegerConstant(loc, idxTy, extent));
1136     return;
1137   }
1138   for (const auto &spec : llvm::enumerate(box.dynamicBound())) {
1139     if (auto up = spec.value()->ubound().GetExplicit()) {
1140       auto expr = Fortran::lower::SomeExpr{*up};
1141       mlir::Value ub = builder.createConvert(
1142           loc, idxTy, genScalarValue(converter, loc, expr, symMap, stmtCtx));
1143       if (lowerBounds.empty())
1144         result.emplace_back(Fortran::lower::genMaxWithZero(builder, loc, ub));
1145       else
1146         result.emplace_back(
1147             computeExtent(builder, loc, lowerBounds[spec.index()], ub));
1148     } else if (spec.value()->ubound().isStar()) {
1149       // Assumed extent is undefined. Must be provided by user's code.
1150       result.emplace_back(builder.create<fir::UndefOp>(loc, idxTy));
1151     }
1152   }
1153   assert(result.empty() || result.size() == box.dynamicBound().size());
1154 }
1155 
1156 /// Lower explicit character length if any. Return empty mlir::Value if no
1157 /// explicit length.
1158 static mlir::Value
1159 lowerExplicitCharLen(Fortran::lower::AbstractConverter &converter,
1160                      mlir::Location loc, const Fortran::lower::BoxAnalyzer &box,
1161                      Fortran::lower::SymMap &symMap,
1162                      Fortran::lower::StatementContext &stmtCtx) {
1163   if (!box.isChar())
1164     return mlir::Value{};
1165   fir::FirOpBuilder &builder = converter.getFirOpBuilder();
1166   mlir::Type lenTy = builder.getCharacterLengthType();
1167   if (llvm::Optional<int64_t> len = box.getCharLenConst())
1168     return builder.createIntegerConstant(loc, lenTy, *len);
1169   if (llvm::Optional<Fortran::lower::SomeExpr> lenExpr = box.getCharLenExpr())
1170     // If the length expression is negative, the length is zero. See F2018
1171     // 7.4.4.2 point 5.
1172     return Fortran::lower::genMaxWithZero(
1173         builder, loc,
1174         genScalarValue(converter, loc, *lenExpr, symMap, stmtCtx));
1175   return mlir::Value{};
1176 }
1177 
1178 /// Treat negative values as undefined. Assumed size arrays will return -1 from
1179 /// the front end for example. Using negative values can produce hard to find
1180 /// bugs much further along in the compilation.
1181 static mlir::Value genExtentValue(fir::FirOpBuilder &builder,
1182                                   mlir::Location loc, mlir::Type idxTy,
1183                                   long frontEndExtent) {
1184   if (frontEndExtent >= 0)
1185     return builder.createIntegerConstant(loc, idxTy, frontEndExtent);
1186   return builder.create<fir::UndefOp>(loc, idxTy);
1187 }
1188 
1189 /// Lower specification expressions and attributes of variable \p var and
1190 /// add it to the symbol map.
1191 /// For global and aliases, the address must be pre-computed and provided
1192 /// in \p preAlloc.
1193 /// Dummy arguments must have already been mapped to mlir block arguments
1194 /// their mapping may be updated here.
1195 void Fortran::lower::mapSymbolAttributes(
1196     AbstractConverter &converter, const Fortran::lower::pft::Variable &var,
1197     Fortran::lower::SymMap &symMap, Fortran::lower::StatementContext &stmtCtx,
1198     mlir::Value preAlloc) {
1199   fir::FirOpBuilder &builder = converter.getFirOpBuilder();
1200   const Fortran::semantics::Symbol &sym = var.getSymbol();
1201   const mlir::Location loc = converter.genLocation(sym.name());
1202   mlir::IndexType idxTy = builder.getIndexType();
1203   const bool isDummy = Fortran::semantics::IsDummy(sym);
1204   const bool isResult = Fortran::semantics::IsFunctionResult(sym);
1205   const bool replace = isDummy || isResult;
1206   fir::factory::CharacterExprHelper charHelp{builder, loc};
1207   Fortran::lower::BoxAnalyzer ba;
1208   ba.analyze(sym);
1209 
1210   // First deal with pointers an allocatables, because their handling here
1211   // is the same regardless of their rank.
1212   if (Fortran::semantics::IsAllocatableOrPointer(sym)) {
1213     // Get address of fir.box describing the entity.
1214     // global
1215     mlir::Value boxAlloc = preAlloc;
1216     // dummy or passed result
1217     if (!boxAlloc)
1218       if (Fortran::lower::SymbolBox symbox = symMap.lookupSymbol(sym))
1219         boxAlloc = symbox.getAddr();
1220     // local
1221     if (!boxAlloc)
1222       boxAlloc = createNewLocal(converter, loc, var, preAlloc);
1223     // Lower non deferred parameters.
1224     llvm::SmallVector<mlir::Value> nonDeferredLenParams;
1225     if (ba.isChar()) {
1226       if (mlir::Value len =
1227               lowerExplicitCharLen(converter, loc, ba, symMap, stmtCtx))
1228         nonDeferredLenParams.push_back(len);
1229       else if (Fortran::semantics::IsAssumedLengthCharacter(sym))
1230         TODO(loc, "assumed length character allocatable");
1231     } else if (const Fortran::semantics::DeclTypeSpec *declTy = sym.GetType()) {
1232       if (const Fortran::semantics::DerivedTypeSpec *derived =
1233               declTy->AsDerived())
1234         if (Fortran::semantics::CountLenParameters(*derived) != 0)
1235           TODO(loc,
1236                "derived type allocatable or pointer with length parameters");
1237     }
1238     fir::MutableBoxValue box = Fortran::lower::createMutableBox(
1239         converter, loc, var, boxAlloc, nonDeferredLenParams);
1240     symMap.addAllocatableOrPointer(var.getSymbol(), box, replace);
1241     return;
1242   }
1243 
1244   if (isDummy) {
1245     mlir::Value dummyArg = symMap.lookupSymbol(sym).getAddr();
1246     if (lowerToBoxValue(sym, dummyArg)) {
1247       llvm::SmallVector<mlir::Value> lbounds;
1248       llvm::SmallVector<mlir::Value> explicitExtents;
1249       llvm::SmallVector<mlir::Value> explicitParams;
1250       // Lower lower bounds, explicit type parameters and explicit
1251       // extents if any.
1252       if (ba.isChar())
1253         if (mlir::Value len =
1254                 lowerExplicitCharLen(converter, loc, ba, symMap, stmtCtx))
1255           explicitParams.push_back(len);
1256       // TODO: derived type length parameters.
1257       lowerExplicitLowerBounds(converter, loc, ba, lbounds, symMap, stmtCtx);
1258       lowerExplicitExtents(converter, loc, ba, lbounds, explicitExtents, symMap,
1259                            stmtCtx);
1260       symMap.addBoxSymbol(sym, dummyArg, lbounds, explicitParams,
1261                           explicitExtents, replace);
1262       return;
1263     }
1264   }
1265 
1266   // Helper to generate scalars for the symbol properties.
1267   auto genValue = [&](const Fortran::lower::SomeExpr &expr) {
1268     return genScalarValue(converter, loc, expr, symMap, stmtCtx);
1269   };
1270 
1271   // For symbols reaching this point, all properties are constant and can be
1272   // read/computed already into ssa values.
1273 
1274   // The origin must be \vec{1}.
1275   auto populateShape = [&](auto &shapes, const auto &bounds, mlir::Value box) {
1276     for (auto iter : llvm::enumerate(bounds)) {
1277       auto *spec = iter.value();
1278       assert(spec->lbound().GetExplicit() &&
1279              "lbound must be explicit with constant value 1");
1280       if (auto high = spec->ubound().GetExplicit()) {
1281         Fortran::lower::SomeExpr highEx{*high};
1282         mlir::Value ub = genValue(highEx);
1283         ub = builder.createConvert(loc, idxTy, ub);
1284         shapes.emplace_back(genMaxWithZero(builder, loc, ub));
1285       } else if (spec->ubound().isColon()) {
1286         assert(box && "assumed bounds require a descriptor");
1287         mlir::Value dim =
1288             builder.createIntegerConstant(loc, idxTy, iter.index());
1289         auto dimInfo =
1290             builder.create<fir::BoxDimsOp>(loc, idxTy, idxTy, idxTy, box, dim);
1291         shapes.emplace_back(dimInfo.getResult(1));
1292       } else if (spec->ubound().isStar()) {
1293         shapes.emplace_back(builder.create<fir::UndefOp>(loc, idxTy));
1294       } else {
1295         llvm::report_fatal_error("unknown bound category");
1296       }
1297     }
1298   };
1299 
1300   // The origin is not \vec{1}.
1301   auto populateLBoundsExtents = [&](auto &lbounds, auto &extents,
1302                                     const auto &bounds, mlir::Value box) {
1303     for (auto iter : llvm::enumerate(bounds)) {
1304       auto *spec = iter.value();
1305       fir::BoxDimsOp dimInfo;
1306       mlir::Value ub, lb;
1307       if (spec->lbound().isColon() || spec->ubound().isColon()) {
1308         // This is an assumed shape because allocatables and pointers extents
1309         // are not constant in the scope and are not read here.
1310         assert(box && "deferred bounds require a descriptor");
1311         mlir::Value dim =
1312             builder.createIntegerConstant(loc, idxTy, iter.index());
1313         dimInfo =
1314             builder.create<fir::BoxDimsOp>(loc, idxTy, idxTy, idxTy, box, dim);
1315         extents.emplace_back(dimInfo.getResult(1));
1316         if (auto low = spec->lbound().GetExplicit()) {
1317           auto expr = Fortran::lower::SomeExpr{*low};
1318           mlir::Value lb = builder.createConvert(loc, idxTy, genValue(expr));
1319           lbounds.emplace_back(lb);
1320         } else {
1321           // Implicit lower bound is 1 (Fortran 2018 section 8.5.8.3 point 3.)
1322           lbounds.emplace_back(builder.createIntegerConstant(loc, idxTy, 1));
1323         }
1324       } else {
1325         if (auto low = spec->lbound().GetExplicit()) {
1326           auto expr = Fortran::lower::SomeExpr{*low};
1327           lb = builder.createConvert(loc, idxTy, genValue(expr));
1328         } else {
1329           TODO(loc, "assumed rank lowering");
1330         }
1331         lbounds.emplace_back(lb);
1332 
1333         if (auto high = spec->ubound().GetExplicit()) {
1334           auto expr = Fortran::lower::SomeExpr{*high};
1335           ub = builder.createConvert(loc, idxTy, genValue(expr));
1336           extents.emplace_back(computeExtent(builder, loc, lb, ub));
1337         } else {
1338           // An assumed size array. The extent is not computed.
1339           assert(spec->ubound().isStar() && "expected assumed size");
1340           extents.emplace_back(builder.create<fir::UndefOp>(loc, idxTy));
1341         }
1342       }
1343     }
1344   };
1345 
1346   // Lower length expression for non deferred and non dummy assumed length
1347   // characters.
1348   auto genExplicitCharLen =
1349       [&](llvm::Optional<Fortran::lower::SomeExpr> charLen) -> mlir::Value {
1350     if (!charLen)
1351       fir::emitFatalError(loc, "expected explicit character length");
1352     mlir::Value rawLen = genValue(*charLen);
1353     // If the length expression is negative, the length is zero. See
1354     // F2018 7.4.4.2 point 5.
1355     return genMaxWithZero(builder, loc, rawLen);
1356   };
1357 
1358   ba.match(
1359       //===--------------------------------------------------------------===//
1360       // Trivial case.
1361       //===--------------------------------------------------------------===//
1362       [&](const Fortran::lower::details::ScalarSym &) {
1363         if (isDummy) {
1364           // This is an argument.
1365           if (!symMap.lookupSymbol(sym))
1366             mlir::emitError(loc, "symbol \"")
1367                 << toStringRef(sym.name()) << "\" must already be in map";
1368           return;
1369         } else if (isResult) {
1370           // Some Fortran results may be passed by argument (e.g. derived
1371           // types)
1372           if (symMap.lookupSymbol(sym))
1373             return;
1374         }
1375         // Otherwise, it's a local variable or function result.
1376         mlir::Value local = createNewLocal(converter, loc, var, preAlloc);
1377         symMap.addSymbol(sym, local);
1378       },
1379 
1380       //===--------------------------------------------------------------===//
1381       // The non-trivial cases are when we have an argument or local that has
1382       // a repetition value. Arguments might be passed as simple pointers and
1383       // need to be cast to a multi-dimensional array with constant bounds
1384       // (possibly with a missing column), bounds computed in the callee
1385       // (here), or with bounds from the caller (boxed somewhere else). Locals
1386       // have the same properties except they are never boxed arguments from
1387       // the caller and never having a missing column size.
1388       //===--------------------------------------------------------------===//
1389 
1390       [&](const Fortran::lower::details::ScalarStaticChar &x) {
1391         // type is a CHARACTER, determine the LEN value
1392         auto charLen = x.charLen();
1393         if (replace) {
1394           Fortran::lower::SymbolBox symBox = symMap.lookupSymbol(sym);
1395           std::pair<mlir::Value, mlir::Value> unboxchar =
1396               charHelp.createUnboxChar(symBox.getAddr());
1397           mlir::Value boxAddr = unboxchar.first;
1398           // Set/override LEN with a constant
1399           mlir::Value len = builder.createIntegerConstant(loc, idxTy, charLen);
1400           symMap.addCharSymbol(sym, boxAddr, len, true);
1401           return;
1402         }
1403         mlir::Value len = builder.createIntegerConstant(loc, idxTy, charLen);
1404         if (preAlloc) {
1405           symMap.addCharSymbol(sym, preAlloc, len);
1406           return;
1407         }
1408         mlir::Value local = createNewLocal(converter, loc, var, preAlloc);
1409         symMap.addCharSymbol(sym, local, len);
1410       },
1411 
1412       //===--------------------------------------------------------------===//
1413 
1414       [&](const Fortran::lower::details::ScalarDynamicChar &x) {
1415         // type is a CHARACTER, determine the LEN value
1416         auto charLen = x.charLen();
1417         if (replace) {
1418           Fortran::lower::SymbolBox symBox = symMap.lookupSymbol(sym);
1419           mlir::Value boxAddr = symBox.getAddr();
1420           mlir::Value len;
1421           mlir::Type addrTy = boxAddr.getType();
1422           if (addrTy.isa<fir::BoxCharType>() || addrTy.isa<fir::BoxType>()) {
1423             std::tie(boxAddr, len) = charHelp.createUnboxChar(symBox.getAddr());
1424           } else {
1425             // dummy from an other entry case: we cannot get a dynamic length
1426             // for it, it's illegal for the user program to use it. However,
1427             // since we are lowering all function unit statements regardless
1428             // of whether the execution will reach them or not, we need to
1429             // fill a value for the length here.
1430             len = builder.createIntegerConstant(
1431                 loc, builder.getCharacterLengthType(), 1);
1432           }
1433           // Override LEN with an expression
1434           if (charLen)
1435             len = genExplicitCharLen(charLen);
1436           symMap.addCharSymbol(sym, boxAddr, len, true);
1437           return;
1438         }
1439         // local CHARACTER variable
1440         mlir::Value len = genExplicitCharLen(charLen);
1441         if (preAlloc) {
1442           symMap.addCharSymbol(sym, preAlloc, len);
1443           return;
1444         }
1445         llvm::SmallVector<mlir::Value> lengths = {len};
1446         mlir::Value local =
1447             createNewLocal(converter, loc, var, preAlloc, llvm::None, lengths);
1448         symMap.addCharSymbol(sym, local, len);
1449       },
1450 
1451       //===--------------------------------------------------------------===//
1452 
1453       [&](const Fortran::lower::details::StaticArray &x) {
1454         // object shape is constant, not a character
1455         mlir::Type castTy = builder.getRefType(converter.genType(var));
1456         mlir::Value addr = symMap.lookupSymbol(sym).getAddr();
1457         if (addr)
1458           addr = builder.createConvert(loc, castTy, addr);
1459         if (x.lboundAllOnes()) {
1460           // if lower bounds are all ones, build simple shaped object
1461           llvm::SmallVector<mlir::Value> shape;
1462           for (int64_t i : x.shapes)
1463             shape.push_back(genExtentValue(builder, loc, idxTy, i));
1464           mlir::Value local =
1465               isDummy ? addr : createNewLocal(converter, loc, var, preAlloc);
1466           symMap.addSymbolWithShape(sym, local, shape, isDummy);
1467           return;
1468         }
1469         // If object is an array process the lower bound and extent values by
1470         // constructing constants and populating the lbounds and extents.
1471         llvm::SmallVector<mlir::Value> extents;
1472         llvm::SmallVector<mlir::Value> lbounds;
1473         for (auto [fst, snd] : llvm::zip(x.lbounds, x.shapes)) {
1474           lbounds.emplace_back(builder.createIntegerConstant(loc, idxTy, fst));
1475           extents.emplace_back(genExtentValue(builder, loc, idxTy, snd));
1476         }
1477         mlir::Value local =
1478             isDummy ? addr
1479                     : createNewLocal(converter, loc, var, preAlloc, extents);
1480         assert(isDummy || Fortran::lower::isExplicitShape(sym));
1481         symMap.addSymbolWithBounds(sym, local, extents, lbounds, isDummy);
1482       },
1483 
1484       //===--------------------------------------------------------------===//
1485 
1486       [&](const Fortran::lower::details::DynamicArray &x) {
1487         // cast to the known constant parts from the declaration
1488         mlir::Type varType = converter.genType(var);
1489         mlir::Value addr = symMap.lookupSymbol(sym).getAddr();
1490         mlir::Value argBox;
1491         mlir::Type castTy = builder.getRefType(varType);
1492         if (addr) {
1493           if (auto boxTy = addr.getType().dyn_cast<fir::BoxType>()) {
1494             argBox = addr;
1495             mlir::Type refTy = builder.getRefType(boxTy.getEleTy());
1496             addr = builder.create<fir::BoxAddrOp>(loc, refTy, argBox);
1497           }
1498           addr = builder.createConvert(loc, castTy, addr);
1499         }
1500         if (x.lboundAllOnes()) {
1501           // if lower bounds are all ones, build simple shaped object
1502           llvm::SmallVector<mlir::Value> shapes;
1503           populateShape(shapes, x.bounds, argBox);
1504           if (isDummy) {
1505             symMap.addSymbolWithShape(sym, addr, shapes, true);
1506             return;
1507           }
1508           // local array with computed bounds
1509           assert(Fortran::lower::isExplicitShape(sym) ||
1510                  Fortran::semantics::IsAllocatableOrPointer(sym));
1511           mlir::Value local =
1512               createNewLocal(converter, loc, var, preAlloc, shapes);
1513           symMap.addSymbolWithShape(sym, local, shapes);
1514           return;
1515         }
1516         // if object is an array process the lower bound and extent values
1517         llvm::SmallVector<mlir::Value> extents;
1518         llvm::SmallVector<mlir::Value> lbounds;
1519         populateLBoundsExtents(lbounds, extents, x.bounds, argBox);
1520         if (isDummy) {
1521           symMap.addSymbolWithBounds(sym, addr, extents, lbounds, true);
1522           return;
1523         }
1524         // local array with computed bounds
1525         assert(Fortran::lower::isExplicitShape(sym));
1526         mlir::Value local =
1527             createNewLocal(converter, loc, var, preAlloc, extents);
1528         symMap.addSymbolWithBounds(sym, local, extents, lbounds);
1529       },
1530 
1531       //===--------------------------------------------------------------===//
1532 
1533       [&](const Fortran::lower::details::StaticArrayStaticChar &x) {
1534         // if element type is a CHARACTER, determine the LEN value
1535         auto charLen = x.charLen();
1536         mlir::Value addr;
1537         mlir::Value len;
1538         if (isDummy) {
1539           Fortran::lower::SymbolBox symBox = symMap.lookupSymbol(sym);
1540           std::pair<mlir::Value, mlir::Value> unboxchar =
1541               charHelp.createUnboxChar(symBox.getAddr());
1542           addr = unboxchar.first;
1543           // Set/override LEN with a constant
1544           len = builder.createIntegerConstant(loc, idxTy, charLen);
1545         } else {
1546           // local CHARACTER variable
1547           len = builder.createIntegerConstant(loc, idxTy, charLen);
1548         }
1549 
1550         // object shape is constant
1551         mlir::Type castTy = builder.getRefType(converter.genType(var));
1552         if (addr)
1553           addr = builder.createConvert(loc, castTy, addr);
1554 
1555         if (x.lboundAllOnes()) {
1556           // if lower bounds are all ones, build simple shaped object
1557           llvm::SmallVector<mlir::Value> shape;
1558           for (int64_t i : x.shapes)
1559             shape.push_back(genExtentValue(builder, loc, idxTy, i));
1560           mlir::Value local =
1561               isDummy ? addr : createNewLocal(converter, loc, var, preAlloc);
1562           symMap.addCharSymbolWithShape(sym, local, len, shape, isDummy);
1563           return;
1564         }
1565 
1566         // if object is an array process the lower bound and extent values
1567         llvm::SmallVector<mlir::Value> extents;
1568         llvm::SmallVector<mlir::Value> lbounds;
1569         // construct constants and populate `bounds`
1570         for (auto [fst, snd] : llvm::zip(x.lbounds, x.shapes)) {
1571           lbounds.emplace_back(builder.createIntegerConstant(loc, idxTy, fst));
1572           extents.emplace_back(genExtentValue(builder, loc, idxTy, snd));
1573         }
1574 
1575         if (isDummy) {
1576           symMap.addCharSymbolWithBounds(sym, addr, len, extents, lbounds,
1577                                          true);
1578           return;
1579         }
1580         // local CHARACTER array with computed bounds
1581         assert(Fortran::lower::isExplicitShape(sym));
1582         mlir::Value local =
1583             createNewLocal(converter, loc, var, preAlloc, extents);
1584         symMap.addCharSymbolWithBounds(sym, local, len, extents, lbounds);
1585       },
1586 
1587       //===--------------------------------------------------------------===//
1588 
1589       [&](const Fortran::lower::details::StaticArrayDynamicChar &x) {
1590         mlir::Value addr;
1591         mlir::Value len;
1592         [[maybe_unused]] bool mustBeDummy = false;
1593         auto charLen = x.charLen();
1594         // if element type is a CHARACTER, determine the LEN value
1595         if (isDummy) {
1596           Fortran::lower::SymbolBox symBox = symMap.lookupSymbol(sym);
1597           std::pair<mlir::Value, mlir::Value> unboxchar =
1598               charHelp.createUnboxChar(symBox.getAddr());
1599           addr = unboxchar.first;
1600           if (charLen) {
1601             // Set/override LEN with an expression
1602             len = genExplicitCharLen(charLen);
1603           } else {
1604             // LEN is from the boxchar
1605             len = unboxchar.second;
1606             mustBeDummy = true;
1607           }
1608         } else {
1609           // local CHARACTER variable
1610           len = genExplicitCharLen(charLen);
1611         }
1612         llvm::SmallVector<mlir::Value> lengths = {len};
1613 
1614         // cast to the known constant parts from the declaration
1615         mlir::Type castTy = builder.getRefType(converter.genType(var));
1616         if (addr)
1617           addr = builder.createConvert(loc, castTy, addr);
1618 
1619         if (x.lboundAllOnes()) {
1620           // if lower bounds are all ones, build simple shaped object
1621           llvm::SmallVector<mlir::Value> shape;
1622           for (int64_t i : x.shapes)
1623             shape.push_back(genExtentValue(builder, loc, idxTy, i));
1624           if (isDummy) {
1625             symMap.addCharSymbolWithShape(sym, addr, len, shape, true);
1626             return;
1627           }
1628           // local CHARACTER array with constant size
1629           mlir::Value local = createNewLocal(converter, loc, var, preAlloc,
1630                                              llvm::None, lengths);
1631           symMap.addCharSymbolWithShape(sym, local, len, shape);
1632           return;
1633         }
1634 
1635         // if object is an array process the lower bound and extent values
1636         llvm::SmallVector<mlir::Value> extents;
1637         llvm::SmallVector<mlir::Value> lbounds;
1638 
1639         // construct constants and populate `bounds`
1640         for (auto [fst, snd] : llvm::zip(x.lbounds, x.shapes)) {
1641           lbounds.emplace_back(builder.createIntegerConstant(loc, idxTy, fst));
1642           extents.emplace_back(genExtentValue(builder, loc, idxTy, snd));
1643         }
1644         if (isDummy) {
1645           symMap.addCharSymbolWithBounds(sym, addr, len, extents, lbounds,
1646                                          true);
1647           return;
1648         }
1649         // local CHARACTER array with computed bounds
1650         assert((!mustBeDummy) && (Fortran::lower::isExplicitShape(sym)));
1651         mlir::Value local =
1652             createNewLocal(converter, loc, var, preAlloc, llvm::None, lengths);
1653         symMap.addCharSymbolWithBounds(sym, local, len, extents, lbounds);
1654       },
1655 
1656       //===--------------------------------------------------------------===//
1657 
1658       [&](const Fortran::lower::details::DynamicArrayStaticChar &x) {
1659         mlir::Value addr;
1660         mlir::Value len;
1661         mlir::Value argBox;
1662         auto charLen = x.charLen();
1663         // if element type is a CHARACTER, determine the LEN value
1664         if (isDummy) {
1665           mlir::Value actualArg = symMap.lookupSymbol(sym).getAddr();
1666           if (auto boxTy = actualArg.getType().dyn_cast<fir::BoxType>()) {
1667             argBox = actualArg;
1668             mlir::Type refTy = builder.getRefType(boxTy.getEleTy());
1669             addr = builder.create<fir::BoxAddrOp>(loc, refTy, argBox);
1670           } else {
1671             addr = charHelp.createUnboxChar(actualArg).first;
1672           }
1673           // Set/override LEN with a constant
1674           len = builder.createIntegerConstant(loc, idxTy, charLen);
1675         } else {
1676           // local CHARACTER variable
1677           len = builder.createIntegerConstant(loc, idxTy, charLen);
1678         }
1679 
1680         // cast to the known constant parts from the declaration
1681         mlir::Type castTy = builder.getRefType(converter.genType(var));
1682         if (addr)
1683           addr = builder.createConvert(loc, castTy, addr);
1684         if (x.lboundAllOnes()) {
1685           // if lower bounds are all ones, build simple shaped object
1686           llvm::SmallVector<mlir::Value> shape;
1687           populateShape(shape, x.bounds, argBox);
1688           if (isDummy) {
1689             symMap.addCharSymbolWithShape(sym, addr, len, shape, true);
1690             return;
1691           }
1692           // local CHARACTER array
1693           mlir::Value local =
1694               createNewLocal(converter, loc, var, preAlloc, shape);
1695           symMap.addCharSymbolWithShape(sym, local, len, shape);
1696           return;
1697         }
1698         // if object is an array process the lower bound and extent values
1699         llvm::SmallVector<mlir::Value> extents;
1700         llvm::SmallVector<mlir::Value> lbounds;
1701         populateLBoundsExtents(lbounds, extents, x.bounds, argBox);
1702         if (isDummy) {
1703           symMap.addCharSymbolWithBounds(sym, addr, len, extents, lbounds,
1704                                          true);
1705           return;
1706         }
1707         // local CHARACTER array with computed bounds
1708         assert(Fortran::lower::isExplicitShape(sym));
1709         mlir::Value local =
1710             createNewLocal(converter, loc, var, preAlloc, extents);
1711         symMap.addCharSymbolWithBounds(sym, local, len, extents, lbounds);
1712       },
1713 
1714       //===--------------------------------------------------------------===//
1715 
1716       [&](const Fortran::lower::details::DynamicArrayDynamicChar &x) {
1717         mlir::Value addr;
1718         mlir::Value len;
1719         mlir::Value argBox;
1720         auto charLen = x.charLen();
1721         // if element type is a CHARACTER, determine the LEN value
1722         if (isDummy) {
1723           mlir::Value actualArg = symMap.lookupSymbol(sym).getAddr();
1724           if (auto boxTy = actualArg.getType().dyn_cast<fir::BoxType>()) {
1725             argBox = actualArg;
1726             mlir::Type refTy = builder.getRefType(boxTy.getEleTy());
1727             addr = builder.create<fir::BoxAddrOp>(loc, refTy, argBox);
1728             if (charLen)
1729               // Set/override LEN with an expression.
1730               len = genExplicitCharLen(charLen);
1731             else
1732               // Get the length from the actual arguments.
1733               len = charHelp.readLengthFromBox(argBox);
1734           } else {
1735             std::pair<mlir::Value, mlir::Value> unboxchar =
1736                 charHelp.createUnboxChar(actualArg);
1737             addr = unboxchar.first;
1738             if (charLen) {
1739               // Set/override LEN with an expression
1740               len = genExplicitCharLen(charLen);
1741             } else {
1742               // Get the length from the actual arguments.
1743               len = unboxchar.second;
1744             }
1745           }
1746         } else {
1747           // local CHARACTER variable
1748           len = genExplicitCharLen(charLen);
1749         }
1750         llvm::SmallVector<mlir::Value> lengths = {len};
1751 
1752         // cast to the known constant parts from the declaration
1753         mlir::Type castTy = builder.getRefType(converter.genType(var));
1754         if (addr)
1755           addr = builder.createConvert(loc, castTy, addr);
1756         if (x.lboundAllOnes()) {
1757           // if lower bounds are all ones, build simple shaped object
1758           llvm::SmallVector<mlir::Value> shape;
1759           populateShape(shape, x.bounds, argBox);
1760           if (isDummy) {
1761             symMap.addCharSymbolWithShape(sym, addr, len, shape, true);
1762             return;
1763           }
1764           // local CHARACTER array
1765           mlir::Value local =
1766               createNewLocal(converter, loc, var, preAlloc, shape, lengths);
1767           symMap.addCharSymbolWithShape(sym, local, len, shape);
1768           return;
1769         }
1770         // Process the lower bound and extent values.
1771         llvm::SmallVector<mlir::Value> extents;
1772         llvm::SmallVector<mlir::Value> lbounds;
1773         populateLBoundsExtents(lbounds, extents, x.bounds, argBox);
1774         if (isDummy) {
1775           symMap.addCharSymbolWithBounds(sym, addr, len, extents, lbounds,
1776                                          true);
1777           return;
1778         }
1779         // local CHARACTER array with computed bounds
1780         assert(Fortran::lower::isExplicitShape(sym));
1781         mlir::Value local =
1782             createNewLocal(converter, loc, var, preAlloc, extents, lengths);
1783         symMap.addCharSymbolWithBounds(sym, local, len, extents, lbounds);
1784       },
1785 
1786       //===--------------------------------------------------------------===//
1787 
1788       [&](const Fortran::lower::BoxAnalyzer::None &) {
1789         mlir::emitError(loc, "symbol analysis failed on ")
1790             << toStringRef(sym.name());
1791       });
1792 }
1793 
1794 void Fortran::lower::defineModuleVariable(
1795     AbstractConverter &converter, const Fortran::lower::pft::Variable &var) {
1796   // Use empty linkage for module variables, which makes them available
1797   // for use in another unit.
1798   mlir::StringAttr linkage =
1799       getLinkageAttribute(converter.getFirOpBuilder(), var);
1800   if (!var.isGlobal())
1801     fir::emitFatalError(converter.getCurrentLocation(),
1802                         "attempting to lower module variable as local");
1803   // Define aggregate storages for equivalenced objects.
1804   if (var.isAggregateStore()) {
1805     const Fortran::lower::pft::Variable::AggregateStore &aggregate =
1806         var.getAggregateStore();
1807     std::string aggName = mangleGlobalAggregateStore(aggregate);
1808     defineGlobalAggregateStore(converter, aggregate, aggName, linkage);
1809     return;
1810   }
1811   const Fortran::semantics::Symbol &sym = var.getSymbol();
1812   if (const Fortran::semantics::Symbol *common =
1813           Fortran::semantics::FindCommonBlockContaining(var.getSymbol())) {
1814     // Nothing to do, common block are generated before everything. Ensure
1815     // this was done by calling getCommonBlockGlobal.
1816     getCommonBlockGlobal(converter, *common);
1817   } else if (var.isAlias()) {
1818     // Do nothing. Mapping will be done on user side.
1819   } else {
1820     std::string globalName = Fortran::lower::mangle::mangleName(sym);
1821     defineGlobal(converter, var, globalName, linkage);
1822   }
1823 }
1824 
1825 void Fortran::lower::instantiateVariable(AbstractConverter &converter,
1826                                          const pft::Variable &var,
1827                                          Fortran::lower::SymMap &symMap,
1828                                          AggregateStoreMap &storeMap) {
1829   if (var.isAggregateStore()) {
1830     instantiateAggregateStore(converter, var, storeMap);
1831   } else if (const Fortran::semantics::Symbol *common =
1832                  Fortran::semantics::FindCommonBlockContaining(
1833                      var.getSymbol().GetUltimate())) {
1834     instantiateCommon(converter, *common, var, symMap);
1835   } else if (var.isAlias()) {
1836     instantiateAlias(converter, var, symMap, storeMap);
1837   } else if (var.isGlobal()) {
1838     instantiateGlobal(converter, var, symMap);
1839   } else {
1840     instantiateLocal(converter, var, symMap);
1841   }
1842 }
1843 
1844 void Fortran::lower::mapCallInterfaceSymbols(
1845     AbstractConverter &converter, const Fortran::lower::CallerInterface &caller,
1846     SymMap &symMap) {
1847   Fortran::lower::AggregateStoreMap storeMap;
1848   const Fortran::semantics::Symbol &result = caller.getResultSymbol();
1849   for (Fortran::lower::pft::Variable var :
1850        Fortran::lower::pft::buildFuncResultDependencyList(result)) {
1851     if (var.isAggregateStore()) {
1852       instantiateVariable(converter, var, symMap, storeMap);
1853     } else {
1854       const Fortran::semantics::Symbol &sym = var.getSymbol();
1855       const auto *hostDetails =
1856           sym.detailsIf<Fortran::semantics::HostAssocDetails>();
1857       if (hostDetails && !var.isModuleVariable()) {
1858         // The callee is an internal procedure `A` whose result properties
1859         // depend on host variables. The caller may be the host, or another
1860         // internal procedure `B` contained in the same host.  In the first
1861         // case, the host symbol is obviously mapped, in the second case, it
1862         // must also be mapped because
1863         // HostAssociations::internalProcedureBindings that was called when
1864         // lowering `B` will have mapped all host symbols of captured variables
1865         // to the tuple argument containing the composite of all host associated
1866         // variables, whether or not the host symbol is actually referred to in
1867         // `B`. Hence it is possible to simply lookup the variable associated to
1868         // the host symbol without having to go back to the tuple argument.
1869         Fortran::lower::SymbolBox hostValue =
1870             symMap.lookupSymbol(hostDetails->symbol());
1871         assert(hostValue && "callee host symbol must be mapped on caller side");
1872         symMap.addSymbol(sym, hostValue.toExtendedValue());
1873         // The SymbolBox associated to the host symbols is complete, skip
1874         // instantiateVariable that would try to allocate a new storage.
1875         continue;
1876       }
1877       if (Fortran::semantics::IsDummy(sym) && sym.owner() == result.owner()) {
1878         // Get the argument for the dummy argument symbols of the current call.
1879         symMap.addSymbol(sym, caller.getArgumentValue(sym));
1880         // All the properties of the dummy variable may not come from the actual
1881         // argument, let instantiateVariable handle this.
1882       }
1883       // If this is neither a host associated or dummy symbol, it must be a
1884       // module or common block variable to satisfy specification expression
1885       // requirements in 10.1.11, instantiateVariable will get its address and
1886       // properties.
1887       instantiateVariable(converter, var, symMap, storeMap);
1888     }
1889   }
1890 }
1891 
1892 void Fortran::lower::createRuntimeTypeInfoGlobal(
1893     Fortran::lower::AbstractConverter &converter, mlir::Location loc,
1894     const Fortran::semantics::Symbol &typeInfoSym) {
1895   fir::FirOpBuilder &builder = converter.getFirOpBuilder();
1896   std::string globalName = Fortran::lower::mangle::mangleName(typeInfoSym);
1897   auto var = Fortran::lower::pft::Variable(typeInfoSym, /*global=*/true);
1898   mlir::StringAttr linkage = getLinkageAttribute(builder, var);
1899   defineGlobal(converter, var, globalName, linkage);
1900 }
1901