[CIR][OpenMP] Add support for omp single construct #243797
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Thirumalai Shaktivel (Thirumalai-Shaktivel)
<details>
<summary>Changes</summary>
Issue: emitOMPSingleDirective was Not-Yet-Implemented, so #pragma omp single hit errorNYI in ClangIR codegen.
Fix: Lower OMPSingleDirective to omp.single, adding an emitNowait clause emitter. allocate, copyprivate,
firstprivate, and private clauses remain NYI. Add CIR and LLVM lowering tests.
---
Full diff: https://github.com/llvm/llvm-project/pull/243797.diff
6 Files Affected:
````````````diff --git a/clang/lib/CIR/CodeGen/CIRGenOpenMPClause.cpp b/clang/lib/CIR/CodeGen/CIRGenOpenMPClause.cpp
index 2e64632f037c53..7eea93da7e7908 100644
--- a/clang/lib/CIR/CodeGen/CIRGenOpenMPClause.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenOpenMPClause.cpp
@@ -112,6 +112,17 @@ bool OpenMPClauseEmitter::emitNumThreads(
return false;
}
+bool OpenMPClauseEmitter::emitNowait(mlir::omp::NowaitClauseOps &result) const {
+ for (const OMPClause *clause : clauses) {
+ if (!isa<OMPNowaitClause>(clause))
+ continue;
+
+ result.nowait = builder.getUnitAttr();
+ return true;
+ }
+ return false;
+}
+
bool OpenMPClauseEmitter::emitIf(mlir::omp::IfClauseOps &result,
llvm::omp::Directive directiveName) const {
for (const OMPClause *clause : clauses) {
diff --git a/clang/lib/CIR/CodeGen/CIRGenOpenMPClause.h b/clang/lib/CIR/CodeGen/CIRGenOpenMPClause.h
index a5dcbcb918c737..dc5d2e9fd5c112 100644
--- a/clang/lib/CIR/CodeGen/CIRGenOpenMPClause.h
+++ b/clang/lib/CIR/CodeGen/CIRGenOpenMPClause.h
@@ -52,6 +52,8 @@ class OpenMPClauseEmitter {
bool emitProcBind(mlir::omp::ProcBindClauseOps &result) const;
+ bool emitNowait(mlir::omp::NowaitClauseOps &result) const;
+
/// Verify the clauses of a directive to make sure all legal cases are either
/// implemented or give a NYI error. The \p SupportedClauses and \p
/// NYIClauses type lists must be disjoint and cover all clauses eligible for
diff --git a/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp b/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp
index eb5f3a430b290c..5ffc8f37b9f11d 100644
--- a/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp
@@ -484,10 +484,49 @@ CIRGenFunction::emitOMPSectionDirective(const OMPSectionDirective &s) {
getCIRGenModule().errorNYI(s.getSourceRange(), "OpenMP OMPSectionDirective");
return mlir::failure();
}
+static mlir::LogicalResult
+emitSingleClauses(CIRGenFunction &cgf, CIRGenModule &cgm,
+ CIRGenBuilderTy &builder, mlir::Location loc,
+ llvm::ArrayRef<const OMPClause *> clauses,
+ mlir::omp::SingleOperands &clauseOps) {
+ OpenMPClauseEmitter ce(cgf, cgm, builder, loc, clauses);
+ ce.emitNowait(clauseOps);
+ return ce.emitNYI</*supported=*/OMPNowaitClause>(
+ /*nyi=*/OpenMPNYIClauseList<OMPAllocateClause, OMPCopyprivateClause,
+ OMPFirstprivateClause, OMPPrivateClause>{},
+ llvm::omp::Directive::OMPD_single);
+}
+
mlir::LogicalResult
CIRGenFunction::emitOMPSingleDirective(const OMPSingleDirective &s) {
- getCIRGenModule().errorNYI(s.getSourceRange(), "OpenMP OMPSingleDirective");
- return mlir::failure();
+ mlir::Location begin = getLoc(s.getBeginLoc());
+ mlir::Location end = getLoc(s.getEndLoc());
+
+ omp::ConstructQueue queue =
+ omp::buildConstructQueue(getContext().getLangOpts().OpenMP, s);
+ omp::ConstructQueue::const_iterator item = queue.begin();
+
+ if (mlir::failed(checkSynthesizedClauses(*this, s, item)))
+ return mlir::failure();
+
+ mlir::omp::SingleOperands clauseOps;
+ if (mlir::failed(emitSingleClauses(*this, getCIRGenModule(), builder, begin,
+ item->clauses, clauseOps)))
+ return mlir::failure();
+
+ auto singleOp = mlir::omp::SingleOp::create(builder, begin, clauseOps);
+
+ mlir::Block &block = singleOp.getRegion().emplaceBlock();
+ mlir::OpBuilder::InsertionGuard guard(builder);
+ builder.setInsertionPointToEnd(&block);
+
+ CIRGenFunction::LexicalScope ls{*this, begin, builder.getInsertionBlock()};
+
+ const CapturedStmt *cs = s.getInnermostCapturedStmt();
+ mlir::LogicalResult res =
+ emitStmt(cs->getCapturedStmt(), /*useCurrentScope=*/true);
+ mlir::omp::TerminatorOp::create(builder, end);
+ return res;
}
mlir::LogicalResult
CIRGenFunction::emitOMPMasterDirective(const OMPMasterDirective &s) {
diff --git a/clang/test/CIR/CodeGenOpenMP/not-yet-implemented.c b/clang/test/CIR/CodeGenOpenMP/not-yet-implemented.c
index 1cf0ecd315e0d1..3e905d225126c1 100644
--- a/clang/test/CIR/CodeGenOpenMP/not-yet-implemented.c
+++ b/clang/test/CIR/CodeGenOpenMP/not-yet-implemented.c
@@ -5,10 +5,6 @@ void do_things() {
#pragma omp critical
{}
- // expected-error@+1{{ClangIR code gen Not Yet Implemented: OpenMP OMPSingleDirective}}
-#pragma omp single
- {}
-
int i;
// A leaf that reports a not-yet-implemented clause emits no op at all, rather
// than one that silently ignores the clause.
@@ -18,6 +14,10 @@ void do_things() {
#pragma omp parallel shared(a) firstprivate(b)
{}
+ // expected-error@+1{{ClangIR code gen Not Yet Implemented: OpenMP SINGLE 'copyprivate' clause}}
+#pragma omp single copyprivate(a)
+ {}
+
// A clause routed through construct decomposition but not yet emittable must
// still be diagnosed by the leaf emitter's NYI handling.
// expected-error@+1{{ClangIR code gen Not Yet Implemented: OpenMP TARGET 'private' clause}}
diff --git a/clang/test/CIR/CodeGenOpenMP/single.c b/clang/test/CIR/CodeGenOpenMP/single.c
new file mode 100644
index 00000000000000..bb18561791ffe3
--- /dev/null
+++ b/clang/test/CIR/CodeGenOpenMP/single.c
@@ -0,0 +1,57 @@
+// RUN: %clang_cc1 -fopenmp -triple x86_64-linux-gnu -emit-cir -fclangir %s -o - | FileCheck %s
+
+void before(void);
+void during(void);
+void after(void);
+
+void emit_simple_single() {
+ // CHECK: cir.func{{.*}}@emit_simple_single
+ before();
+ // CHECK-NEXT: cir.call @before
+#pragma omp single
+ { during(); }
+ // CHECK-NEXT: omp.single {
+ // CHECK-NEXT: cir.call @during
+ // CHECK-NEXT: omp.terminator
+ // CHECK-NEXT: }
+ after();
+ // CHECK-NEXT: cir.call @after
+}
+
+void nowait_single() {
+ // CHECK: cir.func{{.*}}@nowait_single
+#pragma omp single nowait
+ {}
+ // CHECK-NEXT: omp.single nowait {
+ // CHECK-NEXT: omp.terminator
+ // CHECK-NEXT: }
+}
+
+void single_with_local_variable(int *ptr) {
+ // CHECK: cir.func{{.*}}@single_with_local_variable
+#pragma omp single
+ {
+ int local = *ptr;
+ *ptr = local + 1;
+ }
+ // CHECK: omp.single {
+ // CHECK-NEXT: cir.alloca {{.*}}"local"
+ // CHECK: omp.terminator
+ // CHECK-NEXT: }
+}
+
+void single_inside_parallel(void) {
+ // CHECK: cir.func{{.*}}@single_inside_parallel
+#pragma omp parallel
+ {
+#pragma omp single
+ { during(); }
+ }
+ // CHECK-NEXT: omp.parallel {
+ // CHECK-NEXT: omp.single {
+ // CHECK-NEXT: cir.call @during
+ // CHECK-NEXT: omp.terminator
+ // CHECK-NEXT: }
+ // CHECK-NEXT: omp.terminator
+ // CHECK-NEXT: }
+}
diff --git a/clang/test/CIR/CodeGenOpenMP/single_llvm.c b/clang/test/CIR/CodeGenOpenMP/single_llvm.c
new file mode 100644
index 00000000000000..6a50a42086e829
--- /dev/null
+++ b/clang/test/CIR/CodeGenOpenMP/single_llvm.c
@@ -0,0 +1,171 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fopenmp -fclangir -emit-llvm %s -o %t-cir.ll
+// RUN: FileCheck --input-file=%t-cir.ll %s --check-prefixes=LLVM,CIR
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fopenmp -emit-llvm %s -o %t.ll
+// RUN: FileCheck --input-file=%t.ll %s --check-prefixes=LLVM,OGCG
+
+// Check the LLVM IR of omp single from ClangIR and classic codegen. The
+// prefixes are LLVM (checks shared by both paths), CIR (IR lowered from
+// ClangIR) and OGCG (IR from the original, classic Clang codegen).
+
+void work(void);
+void other_work(void);
+
+void single_only(void) {
+#pragma omp single
+ work();
+}
+
+// LLVM-LABEL: define {{.*}}void @single_only()
+// LLVM: call i32 @__kmpc_single(
+// LLVM: call void @work()
+// LLVM: call void @__kmpc_end_single(
+// LLVM: call void @__kmpc_barrier(
+
+void nowait_single_only(void) {
+#pragma omp single nowait
+ work();
+}
+
+// LLVM-LABEL: define {{.*}}void @nowait_single_only()
+// LLVM: call i32 @__kmpc_single(
+// LLVM: call void @work()
+// LLVM: call void @__kmpc_end_single(
+// LLVM-NOT: @__kmpc_barrier
+
+void single_inside_parallel(void) {
+#pragma omp parallel
+ {
+#pragma omp single
+ work();
+ }
+}
+
+// CIR-LABEL: define {{.*}}void @single_inside_parallel()
+// CIR: call {{.*}}@__kmpc_fork_call(
+// CIR-LABEL: define {{.*}}void @single_inside_parallel..omp_par(
+// CIR: call i32 @__kmpc_single(
+// CIR-DAG: call void @work()
+// CIR-DAG: call void @__kmpc_end_single(
+// CIR-DAG: call void @__kmpc_barrier(
+
+// OGCG-LABEL: define {{.*}}void @single_inside_parallel()
+// OGCG: call {{.*}}@__kmpc_fork_call(
+// OGCG-LABEL: define {{.*}}void @single_inside_parallel.omp_outlined(
+// OGCG: call i32 @__kmpc_single(
+// OGCG: call void @work()
+// OGCG: call void @__kmpc_end_single(
+// OGCG: call void @__kmpc_barrier(
+
+void nowait_single_then_other_work_in_parallel(void) {
+#pragma omp parallel
+ {
+#pragma omp single nowait
+ work();
+ other_work();
+ }
+}
+
+// CIR-LABEL: define {{.*}}void @nowait_single_then_other_work_in_parallel()
+// CIR: call {{.*}}@__kmpc_fork_call(
+// CIR-LABEL: define {{.*}}void @nowait_single_then_other_work_in_parallel..omp_par(
+// CIR: call i32 @__kmpc_single(
+// CIR-DAG: call void @work()
+// CIR-DAG: call void @__kmpc_end_single(
+// CIR-DAG: call void @other_work()
+// CIR-NOT: @__kmpc_barrier
+
+// OGCG-LABEL: define {{.*}}void @nowait_single_then_other_work_in_parallel()
+// OGCG: call {{.*}}@__kmpc_fork_call(
+// OGCG-LABEL: define {{.*}}void @nowait_single_then_other_work_in_parallel.omp_outlined(
+// OGCG: call i32 @__kmpc_single(
+// OGCG: call void @work()
+// OGCG: call void @__kmpc_end_single(
+// OGCG: call void @other_work()
+// OGCG-NOT: @__kmpc_barrier
+
+void single_in_parallel_with_clauses(int num_threads_value, int condition) {
+#pragma omp parallel num_threads(num_threads_value) if (condition)
+ {
+#pragma omp single
+ work();
+ }
+}
+
+// CIR-LABEL: define {{.*}}void @single_in_parallel_with_clauses(
+// CIR: call void @__kmpc_push_num_threads(
+// CIR: call {{.*}}@__kmpc_fork_call_if(
+// CIR-LABEL: define {{.*}}void @single_in_parallel_with_clauses..omp_par(
+// CIR: call i32 @__kmpc_single(
+// CIR-DAG: call void @work()
+// CIR-DAG: call void @__kmpc_end_single(
+// CIR-DAG: call void @__kmpc_barrier(
+
+// OGCG-LABEL: define {{.*}}void @single_in_parallel_with_clauses(
+// OGCG: call void @__kmpc_push_num_threads(
+// OGCG: call {{.*}}@__kmpc_fork_call(
+// OGCG-LABEL: define {{.*}}void @single_in_parallel_with_clauses.omp_outlined(
+// OGCG: call i32 @__kmpc_single(
+// OGCG: call void @work()
+// OGCG: call void @__kmpc_end_single(
+// OGCG: call void @__kmpc_barrier(
+
+void two_singles_around_barrier(void) {
+#pragma omp parallel
+ {
+#pragma omp single
+ work();
+#pragma omp barrier
+#pragma omp single nowait
+ other_work();
+ }
+}
+
+// CIR-LABEL: define {{.*}}void @two_singles_around_barrier()
+// CIR: call {{.*}}@__kmpc_fork_call(
+// CIR-LABEL: define {{.*}}void @two_singles_around_barrier..omp_par(
+// CIR: call i32 @__kmpc_single(
+// CIR-DAG: call void @work()
+// CIR-DAG: call void @__kmpc_end_single(
+// CIR-DAG: call void @__kmpc_barrier(
+// CIR-DAG: call void @__kmpc_barrier(
+// CIR-DAG: call i32 @__kmpc_single(
+// CIR-DAG: call void @other_work()
+// CIR-DAG: call void @__kmpc_end_single(
+
+// OGCG-LABEL: define {{.*}}void @two_singles_around_barrier()
+// OGCG: call {{.*}}@__kmpc_fork_call(
+// OGCG-LABEL: define {{.*}}void @two_singles_around_barrier.omp_outlined(
+// OGCG: call i32 @__kmpc_single(
+// OGCG: call void @work()
+// OGCG: call void @__kmpc_end_single(
+// OGCG: call void @__kmpc_barrier(
+// OGCG: call void @__kmpc_barrier(
+// OGCG: call i32 @__kmpc_single(
+// OGCG: call void @other_work()
+// OGCG: call void @__kmpc_end_single(
+
+void single_inside_target_parallel(void) {
+#pragma omp target parallel
+ {
+#pragma omp single
+ work();
+ }
+}
+
+// CIR-LABEL: define {{.*}}void @single_inside_target_parallel()
+// CIR-LABEL: define {{.*}}void @__omp_offloading_{{.*}}_single_inside_target_parallel_l{{[0-9]+}}(
+// CIR: call {{.*}}@__kmpc_fork_call(
+// CIR-LABEL: define {{.*}}void @__omp_offloading_{{.*}}_single_inside_target_parallel_l{{[0-9]+}}..omp_par(
+// CIR: call i32 @__kmpc_single(
+// CIR-DAG: call void @work()
+// CIR-DAG: call void @__kmpc_end_single(
+// CIR-DAG: call void @__kmpc_barrier(
+
+// OGCG-LABEL: define {{.*}}void @single_inside_target_parallel()
+// OGCG-LABEL: define {{.*}}void @__omp_offloading_{{.*}}_single_inside_target_parallel_l{{[0-9]+}}(
+// OGCG: call {{.*}}@__kmpc_fork_call(
+// OGCG-LABEL: define {{.*}}void @__omp_offloading_{{.*}}_single_inside_target_parallel_l{{[0-9]+}}.omp_outlined(
+// OGCG: call i32 @__kmpc_single(
+// OGCG: call void @work()
+// OGCG: call void @__kmpc_end_single(
+// OGCG: call void @__kmpc_barrier(
</details>
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clangir
Author: Thirumalai Shaktivel (Thirumalai-Shaktivel)
<details>
<summary>Changes</summary>
Issue: emitOMPSingleDirective was Not-Yet-Implemented, so #pragma omp single hit errorNYI in ClangIR codegen.
Fix: Lower OMPSingleDirective to omp.single, adding an emitNowait clause emitter. allocate, copyprivate,
firstprivate, and private clauses remain NYI. Add CIR and LLVM lowering tests.
---
Full diff: https://github.com/llvm/llvm-project/pull/243797.diff
6 Files Affected:
````````````diff --git a/clang/lib/CIR/CodeGen/CIRGenOpenMPClause.cpp b/clang/lib/CIR/CodeGen/CIRGenOpenMPClause.cpp
index 2e64632f037c53..7eea93da7e7908 100644
--- a/clang/lib/CIR/CodeGen/CIRGenOpenMPClause.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenOpenMPClause.cpp
@@ -112,6 +112,17 @@ bool OpenMPClauseEmitter::emitNumThreads(
return false;
}
+bool OpenMPClauseEmitter::emitNowait(mlir::omp::NowaitClauseOps &result) const {
+ for (const OMPClause *clause : clauses) {
+ if (!isa<OMPNowaitClause>(clause))
+ continue;
+
+ result.nowait = builder.getUnitAttr();
+ return true;
+ }
+ return false;
+}
+
bool OpenMPClauseEmitter::emitIf(mlir::omp::IfClauseOps &result,
llvm::omp::Directive directiveName) const {
for (const OMPClause *clause : clauses) {
diff --git a/clang/lib/CIR/CodeGen/CIRGenOpenMPClause.h b/clang/lib/CIR/CodeGen/CIRGenOpenMPClause.h
index a5dcbcb918c737..dc5d2e9fd5c112 100644
--- a/clang/lib/CIR/CodeGen/CIRGenOpenMPClause.h
+++ b/clang/lib/CIR/CodeGen/CIRGenOpenMPClause.h
@@ -52,6 +52,8 @@ class OpenMPClauseEmitter {
bool emitProcBind(mlir::omp::ProcBindClauseOps &result) const;
+ bool emitNowait(mlir::omp::NowaitClauseOps &result) const;
+
/// Verify the clauses of a directive to make sure all legal cases are either
/// implemented or give a NYI error. The \p SupportedClauses and \p
/// NYIClauses type lists must be disjoint and cover all clauses eligible for
diff --git a/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp b/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp
index eb5f3a430b290c..5ffc8f37b9f11d 100644
--- a/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp
@@ -484,10 +484,49 @@ CIRGenFunction::emitOMPSectionDirective(const OMPSectionDirective &s) {
getCIRGenModule().errorNYI(s.getSourceRange(), "OpenMP OMPSectionDirective");
return mlir::failure();
}
+static mlir::LogicalResult
+emitSingleClauses(CIRGenFunction &cgf, CIRGenModule &cgm,
+ CIRGenBuilderTy &builder, mlir::Location loc,
+ llvm::ArrayRef<const OMPClause *> clauses,
+ mlir::omp::SingleOperands &clauseOps) {
+ OpenMPClauseEmitter ce(cgf, cgm, builder, loc, clauses);
+ ce.emitNowait(clauseOps);
+ return ce.emitNYI</*supported=*/OMPNowaitClause>(
+ /*nyi=*/OpenMPNYIClauseList<OMPAllocateClause, OMPCopyprivateClause,
+ OMPFirstprivateClause, OMPPrivateClause>{},
+ llvm::omp::Directive::OMPD_single);
+}
+
mlir::LogicalResult
CIRGenFunction::emitOMPSingleDirective(const OMPSingleDirective &s) {
- getCIRGenModule().errorNYI(s.getSourceRange(), "OpenMP OMPSingleDirective");
- return mlir::failure();
+ mlir::Location begin = getLoc(s.getBeginLoc());
+ mlir::Location end = getLoc(s.getEndLoc());
+
+ omp::ConstructQueue queue =
+ omp::buildConstructQueue(getContext().getLangOpts().OpenMP, s);
+ omp::ConstructQueue::const_iterator item = queue.begin();
+
+ if (mlir::failed(checkSynthesizedClauses(*this, s, item)))
+ return mlir::failure();
+
+ mlir::omp::SingleOperands clauseOps;
+ if (mlir::failed(emitSingleClauses(*this, getCIRGenModule(), builder, begin,
+ item->clauses, clauseOps)))
+ return mlir::failure();
+
+ auto singleOp = mlir::omp::SingleOp::create(builder, begin, clauseOps);
+
+ mlir::Block &block = singleOp.getRegion().emplaceBlock();
+ mlir::OpBuilder::InsertionGuard guard(builder);
+ builder.setInsertionPointToEnd(&block);
+
+ CIRGenFunction::LexicalScope ls{*this, begin, builder.getInsertionBlock()};
+
+ const CapturedStmt *cs = s.getInnermostCapturedStmt();
+ mlir::LogicalResult res =
+ emitStmt(cs->getCapturedStmt(), /*useCurrentScope=*/true);
+ mlir::omp::TerminatorOp::create(builder, end);
+ return res;
}
mlir::LogicalResult
CIRGenFunction::emitOMPMasterDirective(const OMPMasterDirective &s) {
diff --git a/clang/test/CIR/CodeGenOpenMP/not-yet-implemented.c b/clang/test/CIR/CodeGenOpenMP/not-yet-implemented.c
index 1cf0ecd315e0d1..3e905d225126c1 100644
--- a/clang/test/CIR/CodeGenOpenMP/not-yet-implemented.c
+++ b/clang/test/CIR/CodeGenOpenMP/not-yet-implemented.c
@@ -5,10 +5,6 @@ void do_things() {
#pragma omp critical
{}
- // expected-error@+1{{ClangIR code gen Not Yet Implemented: OpenMP OMPSingleDirective}}
-#pragma omp single
- {}
-
int i;
// A leaf that reports a not-yet-implemented clause emits no op at all, rather
// than one that silently ignores the clause.
@@ -18,6 +14,10 @@ void do_things() {
#pragma omp parallel shared(a) firstprivate(b)
{}
+ // expected-error@+1{{ClangIR code gen Not Yet Implemented: OpenMP SINGLE 'copyprivate' clause}}
+#pragma omp single copyprivate(a)
+ {}
+
// A clause routed through construct decomposition but not yet emittable must
// still be diagnosed by the leaf emitter's NYI handling.
// expected-error@+1{{ClangIR code gen Not Yet Implemented: OpenMP TARGET 'private' clause}}
diff --git a/clang/test/CIR/CodeGenOpenMP/single.c b/clang/test/CIR/CodeGenOpenMP/single.c
new file mode 100644
index 00000000000000..bb18561791ffe3
--- /dev/null
+++ b/clang/test/CIR/CodeGenOpenMP/single.c
@@ -0,0 +1,57 @@
+// RUN: %clang_cc1 -fopenmp -triple x86_64-linux-gnu -emit-cir -fclangir %s -o - | FileCheck %s
+
+void before(void);
+void during(void);
+void after(void);
+
+void emit_simple_single() {
+ // CHECK: cir.func{{.*}}@emit_simple_single
+ before();
+ // CHECK-NEXT: cir.call @before
+#pragma omp single
+ { during(); }
+ // CHECK-NEXT: omp.single {
+ // CHECK-NEXT: cir.call @during
+ // CHECK-NEXT: omp.terminator
+ // CHECK-NEXT: }
+ after();
+ // CHECK-NEXT: cir.call @after
+}
+
+void nowait_single() {
+ // CHECK: cir.func{{.*}}@nowait_single
+#pragma omp single nowait
+ {}
+ // CHECK-NEXT: omp.single nowait {
+ // CHECK-NEXT: omp.terminator
+ // CHECK-NEXT: }
+}
+
+void single_with_local_variable(int *ptr) {
+ // CHECK: cir.func{{.*}}@single_with_local_variable
+#pragma omp single
+ {
+ int local = *ptr;
+ *ptr = local + 1;
+ }
+ // CHECK: omp.single {
+ // CHECK-NEXT: cir.alloca {{.*}}"local"
+ // CHECK: omp.terminator
+ // CHECK-NEXT: }
+}
+
+void single_inside_parallel(void) {
+ // CHECK: cir.func{{.*}}@single_inside_parallel
+#pragma omp parallel
+ {
+#pragma omp single
+ { during(); }
+ }
+ // CHECK-NEXT: omp.parallel {
+ // CHECK-NEXT: omp.single {
+ // CHECK-NEXT: cir.call @during
+ // CHECK-NEXT: omp.terminator
+ // CHECK-NEXT: }
+ // CHECK-NEXT: omp.terminator
+ // CHECK-NEXT: }
+}
diff --git a/clang/test/CIR/CodeGenOpenMP/single_llvm.c b/clang/test/CIR/CodeGenOpenMP/single_llvm.c
new file mode 100644
index 00000000000000..6a50a42086e829
--- /dev/null
+++ b/clang/test/CIR/CodeGenOpenMP/single_llvm.c
@@ -0,0 +1,171 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fopenmp -fclangir -emit-llvm %s -o %t-cir.ll
+// RUN: FileCheck --input-file=%t-cir.ll %s --check-prefixes=LLVM,CIR
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fopenmp -emit-llvm %s -o %t.ll
+// RUN: FileCheck --input-file=%t.ll %s --check-prefixes=LLVM,OGCG
+
+// Check the LLVM IR of omp single from ClangIR and classic codegen. The
+// prefixes are LLVM (checks shared by both paths), CIR (IR lowered from
+// ClangIR) and OGCG (IR from the original, classic Clang codegen).
+
+void work(void);
+void other_work(void);
+
+void single_only(void) {
+#pragma omp single
+ work();
+}
+
+// LLVM-LABEL: define {{.*}}void @single_only()
+// LLVM: call i32 @__kmpc_single(
+// LLVM: call void @work()
+// LLVM: call void @__kmpc_end_single(
+// LLVM: call void @__kmpc_barrier(
+
+void nowait_single_only(void) {
+#pragma omp single nowait
+ work();
+}
+
+// LLVM-LABEL: define {{.*}}void @nowait_single_only()
+// LLVM: call i32 @__kmpc_single(
+// LLVM: call void @work()
+// LLVM: call void @__kmpc_end_single(
+// LLVM-NOT: @__kmpc_barrier
+
+void single_inside_parallel(void) {
+#pragma omp parallel
+ {
+#pragma omp single
+ work();
+ }
+}
+
+// CIR-LABEL: define {{.*}}void @single_inside_parallel()
+// CIR: call {{.*}}@__kmpc_fork_call(
+// CIR-LABEL: define {{.*}}void @single_inside_parallel..omp_par(
+// CIR: call i32 @__kmpc_single(
+// CIR-DAG: call void @work()
+// CIR-DAG: call void @__kmpc_end_single(
+// CIR-DAG: call void @__kmpc_barrier(
+
+// OGCG-LABEL: define {{.*}}void @single_inside_parallel()
+// OGCG: call {{.*}}@__kmpc_fork_call(
+// OGCG-LABEL: define {{.*}}void @single_inside_parallel.omp_outlined(
+// OGCG: call i32 @__kmpc_single(
+// OGCG: call void @work()
+// OGCG: call void @__kmpc_end_single(
+// OGCG: call void @__kmpc_barrier(
+
+void nowait_single_then_other_work_in_parallel(void) {
+#pragma omp parallel
+ {
+#pragma omp single nowait
+ work();
+ other_work();
+ }
+}
+
+// CIR-LABEL: define {{.*}}void @nowait_single_then_other_work_in_parallel()
+// CIR: call {{.*}}@__kmpc_fork_call(
+// CIR-LABEL: define {{.*}}void @nowait_single_then_other_work_in_parallel..omp_par(
+// CIR: call i32 @__kmpc_single(
+// CIR-DAG: call void @work()
+// CIR-DAG: call void @__kmpc_end_single(
+// CIR-DAG: call void @other_work()
+// CIR-NOT: @__kmpc_barrier
+
+// OGCG-LABEL: define {{.*}}void @nowait_single_then_other_work_in_parallel()
+// OGCG: call {{.*}}@__kmpc_fork_call(
+// OGCG-LABEL: define {{.*}}void @nowait_single_then_other_work_in_parallel.omp_outlined(
+// OGCG: call i32 @__kmpc_single(
+// OGCG: call void @work()
+// OGCG: call void @__kmpc_end_single(
+// OGCG: call void @other_work()
+// OGCG-NOT: @__kmpc_barrier
+
+void single_in_parallel_with_clauses(int num_threads_value, int condition) {
+#pragma omp parallel num_threads(num_threads_value) if (condition)
+ {
+#pragma omp single
+ work();
+ }
+}
+
+// CIR-LABEL: define {{.*}}void @single_in_parallel_with_clauses(
+// CIR: call void @__kmpc_push_num_threads(
+// CIR: call {{.*}}@__kmpc_fork_call_if(
+// CIR-LABEL: define {{.*}}void @single_in_parallel_with_clauses..omp_par(
+// CIR: call i32 @__kmpc_single(
+// CIR-DAG: call void @work()
+// CIR-DAG: call void @__kmpc_end_single(
+// CIR-DAG: call void @__kmpc_barrier(
+
+// OGCG-LABEL: define {{.*}}void @single_in_parallel_with_clauses(
+// OGCG: call void @__kmpc_push_num_threads(
+// OGCG: call {{.*}}@__kmpc_fork_call(
+// OGCG-LABEL: define {{.*}}void @single_in_parallel_with_clauses.omp_outlined(
+// OGCG: call i32 @__kmpc_single(
+// OGCG: call void @work()
+// OGCG: call void @__kmpc_end_single(
+// OGCG: call void @__kmpc_barrier(
+
+void two_singles_around_barrier(void) {
+#pragma omp parallel
+ {
+#pragma omp single
+ work();
+#pragma omp barrier
+#pragma omp single nowait
+ other_work();
+ }
+}
+
+// CIR-LABEL: define {{.*}}void @two_singles_around_barrier()
+// CIR: call {{.*}}@__kmpc_fork_call(
+// CIR-LABEL: define {{.*}}void @two_singles_around_barrier..omp_par(
+// CIR: call i32 @__kmpc_single(
+// CIR-DAG: call void @work()
+// CIR-DAG: call void @__kmpc_end_single(
+// CIR-DAG: call void @__kmpc_barrier(
+// CIR-DAG: call void @__kmpc_barrier(
+// CIR-DAG: call i32 @__kmpc_single(
+// CIR-DAG: call void @other_work()
+// CIR-DAG: call void @__kmpc_end_single(
+
+// OGCG-LABEL: define {{.*}}void @two_singles_around_barrier()
+// OGCG: call {{.*}}@__kmpc_fork_call(
+// OGCG-LABEL: define {{.*}}void @two_singles_around_barrier.omp_outlined(
+// OGCG: call i32 @__kmpc_single(
+// OGCG: call void @work()
+// OGCG: call void @__kmpc_end_single(
+// OGCG: call void @__kmpc_barrier(
+// OGCG: call void @__kmpc_barrier(
+// OGCG: call i32 @__kmpc_single(
+// OGCG: call void @other_work()
+// OGCG: call void @__kmpc_end_single(
+
+void single_inside_target_parallel(void) {
+#pragma omp target parallel
+ {
+#pragma omp single
+ work();
+ }
+}
+
+// CIR-LABEL: define {{.*}}void @single_inside_target_parallel()
+// CIR-LABEL: define {{.*}}void @__omp_offloading_{{.*}}_single_inside_target_parallel_l{{[0-9]+}}(
+// CIR: call {{.*}}@__kmpc_fork_call(
+// CIR-LABEL: define {{.*}}void @__omp_offloading_{{.*}}_single_inside_target_parallel_l{{[0-9]+}}..omp_par(
+// CIR: call i32 @__kmpc_single(
+// CIR-DAG: call void @work()
+// CIR-DAG: call void @__kmpc_end_single(
+// CIR-DAG: call void @__kmpc_barrier(
+
+// OGCG-LABEL: define {{.*}}void @single_inside_target_parallel()
+// OGCG-LABEL: define {{.*}}void @__omp_offloading_{{.*}}_single_inside_target_parallel_l{{[0-9]+}}(
+// OGCG: call {{.*}}@__kmpc_fork_call(
+// OGCG-LABEL: define {{.*}}void @__omp_offloading_{{.*}}_single_inside_target_parallel_l{{[0-9]+}}.omp_outlined(
+// OGCG: call i32 @__kmpc_single(
+// OGCG: call void @work()
+// OGCG: call void @__kmpc_end_single(
+// OGCG: call void @__kmpc_barrier(
</details>
Ready for review!
I have merged main into the PR. I will merge it after the build is clean
Thank you for the review, Steffen.
xlauko
Thirumalai-Shaktivel
Issue: emitOMPSingleDirective was Not-Yet-Implemented, so #pragma omp single hit error NYI in ClangIR codegen.
Fix: Lower OMPSingleDirective to omp.single, adding an emitNowait clause emitter. allocate, copyprivate,
firstprivate, and private clauses remain NYI. Add CIR and LLVM lowering tests.
Assisted by Claude.