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 /// Define a global for a common block if it does not already exist in the
886 /// mlir module.
887 /// There is no "declare" version since there is not a
888 /// scope that owns common blocks more that the others. All scopes using
889 /// a common block attempts to define it with common linkage.
890 static fir::GlobalOp
891 defineCommonBlock(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   if (global)
897     return global;
898   Fortran::semantics::MutableSymbolVector cmnBlkMems =
899       getCommonMembersWithInitAliases(common);
900   mlir::Location loc = converter.genLocation(common.name());
901   mlir::IndexType idxTy = builder.getIndexType();
902   mlir::StringAttr linkage = builder.createCommonLinkage();
903   if (!common.name().size() || !commonBlockHasInit(cmnBlkMems)) {
904     // A blank (anonymous) COMMON block must always be initialized to zero.
905     // A named COMMON block sans initializers is also initialized to zero.
906     // mlir::Vector types must have a strictly positive size, so at least
907     // temporarily, force a zero size COMMON block to have one byte.
908     const auto sz = static_cast<fir::SequenceType::Extent>(
909         common.size() > 0 ? common.size() : 1);
910     fir::SequenceType::Shape shape = {sz};
911     mlir::IntegerType i8Ty = builder.getIntegerType(8);
912     auto commonTy = fir::SequenceType::get(shape, i8Ty);
913     auto vecTy = mlir::VectorType::get(sz, i8Ty);
914     mlir::Attribute zero = builder.getIntegerAttr(i8Ty, 0);
915     auto init = mlir::DenseElementsAttr::get(vecTy, llvm::makeArrayRef(zero));
916     return builder.createGlobal(loc, commonTy, commonName, linkage, init);
917   }
918 
919   // Named common with initializer, sort members by offset before generating
920   // the type and initializer.
921   std::sort(cmnBlkMems.begin(), cmnBlkMems.end(),
922             [](auto &s1, auto &s2) { return s1->offset() < s2->offset(); });
923   mlir::TupleType commonTy =
924       getTypeOfCommonWithInit(converter, cmnBlkMems, common.size());
925   auto initFunc = [&](fir::FirOpBuilder &builder) {
926     mlir::Value cb = builder.create<fir::UndefOp>(loc, commonTy);
927     unsigned tupIdx = 0;
928     std::size_t offset = 0;
929     LLVM_DEBUG(llvm::dbgs() << "block {\n");
930     for (const Fortran::semantics::MutableSymbolRef &mem : cmnBlkMems) {
931       if (const auto *memDet =
932               mem->detailsIf<Fortran::semantics::ObjectEntityDetails>()) {
933         if (mem->offset() > offset) {
934           ++tupIdx;
935           offset = mem->offset();
936         }
937         if (memDet->init()) {
938           LLVM_DEBUG(llvm::dbgs()
939                      << "offset: " << mem->offset() << " is " << *mem << '\n');
940           Fortran::lower::StatementContext stmtCtx;
941           auto initExpr = memDet->init().value();
942           fir::ExtendedValue initVal =
943               Fortran::semantics::IsPointer(*mem)
944                   ? Fortran::lower::genInitialDataTarget(
945                         converter, loc, converter.genType(*mem), initExpr)
946                   : genInitializerExprValue(converter, loc, initExpr, stmtCtx);
947           mlir::IntegerAttr offVal = builder.getIntegerAttr(idxTy, tupIdx);
948           mlir::Value castVal = builder.createConvert(
949               loc, commonTy.getType(tupIdx), fir::getBase(initVal));
950           cb = builder.create<fir::InsertValueOp>(loc, commonTy, cb, castVal,
951                                                   builder.getArrayAttr(offVal));
952           ++tupIdx;
953           offset = mem->offset() + mem->size();
954         }
955       }
956     }
957     LLVM_DEBUG(llvm::dbgs() << "}\n");
958     builder.create<fir::HasValueOp>(loc, cb);
959   };
960   // create the global object
961   return builder.createGlobal(loc, commonTy, commonName,
962                               /*isConstant=*/false, initFunc);
963 }
964 /// The COMMON block is a global structure. `var` will be at some offset
965 /// within the COMMON block. Adds the address of `var` (COMMON + offset) to
966 /// the symbol map.
967 static void instantiateCommon(Fortran::lower::AbstractConverter &converter,
968                               const Fortran::semantics::Symbol &common,
969                               const Fortran::lower::pft::Variable &var,
970                               Fortran::lower::SymMap &symMap) {
971   fir::FirOpBuilder &builder = converter.getFirOpBuilder();
972   const Fortran::semantics::Symbol &varSym = var.getSymbol();
973   mlir::Location loc = converter.genLocation(varSym.name());
974 
975   mlir::Value commonAddr;
976   if (Fortran::lower::SymbolBox symBox = symMap.lookupSymbol(common))
977     commonAddr = symBox.getAddr();
978   if (!commonAddr) {
979     // introduce a local AddrOf and add it to the map
980     fir::GlobalOp global = defineCommonBlock(converter, common);
981     commonAddr = builder.create<fir::AddrOfOp>(loc, global.resultType(),
982                                                global.getSymbol());
983 
984     symMap.addSymbol(common, commonAddr);
985   }
986   std::size_t byteOffset = varSym.GetUltimate().offset();
987   mlir::IntegerType i8Ty = builder.getIntegerType(8);
988   mlir::Type i8Ptr = builder.getRefType(i8Ty);
989   mlir::Type seqTy = builder.getRefType(builder.getVarLenSeqTy(i8Ty));
990   mlir::Value base = builder.createConvert(loc, seqTy, commonAddr);
991   mlir::Value offs =
992       builder.createIntegerConstant(loc, builder.getIndexType(), byteOffset);
993   auto varAddr = builder.create<fir::CoordinateOp>(loc, i8Ptr, base,
994                                                    mlir::ValueRange{offs});
995   mlir::Type symType = converter.genType(var.getSymbol());
996   mlir::Value local;
997   if (Fortran::semantics::FindEquivalenceSet(var.getSymbol()) != nullptr)
998     local = castAliasToPointer(builder, loc, symType, varAddr);
999   else
1000     local = builder.createConvert(loc, builder.getRefType(symType), varAddr);
1001   Fortran::lower::StatementContext stmtCtx;
1002   mapSymbolAttributes(converter, var, symMap, stmtCtx, local);
1003 }
1004 
1005 //===--------------------------------------------------------------===//
1006 // Lower Variables specification expressions and attributes
1007 //===--------------------------------------------------------------===//
1008 
1009 /// Helper to decide if a dummy argument must be tracked in an BoxValue.
1010 static bool lowerToBoxValue(const Fortran::semantics::Symbol &sym,
1011                             mlir::Value dummyArg) {
1012   // Only dummy arguments coming as fir.box can be tracked in an BoxValue.
1013   if (!dummyArg || !dummyArg.getType().isa<fir::BoxType>())
1014     return false;
1015   // Non contiguous arrays must be tracked in an BoxValue.
1016   if (sym.Rank() > 0 && !sym.attrs().test(Fortran::semantics::Attr::CONTIGUOUS))
1017     return true;
1018   // Assumed rank and optional fir.box cannot yet be read while lowering the
1019   // specifications.
1020   if (Fortran::evaluate::IsAssumedRank(sym) ||
1021       Fortran::semantics::IsOptional(sym))
1022     return true;
1023   // Polymorphic entity should be tracked through a fir.box that has the
1024   // dynamic type info.
1025   if (const Fortran::semantics::DeclTypeSpec *type = sym.GetType())
1026     if (type->IsPolymorphic())
1027       return true;
1028   return false;
1029 }
1030 
1031 /// Compute extent from lower and upper bound.
1032 static mlir::Value computeExtent(fir::FirOpBuilder &builder, mlir::Location loc,
1033                                  mlir::Value lb, mlir::Value ub) {
1034   mlir::IndexType idxTy = builder.getIndexType();
1035   // Let the folder deal with the common `ub - <const> + 1` case.
1036   auto diff = builder.create<mlir::arith::SubIOp>(loc, idxTy, ub, lb);
1037   mlir::Value one = builder.createIntegerConstant(loc, idxTy, 1);
1038   return builder.create<mlir::arith::AddIOp>(loc, idxTy, diff, one);
1039 }
1040 
1041 /// Lower explicit lower bounds into \p result. Does nothing if this is not an
1042 /// array, or if the lower bounds are deferred, or all implicit or one.
1043 static void lowerExplicitLowerBounds(
1044     Fortran::lower::AbstractConverter &converter, mlir::Location loc,
1045     const Fortran::lower::BoxAnalyzer &box,
1046     llvm::SmallVectorImpl<mlir::Value> &result, Fortran::lower::SymMap &symMap,
1047     Fortran::lower::StatementContext &stmtCtx) {
1048   if (!box.isArray() || box.lboundIsAllOnes())
1049     return;
1050   fir::FirOpBuilder &builder = converter.getFirOpBuilder();
1051   mlir::IndexType idxTy = builder.getIndexType();
1052   if (box.isStaticArray()) {
1053     for (int64_t lb : box.staticLBound())
1054       result.emplace_back(builder.createIntegerConstant(loc, idxTy, lb));
1055     return;
1056   }
1057   for (const Fortran::semantics::ShapeSpec *spec : box.dynamicBound()) {
1058     if (auto low = spec->lbound().GetExplicit()) {
1059       auto expr = Fortran::lower::SomeExpr{*low};
1060       mlir::Value lb = builder.createConvert(
1061           loc, idxTy, genScalarValue(converter, loc, expr, symMap, stmtCtx));
1062       result.emplace_back(lb);
1063     }
1064   }
1065   assert(result.empty() || result.size() == box.dynamicBound().size());
1066 }
1067 
1068 /// Lower explicit extents into \p result if this is an explicit-shape or
1069 /// assumed-size array. Does nothing if this is not an explicit-shape or
1070 /// assumed-size array.
1071 static void lowerExplicitExtents(Fortran::lower::AbstractConverter &converter,
1072                                  mlir::Location loc,
1073                                  const Fortran::lower::BoxAnalyzer &box,
1074                                  llvm::ArrayRef<mlir::Value> lowerBounds,
1075                                  llvm::SmallVectorImpl<mlir::Value> &result,
1076                                  Fortran::lower::SymMap &symMap,
1077                                  Fortran::lower::StatementContext &stmtCtx) {
1078   if (!box.isArray())
1079     return;
1080   fir::FirOpBuilder &builder = converter.getFirOpBuilder();
1081   mlir::IndexType idxTy = builder.getIndexType();
1082   if (box.isStaticArray()) {
1083     for (int64_t extent : box.staticShape())
1084       result.emplace_back(builder.createIntegerConstant(loc, idxTy, extent));
1085     return;
1086   }
1087   for (const auto &spec : llvm::enumerate(box.dynamicBound())) {
1088     if (auto up = spec.value()->ubound().GetExplicit()) {
1089       auto expr = Fortran::lower::SomeExpr{*up};
1090       mlir::Value ub = builder.createConvert(
1091           loc, idxTy, genScalarValue(converter, loc, expr, symMap, stmtCtx));
1092       if (lowerBounds.empty())
1093         result.emplace_back(ub);
1094       else
1095         result.emplace_back(
1096             computeExtent(builder, loc, lowerBounds[spec.index()], ub));
1097     } else if (spec.value()->ubound().isStar()) {
1098       // Assumed extent is undefined. Must be provided by user's code.
1099       result.emplace_back(builder.create<fir::UndefOp>(loc, idxTy));
1100     }
1101   }
1102   assert(result.empty() || result.size() == box.dynamicBound().size());
1103 }
1104 
1105 /// Lower explicit character length if any. Return empty mlir::Value if no
1106 /// explicit length.
1107 static mlir::Value
1108 lowerExplicitCharLen(Fortran::lower::AbstractConverter &converter,
1109                      mlir::Location loc, const Fortran::lower::BoxAnalyzer &box,
1110                      Fortran::lower::SymMap &symMap,
1111                      Fortran::lower::StatementContext &stmtCtx) {
1112   if (!box.isChar())
1113     return mlir::Value{};
1114   fir::FirOpBuilder &builder = converter.getFirOpBuilder();
1115   mlir::Type lenTy = builder.getCharacterLengthType();
1116   if (llvm::Optional<int64_t> len = box.getCharLenConst())
1117     return builder.createIntegerConstant(loc, lenTy, *len);
1118   if (llvm::Optional<Fortran::lower::SomeExpr> lenExpr = box.getCharLenExpr())
1119     // If the length expression is negative, the length is zero. See F2018
1120     // 7.4.4.2 point 5.
1121     return Fortran::lower::genMaxWithZero(
1122         builder, loc,
1123         genScalarValue(converter, loc, *lenExpr, symMap, stmtCtx));
1124   return mlir::Value{};
1125 }
1126 
1127 /// Treat negative values as undefined. Assumed size arrays will return -1 from
1128 /// the front end for example. Using negative values can produce hard to find
1129 /// bugs much further along in the compilation.
1130 static mlir::Value genExtentValue(fir::FirOpBuilder &builder,
1131                                   mlir::Location loc, mlir::Type idxTy,
1132                                   long frontEndExtent) {
1133   if (frontEndExtent >= 0)
1134     return builder.createIntegerConstant(loc, idxTy, frontEndExtent);
1135   return builder.create<fir::UndefOp>(loc, idxTy);
1136 }
1137 
1138 /// Lower specification expressions and attributes of variable \p var and
1139 /// add it to the symbol map.
1140 /// For global and aliases, the address must be pre-computed and provided
1141 /// in \p preAlloc.
1142 /// Dummy arguments must have already been mapped to mlir block arguments
1143 /// their mapping may be updated here.
1144 void Fortran::lower::mapSymbolAttributes(
1145     AbstractConverter &converter, const Fortran::lower::pft::Variable &var,
1146     Fortran::lower::SymMap &symMap, Fortran::lower::StatementContext &stmtCtx,
1147     mlir::Value preAlloc) {
1148   fir::FirOpBuilder &builder = converter.getFirOpBuilder();
1149   const Fortran::semantics::Symbol &sym = var.getSymbol();
1150   const mlir::Location loc = converter.genLocation(sym.name());
1151   mlir::IndexType idxTy = builder.getIndexType();
1152   const bool isDummy = Fortran::semantics::IsDummy(sym);
1153   const bool isResult = Fortran::semantics::IsFunctionResult(sym);
1154   const bool replace = isDummy || isResult;
1155   fir::factory::CharacterExprHelper charHelp{builder, loc};
1156   Fortran::lower::BoxAnalyzer ba;
1157   ba.analyze(sym);
1158 
1159   // First deal with pointers an allocatables, because their handling here
1160   // is the same regardless of their rank.
1161   if (Fortran::semantics::IsAllocatableOrPointer(sym)) {
1162     // Get address of fir.box describing the entity.
1163     // global
1164     mlir::Value boxAlloc = preAlloc;
1165     // dummy or passed result
1166     if (!boxAlloc)
1167       if (Fortran::lower::SymbolBox symbox = symMap.lookupSymbol(sym))
1168         boxAlloc = symbox.getAddr();
1169     // local
1170     if (!boxAlloc)
1171       boxAlloc = createNewLocal(converter, loc, var, preAlloc);
1172     // Lower non deferred parameters.
1173     llvm::SmallVector<mlir::Value> nonDeferredLenParams;
1174     if (ba.isChar()) {
1175       if (mlir::Value len =
1176               lowerExplicitCharLen(converter, loc, ba, symMap, stmtCtx))
1177         nonDeferredLenParams.push_back(len);
1178       else if (Fortran::semantics::IsAssumedLengthCharacter(sym))
1179         TODO(loc, "assumed length character allocatable");
1180     } else if (const Fortran::semantics::DeclTypeSpec *declTy = sym.GetType()) {
1181       if (const Fortran::semantics::DerivedTypeSpec *derived =
1182               declTy->AsDerived())
1183         if (Fortran::semantics::CountLenParameters(*derived) != 0)
1184           TODO(loc,
1185                "derived type allocatable or pointer with length parameters");
1186     }
1187     fir::MutableBoxValue box = Fortran::lower::createMutableBox(
1188         converter, loc, var, boxAlloc, nonDeferredLenParams);
1189     symMap.addAllocatableOrPointer(var.getSymbol(), box, replace);
1190     return;
1191   }
1192 
1193   if (isDummy) {
1194     mlir::Value dummyArg = symMap.lookupSymbol(sym).getAddr();
1195     if (lowerToBoxValue(sym, dummyArg)) {
1196       llvm::SmallVector<mlir::Value> lbounds;
1197       llvm::SmallVector<mlir::Value> extents;
1198       llvm::SmallVector<mlir::Value> explicitParams;
1199       // Lower lower bounds, explicit type parameters and explicit
1200       // extents if any.
1201       if (ba.isChar())
1202         if (mlir::Value len =
1203                 lowerExplicitCharLen(converter, loc, ba, symMap, stmtCtx))
1204           explicitParams.push_back(len);
1205       // TODO: derived type length parameters.
1206       lowerExplicitLowerBounds(converter, loc, ba, lbounds, symMap, stmtCtx);
1207       lowerExplicitExtents(converter, loc, ba, lbounds, extents, symMap,
1208                            stmtCtx);
1209       symMap.addBoxSymbol(sym, dummyArg, lbounds, explicitParams, extents,
1210                           replace);
1211       return;
1212     }
1213   }
1214 
1215   // Helper to generate scalars for the symbol properties.
1216   auto genValue = [&](const Fortran::lower::SomeExpr &expr) {
1217     return genScalarValue(converter, loc, expr, symMap, stmtCtx);
1218   };
1219 
1220   // For symbols reaching this point, all properties are constant and can be
1221   // read/computed already into ssa values.
1222 
1223   // The origin must be \vec{1}.
1224   auto populateShape = [&](auto &shapes, const auto &bounds, mlir::Value box) {
1225     for (auto iter : llvm::enumerate(bounds)) {
1226       auto *spec = iter.value();
1227       assert(spec->lbound().GetExplicit() &&
1228              "lbound must be explicit with constant value 1");
1229       if (auto high = spec->ubound().GetExplicit()) {
1230         Fortran::lower::SomeExpr highEx{*high};
1231         mlir::Value ub = genValue(highEx);
1232         shapes.emplace_back(builder.createConvert(loc, idxTy, ub));
1233       } else if (spec->ubound().isColon()) {
1234         assert(box && "assumed bounds require a descriptor");
1235         mlir::Value dim =
1236             builder.createIntegerConstant(loc, idxTy, iter.index());
1237         auto dimInfo =
1238             builder.create<fir::BoxDimsOp>(loc, idxTy, idxTy, idxTy, box, dim);
1239         shapes.emplace_back(dimInfo.getResult(1));
1240       } else if (spec->ubound().isStar()) {
1241         shapes.emplace_back(builder.create<fir::UndefOp>(loc, idxTy));
1242       } else {
1243         llvm::report_fatal_error("unknown bound category");
1244       }
1245     }
1246   };
1247 
1248   // The origin is not \vec{1}.
1249   auto populateLBoundsExtents = [&](auto &lbounds, auto &extents,
1250                                     const auto &bounds, mlir::Value box) {
1251     for (auto iter : llvm::enumerate(bounds)) {
1252       auto *spec = iter.value();
1253       fir::BoxDimsOp dimInfo;
1254       mlir::Value ub, lb;
1255       if (spec->lbound().isColon() || spec->ubound().isColon()) {
1256         // This is an assumed shape because allocatables and pointers extents
1257         // are not constant in the scope and are not read here.
1258         assert(box && "deferred bounds require a descriptor");
1259         mlir::Value dim =
1260             builder.createIntegerConstant(loc, idxTy, iter.index());
1261         dimInfo =
1262             builder.create<fir::BoxDimsOp>(loc, idxTy, idxTy, idxTy, box, dim);
1263         extents.emplace_back(dimInfo.getResult(1));
1264         if (auto low = spec->lbound().GetExplicit()) {
1265           auto expr = Fortran::lower::SomeExpr{*low};
1266           mlir::Value lb = builder.createConvert(loc, idxTy, genValue(expr));
1267           lbounds.emplace_back(lb);
1268         } else {
1269           // Implicit lower bound is 1 (Fortran 2018 section 8.5.8.3 point 3.)
1270           lbounds.emplace_back(builder.createIntegerConstant(loc, idxTy, 1));
1271         }
1272       } else {
1273         if (auto low = spec->lbound().GetExplicit()) {
1274           auto expr = Fortran::lower::SomeExpr{*low};
1275           lb = builder.createConvert(loc, idxTy, genValue(expr));
1276         } else {
1277           TODO(loc, "assumed rank lowering");
1278         }
1279 
1280         if (auto high = spec->ubound().GetExplicit()) {
1281           auto expr = Fortran::lower::SomeExpr{*high};
1282           ub = builder.createConvert(loc, idxTy, genValue(expr));
1283           lbounds.emplace_back(lb);
1284           extents.emplace_back(computeExtent(builder, loc, lb, ub));
1285         } else {
1286           // An assumed size array. The extent is not computed.
1287           assert(spec->ubound().isStar() && "expected assumed size");
1288           lbounds.emplace_back(lb);
1289           extents.emplace_back(builder.create<fir::UndefOp>(loc, idxTy));
1290         }
1291       }
1292     }
1293   };
1294 
1295   // Lower length expression for non deferred and non dummy assumed length
1296   // characters.
1297   auto genExplicitCharLen =
1298       [&](llvm::Optional<Fortran::lower::SomeExpr> charLen) -> mlir::Value {
1299     if (!charLen)
1300       fir::emitFatalError(loc, "expected explicit character length");
1301     mlir::Value rawLen = genValue(*charLen);
1302     // If the length expression is negative, the length is zero. See
1303     // F2018 7.4.4.2 point 5.
1304     return genMaxWithZero(builder, loc, rawLen);
1305   };
1306 
1307   ba.match(
1308       //===--------------------------------------------------------------===//
1309       // Trivial case.
1310       //===--------------------------------------------------------------===//
1311       [&](const Fortran::lower::details::ScalarSym &) {
1312         if (isDummy) {
1313           // This is an argument.
1314           if (!symMap.lookupSymbol(sym))
1315             mlir::emitError(loc, "symbol \"")
1316                 << toStringRef(sym.name()) << "\" must already be in map";
1317           return;
1318         } else if (isResult) {
1319           // Some Fortran results may be passed by argument (e.g. derived
1320           // types)
1321           if (symMap.lookupSymbol(sym))
1322             return;
1323         }
1324         // Otherwise, it's a local variable or function result.
1325         mlir::Value local = createNewLocal(converter, loc, var, preAlloc);
1326         symMap.addSymbol(sym, local);
1327       },
1328 
1329       //===--------------------------------------------------------------===//
1330       // The non-trivial cases are when we have an argument or local that has
1331       // a repetition value. Arguments might be passed as simple pointers and
1332       // need to be cast to a multi-dimensional array with constant bounds
1333       // (possibly with a missing column), bounds computed in the callee
1334       // (here), or with bounds from the caller (boxed somewhere else). Locals
1335       // have the same properties except they are never boxed arguments from
1336       // the caller and never having a missing column size.
1337       //===--------------------------------------------------------------===//
1338 
1339       [&](const Fortran::lower::details::ScalarStaticChar &x) {
1340         // type is a CHARACTER, determine the LEN value
1341         auto charLen = x.charLen();
1342         if (replace) {
1343           Fortran::lower::SymbolBox symBox = symMap.lookupSymbol(sym);
1344           std::pair<mlir::Value, mlir::Value> unboxchar =
1345               charHelp.createUnboxChar(symBox.getAddr());
1346           mlir::Value boxAddr = unboxchar.first;
1347           // Set/override LEN with a constant
1348           mlir::Value len = builder.createIntegerConstant(loc, idxTy, charLen);
1349           symMap.addCharSymbol(sym, boxAddr, len, true);
1350           return;
1351         }
1352         mlir::Value len = builder.createIntegerConstant(loc, idxTy, charLen);
1353         if (preAlloc) {
1354           symMap.addCharSymbol(sym, preAlloc, len);
1355           return;
1356         }
1357         mlir::Value local = createNewLocal(converter, loc, var, preAlloc);
1358         symMap.addCharSymbol(sym, local, len);
1359       },
1360 
1361       //===--------------------------------------------------------------===//
1362 
1363       [&](const Fortran::lower::details::ScalarDynamicChar &x) {
1364         // type is a CHARACTER, determine the LEN value
1365         auto charLen = x.charLen();
1366         if (replace) {
1367           Fortran::lower::SymbolBox symBox = symMap.lookupSymbol(sym);
1368           mlir::Value boxAddr = symBox.getAddr();
1369           mlir::Value len;
1370           mlir::Type addrTy = boxAddr.getType();
1371           if (addrTy.isa<fir::BoxCharType>() || addrTy.isa<fir::BoxType>()) {
1372             std::tie(boxAddr, len) = charHelp.createUnboxChar(symBox.getAddr());
1373           } else {
1374             // dummy from an other entry case: we cannot get a dynamic length
1375             // for it, it's illegal for the user program to use it. However,
1376             // since we are lowering all function unit statements regardless
1377             // of whether the execution will reach them or not, we need to
1378             // fill a value for the length here.
1379             len = builder.createIntegerConstant(
1380                 loc, builder.getCharacterLengthType(), 1);
1381           }
1382           // Override LEN with an expression
1383           if (charLen)
1384             len = genExplicitCharLen(charLen);
1385           symMap.addCharSymbol(sym, boxAddr, len, true);
1386           return;
1387         }
1388         // local CHARACTER variable
1389         mlir::Value len = genExplicitCharLen(charLen);
1390         if (preAlloc) {
1391           symMap.addCharSymbol(sym, preAlloc, len);
1392           return;
1393         }
1394         llvm::SmallVector<mlir::Value> lengths = {len};
1395         mlir::Value local =
1396             createNewLocal(converter, loc, var, preAlloc, llvm::None, lengths);
1397         symMap.addCharSymbol(sym, local, len);
1398       },
1399 
1400       //===--------------------------------------------------------------===//
1401 
1402       [&](const Fortran::lower::details::StaticArray &x) {
1403         // object shape is constant, not a character
1404         mlir::Type castTy = builder.getRefType(converter.genType(var));
1405         mlir::Value addr = symMap.lookupSymbol(sym).getAddr();
1406         if (addr)
1407           addr = builder.createConvert(loc, castTy, addr);
1408         if (x.lboundAllOnes()) {
1409           // if lower bounds are all ones, build simple shaped object
1410           llvm::SmallVector<mlir::Value> shape;
1411           for (int64_t i : x.shapes)
1412             shape.push_back(genExtentValue(builder, loc, idxTy, i));
1413           mlir::Value local =
1414               isDummy ? addr : createNewLocal(converter, loc, var, preAlloc);
1415           symMap.addSymbolWithShape(sym, local, shape, isDummy);
1416           return;
1417         }
1418         // If object is an array process the lower bound and extent values by
1419         // constructing constants and populating the lbounds and extents.
1420         llvm::SmallVector<mlir::Value> extents;
1421         llvm::SmallVector<mlir::Value> lbounds;
1422         for (auto [fst, snd] : llvm::zip(x.lbounds, x.shapes)) {
1423           lbounds.emplace_back(builder.createIntegerConstant(loc, idxTy, fst));
1424           extents.emplace_back(genExtentValue(builder, loc, idxTy, snd));
1425         }
1426         mlir::Value local =
1427             isDummy ? addr
1428                     : createNewLocal(converter, loc, var, preAlloc, extents);
1429         assert(isDummy || Fortran::lower::isExplicitShape(sym));
1430         symMap.addSymbolWithBounds(sym, local, extents, lbounds, isDummy);
1431       },
1432 
1433       //===--------------------------------------------------------------===//
1434 
1435       [&](const Fortran::lower::details::DynamicArray &x) {
1436         // cast to the known constant parts from the declaration
1437         mlir::Type varType = converter.genType(var);
1438         mlir::Value addr = symMap.lookupSymbol(sym).getAddr();
1439         mlir::Value argBox;
1440         mlir::Type castTy = builder.getRefType(varType);
1441         if (addr) {
1442           if (auto boxTy = addr.getType().dyn_cast<fir::BoxType>()) {
1443             argBox = addr;
1444             mlir::Type refTy = builder.getRefType(boxTy.getEleTy());
1445             addr = builder.create<fir::BoxAddrOp>(loc, refTy, argBox);
1446           }
1447           addr = builder.createConvert(loc, castTy, addr);
1448         }
1449         if (x.lboundAllOnes()) {
1450           // if lower bounds are all ones, build simple shaped object
1451           llvm::SmallVector<mlir::Value> shapes;
1452           populateShape(shapes, x.bounds, argBox);
1453           if (isDummy) {
1454             symMap.addSymbolWithShape(sym, addr, shapes, true);
1455             return;
1456           }
1457           // local array with computed bounds
1458           assert(Fortran::lower::isExplicitShape(sym) ||
1459                  Fortran::semantics::IsAllocatableOrPointer(sym));
1460           mlir::Value local =
1461               createNewLocal(converter, loc, var, preAlloc, shapes);
1462           symMap.addSymbolWithShape(sym, local, shapes);
1463           return;
1464         }
1465         // if object is an array process the lower bound and extent values
1466         llvm::SmallVector<mlir::Value> extents;
1467         llvm::SmallVector<mlir::Value> lbounds;
1468         populateLBoundsExtents(lbounds, extents, x.bounds, argBox);
1469         if (isDummy) {
1470           symMap.addSymbolWithBounds(sym, addr, extents, lbounds, true);
1471           return;
1472         }
1473         // local array with computed bounds
1474         assert(Fortran::lower::isExplicitShape(sym));
1475         mlir::Value local =
1476             createNewLocal(converter, loc, var, preAlloc, extents);
1477         symMap.addSymbolWithBounds(sym, local, extents, lbounds);
1478       },
1479 
1480       //===--------------------------------------------------------------===//
1481 
1482       [&](const Fortran::lower::details::StaticArrayStaticChar &x) {
1483         // if element type is a CHARACTER, determine the LEN value
1484         auto charLen = x.charLen();
1485         mlir::Value addr;
1486         mlir::Value len;
1487         if (isDummy) {
1488           Fortran::lower::SymbolBox symBox = symMap.lookupSymbol(sym);
1489           std::pair<mlir::Value, mlir::Value> unboxchar =
1490               charHelp.createUnboxChar(symBox.getAddr());
1491           addr = unboxchar.first;
1492           // Set/override LEN with a constant
1493           len = builder.createIntegerConstant(loc, idxTy, charLen);
1494         } else {
1495           // local CHARACTER variable
1496           len = builder.createIntegerConstant(loc, idxTy, charLen);
1497         }
1498 
1499         // object shape is constant
1500         mlir::Type castTy = builder.getRefType(converter.genType(var));
1501         if (addr)
1502           addr = builder.createConvert(loc, castTy, addr);
1503 
1504         if (x.lboundAllOnes()) {
1505           // if lower bounds are all ones, build simple shaped object
1506           llvm::SmallVector<mlir::Value> shape;
1507           for (int64_t i : x.shapes)
1508             shape.push_back(genExtentValue(builder, loc, idxTy, i));
1509           mlir::Value local =
1510               isDummy ? addr : createNewLocal(converter, loc, var, preAlloc);
1511           symMap.addCharSymbolWithShape(sym, local, len, shape, isDummy);
1512           return;
1513         }
1514 
1515         // if object is an array process the lower bound and extent values
1516         llvm::SmallVector<mlir::Value> extents;
1517         llvm::SmallVector<mlir::Value> lbounds;
1518         // construct constants and populate `bounds`
1519         for (auto [fst, snd] : llvm::zip(x.lbounds, x.shapes)) {
1520           lbounds.emplace_back(builder.createIntegerConstant(loc, idxTy, fst));
1521           extents.emplace_back(genExtentValue(builder, loc, idxTy, snd));
1522         }
1523 
1524         if (isDummy) {
1525           symMap.addCharSymbolWithBounds(sym, addr, len, extents, lbounds,
1526                                          true);
1527           return;
1528         }
1529         // local CHARACTER array with computed bounds
1530         assert(Fortran::lower::isExplicitShape(sym));
1531         mlir::Value local =
1532             createNewLocal(converter, loc, var, preAlloc, extents);
1533         symMap.addCharSymbolWithBounds(sym, local, len, extents, lbounds);
1534       },
1535 
1536       //===--------------------------------------------------------------===//
1537 
1538       [&](const Fortran::lower::details::StaticArrayDynamicChar &x) {
1539         mlir::Value addr;
1540         mlir::Value len;
1541         [[maybe_unused]] bool mustBeDummy = false;
1542         auto charLen = x.charLen();
1543         // if element type is a CHARACTER, determine the LEN value
1544         if (isDummy) {
1545           Fortran::lower::SymbolBox symBox = symMap.lookupSymbol(sym);
1546           std::pair<mlir::Value, mlir::Value> unboxchar =
1547               charHelp.createUnboxChar(symBox.getAddr());
1548           addr = unboxchar.first;
1549           if (charLen) {
1550             // Set/override LEN with an expression
1551             len = genExplicitCharLen(charLen);
1552           } else {
1553             // LEN is from the boxchar
1554             len = unboxchar.second;
1555             mustBeDummy = true;
1556           }
1557         } else {
1558           // local CHARACTER variable
1559           len = genExplicitCharLen(charLen);
1560         }
1561         llvm::SmallVector<mlir::Value> lengths = {len};
1562 
1563         // cast to the known constant parts from the declaration
1564         mlir::Type castTy = builder.getRefType(converter.genType(var));
1565         if (addr)
1566           addr = builder.createConvert(loc, castTy, addr);
1567 
1568         if (x.lboundAllOnes()) {
1569           // if lower bounds are all ones, build simple shaped object
1570           llvm::SmallVector<mlir::Value> shape;
1571           for (int64_t i : x.shapes)
1572             shape.push_back(genExtentValue(builder, loc, idxTy, i));
1573           if (isDummy) {
1574             symMap.addCharSymbolWithShape(sym, addr, len, shape, true);
1575             return;
1576           }
1577           // local CHARACTER array with constant size
1578           mlir::Value local = createNewLocal(converter, loc, var, preAlloc,
1579                                              llvm::None, lengths);
1580           symMap.addCharSymbolWithShape(sym, local, len, shape);
1581           return;
1582         }
1583 
1584         // if object is an array process the lower bound and extent values
1585         llvm::SmallVector<mlir::Value> extents;
1586         llvm::SmallVector<mlir::Value> lbounds;
1587 
1588         // construct constants and populate `bounds`
1589         for (auto [fst, snd] : llvm::zip(x.lbounds, x.shapes)) {
1590           lbounds.emplace_back(builder.createIntegerConstant(loc, idxTy, fst));
1591           extents.emplace_back(genExtentValue(builder, loc, idxTy, snd));
1592         }
1593         if (isDummy) {
1594           symMap.addCharSymbolWithBounds(sym, addr, len, extents, lbounds,
1595                                          true);
1596           return;
1597         }
1598         // local CHARACTER array with computed bounds
1599         assert((!mustBeDummy) && (Fortran::lower::isExplicitShape(sym)));
1600         mlir::Value local =
1601             createNewLocal(converter, loc, var, preAlloc, llvm::None, lengths);
1602         symMap.addCharSymbolWithBounds(sym, local, len, extents, lbounds);
1603       },
1604 
1605       //===--------------------------------------------------------------===//
1606 
1607       [&](const Fortran::lower::details::DynamicArrayStaticChar &x) {
1608         mlir::Value addr;
1609         mlir::Value len;
1610         mlir::Value argBox;
1611         auto charLen = x.charLen();
1612         // if element type is a CHARACTER, determine the LEN value
1613         if (isDummy) {
1614           mlir::Value actualArg = symMap.lookupSymbol(sym).getAddr();
1615           if (auto boxTy = actualArg.getType().dyn_cast<fir::BoxType>()) {
1616             argBox = actualArg;
1617             mlir::Type refTy = builder.getRefType(boxTy.getEleTy());
1618             addr = builder.create<fir::BoxAddrOp>(loc, refTy, argBox);
1619           } else {
1620             addr = charHelp.createUnboxChar(actualArg).first;
1621           }
1622           // Set/override LEN with a constant
1623           len = builder.createIntegerConstant(loc, idxTy, charLen);
1624         } else {
1625           // local CHARACTER variable
1626           len = builder.createIntegerConstant(loc, idxTy, charLen);
1627         }
1628 
1629         // cast to the known constant parts from the declaration
1630         mlir::Type castTy = builder.getRefType(converter.genType(var));
1631         if (addr)
1632           addr = builder.createConvert(loc, castTy, addr);
1633         if (x.lboundAllOnes()) {
1634           // if lower bounds are all ones, build simple shaped object
1635           llvm::SmallVector<mlir::Value> shape;
1636           populateShape(shape, x.bounds, argBox);
1637           if (isDummy) {
1638             symMap.addCharSymbolWithShape(sym, addr, len, shape, true);
1639             return;
1640           }
1641           // local CHARACTER array
1642           mlir::Value local =
1643               createNewLocal(converter, loc, var, preAlloc, shape);
1644           symMap.addCharSymbolWithShape(sym, local, len, shape);
1645           return;
1646         }
1647         // if object is an array process the lower bound and extent values
1648         llvm::SmallVector<mlir::Value> extents;
1649         llvm::SmallVector<mlir::Value> lbounds;
1650         populateLBoundsExtents(lbounds, extents, x.bounds, argBox);
1651         if (isDummy) {
1652           symMap.addCharSymbolWithBounds(sym, addr, len, extents, lbounds,
1653                                          true);
1654           return;
1655         }
1656         // local CHARACTER array with computed bounds
1657         assert(Fortran::lower::isExplicitShape(sym));
1658         mlir::Value local =
1659             createNewLocal(converter, loc, var, preAlloc, extents);
1660         symMap.addCharSymbolWithBounds(sym, local, len, extents, lbounds);
1661       },
1662 
1663       //===--------------------------------------------------------------===//
1664 
1665       [&](const Fortran::lower::details::DynamicArrayDynamicChar &x) {
1666         mlir::Value addr;
1667         mlir::Value len;
1668         mlir::Value argBox;
1669         auto charLen = x.charLen();
1670         // if element type is a CHARACTER, determine the LEN value
1671         if (isDummy) {
1672           mlir::Value actualArg = symMap.lookupSymbol(sym).getAddr();
1673           if (auto boxTy = actualArg.getType().dyn_cast<fir::BoxType>()) {
1674             argBox = actualArg;
1675             mlir::Type refTy = builder.getRefType(boxTy.getEleTy());
1676             addr = builder.create<fir::BoxAddrOp>(loc, refTy, argBox);
1677             if (charLen)
1678               // Set/override LEN with an expression.
1679               len = genExplicitCharLen(charLen);
1680             else
1681               // Get the length from the actual arguments.
1682               len = charHelp.readLengthFromBox(argBox);
1683           } else {
1684             std::pair<mlir::Value, mlir::Value> unboxchar =
1685                 charHelp.createUnboxChar(actualArg);
1686             addr = unboxchar.first;
1687             if (charLen) {
1688               // Set/override LEN with an expression
1689               len = genExplicitCharLen(charLen);
1690             } else {
1691               // Get the length from the actual arguments.
1692               len = unboxchar.second;
1693             }
1694           }
1695         } else {
1696           // local CHARACTER variable
1697           len = genExplicitCharLen(charLen);
1698         }
1699         llvm::SmallVector<mlir::Value> lengths = {len};
1700 
1701         // cast to the known constant parts from the declaration
1702         mlir::Type castTy = builder.getRefType(converter.genType(var));
1703         if (addr)
1704           addr = builder.createConvert(loc, castTy, addr);
1705         if (x.lboundAllOnes()) {
1706           // if lower bounds are all ones, build simple shaped object
1707           llvm::SmallVector<mlir::Value> shape;
1708           populateShape(shape, x.bounds, argBox);
1709           if (isDummy) {
1710             symMap.addCharSymbolWithShape(sym, addr, len, shape, true);
1711             return;
1712           }
1713           // local CHARACTER array
1714           mlir::Value local =
1715               createNewLocal(converter, loc, var, preAlloc, shape, lengths);
1716           symMap.addCharSymbolWithShape(sym, local, len, shape);
1717           return;
1718         }
1719         // Process the lower bound and extent values.
1720         llvm::SmallVector<mlir::Value> extents;
1721         llvm::SmallVector<mlir::Value> lbounds;
1722         populateLBoundsExtents(lbounds, extents, x.bounds, argBox);
1723         if (isDummy) {
1724           symMap.addCharSymbolWithBounds(sym, addr, len, extents, lbounds,
1725                                          true);
1726           return;
1727         }
1728         // local CHARACTER array with computed bounds
1729         assert(Fortran::lower::isExplicitShape(sym));
1730         mlir::Value local =
1731             createNewLocal(converter, loc, var, preAlloc, extents, lengths);
1732         symMap.addCharSymbolWithBounds(sym, local, len, extents, lbounds);
1733       },
1734 
1735       //===--------------------------------------------------------------===//
1736 
1737       [&](const Fortran::lower::BoxAnalyzer::None &) {
1738         mlir::emitError(loc, "symbol analysis failed on ")
1739             << toStringRef(sym.name());
1740       });
1741 }
1742 
1743 void Fortran::lower::defineModuleVariable(
1744     AbstractConverter &converter, const Fortran::lower::pft::Variable &var) {
1745   // Use empty linkage for module variables, which makes them available
1746   // for use in another unit.
1747   mlir::StringAttr linkage =
1748       getLinkageAttribute(converter.getFirOpBuilder(), var);
1749   if (!var.isGlobal())
1750     fir::emitFatalError(converter.getCurrentLocation(),
1751                         "attempting to lower module variable as local");
1752   // Define aggregate storages for equivalenced objects.
1753   if (var.isAggregateStore()) {
1754     const Fortran::lower::pft::Variable::AggregateStore &aggregate =
1755         var.getAggregateStore();
1756     std::string aggName = mangleGlobalAggregateStore(aggregate);
1757     defineGlobalAggregateStore(converter, aggregate, aggName, linkage);
1758     return;
1759   }
1760   const Fortran::semantics::Symbol &sym = var.getSymbol();
1761   if (const Fortran::semantics::Symbol *common =
1762           Fortran::semantics::FindCommonBlockContaining(var.getSymbol())) {
1763     // Define common block containing the variable.
1764     defineCommonBlock(converter, *common);
1765   } else if (var.isAlias()) {
1766     // Do nothing. Mapping will be done on user side.
1767   } else {
1768     std::string globalName = Fortran::lower::mangle::mangleName(sym);
1769     defineGlobal(converter, var, globalName, linkage);
1770   }
1771 }
1772 
1773 void Fortran::lower::instantiateVariable(AbstractConverter &converter,
1774                                          const pft::Variable &var,
1775                                          Fortran::lower::SymMap &symMap,
1776                                          AggregateStoreMap &storeMap) {
1777   if (var.isAggregateStore()) {
1778     instantiateAggregateStore(converter, var, storeMap);
1779   } else if (const Fortran::semantics::Symbol *common =
1780                  Fortran::semantics::FindCommonBlockContaining(
1781                      var.getSymbol().GetUltimate())) {
1782     instantiateCommon(converter, *common, var, symMap);
1783   } else if (var.isAlias()) {
1784     instantiateAlias(converter, var, symMap, storeMap);
1785   } else if (var.isGlobal()) {
1786     instantiateGlobal(converter, var, symMap);
1787   } else {
1788     instantiateLocal(converter, var, symMap);
1789   }
1790 }
1791 
1792 void Fortran::lower::mapCallInterfaceSymbols(
1793     AbstractConverter &converter, const Fortran::lower::CallerInterface &caller,
1794     SymMap &symMap) {
1795   Fortran::lower::AggregateStoreMap storeMap;
1796   const Fortran::semantics::Symbol &result = caller.getResultSymbol();
1797   for (Fortran::lower::pft::Variable var :
1798        Fortran::lower::pft::buildFuncResultDependencyList(result)) {
1799     if (var.isAggregateStore()) {
1800       instantiateVariable(converter, var, symMap, storeMap);
1801     } else {
1802       const Fortran::semantics::Symbol &sym = var.getSymbol();
1803       const auto *hostDetails =
1804           sym.detailsIf<Fortran::semantics::HostAssocDetails>();
1805       if (hostDetails && !var.isModuleVariable()) {
1806         // The callee is an internal procedure `A` whose result properties
1807         // depend on host variables. The caller may be the host, or another
1808         // internal procedure `B` contained in the same host.  In the first
1809         // case, the host symbol is obviously mapped, in the second case, it
1810         // must also be mapped because
1811         // HostAssociations::internalProcedureBindings that was called when
1812         // lowering `B` will have mapped all host symbols of captured variables
1813         // to the tuple argument containing the composite of all host associated
1814         // variables, whether or not the host symbol is actually referred to in
1815         // `B`. Hence it is possible to simply lookup the variable associated to
1816         // the host symbol without having to go back to the tuple argument.
1817         Fortran::lower::SymbolBox hostValue =
1818             symMap.lookupSymbol(hostDetails->symbol());
1819         assert(hostValue && "callee host symbol must be mapped on caller side");
1820         symMap.addSymbol(sym, hostValue.toExtendedValue());
1821         // The SymbolBox associated to the host symbols is complete, skip
1822         // instantiateVariable that would try to allocate a new storage.
1823         continue;
1824       }
1825       if (Fortran::semantics::IsDummy(sym) && sym.owner() == result.owner()) {
1826         // Get the argument for the dummy argument symbols of the current call.
1827         symMap.addSymbol(sym, caller.getArgumentValue(sym));
1828         // All the properties of the dummy variable may not come from the actual
1829         // argument, let instantiateVariable handle this.
1830       }
1831       // If this is neither a host associated or dummy symbol, it must be a
1832       // module or common block variable to satisfy specification expression
1833       // requirements in 10.1.11, instantiateVariable will get its address and
1834       // properties.
1835       instantiateVariable(converter, var, symMap, storeMap);
1836     }
1837   }
1838 }
1839 
1840 void Fortran::lower::createRuntimeTypeInfoGlobal(
1841     Fortran::lower::AbstractConverter &converter, mlir::Location loc,
1842     const Fortran::semantics::Symbol &typeInfoSym) {
1843   fir::FirOpBuilder &builder = converter.getFirOpBuilder();
1844   std::string globalName = Fortran::lower::mangle::mangleName(typeInfoSym);
1845   auto var = Fortran::lower::pft::Variable(typeInfoSym, /*global=*/true);
1846   mlir::StringAttr linkage = getLinkageAttribute(builder, var);
1847   defineGlobal(converter, var, globalName, linkage);
1848 }
1849