[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
Thu Sep 3 15:51:11 PDT 2026
https://github.com/pedropiin updated https://github.com/llvm/llvm-project/pull/202466
>From ba02ec0eb1425fb4524e5244bb76badeeb40b687 Mon Sep 17 00:00:00 2001
From: pedropiin <pedarosa04 at gmail.com>
Date: Mon, 8 Jun 2026 20:53:51 -0300
Subject: [PATCH 1/4] [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 a42735391629f..52b2b5140bb50 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: }
+}
>From 8bf660554b7337af23fa1fda38d3f16551becca5 Mon Sep 17 00:00:00 2001
From: pedropiin <pedarosa04 at gmail.com>
Date: Mon, 8 Jun 2026 20:53:51 -0300
Subject: [PATCH 2/4] [CIR][OpenMP] Implement lowering for the 'num_threads'
clause for 'parallel' directive
---
clang/test/CIR/CodeGenOpenMP/parallel.c | 18 ++++++++++++++----
1 file changed, 14 insertions(+), 4 deletions(-)
diff --git a/clang/test/CIR/CodeGenOpenMP/parallel.c b/clang/test/CIR/CodeGenOpenMP/parallel.c
index c256e9ff25e71..115bbd47c32fa 100644
--- a/clang/test/CIR/CodeGenOpenMP/parallel.c
+++ b/clang/test/CIR/CodeGenOpenMP/parallel.c
@@ -86,16 +86,26 @@ void proc_bind_parallel() {
}
void num_threads_parallel() {
- // CHECK: omp.parallel num_threads(%{{.*}}: i32) {
+ // CHECK: cir.func{{.*}}@num_threads_parallel
+
+ int numThreads = 4;
+
+ // CHECK-NEXT: %[[NUM_THREADS_ADDR:.*]] = cir.alloca "numThreads"
+ // CHECK-NEXT: %[[CONST_4:.*]] = cir.const #cir.int<4>
+ // CHECK-NEXT: cir.store align(4) %[[CONST_4]], %[[NUM_THREADS_ADDR]]
#pragma omp parallel num_threads(16)
{}
+ // CHECK-NEXT: %[[CONST_16:.*]] = cir.const #cir.int<16>
+ // CHECK-NEXT: %[[CONST_16_I32:.*]] = cir.builtin_int_cast %[[CONST_16]]
+ // CHECK-NEXT: omp.parallel num_threads(%[[CONST_16_I32]] : i32) {
// CHECK-NEXT: omp.terminator
// CHECK-NEXT: }
-int numThreads = 4;
- // CHECK: omp.parallel num_threads(%{{.*}}: i32) {
-#pragma omp parallel num_threads(numThreads)
+ #pragma omp parallel num_threads(numThreads)
{}
+ // CHECK-NEXT: %[[NUM_THREADS_PTR:.*]] = cir.load align(4) %[[NUM_THREADS_ADDR]]
+ // CHECK-NEXT: %[[NUM_THREADS_I32:.*]] = cir.builtin_int_cast %[[NUM_THREADS_PTR]]
+ // CHECK-NEXT: omp.parallel num_threads(%[[NUM_THREADS_I32]] : i32) {
// CHECK-NEXT: omp.terminator
// CHECK-NEXT: }
}
>From 1e9fc051e897d4703444799bbc2c79b0c42c7076 Mon Sep 17 00:00:00 2001
From: pedropiin <pedarosa04 at gmail.com>
Date: Mon, 8 Jun 2026 20:53:51 -0300
Subject: [PATCH 3/4] [CIR][OpenMP] Implement lowering for the 'num_threads'
clause for 'parallel' directive
---
clang/lib/CIR/CodeGen/CIRGenOpenMPClause.cpp | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/clang/lib/CIR/CodeGen/CIRGenOpenMPClause.cpp b/clang/lib/CIR/CodeGen/CIRGenOpenMPClause.cpp
index fab66241de112..22b70a023f101 100644
--- a/clang/lib/CIR/CodeGen/CIRGenOpenMPClause.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenOpenMPClause.cpp
@@ -161,4 +161,5 @@ bool OpenMPClauseEmitter::emitMap(
}
}
return found;
-}
\ No newline at end of file
+}
+
>From f9f29503878ae025de34009446012820a818f7a8 Mon Sep 17 00:00:00 2001
From: pedropiin <pedarosa04 at gmail.com>
Date: Mon, 8 Jun 2026 20:53:51 -0300
Subject: [PATCH 4/4] [CIR][OpenMP] Implement lowering for the 'num_threads'
clause for 'parallel' directive
---
clang/lib/CIR/CodeGen/CIRGenOpenMPClause.cpp | 13 ++++++-------
1 file changed, 6 insertions(+), 7 deletions(-)
diff --git a/clang/lib/CIR/CodeGen/CIRGenOpenMPClause.cpp b/clang/lib/CIR/CodeGen/CIRGenOpenMPClause.cpp
index 22b70a023f101..cc5c76ec49b5f 100644
--- a/clang/lib/CIR/CodeGen/CIRGenOpenMPClause.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenOpenMPClause.cpp
@@ -100,12 +100,12 @@ bool OpenMPClauseEmitter::emitNumThreads(
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});
+ for (const Expr *expr : ntc->getNumThreads()) {
+ mlir::Value numThreadsValue = cgf.emitScalarExpr(expr);
+ auto intType = builder.getIntegerType(32);
+ numThreadsValue = builder.createBuiltinIntCast(numThreadsValue, intType);
+ result.numThreadsVars.push_back(numThreadsValue);
+ }
return true;
}
@@ -162,4 +162,3 @@ bool OpenMPClauseEmitter::emitMap(
}
return found;
}
-
More information about the cfe-commits
mailing list