[clang] [CIR][OpenMP] Support the 'shared' clause on 'parallel' (PR #241223)

Arseniy Obolenskiy via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 11 03:05:07 PST 2026


https://github.com/aobolensk created https://github.com/llvm/llvm-project/pull/241223

omp.parallel body already reads and writes the variables of the enclosing function directly

>From 601b2a90ec13cab585a824106b7b05074827a7e3 Mon Sep 17 00:00:00 2001
From: Arseniy Obolenskiy <arseniy.obolenskiy at amd.com>
Date: Wed, 11 Nov 2026 12:03:28 +0100
Subject: [PATCH] [CIR][OpenMP] Support the 'shared' clause on 'parallel'

omp.parallel body already reads and writes the variables of the enclosing function directly
---
 clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp      |  5 ++---
 .../CIR/CodeGenOpenMP/not-yet-implemented.c     |  1 -
 clang/test/CIR/CodeGenOpenMP/parallel.c         | 17 +++++++++++++++++
 clang/test/CIR/CodeGenOpenMP/target-parallel.c  | 17 +++++++++++++++++
 4 files changed, 36 insertions(+), 4 deletions(-)

diff --git a/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp b/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp
index 2b0e6819898374..522285d71c27ed 100644
--- a/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp
@@ -61,11 +61,10 @@ emitParallelClauses(CIRGenFunction &cgf, CIRGenModule &cgm,
   ce.emitNumThreads(clauseOps);
   ce.emitProcBind(clauseOps);
   return ce.emitNYI</*supported=*/OMPIfClause, OMPNumThreadsClause,
-                    OMPProcBindClause>(
+                    OMPProcBindClause, OMPSharedClause>(
       /*nyi=*/OpenMPNYIClauseList<OMPAllocateClause, OMPCopyinClause,
                                   OMPDefaultClause, OMPFirstprivateClause,
-                                  OMPPrivateClause, OMPReductionClause,
-                                  OMPSharedClause>{},
+                                  OMPPrivateClause, OMPReductionClause>{},
       llvm::omp::Directive::OMPD_parallel);
 }
 
diff --git a/clang/test/CIR/CodeGenOpenMP/not-yet-implemented.c b/clang/test/CIR/CodeGenOpenMP/not-yet-implemented.c
index c4a97e2b76d05b..fab2047bfb17ba 100644
--- a/clang/test/CIR/CodeGenOpenMP/not-yet-implemented.c
+++ b/clang/test/CIR/CodeGenOpenMP/not-yet-implemented.c
@@ -13,7 +13,6 @@ void do_things() {
   // A leaf that reports a not-yet-implemented clause emits no op at all, rather
   // than one that silently ignores the clause.
   int a, b;
-  // expected-error at +2{{ClangIR code gen Not Yet Implemented: OpenMP PARALLEL 'shared' clause}}
   // expected-error at +1{{ClangIR code gen Not Yet Implemented: OpenMP PARALLEL 'firstprivate' clause}}
 #pragma omp parallel shared(a) firstprivate(b)
   {}
diff --git a/clang/test/CIR/CodeGenOpenMP/parallel.c b/clang/test/CIR/CodeGenOpenMP/parallel.c
index 6a9a349c59489f..09b691e070d0ce 100644
--- a/clang/test/CIR/CodeGenOpenMP/parallel.c
+++ b/clang/test/CIR/CodeGenOpenMP/parallel.c
@@ -176,3 +176,20 @@ void num_threads_parallel() {
   // CHECK-NEXT: omp.terminator
   // CHECK-NEXT: }
 }
+
+void shared_parallel() {
+  // CHECK: cir.func{{.*}}@shared_parallel
+  int a;
+  // CHECK-NEXT: %[[A_ADDR:.*]] = cir.alloca "a"
+#pragma omp parallel shared(a)
+  {
+    a = a + 1;
+  }
+  // CHECK-NEXT: omp.parallel {
+  // CHECK-NEXT: %[[A_LOAD:.*]] = cir.load align(4) %[[A_ADDR]]
+  // CHECK-NEXT: %[[ONE:.*]] = cir.const #cir.int<1> : !s32i
+  // CHECK-NEXT: %[[ADD:.*]] = cir.add nsw %[[A_LOAD]], %[[ONE]] : !s32i
+  // CHECK-NEXT: cir.store align(4) %[[ADD]], %[[A_ADDR]] : !s32i, !cir.ptr<!s32i>
+  // CHECK-NEXT: omp.terminator
+  // CHECK-NEXT: }
+}
diff --git a/clang/test/CIR/CodeGenOpenMP/target-parallel.c b/clang/test/CIR/CodeGenOpenMP/target-parallel.c
index 3aad305e5a1b27..413e67f5a706b8 100644
--- a/clang/test/CIR/CodeGenOpenMP/target-parallel.c
+++ b/clang/test/CIR/CodeGenOpenMP/target-parallel.c
@@ -57,3 +57,20 @@ void target_parallel_proc_bind(int x) {
     use(x);
   }
 }
+
+// 'target parallel' routes the shared clause to the parallel leaf.
+void target_parallel_shared(int x) {
+  // CIR-HOST: cir.func{{.*}}@target_parallel_shared
+  // CIR-HOST: omp.target kernel_type(generic) map_entries({{.*}} -> %[[ARG:.*]] : !cir.ptr<!s32i>) {
+  // CIR-HOST: omp.parallel {
+  // CIR-HOST: %[[LOAD:.*]] = cir.load align(4) %[[ARG]]
+  // CIR-HOST: cir.call @use(%[[LOAD]])
+  // CIR-HOST: omp.terminator
+  // CIR-HOST: }
+  // CIR-HOST: omp.terminator
+  // CIR-HOST: } {omp.combined}
+#pragma omp target parallel shared(x) map(tofrom : x)
+  {
+    use(x);
+  }
+}



More information about the cfe-commits mailing list