This is an archive of the discontinued LLVM Phabricator instance.

[flang] Add -fno-automatic, refine IsSaved()
ClosedPublic

Authored by klausler on Nov 18 2021, 4:51 PM.

Details

Summary

This legacy option (available in other Fortran compilers with various
spellings) implies the SAVE attribute for local variables on subprograms
that are not explicitly RECURSIVE. The SAVE attribute essentially implies
static rather than stack storage. This was the default setting in Fortran
until surprisingly recently, so explicit SAVE statements & attributes
could be and often were omitted from older codes. Note that initialized
objects already have an implied SAVE attribute, and objects in COMMON
effectively do too, as data overlays are extinct; and since objects that are
expected to survive from one invocation of a procedure to the next in static
storage should probably be explicit initialized in the first place, so the
use cases for this option are somewhat rare, and all of them could be
handled with explicit SAVE statements or attributes.

This implicit SAVE attribute must not apply to automatic (in the Fortran sense)
local objects, whose sizes cannot be known at compilation time. To get the
semantics of IsSaved() right, the IsAutomatic() predicate was moved into
Evaluate/tools.cpp to allow for dynamic linking of the compiler. The
redundant predicate IsAutomatic() was noticed, removed, and its uses replaced.

GNU Fortran's spelling of the option (-fno-automatic) was added to
the clang-based driver and used for basic sanity testing.

Diff Detail

Event Timeline

klausler created this revision.Nov 18 2021, 4:51 PM
klausler requested review of this revision.Nov 18 2021, 4:51 PM
jeanPerier accepted this revision.Nov 19 2021, 12:37 AM

Please double check that you indeed want to SAVE all main program variables and check clang/lib/Driver/ToolChains/Flang.cpp formatting. Otherwise the semantics/evaluate changes look good to me.

clang/lib/Driver/ToolChains/Flang.cpp
33

It looks like clang-format is unhappy here.

flang/lib/Evaluate/tools.cpp
1203

This will trigger all main program objects to be static, whether they are scalars or not right ?

Currently, lowering was promoting main program array objects to static storage, but not scalars (which matches nvfortran/classic flang). Do we want/need a different behavior here ?

This revision is now accepted and ready to land.Nov 19 2021, 12:37 AM
klausler marked an inline comment as done.Nov 19 2021, 9:18 AM
klausler added inline comments.
flang/lib/Evaluate/tools.cpp
1203

8.5.16p4 says "A variable, common block, or procedure pointer declared in the scoping unit of a main program, module, or submodule implicitly has the SAVE attribute, which may be confirmed by explicit specification." So yes. This has a semantic consequence in that a pointer in the main program may be initialized with a local TARGET variable in the main program without that variable having an explicit SAVE attribute.

klausler updated this revision to Diff 388529.Nov 19 2021, 9:29 AM
klausler marked an inline comment as done.

Tweak formatting.

klausler marked an inline comment as done.Nov 19 2021, 10:40 AM
klausler updated this revision to Diff 388635.Nov 19 2021, 2:46 PM

More clarification in IsSaved(), including a TODO comment about an interaction with lowering that needs correction.

This revision was landed with ongoing or failed builds.Nov 22 2021, 10:06 AM
This revision was automatically updated to reflect the committed changes.
Herald added a project: Restricted Project. · View Herald TranscriptNov 22 2021, 10:06 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript