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