[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 01:29:13 PST 2026


https://github.com/skadewdl3 created https://github.com/llvm/llvm-project/pull/238260

Fixes #238258.

This PR adds code generation for the OpenMP `master` directive when using ClangIR. 

Since the `master` directive takes no clauses, I just emitted the `omp::MasterOp` operation and attached get associated `Stmt*` to it using `emitStmt`.

AI Usage: DeepSeek v4.1 Flash
I used DeepSeek v4.1 Flash to understand the CIR codegen pipeline.

>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/3] 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/3] 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/3] 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) {



More information about the cfe-commits mailing list