https://github.com/pedropiin updated https://github.com/llvm/llvm-project/pull/202466
>From 405cf63947e469a55d12ca90ccbaaa496059ef65 Mon Sep 17 00:00:00 2001 From: pedropiin <[email protected]> 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 +++++----- .../CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp | 4 ++++ clang/test/CIR/CodeGenOpenMP/parallel.c | 15 +++++++++++++ 5 files changed, 47 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/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp index 7748c81664a40..676eebe5609a8 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" @@ -3899,6 +3900,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); @@ -5138,6 +5141,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 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: } +} _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
