26 Commits

Author SHA1 Message Date
Raghu Maddhipatla
eec41d2f8d
Revert "[Flang] [OpenMP] [Semantics] [MLIR] [Lowering] Add lowering support for IS_DEVICE_PTR and HAS_DEVICE_ADDR clauses on OMP TARGET directive." (#88198)
Reverts llvm/llvm-project#74187
2024-04-09 16:18:56 -05:00
Raghu Maddhipatla
9d9560facb
[Flang] [OpenMP] [Semantics] [MLIR] [Lowering] Add lowering support for IS_DEVICE_PTR and HAS_DEVICE_ADDR clauses on OMP TARGET directive. (#74187)
Added lowering support for IS_DEVICE_PTR and HAS_DEVICE_ADDR clauses for
OMP TARGET directive and added related tests for these changes.

IS_DEVICE_PTR and HAS_DEVICE_ADDR clauses apply to OMP TARGET directive
OpenMP spec states

`The **is_device_ptr** clause indicates that its list items are device
pointers.`

`The **has_device_addr** clause indicates that its list items already
have device addresses and therefore they may be directly accessed from a
target device.`

Whereas USE_DEVICE_PTR and USE_DEVICE_ADDR clauses apply to OMP TARGET
DATA directive and OpenMP spec for them states

`Each list item in the **use_device_ptr** clause results in a new list
item that is a device pointer that refers to a device address`

`Each list item in a **use_device_addr** clause that is present in the
device data environment is treated as if it is implicitly mapped by a
map clause on the construct with a map-type of alloc`
2024-04-09 14:59:20 -05:00
Krzysztof Parzyszek
79199753fd
[flang][OpenMP] Make several function local to OpenMP.cpp, NFC (#86726)
There were several functions, mostly reduction-related, that were only
called from OpenMP.cpp. Remove them from OpenMP.h, and make them local
in OpenMP.cpp:
- genOpenMPReduction
- findReductionChain
- getConvertFromReductionOp
- updateReduction
- removeStoreOp

Also, move the function bodies out of the "public" section.
2024-03-28 07:46:01 -05:00
Krzysztof Parzyszek
4d177435ba
[flang][OpenMP] Rename makeList overloads to make{Objects,Clauses}, NFC (#86725)
Reserve `makeList` to create a list given an explicit converter
function.
2024-03-27 11:37:09 -05:00
Krzysztof Parzyszek
148a55795d
[flang][OpenMP] Make OpenMP clause representation language-agnostic (#86289)
The clause templates defined in ClauseT.h were originally based on
flang's parse tree nodes. Since those representations are going to be
reused for clang (together with the clause splitting code), it makes
sense to separate them from flang, and instead have them based on the
actual OpenMP spec (v5.2).

The member names in the templates follow the naming presented in the
spec, and the representation (e.g. members) is derived from the clause
definitions as described in the spec.

Since the representations of some clauses has changed (while preserving
the information), the current code using the clauses (especially the
code converting parser::OmpClause to omp::Clause) needs to be adjusted.

This patch does not make any functional changes.
2024-03-26 13:54:26 -05:00
Krzysztof Parzyszek
564035edb0
[flang][OpenMP] Organize genOMP functions in OpenMP.cpp, NFC (#86309)
Put all of the genOMP functions together, organize them in two groups:
for declarative constructs and for other (executable) constructs.

Replace visit functions for OpenMPDeclarativeConstruct and
OpenMPConstruct from listing individual visitors for each variant
alternative to using a single generic visitor. Essentially, going from
```
  std::visit(
    [](foo x) { genOMP(foo); }
    [](bar x) { TODO }
    [](baz x) { genOMP(baz); }
  )
```
to
```
void genOMP(bar x) {  // Separate visitor for an unhandled case
  TODO
}

[...]
  std::visit([&](auto &&s) { genOMP(s); })  // generic
```

This doesn't change any functionality, just reorganizes the functions a
bit. The intent here is to improve the readability of this file.
2024-03-25 09:54:04 -05:00
Krzysztof Parzyszek
2ab106cbd4
[flang][OpenMP] Convert processTODO and remove unused objects (#81627)
Remove `ClauseIterator2` and `clauses2` from ClauseProcessor.

[Clause representation 5/6]
2024-03-21 15:12:43 -05:00
Sergio Afonso
734026347c
Reapply "[Flang][OpenMP][Lower] NFC: Move clause processing helpers into the ClauseProcessor (#85258)" (#85807)
This patch contains slight modifications to the reverted PR #85258 to
avoid issues with constructs containing multiple reduction clauses,
uncovered by a test on the gfortran testsuite.

This reverts commit 9f80444c2e669237a5c92013f1a42b91b5609012.
2024-03-21 12:25:48 +00:00
Krzysztof Parzyszek
84115494d6
[flang][Lower] Convert OMP Map and related functions to evaluate::Expr (#81626)
The related functions are `gatherDataOperandAddrAndBounds` and
`genBoundsOps`. The former is used in OpenACC as well, and it was
updated to pass evaluate::Expr instead of parser objects.

The difference in the test case comes from unfolded conversions of index
expressions, which are explicitly of type integer(kind=8).

Delete now unused `findRepeatableClause2` and `findClause2`.

Add `AsGenericExpr` that takes std::optional. It already returns
optional Expr. Making it accept an optional Expr as input would reduce
the number of necessary checks when handling frequent optional values in
evaluator.

[Clause representation 4/6]
2024-03-20 15:00:29 -05:00
Sergio Afonso
d84252e064
[MLIR][OpenMP] NFC: Uniformize OpenMP ops names (#85393)
This patch proposes the renaming of certain OpenMP dialect operations with the
goal of improving readability and following a uniform naming convention for
MLIR operations and associated classes. In particular, the following operations
are renamed:

- `omp.map_info` -> `omp.map.info`
- `omp.target_update_data` -> `omp.target_update`
- `omp.ordered_region` -> `omp.ordered.region`
- `omp.cancellationpoint` -> `omp.cancellation_point`
- `omp.bounds` -> `omp.map.bounds`
- `omp.reduction.declare` -> `omp.declare_reduction`

Also, the following MLIR operation classes have been renamed:

- `omp::TaskLoopOp` -> `omp::TaskloopOp`
- `omp::TaskGroupOp` -> `omp::TaskgroupOp`
- `omp::DataBoundsOp` -> `omp::MapBoundsOp`
- `omp::DataOp` -> `omp::TargetDataOp`
- `omp::EnterDataOp` -> `omp::TargetEnterDataOp`
- `omp::ExitDataOp` -> `omp::TargetExitDataOp`
- `omp::UpdateDataOp` -> `omp::TargetUpdateOp`
- `omp::ReductionDeclareOp` -> `omp::DeclareReductionOp`
- `omp::WsLoopOp` -> `omp::WsloopOp`
2024-03-20 11:19:38 +00:00
Sergio Afonso
9f80444c2e Revert "[Flang][OpenMP][Lower] NFC: Move clause processing helpers into the ClauseProcessor (#85258)"
Reverting due to failing gfortran test.

This reverts commit 2f2f16f32bb2a6c250b19adbc229d9dc3b38640c.
2024-03-19 13:25:33 +00:00
Sergio Afonso
2f2f16f32b
[Flang][OpenMP][Lower] NFC: Move clause processing helpers into the ClauseProcessor (#85258)
This patch moves some code in PFT to MLIR OpenMP lowering to the
`ClauseProcessor` class. This is so that some behavior that is related
to certain clauses stays within the `ClauseProcessor` and it's not the
caller the one responsible for always doing this when the clause is
present.
2024-03-19 11:49:45 +00:00
Sergio Afonso
d671ebe46c
[Flang][Lower] NFC: Replace SmallVector with more suitable alternatives (#85227)
In this patch some uses of `llvm::SmallVector` in Flang's lowering to
MLIR are replaced by other types (i.e. `llvm::ArrayRef` and
`llvm::SmallVectorImpl`) which are intended for these uses. This
generally prevents relying on always passing small vectors with a
particular number of elements in the stack.
2024-03-19 10:45:59 +00:00
Krzysztof Parzyszek
037a32a9a7
[flang][OpenMP] Convert DataSharingProcessor to omp::Clause (#81629)
[Clause representation 6/6]
2024-03-15 16:42:06 -05:00
Krzysztof Parzyszek
63e70c0553
[flang][OpenMP] Convert repeatable clauses (except Map) in ClauseProc… (#81623)
…essor

Rename `findRepeatableClause` to `findRepeatableClause2`, and make the
new `findRepeatableClause` operate on new `omp::Clause` objects.

Leave `Map` unchanged, because it will require more changes for it to
work.

[Clause representation 3/6]
2024-03-15 07:04:42 -05:00
agozillon
096ee4e16f
[Flang][OpenMP] Implement "promotion" of use_device_ptr non-cptr arguments to use_device_addr (#82834)
This effectively implements some now deprecated OpenMP functionality
that some applications (most notably at the moment GenASiS)
unfortunately depend on (deprecated in specification version 5.2):

"If a list item in a use_device_ptr clause is not of type C_PTR, the
behavior is as if the list item appeared in a use_device_addr clause.
Support for such list items in a use_device_ptr clause is deprecated."

This PR downgrades the hard-error to a deprecated warning and "promotes"
the above cases by simply moving the offending operands from the
use_device_ptr value list to the back of the use_device_addr list (and
moves the related symbols, locs and types that form the BlockArgs
correspondingly) and then the generation of the target data construct
proceeds as normal.
2024-03-13 16:18:21 +01:00
Tom Eccles
f46f5a01f4
[flang][OpenMP][OMPIRBuilder][mlir] Optionally pass reduction vars by ref (#84304)
Previously reduction variables were always passed by value into and out
of the initialization and combiner regions of the OpenMP reduction
declare operation.

This worked well for reductions of primitive types (and might perform
better than passing by reference). But passing by reference will be
useful for array and derived type reductions (e.g. to move allocation
inside of the init region).

Passing reductions by reference requires different LLVM-IR generation
when lowering from MLIR because some of the loads/stores/allocations
will now be moved inside of the init and combiner regions. This
alternate code generation is requested using a new attribute to
omp.wsloop and omp.parallel.

Existing lowerings from mlir are unaffected (these will continue to use
the by-value argument passing.

Flang will continue to pass by-value argument passing for trivial types
unless a (hidden) command line argument is supplied. Non-trivial types
will always use the by-ref lowering.

Array reductions are not ready yet (but are coming very soon). In the
meantime, this is tested by forcing existing reductions to use by-ref.

Commit series for by-ref OpenMP reductions 3/3

---------

Co-authored-by: Mats Petersson <mats.petersson@arm.com>
2024-03-13 14:51:09 +00:00
harishch4
a4aac22683
[Flang][OpenMp] Fix to threadprivate not working with host-association. (#74966)
This patch considers host-associated variables to generate threadprivate
Ops.

Fixes: #60763 #84561
2024-03-12 20:04:35 +05:30
Anchu Rajendran S
c03fd37d9b
[flang] Changes to map variables in link clause of declare target (#83643)
As per the OpenMP standard, "If a variable appears in a link clause on a
declare target directive that does not have a device_type clause with
the nohost device-type-description then it is treated as if it had
appeared in a map clause with a map-type of tofrom" is an implicit
mapping rule. Before this change, such variables were mapped as to by
default.
2024-03-07 08:23:58 -08:00
agozillon
afb05cd646
[Flang][MLIR][OpenMP] Create a deferred declare target marking process for Bridge.cpp (#78502)
This patch seeks to create a process that happens on module finalization
for OpenMP, in which a list of operations that had declare target
directives applied to them and were not generated at the time of
processing the original declare target directive are re-checked to apply
the appropriate declare target semantics.

This works by maintaining a vector of declare target related data inside
of the FIR converter, in this case the symbol and the two relevant
unsigned integers representing the enumerators. This vector is added to
via a new function called from Bridge.cpp, insertDeferredDeclareTargets,
which happens prior to the processing of the directive (similarly to
getDeclareTargetFunctionDevice currently for requires), it effectively
checks if the Operation the declare target directive is applied to
currently exists, if it doesn't it appends to the vector. This is a
seperate function to the processing of the declare target via the
overloaded genOMP as we unfortunately do not have access to the list
without passing it through every call, as the AbstractConverter we pass
will not allow access to it (I've seen no other cases of casting it to a
FirConverter, so I opted to not do that).

The list is then processed at the end of the module in the
finalizeOpenMPLowering function in Bridge by calling a new function
markDelayedDeclareTargetFunctions which marks the latently generated
operations. In certain cases, some still will not be generated, e.g. if
an interface is defined, marked as declare target, but has no definition
or usage in the module then it will not be emitted to the module, so due
to these cases we must silently ignore when an operation has not been
found via it's symbol.

The main use-case for this (although, I imagine there is others) is for
processing interfaces that have been declared in a module with a declare
target directive but do not have their implementation defined in the
same module. For example, inside of a seperate C++ module that will be
linked in. In cases where the interface is called inside of a target
region it'll be marked as used on device appropriately (although,
realistically a user should explicitly mark it to match the
corresponding definition), however, in cases where it's used in a
non-clear manner through something like a function pointer passed to an
external call we require this explicit marking, which this patch adds
support for (currently will cause the compiler to crash).

This patch also adds documentation on the declare target process and 
mechanisms within the compiler currently.
2024-03-05 17:27:16 +01:00
Kiran Chandramohan
b585c43dcc
[Flang][OpenMP] : Add a temporary lowering for workshare directive (#78268)
As a temporary solution, lower workshare to the single directive
2024-03-05 10:28:36 +00:00
Peter Klausler
5225901ecd
[flang] Add [[maybe_unused]] to fix -Werror build (#83456)
Add the [[maybe_unused]] attribute to a variable in
lib/Lower/OpenMP/OpenMP.cpp to avoid a (possibly bogus) unused variable
warning when building with GCC 9.3.0.
2024-02-29 13:02:39 -08:00
Kareem Ergawy
26b8be201e
[flang][OpenMP][MLIR] Basic support for delayed privatization code-gen (#81833)
Adds basic support for emitting delayed privatizers from flang. So far,
only types of symbols are supported (i.e. scalars), support for more
complicated types will be added later. This also makes sure that
reduction and delayed privatization work properly together by merging
the
body-gen callbacks for both in case both clauses are present on the
parallel construct.
2024-02-28 10:15:57 +01:00
Leandro Lupori
e50a231dcd
[flang][OpenMP] Add support for copyprivate (#80485)
Add initial handling of OpenMP copyprivate clause in Flang.

When lowering copyprivate, Flang generates the copy function
needed by each variable and builds the appropriate
omp.single's CopyPrivateVarList.

This is patch 3 of 4, to add support for COPYPRIVATE in Flang.
Original PR: https://github.com/llvm/llvm-project/pull/73128
2024-02-21 14:51:37 -03:00
Pranav Bhandarkar
58f45d909d
[flang][openmp] - depend clause support in target, target enter/update/exit data constructs (#81610)
This patch adds support in flang for the depend clause in target and
target enter/update/exit constructs. Previously, the following line in a
fortran program would have resulted in the error shown below it.

    !$omp target map(to:a) depend(in:a)


"not yet implemented: Unhandled clause DEPEND in TARGET construct"
2024-02-21 11:28:25 -06:00
Kareem Ergawy
4d4af15c3f
[NFC][flang][OpenMP] Split DataSharing and Clause processors (#81973)
This started as an experiment to reduce the compilation time of
iterating over `Lower/OpenMP.cpp` a bit since it is too slow at the
moment. Trying to do that, I split the `DataSharingProcessor`,
`ReductionProcessor`, and `ClauseProcessor` into their own files and
extracted some shared code into a util file. All of these new `.h/.cpp`
files as well as `OpenMP.cpp` are now under a `Lower/OpenMP/` directory.

This resulted is a slightly better organization of the OpenMP lowering
code and hence opening this NFC.

As for the compilation time, this unfortunately does not affect it much
(it shaves off a few seconds of `OpenMP.cpp` compilation) since from
what I learned the bottleneck is in `DirectivesCommon.h` and
`PFTBuilder.h` which both consume a lot of time in template
instantiation it seems.
2024-02-21 15:55:42 +01:00