I am posting this RFC as per @kiranchandramohan’s feedback on my PR.
Goal
The goal of this RFC is to get feedback on the approach I have used to implement translation of the depend
clause on OpenMP offloading directives (omp.target
, omp.target_enter_data
, omp.target_exit_data
and omp.target_update_data
)
Background
A few weeks ago, I added support for the depend
clause on OpenMP target directives. Here are the relevant PRs
- #81081 - Support in MLIR for the
depend
clause in OpenMP target offloading MLIR ops - #81601 - Lowering the
depend
clause into MLIR in flang
This is needed so that we can use the depend
clause in Fortran.
Translation to LLVMIR
The next step in compilation is to translate this into LLVMIR. In Clang, this is done by enclosing the offloading call (target
directive) inside a new OpenMP task when lowering to LLVMIR. The dependencies are then associated with the newly created task
. This is convenient to implement and also consistent with the OpenMP standard.
From the 5.2 spec -
The target construct generates a target task. The generated task region encloses the target region.
If a depend clause is present, it is associated with the target task
High-level abstraction and progressive lowering benefit of MLIR
The obvious choice to implement this translation was to move Clang’s implementation into OpenMPIRBuilder
. However, I believe this transformation is a higher level problem that MLIR is well suited to solve. So, I implemented a transformation that converts a target offloading omp
MLIR op (eg. omp.target
) with depend
into an omp.task
with the same depend
clause. The new omp.task
now encloses the original omp.target
without the depend
clause on it.
omp.target depend(..) {
omp.terminator
}
is transformed into
omp.task depend(..) {
omp.target {
omp.terminator
}
omp.terminator
}
I have two PRs that are alternatives to each other open for this.
- The first approach is an MLIR pass that does the above transformation -
#83966. - The second approach is to do the transformation ‘on-the-fly’ as we lowering to FIR in flang - #85130
The benefit of the first approach is that it is high-level language-agnostic while the second approach doesn’t require a full pass over the code and does the right transformation early while no other changes in MLIR are needed.
I would love to hear thoughts on this especially from the point of view of not going the OpenMPIRBuilder
route as I believe, like I said earlier, this transformation is more suited to MLIR.