[clang] [CIR][OpenMP] Implement lowering for the 'num_threads' clause for 'parallel' directive (PR #202466)
Pedro da Rosa Pinheiro via cfe-commits
cfe-commits at lists.llvm.org
Sat Jul 18 16:37:02 PDT 2026
https://github.com/pedropiin updated https://github.com/llvm/llvm-project/pull/202466
>From 76acfb1baa6ef80a3fba3410d2b7c88cb79698da Mon Sep 17 00:00:00 2001
From: pedropiin <pedarosa04 at gmail.com>
Date: Mon, 8 Jun 2026 20:53:51 -0300
Subject: [PATCH] [CIR][OpenMP] Implement lowering for the 'num_threads' clause
for 'parallel' directive
---
clang/lib/CIR/CodeGen/CIRGenOpenMPClause.cpp | 21 +++++++++++++++++++-
clang/lib/CIR/CodeGen/CIRGenOpenMPClause.h | 2 ++
clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp | 11 +++++-----
clang/test/CIR/CodeGenOpenMP/parallel.c | 15 ++++++++++++++
4 files changed, 43 insertions(+), 6 deletions(-)
diff --git a/clang/lib/CIR/CodeGen/CIRGenOpenMPClause.cpp b/clang/lib/CIR/CodeGen/CIRGenOpenMPClause.cpp
index 16ac4440660b5..fab66241de112 100644
--- a/clang/lib/CIR/CodeGen/CIRGenOpenMPClause.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenOpenMPClause.cpp
@@ -93,6 +93,25 @@ bool OpenMPClauseEmitter::emitProcBind(
return false;
}
+bool OpenMPClauseEmitter::emitNumThreads(
+ mlir::omp::NumThreadsClauseOps &result) const {
+ for (const OMPClause *clause : clauses) {
+ const auto *ntc = dyn_cast<OMPNumThreadsClause>(clause);
+ if (!ntc)
+ continue;
+
+ const Expr *numThreadsExpr = ntc->getNumThreads();
+ mlir::Value numThreadsValue = cgf.emitScalarExpr(numThreadsExpr);
+ auto intType = builder.getIntegerType(32); // Assuming 32-bit integer type.
+ numThreadsValue = builder.createBuiltinIntCast(numThreadsValue, intType);
+
+ result.numThreadsVars.assign({numThreadsValue});
+
+ return true;
+ }
+ return false;
+}
+
bool OpenMPClauseEmitter::emitMap(
mlir::omp::MapClauseOps &result,
llvm::SmallVectorImpl<const VarDecl *> *mapSyms) const {
@@ -142,4 +161,4 @@ bool OpenMPClauseEmitter::emitMap(
}
}
return found;
-}
+}
\ No newline at end of file
diff --git a/clang/lib/CIR/CodeGen/CIRGenOpenMPClause.h b/clang/lib/CIR/CodeGen/CIRGenOpenMPClause.h
index 54c7366b1d769..12fa6d9459a19 100644
--- a/clang/lib/CIR/CodeGen/CIRGenOpenMPClause.h
+++ b/clang/lib/CIR/CodeGen/CIRGenOpenMPClause.h
@@ -42,6 +42,8 @@ class OpenMPClauseEmitter {
bool emitProcBind(mlir::omp::ProcBindClauseOps &result) const;
+ bool emitNumThreads(mlir::omp::NumThreadsClauseOps &result) const;
+
/// Emit map clauses. The optional \p mapSyms parameter collects the
/// VarDecls corresponding to each map operand.
bool emitMap(mlir::omp::MapClauseOps &result,
diff --git a/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp b/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp
index 17a1fb8090f5c..cc9fa6cfe7268 100644
--- a/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp
@@ -39,11 +39,12 @@ CIRGenFunction::emitOMPParallelDirective(const OMPParallelDirective &s) {
mlir::omp::ParallelOperands clauseOps;
OpenMPClauseEmitter ce(*this, getCIRGenModule(), builder, begin, s.clauses());
ce.emitProcBind(clauseOps);
- ce.emitNYI</*supported=*/OMPProcBindClause>(
- /*nyi=*/OpenMPNYIClauseList<
- OMPAllocateClause, OMPCopyinClause, OMPDefaultClause,
- OMPFirstprivateClause, OMPIfClause, OMPNumThreadsClause,
- OMPPrivateClause, OMPReductionClause, OMPSharedClause>{},
+ ce.emitNumThreads(clauseOps);
+ ce.emitNYI</*supported=*/OMPProcBindClause, OMPNumThreadsClause>(
+ /*nyi=*/OpenMPNYIClauseList<OMPAllocateClause, OMPCopyinClause,
+ OMPDefaultClause, OMPFirstprivateClause,
+ OMPIfClause, OMPPrivateClause,
+ OMPReductionClause, OMPSharedClause>{},
llvm::omp::Directive::OMPD_parallel);
auto parallelOp = mlir::omp::ParallelOp::create(builder, begin, clauseOps);
diff --git a/clang/test/CIR/CodeGenOpenMP/parallel.c b/clang/test/CIR/CodeGenOpenMP/parallel.c
index 36a48b38ee789..c256e9ff25e71 100644
--- a/clang/test/CIR/CodeGenOpenMP/parallel.c
+++ b/clang/test/CIR/CodeGenOpenMP/parallel.c
@@ -84,3 +84,18 @@ void proc_bind_parallel() {
// CHECK-NEXT: omp.terminator
// CHECK-NEXT: }
}
+
+void num_threads_parallel() {
+ // CHECK: omp.parallel num_threads(%{{.*}}: i32) {
+ #pragma omp parallel num_threads(16)
+ {}
+ // CHECK-NEXT: omp.terminator
+ // CHECK-NEXT: }
+
+int numThreads = 4;
+ // CHECK: omp.parallel num_threads(%{{.*}}: i32) {
+#pragma omp parallel num_threads(numThreads)
+ {}
+ // CHECK-NEXT: omp.terminator
+ // CHECK-NEXT: }
+}
More information about the cfe-commits
mailing list