[clang] [CIR][OpenMP] Lowering for the private clause for scalar local variables (PR #245430)

Pedro da Rosa Pinheiro via cfe-commits cfe-commits at lists.llvm.org
Sun Dec 6 07:16:39 PST 2026


https://github.com/pedropiin updated https://github.com/llvm/llvm-project/pull/245430

>From e213537a27aea6bce5af99adf354f0148852a071 Mon Sep 17 00:00:00 2001
From: pedropiin <pedarosa04 at gmail.com>
Date: Fri, 20 Nov 2026 18:24:31 -0300
Subject: [PATCH 1/2] Basic privatization logic

---
 clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp b/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp
index eb5f3a430b290c..6ba01a9cecb855 100644
--- a/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp
@@ -62,10 +62,10 @@ emitParallelClauses(CIRGenFunction &cgf, CIRGenModule &cgm,
   ce.emitNumThreads(clauseOps);
   ce.emitProcBind(clauseOps);
   return ce.emitNYI</*supported=*/OMPIfClause, OMPNumThreadsClause,
-                    OMPProcBindClause>(
+                    OMPProcBindClause, OMPPrivateClause>(
       /*nyi=*/OpenMPNYIClauseList<OMPAllocateClause, OMPCopyinClause,
                                   OMPDefaultClause, OMPFirstprivateClause,
-                                  OMPPrivateClause, OMPReductionClause,
+                                  OMPReductionClause,
                                   OMPSharedClause>{},
       llvm::omp::Directive::OMPD_parallel);
 }

>From 3b62bed01d0f4315fe0632c6e2bc586f14c6aacc Mon Sep 17 00:00:00 2001
From: pedropiin <pedarosa04 at gmail.com>
Date: Mon, 23 Nov 2026 20:13:14 -0300
Subject: [PATCH 2/2] [CIR][OpenMP] Lowering for the private clause for scalar
 local variables

---
 clang/lib/CIR/CodeGen/CIRGenOpenMPClause.cpp  |  56 +++++++++
 clang/lib/CIR/CodeGen/CIRGenOpenMPClause.h    |   7 ++
 clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp    |  41 +++++--
 .../CIR/CodeGenOpenMP/not-yet-implemented.c   |   6 +
 clang/test/CIR/CodeGenOpenMP/parallel.c       | 108 ++++++++++++++++++
 5 files changed, 208 insertions(+), 10 deletions(-)

diff --git a/clang/lib/CIR/CodeGen/CIRGenOpenMPClause.cpp b/clang/lib/CIR/CodeGen/CIRGenOpenMPClause.cpp
index 2e64632f037c53..0d1d2ea21ce2e5 100644
--- a/clang/lib/CIR/CodeGen/CIRGenOpenMPClause.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenOpenMPClause.cpp
@@ -190,3 +190,59 @@ bool OpenMPClauseEmitter::emitMap(
   }
   return found;
 }
+
+bool OpenMPClauseEmitter::emitPrivate(
+    mlir::omp::PrivateClauseOps &result,
+    llvm::SmallVector<const VarDecl *> &emittedAsPrivate,
+    PrivatizerFn emitPrivatizer) const {
+  llvm::SmallDenseSet<const VarDecl *> alreadyEmittedSet;
+  for (const clang::OMPClause *const clause : clauses) {
+    const auto *pc = dyn_cast<OMPPrivateClause>(clause);
+    if (!pc)
+      continue;
+
+    for (const Expr *const originalRef : pc->varlist()) {
+      const DeclRefExpr *originalDeclRef = dyn_cast<DeclRefExpr>(originalRef);
+      if (!originalDeclRef) {
+        cgm.errorNYI(originalRef->getBeginLoc(),
+                     "OpenMP private cause with non-DeclRefExpr variable.");
+        return false;
+      }
+
+      // TODO: Not necessarily a VarDecl. Could be, e.g., BindingDecl.
+      const VarDecl *vd = dyn_cast<VarDecl>(originalDeclRef->getDecl());
+      if (!vd) {
+        cgm.errorNYI(originalDeclRef->getBeginLoc(),
+                     "OpenMP private clause with non-VarDecl variable.");
+        return false;
+      }
+
+      if (vd->hasGlobalStorage()) {
+        cgm.errorNYI(originalDeclRef->getBeginLoc(),
+                     "OpenMP private clause with global variable.");
+        return false;
+      }
+      if (!vd->getType()->isBuiltinType()) {
+        cgm.errorNYI(originalDeclRef->getBeginLoc(),
+                     "OpenMP private clause with non-builtin types.");
+        return false;
+      }
+      if (!alreadyEmittedSet.insert(vd).second)
+        continue;
+
+      mlir::Location vdLoc = cgf.getLoc(vd->getBeginLoc());
+      Address vdAddr = cgf.getAddrOfLocalVar(vd);
+      mlir::Value vdSSA = vdAddr.getPointer();
+      mlir::Type vdType = vdAddr.getElementType();
+
+      mlir::omp::PrivateClauseOp privatizer =
+          emitPrivatizer(cgm, vdLoc, vd->getName(), vdType);
+
+      result.privateVars.push_back(vdSSA);
+      result.privateSyms.push_back(mlir::SymbolRefAttr::get(privatizer));
+
+      emittedAsPrivate.push_back(vd);
+    }
+  }
+  return !alreadyEmittedSet.empty();
+}
diff --git a/clang/lib/CIR/CodeGen/CIRGenOpenMPClause.h b/clang/lib/CIR/CodeGen/CIRGenOpenMPClause.h
index a5dcbcb918c737..4faee68f506f92 100644
--- a/clang/lib/CIR/CodeGen/CIRGenOpenMPClause.h
+++ b/clang/lib/CIR/CodeGen/CIRGenOpenMPClause.h
@@ -12,6 +12,7 @@
 #include "CIRGenBuilder.h"
 #include "CIRGenModule.h"
 #include "mlir/Dialect/OpenMP/OpenMPClauseOperands.h"
+#include "mlir/Dialect/OpenMP/OpenMPDialect.h"
 #include "clang/AST/OpenMPClause.h"
 #include "clang/AST/StmtOpenMP.h"
 #include "llvm/Frontend/OpenMP/OMPConstants.h"
@@ -50,6 +51,12 @@ class OpenMPClauseEmitter {
 
   bool emitNumThreads(mlir::omp::NumThreadsClauseOps &result) const;
 
+  using PrivatizerFn = llvm::function_ref<mlir::omp::PrivateClauseOp(
+      CIRGenModule &, mlir::Location, llvm::StringRef, mlir::Type)>;
+  bool emitPrivate(mlir::omp::PrivateClauseOps &result,
+                   llvm::SmallVector<const VarDecl *> &emittedAsPrivate,
+                   PrivatizerFn emitPrivatizer) const;
+
   bool emitProcBind(mlir::omp::ProcBindClauseOps &result) const;
 
   /// Verify the clauses of a directive to make sure all legal cases are either
diff --git a/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp b/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp
index 6ba01a9cecb855..0bf0cfa24eecc3 100644
--- a/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp
@@ -24,6 +24,11 @@
 using namespace clang;
 using namespace clang::CIRGen;
 
+static mlir::omp::PrivateClauseOp emitScalarPrivatizer(CIRGenModule &cgm,
+                                                       mlir::Location loc,
+                                                       llvm::StringRef baseName,
+                                                       mlir::Type elemTy);
+
 mlir::LogicalResult
 CIRGenFunction::emitOMPScopeDirective(const OMPScopeDirective &s) {
   getCIRGenModule().errorNYI(s.getSourceRange(), "OpenMP OMPScopeDirective");
@@ -56,17 +61,18 @@ static mlir::LogicalResult
 emitParallelClauses(CIRGenFunction &cgf, CIRGenModule &cgm,
                     CIRGenBuilderTy &builder, mlir::Location loc,
                     llvm::ArrayRef<const OMPClause *> clauses,
-                    mlir::omp::ParallelOperands &clauseOps) {
+                    mlir::omp::ParallelOperands &clauseOps,
+                    llvm::SmallVector<const VarDecl *> &emittedAsPrivate) {
   OpenMPClauseEmitter ce(cgf, cgm, builder, loc, clauses);
   ce.emitIf(clauseOps, llvm::omp::Directive::OMPD_parallel);
   ce.emitNumThreads(clauseOps);
+  ce.emitPrivate(clauseOps, emittedAsPrivate, emitScalarPrivatizer);
   ce.emitProcBind(clauseOps);
   return ce.emitNYI</*supported=*/OMPIfClause, OMPNumThreadsClause,
-                    OMPProcBindClause, OMPPrivateClause>(
+                    OMPPrivateClause, OMPProcBindClause>(
       /*nyi=*/OpenMPNYIClauseList<OMPAllocateClause, OMPCopyinClause,
                                   OMPDefaultClause, OMPFirstprivateClause,
-                                  OMPReductionClause,
-                                  OMPSharedClause>{},
+                                  OMPReductionClause, OMPSharedClause>{},
       llvm::omp::Directive::OMPD_parallel);
 }
 
@@ -96,8 +102,9 @@ static mlir::LogicalResult
 emitParallelOp(CIRGenFunction &cgf, const DirectiveTy &s,
                const omp::ConstructQueue &queue,
                omp::ConstructQueue::const_iterator item, mlir::Location begin,
-               mlir::Location end, const mlir::omp::ParallelOperands &clauseOps,
-               llvm::function_ref<mlir::LogicalResult()> emitBody) {
+               mlir::Location end, mlir::omp::ParallelOperands &clauseOps,
+               llvm::function_ref<mlir::LogicalResult()> emitBody,
+               llvm::SmallVector<const VarDecl *> &emittedAsPrivate) {
   CIRGenBuilderTy &builder = cgf.getBuilder();
   CIRGenModule &cgm = cgf.getCIRGenModule();
 
@@ -117,6 +124,19 @@ emitParallelOp(CIRGenFunction &cgf, const DirectiveTy &s,
     parallelOp.setCombined(true);
 
   mlir::Block &block = parallelOp.getRegion().emplaceBlock();
+
+  for (auto varAddr : clauseOps.privateVars)
+    block.addArgument(varAddr.getType(), varAddr.getLoc());
+
+  llvm::SmallVector<CIRGenFunction::DeclMapRevertingRAII> guards;
+  guards.reserve(emittedAsPrivate.size());
+  for (auto [idx, vd] : llvm::enumerate(emittedAsPrivate)) {
+    Address origAddr = cgf.getAddrOfLocalVar(vd);
+    guards.emplace_back(cgf, vd);
+    mlir::Value blockSSA = block.getArgument(idx);
+    cgf.replaceAddrOfLocalVar(vd, Address(blockSSA, origAddr.getAlignment()));
+  }
+
   return emitOMPRegionBody<mlir::omp::TerminatorOp>(cgf, block, begin, end,
                                                     emitBody);
 }
@@ -133,13 +153,14 @@ static mlir::LogicalResult emitParallel(
     return mlir::failure();
 
   mlir::omp::ParallelOperands parallelOps;
-  if (mlir::failed(emitParallelClauses(cgf, cgf.getCIRGenModule(),
-                                       cgf.getBuilder(), begin,
-                                       parallelItem->clauses, parallelOps)))
+  llvm::SmallVector<const VarDecl *> emittedAsPrivate;
+  if (mlir::failed(emitParallelClauses(
+          cgf, cgf.getCIRGenModule(), cgf.getBuilder(), begin,
+          parallelItem->clauses, parallelOps, emittedAsPrivate)))
     return mlir::failure();
 
   return emitParallelOp(cgf, s, queue, parallelItem, begin, end, parallelOps,
-                        emitBody);
+                        emitBody, emittedAsPrivate);
 }
 
 mlir::LogicalResult
diff --git a/clang/test/CIR/CodeGenOpenMP/not-yet-implemented.c b/clang/test/CIR/CodeGenOpenMP/not-yet-implemented.c
index 1cf0ecd315e0d1..6fa78a8c1d3af1 100644
--- a/clang/test/CIR/CodeGenOpenMP/not-yet-implemented.c
+++ b/clang/test/CIR/CodeGenOpenMP/not-yet-implemented.c
@@ -1,5 +1,7 @@
 // RUN: %clang_cc1 -fopenmp -triple x86_64-linux-gnu -fclangir %s -verify -emit-cir -o -
 
+int globalX = 10;
+
 void do_things() {
   // expected-error at +1{{ClangIR code gen Not Yet Implemented: OpenMP OMPCriticalDirective}}
 #pragma omp critical
@@ -17,6 +19,10 @@ void do_things() {
   // expected-error at +1{{ClangIR code gen Not Yet Implemented: OpenMP PARALLEL 'firstprivate' clause}}
 #pragma omp parallel shared(a) firstprivate(b)
   {}
+  
+// expected-error at +1{{ClangIR code gen Not Yet Implemented: OpenMP private clause with global variable.}}
+#pragma omp parallel private(globalX) 
+  {}
 
   // A clause routed through construct decomposition but not yet emittable must
   // still be diagnosed by the leaf emitter's NYI handling.
diff --git a/clang/test/CIR/CodeGenOpenMP/parallel.c b/clang/test/CIR/CodeGenOpenMP/parallel.c
index 678a4d9491529b..98f017f56f7464 100644
--- a/clang/test/CIR/CodeGenOpenMP/parallel.c
+++ b/clang/test/CIR/CodeGenOpenMP/parallel.c
@@ -174,3 +174,111 @@ void num_threads_parallel() {
   // CHECK-NEXT: omp.terminator
   // CHECK-NEXT: }
 }
+
+void aux_private_parallel(int x) {
+  // CHECK: cir.func{{.*}}@aux_private_parallel
+
+  // CHECK-NEXT: %[[FUNC_X_ADDR:.*]] = cir.alloca "x"
+  // CHECK-NEXT: cir.store %arg0, %[[FUNC_X_ADDR]]
+  #pragma omp parallel private(x)
+  {
+    x = 20;
+  }
+  // CHECK-NEXT: omp.parallel private(@[[FUNC_X_PRIVATIZER:.*]] %[[FUNC_X_ADDR]] -> %[[PRIV_X_ADDR:.*]] {{.*}}) {
+  // CHECK-NEXT: %[[AUX_ONE_ADDR:.*]] = cir.const #cir.int<20>
+  // CHECK-NEXT: cir.store align(4) %[[AUX_ONE_ADDR]], %[[PRIV_X_ADDR]]
+  // CHECK-NEXT: omp.terminator
+  // CHECK-NEXT: }
+
+  // CHECK: omp.private {type = private} @[[FUNC_X_PRIVATIZER]]
+}
+
+void private_parallel() {
+  // CHECK: cir.func{{.*}}@private_parallel
+
+  int x = 10;
+  int y = 50;
+
+  // CHECK-NEXT: %[[X_LOCAL_ADDR:.*]] = cir.alloca "x"
+  // CHECK-NEXT: %[[Y_LOCAL_ADDR:.*]] = cir.alloca "y"
+  // CHECK-NEXT: %[[CONST_10_ADDR:.*]] = cir.const #cir.int<10>
+  // CHECK-NEXT: cir.store align(4) %[[CONST_10_ADDR]], %[[X_LOCAL_ADDR]]
+  // CHECK-NEXT: %[[CONST_50_ADDR:.*]] = cir.const #cir.int<50>
+  // CHECK-NEXT: cir.store align(4) %[[CONST_50_ADDR]], %[[Y_LOCAL_ADDR]]
+  #pragma omp parallel private(x)
+  {
+    x = 20;
+  }
+  // CHECK-NEXT: omp.parallel private(@[[X_PRIVATIZER_1:.*]] %[[X_LOCAL_ADDR]] -> %[[X_PRIV_ADDR:.*]] {{.*}}) {
+  // CHECK-NEXT: %[[CONST_20_ADDR:.*]] = cir.const #cir.int<20>
+  // CHECK-NEXT: cir.store align(4) %[[CONST_20_ADDR]], %[[X_PRIV_ADDR]]
+  // CHECK-NEXT: omp.terminator
+  // CHECK-NEXT: }
+
+  #pragma omp parallel private(x, y)
+  {
+    x = 20;
+    y = 50;
+  }
+  // CHECK-NEXT: omp.parallel private(@[[X_PRIVATIZER_2:.*]] %[[X_LOCAL_ADDR]] -> %[[X_PRIV_ADDR:.*]], @[[Y_PRIVATIZER_0:.*]] %[[Y_LOCAL_ADDR]] -> %[[Y_PRIV_ADDR:.*]] : {{.*}}) {
+  // CHECK-NEXT: %[[CONST_20_ADDR:.*]] = cir.const #cir.int<20>
+  // CHECK-NEXT: cir.store align(4) %[[CONST_20_ADDR]], %[[X_PRIV_ADDR]]
+  // CHECK-NEXT: %[[CONST_50_ADDR:.*]] = cir.const #cir.int<50>
+  // CHECK-NEXT: cir.store align(4) %[[CONST_50_ADDR]], %[[Y_PRIV_ADDR]]
+  // CHECK-NEXT: omp.terminator
+  // CHECK-NEXT: }
+
+  #pragma omp parallel private(x)
+  {
+    x = 20;
+    #pragma omp parallel private(x, y)
+    {
+      x = 30;
+      y = 100;
+    }
+    x = 20;
+    y = 50;
+  }
+  // CHECK-NEXT: omp.parallel private(@[[X_PRIVATIZER_3:.*]] %[[X_LOCAL_ADDR]] -> %[[X_PRIV_ADDR:.*]] : {{.*}}) {
+  // CHECK-NEXT: %[[CONST_20_ADDR:.*]] = cir.const #cir.int<20>
+  // CHECK-NEXT: cir.store align(4) %[[CONST_20_ADDR]], %[[X_PRIV_ADDR]]
+  // CHECK-NEXT: omp.parallel private(@[[X_PRIVATIZER_4:.*]] %[[X_PRIV_ADDR]] -> %[[X_INNER_PRIV_ADDR:.*]], @[[Y_PRIVATIZER_1:.*]] %[[Y_LOCAL_ADDR]] -> %[[Y_INNER_PRIV_ADDR:.*]] : {{.*}}) {
+  // CHECK-NEXT: %[[CONST_30_ADDR:.*]] = cir.const #cir.int<30>
+  // CHECK-NEXT: cir.store align(4) %[[CONST_30_ADDR]], %[[X_INNER_PRIV_ADDR]]
+  // CHECK-NEXT: %[[CONST_100_ADDR:.*]] = cir.const #cir.int<100>
+  // CHECK-NEXT: cir.store align(4) %[[CONST_100_ADDR]], %[[Y_INNER_PRIV_ADDR]]
+  // CHECK-NEXT: omp.terminator
+  // CHECK-NEXT: }
+  // CHECK-NEXT: %[[CONST_20_ADDR:.*]] = cir.const #cir.int<20>
+  // CHECK-NEXT: cir.store align(4) %[[CONST_20_ADDR]], %[[X_PRIV_ADDR]]
+  // CHECK-NEXT: %[[CONST_50_ADDR:.*]] = cir.const #cir.int<50> 
+  // CHECK-NEXT: cir.store align(4) %[[CONST_50_ADDR]], %[[Y_LOCAL_ADDR]]
+  // CHECK-NEXT: omp.terminator
+  // CHECK-NEXT: }
+
+  aux_private_parallel(x);
+  #pragma omp parallel private(x)
+  {
+    x = 30;
+    aux_private_parallel(x);
+  }
+  // CHECK-NEXT: %[[X_PARAM_ADDR:.*]] = cir.load align(4) %[[X_LOCAL_ADDR]]
+  // CHECK-NEXT: cir.call @aux_private_parallel(%[[X_PARAM_ADDR]])
+  // CHECK-NEXT: omp.parallel private(@[[X_PRIVATIZER_5:.*]] %[[X_LOCAL_ADDR]] -> %[[X_PRIV_ADDR:.*]] : {{.*}})
+  // CHECK-NEXT: %[[CONST_30_ADDR:.*]] = cir.const #cir.int<30>
+  // CHECK-NEXT: cir.store align(4) %[[CONST_30_ADDR]], %[[X_PRIV_ADDR]]
+  // CHECK-NEXT: %[[X_CALL_ADDR:.*]] = cir.load align(4) %[[X_PRIV_ADDR]]
+  // CHECK-NEXT: cir.call @aux_private_parallel(%[[X_CALL_ADDR]])
+  // CHECK-NEXT: omp.terminator
+  // CHECK-NEXT: }
+
+  // CHECK-NEXT: cir.return
+  // CHECK-NEXT: }
+  // CHECK-NEXT: omp.private {type = private} @[[X_PRIVATIZER_1]]
+  // CHECK-NEXT: omp.private {type = private} @[[X_PRIVATIZER_2]]
+  // CHECK-NEXT: omp.private {type = private} @[[Y_PRIVATIZER_0]]
+  // CHECK-NEXT: omp.private {type = private} @[[X_PRIVATIZER_3]]
+  // CHECK-NEXT: omp.private {type = private} @[[X_PRIVATIZER_4]]
+  // CHECK-NEXT: omp.private {type = private} @[[Y_PRIVATIZER_1]]
+  // CHECK-NEXT: omp.private {type = private} @[[X_PRIVATIZER_5]]
+}



More information about the cfe-commits mailing list