[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 08:40:33 PST 2026


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

>From 9c17104db844ecbc24f5df4aa6f6350bce1833be 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 a634f0ea297b4d..1c3ec8e99e5793 100644
--- a/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp
@@ -132,11 +132,11 @@ emitParallelClauses(CIRGenFunction &cgf, CIRGenModule &cgm,
   ce.emitIf(clauseOps, llvm::omp::Directive::OMPD_parallel);
   ce.emitNumThreads(clauseOps);
   ce.emitProcBind(clauseOps);
-  return ce.emitNYI</*supported=*/OMPIfClause, OMPNumThreadsClause,
+  return ce.emitNYI</*supported=*/OMPIfClause, OMPNumThreadsClause, OMPPrivateClause,
                     OMPProcBindClause, OMPSharedClause>(
       /*nyi=*/OpenMPNYIClauseList<OMPAllocateClause, OMPCopyinClause,
                                   OMPDefaultClause, OMPFirstprivateClause,
-                                  OMPPrivateClause, OMPReductionClause>{},
+                                  OMPReductionClause>{},
       llvm::omp::Directive::OMPD_parallel);
 }
 

>From 378d0bfd3fbfd1eaaba070d64402d92c3d47eb1e 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    |  42 +++++--
 .../CIR/CodeGenOpenMP/not-yet-implemented.c   |   5 +
 clang/test/CIR/CodeGenOpenMP/parallel.c       | 108 ++++++++++++++++++
 5 files changed, 209 insertions(+), 9 deletions(-)

diff --git a/clang/lib/CIR/CodeGen/CIRGenOpenMPClause.cpp b/clang/lib/CIR/CodeGen/CIRGenOpenMPClause.cpp
index de04282c532a0b..e5f74e6c0a9cdf 100644
--- a/clang/lib/CIR/CodeGen/CIRGenOpenMPClause.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenOpenMPClause.cpp
@@ -197,3 +197,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 dc5d2e9fd5c112..bff940d7f8d5a0 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;
 
   bool emitNowait(mlir::omp::NowaitClauseOps &result) const;
diff --git a/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp b/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp
index 1c3ec8e99e5793..f429c8be2efeb0 100644
--- a/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp
@@ -25,6 +25,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");
@@ -127,10 +132,12 @@ 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, OMPPrivateClause,
                     OMPProcBindClause, OMPSharedClause>(
@@ -166,8 +173,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();
 
@@ -193,6 +201,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);
 }
@@ -209,13 +230,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
@@ -838,8 +860,9 @@ mlir::LogicalResult CIRGenFunction::emitOMPParallelMasterDirective(
     return mlir::failure();
 
   mlir::omp::ParallelOperands clauseOps;
+  llvm::SmallVector<const VarDecl *> emittedAsPrivate;
   if (mlir::failed(emitParallelClauses(*this, getCIRGenModule(), builder, begin,
-                                       parallelItem->clauses, clauseOps)))
+                                       parallelItem->clauses, clauseOps, emittedAsPrivate)))
     return mlir::failure();
 
   return emitParallelOp(
@@ -847,7 +870,8 @@ mlir::LogicalResult CIRGenFunction::emitOMPParallelMasterDirective(
       [&]() -> mlir::LogicalResult {
         const CapturedStmt *cs = s.getCapturedStmt(llvm::omp::OMPD_parallel);
         return emitMasterOp(*this, begin, end, cs->getCapturedStmt());
-      });
+      },
+      emittedAsPrivate);
 }
 mlir::LogicalResult CIRGenFunction::emitOMPParallelSectionsDirective(
     const OMPParallelSectionsDirective &s) {
diff --git a/clang/test/CIR/CodeGenOpenMP/not-yet-implemented.c b/clang/test/CIR/CodeGenOpenMP/not-yet-implemented.c
index 9b5e31eb0a5720..4fffaa1e448075 100644
--- a/clang/test/CIR/CodeGenOpenMP/not-yet-implemented.c
+++ b/clang/test/CIR/CodeGenOpenMP/not-yet-implemented.c
@@ -1,6 +1,7 @@
 // RUN: %clang_cc1 -fopenmp -triple x86_64-linux-gnu -fclangir %s -verify -emit-cir -o -
 
 void clash(void);
+int globalX = 10;
 
 void do_things() {
   int i;
@@ -10,6 +11,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) 
+  {}
 
   // expected-error at +1{{ClangIR code gen Not Yet Implemented: OpenMP SINGLE 'copyprivate' clause}}
 #pragma omp single copyprivate(a)
diff --git a/clang/test/CIR/CodeGenOpenMP/parallel.c b/clang/test/CIR/CodeGenOpenMP/parallel.c
index 0a8f7e210a8e72..8d234a526830a1 100644
--- a/clang/test/CIR/CodeGenOpenMP/parallel.c
+++ b/clang/test/CIR/CodeGenOpenMP/parallel.c
@@ -191,3 +191,111 @@ void shared_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