[clang] [CIR] [OpenMP] Implement codegen for OpenMP `master` directive (PR #238260)

Soham Karandikar via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 3 22:19:22 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/5] 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/5] 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/5] 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/5] 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/5] 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);



More information about the cfe-commits mailing list