[clang] [Clang][OpenMP] Keep untied task locals in the task on the device (PR #248062)
Julian Brown via cfe-commits
cfe-commits at lists.llvm.org
Fri Dec 4 03:14:50 PST 2026
https://github.com/jtb20 updated https://github.com/llvm/llvm-project/pull/248062
>From 1813591015d352cd08b7b7093503b5b58e896680 Mon Sep 17 00:00:00 2001
From: Julian Brown <julian.brown at amd.com>
Date: Wed, 2 Dec 2026 06:32:33 -0600
Subject: [PATCH 1/2] [Clang][OpenMP] Keep untied task locals in the task on
the device
An untied task returns at each task scheduling point and is called again
to resume, so its local variables are kept in the task rather than on the
stack. CGOpenMPRuntimeGPU overrides getAddressOfLocalVariable and does not
consult the untied task locals that the base class looks up, so on the
device these variables were emitted as allocas and lost their values when
the task was resumed.
Look up the untied task locals first, as the base class does, for
variables that are not handled by an allocate directive.
Assisted-By: Claude
---
clang/lib/CodeGen/CGOpenMPRuntimeGPU.cpp | 8 +++++
.../target_task_untied_locals_codegen.c | 35 +++++++++++++++++++
2 files changed, 43 insertions(+)
create mode 100644 clang/test/OpenMP/target_task_untied_locals_codegen.c
diff --git a/clang/lib/CodeGen/CGOpenMPRuntimeGPU.cpp b/clang/lib/CodeGen/CGOpenMPRuntimeGPU.cpp
index bf8cd97e10166f..94796af70f522d 100644
--- a/clang/lib/CodeGen/CGOpenMPRuntimeGPU.cpp
+++ b/clang/lib/CodeGen/CGOpenMPRuntimeGPU.cpp
@@ -2211,6 +2211,14 @@ void CGOpenMPRuntimeGPU::emitFunctionProlog(CodeGenFunction &CGF,
Address CGOpenMPRuntimeGPU::getAddressOfLocalVariable(CodeGenFunction &CGF,
const VarDecl *VD) {
+ // A local variable of an untied task is kept in the task, so that it
+ // survives the task returning at a task scheduling point and being resumed.
+ if (VD && !VD->hasAttr<OMPAllocateDeclAttr>()) {
+ Address UntiedAddr = CGOpenMPRuntime::getAddressOfLocalVariable(CGF, VD);
+ if (UntiedAddr.isValid())
+ return UntiedAddr;
+ }
+
if (VD && VD->hasAttr<OMPAllocateDeclAttr>()) {
const auto *A = VD->getAttr<OMPAllocateDeclAttr>();
auto AS = LangAS::Default;
diff --git a/clang/test/OpenMP/target_task_untied_locals_codegen.c b/clang/test/OpenMP/target_task_untied_locals_codegen.c
new file mode 100644
index 00000000000000..162255a338eaa7
--- /dev/null
+++ b/clang/test/OpenMP/target_task_untied_locals_codegen.c
@@ -0,0 +1,35 @@
+// REQUIRES: amdgpu-registered-target, nvptx-registered-target
+
+// 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
+// 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
+// expected-no-diagnostics
+
+// An untied task returns at each task scheduling point and is called again to
+// resume, so its local variables are kept in the task rather than on the stack.
+// Check that this is also done on the device.
+
+void use(int);
+
+void foo(void) {
+#pragma omp target
+ {
+#pragma omp task untied
+ {
+ int local = 42;
+#pragma omp taskyield
+ use(local);
+ }
+ }
+}
+
+// CHECK-LABEL: define internal {{.*}}i32 @"_omp_task_entry$"(
+// CHECK-NOT: %local{{.*}} = alloca
+// CHECK: [[LOCAL_PTR_ADDR:%.local.ptr.addr[^ ]*]] = alloca ptr
+// CHECK-NOT: %local{{.*}} = alloca
+// CHECK: [[LOCAL:%.*]] = load ptr, ptr{{( addrspace\(5\))?}} [[LOCAL_PTR_ADDR]]
+// CHECK: store i32 42, ptr [[LOCAL]]
+// CHECK: call {{.*}}@__kmpc_omp_taskyield(
+// CHECK: [[VAL:%.*]] = load i32, ptr [[LOCAL]]
+// CHECK-NEXT: call {{.*}}void @use(i32 noundef [[VAL]])
>From b6db3c69c7ce3136be79839aac19901bfd890924 Mon Sep 17 00:00:00 2001
From: Julian Brown <julian.brown at amd.com>
Date: Thu, 3 Dec 2026 13:37:44 -0600
Subject: [PATCH 2/2] Address review comments
Handle a null declaration up front, and look for an untied task's local
after the allocate directive has been handled.
Assisted-By: Claude
---
clang/lib/CodeGen/CGOpenMPRuntimeGPU.cpp | 17 +++++++++--------
1 file changed, 9 insertions(+), 8 deletions(-)
diff --git a/clang/lib/CodeGen/CGOpenMPRuntimeGPU.cpp b/clang/lib/CodeGen/CGOpenMPRuntimeGPU.cpp
index 94796af70f522d..47b4ea700f4709 100644
--- a/clang/lib/CodeGen/CGOpenMPRuntimeGPU.cpp
+++ b/clang/lib/CodeGen/CGOpenMPRuntimeGPU.cpp
@@ -2211,15 +2211,10 @@ void CGOpenMPRuntimeGPU::emitFunctionProlog(CodeGenFunction &CGF,
Address CGOpenMPRuntimeGPU::getAddressOfLocalVariable(CodeGenFunction &CGF,
const VarDecl *VD) {
- // A local variable of an untied task is kept in the task, so that it
- // survives the task returning at a task scheduling point and being resumed.
- if (VD && !VD->hasAttr<OMPAllocateDeclAttr>()) {
- Address UntiedAddr = CGOpenMPRuntime::getAddressOfLocalVariable(CGF, VD);
- if (UntiedAddr.isValid())
- return UntiedAddr;
- }
+ if (!VD)
+ return Address::invalid();
- if (VD && VD->hasAttr<OMPAllocateDeclAttr>()) {
+ if (VD->hasAttr<OMPAllocateDeclAttr>()) {
const auto *A = VD->getAttr<OMPAllocateDeclAttr>();
auto AS = LangAS::Default;
switch (A->getAllocatorType()) {
@@ -2259,6 +2254,12 @@ Address CGOpenMPRuntimeGPU::getAddressOfLocalVariable(CodeGenFunction &CGF,
VarTy, Align);
}
+ // A local variable of an untied task is kept in the task, so that it
+ // survives the task returning at a task scheduling point and being resumed.
+ if (Address UntiedAddr = CGOpenMPRuntime::getAddressOfLocalVariable(CGF, VD);
+ UntiedAddr.isValid())
+ return UntiedAddr;
+
if (getDataSharingMode() != CGOpenMPRuntimeGPU::DS_Generic)
return Address::invalid();
More information about the cfe-commits
mailing list