Skip to content

Commit 4aeb31f

Browse files
committed
[OpenMP] Address __kmp_dist_for_static_init issue
This patch attempts to provide a fix for an issue that appears when the `__kmp_dist_for_static_init` function is called from a serialized team. This is triggered by code generated by flang for `distribute parallel do` constructs whenever an `if` clause for the `parallel` leaf construct is present. This results in the introduction of a call to `__kmpc_fork_call_if` in place of `__kmpc_fork_call`. When it evaluates to `false`, it defers execution to `__kmp_serialized_parallel`, which creates a new serial team that is picked up by `__kmp_dist_for_static_init`, resulting in an incorrect `team` pointer that causes the `nteams == (kmp_uint32)team->t.t_parent->t.t_nproc` assertion to fail. The sequence of calls replicating this issue can be summarized as: - __kmpc_fork_teams - __kmpc_fork_call_if - __kmpc_dist_for_static_init_* Since I am not familiar with the implementation of the OpenMP runtime, it is possible that the previous sequence of calls is incorrect, or that the bug can be better fixed in another way, so I am open to discussing this. The following Fortran program can be compiled with flang to show the issue: ```f90 ! Compile and run: flang -fopenmp test.f90 -o test && ./test ! Check LLVM IR: flang -fc1 -emit-llvm -fopenmp test.f90 -o - program main implicit none integer, parameter :: n = 10 integer :: i, idx(n) !$omp teams !$omp distribute parallel do if(.false.) do i=1,n idx(i) = i end do !$omp end teams print *, idx end program ```
1 parent 3dc7991 commit 4aeb31f

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

openmp/runtime/src/kmp_sched.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,10 @@ static void __kmp_dist_for_static_init(ident_t *loc, kmp_int32 gtid,
542542
nth = th->th.th_team_nproc;
543543
team = th->th.th_team;
544544
KMP_DEBUG_ASSERT(th->th.th_teams_microtask); // we are in the teams construct
545+
// skip optional serialized teams to prevent this being called after
546+
// __kmp_serialized_parallel from using the wrong teams information
547+
while (team->t.t_serialized)
548+
team = team->t.t_parent;
545549
nteams = th->th.th_teams_size.nteams;
546550
team_id = team->t.t_master_tid;
547551
KMP_DEBUG_ASSERT(nteams == (kmp_uint32)team->t.t_parent->t.t_nproc);

0 commit comments

Comments
 (0)