[Openmp-commits] [openmp] [OpenMP] Compute taskloop chunk bounds in iteration space (PR #237557)
via Openmp-commits
openmp-commits at lists.llvm.org
Sat Oct 31 05:37:08 PDT 2026
https://github.com/SmaCow666 created https://github.com/llvm/llvm-project/pull/237557
When taskloop bounds use descending or signed coordinates, comparing an
unsigned chunk upper bound with the terminal bound can truncate a valid chunk
or retain an oversized strict tail. For example, 53 logical iterations starting
at 17 with stride -3 should form strict chunks of 10/10/10/10/10/3; the current
runtime does not execute that iteration space correctly. Increasing loops that
cross zero can also lose iterations, including without a strict modifier.
Compute the last strict chunk's length using the iteration-count deficit already
propagated through linear and recursive task creation, before mapping the chunk
into loop coordinates. Remove the unsigned coordinate clamp. Also express the
negative-stride lastprivate assertion as a modular distance, consistent with the
existing lastprivate test.
Add a runtime-ABI regression following `kmp_taskloop_5.c`. Direct ABI calls
preserve the original coordinates that a frontend may otherwise normalize. The
test checks each iteration exactly once, chunk sizes, and exactly one lastprivate
marker on the last logical iteration. It covers ascending, descending,
cross-zero, non-unit and non-aligned bounds, high unsigned coordinates, exact
and partial chunks, a zero-trip control, grainsize/num_tasks, strict/non-strict,
if(0)/if(1), and one/two threads. RUN lines force both recursive and linear
spawning with `KMP_TASKLOOP_MIN_TASKS`.
Validation against main `7a59bfc6e7ad44858258af2f0d716a56a6cf102f`:
- Complete independent x86-64 Release and assertions-enabled Debug runtimes;
GCC 11 and Clang 21 callers. The unmodified runtime fails 556 of 1,441 matrix
cases; the fixed runtime passes all cases with thresholds 1 and 64.
- The new regression fails against the baseline and passes against Release and
Debug through the current upstream lit configuration.
- Six unchanged neighboring taskloop/taskgroup tests pass against this fix and
against the separately tested GNU strict-forwarding fix and their combination.
- Complete RV64GC/LP64D runtime build and new GCC ABI regression under QEMU:
baseline fails; fixed linear and recursive configurations pass. This is
functional validation, with no performance claim.
The full upstream test suite was not run. This fixes portable runtime correctness;
RISC-V is an additional validation target.
>From 7b5f010f8ca83e5463b2f45d4205e9b62281943b Mon Sep 17 00:00:00 2001
From: XuhuiNiu <uiniu666 at gmail.com>
Date: Sat, 31 Oct 2026 20:31:15 +0800
Subject: [PATCH] [OpenMP] Compute taskloop chunk bounds in iteration space
Use the propagated iteration deficit to shorten a strict tail before
mapping its bounds into loop coordinates. Drop the unsigned coordinate
clamp, which also truncates valid descending and signed chunks outside
strict mode. Express the negative-stride lastprivate assertion using
modular distance.
Add runtime ABI coverage for linear and recursive splitting, signed and
unsigned coordinates, scheduling modes and lastprivate.
---
openmp/runtime/src/kmp_tasking.cpp | 11 +-
.../test/tasking/taskloop_signed_bounds.c | 135 ++++++++++++++++++
2 files changed, 142 insertions(+), 4 deletions(-)
create mode 100644 openmp/runtime/test/tasking/taskloop_signed_bounds.c
diff --git a/openmp/runtime/src/kmp_tasking.cpp b/openmp/runtime/src/kmp_tasking.cpp
index eae78848988d23..f7109593066571 100644
--- a/openmp/runtime/src/kmp_tasking.cpp
+++ b/openmp/runtime/src/kmp_tasking.cpp
@@ -4522,10 +4522,13 @@ void __kmp_taskloop_linear(ident_t *loc, int gtid, kmp_task_t *task,
chunk_minus_1 = grainsize;
--extras; // first extras iterations get bigger chunk (grainsize+1)
}
- upper = lower + st * chunk_minus_1;
- if (upper > *ub) {
- upper = *ub;
+ // last_chunk is a non-positive iteration-count deficit for a strict tail.
+ // Account for it before converting iteration counts to loop coordinates;
+ // ordering uint64_t bounds is not valid for descending or signed loops.
+ if (i == num_tasks - 1 && last_chunk < 0) {
+ chunk_minus_1 += last_chunk;
}
+ upper = lower + st * chunk_minus_1;
if (i == num_tasks - 1) {
// schedule the last task, set lastprivate flag if needed
if (st == 1) { // most common case
@@ -4537,7 +4540,7 @@ void __kmp_taskloop_linear(ident_t *loc, int gtid, kmp_task_t *task,
if ((kmp_uint64)st > ub_glob - upper)
lastpriv = 1;
} else { // negative loop stride
- KMP_DEBUG_ASSERT(upper + st < *ub);
+ KMP_DEBUG_ASSERT(upper - *ub < (kmp_uint64)(-st));
if (upper - ub_glob < (kmp_uint64)(-st))
lastpriv = 1;
}
diff --git a/openmp/runtime/test/tasking/taskloop_signed_bounds.c b/openmp/runtime/test/tasking/taskloop_signed_bounds.c
new file mode 100644
index 00000000000000..03066a5e6b9e27
--- /dev/null
+++ b/openmp/runtime/test/tasking/taskloop_signed_bounds.c
@@ -0,0 +1,135 @@
+// RUN: %libomp-compile-and-run
+// RUN: %libomp-compile && env KMP_TASKLOOP_MIN_TASKS=1 %libomp-run
+// RUN: %libomp-compile && env KMP_TASKLOOP_MIN_TASKS=64 %libomp-run
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+
+// Exercise the runtime ABI directly, as in kmp_taskloop_5.c. Clang normally
+// normalizes source loop coordinates, hiding these signed/descending bounds.
+#include <omp.h>
+#include <stdint.h>
+#include <stdio.h>
+
+enum { Capacity = 64 };
+struct context {
+ uint64_t start;
+ int64_t step;
+ int count, tasks, bad, last_count, last_index;
+ int seen[Capacity], sizes[Capacity];
+};
+struct task {
+ void *shareds;
+ int (*routine)(int, struct task *);
+ int part_id;
+ uint64_t lb, ub;
+ int last;
+};
+extern int __kmpc_global_thread_num(void *);
+extern struct task *__kmpc_omp_task_alloc(void *, int, int, size_t, size_t,
+ int (*)(int, struct task *));
+extern void __kmpc_taskloop_5(void *, int, struct task *, int, uint64_t *,
+ uint64_t *, int64_t, int, int, uint64_t, int, void *);
+
+static void duplicate(struct task *dest, struct task *source, int last) {
+ (void)source;
+ dest->last = last;
+}
+
+static int execute(int gtid, struct task *task) {
+ (void)gtid;
+ struct context *c = *(struct context **)task->shareds;
+ uint64_t stride = c->step > 0 ? (uint64_t)c->step : (uint64_t)-c->step;
+ uint64_t delta = c->step > 0 ? task->lb - c->start : c->start - task->lb;
+ uint64_t span = c->step > 0 ? task->ub - task->lb : task->lb - task->ub;
+ uint64_t first = delta / stride, size = span / stride + 1;
+ int id = __atomic_fetch_add(&c->tasks, 1, __ATOMIC_RELAXED);
+ // Guard before iterating: a broken runtime can supply an enormous span.
+ if (id >= Capacity || delta % stride || span % stride ||
+ first >= (uint64_t)c->count || size > (uint64_t)c->count - first) {
+ __atomic_fetch_add(&c->bad, 1, __ATOMIC_RELAXED);
+ return 0;
+ }
+ c->sizes[id] = (int)size;
+ for (uint64_t i = first; i < first + size; ++i)
+ __atomic_fetch_add(&c->seen[i], 1, __ATOMIC_RELAXED);
+ if (task->last) {
+ __atomic_fetch_add(&c->last_count, 1, __ATOMIC_RELAXED);
+ __atomic_store_n(&c->last_index, (int)(first + size - 1), __ATOMIC_RELAXED);
+ }
+ return 0;
+}
+
+static int run(uint64_t start, int64_t step, int n, int grain, int sched,
+ int strict, int if_val, int threads, int unaligned) {
+ struct context c = {.start = start, .step = step, .count = n, .last_index = -1};
+ // Inclusive terminal may be off the stride lattice. Keep signed arithmetic
+ // out of coordinate construction; the runtime ABI uses uint64_t bounds.
+ uint64_t terminal = start + (uint64_t)step * (uint64_t)(n - 1);
+ if (n && unaligned)
+ terminal += step > 0 ? 1 : (uint64_t)-1;
+ #pragma omp parallel num_threads(threads) shared(c)
+ {
+ #pragma omp single
+ {
+ int gtid = __kmpc_global_thread_num(NULL);
+ struct task *t = __kmpc_omp_task_alloc(NULL, gtid, 1, sizeof(*t),
+ sizeof(struct context *), execute);
+ *(struct context **)t->shareds = &c;
+ t->lb = start;
+ t->ub = terminal;
+ t->last = 0;
+ __kmpc_taskloop_5(NULL, gtid, t, if_val, &t->lb, &t->ub, step,
+ 0, sched, grain, strict, (void *)duplicate);
+ }
+ }
+ if (!n)
+ return c.tasks != 0 || c.last_count != 0 || c.bad != 0;
+ int tasks = sched == 2 ? (grain < n ? grain : n) :
+ strict ? (n + grain - 1) / grain : (n < grain ? 1 : n / grain);
+ int bad = c.bad || c.tasks != tasks || c.last_count != 1 || c.last_index != n - 1;
+ for (int i = 0; i < n; ++i)
+ bad |= c.seen[i] != 1;
+ int small = n / tasks, large_count = n % tasks, actual_large = 0;
+ for (int i = 0; i < c.tasks && i < Capacity; ++i) {
+ if (sched == 1 && strict) {
+ int tail = n % grain ? n % grain : grain;
+ bad |= c.sizes[i] != grain && c.sizes[i] != tail;
+ } else {
+ bad |= c.sizes[i] != small && c.sizes[i] != small + 1;
+ actual_large += c.sizes[i] == small + 1;
+ }
+ }
+ if (sched != 1 || !strict)
+ bad |= actual_large != large_count;
+ if (bad)
+ printf("bounds failure: start=%llu step=%lld n=%d grain=%d sched=%d strict=%d "
+ "if=%d threads=%d tasks=%d/%d bad=%d last=%d/%d\n",
+ (unsigned long long)start, (long long)step, n, grain, sched, strict,
+ if_val, threads, c.tasks, tasks, c.bad, c.last_count, c.last_index);
+ return !!bad;
+}
+
+int main(void) {
+ const uint64_t starts[] = {7, (uint64_t)-17, 17, 1000, (uint64_t)-200,
+ (UINT64_C(1) << 63) + 17};
+ const int64_t steps[] = {1, 1, -3, -3, 3, 3};
+ const int counts[] = {1, 10, 11, 53, 60};
+ const int grains[] = {3, 10, 64};
+ int errors = 0, cases = 0;
+ omp_set_dynamic(0);
+ for (int threads = 1; threads <= 2; ++threads)
+ for (int if_val = 0; if_val <= 1; ++if_val)
+ for (int shape = 0; shape < 6; ++shape)
+ for (int n = 0; n < 5; ++n)
+ for (int g = 0; g < 3; ++g)
+ for (int sched = 1; sched <= 2; ++sched)
+ for (int strict = 0; strict <= 1; ++strict) {
+ errors += run(starts[shape], steps[shape], counts[n], grains[g],
+ sched, strict, if_val, threads,
+ steps[shape] != 1);
+ ++cases;
+ }
+ errors += run(7, 1, 0, 10, 1, 1, 1, 2, 0);
+ ++cases;
+ printf("taskloop bounds: cases=%d errors=%d\n", cases, errors);
+ return errors != 0;
+}
More information about the Openmp-commits
mailing list