[flang] Allow and use fir.rebox in fir.global
authorJean Perier <jperier@nvidia.com>
Tue, 10 Jan 2023 08:01:38 +0000 (09:01 +0100)
committerJean Perier <jperier@nvidia.com>
Tue, 10 Jan 2023 08:25:19 +0000 (09:25 +0100)
commitde2811ee45d340920cdd325bc791dafeef827808
treedaa933981c85413c17c11684d79502b1a0f33588
parentac07911b455ed0b29c201b1d59d3f33105777af9
[flang] Allow and use fir.rebox in fir.global

The current lowering of initial target in fir.global is relying
on how fir.box are created: instead of using a fir.rebox to add
the POINTER attribute to the created descriptor, it is looking
for a fir.embox defining operation and creating a copy of
it with a different result types.

The rational for doing so was that fir.rebox codegen was not possible
inside fir.global because it expects to manipulate the input fir.box
in memory, while objects cannot be manipulated in memory inside
a fir.global region that must be constant foldable.

But this approach has two problems:
- it won't work with hlfir where fir.box may be created by more
  operations than fir.embox (e.g. hlfir.delcare or hlfir.designate).
  In general, looking for a precise defining op for a value is
  fragile.
- manually copying and modifying an operation is risky: it is easy
  to forget copying some default operands (that could be added later).

This patch modifies the helpers to get descriptor fields so that they
can both operate on fir.box lowered in memory or in an llvm.struct
value. This enables the usage of fir.rebox in fir.global op.

The fallout in FIR tests is caused by the usage of constant index
when creating GEP (because extractOp requires constant indices).
MLIR builder uses i32 bit constant indices when non mlir::Value
indices are passed to the MLIR GEP op builder. Previously,
an 64 nist mlir constant value was created and passed to the GEP
builder. In this case, the builder respect the value type when
later generating the GEP.
Given this changes impact the "dimension" index that can, per
Fortran requirement, not be greated than 15, using a 32 bit index
is just fine and actually simplify the MLIR LLVM IR generation.

The fallout in lowering tests is caused by the introduction
of the fir.rebox everytime an initial target is created.

Differential Revision: https://reviews.llvm.org/D141136
14 files changed:
flang/lib/Lower/ConvertVariable.cpp
flang/lib/Optimizer/CodeGen/CodeGen.cpp
flang/lib/Optimizer/CodeGen/TypeConverter.h
flang/test/Fir/array-coor.fir
flang/test/Fir/arrexp.fir
flang/test/Fir/convert-to-llvm.fir
flang/test/Fir/coordinateof.fir
flang/test/Fir/rebox-global.fir [new file with mode: 0644]
flang/test/Fir/rebox-susbtring.fir
flang/test/Fir/rebox.fir
flang/test/Lower/default-initialization-globals.f90
flang/test/Lower/derived-pointer-components.f90
flang/test/Lower/pointer-initial-target-2.f90
flang/test/Lower/pointer-initial-target.f90