263 Commits

Author SHA1 Message Date
Chi-Chun, Chen
36dadddd74
[Flang][mlir][OpenMP] Add affinity clause to omp.task and Flang lowering (#179003)
- Add MLIR OpenMP affinity clause
- Lower flang task affinity to mlir
- Emit TODO for iterator modifier and update negative test
2026-02-04 10:30:35 -06:00
Chaitanya
55f0ed91ef
[OpenMP][MLIR] Add thread_limit with dims modifier support (#171825)
PR adds support of openmp 6.1 feature thread_limit with dims modifier.
llvmIR translation for thread_limit with dims modifier is marked as NYI.
2026-01-27 18:16:48 +05:30
Chaitanya
08654adc62
[OpenMP][MLIR] Add num_threads clause with dims modifier support (#171767)
PR adds support of openmp 6.1 feature num_threads with dims modifier.
llvmIR translation for num_threads with dims modifier is marked as NYI.
2026-01-27 15:30:55 +05:30
Chaitanya
3aaeace4e2
[OpenMP][MLIR] Add num_teams clause with dims modifier support (#169883)
PR adds support of openmp 6.1 feature `num_teams` with dims modifier.
llvmIR translation for num_teams with dims modifier is marked as NYI.
2026-01-27 10:55:40 +05:30
Chi-Chun, Chen
2f1b1f3a54
[flang][mlir][OpenMP] Support inbranch and notinbranch clause (#177310)
Support inbranch and notinbranch clause for OpenMP declare simd
directive.
2026-01-24 13:19:13 -06:00
Chi-Chun, Chen
d13119f269
[flang][mlir][OpenMP] Add support for uniform clause in declare simd (#176046)
Define OpenMP uniform clause in mlir and emit it from flang.
2026-01-20 11:08:01 -06:00
agozillon
c16340e49e
[NFC][MLIR][OpenMP] Correct attach_none to attach_never (#176855)
Originally gave attach_never the incorrect name, so this patch corrects
that to keep things consistent everywhere.
2026-01-20 15:43:27 +01:00
Chi-Chun, Chen
1c6d2add76
[OpenMP][Flang][MLIR] Introduce omp.declare_simd op and emit from Flang (#175604)
Changes:
- Adds a new `omp.declare_simd` operation to the OpenMP MLIR dialect
- Lowers Fortran `!$omp declare simd` into `omp.declare_simd` inside the
enclosing function body

mlir to LLVMIR translation and uniform clause will be added in follow-up
PRs.
2026-01-16 10:51:27 -06:00
NimishMishra
6bfa042a10
[flang][mlir] Add checks and test for linear clause on omp.wsloop and omp.simd (#174916)
This PR adds additional checks and tests for linear clause on omp.wsloop
and omp.simd (both standalone and composite). For composite simd
constructs, the translation to LLVMIR uses the same
`LinearClauseProcessor` under `convertOmpSimd`, as already present in
previous PRs like https://github.com/llvm/llvm-project/pull/150386 and
https://github.com/llvm/llvm-project/pull/139386
2026-01-09 06:51:29 -08:00
Tom Eccles
804aa88317
[MLIR][OpenMP] Support cancel taskgroup inside of taskloop (#174815)
Implementation follows exactly what is done for omp.wsloop and omp.task.
See #137841.

The change to the operation verifier is to allow a taskgroup
cancellation point inside of a taskloop. This was already allowed for
omp.cancel.
2026-01-09 11:43:54 +00:00
Akash Banerjee
b360a782ca
Reland "[Flang][OpenMP] Add lowering support for is_device_ptr clause (#169331)" (#170851)
Add support for OpenMP is_device_ptr clause for target directives.

[MLIR][OpenMP] Add OpenMPToLLVMIRTranslation support for is_device_ptr
#169367 This PR adds support for the OpenMP is_device_ptr clause in the
MLIR to LLVM IR translation for target regions. The is_device_ptr clause
allows device pointers (allocated via OpenMP runtime APIs) to be used
directly in target regions without implicit mapping.
2025-12-05 17:38:41 +00:00
NimishMishra
290b32a699
[llvm][mlir][OpenMP] Support translation for linear clause in omp.wsloop and omp.simd (#139386)
This patch adds support for LLVM translation of linear clause on
omp.wsloop (except for linear modifiers).
2025-12-04 20:39:17 -08:00
theRonShark
be79a0d90f
Revert "[Flang][OpenMP] Add lowering support for is_device_ptr clause" (#170778)
Reverts llvm/llvm-project#169331
2025-12-04 19:38:16 -05:00
Akash Banerjee
a77c4948a5
[Flang][OpenMP] Add lowering support for is_device_ptr clause (#169331)
Add support for OpenMP is_device_ptr clause for target directives.

[MLIR][OpenMP] Add OpenMPToLLVMIRTranslation support for is_device_ptr #169367
This PR adds support for the OpenMP is_device_ptr clause in the MLIR to LLVM IR translation for target regions. The is_device_ptr clause allows device pointers (allocated via OpenMP runtime APIs) to be used directly in target regions without implicit mapping.
2025-12-04 15:57:24 +00:00
Jack Styles
47ae3eaa29
[MLIR][OpenMP] Add MLIR Lowering Support for dist_schedule (#152736)
`dist_schedule` was previously supported in Flang/Clang but was not
implemented in MLIR, instead a user would get a "not yet implemented"
error. This patch adds support for the `dist_schedule` clause to be
lowered to LLVM IR when used in an `omp.distribute` or `omp.wsloop`
section.

There has needed to be some rework required to ensure that MLIR/LLVM
emits the correct Schedule Type for the clause, as it uses a different
schedule type to other OpenMP directives/clauses in the runtime library.

This patch also ensures that when using dist_schedule or a chunked
schedule clause, the correct llvm loop parallel accesses details are
added.
2025-11-27 14:16:44 +00:00
agozillon
f2b20d3410
[Flang][OpenMP][Dialect] Swap to using MLIR dialect enum to encode map flags (#164043)
This PR shifts from using the LLVM OpenMP enumerator bit flags to an
OpenMP dialect specific enumerator. This allows us to better represent
map types that wouldn't be of interest to the LLVM backend and runtime
in the dialect.

Primarily things like
ref_ptr/ref_ptee/ref_ptr_ptee/atach_none/attach_always/attach_auto which
are of interest to the compiler for certrain transformations (primarily
in the FIR transformation passes dealing with mapping), but the runtime
has no need to know about them. It also means if another OpenMP
implementation comes along they won't need to stick to the same bit flag
system LLVM chose/do leg work to address it.
2025-10-21 21:54:25 +02:00
Jakub Kuderski
8bab6c4e8c
[mlir] Simplify unreachable type switch cases. NFC. (#162032)
Use `DefaultUnreachable` from
https://github.com/llvm/llvm-project/pull/161970.
2025-10-06 09:23:25 -04:00
Michael Kruse
419594230f
[mlir][omp] Add omp.tile operation (#160292)
Add the `omp.tile` loop transformations for the OpenMP dialect. Used for
lowering a standalone `!$omp tile` in Flang.
2025-10-02 17:12:14 +00:00
Michael Kruse
1c1d525bf2
[mlir][omp] Improve canonloop/iv naming (#159773)
Improve the automatic naming of variables defined by the
`omp.canonical_loop` operation:

1. The iteration variable gets a name consistent with the cli variable
2. Instead of appending `_s0` for each nesting level, shorten it to
   `_d<num>` for a perfectly nested loop at depth `<num>`
3. Do not add any suffix to the top-level loop if it is the only
   top-level loop
2025-10-02 15:15:59 +02:00
Mehdi Amini
e95ed3352e [MLIR] Apply clang-tidy fixes for misc-use-internal-linkage in OpenMPDialect.cpp (NFC) 2025-10-01 03:55:10 -07:00
Dominik Adamski
83ef38a274
[Flang][OpenMP] Enable no-loop kernels (#155818)
Enable the generation of no-loop kernels for Fortran OpenMP code. target
teams distribute parallel do pragmas can be promoted to no-loop kernels
if the user adds the -fopenmp-assume-teams-oversubscription and
-fopenmp-assume-threads-oversubscription flags.

If the OpenMP kernel contains reduction or num_teams clauses, it is not
promoted to no-loop mode.

The global OpenMP device RTL oversubscription flags no longer force
no-loop code generation for Fortran.
2025-09-26 13:57:51 +02:00
Mehdi Amini
5cf35f1772 [MLIR] Apply clang-tidy fixes for performance-for-range-copy in OpenMPDialect.cpp (NFC) 2025-09-10 14:49:52 -07:00
Jan Leyonberg
d452e67ee7
[flang][OpenMP] Enable tiling (#143715)
This patch enables tiling in flang. In MLIR tiling is handled by
changing the the omp.loop_nest op to be able to represent both collapse
and tiling, so the flang front-end will combine the nested constructs into
a single MLIR op. The MLIR->LLVM-IR lowering of the LoopNestOp is
enhanced to first do the tiling if present, then collapse.
2025-09-10 09:25:40 -04:00
Chaitanya
7681855dd7
[OpenMP] Add workdistribute construct in openMP dialect and in llvm frontend (#154376)
This PR adds workdistribute mlir op in omp dialect and also in llvm
frontend.

The work in this PR is c-p and updated from @ivanradanov commits from coexecute implementation:
flang_workdistribute_iwomp_2024
2025-08-25 12:20:07 +05:30
Chaitanya
4a3bf27c69
[OpenMP] Introduce omp.target_allocmem and omp.target_freemem omp dialect ops. (#145464)
This PR introduces two new ops in omp dialect, omp.target_allocmem and
omp.target_freemem.
omp.target_allocmem: Allocates heap memory on device. Will be lowered to
omp_target_alloc call in llvm.
omp.target_freemem: Deallocates heap memory on device. Will be lowered
to omp+target_free call in llvm.


Example:
  %1 = omp.target_allocmem %device : i32, i64
  omp.target_freemem %device, %1 : i32, i64

The work in this PR is C-P/inspired from @ivanradanov commit from
coexecute implementation:
[Add fir omp target alloc and free
ops](be860ac8ba)
[Lower omp_target_{alloc,free} to
llvm](6e2d584dc9)
2025-08-18 18:15:11 +05:30
Longsheng Mou
f047b735e9
[mlir][NFC] Use getDefiningOp<OpTy>() instead of dyn_cast<OpTy>(getDefiningOp()) (#150428)
This PR uses `val.getDefiningOp<OpTy>()` to replace `dyn_cast<OpTy>(val.getDefiningOp())` , `dyn_cast_or_null<OpTy>(val.getDefiningOp())` and `dyn_cast_if_present<OpTy>(val.getDefiningOp())`.
2025-07-25 10:35:51 +08:00
Kazu Hirata
0925d7572a
[mlir] Remove unused includes (NFC) (#150266)
These are identified by misc-include-cleaner.  I've filtered out those
that break builds.  Also, I'm staying away from llvm-config.h,
config.h, and Compiler.h, which likely cause platform- or
compiler-specific build failures.
2025-07-23 15:18:53 -07:00
Maksim Levental
91d82bf4ab
[mlir] Fix u_int64_t in OpenMPDialect.cpp (#148963) 2025-07-15 17:13:53 -04:00
Raghu Maddhipatla
e2eade42b6
[MLIR] [OpenMP] Initial support for OMP ALLOCATE directive op. (#147900)
This patch includes adding support for OMP ALLOCATE directive along with
ALIGN clause and ALLOCATOR clause which are used within OMP ALLOCATE
directive
2025-07-15 10:10:33 -05:00
Peiming Liu
77d04ffd6d
[mlir][OpenMP] fix compilation warning (#147987) 2025-07-10 08:50:19 -07:00
Kazu Hirata
86320e0a8f [mlir] Fix warnings
This patch fixes:

  mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp:3047:16: error: unused
  variable 'ctx' [-Werror,-Wunused-variable]

  mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp:3171:16: error: unused
  variable 'ctx' [-Werror,-Wunused-variable]
2025-07-10 07:58:52 -07:00
Michael Kruse
628c735010
[MLIR][OpenMP] Add canonical loop operations (#147061)
Add the supporting OpenMP Dialect operations, types, and interfaces for
modelling

MLIR Operations:
 * omp.newcli
 * omp.canonical_loop

MLIR Types:
 * !omp.cli

MLIR Interfaces:
 * LoopTransformationInterface

As a first loop transformations to be able to use these new operation in
follow-up PRs (#144785)
 * omp.unroll_heuristic
2025-07-10 12:53:07 +02:00
Tom Eccles
ea5ee2e743
[mlir][OpenMP] Don't allow firstprivate for simd (#146734)
This is not allowed by the openmp standard.
2025-07-04 12:15:07 +01:00
Tom Eccles
a24ed7d477
[mlir][OpenMP] add attribute for privatization barrier (#140089)
A barrier is needed at the end of initialization/copying of private
variables if any of those variables is lastprivate. This ensures that
all firstprivate variables receive the original value of the variable
before the lastprivate clause overwrites it.

Previously this barrier was added by the flang fontend, but there is not
a reliable way to put the barrier in the correct place for delayed
privatization, and the OpenMP dialect could some day have other users.
It is important that there are safe ways to use the constructs available
in the dialect.

lastprivate is currently not modelled in the OpenMP dialect, and so
there is no way to reliably determine whether there were lastprivate
variables. Therefore the frontend will have to provide this information
through this new attribute.

Part of a series of patches to fix
https://github.com/llvm/llvm-project/issues/136357
2025-05-22 15:24:02 +01:00
Sergio Afonso
30b0946326
[Flang][MLIR][OpenMP] Improve use_device_* handling (#137198)
This patch updates MLIR op verifiers for operations taking arguments
that must always be defined by an `omp.map.info` operation to check this
requirement.

It also modifies Flang lowering for `use_device_{addr, ptr}`, as well as
the custom MLIR printer and parser for these clauses, to support
initializing it to `OMP_MAP_RETURN_PARAM` and represent this in the MLIR
representation as `return_param`. This internal mapping flag is what
eventually is used for variables passed via these clauses into the
target region when translating to LLVM IR, so making it explicit in
Flang and MLIR removes an inconsistency in the current representation.
2025-05-15 12:28:06 +01:00
Rahul Joshi
b17f3c63de
[NFC][MLIR] Add {} for else when if body has {} (#139422) 2025-05-12 10:29:03 -07:00
Kaviya Rajendiran
e356893551
[Flang][OpenMP] Support for lowering of taskloop construct to MLIR (#138646)
Added support for lowering of taskloop construct and its clauses(Private
and Firstprivate) to MLIR.
2025-05-06 19:47:22 +05:30
Kaviya Rajendiran
dad3162756
[mlir][OpenMP] Allow 'cancel taskgroup' inside taskloop region (#138634)
Added support which allows "cancel taskgroup" inside taskloop region.
2025-05-06 16:13:19 +05:30
Tom Eccles
d9bab3349a
[mlir][OpenMP] add verifier and tests for cancel taskgroup (#137154) 2025-04-25 10:47:59 +01:00
Tom Eccles
77341388a7
[mlir][OpenMP] allow cancellation to not be directly nested (#134084)
omp.cancel and omp.cancellationpoint contain an attribute describing the
type of parent construct which should be cancelled. e.g.
```
!$omp cancel do
```
Must be inside of a wsloop. Previously the verifer required the
immediate parent to be this operation. This is not quite right because
something like the following is valid:
```
!$omp parallel do
do i = 1, N
  if (cond) then
    !$omp cancel do
  endif
enddo
```

This patch relaxes the verifier to only require that some parent
operation matches (not necessarily the immediate parent).
2025-04-14 10:39:03 +01:00
Sergio Afonso
0de48de36e
[MLIR][OpenMP] Improve loop wrapper op verifiers (#134833)
This patch revisits op verifiers for `LoopWrapperInterface` operations
to improve consistency across operations and to properly cover some
previously misreported cases.

Checks that should be done for these kinds of operations are documented
in the interface description.
2025-04-09 12:36:07 +01:00
Sergio Afonso
f59b5b8d59
[MLIR][OpenMP] Fix standalone distribute on the device (#133094)
This patch updates the handling of target regions to set trip counts and
kernel execution modes properly, based on clang's behavior. This fixes a
race condition on `target teams distribute` constructs with no `parallel
do` loop inside.

This is how kernels are classified, after changes introduced in this
patch:

```f90
! Exec mode: SPMD.
! Trip count: Set.
!$omp target teams distribute parallel do
do i=...
end do

! Exec mode: Generic-SPMD.
! Trip count: Set (outer loop).
!$omp target teams distribute
do i=...
  !$omp parallel do private(idx, y)
  do j=...
  end do
end do

! Exec mode: Generic-SPMD.
! Trip count: Set (outer loop).
!$omp target teams distribute
do i=...
  !$omp parallel
    ...
  !$omp end parallel
end do

! Exec mode: Generic.
! Trip count: Set.
!$omp target teams distribute
do i=...
end do

! Exec mode: SPMD.
! Trip count: Not set.
!$omp target parallel do
do i=...
end do

! Exec mode: Generic.
! Trip count: Not set.
!$omp target
  ...
!$omp end target
```

For the split `target teams distribute + parallel do` case, clang
produces a Generic kernel which gets promoted to Generic-SPMD by the
openmp-opt pass. We can't currently replicate that behavior in flang
because our codegen for these constructs results in the introduction of
calls to the `kmpc_distribute_static_loop` family of functions, instead
of `kmpc_distribute_static_init`, which currently prevent promotion of
the kernel to Generic-SPMD.

For the time being, instead of relying on the openmp-opt pass, we look
at the MLIR representation to find the Generic-SPMD pattern and directly
tag the kernel as such during codegen. This is what we were already
doing, but incorrectly matching other kinds of kernels as such in the
process.
2025-04-03 15:41:00 +01:00
Sergio Afonso
18dd299fb1
[Flang][MLIR][OpenMP] Host-evaluation of omp.loop bounds (#133908)
This patch updates Flang lowering and kernel flags identification in
MLIR so that loop bounds on `target teams loop` constructs are evaluated
on the host, making the trip count available to the corresponding
`__tgt_target_kernel` call emitted for the target region.

This is necessary in order to properly execute these constructs as
`target teams distribute parallel do`.

Co-authored-by: Kareem Ergawy <kareem.ergawy@amd.com>
2025-04-03 15:06:19 +01:00
Sergio Afonso
b231f6f862
[MLIR][OpenMP] Improve omp.map.info verification (#132066)
This patch makes the `map_type` and `map_capture_type` arguments of the
`omp.map.info` operation required, which was already an invariant being
verified by its users via `verifyMapClause()`. This makes it clearer, as
getters no longer return misleading `std::optional` values.

Checks for the `mapper_id` argument are moved to a verifier for the
operation, rather than being checked by users.

Functionally NFC, but not marked as such due to a reordering of
arguments in the assembly format of `omp.map.info`.
2025-03-20 15:48:45 +00:00
Sergio Afonso
72b8744aa5
[MLIR][OpenMP] Reduce overhead of target compilation (#130945)
This patch avoids calling `TargetOp::getInnermostCapturedOmpOp` multiple
times during initialization of default and runtime target attributes in
MLIR to LLVM IR translation of `omp.target` operations. This is a
potentially expensive operation, so this change should help keep compile
times lower.
2025-03-14 15:18:32 +00:00
Sergio Afonso
237a910819
[MLIR][OpenMP] Remove the ReductionClauseInterface, NFC (#130978)
This patch removes the `ReductionClauseInterface` and all definitions of
its associated `getAllReductionVars` method.

The method mandated by this interface is not used anywhere and the
conflicts its definition produces when multiple reduction clauses are
present in an operation result in a more convoluted operation
definition, so it seems better to remove it and only add something like
this if there's a clear advantage to it.
2025-03-13 14:50:23 +00:00
Sergio Afonso
032f83b743
[MLIR][OpenMP] Enable BlockArgOpenMPOpInterface accessing operands (#130769)
This patch makes additions to the `BlockArgOpenMPOpInterface` to
simplify its use by letting it handle the matching between operands and
their associated entry block arguments. Most significantly, the
following is now possible:

```c++
SmallVector<std::pair<Value, BlockArgument>> pairs;
cast<BlockArgOpenMPOpInterface>(op).getBlockArgsPairs(pairs);
for (auto [var, arg] : pairs) {
  // var points to the operand (outside value) and arg points to the entry
  // block argument associated to that value.
}
```

This is achieved by making the interface define and use `getXyzVars()`
methods, which by default return empty `OperandRange`s and are overriden
by getters automatically produced for the `Variadic<...> $xyz_vars`
tablegen argument of the corresponding clause. These definitions can
then be simplified, since they no longer need to manually define
`numXyzBlockArgs` functions as a result.

A side-effect of this is that all ops implementing this interface will
now publicly define `getXyzVars()` functions for all entry block
argument-generating clauses, even if they don't actually accept all
clauses. However, these would just return empty ranges, so it shouldn't
cause issues.

This change uncovered some incorrect definitions of class declarations
related to the `ReductionClauseInterface`, and the `OpenMP_DetachClause`
incorrectly implementing the `BlockArgOpenMPOpInterface`, so these
issues are also addressed.
2025-03-12 11:50:12 +00:00
Krzysztof Parzyszek
d67947162f
[flang][OpenMP] Implement HAS_DEVICE_ADDR clause (#128568)
The HAS_DEVICE_ADDR indicates that the object(s) listed exists at an
address that is a valid device address. Specifically,
`has_device_addr(x)` means that (in C/C++ terms) `&x` is a device
address.

When entering a target region, `x` does not need to be allocated on the
device, or have its contents copied over (in the absence of additional
mapping clauses). Passing its address verbatim to the region for use is
sufficient, and is the intended goal of the clause.

Some Fortran objects use descriptors in their in-memory representation.
If `x` had a descriptor, both the descriptor and the contents of `x`
would be located in the device memory. However, the descriptors are
managed by the compiler, and can be regenerated at various points as
needed. The address of the effective descriptor may change, hence it's
not safe to pass the address of the descriptor to the target region.
Instead, the descriptor itself is always copied, but for objects like
`x`, no further mapping takes place (as this keeps the storage pointer
in the descriptor unchanged).

---------

Co-authored-by: Sergio Afonso <safonsof@amd.com>
2025-03-10 08:11:01 -05:00
agozillon
f1178815d2
[Flang][OpenMP][MLIR] Implement close, present and ompx_hold modifiers for Flang maps (#129586)
This PR adds an initial implementation for the map modifiers close,
present and ompx_hold, primarily just required adding the appropriate
map type flags to the map type bits. In the case of ompx_hold it
required adding the map type to the OpenMP dialect. Close has a bit of a
problem when utilised with the ALWAYS map type on descriptors, so it is
likely we'll have to make sure close and always are not applied to the
descriptor simultaneously in the future when we apply always to the
descriptors to facilitate movement of descriptor information to device
for consistency, however, we may find an alternative to this with
further investigation. For the moment, it is a TODO/Note to keep track
of it.
2025-03-07 22:22:30 +01:00
Kaviya Rajendiran
d39f4a1980
[MLIR][OpenMP]Add prescriptiveness-modifier support to granularity clauses of taskloop construct (#128477)
Added modifier(strict) support to the granularity(grainsize and num_tasks) clauses of taskloop construct.
2025-02-27 21:56:32 +05:30