[Openmp-commits] [openmp] [OpenMP] Copy GNU undeferred task data in task context (PR #237136)
via Openmp-commits
openmp-commits at lists.llvm.org
Fri Oct 30 01:06:25 PDT 2026
https://github.com/SmaCow666 created https://github.com/llvm/llvm-project/pull/237136
## Summary
Preserve the GNU task data-copy callback for undeferred (`if(0)`) tasks,
construct the copied environment in the task's execution context, and pass that
environment to the task body.
The current `GOMP_task` path sets `arg_size` to zero for every undeferred task.
It therefore skips `copy_func` and calls `func(data)`. For a non-trivial C++
`firstprivate` object, the caller's source environment may contain a pointer to
the original object while the task body expects the object constructed inline
in the destination environment. Passing the source environment is incorrect.
## Change
- Retain the existing direct-data fast path when no copy callback is needed.
- Allocate and align the destination for an undeferred task that needs copying.
- Wait for dependencies and enter the undeferred task before invoking its copy
callback, matching the ordering of libgomp's undeferred path.
- Execute the task body with the copied environment, then use the existing task
completion path.
- Keep deferred-task copying at its existing point before scheduling.
The copy callback can observe task context, for example through `omp_in_final()`.
Moving only the allocation/copy while still invoking the constructor in the
parent context would leave that behavior incorrect.
## Regression
The new Linux test calls the GNU compatibility entry directly so it also runs
with Clang. It covers:
- A non-trivial firstprivate source/destination layout and 128-byte alignment.
- Exactly one copy construction and destruction, with the original unchanged.
- Copying in a final undeferred task's context.
- The deferred-copy control.
- Direct-data identity without a copy callback and the zero-sized no-data path.
- One- and two-thread teams.
The unpatched runtime fails with `GOMP_IF0_COPY errors=8`; the patched runtime
passes with `errors=0`. The test checks construction before interpreting the
copied layout, so the unpatched case fails without relying on an invalid read.
## Validation
Validated against LLVM main revision
`c97f503e1f1f4c81c04ac20344405aa5192ca6f1`:
- Complete RV64GC/LP64D and x86-64 Release runtime builds, OMPT enabled.
- Complete RV64 Debug build, assertions enabled and OMPT disabled.
- The proposed test executed using that revision's unchanged upstream lit
configuration: baseline FAIL and patched PASS on x86-64 and RV64/QEMU.
- Ten existing neighboring tasking regressions pass on x86-64.
- A separate GNU-ABI matrix checks 750 cases per execution: lengths, alignment,
tied/untied and final flags, dependency-wait/copy/begin/complete ordering,
direct-data and deferred-copy controls. GCC and Clang callers pass on both
targets; the RV64 Debug variant also passes.
- GCC C++ callers using actual `#pragma omp task if(0) firstprivate` pass.
- Three x86-64 caller/callback programs pass ASan and UBSan checks; the runtime
library itself was not sanitizer-instrumented. Leak detection was disabled.
- The same proposed regression also passes with the installed GNU libgomp.
The full upstream suite was not run; FileCheck was unavailable in this environment.
This is a GNU-compatibility correctness fix validated on RISC-V and x86-64 Linux.
The validation was repeated after refreshing main from the previous tested
revision. The upstream update changed taskgraph-related runtime code and the
Wasm affinity guard; the `GOMP_task` source and this focused fix were unchanged.
Both unpatched controls and patched runtimes were rebuilt on the new revision.
The earlier copy-only negative control remains a separately identified
historical build, not the new-main baseline.
>From ca245eee2df8695c5e777f7d07474ca45306b9d5 Mon Sep 17 00:00:00 2001
From: XuhuiNiu <uiniu666 at gmail.com>
Date: Fri, 30 Oct 2026 16:04:37 +0800
Subject: [PATCH] [OpenMP] Copy GNU undeferred task data in task context
Keep the copy callback and aligned environment for undeferred tasks.
Construct after dependency waiting and begin_if0, then invoke the body
with the copied data. Preserve the no-callback direct-data path and
existing deferred-task copying.
Add a Clang-runnable GNU entry regression for construction, lifetime,
alignment, final context and no-copy controls.
---
openmp/runtime/src/kmp_gsupport.cpp | 22 +++-
.../test/tasking/gomp_task_if0_copy.cpp | 111 ++++++++++++++++++
2 files changed, 127 insertions(+), 6 deletions(-)
create mode 100644 openmp/runtime/test/tasking/gomp_task_if0_copy.cpp
diff --git a/openmp/runtime/src/kmp_gsupport.cpp b/openmp/runtime/src/kmp_gsupport.cpp
index be95e0f3de6e2b..d002a9d1de26d3 100644
--- a/openmp/runtime/src/kmp_gsupport.cpp
+++ b/openmp/runtime/src/kmp_gsupport.cpp
@@ -1245,7 +1245,7 @@ void KMP_EXPAND_NAME(KMP_API_NAME_GOMP_TASK)(void (*func)(void *), void *data,
input_flags->native = 1;
// __kmp_task_alloc() sets up all other flags
- if (!if_cond) {
+ if (!if_cond && !copy_func) {
arg_size = 0;
}
@@ -1260,10 +1260,15 @@ void KMP_EXPAND_NAME(KMP_API_NAME_GOMP_TASK)(void (*func)(void *), void *data,
}
// else error??
- if (copy_func) {
- (*copy_func)(task->shareds, data);
- } else {
- KMP_MEMCPY(task->shareds, data, arg_size);
+ // Deferred tasks need their data environment before being scheduled.
+ // For undeferred tasks, construct it after waiting for dependencies and
+ // entering the task context below.
+ if (if_cond) {
+ if (copy_func) {
+ (*copy_func)(task->shareds, data);
+ } else {
+ KMP_MEMCPY(task->shareds, data, arg_size);
+ }
}
}
@@ -1318,7 +1323,12 @@ void KMP_EXPAND_NAME(KMP_API_NAME_GOMP_TASK)(void (*func)(void *), void *data,
}
__kmpc_omp_task_begin_if0(&loc, gtid, task);
- func(data);
+ if (copy_func && arg_size > 0) {
+ (*copy_func)(task->shareds, data);
+ func(task->shareds);
+ } else {
+ func(data);
+ }
__kmpc_omp_task_complete_if0(&loc, gtid, task);
#if OMPT_SUPPORT
diff --git a/openmp/runtime/test/tasking/gomp_task_if0_copy.cpp b/openmp/runtime/test/tasking/gomp_task_if0_copy.cpp
new file mode 100644
index 00000000000000..6e8a8219d79722
--- /dev/null
+++ b/openmp/runtime/test/tasking/gomp_task_if0_copy.cpp
@@ -0,0 +1,111 @@
+// RUN: %libomp-cxx-compile-and-run
+// REQUIRES: linux
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+
+#include <atomic>
+#include <cstdint>
+#include <cstdio>
+#include <new>
+#include <omp.h>
+
+// Call the GNU compatibility entry directly so this regression also runs with
+// Clang, whose task lowering otherwise exercises the native entry points.
+extern "C" void GOMP_task(void (*)(void *), void *, void (*)(void *, void *),
+ long, long, bool, unsigned, void **);
+extern "C" void GOMP_taskwait();
+
+static std::atomic<int> Copies{0}, Destructions{0}, Errors{0};
+static int ExpectedCopyFinal;
+
+struct Value {
+ int number;
+ bool is_private;
+ explicit Value(int n) : number(n), is_private(false) {}
+ Value(const Value &other) : number(other.number + 100), is_private(true) {
+ if (omp_in_final() != ExpectedCopyFinal)
+ ++Errors;
+ ++Copies;
+ }
+ ~Value() {
+ if (is_private)
+ ++Destructions;
+ }
+};
+
+// GCC may pass a pointer to the original non-trivial firstprivate object in
+// the source environment and construct the object inline in the copied one.
+struct Source { Value *original; int *observed; };
+struct alignas(128) Copied { Value value; int *observed; };
+
+static void copy(void *destination, void *source) {
+ auto *from = static_cast<Source *>(source);
+ if (reinterpret_cast<std::uintptr_t>(destination) % alignof(Copied))
+ ++Errors;
+ new (destination) Copied{*from->original, from->observed};
+}
+
+static void run_copied(void *data) {
+ // Fail cleanly on an unfixed runtime, without interpreting the source
+ // environment as a Copied object when its constructor never ran.
+ if (Copies.load() != 1) {
+ ++Errors;
+ return;
+ }
+ auto *arg = static_cast<Copied *>(data);
+ *arg->observed = arg->value.number;
+ arg->~Copied();
+}
+
+static void check_copy(bool deferred, unsigned flags, int copy_final) {
+ Copies = 0;
+ Destructions = 0;
+ ExpectedCopyFinal = copy_final;
+ Value original(7);
+ int observed = 0;
+ Source source{&original, &observed};
+ GOMP_task(run_copied, &source, copy, sizeof(Copied), alignof(Copied),
+ deferred, flags, nullptr);
+ GOMP_taskwait();
+ if (Copies != 1 || Destructions != 1 || observed != 107 ||
+ original.number != 7)
+ ++Errors;
+}
+
+static void *ExpectedData;
+static void run_uncopied(void *data) {
+ if (data != ExpectedData)
+ ++Errors;
+ if (data)
+ ++*static_cast<int *>(data);
+}
+
+int main() {
+ omp_set_dynamic(0);
+ for (int threads = 1; threads <= 2; ++threads) {
+#pragma omp parallel num_threads(threads)
+ {
+#pragma omp single
+ {
+ if (omp_get_num_threads() != threads)
+ ++Errors;
+ check_copy(false, 0, 0);
+ // GOMP_TASK_FLAG_FINAL: the copy constructor must run in the
+ // undeferred task's context, not in its non-final parent.
+ check_copy(false, 2, 1);
+ check_copy(true, 0, 0);
+ int value = 19;
+ ExpectedData = &value;
+ GOMP_task(run_uncopied, &value, nullptr, sizeof(value), alignof(int),
+ false, 0, nullptr);
+ if (value != 20)
+ ++Errors;
+ ExpectedData = nullptr;
+ GOMP_task(run_uncopied, nullptr, nullptr, 0, 1, false, 0, nullptr);
+ }
+ }
+ }
+ std::printf("GOMP_IF0_COPY errors=%d\n", Errors.load());
+ return Errors ? 1 : 0;
+}
More information about the Openmp-commits
mailing list