Commit Graph

176 Commits

Author SHA1 Message Date
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
Valentin Clement
9d395e79a6 [flang] Set initial size and type code for unlimited polymorphic descriptor
Initialization of unlimited polymorphic descriptor was raising an error.
This patch sets a default size and type code for unlimited polymoprhic descriptor
that will be updated once allocated/assigned.

Reviewed By: PeteSteinfeld

Differential Revision: https://reviews.llvm.org/D138479
2022-11-22 14:48:34 +01:00
Valentin Clement
3b257a6373 [flang] Accept BaseBoxType in couple of fir.array_* operations
Couple of operation are expecting BoxType but can totally handle
ClassType as well. This patch updates couple of locations to support
BaseBoxType instead of BoxType only.

Reviewed By: jeanPerier, PeteSteinfeld

Differential Revision: https://reviews.llvm.org/D138422
2022-11-21 16:54:47 +01:00
Valentin Clement
6393d2ea24 [flang] Create fir.dispatch_table and fir.dt_entry operations
Create the fir.dispatch_table operation based on semantics
information. The fir.dispatch_table will be used for static devirtualization
as well as for fir.select_type conversion.

Depends on D138129

Reviewed By: jeanPerier, PeteSteinfeld

Differential Revision: https://reviews.llvm.org/D138131
2022-11-17 10:53:43 +01:00
Valentin Clement
49ee5634e7 [flang] Add fir.box_typecode operation
`fir.box_typecode` operation allows to retrieve the type code
from a boxed value. This will be used in the `fir.select_type` conversion
to if-then-else ladder for type guard statement with intrinsic type spec
instead of using a runtime call.

Reviewed By: jeanPerier

Differential Revision: https://reviews.llvm.org/D137829
2022-11-14 10:51:18 +01:00
Slava Zakharin
bc955cae35 [flang] Support arith::FastMathFlagsAttr for fir::CallOp.
The main purpose of this patch is to propagate fastmath attribute
to SimplifyIntrinsicsPass, so that the inline code can inherit
the call operation's attributes. Even though I added translation
of fastmath from fir::CallOp to LLVM::CallOp, there are no fastmath
attributes in LLVM IR. It looks like the translation drops it.
This will need additional commits.

Reviewed By: jeanPerier

Differential Revision: https://reviews.llvm.org/D137602
2022-11-09 15:31:09 -08:00
Valentin Clement
943b4c90a9 [flang] Handle dispatch on nopass procedure from array element
When a fir.dispatch is done with a NOPASS type-bound
procedure, the object in fir.dispatch is just used to gather the vtable.
Therefore, no transformation is done on it and the original entity
is used. The current code generation didn't expect the entity to be an array.
This patch update the code generation to be able to retrieve the vtable
accordingly.

Reviewed By: PeteSteinfeld

Differential Revision: https://reviews.llvm.org/D137255
2022-11-02 16:52:25 +01:00
Valentin Clement
2406ba1563 [flang] Fix code generation for polymorphic deallocation
This patch fix issues found during call to deallocate.
fir.class can be handled the same way as fir.box in fir.store operation
code generation. In MutableBox::createNewFirBox, the fir.class is
also already a boxed entity.

Reviewed By: PeteSteinfeld

Differential Revision: https://reviews.llvm.org/D137079
2022-10-31 16:34:06 +01:00
Valentin Clement
06fcd149d1 [flang] Lower allocate for polymorphic pointer
Lowering of allocate statement for polymoprhic pointers is a bit
different than for allocatables. A call to `PointerNullifyDerived`
runtime function is done instead of `AllocatableInitDerived`.

Reviewed By: PeteSteinfeld

Differential Revision: https://reviews.llvm.org/D136820
2022-10-27 20:56:06 +02:00
David Truby
000de66649 [flang] Use mlir complex dialect for supported operations
This patch lowers the complex operations supported by the MLIR complex
dialect to those operations rather than libm. When the math runtime flag
is set to precise, libm lowering is used instead.

Differential Revision: https://reviews.llvm.org/D135882
2022-10-26 12:37:15 +00:00
Valentin Clement
2e97898656 [flang] Use tdesc on fir.embox in code generation
For polymoprhic entities, the type descriptor is dynamic. The tdesc
operand on fir.embox is meant to propagate the dynamic type when
embox a polymorphic entity.
This patch makes use of this operand in code generation and update
the created descriptor accordingly.

Reviewed By: jeanPerier

Differential Revision: https://reviews.llvm.org/D136748
2022-10-26 14:17:00 +02:00