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

via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 3 01:29:59 PST 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clangir

Author: Soham Karandikar (skadewdl3)

<details>
<summary>Changes</summary>

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.

---
Full diff: https://github.com/llvm/llvm-project/pull/238260.diff


2 Files Affected:

- (modified) clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp (+27-2) 
- (added) clang/test/CIR/CodeGenOpenMP/master.c (+22) 


``````````diff
diff --git a/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp b/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp
index 9d937f5ed67f90..a3bf6551f2d2f9 100644
--- a/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp
@@ -105,6 +105,24 @@ 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 +224,15 @@ 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) {
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();
+}

``````````

</details>


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


More information about the cfe-commits mailing list