Commit Graph

186 Commits

Author SHA1 Message Date
Jie Fu
910b9372d1 [flang] Function 'attributeTypeIsCompatible' should be debug only (NFC)
/data/home/jiefu/llvm-project/flang/lib/Optimizer/CodeGen/CodeGen.cpp:2905:20: error: unused function 'attributeTypeIsCompatible' [-Werror,-Wunused-function]
static inline bool attributeTypeIsCompatible(mlir::MLIRContext *ctx,
                   ^
1 error generated.
2023-08-30 22:28:35 +08:00
Leandro Lupori
c8517f1752 [flang] Add support for dense complex constants
Add support for representing complex array constants with MLIR
dense attribute. This improves compile time and greatly reduces
memory usage of programs with large complex array constants.

Fixes https://github.com/llvm/llvm-project/issues/63610

Reviewed By: vzakhari

Differential Revision: https://reviews.llvm.org/D155951
2023-08-30 10:51:02 -03:00
Slava Zakharin
668f261bfa [flang] Make ISO_Fortran_binding.h a standalone header again.
This implements the proposal from
https://discourse.llvm.org/t/adding-flang-specific-header-files-to-clang/72442/6
Since ISO_Fortran_binding.h is supposed to be included from users'
C/C++ codes, it would better have no dependencies on other header
files.

Reviewed By: PeteSteinfeld

Differential Revision: https://reviews.llvm.org/D158549
2023-08-22 18:56:27 -07:00
Matthias Springer
ce254598b7 [mlir][Conversion] Store const type converter in ConversionPattern
ConversionPatterns do not (and should not) modify the type converter that they are using.

* Make `ConversionPattern::typeConverter` const.
* Make member functions of the `LLVMTypeConverter` const.
* Conversion patterns take a const type converter.
* Various helper functions (that are called from patterns) now also take a const type converter.

Differential Revision: https://reviews.llvm.org/D157601
2023-08-14 09:03:11 +02:00
Slava Zakharin
315939fd61 [flang] Fixed slice offset computation in XEmbox codegen.
For character type with unknown length we end up generating
a GEP with the base type `llvm.ptr<i[width]>`. The GEP produces
the address of the first element of the slice, and it should be
using the offset computed in the number of characters, while we were
providing the offset in bytes.

Simple reproducer fails with and w/o HLFIR:
```
program test
  integer,parameter :: ck = 4
  character(:,ck),allocatable :: res(:,:)
  allocate(character(3,ck) :: res(2,2))
  res(1,1) = ck_'111'
  res(1,2) = ck_'222'
  res(2,1) = ck_'333'
  res(2,2) = ck_'444'
  call check(res)
contains
  subroutine check(res)
    character(:,ck),allocatable :: res(:,:)
    print *, res(2,:)
  end subroutine check
end program test
```

Reviewed By: clementval

Differential Revision: https://reviews.llvm.org/D156849
2023-08-02 10:45:00 -07:00
Leandro Lupori
783222efde [flang] Fix codegen of subcomponents' indexing
Identify multidimensional array indices in subcomponents and
convert them from column-major to row-major ordering.

This fixes codegen for fircg.ext_array_coor, fircg.ext_embox and,
possibly, fircg.ext_rebox.

Fixes https://github.com/llvm/llvm-project/issues/62038

Reviewed By: jeanPerier

Differential Revision: https://reviews.llvm.org/D154214
2023-07-03 08:59:53 -03:00
Jean Perier
b881fc2737 [flang] Fix array substring emboxing code generation
The code generation of the fir.embox op creating descriptors for
array substring with a non constant length base was using the
substring length to compute the first dimension result stride.
Fix it to use the input length instead.

Reviewed By: PeteSteinfeld

Differential Revision: https://reviews.llvm.org/D154086
2023-06-29 18:40:01 +02:00
Jean Perier
51a3468150 [flang] Support CHARACTER(4) pointer targets
fir.rebox is emitting an llvm.sdiv to compute the character length
given the byte size from the input descriptor.
Inside a fir.global, this is not needed given the target length must
be accessible via the type, and it caused MLIR to fail LLVM IR
code generation (and crash).

Use the input type length when available instead.

Reviewed By: PeteSteinfeld, vzakhari

Differential Revision: https://reviews.llvm.org/D154072
2023-06-29 18:36:44 +02:00
David Truby
8cb0c3bb21 [flang] Add COMDAT to global variables where needed
On platforms which support COMDAT sections we should use them when
linkonce or linkonce_odr linkage is requested. This is required on
Windows (PE/COFF) and provides better behaviour than weak symbols on
ELF-based platforms.

This patch also reverts string literals to use linkonce instead of
internal linkage now that comdats are supported.

Differential Revision: https://reviews.llvm.org/D153768
2023-06-28 13:49:30 +01:00
Kelvin Li
a9e1d2e760 [flang] Add PowerPC vec_add, vec_and, vec_mul, vec_sub and vec_xor intrinsics
Differential Revision: https://reviews.llvm.org/D151857
2023-06-13 16:05:21 -04:00
Valentin Clement
677f7cc55a [mlir][flang][openacc] Remove obsolete operand legalization passes
The information needed for translation is now encoded in the dialect
operations and does not require a dedicated pass to be extracted.
Remove the obsolete passes that were performing operand legalization.

Reviewed By: jeanPerier

Differential Revision: https://reviews.llvm.org/D150248
2023-05-11 10:33:00 -07:00
Mats Petersson
43cf32a1c0 [flang]Zero Initialize simple types
Instead of filling uninitialized global variables with "undef",
initialize them with 0. Only for Integer, Float or Logical type
variables. Complex, user defined data structures, arrays, etc
are not supported at this point.

This patch fixes the main problem of
https://github.com/llvm/llvm-project/issues/62432

Reviewed By: jeanPerier

Differential Revision: https://reviews.llvm.org/D149877
2023-05-05 17:37:41 +01:00
Andrew Gozillon
6b44274d83 [Flang][MLIR] Alter Fir.GlobalOp to print and lower external attributes
Fir.GlobalOp's currently do not respect attributes that
are applied to them, this change will do two things:

- Allow lowering of arbitrary attributes applied to
Fir.GlobalOp's to LLVMGlobalOp's during CodeGen
- Allow printing and parsing of arbitrarily applied attributes

This allows applying other dialects attributes (or other
fir attributes) to fir.GlobalOps on the fly and have them
exist in the resulting LLVM dialect IR or FIR IR.

Reviewer: jeanPerier

Differential Revision: https://reviews.llvm.org/D148352
2023-04-20 07:06:05 -05:00
Slava Zakharin
a45ca5d999 [flang] Fixed substr access in embox/rebox CodeGen.
The code was using the original operand of the operation, while
it should have been using the remapped operands via the adaptor.

Differential Revision: https://reviews.llvm.org/D148587
2023-04-18 08:39:35 -07:00
Kiran Chandramohan
96e1d2b5b2 Revert "[Flang] Change fir.divc to perform library call rather than generate inline operations."
This reverts commit a7bb8e273f.

Revertin since this runs into an ABI issue.
2023-04-18 11:08:16 +00:00
V Donaldson
bddd7a6436 [flang] REAL(KIND=3) and COMPLEX(KIND=3) descriptors
Update descriptor generation to correctly set the `type` field for
REAL(3) and COMPLEX(3) objects.
2023-04-17 09:10:47 -07:00
Jean Perier
3ce7e4b28d [flang] fix fir.array_coor of fir.box with component references
When dealing with "derived_array(j)%component" where derived_array
is not a contiguous array, but for which we know the extent, lowering
generates a fir.array_coor op on a !fir.box<!fir.array<cst x T>> with
a fir.slice containing "j" in the component path.

Codegen first computes "derived_array(j)" address using the byte
strides inside the descriptor, and then computes the offset of "j"
from that address with a second GEP.
The type of the address in that second GEP matters since "j" is passed
in the GEP via an index indicating its component position in the type.

The code was using the LLVM type of "derived_array" instead of
"derived_array(j)".
In general, with fir.box, the extent ("cst" above) is unknown and those
types match. But if the extent of "derived_array" is a compile time
constant, its LLVM type will be [cst x T] instead of T*, and the produced
GEP will compute the address of the nth T instead of the nth component
inside T leading to undefined behaviors.

Fix this by computing the element type for the second GEP.

Differential Revision: https://reviews.llvm.org/D148226
2023-04-14 08:47:50 +02:00
V Donaldson
4add0e3db9 Revert "[flang] REAL(KIND=3) and COMPLEX(KIND=3) descriptors"
This reverts commit 17a4fcecf4.
2023-04-13 18:34:18 -07:00
V Donaldson
17a4fcecf4 [flang] REAL(KIND=3) and COMPLEX(KIND=3) descriptors
Update descriptor generation to correctly set the `type` field for
REAL(3) and COMPLEX(3) objects.
2023-04-13 18:02:13 -07:00
Valentin Clement
30408f5ccf [flang][NFC] Move TypeConverter.h header file to include dir
After the extraction of the TypeConverter, move the header files
to the include dir so the shared library build is fine.

Reviewed By: PeteSteinfeld

Differential Revision: https://reviews.llvm.org/D147979
2023-04-10 17:01:50 -07:00
Valentin Clement
cd9cdc6837 [flang][openacc] Add missing piece to translate to LLVM IR dialect
Add missing pieces to translate handle OpenACC dialect in the translation.

Depends on D147825

Reviewed By: PeteSteinfeld, razvanlupusoru

Differential Revision: https://reviews.llvm.org/D147828
2023-04-10 14:30:25 -07:00
Valentin Clement
cc0a0044bf Revert "[flang][openacc] Add missing piece to translate to LLVM IR dialect"
This reverts commit 03289dc7af.
2023-04-10 13:05:23 -07:00
Valentin Clement
03289dc7af [flang][openacc] Add missing piece to translate to LLVM IR dialect
Add missing pieces to translate handle OpenACC dialect in the translation.

Depends on D147825

Reviewed By: PeteSteinfeld, razvanlupusoru

Differential Revision: https://reviews.llvm.org/D147828
2023-04-10 12:18:59 -07:00
Sacha Ballantyne
a7bb8e273f [Flang] Change fir.divc to perform library call rather than generate inline operations.
Currently `fir.divc` is always lowered to a sequence of llvm operations to perform complex division, however this causes issues for extreme values when the calculations overflow. While this behaviour would be fine at -Ofast, this is currently the default at all levels.

This patch changes `fir.divc` to lower to a library call instead, except for when KIND=3 as there is no appropriate library call for this case.

Reviewed By: vzakhari

Differential Revision: https://reviews.llvm.org/D145808
2023-04-04 16:09:21 +00:00
Slava Zakharin
f9e995b4bd [flang] Normalize logical values during type conversions.
Flang was missing value normalization for logical<->integer conversions
which is required by Flang specification. The shrinking logical<->logical
conversions were also incorrectly truncating the input.
This change performs value normalization for all logical<->integer
conversions and logical<->logical conversions between different kinds.

Note that value normalization is not strictly required for
logical(kind=k1)->logical(kind=k2) conversions when k1 < k2.

Differential Revision: https://reviews.llvm.org/D147019
2023-03-28 10:11:47 -07:00
Renaud-K
4c5dee7773 [flang] Lowering fir.dispatch in the polymorphic op pass
Differential revision: https://reviews.llvm.org/D146594
2023-03-23 09:40:47 -07:00
Valentin Clement
c024fa4b5b [flang] Use llvm.zext when converting from i1 -> iXX
CodeGen used llvm.sext when converting fir.convert %0 : (i1) -> iXX
where iXX is any integer. This leads to wrong values when the initial
i1 is equal to 1.

Reviewed By: PeteSteinfeld

Differential Revision: https://reviews.llvm.org/D145984
2023-03-14 06:31:49 +01:00
Renaud-K
0538bfe774 [flang] Moving common polymorphic code into utility files
Differential revision: https://reviews.llvm.org/D145530
2023-03-08 08:23:21 -08:00
Tobias Gysi
ba0c082b49 [flang] Use the newly introduced AliasAnalysisOpInterface.
This revision uses the AliasAnalysisOpInterface introduced in
https://reviews.llvm.org/D144851 to set the tbaa attributes
of load and store ops. With the attribute it is also possible to
set the attribute on atomics as well as on memcopy and
memove intrinsics as soon as https://reviews.llvm.org/D144965
lands.

Reviewed By: vzakhari

Differential Revision: https://reviews.llvm.org/D145044
2023-03-02 08:32:42 +01:00
Slava Zakharin
cf550e6184 [flang] Run MathToLLVM conversion as part of math conversion PM.
We used to specify benefit=0 for MathToLibm patterns so that
MathToLLVM patterns are more preferable. D144450 removed
the benefit parameter, so we have to run MathToLLVM separately now.

This resolves Intrinsics/math-codegen failure caused by D144450.

Differential Revision: https://reviews.llvm.org/D144523
2023-02-21 19:15:45 -08:00
Alexander Belyaev
d61a3d3a9b [flang] Remove parameter for libm mlir::populateMathToLibmConversionPatterns.
https://reviews.llvm.org/rG8a9d4895df780231a14a1afc44e18b1f6b7eab93
2023-02-21 19:42:42 +01:00
Valentin Clement
16bd0fe56c [flang] Use fir.type_desc and delay type desc address resolution
Makes use of fir.type_desc in order to delay the type desc address
resolution. The lowering inserts fir.type_desc operation instead of fir.addr_of
operation pointing to the fir.global type descriptor. The fir.type_desc
operation is then lowered in code gen to an address of operation in the LLVM
dialect. At this stage, the type descriptor is generated in all cases.

Reviewed By: vdonaldson

Differential Revision: https://reviews.llvm.org/D142920
2023-01-31 20:11:35 +01:00
Valentin Clement
c97d3e5f68 [flang] Use input type to recover the type desc when emboxing
When emboxing to a polymorphic entity without a type source box,
the type desc address must be retrived from the input type and
not from the box type.

Reviewed By: jeanPerier

Differential Revision: https://reviews.llvm.org/D142435
2023-01-24 14:46:31 +01:00
Valentin Clement
f3a94ff057 [flang][NFC] Fix typo 2023-01-24 14:34:16 +01:00
Valentin Clement
959caaaa7b [flang] Add conditional rebox when passing fir.box to optional fir.class
When a `!fir.box<>` is passed as an actual argument to an optional
`!fir.class<>` dummy it needs a `fir.rebox` in order to propagate
the dynamic type information.
The `fir.rebox` needs to happen only on present argument.

Reviewed By: jeanPerier

Differential Revision: https://reviews.llvm.org/D142340
2023-01-23 15:42:04 +01:00
Slava Zakharin
9c84d20fa6 [flang] Generate TBAA information.
This is initial version of TBAA information generation for Flang
generated IR. The desired behavior is that TBAA type descriptors
are generated for FIR types during FIR to LLVM types conversion,
and then TBAA access tags are attached to memory accessing operations
when they are converted to LLVM IR dialect.

In the initial version the type conversion is not producing
TBAA type descriptors, and all memory accesses are just partitioned
into two sets of box and non-box accesses, which can never alias.

The TBAA generation is enabled by default at >O0 optimization levels.
TBAA generation may also be enabled via `apply-tbaa` option of
`fir-to-llvm-ir` conversion pass. `-mllvm -disable-tbaa` engineering
option allows disabling TBAA generation to override Flang's default
(e.g. when -O1 is used).

SPEC CPU2006/437.leslie3d speeds up by more than 2x on Icelake.

Reviewed By: jeanPerier, clementval

Differential Revision: https://reviews.llvm.org/D141820
2023-01-17 10:19:11 -08:00
Valentin Clement
0b19ac8300 [flang] Update createTempMutableBox to support polymorphic entities
When creating temporary from a polymorphic entity, its dynamic type
information must be carried over to the temporary.
This patch updates createTempMutableBox to support passing a source_box
from which the information will be carried over.
This is tested on the spread intrinsic and follow-up patches will updates
other temporary creation where needed.

Reviewed By: jeanPerier, PeteSteinfeld

Differential Revision: https://reviews.llvm.org/D141667
2023-01-16 09:36:59 +01:00
Valentin Clement
9f1bb307da [flang] Carry the whole polymorphic box in PolymorphicValue
Until now, only the address of the type descriptor was hold in
a PolymorphicValue. In some cases, the element size and the
type code are also needed when creating new polymorphic
descriptors from an element of a polymorphic entity.

This patch updates PolymorphicValue to carry the source
descriptor from which the element is extracted. The source
descriptor is then used when emboxing the element to a new
polymorphic descriptor.

This simplify the code done in D141274 and will be used
when creating polymorphic temporary as well.

Reviewed By: jeanPerier, PeteSteinfeld

Differential Revision: https://reviews.llvm.org/D141609
2023-01-12 18:00:16 +01:00
Valentin Clement
69bbcdd0ab [flang] Load unlimited polymorphic box the same way as other
Unlimited polymorphic descriptor have a set size and can be loaded the
same way as polymorphic or monomorphic descriptors. The descriptor
code gen as been set in D138587.
Of course, the data hold by those descriptors have an unknown size
at compile time.

Depends on D141383

Reviewed By: jeanPerier, PeteSteinfeld

Differential Revision: https://reviews.llvm.org/D141390
2023-01-10 20:58:49 +01:00
Jean Perier
de2811ee45 [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
2023-01-10 09:25:19 +01:00
Valentin Clement
747211b712 [flang] Handle emboxing of a fir.ref<none> to an unlimited polymorphic box
When an array element is extracted from an unlimited polymorphic array, the
emboxing of this element has to retrive the type code and element size from
the initial array. This patch retrive this information through the extracted
type descriptor.

This situation can be found in code like:

```
subroutine sub1(a)
  class(*) :: a(:)
  select type (x=>a(1))
  type is (integer)
    x = 10
  end select
end subroutine
```

Reviewed By: jeanPerier, PeteSteinfeld

Differential Revision: https://reviews.llvm.org/D141274
2023-01-09 17:52:44 +01:00
Ramkumar Ramachandra
22426110c5 mlir/tblgen: use std::optional in generation
This is part of an effort to migrate from llvm::Optional to
std::optional. This patch changes the way mlir-tblgen generates .inc
files, and modifies tests and documentation appropriately. It is a "no
compromises" patch, and doesn't leave the user with an unpleasant mix of
llvm::Optional and std::optional.

A non-trivial change has been made to ControlFlowInterfaces to split one
constructor into two, relating to a build failure on Windows.

See also: https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716

Signed-off-by: Ramkumar Ramachandra <r@artagnon.com>

Differential Revision: https://reviews.llvm.org/D138934
2022-12-17 11:13:26 +01:00
Slava Zakharin
9e8f677299 [flang] Lower exponentiation without using pgmath.
Exponentiation is lowered to either math::FPowI or Fortran runtime
call (in case of --math-runtime=precise).

MathToFuncs convertor will convert math::FPowI operations with
exponent width >32 to calls of outlined implementations and otherwise
will leave the operation to MathToLLVM convertor.

Reviewed By: jeanPerier

Differential Revision: https://reviews.llvm.org/D139806
2022-12-14 15:32:51 -08:00
Slava Zakharin
22702cc76c [mlir][math] Added math::FPowI conversion to calls of outlined implementations.
Power functions are implemented as linkonce_odr scalar functions
for FPowI operations met in a module.
Vector form of FPowI is linearized into a sequence of calls
of the scalar functions.

Option {min-width-of-fpowi-exponent} controls which FPowI operations
are converted by MathToFuncs: if the width of the exponent's integer
type is less than the specified value, then the operation is not converted.

Flang will specify {min-width-of-fpowi-exponent=33} to make sure that
math::FPowI operations with exponent wider than 32 bits will be converted
by MathToFuncs, and operations with more narrow exponent will be left
for MathToLLVM to convert them to LLVM::PowIOp.

Reviewed By: Mogball

Differential Revision: https://reviews.llvm.org/D139804
2022-12-13 12:15:35 -08:00
Valentin Clement
a6924c6d74 [flang] Use input type when emboxing/reboxing polymorphic entities
When emboxing an entity to a polymorphic box, use the input type to
compute the type code and element size as the box type is too generic.
When reboxing a polymorphic box, get this information from the input
box.

Reviewed By: jeanPerier

Differential Revision: https://reviews.llvm.org/D139916
2022-12-13 09:44:25 +01:00
Jean Perier
959de15055 [flang] fir.emboxchar codegen with static length fir.char types
The verifier of fir.emboxchar accepts `fir.char<kind, constant>` types,
but its codegen failed because of a missing cast. It is convenient and
safe to not require making the cast in FIR, so add it in codegen.

Differential Revision: https://reviews.llvm.org/D139514
2022-12-07 11:59:10 +01:00
Kazu Hirata
9a41739565 [flang] Use std::nullopt instead of None (NFC)
This patch mechanically replaces None with std::nullopt where the
compiler would warn if None were deprecated.  The intent is to reduce
the amount of manual work required in migrating from Optional to
std::optional.

This is part of an effort to migrate from llvm::Optional to
std::optional:

https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
2022-12-03 12:14:21 -08:00
Valentin Clement
ef06327063 [flang] Make sure PointerNullifyDerived is called on pointer array
Record type was not correctly retrived so the runtime call was not
produced correctly.
Fix how the record type is retrived so the correct call is
produced.

Reviewed By: jeanPerier

Differential Revision: https://reviews.llvm.org/D139084
2022-12-01 11:18:28 +01:00
Valentin Clement
391efeb538 [flang][NFC] Retrieve binding table from fir.dispatch_table operations
Change how the binding tables are retrived. Use the newly lowered
fir.dispatch_table operations instead of the fir.global type infos.

Reviewed By: jeanPerier

Differential Revision: https://reviews.llvm.org/D138903
2022-11-29 13:22:16 +01:00
Valentin Clement
c1b7e9c962 [flang] Adapt descriptor codegen to support unlimited polymorphic entities
Code generation to create and populate the descriptor (element size and
type code) is based on the boxed result type. This does not work well with
unlimited polymorphic entities since the fir type does not represent what is
actually emboxed or reboxed.
In the case of emboxing, the input type will be used to populate
the descriptor element size and type code.
When reboxing an unlimited polymorphic to a unlimited polymorphic entities, the
element size and type code is retrieve from the input box.

Reviewed By: jeanPerier

Differential Revision: https://reviews.llvm.org/D138587
2022-11-24 20:47:21 +01:00