[all-commits] [llvm/llvm-project] 2c1433: [flang] Block construct

vdonaldson via All-commits all-commits at lists.llvm.org
Tue Feb 28 09:55:34 PST 2023


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: 2c1433453d1670f668220670b8f2df60f9dc9949
      https://github.com/llvm/llvm-project/commit/2c1433453d1670f668220670b8f2df60f9dc9949
  Author: V Donaldson <vdonaldson at nvidia.com>
  Date:   2023-02-28 (Tue, 28 Feb 2023)

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

  Log Message:
  -----------
  [flang] Block construct

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 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 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.




More information about the All-commits mailing list