[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 Sep 26 08:02:43 PDT 2026
https://github.com/pedropiin updated https://github.com/llvm/llvm-project/pull/202466
>From aa5efb0c126dd4c9f5f5b6c38f7c58f546c5c126 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 | 6 ++++--
clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp | 4 ++--
clang/test/CIR/CodeGenOpenMP/parallel.c | 13 ++++++++++++
4 files changed, 39 insertions(+), 5 deletions(-)
diff --git a/clang/lib/CIR/CodeGen/CIRGenOpenMPClause.cpp b/clang/lib/CIR/CodeGen/CIRGenOpenMPClause.cpp
index 2f6395486939f..ec7a2e26b93d2 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::emitIf(mlir::omp::IfClauseOps &result,
llvm::omp::Directive directiveName) const {
for (const OMPClause *clause : clauses) {
@@ -170,4 +189,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 aa6c6653bc540..6a731a9128986 100644
--- a/clang/lib/CIR/CodeGen/CIRGenOpenMPClause.h
+++ b/clang/lib/CIR/CodeGen/CIRGenOpenMPClause.h
@@ -40,8 +40,6 @@ class OpenMPClauseEmitter {
llvm::ArrayRef<const OMPClause *> clauses)
: cgf(cgf), cgm(cgm), builder(builder), loc(loc), clauses(clauses) {}
- bool emitProcBind(mlir::omp::ProcBindClauseOps &result) const;
-
bool emitIf(mlir::omp::IfClauseOps &result,
llvm::omp::Directive directiveName) const;
@@ -50,6 +48,10 @@ class OpenMPClauseEmitter {
bool emitMap(mlir::omp::MapClauseOps &result,
llvm::SmallVectorImpl<const VarDecl *> *mapSyms = nullptr) const;
+ bool emitNumThreads(mlir::omp::NumThreadsClauseOps &result) const;
+
+ bool emitProcBind(mlir::omp::ProcBindClauseOps &result) const;
+
/// Verify the clauses of a directive to make sure all legal cases are either
/// implemented or give a NYI error. The \p SupportedClauses and \p
/// NYIClauses type lists must be disjoint and cover all clauses eligible for
diff --git a/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp b/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp
index 73afa76d94394..93b315bb7fef2 100644
--- a/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp
@@ -40,10 +40,10 @@ CIRGenFunction::emitOMPParallelDirective(const OMPParallelDirective &s) {
OpenMPClauseEmitter ce(*this, getCIRGenModule(), builder, begin, s.clauses());
ce.emitIf(clauseOps, llvm::omp::Directive::OMPD_parallel);
ce.emitProcBind(clauseOps);
- ce.emitNYI</*supported=*/OMPIfClause, OMPProcBindClause>(
+ ce.emitNYI</*supported=*/OMPIfClause, OMPNumThreadsClause, OMPProcBindClause>(
/*nyi=*/OpenMPNYIClauseList<OMPAllocateClause, OMPCopyinClause,
OMPDefaultClause, OMPFirstprivateClause,
- OMPNumThreadsClause, OMPPrivateClause,
+ OMPPrivateClause,
OMPReductionClause, OMPSharedClause>{},
llvm::omp::Directive::OMPD_parallel);
diff --git a/clang/test/CIR/CodeGenOpenMP/parallel.c b/clang/test/CIR/CodeGenOpenMP/parallel.c
index 68b78ed1ce7ee..081fb9d99fb42 100644
--- a/clang/test/CIR/CodeGenOpenMP/parallel.c
+++ b/clang/test/CIR/CodeGenOpenMP/parallel.c
@@ -153,6 +153,19 @@ void if_parallel() {
// CHECK-NEXT: %[[INVALID_CONDITION_DIRECTIVE_U1:.*]] = cir.cast bool_to_int %[[INVALID_CONDITION_DIRECTIVE_BOOL]]
// CHECK-NEXT: %[[INVALID_CONDITION_DIRECTIVE_I1:.*]] = cir.builtin_int_cast %[[INVALID_CONDITION_DIRECTIVE_U1]]
// CHECK-NEXT: omp.parallel if(%[[INVALID_CONDITION_DIRECTIVE_I1]]) {
+}
+
+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 459f0a50747a6ee99d1036df7f01001432192077 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 081fb9d99fb42..49c5e10db7183 100644
--- a/clang/test/CIR/CodeGenOpenMP/parallel.c
+++ b/clang/test/CIR/CodeGenOpenMP/parallel.c
@@ -156,16 +156,26 @@ void if_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 e71a9c43f42be88f40b492b8d748f5303232549c 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 ec7a2e26b93d2..f0857e587dcad 100644
--- a/clang/lib/CIR/CodeGen/CIRGenOpenMPClause.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenOpenMPClause.cpp
@@ -189,4 +189,5 @@ bool OpenMPClauseEmitter::emitMap(
}
}
return found;
-}
\ No newline at end of file
+}
+
>From d5042a0ff6bfb3b9a17e978dd5f297395c74a830 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 ++++++-------
clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp | 5 +++--
2 files changed, 9 insertions(+), 9 deletions(-)
diff --git a/clang/lib/CIR/CodeGen/CIRGenOpenMPClause.cpp b/clang/lib/CIR/CodeGen/CIRGenOpenMPClause.cpp
index f0857e587dcad..2e64632f037c5 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;
}
@@ -190,4 +190,3 @@ bool OpenMPClauseEmitter::emitMap(
}
return found;
}
-
diff --git a/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp b/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp
index 93b315bb7fef2..eca46da4658b3 100644
--- a/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp
@@ -39,12 +39,13 @@ CIRGenFunction::emitOMPParallelDirective(const OMPParallelDirective &s) {
mlir::omp::ParallelOperands clauseOps;
OpenMPClauseEmitter ce(*this, getCIRGenModule(), builder, begin, s.clauses());
ce.emitIf(clauseOps, llvm::omp::Directive::OMPD_parallel);
+ ce.emitNumThreads(clauseOps);
ce.emitProcBind(clauseOps);
ce.emitNYI</*supported=*/OMPIfClause, OMPNumThreadsClause, OMPProcBindClause>(
/*nyi=*/OpenMPNYIClauseList<OMPAllocateClause, OMPCopyinClause,
OMPDefaultClause, OMPFirstprivateClause,
- OMPPrivateClause,
- OMPReductionClause, OMPSharedClause>{},
+ OMPPrivateClause, OMPReductionClause,
+ OMPSharedClause>{},
llvm::omp::Directive::OMPD_parallel);
auto parallelOp = mlir::omp::ParallelOp::create(builder, begin, clauseOps);
More information about the cfe-commits
mailing list