[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
Wed Sep 23 18:03:59 PDT 2026
https://github.com/pedropiin updated https://github.com/llvm/llvm-project/pull/204999
>From 4993d3a2525313bfbba630e1a02dfeffc27fc0be Mon Sep 17 00:00:00 2001
From: pedropiin <pedarosa04 at gmail.com>
Date: Sun, 21 Jun 2026 15:12:26 -0300
Subject: [PATCH 1/2] [CIR][OpenMP] Implement lowering for the 'if' clause for
'parallel' directive
---
clang/lib/CIR/CodeGen/CIRGenOpenMPClause.cpp | 23 ++++++++++++++++++++
clang/lib/CIR/CodeGen/CIRGenOpenMPClause.h | 2 ++
clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp | 5 +++--
clang/test/CIR/CodeGenOpenMP/parallel.c | 22 +++++++++++++++++++
4 files changed, 50 insertions(+), 2 deletions(-)
diff --git a/clang/lib/CIR/CodeGen/CIRGenOpenMPClause.cpp b/clang/lib/CIR/CodeGen/CIRGenOpenMPClause.cpp
index 16ac4440660b5..8155e4697e32d 100644
--- a/clang/lib/CIR/CodeGen/CIRGenOpenMPClause.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenOpenMPClause.cpp
@@ -93,6 +93,29 @@ bool OpenMPClauseEmitter::emitProcBind(
return false;
}
+bool OpenMPClauseEmitter::emitIf(mlir::omp::IfClauseOps &result) const {
+ for (const OMPClause *clause : clauses) {
+ const auto *ic = dyn_cast<OMPIfClause>(clause);
+ if (!ic)
+ continue;
+
+ Expr *ifCondition = ic->getCondition();
+ mlir::Value ifBoolValue = cgf.evaluateExprAsBool(ifCondition); // !cir.bool
+
+ mlir::Type uIntType = builder.getUIntNTy(1);
+ mlir::Value ifUIntValue =
+ builder.createBoolToInt(ifBoolValue, uIntType); // u1
+
+ mlir::Type intType = builder.getI1Type();
+ mlir::Value ifExpr =
+ builder.createBuiltinIntCast(ifUIntValue, intType); // i1
+
+ result.ifExpr = ifExpr;
+ return true;
+ }
+ return false;
+}
+
bool OpenMPClauseEmitter::emitMap(
mlir::omp::MapClauseOps &result,
llvm::SmallVectorImpl<const VarDecl *> *mapSyms) const {
diff --git a/clang/lib/CIR/CodeGen/CIRGenOpenMPClause.h b/clang/lib/CIR/CodeGen/CIRGenOpenMPClause.h
index 54c7366b1d769..ee048296f1946 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 emitIf(mlir::omp::IfClauseOps &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..c60b1bd12e1f8 100644
--- a/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp
@@ -39,10 +39,11 @@ CIRGenFunction::emitOMPParallelDirective(const OMPParallelDirective &s) {
mlir::omp::ParallelOperands clauseOps;
OpenMPClauseEmitter ce(*this, getCIRGenModule(), builder, begin, s.clauses());
ce.emitProcBind(clauseOps);
- ce.emitNYI</*supported=*/OMPProcBindClause>(
+ ce.emitIf(clauseOps);
+ ce.emitNYI</*supported=*/OMPProcBindClause, OMPIfClause>(
/*nyi=*/OpenMPNYIClauseList<
OMPAllocateClause, OMPCopyinClause, OMPDefaultClause,
- OMPFirstprivateClause, OMPIfClause, OMPNumThreadsClause,
+ OMPFirstprivateClause, OMPNumThreadsClause,
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 4fb7997ab6635..11804de1010a3 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: }
+}
>From 9cd8fbad7e31d701aadb978fb5b8b6f64e176078 Mon Sep 17 00:00:00 2001
From: pedropiin <pedarosa04 at gmail.com>
Date: Sun, 21 Jun 2026 15:12:26 -0300
Subject: [PATCH 2/2] [CIR][OpenMP] Implement lowering for the 'if' clause for
'parallel' directive
---
clang/lib/CIR/CodeGen/CIRGenOpenMPClause.cpp | 7 ++-
clang/lib/CIR/CodeGen/CIRGenOpenMPClause.h | 3 +-
clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp | 12 ++--
.../CIR/CodeGenOpenMP/not-yet-implemented.c | 5 --
clang/test/CIR/CodeGenOpenMP/parallel.c | 62 +++++++++++++++++--
5 files changed, 70 insertions(+), 19 deletions(-)
diff --git a/clang/lib/CIR/CodeGen/CIRGenOpenMPClause.cpp b/clang/lib/CIR/CodeGen/CIRGenOpenMPClause.cpp
index 8155e4697e32d..2f6395486939f 100644
--- a/clang/lib/CIR/CodeGen/CIRGenOpenMPClause.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenOpenMPClause.cpp
@@ -93,12 +93,17 @@ bool OpenMPClauseEmitter::emitProcBind(
return false;
}
-bool OpenMPClauseEmitter::emitIf(mlir::omp::IfClauseOps &result) const {
+bool OpenMPClauseEmitter::emitIf(mlir::omp::IfClauseOps &result,
+ llvm::omp::Directive directiveName) const {
for (const OMPClause *clause : clauses) {
const auto *ic = dyn_cast<OMPIfClause>(clause);
if (!ic)
continue;
+ if (!(ic->getNameModifier() == llvm::omp::Directive::OMPD_unknown) &&
+ ic->getNameModifier() != directiveName)
+ continue;
+
Expr *ifCondition = ic->getCondition();
mlir::Value ifBoolValue = cgf.evaluateExprAsBool(ifCondition); // !cir.bool
diff --git a/clang/lib/CIR/CodeGen/CIRGenOpenMPClause.h b/clang/lib/CIR/CodeGen/CIRGenOpenMPClause.h
index ee048296f1946..aa6c6653bc540 100644
--- a/clang/lib/CIR/CodeGen/CIRGenOpenMPClause.h
+++ b/clang/lib/CIR/CodeGen/CIRGenOpenMPClause.h
@@ -42,7 +42,8 @@ class OpenMPClauseEmitter {
bool emitProcBind(mlir::omp::ProcBindClauseOps &result) const;
- bool emitIf(mlir::omp::IfClauseOps &result) const;
+ bool emitIf(mlir::omp::IfClauseOps &result,
+ llvm::omp::Directive directiveName) const;
/// Emit map clauses. The optional \p mapSyms parameter collects the
/// VarDecls corresponding to each map operand.
diff --git a/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp b/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp
index c60b1bd12e1f8..3e9355e619bc1 100644
--- a/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp
@@ -38,13 +38,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.emitProcBind(clauseOps);
- ce.emitIf(clauseOps);
- ce.emitNYI</*supported=*/OMPProcBindClause, OMPIfClause>(
- /*nyi=*/OpenMPNYIClauseList<
- OMPAllocateClause, OMPCopyinClause, OMPDefaultClause,
- OMPFirstprivateClause, OMPNumThreadsClause,
- OMPPrivateClause, OMPReductionClause, OMPSharedClause>{},
+ ce.emitNYI</*supported=*/OMPIfClause, OMPProcBindClause>(
+ /*nyi=*/OpenMPNYIClauseList<OMPAllocateClause, OMPCopyinClause,
+ OMPDefaultClause, OMPFirstprivateClause,
+ OMPNumThreadsClause, OMPPrivateClause,
+ OMPReductionClause, OMPSharedClause>{},
llvm::omp::Directive::OMPD_parallel);
auto parallelOp = mlir::omp::ParallelOp::create(builder, begin, clauseOps);
diff --git a/clang/test/CIR/CodeGenOpenMP/not-yet-implemented.c b/clang/test/CIR/CodeGenOpenMP/not-yet-implemented.c
index ed4240727ff64..93a17494ad605 100644
--- a/clang/test/CIR/CodeGenOpenMP/not-yet-implemented.c
+++ b/clang/test/CIR/CodeGenOpenMP/not-yet-implemented.c
@@ -8,9 +8,4 @@ void do_things() {
// expected-error at +1{{ClangIR code gen Not Yet Implemented: OpenMP OMPSingleDirective}}
#pragma omp single
{}
-
- int i;
- // expected-error at +1{{ClangIR code gen Not Yet Implemented: OpenMP PARALLEL 'if' clause}}
-#pragma omp parallel if(i)
- {}
}
diff --git a/clang/test/CIR/CodeGenOpenMP/parallel.c b/clang/test/CIR/CodeGenOpenMP/parallel.c
index 11804de1010a3..68b78ed1ce7ee 100644
--- a/clang/test/CIR/CodeGenOpenMP/parallel.c
+++ b/clang/test/CIR/CodeGenOpenMP/parallel.c
@@ -86,23 +86,73 @@ void proc_bind_parallel() {
}
void if_parallel() {
- // CHECK: omp.parallel if(%{{.*}}) {
-#pragma omp parallel if (1)
+ // CHECK: cir.func{{.*}}@if_parallel
+
+ int validCondition = 10;
+ int invalidCondition = 0;
+ void *nullPtr = ((void *)0);
+
+ // CHECK-NEXT: %[[VALID_CONDITION_ADDR:.*]] = cir.alloca "validCondition"
+ // CHECK-NEXT: %[[INVALID_CONDITION_ADDR:.*]] = cir.alloca "invalidCondition"
+ // CHECK-NEXT: %[[NULL_ADDR:.*]] = cir.alloca "nullPtr"
+
+ #pragma omp parallel if (1)
{}
+ // CHECK: %[[ONE_CONST:.*]] = cir.const #cir.int<1>
+ // CHECK-NEXT: %[[ONE_BOOL:.*]] = cir.cast int_to_bool %[[ONE_CONST]]
+ // CHECK-NEXT: %[[ONE_U1:.*]] = cir.cast bool_to_int %[[ONE_BOOL]]
+ // CHECK-NEXT: %[[ONE_I1:.*]] = cir.builtin_int_cast %[[ONE_U1]]
+ // CHECK-NEXT: omp.parallel if(%[[ONE_I1]]) {
// CHECK-NEXT: omp.terminator
// CHECK-NEXT: }
-int validCondition = 10;
- // CHECK: omp.parallel if(%{{.*}}) {
#pragma omp parallel if (validCondition)
{}
+ // CHECK-NEXT: %[[VALID_CONDITION_PTR:.*]] = cir.load align(4) %[[VALID_CONDITION_ADDR]]
+ // CHECK-NEXT: %[[VALID_CONDITION_BOOL:.*]] = cir.cast int_to_bool %[[VALID_CONDITION_PTR]]
+ // CHECK-NEXT: %[[VALID_CONDITION_U1:.*]] = cir.cast bool_to_int %[[VALID_CONDITION_BOOL]]
+ // CHECK-NEXT: %[[VALID_CONDITION_I1:.*]] = cir.builtin_int_cast %[[VALID_CONDITION_U1]]
+ // CHECK-NEXT: omp.parallel if(%[[VALID_CONDITION_I1]]) {
// CHECK-NEXT: omp.terminator
// CHECK-NEXT: }
-void *nullPtr = ((void *)0);
- // CHECK: omp.parallel if(%{{.*}}) {
#pragma omp parallel if (nullPtr)
{}
+ // CHECK-NEXT: %[[NULL_PTR:.*]] = cir.load align(8) %[[NULL_ADDR]]
+ // CHECK-NEXT: %[[NULL_BOOL:.*]] = cir.cast ptr_to_bool %[[NULL_PTR]]
+ // CHECK-NEXT: %[[NULL_U1:.*]] = cir.cast bool_to_int %[[NULL_BOOL]]
+ // CHECK-NEXT: %[[NULL_I1:.*]] = cir.builtin_int_cast %[[NULL_U1]]
+ // CHECK-NEXT: omp.parallel if(%[[NULL_I1]]) {
+ // CHECK-NEXT: omp.terminator
+ // CHECK-NEXT: }
+
+#pragma omp parallel if (invalidCondition)
+ {}
+ // CHECK-NEXT: %[[INVALID_CONDITION_PTR:.*]] = cir.load align(4) %[[INVALID_CONDITION_ADDR]]
+ // CHECK-NEXT: %[[INVALID_CONDITION_BOOL:.*]] = cir.cast int_to_bool %[[INVALID_CONDITION_PTR]]
+ // CHECK-NEXT: %[[INVALID_CONDITION_U1:.*]] = cir.cast bool_to_int %[[INVALID_CONDITION_BOOL]]
+ // CHECK-NEXT: %[[INVALID_CONDITION_I1:.*]] = cir.builtin_int_cast %[[INVALID_CONDITION_U1]]
+ // CHECK-NEXT: omp.parallel if(%[[INVALID_CONDITION_I1]]) {
+ // CHECK-NEXT: omp.terminator
+ // CHECK-NEXT: }
+
+#pragma omp parallel if (parallel: validCondition)
+ {}
+ // CHECK-NEXT: %[[VALID_CONDITION_DIRECTIVE_PTR:.*]] = cir.load align(4) %[[VALID_CONDITION_ADDR]]
+ // CHECK-NEXT: %[[VALID_CONDITION_DIRECTIVE_BOOL:.*]] = cir.cast int_to_bool %[[VALID_CONDITION_DIRECTIVE_PTR]]
+ // CHECK-NEXT: %[[VALID_CONDITION_DIRECTIVE_U1:.*]] = cir.cast bool_to_int %[[VALID_CONDITION_DIRECTIVE_BOOL]]
+ // CHECK-NEXT: %[[VALID_CONDITION_DIRECTIVE_I1:.*]] = cir.builtin_int_cast %[[VALID_CONDITION_DIRECTIVE_U1]]
+ // CHECK-NEXT: omp.parallel if(%[[VALID_CONDITION_DIRECTIVE_I1]]) {
+ // CHECK-NEXT: omp.terminator
+ // CHECK-NEXT: }
+
+#pragma omp parallel if (parallel: invalidCondition)
+ {}
+ // CHECK-NEXT: %[[INVALID_CONDITION_DIRECTIVE_PTR:.*]] = cir.load align(4) %[[INVALID_CONDITION_ADDR]]
+ // CHECK-NEXT: %[[INVALID_CONDITION_DIRECTIVE_BOOL:.*]] = cir.cast int_to_bool %[[INVALID_CONDITION_DIRECTIVE_PTR]]
+ // 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]]) {
// CHECK-NEXT: omp.terminator
// CHECK-NEXT: }
}
More information about the cfe-commits
mailing list