Skip to content

Commit b690597

Browse files
clementvaljeanPerierschweitzpgi
committed
[flang] Fixes several bugs relating to initialization expressions. An (#1493)
Fixes several bugs relating to initialization expressions. An initialization expression has no access to dynamic resources like the stack or the heap. It must reduce to a relocatable expression that the loader can complete at runtime. Adds regression test. This patch is part of the upstreaming effort from fir-dev branch. Reviewed By: PeteSteinfeld Differential Revision: https://reviews.llvm.org/D128380 Co-authored-by: Jean Perier <[email protected]> Co-authored-by: Eric Schweitz <[email protected]>
1 parent aa5492e commit b690597

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

flang/lib/Lower/PFTBuilder.cpp

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ static llvm::cl::opt<bool> nonRecursiveProcedures(
3030
"default for all Fortran standards prior to 2018."),
3131
llvm::cl::init(/*2018 standard=*/false));
3232

33+
static llvm::cl::opt<bool> mainProgramGlobals(
34+
"main-program-globals",
35+
llvm::cl::desc(
36+
"Allocate all variables in the main program as global variables and "
37+
"not on the stack regardless of type, kind, and rank."),
38+
llvm::cl::init(/*2018 standard=*/false), llvm::cl::Hidden);
39+
3340
using namespace Fortran;
3441

3542
namespace {
@@ -416,6 +423,8 @@ class PFTBuilder {
416423
} else if (const auto *entryStmt = p->getIf<parser::EntryStmt>()) {
417424
const semantics::Symbol *sym =
418425
std::get<parser::Name>(entryStmt->t).symbol;
426+
if (auto *details = sym->detailsIf<semantics::GenericDetails>())
427+
sym = details->specific();
419428
assert(sym->has<semantics::SubprogramDetails>() &&
420429
"entry must be a subprogram");
421430
entryPointList.push_back(std::pair{sym, p});
@@ -1257,7 +1266,7 @@ bool Fortran::lower::definedInCommonBlock(const semantics::Symbol &sym) {
12571266
return semantics::FindCommonBlockContaining(sym);
12581267
}
12591268

1260-
static bool isReEntrant(const Fortran::semantics::Scope &scope) {
1269+
static bool isReentrant(const Fortran::semantics::Scope &scope) {
12611270
if (scope.kind() == Fortran::semantics::Scope::Kind::MainProgram)
12621271
return false;
12631272
if (scope.kind() == Fortran::semantics::Scope::Kind::Subprogram) {
@@ -1277,7 +1286,7 @@ bool Fortran::lower::symbolIsGlobal(const semantics::Symbol &sym) {
12771286
if (const auto *details = sym.detailsIf<semantics::ObjectEntityDetails>()) {
12781287
if (details->init())
12791288
return true;
1280-
if (!isReEntrant(sym.owner())) {
1289+
if (!isReentrant(sym.owner())) {
12811290
// Turn array and character of non re-entrant programs (like the main
12821291
// program) into global memory.
12831292
if (const Fortran::semantics::DeclTypeSpec *symTy = sym.GetType())
@@ -1287,6 +1296,9 @@ bool Fortran::lower::symbolIsGlobal(const semantics::Symbol &sym) {
12871296
if (!details->shape().empty() || !details->coshape().empty())
12881297
return true;
12891298
}
1299+
if (mainProgramGlobals &&
1300+
sym.owner().kind() == Fortran::semantics::Scope::Kind::MainProgram)
1301+
return true;
12901302
}
12911303
return semantics::IsSaved(sym) || lower::definedInCommonBlock(sym) ||
12921304
semantics::IsNamedConstant(sym);
@@ -1821,10 +1833,9 @@ struct SymbolVisitor {
18211833
if (std::optional<Fortran::evaluate::ExtentExpr> length =
18221834
dynamicType->GetCharLength())
18231835
visitExpr(*length);
1824-
} else if (dynamicType->category() == common::TypeCategory::Derived) {
1825-
const Fortran::semantics::DerivedTypeSpec &derivedTypeSpec =
1826-
dynamicType->GetDerivedTypeSpec();
1827-
for (const auto &[_, param] : derivedTypeSpec.parameters())
1836+
} else if (const Fortran::semantics::DerivedTypeSpec *derivedTypeSpec =
1837+
Fortran::evaluate::GetDerivedTypeSpec(dynamicType)) {
1838+
for (const auto &[_, param] : derivedTypeSpec->parameters())
18281839
if (const Fortran::semantics::MaybeIntExpr &expr =
18291840
param.GetExplicit())
18301841
visitExpr(expr.value());

0 commit comments

Comments
 (0)