[clang] [CIR] [OpenMP] Implement codegen for OpenMP `master` directive (PR #238260)
Soham Karandikar via cfe-commits
cfe-commits at lists.llvm.org
Sun Nov 15 23:06:46 PST 2026
https://github.com/skadewdl3 updated https://github.com/llvm/llvm-project/pull/238260
>From 1673c00ec1ab6e22d987843e26b05e56932f619b Mon Sep 17 00:00:00 2001
From: Soham Karandikar <sohamk10 at gmail.com>
Date: Tue, 3 Nov 2026 02:48:42 -0600
Subject: [PATCH 1/9] Added codegen for master directive
---
clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp | 29 ++++++++++++++++++++--
1 file changed, 27 insertions(+), 2 deletions(-)
diff --git a/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp b/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp
index 9d937f5ed67f90..7c15bfef4d68b2 100644
--- a/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp
@@ -105,6 +105,22 @@ emitParallelOp(CIRGenFunction &cgf, const DirectiveTy &s,
return res;
}
+static mlir::LogicalResult emitMasterOp(CIRGenFunction &cgf, mlir::Location begin, mlir::Location end, llvm::function_ref<mlir::LogicalResult()> emitBody){
+ CIRGenBuilderTy &builder = cgf.getBuilder();
+ CIRGenModule &cgm = cgf.getCIRGenModule();
+
+ auto masterOp = mlir::omp::MasterOp::create(builder, begin);
+
+ mlir::Block &block = masterOp.getRegion().emplaceBlock();
+ mlir::OpBuilder::InsertionGuard guard(builder);
+ builder.setInsertionPointToEnd(&block);
+
+ CIRGenFunction::LexicalScope ls{cgf, begin, builder.getInsertionBlock()};
+ mlir::LogicalResult res = emitBody();
+ mlir::omp::TerminatorOp::create(builder, end);
+ return res;
+}
+
mlir::LogicalResult
CIRGenFunction::emitOMPParallelDirective(const OMPParallelDirective &s) {
mlir::Location begin = getLoc(s.getBeginLoc());
@@ -206,8 +222,17 @@ CIRGenFunction::emitOMPSingleDirective(const OMPSingleDirective &s) {
}
mlir::LogicalResult
CIRGenFunction::emitOMPMasterDirective(const OMPMasterDirective &s) {
- getCIRGenModule().errorNYI(s.getSourceRange(), "OpenMP OMPMasterDirective");
- return mlir::failure();
+ mlir::Location begin = getLoc(s.getBeginLoc());
+ mlir::Location end = getLoc(s.getEndLoc());
+
+ return emitMasterOp(
+ *this, begin, end,
+ [&]() -> mlir::LogicalResult {
+ // emitStmt() rejects CapturedStmt directly; the parent construct
+ // must unwrap it, so emit the inner statement instead.
+ const Stmt *innerStmt = s.getAssociatedStmt();
+ return emitStmt(innerStmt, /*useCurrentScope=*/true);
+ });
}
mlir::LogicalResult
CIRGenFunction::emitOMPCriticalDirective(const OMPCriticalDirective &s) {
>From d065866c1d0f27c6ab1a2af40c5f9ece74f8ef62 Mon Sep 17 00:00:00 2001
From: Soham Karandikar <sohamk10 at gmail.com>
Date: Tue, 3 Nov 2026 03:22:49 -0600
Subject: [PATCH 2/9] Added lit-test
---
clang/test/CIR/CodeGenOpenMP/master.c | 22 ++++++++++++++++++++++
1 file changed, 22 insertions(+)
create mode 100644 clang/test/CIR/CodeGenOpenMP/master.c
diff --git a/clang/test/CIR/CodeGenOpenMP/master.c b/clang/test/CIR/CodeGenOpenMP/master.c
new file mode 100644
index 00000000000000..31bce4726b9275
--- /dev/null
+++ b/clang/test/CIR/CodeGenOpenMP/master.c
@@ -0,0 +1,22 @@
+
+// RUN: %clang_cc1 -fopenmp -triple x86_64-linux-gnu -fclangir -emit-cir %s -o - | FileCheck %s --check-prefix=CIR
+
+void before(void);
+void body(void);
+void after(void);
+
+// CIR-LABEL: cir.func{{.*}}@simple_master(
+void simple_master(void) {
+ // CIR: cir.call @before
+ before();
+
+ // CIR-NEXT: omp.master {
+#pragma omp master
+ body();
+ // CIR-NEXT: cir.call @body
+ // CIR-NEXT: omp.terminator
+ // CIR-NEXT: }
+
+ // CIR-NEXT: cir.call @after
+ after();
+}
>From 78bc9738aa04b1a8693b49161a3b0b3c2f9d3570 Mon Sep 17 00:00:00 2001
From: Soham Karandikar <sohamk10 at gmail.com>
Date: Tue, 3 Nov 2026 03:23:31 -0600
Subject: [PATCH 3/9] Ran clang-format
---
clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp | 44 +++++++++++-----------
1 file changed, 22 insertions(+), 22 deletions(-)
diff --git a/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp b/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp
index 7c15bfef4d68b2..a3bf6551f2d2f9 100644
--- a/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp
@@ -105,20 +105,22 @@ emitParallelOp(CIRGenFunction &cgf, const DirectiveTy &s,
return res;
}
-static mlir::LogicalResult emitMasterOp(CIRGenFunction &cgf, mlir::Location begin, mlir::Location end, llvm::function_ref<mlir::LogicalResult()> emitBody){
- CIRGenBuilderTy &builder = cgf.getBuilder();
- CIRGenModule &cgm = cgf.getCIRGenModule();
+static mlir::LogicalResult
+emitMasterOp(CIRGenFunction &cgf, mlir::Location begin, mlir::Location end,
+ llvm::function_ref<mlir::LogicalResult()> emitBody) {
+ CIRGenBuilderTy &builder = cgf.getBuilder();
+ CIRGenModule &cgm = cgf.getCIRGenModule();
- auto masterOp = mlir::omp::MasterOp::create(builder, begin);
+ auto masterOp = mlir::omp::MasterOp::create(builder, begin);
- mlir::Block &block = masterOp.getRegion().emplaceBlock();
- mlir::OpBuilder::InsertionGuard guard(builder);
- builder.setInsertionPointToEnd(&block);
+ mlir::Block &block = masterOp.getRegion().emplaceBlock();
+ mlir::OpBuilder::InsertionGuard guard(builder);
+ builder.setInsertionPointToEnd(&block);
- CIRGenFunction::LexicalScope ls{cgf, begin, builder.getInsertionBlock()};
- mlir::LogicalResult res = emitBody();
- mlir::omp::TerminatorOp::create(builder, end);
- return res;
+ CIRGenFunction::LexicalScope ls{cgf, begin, builder.getInsertionBlock()};
+ mlir::LogicalResult res = emitBody();
+ mlir::omp::TerminatorOp::create(builder, end);
+ return res;
}
mlir::LogicalResult
@@ -222,17 +224,15 @@ CIRGenFunction::emitOMPSingleDirective(const OMPSingleDirective &s) {
}
mlir::LogicalResult
CIRGenFunction::emitOMPMasterDirective(const OMPMasterDirective &s) {
- mlir::Location begin = getLoc(s.getBeginLoc());
- mlir::Location end = getLoc(s.getEndLoc());
-
- return emitMasterOp(
- *this, begin, end,
- [&]() -> mlir::LogicalResult {
- // emitStmt() rejects CapturedStmt directly; the parent construct
- // must unwrap it, so emit the inner statement instead.
- const Stmt *innerStmt = s.getAssociatedStmt();
- return emitStmt(innerStmt, /*useCurrentScope=*/true);
- });
+ mlir::Location begin = getLoc(s.getBeginLoc());
+ mlir::Location end = getLoc(s.getEndLoc());
+
+ return emitMasterOp(*this, begin, end, [&]() -> mlir::LogicalResult {
+ // emitStmt() rejects CapturedStmt directly; the parent construct
+ // must unwrap it, so emit the inner statement instead.
+ const Stmt *innerStmt = s.getAssociatedStmt();
+ return emitStmt(innerStmt, /*useCurrentScope=*/true);
+ });
}
mlir::LogicalResult
CIRGenFunction::emitOMPCriticalDirective(const OMPCriticalDirective &s) {
>From db111b4e1db62f4513d0d5d29a0d35e2b9f4feb1 Mon Sep 17 00:00:00 2001
From: Soham Karandikar <sohamk10 at gmail.com>
Date: Tue, 3 Nov 2026 03:49:53 -0600
Subject: [PATCH 4/9] Removed stale comment
---
clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp | 2 --
1 file changed, 2 deletions(-)
diff --git a/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp b/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp
index a3bf6551f2d2f9..69c58afc546286 100644
--- a/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp
@@ -228,8 +228,6 @@ CIRGenFunction::emitOMPMasterDirective(const OMPMasterDirective &s) {
mlir::Location end = getLoc(s.getEndLoc());
return emitMasterOp(*this, begin, end, [&]() -> mlir::LogicalResult {
- // emitStmt() rejects CapturedStmt directly; the parent construct
- // must unwrap it, so emit the inner statement instead.
const Stmt *innerStmt = s.getAssociatedStmt();
return emitStmt(innerStmt, /*useCurrentScope=*/true);
});
>From d1331c6e4963a5c4eb8c9ce9680350faa33f62ad Mon Sep 17 00:00:00 2001
From: Soham Karandikar <sohamk10 at gmail.com>
Date: Tue, 3 Nov 2026 07:48:44 -0600
Subject: [PATCH 5/9] Addressed reviewer comments
---
clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp | 36 +++++++++-------------
clang/test/CIR/CodeGenOpenMP/master.c | 1 -
2 files changed, 14 insertions(+), 23 deletions(-)
diff --git a/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp b/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp
index 69c58afc546286..bab1e5d7c8caa8 100644
--- a/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp
@@ -105,24 +105,6 @@ emitParallelOp(CIRGenFunction &cgf, const DirectiveTy &s,
return res;
}
-static mlir::LogicalResult
-emitMasterOp(CIRGenFunction &cgf, mlir::Location begin, mlir::Location end,
- llvm::function_ref<mlir::LogicalResult()> emitBody) {
- CIRGenBuilderTy &builder = cgf.getBuilder();
- CIRGenModule &cgm = cgf.getCIRGenModule();
-
- auto masterOp = mlir::omp::MasterOp::create(builder, begin);
-
- mlir::Block &block = masterOp.getRegion().emplaceBlock();
- mlir::OpBuilder::InsertionGuard guard(builder);
- builder.setInsertionPointToEnd(&block);
-
- CIRGenFunction::LexicalScope ls{cgf, begin, builder.getInsertionBlock()};
- mlir::LogicalResult res = emitBody();
- mlir::omp::TerminatorOp::create(builder, end);
- return res;
-}
-
mlir::LogicalResult
CIRGenFunction::emitOMPParallelDirective(const OMPParallelDirective &s) {
mlir::Location begin = getLoc(s.getBeginLoc());
@@ -227,11 +209,21 @@ CIRGenFunction::emitOMPMasterDirective(const OMPMasterDirective &s) {
mlir::Location begin = getLoc(s.getBeginLoc());
mlir::Location end = getLoc(s.getEndLoc());
- return emitMasterOp(*this, begin, end, [&]() -> mlir::LogicalResult {
- const Stmt *innerStmt = s.getAssociatedStmt();
- return emitStmt(innerStmt, /*useCurrentScope=*/true);
- });
+ CIRGenBuilderTy &builder = getBuilder();
+
+ auto masterOp = mlir::omp::MasterOp::create(builder, begin);
+
+ mlir::Block &block = masterOp.getRegion().emplaceBlock();
+ mlir::OpBuilder::InsertionGuard guard(builder);
+ builder.setInsertionPointToEnd(&block);
+
+ CIRGenFunction::LexicalScope ls{*this, begin, builder.getInsertionBlock()};
+ const Stmt *innerStmt = s.getAssociatedStmt();
+ mlir::LogicalResult res = emitStmt(innerStmt, /*useCurrentScope=*/true);
+ mlir::omp::TerminatorOp::create(builder, end);
+ return res;
}
+
mlir::LogicalResult
CIRGenFunction::emitOMPCriticalDirective(const OMPCriticalDirective &s) {
getCIRGenModule().errorNYI(s.getSourceRange(), "OpenMP OMPCriticalDirective");
diff --git a/clang/test/CIR/CodeGenOpenMP/master.c b/clang/test/CIR/CodeGenOpenMP/master.c
index 31bce4726b9275..54ccd7fb188ebf 100644
--- a/clang/test/CIR/CodeGenOpenMP/master.c
+++ b/clang/test/CIR/CodeGenOpenMP/master.c
@@ -1,4 +1,3 @@
-
// RUN: %clang_cc1 -fopenmp -triple x86_64-linux-gnu -fclangir -emit-cir %s -o - | FileCheck %s --check-prefix=CIR
void before(void);
>From 551201da2c25c5cc310319df3f894448a04dfbaa Mon Sep 17 00:00:00 2001
From: Soham Karandikar <sohamk10 at gmail.com>
Date: Wed, 11 Nov 2026 06:12:12 -0600
Subject: [PATCH 6/9] Merged CIR tests with classic codegen tests
---
clang/test/CIR/CodeGenOpenMP/master.c | 21 -------
clang/test/OpenMP/master_codegen.cpp | 81 +++++++++++++++++++++++++--
2 files changed, 76 insertions(+), 26 deletions(-)
delete mode 100644 clang/test/CIR/CodeGenOpenMP/master.c
diff --git a/clang/test/CIR/CodeGenOpenMP/master.c b/clang/test/CIR/CodeGenOpenMP/master.c
deleted file mode 100644
index 54ccd7fb188ebf..00000000000000
--- a/clang/test/CIR/CodeGenOpenMP/master.c
+++ /dev/null
@@ -1,21 +0,0 @@
-// RUN: %clang_cc1 -fopenmp -triple x86_64-linux-gnu -fclangir -emit-cir %s -o - | FileCheck %s --check-prefix=CIR
-
-void before(void);
-void body(void);
-void after(void);
-
-// CIR-LABEL: cir.func{{.*}}@simple_master(
-void simple_master(void) {
- // CIR: cir.call @before
- before();
-
- // CIR-NEXT: omp.master {
-#pragma omp master
- body();
- // CIR-NEXT: cir.call @body
- // CIR-NEXT: omp.terminator
- // CIR-NEXT: }
-
- // CIR-NEXT: cir.call @after
- after();
-}
diff --git a/clang/test/OpenMP/master_codegen.cpp b/clang/test/OpenMP/master_codegen.cpp
index 5a92444d9a9273..4f01c17eb7e096 100644
--- a/clang/test/OpenMP/master_codegen.cpp
+++ b/clang/test/OpenMP/master_codegen.cpp
@@ -1,15 +1,18 @@
-// RUN: %clang_cc1 -verify -fopenmp -x c++ -emit-llvm %s -fexceptions -fcxx-exceptions -o - | FileCheck %s --check-prefixes=ALL,NORMAL
+// RUN: %clang_cc1 -verify -fopenmp -x c++ -emit-llvm %s -fexceptions -fcxx-exceptions -o - | FileCheck %s --check-prefixes=ALL,NORMAL,OGCG
// RUN: %clang_cc1 -fopenmp -x c++ -std=c++11 -triple x86_64-unknown-unknown -fexceptions -fcxx-exceptions -emit-pch -o %t %s
-// RUN: %clang_cc1 -fopenmp -x c++ -triple x86_64-unknown-unknown -fexceptions -fcxx-exceptions -std=c++11 -include-pch %t -verify %s -emit-llvm -o - | FileCheck %s --check-prefixes=ALL,NORMAL
+// RUN: %clang_cc1 -fopenmp -x c++ -triple x86_64-unknown-unknown -fexceptions -fcxx-exceptions -std=c++11 -include-pch %t -verify %s -emit-llvm -o - | FileCheck %s --check-prefixes=ALL,NORMAL,OGCG
// RUN: %clang_cc1 -verify -triple x86_64-apple-darwin10 -fopenmp -fexceptions -fcxx-exceptions -debug-info-kind=line-tables-only -x c++ -emit-llvm %s -o - | FileCheck %s --check-prefix=TERM_DEBUG
-// RUN: %clang_cc1 -verify -fopenmp -fopenmp-enable-irbuilder -x c++ -emit-llvm %s -fexceptions -fcxx-exceptions -o - | FileCheck %s --check-prefixes=ALL,IRBUILDER
+// RUN: %clang_cc1 -verify -fopenmp -fopenmp-enable-irbuilder -x c++ -emit-llvm %s -fexceptions -fcxx-exceptions -o - | FileCheck %s --check-prefixes=ALL,IRBUILDER,OGCG
// RUN: %clang_cc1 -fopenmp -fopenmp-enable-irbuilder -x c++ -std=c++11 -triple x86_64-unknown-unknown -fexceptions -fcxx-exceptions -emit-pch -o %t %s
-// RUN: %clang_cc1 -fopenmp -fopenmp-enable-irbuilder -x c++ -triple x86_64-unknown-unknown -fexceptions -fcxx-exceptions -std=c++11 -include-pch %t -verify %s -emit-llvm -o - | FileCheck %s --check-prefixes=ALL,IRBUILDER
+// RUN: %clang_cc1 -fopenmp -fopenmp-enable-irbuilder -x c++ -triple x86_64-unknown-unknown -fexceptions -fcxx-exceptions -std=c++11 -include-pch %t -verify %s -emit-llvm -o - | FileCheck %s --check-prefixes=ALL,IRBUILDER,OGCG
// RUN: %clang_cc1 -verify -fopenmp-simd -x c++ -emit-llvm %s -fexceptions -fcxx-exceptions -o - | FileCheck --check-prefix SIMD-ONLY0 %s
// RUN: %clang_cc1 -fopenmp-simd -x c++ -std=c++11 -triple x86_64-unknown-unknown -fexceptions -fcxx-exceptions -emit-pch -o %t %s
// RUN: %clang_cc1 -fopenmp-simd -x c++ -triple x86_64-unknown-unknown -fexceptions -fcxx-exceptions -std=c++11 -include-pch %t -verify %s -emit-llvm -o - | FileCheck --check-prefix SIMD-ONLY0 %s
// RUN: %clang_cc1 -verify -triple x86_64-apple-darwin10 -fopenmp-simd -fexceptions -fcxx-exceptions -debug-info-kind=line-tables-only -x c++ -emit-llvm %s -o - | FileCheck --check-prefix SIMD-ONLY0 %s
+// RUN: %if cir-enabled %{ %clang_cc1 -verify -fopenmp -std=c++11 -triple x86_64-unknown-linux-gnu -DCIR_TEST -fclangir -emit-cir %s -o - | FileCheck %s --check-prefix=CIR %}
+// RUN: %if cir-enabled %{ %clang_cc1 -verify -fopenmp -std=c++11 -triple x86_64-unknown-linux-gnu -DCIR_TEST -fclangir -emit-llvm %s -o - | FileCheck %s --check-prefixes=ALL,CIRLLVM %}
+
// SIMD-ONLY0-NOT: {{__kmpc|__tgt}}
// expected-no-diagnostics
#ifndef HEADER
@@ -22,9 +25,11 @@
void foo() { extern void mayThrow(); mayThrow(); }
// ALL-LABEL: @main
+// CIR-LABEL: cir.func{{.*}} @main(
// TERM_DEBUG-LABEL: @main
int main() {
// ALL: [[A_ADDR:%.+]] = alloca i8
+ // CIR: [[A_ADDR:%.*]] = cir.alloca "a"
char a;
// ALL: [[GTID:%.+]] = call {{.*}}i32 @__kmpc_global_thread_num(ptr [[DEFAULT_LOC:@.+]])
@@ -32,7 +37,13 @@ int main() {
// ALL-NEXT: [[IS_MASTER:%.+]] = icmp ne i32 [[RES]], 0
// ALL-NEXT: br i1 [[IS_MASTER]], label {{%?}}[[THEN:.+]], label {{%?}}[[EXIT:.+]]
// ALL: [[THEN]]
+// CIRLLVM-NEXT: br label %[[MASTER_REGION:[^ ,]+]]
+// CIRLLVM: [[MASTER_REGION]]:
// ALL-NEXT: store i8 2, ptr [[A_ADDR]]
+// CIRLLVM-NEXT: br label %[[MASTER_CONT:[^ ,]+]]
+// CIRLLVM: [[MASTER_CONT]]:
+// CIRLLVM-NEXT: br label %[[MASTER_FINALIZE:[^ ,]+]]
+// CIRLLVM: [[MASTER_FINALIZE]]:
// IRBUILDER-NEXT: br label %[[AFTER:[^ ,]+]]
// IRBUILDER: [[AFTER]]
// IRBUILDER-NEXT: br label %[[OMP_REGION_FINALIZE:[^ ,]+]]
@@ -40,18 +51,35 @@ int main() {
// ALL-NEXT: call {{.*}}void @__kmpc_end_master(ptr [[DEFAULT_LOC]], i32 [[GTID]])
// ALL-NEXT: br label {{%?}}[[EXIT]]
// ALL: [[EXIT]]
+// CIR: omp.master {
+// CIR-NEXT: [[TWO:%.*]] = cir.const #cir.int<2> : !s8i
+// CIR-NEXT: cir.store align(1) [[TWO]], [[A_ADDR]]
+// CIR-NEXT: omp.terminator
+// CIR-NEXT: }
#pragma omp master
a = 2;
+// CIRLLVM: [[GTID:%.+]] = call {{.*}}i32 @__kmpc_global_thread_num(ptr [[DEFAULT_LOC:@.+]])
// IRBUILDER: [[GTID:%.+]] = call {{.*}}i32 @__kmpc_global_thread_num(ptr [[DEFAULT_LOC:@.+]])
// ALL: [[RES:%.+]] = call {{.*}}i32 @__kmpc_master(ptr [[DEFAULT_LOC]], i32 [[GTID]])
// ALL-NEXT: [[IS_MASTER:%.+]] = icmp ne i32 [[RES]], 0
// ALL-NEXT: br i1 [[IS_MASTER]], label {{%?}}[[THEN:.+]], label {{%?}}[[EXIT:.+]]
// ALL: [[THEN]]
+// CIRLLVM-NEXT: br label %[[MASTER_REGION:[^ ,]+]]
+// CIRLLVM: [[MASTER_REGION]]:
+// CIRLLVM-NEXT: call {{.*}}void [[FOO]]()
+// CIRLLVM-NEXT: br label %[[MASTER_CONT:[^ ,]+]]
+// CIRLLVM: [[MASTER_CONT]]:
+// CIRLLVM-NEXT: br label %[[MASTER_FINALIZE:[^ ,]+]]
+// CIRLLVM: [[MASTER_FINALIZE]]:
// IRBUILDER-NEXT: call {{.*}}void [[FOO]]()
// NORMAL-NEXT: invoke {{.*}}void [[FOO]]()
// ALL: call {{.*}}void @__kmpc_end_master(ptr [[DEFAULT_LOC]], i32 [[GTID]])
// ALL-NEXT: br label {{%?}}[[EXIT]]
// ALL: [[EXIT]]
+// CIR: omp.master {
+// CIR-NEXT: cir.call @_Z3foov(
+// CIR-NEXT: omp.terminator
+// CIR-NEXT: }
#pragma omp master
foo();
// ALL-NOT: call i32 @__kmpc_master
@@ -66,6 +94,13 @@ void lambda_master(int a, int b) {
#pragma omp master
{
// ALL: call i32 @__kmpc_master(
+ // CIR-LABEL: cir.func{{.*}} @_ZZ13lambda_masteriiENK{{.*}}clEv(
+ // CIR: omp.master {
+ // CIR-NEXT: [[C_ADDR:%.*]] = cir.alloca "c"
+ // CIR: [[SUM:%.*]] = cir.add nsw
+ // CIR-NEXT: cir.store align(4) [[SUM]], [[C_ADDR]]
+ // CIR-NEXT: omp.terminator
+ // CIR-NEXT: }
int c = a + b;
}
};
@@ -77,24 +112,60 @@ void lambda_master(int a, int b) {
#pragma omp master
{
// ALL: call i32 @__kmpc_master(
+ // CIR-LABEL: cir.func{{.*}} @_ZZ13lambda_masteriiENK{{.*}}clEv(
+ // CIR: omp.parallel {
+ // CIR-NEXT: omp.master {
+ // CIR-NEXT: [[C_ADDR:%.*]] = cir.alloca "c"
+ // CIR: [[SUM:%.*]] = cir.add nsw
+ // CIR-NEXT: cir.store align(4) [[SUM]], [[C_ADDR]]
+ // CIR-NEXT: omp.terminator
+ // CIR-NEXT: }
+ // CIR-NEXT: omp.terminator
+ // CIR-NEXT: }
int c = a + b;
}
};
l1();
+#ifndef CIR_TEST
auto l2 = [=]() {
#pragma omp parallel master
{
- // ALL: call i32 @__kmpc_master(
+ // OGCG: call i32 @__kmpc_master(
int c = a + b;
}
};
l2();
+#endif
}
// ALL-LABEL: parallel_master
+// CIRLLVM: call {{.*}}@__kmpc_fork_call({{.*}}ptr [[OUTLINED:@[^, )]+]]
+// CIRLLVM: define internal {{.*}}void [[OUTLINED]](
+// CIRLLVM: [[GTID:%.*]] = call i32 @__kmpc_global_thread_num(ptr [[DEFAULT_LOC:@[^, )]+]])
+// CIRLLVM-NEXT: [[RES:%.*]] = call i32 @__kmpc_master(ptr [[DEFAULT_LOC]], i32 [[GTID]])
+// CIRLLVM-NEXT: [[IS_MASTER:%.*]] = icmp ne i32 [[RES]], 0
+// CIRLLVM-NEXT: br i1 [[IS_MASTER]], label %[[THEN:[^ ,]+]], label %[[EXIT:[^ ,]+]]
+// CIRLLVM: [[THEN]]:
+// CIRLLVM-NEXT: br label %[[MASTER_REGION:[^ ,]+]]
+// CIRLLVM: [[MASTER_REGION]]:
+// CIRLLVM-NEXT: call void [[FOO]]()
+// CIRLLVM-NEXT: br label %[[MASTER_CONT:[^ ,]+]]
+// CIRLLVM: [[MASTER_CONT]]:
+// CIRLLVM-NEXT: br label %[[MASTER_FINALIZE:[^ ,]+]]
+// CIRLLVM: [[MASTER_FINALIZE]]:
+// CIRLLVM-NEXT: call void @__kmpc_end_master(ptr [[DEFAULT_LOC]], i32 [[GTID]])
+// CIRLLVM-NEXT: br label %[[EXIT]]
+// CIR-LABEL: cir.func{{.*}} @_Z15parallel_masterv(
+// CIR: omp.parallel {
+// CIR-NEXT: omp.master {
+// CIR-NEXT: cir.call @_Z3foov(
+// CIR-NEXT: omp.terminator
+// CIR-NEXT: }
+// CIR-NEXT: omp.terminator
+// CIR-NEXT: }
// TERM_DEBUG-LABEL: parallel_master
void parallel_master() {
#pragma omp parallel
>From 19773a62a528d8d640aa0614653804825790cf0d Mon Sep 17 00:00:00 2001
From: Soham Karandikar <sohamk10 at gmail.com>
Date: Mon, 16 Nov 2026 00:31:34 -0600
Subject: [PATCH 7/9] Added codegen for parallel-master directive. Updated
test.
---
clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp | 54 +++++++++++++++++-----
clang/test/OpenMP/master_codegen.cpp | 6 +--
2 files changed, 44 insertions(+), 16 deletions(-)
diff --git a/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp b/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp
index bab1e5d7c8caa8..3ee702805e1161 100644
--- a/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp
@@ -204,12 +204,10 @@ CIRGenFunction::emitOMPSingleDirective(const OMPSingleDirective &s) {
getCIRGenModule().errorNYI(s.getSourceRange(), "OpenMP OMPSingleDirective");
return mlir::failure();
}
-mlir::LogicalResult
-CIRGenFunction::emitOMPMasterDirective(const OMPMasterDirective &s) {
- mlir::Location begin = getLoc(s.getBeginLoc());
- mlir::Location end = getLoc(s.getEndLoc());
-
- CIRGenBuilderTy &builder = getBuilder();
+static mlir::LogicalResult emitMasterOp(CIRGenFunction &cgf,
+ mlir::Location begin,
+ mlir::Location end, const Stmt *body) {
+ CIRGenBuilderTy &builder = cgf.getBuilder();
auto masterOp = mlir::omp::MasterOp::create(builder, begin);
@@ -217,13 +215,18 @@ CIRGenFunction::emitOMPMasterDirective(const OMPMasterDirective &s) {
mlir::OpBuilder::InsertionGuard guard(builder);
builder.setInsertionPointToEnd(&block);
- CIRGenFunction::LexicalScope ls{*this, begin, builder.getInsertionBlock()};
- const Stmt *innerStmt = s.getAssociatedStmt();
- mlir::LogicalResult res = emitStmt(innerStmt, /*useCurrentScope=*/true);
+ CIRGenFunction::LexicalScope ls{cgf, begin, builder.getInsertionBlock()};
+ mlir::LogicalResult res = cgf.emitStmt(body, /*useCurrentScope=*/true);
mlir::omp::TerminatorOp::create(builder, end);
return res;
}
+mlir::LogicalResult
+CIRGenFunction::emitOMPMasterDirective(const OMPMasterDirective &s) {
+ return emitMasterOp(*this, getLoc(s.getBeginLoc()), getLoc(s.getEndLoc()),
+ s.getAssociatedStmt());
+}
+
mlir::LogicalResult
CIRGenFunction::emitOMPCriticalDirective(const OMPCriticalDirective &s) {
getCIRGenModule().errorNYI(s.getSourceRange(), "OpenMP OMPCriticalDirective");
@@ -243,9 +246,36 @@ mlir::LogicalResult CIRGenFunction::emitOMPParallelForSimdDirective(
}
mlir::LogicalResult CIRGenFunction::emitOMPParallelMasterDirective(
const OMPParallelMasterDirective &s) {
- getCIRGenModule().errorNYI(s.getSourceRange(),
- "OpenMP OMPParallelMasterDirective");
- return mlir::failure();
+ mlir::Location begin = getLoc(s.getBeginLoc());
+ mlir::Location end = getLoc(s.getEndLoc());
+
+ omp::ConstructQueue queue =
+ omp::buildConstructQueue(getContext().getLangOpts().OpenMP, s);
+ omp::ConstructQueue::const_iterator parallelItem = queue.begin();
+ assert(parallelItem != queue.end() &&
+ parallelItem->id == llvm::omp::OMPD_parallel &&
+ "expected 'parallel' to be the outermost leaf");
+
+ omp::ConstructQueue::const_iterator masterItem = std::next(parallelItem);
+ assert(masterItem != queue.end() &&
+ masterItem->id == llvm::omp::OMPD_master &&
+ omp::isLastItemInQueue(masterItem, queue) &&
+ "expected 'master' to be the innermost leaf");
+
+ if (mlir::failed(checkSynthesizedClauses(*this, s, parallelItem)))
+ return mlir::failure();
+
+ mlir::omp::ParallelOperands clauseOps;
+ if (mlir::failed(emitParallelClauses(*this, getCIRGenModule(), builder, begin,
+ parallelItem->clauses, clauseOps)))
+ return mlir::failure();
+
+ return emitParallelOp(
+ *this, s, queue, parallelItem, begin, end, clauseOps,
+ [&]() -> mlir::LogicalResult {
+ const CapturedStmt *cs = s.getCapturedStmt(llvm::omp::OMPD_parallel);
+ return emitMasterOp(*this, begin, end, cs->getCapturedStmt());
+ });
}
mlir::LogicalResult CIRGenFunction::emitOMPParallelSectionsDirective(
const OMPParallelSectionsDirective &s) {
diff --git a/clang/test/OpenMP/master_codegen.cpp b/clang/test/OpenMP/master_codegen.cpp
index 4f01c17eb7e096..eacce2a0ef41ea 100644
--- a/clang/test/OpenMP/master_codegen.cpp
+++ b/clang/test/OpenMP/master_codegen.cpp
@@ -10,8 +10,8 @@
// RUN: %clang_cc1 -fopenmp-simd -x c++ -std=c++11 -triple x86_64-unknown-unknown -fexceptions -fcxx-exceptions -emit-pch -o %t %s
// RUN: %clang_cc1 -fopenmp-simd -x c++ -triple x86_64-unknown-unknown -fexceptions -fcxx-exceptions -std=c++11 -include-pch %t -verify %s -emit-llvm -o - | FileCheck --check-prefix SIMD-ONLY0 %s
// RUN: %clang_cc1 -verify -triple x86_64-apple-darwin10 -fopenmp-simd -fexceptions -fcxx-exceptions -debug-info-kind=line-tables-only -x c++ -emit-llvm %s -o - | FileCheck --check-prefix SIMD-ONLY0 %s
-// RUN: %if cir-enabled %{ %clang_cc1 -verify -fopenmp -std=c++11 -triple x86_64-unknown-linux-gnu -DCIR_TEST -fclangir -emit-cir %s -o - | FileCheck %s --check-prefix=CIR %}
-// RUN: %if cir-enabled %{ %clang_cc1 -verify -fopenmp -std=c++11 -triple x86_64-unknown-linux-gnu -DCIR_TEST -fclangir -emit-llvm %s -o - | FileCheck %s --check-prefixes=ALL,CIRLLVM %}
+// RUN: %if cir-enabled %{ %clang_cc1 -verify -fopenmp -std=c++11 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o - | FileCheck %s --check-prefix=CIR %}
+// RUN: %if cir-enabled %{ %clang_cc1 -verify -fopenmp -std=c++11 -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm %s -o - | FileCheck %s --check-prefixes=ALL,CIRLLVM %}
// SIMD-ONLY0-NOT: {{__kmpc|__tgt}}
// expected-no-diagnostics
@@ -128,7 +128,6 @@ void lambda_master(int a, int b) {
l1();
-#ifndef CIR_TEST
auto l2 = [=]() {
#pragma omp parallel master
{
@@ -138,7 +137,6 @@ void lambda_master(int a, int b) {
};
l2();
-#endif
}
// ALL-LABEL: parallel_master
>From 8588d483bf69ee6840bcaba4518c5429a159494b Mon Sep 17 00:00:00 2001
From: Soham Karandikar <sohamk10 at gmail.com>
Date: Mon, 16 Nov 2026 00:45:30 -0600
Subject: [PATCH 8/9] Skipped hasCancel check on OMPParallelMasterDirective,
since it dosn't allow cancellation.
---
clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp | 12 +++++++++---
1 file changed, 9 insertions(+), 3 deletions(-)
diff --git a/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp b/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp
index 3ee702805e1161..97ee13d08819f5 100644
--- a/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp
@@ -20,6 +20,7 @@
#include "llvm/ADT/STLFunctionalExtras.h"
#include "llvm/Frontend/OpenMP/OMP.h"
#include "llvm/Frontend/OpenMP/OMPConstants.h"
+#include <type_traits>
using namespace clang;
using namespace clang::CIRGen;
@@ -92,9 +93,14 @@ emitParallelOp(CIRGenFunction &cgf, const DirectiveTy &s,
// hasCancel() reports a nested cancel/cancellation point directive in the
// body, not a clause on `parallel`, so it can't be an emitParallelClauses()
// NYI check.
- if (s.hasCancel()) {
- cgm.errorNYI(s.getBeginLoc(), "OpenMP Parallel with Cancel");
- return mlir::failure();
+ // OMPParallelMasterDirective does not expose a cancellation flag.
+ if constexpr (!std::is_same_v<DirectiveTy, OMPParallelMasterDirective>) {
+ if (s.hasCancel()) {
+ cgm.errorNYI(s.getBeginLoc(),
+ "OpenMP 'cancel' or 'cancellation point' inside a parallel "
+ "region");
+ return mlir::failure();
+ }
}
// Only set for reduction(task: ...), already rejected as NYI above.
assert(!s.getTaskReductionRefExpr() &&
>From 1f688793af7f554f236a1d4694bea3bbc33c3812 Mon Sep 17 00:00:00 2001
From: Soham Karandikar <sohamk10 at gmail.com>
Date: Mon, 16 Nov 2026 01:04:46 -0600
Subject: [PATCH 9/9] Fixed stale check line
---
clang/test/OpenMP/master_codegen.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/test/OpenMP/master_codegen.cpp b/clang/test/OpenMP/master_codegen.cpp
index eacce2a0ef41ea..6882bb89b5be82 100644
--- a/clang/test/OpenMP/master_codegen.cpp
+++ b/clang/test/OpenMP/master_codegen.cpp
@@ -131,7 +131,7 @@ void lambda_master(int a, int b) {
auto l2 = [=]() {
#pragma omp parallel master
{
- // OGCG: call i32 @__kmpc_master(
+ // ALL: call i32 @__kmpc_master(
int c = a + b;
}
};
More information about the cfe-commits
mailing list