[flang-commits] [PATCH] D144916: [flang] Block construct

vdonaldson via Phabricator via flang-commits flang-commits at lists.llvm.org
Mon Feb 27 14:25:50 PST 2023


vdonaldson created this revision.
vdonaldson added a project: Flang.
Herald added subscribers: sunshaoce, mehdi_amini, jdoerfert.
Herald added a project: All.
vdonaldson requested review of this revision.

A block construct is an execution control construct that supports
declaration scopes contained within a parent subprogram scope or another
block scope. (blocks may be nested.) This is implemented by applying
basic scope processing to the block level.

Name uniquing/mangling is extended to support this. The term "block" is
heavily overloaded in Fortran standards. Prior name uniquing used tag `B`
for common block objects. Existing tag choices were modified to free up `B`
for block construct entities, and `C` for common blocks, and resolve
additional issues with other tags. The "old tag -> new tag" changes can
be summarized as:

     -> B  -- block construct -> new
  B  -> C  -- common block
  C  -> YI -- intrinsic type descriptor; not currently generated
  CT -> Y  -- nonintrinsic type descriptor; not currently generated
  G  -> N  -- namelist group
  L  ->    -- block data; not needed -> deleted

Existing name uniquing components consist of a tag followed by a name
from user source code, such as a module, subprogram, or variable name.
Block constructs are different in that they may be anonymous. (Like other
constructs, a block may have a `block-construct-name` that can be used
in exit statements, but this name is optional.) So blocks are given a
numeric compiler-generated preorder index starting with `B1`, `B2`,
and so on, on a per-procedure basis.

Name uniquing is also modified to include component names for all
containing procedures rather than for just the immediate host. This
fixes an existing name clash bug with same-named entities in same-named
host subprograms contained in different-named containing subprograms,
and variations of the bug involving modules and submodules.

F18 <https://reviews.llvm.org/F18> clause 9.7.3.1 (Deallocation of allocatable variables) paragraph 1
has a requirement that an allocated, unsaved allocatable local variable
must be deallocated on procedure exit. The following paragraph 2 states:

  When a BLOCK construct terminates, any unsaved allocated allocatable
  local variable of the construct is deallocated.

Similarly, F18 <https://reviews.llvm.org/F18> clause 7.5.6.3 (When finalization occurs) paragraph 3
has a requirement that a nonpointer, nonallocatable object must be
finalized on procedure exit. The following paragraph 4 states:

  A nonpointer nonallocatable local variable of a BLOCK construct
  is finalized immediately before it would become undefined due to
  termination of the BLOCK construct.

These deallocation and finalization requirements, along with stack
restoration requirements, require knowledge of block exits. In addition
to normal block termination at an end-block-stmt, a block may be
terminated by executing a branching statement that targets a statement
outside of the block. This includes

Single-target branch statements:

- goto
- exit
- cycle
- return

Bounded multiple-target branch statements:

- arithmetic goto
- IO statement with END, EOR, or ERR specifiers

Unbounded multiple-target branch statements:

- call with alternate return specs
- computed goto
- assigned goto

Lowering code is extended to determine if one of these branches exits
one or more relevant blocks or other constructs, and adds a mechanism to
insert any necessary deallocation, finalization, or stack restoration
code at the source of the branch. For a single-target branch it suffices
to generate the exit code just prior to taking the indicated branch.
Each target of a multiple-target branch must be analyzed individually.
Where necessary, the code must first branch to an intermediate basic
block that contains exit code, followed by a branch to the original target
statement.

This patch implements an `activeConstructStack` construct exit mechanism
that queries a new `activeConstruct` PFT bit to insert stack restoration
code at block exits. It ties in to existing code in ConvertVariable.cpp
routine `instantiateLocal` which has code for finalization, making block
exit finalization on par with subprogram exit finalization. Deallocation
is as yet unimplemented for subprograms or blocks. This may result in
memory leaks for affected objects at either the subprogram or block level.
Deallocation cases can be addressed uniformly for both scopes in a future
patch, presumably with code insertion in routine `instantiateLocal`.

The exit code mechanism is not limited to block construct exits. It is
also available for use with other constructs. In particular, it is used
to replace custom deallocation code for a select case construct character
selector expression where applicable. This functionality is also added
to select type and associate constructs. It is available for use with
other constructs, such as select rank and image control constructs,
if that turns out to be necessary.

Overlapping nonfunctional changes include eliminating "FIR" from some
routine names and eliminating obsolete spaces in comments.


https://reviews.llvm.org/D144916

Files:
  flang/docs/BijectiveInternalNameUniquing.md
  flang/include/flang/Lower/AbstractConverter.h
  flang/include/flang/Lower/IterationSpace.h
  flang/include/flang/Lower/Mangler.h
  flang/include/flang/Lower/PFTBuilder.h
  flang/include/flang/Lower/StatementContext.h
  flang/include/flang/Optimizer/Support/InternalNames.h
  flang/lib/Lower/Bridge.cpp
  flang/lib/Lower/CallInterface.cpp
  flang/lib/Lower/ConvertType.cpp
  flang/lib/Lower/ConvertVariable.cpp
  flang/lib/Lower/IO.cpp
  flang/lib/Lower/IterationSpace.cpp
  flang/lib/Lower/Mangler.cpp
  flang/lib/Lower/PFTBuilder.cpp
  flang/lib/Optimizer/Support/InternalNames.cpp
  flang/test/Fir/external-mangling.fir
  flang/test/Lower/HLFIR/allocatable-and-pointer-status-change.f90
  flang/test/Lower/HLFIR/statement-functions.f90
  flang/test/Lower/OpenMP/threadprivate-commonblock.f90
  flang/test/Lower/OpenMP/threadprivate-use-association.f90
  flang/test/Lower/arithmetic-goto.f90
  flang/test/Lower/array.f90
  flang/test/Lower/block.f90
  flang/test/Lower/common-block-2.f90
  flang/test/Lower/common-block.f90
  flang/test/Lower/computed-goto.f90
  flang/test/Lower/equivalence-2.f90
  flang/test/Lower/explicit-interface-results-2.f90
  flang/test/Lower/forall/array-constructor.f90
  flang/test/Lower/host-associated-globals.f90
  flang/test/Lower/module_definition.f90
  flang/test/Lower/module_use.f90
  flang/test/Lower/module_use_in_same_file.f90
  flang/test/Lower/namelist-common-block.f90
  flang/test/Lower/parent-component.f90
  flang/test/Lower/pointer-assignments.f90
  flang/test/Lower/pointer-initial-target-2.f90
  flang/test/Lower/program-units-fir-mangling.f90
  flang/test/Lower/select-case-statement.f90
  flang/unittests/Optimizer/InternalNamesTest.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D144916.500911.patch
Type: text/x-patch
Size: 185369 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/flang-commits/attachments/20230227/196b9cf0/attachment-0001.bin>


More information about the flang-commits mailing list