[clang] [Clang][OpenMP] Emit untied tasks as tied on the device (PR #247736)
Julian Brown via cfe-commits
cfe-commits at lists.llvm.org
Tue Dec 1 09:55:43 PST 2026
https://github.com/jtb20 created https://github.com/llvm/llvm-project/pull/247736
The device runtime executes every task immediately on the encountering
thread and is not prepared to handle untied tasks. Furthermore, untied
tasks could not be resumed by another thread on the device anyway.
This patch emits untied tasks as tied on the device. This is conforming
(untied permits migration, but does not require it). Host behaviour
is unchanged.
Without this, we get a double free in the device runtime (detected on
some targets, ignored on others) when a task re-enqueues itself at a
task scheduling point. This fixes the NVPTX failure on #245784.
Testcase added.
Assisted-By: Claude
>From b1e5f56f374045a02a7f99aa549cf796673cfd42 Mon Sep 17 00:00:00 2001
From: Julian Brown <julian.brown at amd.com>
Date: Tue, 1 Dec 2026 06:08:47 -0600
Subject: [PATCH] [Clang][OpenMP] Emit untied tasks as tied on the device
The device runtime executes every task immediately on the encountering
thread and is not prepared to handle untied tasks. Furthermore, untied
tasks could not be resumed by another thread on the device anyway.
This patch emits untied tasks as tied on the device. This is conforming
(untied permits migration, but does not require it). Host behaviour
is unchanged.
Without this, we get a double free in the device runtime (detected on
some targets, ignored on others) when a task re-enqueues itself at a
task scheduling point. This fixes the NVPTX failure on #245784.
Testcase added.
Assisted-By: Claude
---
clang/lib/CodeGen/CGStmtOpenMP.cpp | 7 ++-
.../test/OpenMP/target_task_untied_codegen.c | 49 +++++++++++++++++++
2 files changed, 54 insertions(+), 2 deletions(-)
create mode 100644 clang/test/OpenMP/target_task_untied_codegen.c
diff --git a/clang/lib/CodeGen/CGStmtOpenMP.cpp b/clang/lib/CodeGen/CGStmtOpenMP.cpp
index 362f583424bc32..41b1677b15615f 100644
--- a/clang/lib/CodeGen/CGStmtOpenMP.cpp
+++ b/clang/lib/CodeGen/CGStmtOpenMP.cpp
@@ -6182,8 +6182,11 @@ void CodeGenFunction::EmitOMPTaskDirective(const OMPTaskDirective &S) {
}
OMPTaskDataTy Data;
- // Check if we should emit tied or untied task.
- Data.Tied = !S.getSingleClause<OMPUntiedClause>();
+ // Check if we should emit tied or untied task. On the device, only tied tasks
+ // are supported and the runtime executes the task immediately on the
+ // encountering thread, so choose tied.
+ Data.Tied = !S.getSingleClause<OMPUntiedClause>() ||
+ CGM.getLangOpts().OpenMPIsTargetDevice;
auto &&BodyGen = [CS](CodeGenFunction &CGF, PrePostActionTy &) {
CGF.EmitStmt(CS->getCapturedStmt());
};
diff --git a/clang/test/OpenMP/target_task_untied_codegen.c b/clang/test/OpenMP/target_task_untied_codegen.c
new file mode 100644
index 00000000000000..f2b214ff14f865
--- /dev/null
+++ b/clang/test/OpenMP/target_task_untied_codegen.c
@@ -0,0 +1,49 @@
+// REQUIRES: amdgpu-registered-target, nvptx-registered-target
+
+// RUN: %clang_cc1 -fopenmp -triple x86_64-unknown-unknown -fopenmp-targets=amdgcn-amd-amdhsa -emit-llvm %s -o - | FileCheck %s --check-prefix=HOST
+// RUN: %clang_cc1 -fopenmp -triple x86_64-unknown-unknown -fopenmp-targets=amdgcn-amd-amdhsa -emit-llvm-bc %s -o %t-amdgcn-host.bc
+// RUN: %clang_cc1 -fopenmp -triple amdgcn-amd-amdhsa -fopenmp-targets=amdgcn-amd-amdhsa -emit-llvm %s -fopenmp-is-target-device -fopenmp-host-ir-file-path %t-amdgcn-host.bc -o - | FileCheck %s --check-prefix=DEVICE
+// RUN: %clang_cc1 -fopenmp -triple x86_64-unknown-unknown -fopenmp-targets=nvptx64-nvidia-cuda -emit-llvm-bc %s -o %t-nvptx-host.bc
+// RUN: %clang_cc1 -fopenmp -triple nvptx64-nvidia-cuda -fopenmp-targets=nvptx64-nvidia-cuda -emit-llvm %s -fopenmp-is-target-device -fopenmp-host-ir-file-path %t-nvptx-host.bc -o - | FileCheck %s --check-prefix=DEVICE
+// expected-no-diagnostics
+
+// The device runtime executes a task immediately on the encountering thread,
+// and an untied task re-enqueues itself at each task scheduling point, which
+// the device runtime would run as a nested task and free twice. Check that an
+// untied task in a target region is emitted as tied on the device: it is
+// allocated with the tied flag (1), and its body neither switches on its part
+// id nor calls __kmpc_omp_task. The host keeps the untied codegen.
+
+void use(int);
+
+void foo(void) {
+#pragma omp target
+ {
+#pragma omp task untied
+ {
+ int local = 42;
+ use(local);
+#pragma omp taskyield
+ use(local);
+ }
+ }
+}
+
+// The host version of the target region allocates the task untied (flags 0),
+// and the task re-enqueues itself.
+// HOST-LABEL: define internal void @__omp_offloading_{{.*}}_foo_l{{[0-9]+}}(
+// HOST: call ptr @__kmpc_omp_task_alloc(ptr @{{.*}}, i32 %{{.*}}, i32 0,
+// HOST-LABEL: define internal i32 @.omp_task_entry.(
+// HOST: switch i32 %{{.*}}, label %.untied.done.
+// HOST: call i32 @__kmpc_omp_task(
+// HOST: call i32 @__kmpc_omp_taskyield(
+// HOST: call i32 @__kmpc_omp_task(
+
+// DEVICE-LABEL: define weak_odr protected {{.*}}void @__omp_offloading_{{.*}}_foo_l{{[0-9]+}}(
+// DEVICE: call {{.*}}ptr @__kmpc_omp_task_alloc(ptr {{.*}}, i32 %{{.*}}, i32 1,
+// DEVICE-LABEL: define internal {{.*}}i32 @"_omp_task_entry$"(
+// DEVICE-NOT: .untied.
+// DEVICE-NOT: call {{.*}}@__kmpc_omp_task(
+// DEVICE: call {{.*}}@__kmpc_omp_taskyield(
+// DEVICE-NOT: call {{.*}}@__kmpc_omp_task(
+// DEVICE: ret i32 0
More information about the cfe-commits
mailing list