[clang] [CIR][OpenMP] Add support for omp single construct (PR #243797)

Thirumalai Shaktivel via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 25 02:09:57 PST 2026


================
@@ -0,0 +1,184 @@
+// RUN: %clang_cc1 -fopenmp -triple x86_64-linux-gnu -emit-cir -fclangir %s -o - | FileCheck %s --check-prefix=CIR
+// 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-prefix=LLVM
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fopenmp -emit-llvm %s -o %t.ll
+// RUN: FileCheck --input-file=%t.ll %s --check-prefix=LLVM
+
+// Check the CIR of omp single, and the LLVM IR from both ClangIR and classic
+// codegen. The prefixes are CIR (ClangIR output) and LLVM (LLVM IR checks
+// shared by both paths).
+
+void before(void);
+void during(void);
+void after(void);
+
+void emit_simple_single() {
+  // CIR: cir.func{{.*}}@emit_simple_single
+  before();
+  // CIR-NEXT: cir.call @before
+#pragma omp single
+  { during(); }
+  // CIR-NEXT: omp.single {
+  // CIR-NEXT: cir.call @during
+  // CIR-NEXT: omp.terminator
+  // CIR-NEXT: }
+  after();
+  // CIR-NEXT: cir.call @after
+}
+
+void nowait_single() {
+  // CIR: cir.func{{.*}}@nowait_single
+#pragma omp single nowait
+  {}
+  // CIR-NEXT: omp.single nowait {
+  // CIR-NEXT: omp.terminator
+  // CIR-NEXT: }
+}
+
+void single_with_local_variable(int *ptr) {
+  // CIR: cir.func{{.*}}@single_with_local_variable
+#pragma omp single
+  {
+    int local = *ptr;
+    *ptr = local + 1;
+  }
+  // CIR: omp.single {
+  // CIR-NEXT: cir.alloca {{.*}}"local"
+  // CIR: omp.terminator
+  // CIR-NEXT: }
+}
+
+void single_inside_parallel(void) {
+  // CIR: cir.func{{.*}}@single_inside_parallel
+#pragma omp parallel
+  {
+#pragma omp single
+    { during(); }
+  }
+  // CIR-NEXT: omp.parallel {
+  // CIR-NEXT: omp.single {
+  // CIR-NEXT: cir.call @during
+  // CIR-NEXT: omp.terminator
+  // CIR-NEXT: }
+  // CIR-NEXT: omp.terminator
+  // CIR-NEXT: }
+}
+
+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_in_parallel_region(void) {
+#pragma omp parallel
+  {
+#pragma omp single
+    work();
+  }
+}
+
+// LLVM-LABEL: define {{.*}}void @single_in_parallel_region()
+// LLVM:         call {{.*}}@__kmpc_fork_call{{.*}}(
+// The outlined function name differs between the two paths (ClangIR:
+// "<fn>..omp_par", classic codegen: "<fn>.omp_outlined"), so the dot count
+// and suffix are matched with a regex.
+// LLVM-LABEL: define {{.*}}void @single_in_parallel_region{{\.+}}omp_{{(par|outlined)}}(
+// LLVM:         call i32 @__kmpc_single(
+// LLVM-DAG:     call void @work()
+// LLVM-DAG:     call void @__kmpc_end_single(
+// LLVM-DAG:     call void @__kmpc_barrier(
----------------
Thirumalai-Shaktivel wrote:

Here is the reason for the DAG usage:
```
// LLVM:         call i32 @__kmpc_single(
// CIRLL:        call void @__kmpc_barrier(
// LLVM:         call void @work()
// LLVM:         call void @__kmpc_end_single(
// OGCG:         call void @__kmpc_barrier(
```

kmpc_barrier is used before the @work call in CIR, and the same is used after the end single in classic clang. I will try to collect some information about this and report back

https://github.com/llvm/llvm-project/pull/243797


More information about the cfe-commits mailing list