3195 Commits

Author SHA1 Message Date
Andrew Gozillon
062fce6f4d [Flang][OpenMP][MLIR] An mlir transformation pass for marking FuncOp's implicitly called from TargetOp's and declare target marked FuncOp's as implicitly declare target
This pass will mark functions called from TargetOp's
and declare target functions as implicitly declare
target by adding the MLIR declare target attribute
directly to the function.

This pass executes after the initial lowering of Fortran's PFT
to MLIR (FIR/OMP+Arith etc.) and is one of a series of passes
that aim to clean up the MLIR for offloading (seperate passes
in different patches, one for early outlining, another for declare
target function filtering).

Reviewers: jsjodin, skatrak, kiaranchandramohan

Differential Revision: https://reviews.llvm.org/D154247
2023-07-17 08:32:26 -05:00
Sergio Afonso
debdfc0ae2
[Flang][OpenMP][MLIR] Filter emitted code depending on declare target and device
This patch adds support for selecting which functions are lowered to LLVM IR
from MLIR depending on declare target information and whether host or device
code is being generated.

The approach proposed by this patch is to perform the filtering in two stages:
  - An MLIR transformation pass, which is added to the Flang translation flow
    after the `OMPEarlyOutliningPass`. The functions that are kept are those
    that match the OpenMP processor (host or device) the compiler invocation
    is targeting, according to the presence of the `-fopenmp-is-target-device`
    compiler option and declare target information. All functions contaning an
    `omp.target` are also kept, regardless of the declare target information of
    the function, due to the need for keeping target regions visible for both
    host and device compilation.
  - A filtering step during translation to LLVM IR, which is peformed for those
    functions that were kept because of the presence of a target region inside.
    If the targeted OpenMP processor does not match the declare target
    information of the function, then it is removed from the LLVM IR after its
    contents have been processed and translated. Since they should only contain
    an omp.target operation which, in turn, should have been outlined into
    another LLVM IR function, the wrapper can be deleted at that point.

Depends on D150328 and D150329.

Differential Revision: https://reviews.llvm.org/D147641
2023-07-17 09:07:54 +01:00
tomnatan
2109587cee [MLIR] Don't sort operand of commutative ops when comparing two ops as there is a correctness issue
This feature was introduced in `D123492`.

Doing equivalence on pointers to sort operands of commutative operations is incorrect when checking equivalence of ops in separate regions (where the lhs and rhs operands are marked as equivalent but are not the same value).

It was also discussed in `D123492` and `D129480` that the correct solution would be to stable sort the operands in canonicalization (based on some numbering in the region maybe), but until that lands, reverting this change will unblock us and other users.

An example of a pass that might not work properly because of this is `DuplicateFunctionEliminationPass`.

Reviewed By: mehdi_amini, jpienaar

Differential Revision: https://reviews.llvm.org/D154699
2023-07-14 16:11:54 -07:00
Valentin Clement
361beeeff0
[flang][openacc] Add extent when creating acc.bounds in genBaseBoundsOps
The extent information is available here so just add it to the acc.bounds
operation so it can be retrieved easily if needed.

Reviewed By: razvanlupusoru

Differential Revision: https://reviews.llvm.org/D155319
2023-07-14 12:56:58 -07:00
Kiran Chandramohan
305ad9a6b2 [Flang][OpenMP] Use typed assignment in Atomic Write lowering
Use typed assignment in Atomic Write lowering to better handle
type conversions of allowed types.

Note: We should make similar changes for other constructs in
later patches.

Reviewed By: NimishMishra

Differential Revision: https://reviews.llvm.org/D154163
2023-07-14 09:21:34 +00:00
Valentin Clement
dd1a4bb0d4
[flang][openacc] Add support for complex mul reduction
Add support to lower reduction with the multiply operator and
complex type.

Depends on D155007

Reviewed By: razvanlupusoru

Differential Revision: https://reviews.llvm.org/D155014
2023-07-13 11:18:20 -07:00
Andrew Gozillon
e909a2c1ca [Flang][OpenMP][Lower] Program level implicit SAVE variable handling for declare target
This is an attempt at mimicing the method in which
threadprivate handles the following type of variables:

program main
  integer :: i
  !$omp declare target to(i)
end

Which essentially generates a GlobalOp for the variable (which
would normally only be an alloca) when it's instantiated. The
main difference is there is no operation generated within the
function, instead the declare target attribute is appended
later within handleDeclareTarget.

Reviewers: kiranchandramohan

Differential Revision: https://reviews.llvm.org/D152037
2023-07-13 12:07:21 -05:00
Jan Sjodin
22a167779a [flang] Fix OMPEarlyOutlining erasing declare target functions
The early outlining pass was erasing target functions that need to be
kept. It should only erase functions that contain target ops.
2023-07-13 13:00:23 -04:00
Slava Zakharin
1fa4a0a012 [flang][hlfir] Fixed character allocatable in structure constructor.
The problem appeared as a segfault for case like this:
```
type t
character(11), allocatable :: c
end type
character(12), alloctable :: x
type(t) y
y = t(x)
```

The frontend representes `y = t(x)` as `y=t(c=%SET_LENGTH(x,11_8))`.
When 'x' is unallocated the hlfir.set_length lowering results in
segfault. It could probably be handled in hlfir.set_length lowering
by using NULL base for the hlfir.declare depending on the allocation
status of 'x', but I am not sure if !hlfir.expr, in general, is supposed
to represent an expression created from unallocated allocatable.
I believe in Fortran that would mean referencing an unallocated
allocatable, which is not allowed.

I decided to special case `SET_LENGTH` in structure constructor,
so that we use its 'x' operand as the RHS for the assign operation
implying the isAllocatable check for cases when 'x' is allocatable.
This requires setting keep_lhs_length_if_realloc flag for the assign
operation. Note that when the component being intialized has
deferred length the frontend does not produce `SET_LENGTH`.

Differential Revision: https://reviews.llvm.org/D155151
2023-07-13 09:44:39 -07:00
Valentin Clement
d3a0df78bf
[flang][openacc] Add semantic check for reduction operator and types
Check the combination of reduction operator and types. This is
currently not checking common block and composite types.

Depends on D155105

Reviewed By: razvanlupusoru

Differential Revision: https://reviews.llvm.org/D155106
2023-07-13 08:55:01 -07:00
Mark Danial
d85b94bf00 [Flang] -funderscoring bug fix
There was a bug with the -funderscoring / -fno-underscoring options from (https://reviews.llvm.org/D140795) that prevented the driver option from controlling the underscoring behaviour and instead the behaviour could only be controlled by the pass option instead of the driver option. The driver test case did not catch the bug and also needed to be updated.

Reviewed By: awarzynski

Differential Revision: https://reviews.llvm.org/D155042
2023-07-13 11:30:35 -04:00
Jan Sjodin
4ea6cee22b Add test case back but with !REQUIRES: amdgpu-registered-target. 2023-07-13 10:53:18 -04:00
Jan Sjodin
f118983b84 Remove amdgpu target to fix aarch64 buildbot failure. 2023-07-13 10:16:20 -04:00
Jan Sjodin
45a9604417 [Flang][OpenMP][MLIR] Add early outlining pass for omp.target operations to flang
This patch implements an early outlining transform of omp.target operations in
flang. The pass is needed because optimizations may cross target op region
boundaries, but with the outlining the resulting functions only contain a
single omp.target op plus a func.return, so there should not be any opportunity
to optimize across region boundaries.

The patch also adds an interface to be able to store and retrieve the parent
function name of the original target operation. This is needed to be able to
create correct kernel function names when lowering to LLVM-IR.

Reviewed By: kiranchandramohan, domada

Differential Revision: https://reviews.llvm.org/D154879
2023-07-13 09:14:42 -04:00
Kelvin Li
10124b3e1e [flang] Add PowerPC vec_sl, vec_sld, vec_sldw, vec_sll, vec_slo, vec_srl and vec_sro intrinsic
Co-authored-by: pscoro

Differential Revision: https://reviews.llvm.org/D154563
2023-07-12 17:39:13 -04:00
Valentin Clement
119c512cb2
[flang][openacc] Add support for complex add reduction
Add lowering support for reduction with the add operator
on complex type.

Reviewed By: razvanlupusoru

Differential Revision: https://reviews.llvm.org/D155007
2023-07-12 14:28:22 -07:00
Slava Zakharin
fba06f7a7c [flang][hlfir] Moved TODO for polymorphic vector subscripted expressions.
If I understand it correctly, the TODO is intended to catch cases
when we may need to create a temporary array for polymorphic entities
resulting from a vector subscription, i.e. when the final expression
type of hlfir.elemental_addr is polymorphic. The TODO check was done
very early, so it kicked in for cases where the it should not have.
For example,
```
type t
  integer n
end type
class(t), pointer :: p(:)
... p(vector_of_indices)%n
```

Such designators are supported by FIR lowering and can be currently
supported by HLFIR, but the TODO prevented this. I just moved the TODO
check later in the lowering pipeline.

I also updated the comment trying to describe my understanding of how
the correct mold entity can be computed for the elemental operation
producing HLFIR expression of polymorphic type.

This change provides 34 new passes in my nag testing.

Reviewed By: tblah

Differential Revision: https://reviews.llvm.org/D154814
2023-07-12 13:52:38 -07:00
Valentin Clement
fb90d5f6c3
[flang][openacc] Support .neqv. reduction operator
Add support for the `.neqv.` reduction operator for
Flang/OpenACC lowering.

Depends on D154900

Reviewed By: razvanlupusoru

Differential Revision: https://reviews.llvm.org/D154901
2023-07-12 13:27:02 -07:00
Valentin Clement
0e543747ab
[flang][openacc] Support .eqv. reduction operator
Add support for the `.eqv.` reduction operator for
Flang/OpenACC lowering.

Depends on D154898

Reviewed By: razvanlupusoru

Differential Revision: https://reviews.llvm.org/D154900
2023-07-12 13:00:10 -07:00
Valentin Clement
464594dc2a
[flang][openacc] Support .or. reduction operator
Add support for the `.or.` reduction operator for
Flang/OpenACC lowering.

Depends on D154896

Reviewed By: razvanlupusoru

Differential Revision: https://reviews.llvm.org/D154898
2023-07-12 12:58:22 -07:00
Valentin Clement
664575a9a9
[flang][openacc] Support .and. reduction operator
Add support for the `.and.` reduction operator
in Flang/OpenACC lowering.

Depends on D154888

Reviewed By: razvanlupusoru

Differential Revision: https://reviews.llvm.org/D154896
2023-07-12 12:54:56 -07:00
Valentin Clement
a48445f1ec
[flang][openacc] Support array slices when creating firstprivate recipe
The return type of the recipe must match the array slice provided by
the user. This patch enhance the recipe creation to take into account
the constant slices.

Depends on D154657

Reviewed By: razvanlupusoru

Differential Revision: https://reviews.llvm.org/D154727
2023-07-12 12:53:18 -07:00
Valentin Clement
67c8110fb3
[flang][openacc] Populate init and copy region of firstprivate recipe for simple type
Similiar to D154259 for private recipe, this patch makes use of the same code
for the init region of the firstprivate recipe and populate the copy region for
trivial types and arrays.

Reviewed By: razvanlupusoru

Differential Revision: https://reviews.llvm.org/D154657
2023-07-12 12:52:06 -07:00
Tom Eccles
345f8699c7 [flang][HLFIR] allow hlfir.get_length with hlfir.associate
hlfir.get_length will not modify the buffer and so it is safe for a
hlfir.associate using the same expression buffer not to make its own
copy.

Differential Revision: https://reviews.llvm.org/D154942
2023-07-12 17:02:58 +00:00
Kelvin Li
fc78b961f0 [flang] Update LIT test due to D154611 (NFC) 2023-07-11 16:51:41 -04:00
Valentin Clement
4a63264daa
[flang] Use fir.type_desc in nullify
Do not look for the global early in nullify codegen. The type descriptor
can be emitted later and it would raise an error as it could not be found.
Use `fir.type_desc` instead so it delays the type descriptor lookup until
evrything is emitted.

https://github.com/llvm/llvm-project/issues/63775

Reviewed By: vzakhari

Differential Revision: https://reviews.llvm.org/D154982
2023-07-11 12:41:53 -07:00
David Truby
f52c64b115 [flang] Add fastmath flags to localBuilder in IntrinsicCall
Currently the local builder used in IntrinsicCall doesn't have the
fastmath flags passed to it. This results in the fastmath attribute
not being added to certain runtime calls. This patch simply forwards
the fastmath flags from the parent builder.

Differential Revision: https://reviews.llvm.org/D154611
2023-07-11 18:53:31 +01:00
Slava Zakharin
07593a39a4 [flang][hlfir] Fixed NULL() handling in structure constructor.
When an initializer value is missing for an allocatable component
in a structure constructor, the RHS is NULL() expression.
We should just skip this part of the initializer, since the component
must become unallocated (as it is from the initialization).
Runtime detected rank mismatch when we tried to pass NULL() box
RHS for assigning it to the unallocated component of rank 1, 2, etc.

Reviewed By: tblah

Differential Revision: https://reviews.llvm.org/D154906
2023-07-11 09:40:08 -07:00
Slava Zakharin
5c42807904 [flang][hlfir] Fixed byval passing for dynamically optional intrinsic args.
In the context of elemental operation a dynamically optional
intrinsic argument must be lowered such that the elemental
designator is generated under isPresent check.

Reviewed By: tblah

Differential Revision: https://reviews.llvm.org/D154897
2023-07-11 09:40:08 -07:00
Valentin Clement
425c574892
[flang][openacc] Support ieor reduction operator
Add support for `ieor` reduction operator in
OpenACC lowering.

Depends on D154887

Reviewed By: razvanlupusoru

Differential Revision: https://reviews.llvm.org/D154888
2023-07-11 08:58:36 -07:00
Valentin Clement
0ec1acec78
[flang][openacc] Support ior reduction operator
Add support for `ior` reduction operator in
OpenACC lowering.

Depends on D154886

Reviewed By: razvanlupusoru

Differential Revision: https://reviews.llvm.org/D154887
2023-07-11 08:56:12 -07:00
Valentin Clement
1aa22fe5ce
[flang][openacc] Support iand reduction operator
Add support for `iand` reduction operator in
OpenACC lowering.

Reviewed By: razvanlupusoru

Differential Revision: https://reviews.llvm.org/D154886
2023-07-11 08:50:48 -07:00
Tom Eccles
a3f9ce69ee [flang][hlfir]: fix associate of expr with more than one use
Make a copy of the expression and associate that so that this is the
only use.

So far as I know, we don't currently generate code for an associate with
more than one use. This is here just in case.

Depends on D154715

Differential Revision: https://reviews.llvm.org/D154721
2023-07-11 10:32:44 +00:00
Tom Eccles
a918df1716 [flang][hlfir] use adaptor in associate bufferization
The associate operation checks if it is the only use of the hlfir.expr,
and if so it can take ownership of the hlfir.expr instead of copying it
(move semantics).

If this check is done by accessing the associate operation's arguments
directly (not through the AssociateOpAdaptor), the expression uses will
contain some operations which have been deleted. These can include prior
copies of the same associate operation, if that operation was cloned
(e.g. to lower a hlfir.elemental into a fir.do_loop). Accessing the
bufferized expression instead of the old hlfir.expr through the adaptor
avoids this false positive.

Differential Revision: https://reviews.llvm.org/D154715
2023-07-11 10:32:43 +00:00
Valentin Clement
5a0b91fc28
[flang][openacc] Fix false positive error in common block resolution
The following error was triggered in the added test case.
This is a false positive.

COMMON block must be declared in the same scoping unit in which the OpenACC directive or clause appears

Reviewed By: kiranchandramohan

Differential Revision: https://reviews.llvm.org/D154742
2023-07-10 08:39:35 -07:00
Sergio Afonso
63ca93c7d1
[OpenMP][OMPIRBuilder] Rename IsEmbedded and IsTargetCodegen flags
This patch renames the `OpenMPIRBuilderConfig` flags to reduce confusion over
their meaning. `IsTargetCodegen` becomes `IsGPU`, whereas `IsEmbedded` becomes
`IsTargetDevice`. The `-fopenmp-is-device` compiler option is also renamed to
`-fopenmp-is-target-device` and the `omp.is_device` MLIR attribute is renamed
to `omp.is_target_device`. Getters and setters of all these renamed properties
are also updated accordingly. Many unit tests have been updated to use the new
names, but an alias for the `-fopenmp-is-device` option is created so that
external programs do not stop working after the name change.

`IsGPU` is set when the target triple is AMDGCN or NVIDIA PTX, and it is only
valid if `IsTargetDevice` is specified as well. `IsTargetDevice` is set by the
`-fopenmp-is-target-device` compiler frontend option, which is only added to
the OpenMP device invocation for offloading-enabled programs.

Differential Revision: https://reviews.llvm.org/D154591
2023-07-10 14:14:16 +01:00
Tom Eccles
0e3fda6c72 [flang][hlfir] allow assoicate where the expr is also used by shape_of
This fixes the majority of cases where we hit the "hlfir.associate of
hlfir.expr with more than one use" TODO. In particular, this allows cam4
to be built.

hlfir.shape_of is just a way to delay reading shape information until
after intrinsics have been lowered to FIR runtime calls. It gets the
shape information from reading existing SSA values (e.g. fetching the
shape used when hlfir.declare'ing the variable).

Therefore hlfir.shape_of doesn't affect decisions about when to
deallocate the buffer.

Differential Revision: https://reviews.llvm.org/D154521
2023-07-07 09:25:13 +00:00
Valentin Clement
897e69dd83
[flang][openacc] Support array slices when creating private recipe
The return type of the recipe must match the array slice provided by
the user. This patch enhance the recipe creation to take into account
the constant slices.

Depends on D154259

Reviewed By: razvanlupusoru

Differential Revision: https://reviews.llvm.org/D154648
2023-07-06 14:08:18 -07:00
Valentin Clement
a05ed478c8
[flang][openacc] Populate the init region for acc.private.recipe for simple type
Generate code to allocate privates for trivial scalars and arrays.

Reviewed By: razvanlupusoru

Differential Revision: https://reviews.llvm.org/D154259
2023-07-06 13:55:54 -07:00
Slava Zakharin
a94eedc7d8 [flang][hlfir] Lower char length inquiry via hlfir.get_length.
ApplyOp provides the type parameters in its argument, so we can
take it from there. For hlfir.expr block arguments (such as with
user-defined assignments) we use hlfir.get_length in lowering.

Depends on D154561

Reviewed By: jeanPerier

Differential Revision: https://reviews.llvm.org/D154562
2023-07-06 13:21:45 -07:00
Slava Zakharin
4642198b65 [flang][hlfir] Codegen for hlfir.get_length.
Lower hlfir.get_length into the char length inquiry of the bufferized
entity. In some cases the codegen will fail with
`hlfir.associate of hlfir.expr with more than one use` - this will be fixed
separately (after D154521).

Depends on D154560

Reviewed By: tblah, jeanPerier

Differential Revision: https://reviews.llvm.org/D154561
2023-07-06 13:21:45 -07:00
Slava Zakharin
b77a06084a [flang][hlfir] Added hlfir.get_length to inquire char length from hlfir.expr.
We will use hlfir.get_length to lower inquiries of char length
applied to hlfir.expr character values.

Reviewed By: tblah, jeanPerier

Differential Revision: https://reviews.llvm.org/D154560
2023-07-06 13:21:45 -07:00
Slava Zakharin
4ef04876ef [flang][hlfir] Lower actual TARGET for dummy POINTER.
This patch implements HLFIR lowering for associating an actual
TARGET argument to a dummy POINTER argument.

Reviewed By: tblah, jeanPerier

Differential Revision: https://reviews.llvm.org/D154311
2023-07-05 11:36:14 -07:00
Valentin Clement
85128d8b6a
[flang][openacc] Fix false error when common block is in copy clause
Wrong error was reported mentioning that the common block was in
more than one data sharing clause.

Reviewed By: kiranchandramohan

Differential Revision: https://reviews.llvm.org/D154393
2023-07-05 08:34:22 -07:00
Kiran Chandramohan
8acae1a88b [Flang][OpenMP] Add source range for a few directives
The sourcerange was missing for a few directives when
they were the first directive to appear in a program
without a program statement.

Reviewed By: DavidTruby

Differential Revision: https://reviews.llvm.org/D153634
2023-07-05 13:27:52 +00:00
Kiran Chandramohan
7505f2c92d [Flang][OpenMP] Only access the MapType if it is specified
Fixes #63638

Reviewed By: TIFitis

Differential Revision: https://reviews.llvm.org/D154442
2023-07-05 12:56:09 +00:00
Jean Perier
e52a6d7784 [flang][hlfir] avoid useless LHS temporaries inside WHERE
The need to save LHS addresses on a stack before doing an assignment
is very limited: it is only really needed for forall and vectore
subscripted LHS where the LHS cannot be computed as a descriptor.

The previous current WHERE codegen was creating address stacks for
LHS element addresses when the LHS evaluation conflicts with the
assignment (may depend on the LHS value). This is not needed
since the computed array designator for the LHS is already "saved"
before the assignment from an SSA point of view.

This patch prevents LHS temporary stack from being created outside
of forall and vector subscripted assignments.

Differential Revision: https://reviews.llvm.org/D154418
2023-07-05 14:26:41 +02:00
Tom Eccles
1538ad9fc0 [flang][hlfir] fix elemental subroutine calls
genElementalCall can return a null option when lowering elemental
subroutine calls (as there is no return value). Therefore
std::option::value should not be used as it will cause an
assertion failure.

This fixes uses of the mvbits intrinsic with array arguments, as used in
the gfortran test suite.

Differential Revision: https://reviews.llvm.org/D154340
2023-07-04 09:34:43 +00:00
Tom Eccles
1b74fadd9e [flang][hlfir] intrinsic dynamically optional arguments
This adds support for dynamically optional arguments for intrinsics
which do not have their own hlfir operations.

The functions for processing these arguments are mostly the same as the
equivalent functions in ConvertExpr.cpp. I chose not to share
implementations so that HLFIR helpers can be used here. Presumably
ConvertExpr.cpp will go away one day.

Depends on D154236

Differential Revision: https://reviews.llvm.org/D154237
2023-07-04 09:34:43 +00:00
Tom Eccles
be4518f230 [flang][hlfir] support optional args for intrinsic ops
This also adds support for allocatable non-optional arguments.

Of the transformational intrinsics which currently have their own hlfir
operations, all of the dynamically optional arguments are lowered as
boxes, so that is all that is implemented for now.

One alternative approach would have been to deal with the dynamically
optional arguments when lowering to FIR runtime calls. I decided not to
do this so that any passes working on the intrinsic operations would not
have to know about and handle the dynamically optional arguments.

Depends on D154235

Differential Revision: https://reviews.llvm.org/D154236
2023-07-04 09:34:43 +00:00