[clang] [CIR][OpenMP] Implement lowering for the 'if' clause for 'parallel' d… (PR #204999)
Pedro da Rosa Pinheiro via cfe-commits
cfe-commits at lists.llvm.org
Sun Jun 21 11:17:59 PDT 2026
https://github.com/pedropiin created https://github.com/llvm/llvm-project/pull/204999
Implementing the lowering of the `if` clause for the `#pragma omp parallel` directive. The clause accepts an `Expr` and adapts the `mlir::omp::ParallelOp.getIfExprMutable()` accordingly. If the `Expr` is later evaluated to a non-zero value, the region is executed in parallel.
Followed the same approach as the one in my other PR #202466 and based myself off of PR #172501 that implements the lowering for the `proc_bind` clause. Also following the adaptations done in PR #181841 to support the use of `UnrealizedConversionCast` when enabling the CIR pipeline. PR #196363 implemented a separate approach to deal with this, but got blocked.
### Evidence:
Output of: `python3 build/bin/llvm-lit -sv clang/test/CIR/CodeGenOpenMP/parallel.c`
```
/CodeGenOpenMP/parallel.c
llvm-lit: /home/pedro/dev/projects/llvm-project-fork/llvm/utils/lit/lit/llvm/config.py:569: note: using clang: /home/pedro/dev/projects/llvm-project-fork/build/bin/clang
Testing Time: 0.05s
Total Discovered Tests: 1
Passed: 1 (100.00%)
```
>From 6a6ee355c2b8e979578799fd827871d93f887b58 Mon Sep 17 00:00:00 2001
From: pedropiin <pedarosa04 at gmail.com>
Date: Sun, 21 Jun 2026 15:12:26 -0300
Subject: [PATCH] [CIR][OpenMP] Implement lowering for the 'if' clause for
'parallel' directive
---
clang/lib/CIR/CodeGen/CIRGenOpenMPClause.cpp | 20 +++++++++++++++++
.../CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp | 3 +++
clang/test/CIR/CodeGenOpenMP/parallel.c | 22 +++++++++++++++++++
3 files changed, 45 insertions(+)
diff --git a/clang/lib/CIR/CodeGen/CIRGenOpenMPClause.cpp b/clang/lib/CIR/CodeGen/CIRGenOpenMPClause.cpp
index a0f0ea9299c8d..5ed80c7a01b5f 100644
--- a/clang/lib/CIR/CodeGen/CIRGenOpenMPClause.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenOpenMPClause.cpp
@@ -66,6 +66,26 @@ class OpenMPClauseCIREmitter final
}
}
+ void VisitOMPIfClause(const OMPIfClause *clause) {
+ if constexpr (std::is_same_v<OpTy, mlir::omp::ParallelOp>) {
+ Expr *ifExpr = clause->getCondition();
+ mlir::Value ifValue = cgf.emitScalarExpr(ifExpr);
+ mlir::Location ifLoc =
+ cgf.cgm.getLoc(ifExpr->getBeginLoc());
+
+ mlir::Type stdBoolType = builder.getI1Type();
+
+ mlir::Value unrealizedCastIf =
+ mlir::UnrealizedConversionCastOp::create(builder, ifLoc, stdBoolType, ifValue).getResult(0);
+
+ operation.getIfExprMutable().append(unrealizedCastIf);
+ } else {
+ cgf.cgm.errorNYI(
+ clause->getBeginLoc(),
+ "OMPProcBindClause 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 c844375a000e0..2dc8dc45258dc 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"
@@ -3695,6 +3696,7 @@ void ConvertCIRToLLVMPass::runOnOperation() {
mlir::populateOpenMPToLLVMConversionPatterns(converter, patterns);
target.addIllegalDialect<mlir::BuiltinDialect, cir::CIRDialect,
mlir::func::FuncDialect>();
+ target.addLegalOp<mlir::UnrealizedConversionCastOp>();
llvm::SmallVector<mlir::Operation *> ops;
ops.push_back(module);
@@ -4935,6 +4937,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..b3b82fbd3ea23 100644
--- a/clang/test/CIR/CodeGenOpenMP/parallel.c
+++ b/clang/test/CIR/CodeGenOpenMP/parallel.c
@@ -84,3 +84,25 @@ void proc_bind_parallel() {
// CHECK-NEXT: omp.terminator
// CHECK-NEXT: }
}
+
+void if_parallel() {
+ // CHECK: omp.parallel if(%{{.*}}) {
+#pragma omp parallel if (1)
+ {}
+ // CHECK-NEXT: omp.terminator
+ // CHECK-NEXT: }
+
+int validCondition = 10;
+ // CHECK: omp.parallel if(%{{.*}}) {
+#pragma omp parallel if (validCondition)
+ {}
+ // CHECK-NEXT: omp.terminator
+ // CHECK-NEXT: }
+
+void *nullPtr = ((void *)0);
+ // CHECK: omp.parallel if(%{{.*}}) {
+#pragma omp parallel if (nullPtr)
+ {}
+ // CHECK-NEXT: omp.terminator
+ // CHECK-NEXT: }
+}
More information about the cfe-commits
mailing list