[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 Jun 18 16:08:12 PDT 2026
https://github.com/pedropiin updated https://github.com/llvm/llvm-project/pull/202466
>From 01c1b77e3f5c636789b37bf013115751bb5ac293 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 | 24 +++++++++++++++++++
.../CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp | 4 ++++
clang/test/CIR/CodeGenOpenMP/parallel.c | 15 ++++++++++++
3 files changed, 43 insertions(+)
diff --git a/clang/lib/CIR/CodeGen/CIRGenOpenMPClause.cpp b/clang/lib/CIR/CodeGen/CIRGenOpenMPClause.cpp
index a0f0ea9299c8d..216bb867159f1 100644
--- a/clang/lib/CIR/CodeGen/CIRGenOpenMPClause.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenOpenMPClause.cpp
@@ -66,6 +66,30 @@ class OpenMPClauseCIREmitter final
}
}
+ void VisitOMPNumThreadsClause(const OMPNumThreadsClause *clause) {
+ if constexpr (std::is_same_v<OpTy, mlir::omp::ParallelOp>) {
+ Expr *numThreadsExpr = clause->getNumThreads();
+ mlir::Value numThreadsValue = cgf.emitScalarExpr(numThreadsExpr);
+ mlir::Location numThreadsLoc =
+ cgf.cgm.getLoc(numThreadsExpr->getBeginLoc());
+
+ cir::IntType cirIntType =
+ mlir::cast<cir::IntType>(numThreadsValue.getType());
+ mlir::Type stdIntType = builder.getIntegerType(cirIntType.getWidth());
+
+ mlir::Value unrealizedCastNumThreads =
+ mlir::UnrealizedConversionCastOp::create(builder, numThreadsLoc,
+ stdIntType, numThreadsValue)
+ .getResult(0);
+
+ operation.getNumThreadsVarsMutable().append(unrealizedCastNumThreads);
+ } else {
+ cgf.cgm.errorNYI(
+ clause->getBeginLoc(),
+ "OMPNumThreadsClause unimplemented on this directive kind");
+ }
+ }
+
void emitClauses(ArrayRef<const OMPClause *> clauses) {
for (const auto *c : clauses)
this->Visit(c);
diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
index c4e98e299dfc1..631bae0915629 100644
--- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
+++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
@@ -17,6 +17,7 @@
#include "mlir/Conversion/LLVMCommon/TypeConverter.h"
#include "mlir/Conversion/OpenMPToLLVM/ConvertOpenMPToLLVM.h"
+#include "mlir/Conversion/ReconcileUnrealizedCasts/ReconcileUnrealizedCasts.h"
#include "mlir/Dialect/DLTI/DLTI.h"
#include "mlir/Dialect/Func/IR/FuncOps.h"
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
@@ -3733,6 +3734,8 @@ void ConvertCIRToLLVMPass::runOnOperation() {
target.addIllegalDialect<mlir::BuiltinDialect, cir::CIRDialect,
mlir::func::FuncDialect>();
+ target.addLegalOp<mlir::UnrealizedConversionCastOp>();
+
llvm::SmallVector<mlir::Operation *> ops;
ops.push_back(module);
cir::collectUnreachable(module, ops);
@@ -5022,6 +5025,7 @@ void populateCIRToLLVMPasses(mlir::OpPassManager &pm) {
mlir::populateCIRPreLoweringPasses(pm);
pm.addPass(mlir::omp::createMarkDeclareTargetPass());
pm.addPass(createConvertCIRToLLVMPass());
+ pm.addPass(mlir::createReconcileUnrealizedCastsPass());
}
std::unique_ptr<llvm::Module>
diff --git a/clang/test/CIR/CodeGenOpenMP/parallel.c b/clang/test/CIR/CodeGenOpenMP/parallel.c
index d04784879540a..e579cd9b6019e 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