[Openmp-commits] [PATCH] D111905: [OpenMP][deviceRTLs] Fix wrong return value of `__kmpc_is_spmd_exec_mode`

Shilei Tian via Phabricator via Openmp-commits openmp-commits at lists.llvm.org
Fri Oct 15 12:33:39 PDT 2021

tianshilei1992 created this revision.
tianshilei1992 added reviewers: carlo.bertolli, JonChesterfield, jdoerfert.
Herald added subscribers: guansong, yaxunl.
tianshilei1992 requested review of this revision.
Herald added subscribers: openmp-commits, sstefan1.
Herald added a project: OpenMP.

D110279 <https://reviews.llvm.org/D110279> introduced a bug to the device runtime. In `__kmpc_parallel_51`, we detect
whether we are already in parallel region by `__kmpc_parallel_level() > __kmpc_is_spmd_exec_mode()`.
It is based on the assumption that:

- In SPMD mode, parallel level is initialized to 1.
- In generic mode, parallel level is initialized to 0.
- `__kmpc_is_spmd_exec_mode` returns `1` for SPMD mode, 0 otherwise.

Because the return value type of `__kmpc_is_spmd_exec_mode` is `int8_t`, there
was an implicit cast from `bool` to `int8_t`. We can make sure it is either 0 or
1 since C++14. In D110279 <https://reviews.llvm.org/D110279>, the return value is the result of an `and` operation,
which is 2 in SPMD mode. This breaks the assumption in `__kmpc_parallel_51`.

  rG LLVM Github Monorepo



Index: openmp/libomptarget/deviceRTLs/common/src/omptarget.cu
--- openmp/libomptarget/deviceRTLs/common/src/omptarget.cu
+++ openmp/libomptarget/deviceRTLs/common/src/omptarget.cu
@@ -161,7 +161,7 @@
 // Return true if the current target region is executed in SPMD mode.
 EXTERN int8_t __kmpc_is_spmd_exec_mode() {
-  return execution_param & OMP_TGT_EXEC_MODE_SPMD;
+  return (execution_param & OMP_TGT_EXEC_MODE_SPMD) == OMP_TGT_EXEC_MODE_SPMD;
 EXTERN int8_t __kmpc_is_generic_main_thread(kmp_int32 Tid) {

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D111905.380078.patch
Type: text/x-patch
Size: 584 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/openmp-commits/attachments/20211015/81337427/attachment.bin>

More information about the Openmp-commits mailing list