[clang] [openmp] [OpenMP] Assign lastprivate loop counter init on empty worksharing loops (PR #243086)
Amit Tiwari via cfe-commits
cfe-commits at lists.llvm.org
Thu Nov 26 23:08:01 PST 2026
https://github.com/loopacino updated https://github.com/llvm/llvm-project/pull/243086
>From 2b4e3d974b4a27cbb294acdac7e008bbd6823b17 Mon Sep 17 00:00:00 2001
From: amtiwari <amtiwari at amd.com>
Date: Mon, 16 Nov 2026 08:55:16 -0500
Subject: [PATCH 1/2] add for-init store for lastprivate empty loop counters
---
clang/lib/CodeGen/CGStmtOpenMP.cpp | 33 +++++-
.../OpenMP/for_lastprivate_empty_codegen.cpp | 64 +++++++++++
.../for/omp_parallel_for_lastprivate_empty.c | 107 ++++++++++++++++++
3 files changed, 202 insertions(+), 2 deletions(-)
create mode 100644 clang/test/OpenMP/for_lastprivate_empty_codegen.cpp
create mode 100644 openmp/runtime/test/worksharing/for/omp_parallel_for_lastprivate_empty.c
diff --git a/clang/lib/CodeGen/CGStmtOpenMP.cpp b/clang/lib/CodeGen/CGStmtOpenMP.cpp
index 36bd4a75fcebbf..235b9cbbdc4b4a 100644
--- a/clang/lib/CodeGen/CGStmtOpenMP.cpp
+++ b/clang/lib/CodeGen/CGStmtOpenMP.cpp
@@ -4016,6 +4016,24 @@ struct ScheduleKindModifiersTy {
};
} // namespace
+/// Return the for-init of a lastprivate loop counter. Empty loops still run
+/// that init in sequential code. Skip collapse nests and
+/// distribute: their empty lastprivate values are not a single init store.
+static const Expr *getEmptyLoopLastprivateInit(const OMPLoopDirective &S) {
+ if (S.counters().size() != 1 ||
+ isOpenMPDistributeDirective(S.getDirectiveKind()))
+ return nullptr;
+ const Decl *CounterVD =
+ cast<DeclRefExpr>(S.counters().front())->getDecl()->getCanonicalDecl();
+ for (const auto *C : S.getClausesOfKind<OMPLastprivateClause>()) {
+ for (const Expr *Ref : C->varlist()) {
+ if (cast<DeclRefExpr>(Ref)->getDecl()->getCanonicalDecl() == CounterVD)
+ return S.inits().front();
+ }
+ }
+ return nullptr;
+}
+
bool CodeGenFunction::EmitOMPWorksharingLoop(
const OMPLoopDirective &S, Expr *EUB,
const CodeGenLoopBoundsTy &CodeGenLoopBounds,
@@ -4045,14 +4063,25 @@ bool CodeGenFunction::EmitOMPWorksharingLoop(
// whole loop.
bool CondConstant;
llvm::BasicBlock *ContBlock = nullptr;
+ const Expr *EmptyLoopInit = getEmptyLoopLastprivateInit(S);
if (ConstantFoldsToSimpleInteger(S.getPreCond(), CondConstant)) {
- if (!CondConstant)
+ if (!CondConstant) {
+ if (EmptyLoopInit)
+ EmitIgnoredExpr(EmptyLoopInit);
return false;
+ }
} else {
llvm::BasicBlock *ThenBlock = createBasicBlock("omp.precond.then");
ContBlock = createBasicBlock("omp.precond.end");
- emitPreCond(*this, S, S.getPreCond(), ThenBlock, ContBlock,
+ llvm::BasicBlock *EmptyBlock =
+ EmptyLoopInit ? createBasicBlock("omp.precond.empty") : ContBlock;
+ emitPreCond(*this, S, S.getPreCond(), ThenBlock, EmptyBlock,
getProfileCount(&S));
+ if (EmptyLoopInit) {
+ EmitBlock(EmptyBlock);
+ EmitIgnoredExpr(EmptyLoopInit);
+ EmitBranch(ContBlock);
+ }
EmitBlock(ThenBlock);
incrementProfileCounter(&S);
}
diff --git a/clang/test/OpenMP/for_lastprivate_empty_codegen.cpp b/clang/test/OpenMP/for_lastprivate_empty_codegen.cpp
new file mode 100644
index 00000000000000..e010c5de3b8c84
--- /dev/null
+++ b/clang/test/OpenMP/for_lastprivate_empty_codegen.cpp
@@ -0,0 +1,64 @@
+// Check that an empty worksharing loop assigns the for-init value to a
+// lastprivate loop iteration variable, and changes nothing else.
+//
+// RUN: %clang_cc1 -verify -triple x86_64-pc-linux-gnu -fopenmp -emit-llvm %s -o - | FileCheck %s
+
+// expected-no-diagnostics
+
+// Runtime trip count: the empty path stores the init to the original i only.
+// lastprivate j is not a loop iteration variable and is not assigned.
+// CHECK-LABEL: define {{.*}}void @runtime_empty.omp_outlined(
+// CHECK: [[I:%.+]] = load ptr, ptr %i.addr
+// CHECK: br i1 {{.+}}, label %omp.precond.then, label %omp.precond.empty
+// CHECK: omp.precond.empty:
+// CHECK-NEXT: store i32 0, ptr [[I]]
+// CHECK-NEXT: br label %omp.precond.end
+// CHECK: omp.precond.then:
+// CHECK: call void @__kmpc_for_static_init
+// CHECK: br i1 {{.+}}, label %.omp.lastprivate.then, label %.omp.lastprivate.done
+extern "C" void runtime_empty(int n) {
+ int i = -1;
+ int j = -1;
+#pragma omp parallel for lastprivate(i, j)
+ for (i = 0; i < n; ++i)
+ for (j = 0; j < 3; ++j)
+ ;
+}
+
+// Constant empty loop: no loop is emitted, only the init store.
+// CHECK-LABEL: define {{.*}}void @constant_empty.omp_outlined(
+// CHECK: [[I:%.+]] = load ptr, ptr %i.addr
+// CHECK-NEXT: store i32 0, ptr [[I]]
+// CHECK-NEXT: ret void
+extern "C" void constant_empty() {
+ int i = -1;
+#pragma omp parallel for lastprivate(i)
+ for (i = 0; i < 0; ++i)
+ ;
+}
+
+// The loop iteration variable is not lastprivate: no extra block.
+// CHECK-LABEL: define {{.*}}void @counter_not_lastprivate.omp_outlined(
+// CHECK: br i1 {{.+}}, label %omp.precond.then, label %omp.precond.end
+// CHECK-NOT: omp.precond.empty
+// CHECK: omp.precond.end:
+extern "C" void counter_not_lastprivate(int n) {
+ int j = -1;
+#pragma omp parallel for lastprivate(j)
+ for (int i = 0; i < n; ++i)
+ j = i;
+}
+
+// Collapsed nest: an outer variable may need its final value, not its init,
+// so the empty path is not changed.
+// CHECK-LABEL: define {{.*}}void @collapse_empty.omp_outlined(
+// CHECK-NOT: omp.precond.empty
+// CHECK: omp.precond.end:
+extern "C" void collapse_empty(int n, int m) {
+ int i = -1;
+ int j = -1;
+#pragma omp parallel for collapse(2) lastprivate(i, j)
+ for (i = 0; i < n; ++i)
+ for (j = 0; j < m; ++j)
+ ;
+}
diff --git a/openmp/runtime/test/worksharing/for/omp_parallel_for_lastprivate_empty.c b/openmp/runtime/test/worksharing/for/omp_parallel_for_lastprivate_empty.c
new file mode 100644
index 00000000000000..96cc5af5225765
--- /dev/null
+++ b/openmp/runtime/test/worksharing/for/omp_parallel_for_lastprivate_empty.c
@@ -0,0 +1,107 @@
+// RUN: %libomp-compile-and-run | FileCheck %s --match-full-lines
+
+// An empty worksharing loop still runs the for-init of a lastprivate loop
+// counter. Variables that were never assigned keep their original values.
+
+#include <stdio.h>
+#include <stdlib.h>
+
+static void reference(void) {
+ int I = -1;
+ int J = -1;
+ int Zero = 0;
+ for (I = 0; I < Zero; ++I)
+ for (J = 0; J < 3; ++J)
+ ;
+ printf("reference: i=%d j=%d\n", I, J);
+}
+
+// Runtime-empty path: omp.precond.empty copy-back for I only.
+static void empty_lastprivate(void) {
+ int I = -1;
+ int J = -1;
+ int Zero = 0;
+#pragma omp parallel for lastprivate(I, J)
+ for (I = 0; I < Zero; ++I)
+ for (J = 0; J < 3; ++J)
+ ;
+ printf("empty: i=%d j=%d\n", I, J);
+}
+
+// Same shape with conditional lastprivate.
+static void empty_conditional(void) {
+ int I = -1;
+ int J = -1;
+ int Zero = 0;
+#pragma omp parallel for lastprivate(conditional : I, J)
+ for (I = 0; I < Zero; ++I)
+ for (J = 0; J < 3; ++J)
+ ;
+ printf("conditional: i=%d j=%d\n", I, J);
+}
+
+// Constant-empty path: Sema folds the precondition away.
+static void const_empty(void) {
+ int I = -1;
+ int J = -1;
+#pragma omp parallel for lastprivate(I, J)
+ for (I = 0; I < 0; ++I)
+ for (J = 0; J < 3; ++J)
+ ;
+ printf("const-empty: i=%d j=%d\n", I, J);
+}
+
+// Outer loop runs; middle loop is empty. I finishes normally, J keeps its
+// for-init, and K is not lastprivate so it stays untouched.
+static void middle_empty(void) {
+ int I = -1;
+ int J = -1;
+ int K = -1;
+ int Zero = 0;
+#pragma omp parallel for lastprivate(I, J)
+ for (I = 0; I < 2; ++I)
+ for (J = 0; J < Zero; ++J)
+ for (K = 0; K < 4; ++K)
+ ;
+ printf("mid-empty: i=%d j=%d k=%d\n", I, J, K);
+}
+
+// Non-empty control: normal lastprivate copy-back is unchanged.
+static void nonempty(void) {
+ int I = -1;
+ int J = -1;
+#pragma omp parallel for lastprivate(I, J)
+ for (I = 0; I < 2; ++I)
+ for (J = 0; J < 3; ++J)
+ ;
+ printf("nonempty: i=%d j=%d\n", I, J);
+}
+
+// The loop counter is declared in the for. The lastprivate variable is only
+// assigned in the body, which does not run.
+static void decl_for_empty(void) {
+ int I = -1;
+#pragma omp parallel for lastprivate(I)
+ for (int T = 0; T < 0; ++T)
+ I = T;
+ printf("decl-for: i=%d\n", I);
+}
+
+int main(void) {
+ reference();
+ empty_lastprivate();
+ empty_conditional();
+ const_empty();
+ middle_empty();
+ nonempty();
+ decl_for_empty();
+ return EXIT_SUCCESS;
+}
+
+// CHECK: reference: i=0 j=-1
+// CHECK-NEXT: empty: i=0 j=-1
+// CHECK-NEXT: conditional: i=0 j=-1
+// CHECK-NEXT: const-empty: i=0 j=-1
+// CHECK-NEXT: mid-empty: i=2 j=0 k=-1
+// CHECK-NEXT: nonempty: i=2 j=3
+// CHECK-NEXT: decl-for: i=-1
>From 6d0136f8a6c2d4fc7ab10992d71dc601b5bc91b7 Mon Sep 17 00:00:00 2001
From: amtiwari <amtiwari at amd.com>
Date: Fri, 27 Nov 2026 02:07:30 -0500
Subject: [PATCH 2/2] revised
---
clang/lib/CodeGen/CGStmtOpenMP.cpp | 28 ++++++++++++++++++----------
1 file changed, 18 insertions(+), 10 deletions(-)
diff --git a/clang/lib/CodeGen/CGStmtOpenMP.cpp b/clang/lib/CodeGen/CGStmtOpenMP.cpp
index 235b9cbbdc4b4a..5bfb83444a4c23 100644
--- a/clang/lib/CodeGen/CGStmtOpenMP.cpp
+++ b/clang/lib/CodeGen/CGStmtOpenMP.cpp
@@ -4016,13 +4016,16 @@ struct ScheduleKindModifiersTy {
};
} // namespace
-/// Return the for-init of a lastprivate loop counter. Empty loops still run
-/// that init in sequential code. Skip collapse nests and
-/// distribute: their empty lastprivate values are not a single init store.
+/// If the loop counter is lastprivate, return its init (e.g. 'i = 0').
+/// A loop with no iterations still runs this init, but it skips the lastprivate
+/// copy-back. So the no-iteration path must store the init itself.
+/// Not handled yet: collapse nests and distribute.
static const Expr *getEmptyLoopLastprivateInit(const OMPLoopDirective &S) {
if (S.counters().size() != 1 ||
isOpenMPDistributeDirective(S.getDirectiveKind()))
return nullptr;
+ // There is one init per counter, so this is the init of the only counter.
+ assert(S.inits().size() == 1 && "Expected one init per loop counter");
const Decl *CounterVD =
cast<DeclRefExpr>(S.counters().front())->getDecl()->getCanonicalDecl();
for (const auto *C : S.getClausesOfKind<OMPLastprivateClause>()) {
@@ -4066,6 +4069,7 @@ bool CodeGenFunction::EmitOMPWorksharingLoop(
const Expr *EmptyLoopInit = getEmptyLoopLastprivateInit(S);
if (ConstantFoldsToSimpleInteger(S.getPreCond(), CondConstant)) {
if (!CondConstant) {
+ // No iterations: store the lastprivate counter init.
if (EmptyLoopInit)
EmitIgnoredExpr(EmptyLoopInit);
return false;
@@ -4073,12 +4077,14 @@ bool CodeGenFunction::EmitOMPWorksharingLoop(
} else {
llvm::BasicBlock *ThenBlock = createBasicBlock("omp.precond.then");
ContBlock = createBasicBlock("omp.precond.end");
- llvm::BasicBlock *EmptyBlock =
+ // No iterations: branch to the end. If the counter init must be stored,
+ // branch to a block that stores it first.
+ llvm::BasicBlock *FalseBlock =
EmptyLoopInit ? createBasicBlock("omp.precond.empty") : ContBlock;
- emitPreCond(*this, S, S.getPreCond(), ThenBlock, EmptyBlock,
+ emitPreCond(*this, S, S.getPreCond(), ThenBlock, FalseBlock,
getProfileCount(&S));
if (EmptyLoopInit) {
- EmitBlock(EmptyBlock);
+ EmitBlock(FalseBlock);
EmitIgnoredExpr(EmptyLoopInit);
EmitBranch(ContBlock);
}
@@ -5269,7 +5275,8 @@ void CodeGenFunction::EmitOMPParallelForDirective(
OMPLoopScope LoopScope(CGF, S);
return CGF.EmitScalarExpr(S.getNumIterations());
};
- bool IsInscan = llvm::any_of(S.getClausesOfKind<OMPReductionClause>(),
+ bool IsInscan =
+ llvm::any_of(S.getClausesOfKind<OMPReductionClause>(),
[](const OMPReductionClause *C) {
return C->getModifier() == OMPC_REDUCTION_inscan;
});
@@ -5303,7 +5310,8 @@ void CodeGenFunction::EmitOMPParallelForSimdDirective(
OMPLoopScope LoopScope(CGF, S);
return CGF.EmitScalarExpr(S.getNumIterations());
};
- bool IsInscan = llvm::any_of(S.getClausesOfKind<OMPReductionClause>(),
+ bool IsInscan =
+ llvm::any_of(S.getClausesOfKind<OMPReductionClause>(),
[](const OMPReductionClause *C) {
return C->getModifier() == OMPC_REDUCTION_inscan;
});
@@ -8268,8 +8276,8 @@ void CodeGenFunction::EmitOMPUseDeviceAddrClause(
// correct mapping, since the pointer to the data was passed to the runtime.
if (isa<DeclRefExpr>(Ref->IgnoreParenImpCasts()) ||
MatchingVD->getType()->isArrayType()) {
- QualType PtrTy = getContext().getPointerType(
- OrigVD->getType().getNonReferenceType());
+ QualType PtrTy =
+ getContext().getPointerType(OrigVD->getType().getNonReferenceType());
PrivAddr =
EmitLoadOfPointer(PrivAddr.withElementType(ConvertTypeForMem(PtrTy)),
PtrTy->castAs<PointerType>());
More information about the cfe-commits
mailing list