Skip to content

Commit c2c4313

Browse files
committed
[OpenMP] Fix bug 50022
Bug 50022 [0] reports target nowait fails in certain case, which is added in this patch. The root cause of the failure is, when the second task is created, its parent's `td_incomplete_child_tasks` will not be incremented because there is no parallel region here thus its team is serialized. Therefore, when the initial thread is waiting for its unfinished children tasks, it thought there is only one, the first task, because it is hidden helper task, so it is tracked. The second task will only be pushed to the queue when the first task is finished. However, when the first task finishes, it first decrements the counter of its parent, and then release dependences. Once the counter is decremented, the thread will move on because its counter is reset, but actually, the second task has not been executed at all. As a result, since in this case, the main function finishes, then `libomp` starts to destroy. When the second task is pushed somewhere, all some of the structures might already have already been destroyed, then anything could happen. This patch simply moves `__kmp_release_deps` ahead of decrement of the counter. In this way, we can make sure that the initial thread is aware of the existence of another task(s) so it will not move on. In addition, in order to tackle dependence chain starting with hidden helper thread, when hidden helper task is encountered, we force the task to release dependences. Reference: [0] https://bugs.llvm.org/show_bug.cgi?id=50022 Reviewed By: AndreyChurbanov Differential Revision: https://reviews.llvm.org/D106519
1 parent e1dedec commit c2c4313

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// RUN: %libomptarget-compilexx-and-run-generic
2+
3+
#include <cassert>
4+
#include <iostream>
5+
#include <stdexcept>
6+
7+
int main(int argc, char *argv[]) {
8+
int a = 0;
9+
std::cout << "outside a = " << a << " addr " << &a << std::endl;
10+
#pragma omp target map(tofrom : a) depend(out : a) nowait
11+
{
12+
int sum = 0;
13+
for (int i = 0; i < 100000; i++)
14+
sum++;
15+
a = 1;
16+
}
17+
18+
#pragma omp task depend(inout : a) shared(a)
19+
{
20+
std::cout << "a = " << a << " addr " << &a << std::endl;
21+
if (a != 1)
22+
throw std::runtime_error("wrong result!");
23+
a = 2;
24+
}
25+
26+
#pragma omp task depend(inout : a) shared(a)
27+
{
28+
std::cout << "a = " << a << " addr " << &a << std::endl;
29+
if (a != 2)
30+
throw std::runtime_error("wrong result!");
31+
a = 3;
32+
}
33+
34+
#pragma omp taskwait
35+
36+
assert(a == 3 && "wrong result!");
37+
38+
return 0;
39+
}

openmp/runtime/src/kmp_tasking.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -940,16 +940,17 @@ static void __kmp_task_finish(kmp_int32 gtid, kmp_task_t *task,
940940
if (!(taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser) ||
941941
taskdata->td_flags.detachable == TASK_DETACHABLE ||
942942
taskdata->td_flags.hidden_helper) {
943+
__kmp_release_deps(gtid, taskdata);
943944
// Predecrement simulated by "- 1" calculation
944945
children =
945946
KMP_ATOMIC_DEC(&taskdata->td_parent->td_incomplete_child_tasks) - 1;
946947
KMP_DEBUG_ASSERT(children >= 0);
947948
if (taskdata->td_taskgroup)
948949
KMP_ATOMIC_DEC(&taskdata->td_taskgroup->count);
949-
__kmp_release_deps(gtid, taskdata);
950-
} else if (task_team && task_team->tt.tt_found_proxy_tasks) {
951-
// if we found proxy tasks there could exist a dependency chain
952-
// with the proxy task as origin
950+
} else if (task_team && (task_team->tt.tt_found_proxy_tasks ||
951+
task_team->tt.tt_hidden_helper_task_encountered)) {
952+
// if we found proxy or hidden helper tasks there could exist a dependency
953+
// chain with the proxy task as origin
953954
__kmp_release_deps(gtid, taskdata);
954955
}
955956
// td_flags.executing must be marked as 0 after __kmp_release_deps has been

0 commit comments

Comments
 (0)