[clang] [mlir] [CIR][OpenMP] Implement unnamed critical directive (PR #236140)
Arseniy Obolenskiy via cfe-commits
cfe-commits at lists.llvm.org
Fri Nov 13 02:09:58 PST 2026
https://github.com/aobolensk updated https://github.com/llvm/llvm-project/pull/236140
>From 6b60e33e4bb86f1a0870cd6c382b51f4b250f240 Mon Sep 17 00:00:00 2001
From: Arseniy Obolenskiy <arseniy.obolenskiy at amd.com>
Date: Tue, 27 Oct 2026 15:40:11 +0100
Subject: [PATCH 1/3] [CIR][OpenMP] Implement unnamed critical directive
Emit the body inside an omp.critical region
---
clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp | 21 +++++++++--
clang/test/CIR/CodeGenOpenMP/critical.c | 36 +++++++++++++++++++
.../CIR/CodeGenOpenMP/not-yet-implemented.c | 4 +--
3 files changed, 57 insertions(+), 4 deletions(-)
create mode 100644 clang/test/CIR/CodeGenOpenMP/critical.c
diff --git a/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp b/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp
index 9d937f5ed67f90..36c7d2d8bf1928 100644
--- a/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp
@@ -211,8 +211,25 @@ CIRGenFunction::emitOMPMasterDirective(const OMPMasterDirective &s) {
}
mlir::LogicalResult
CIRGenFunction::emitOMPCriticalDirective(const OMPCriticalDirective &s) {
- getCIRGenModule().errorNYI(s.getSourceRange(), "OpenMP OMPCriticalDirective");
- return mlir::failure();
+ if (s.getDirectiveName().getName()) {
+ getCIRGenModule().errorNYI(s.getSourceRange(), "OpenMP named critical");
+ return mlir::failure();
+ }
+ assert(s.clauses().empty() && "Sema rejects hint without a name");
+
+ mlir::Location begin = getLoc(s.getBeginLoc());
+ auto criticalOp =
+ mlir::omp::CriticalOp::create(builder, begin, mlir::FlatSymbolRefAttr());
+
+ mlir::Block &block = criticalOp.getRegion().emplaceBlock();
+ mlir::OpBuilder::InsertionGuard guard(builder);
+ builder.setInsertionPointToEnd(&block);
+
+ LexicalScope ls{*this, begin, builder.getInsertionBlock()};
+ mlir::LogicalResult res =
+ emitStmt(s.getAssociatedStmt(), /*useCurrentScope=*/true);
+ mlir::omp::TerminatorOp::create(builder, getLoc(s.getEndLoc()));
+ return res;
}
mlir::LogicalResult
CIRGenFunction::emitOMPParallelForDirective(const OMPParallelForDirective &s) {
diff --git a/clang/test/CIR/CodeGenOpenMP/critical.c b/clang/test/CIR/CodeGenOpenMP/critical.c
new file mode 100644
index 00000000000000..6152b240538f80
--- /dev/null
+++ b/clang/test/CIR/CodeGenOpenMP/critical.c
@@ -0,0 +1,36 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fopenmp -fclangir -emit-cir %s -o %t.cir
+// RUN: FileCheck --input-file=%t.cir %s --check-prefix=CIR
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fopenmp -fclangir -emit-llvm %s -o %t-cir.ll
+// RUN: FileCheck --input-file=%t-cir.ll %s --check-prefix=LLVM
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fopenmp -emit-llvm %s -o %t.ll
+// RUN: FileCheck --input-file=%t.ll %s --check-prefix=LLVM
+
+void before(void);
+void during(int);
+void after(void);
+
+// LLVM: @.gomp_critical_user_.var = {{.*}}[8 x i32] zeroinitializer
+
+void emit_critical(int n) {
+ // CIR-LABEL: cir.func{{.*}}@emit_critical
+ // LLVM-LABEL: define{{.*}}@emit_critical
+ before();
+ // CIR: cir.call @before()
+#pragma omp critical
+ {
+ int i = n;
+ during(i);
+ }
+ // CIR-NEXT: omp.critical {
+ // CIR-NEXT: %[[I:.*]] = cir.alloca "i"
+ // CIR: cir.call @during(
+ // CIR-NEXT: omp.terminator
+ // CIR-NEXT: }
+ // CIR-NEXT: cir.call @after()
+ // LLVM: call void @before()
+ // LLVM: call void @__kmpc_critical(ptr @{{.*}}, i32 %{{.*}}, ptr @.gomp_critical_user_.var)
+ // LLVM: call void @during(
+ // LLVM: call void @__kmpc_end_critical(ptr @{{.*}}, i32 %{{.*}}, ptr @.gomp_critical_user_.var)
+ // LLVM: call void @after()
+ after();
+}
diff --git a/clang/test/CIR/CodeGenOpenMP/not-yet-implemented.c b/clang/test/CIR/CodeGenOpenMP/not-yet-implemented.c
index c4a97e2b76d05b..3d7f849ecd27df 100644
--- a/clang/test/CIR/CodeGenOpenMP/not-yet-implemented.c
+++ b/clang/test/CIR/CodeGenOpenMP/not-yet-implemented.c
@@ -1,8 +1,8 @@
// RUN: %clang_cc1 -fopenmp -triple x86_64-linux-gnu -fclangir %s -verify -emit-cir -o -
void do_things() {
- // expected-error at +1{{ClangIR code gen Not Yet Implemented: OpenMP OMPCriticalDirective}}
-#pragma omp critical
+ // expected-error at +1{{ClangIR code gen Not Yet Implemented: OpenMP named critical}}
+#pragma omp critical(name)
{}
// expected-error at +1{{ClangIR code gen Not Yet Implemented: OpenMP OMPSingleDirective}}
>From 908ca989b69b636ae076f60967cbc0069f276dc1 Mon Sep 17 00:00:00 2001
From: Arseniy Obolenskiy <arseniy.obolenskiy at amd.com>
Date: Wed, 11 Nov 2026 15:28:58 +0100
Subject: [PATCH 2/3] comments
---
clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp | 1 +
clang/test/CIR/CodeGenOpenMP/critical.c | 19 +++++++++++++++++++
2 files changed, 20 insertions(+)
diff --git a/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp b/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp
index 36c7d2d8bf1928..8173b108ec0143 100644
--- a/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp
@@ -228,6 +228,7 @@ CIRGenFunction::emitOMPCriticalDirective(const OMPCriticalDirective &s) {
LexicalScope ls{*this, begin, builder.getInsertionBlock()};
mlir::LogicalResult res =
emitStmt(s.getAssociatedStmt(), /*useCurrentScope=*/true);
+ ls.forceCleanup();
mlir::omp::TerminatorOp::create(builder, getLoc(s.getEndLoc()));
return res;
}
diff --git a/clang/test/CIR/CodeGenOpenMP/critical.c b/clang/test/CIR/CodeGenOpenMP/critical.c
index 6152b240538f80..7bf437b705b40f 100644
--- a/clang/test/CIR/CodeGenOpenMP/critical.c
+++ b/clang/test/CIR/CodeGenOpenMP/critical.c
@@ -4,6 +4,12 @@
// RUN: FileCheck --input-file=%t-cir.ll %s --check-prefix=LLVM
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fopenmp -emit-llvm %s -o %t.ll
// RUN: FileCheck --input-file=%t.ll %s --check-prefix=LLVM
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fopenmp -fclangir -O2 -emit-cir %s -o %t-O2.cir
+// RUN: FileCheck --input-file=%t-O2.cir %s --check-prefix=CIR-O2
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fopenmp -fclangir -O2 -disable-llvm-passes -emit-llvm %s -o %t-cir-O2.ll
+// RUN: FileCheck --input-file=%t-cir-O2.ll %s --check-prefix=LLVM
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fopenmp -O2 -disable-llvm-passes -emit-llvm %s -o %t-O2.ll
+// RUN: FileCheck --input-file=%t-O2.ll %s --check-prefix=LLVM
void before(void);
void during(int);
@@ -13,6 +19,7 @@ void after(void);
void emit_critical(int n) {
// CIR-LABEL: cir.func{{.*}}@emit_critical
+ // CIR-O2-LABEL: cir.func{{.*}}@emit_critical
// LLVM-LABEL: define{{.*}}@emit_critical
before();
// CIR: cir.call @before()
@@ -27,6 +34,18 @@ void emit_critical(int n) {
// CIR-NEXT: omp.terminator
// CIR-NEXT: }
// CIR-NEXT: cir.call @after()
+ // CIR-O2: omp.critical {
+ // CIR-O2-NEXT: %[[I:.*]] = cir.alloca "i"
+ // CIR-O2-NEXT: cir.lifetime.start %[[I]]
+ // CIR-O2-NEXT: cir.cleanup.scope {
+ // CIR-O2: cir.call @during(
+ // CIR-O2: } cleanup normal {
+ // CIR-O2-NEXT: cir.lifetime.end %[[I]]
+ // CIR-O2-NEXT: cir.yield
+ // CIR-O2-NEXT: }
+ // CIR-O2-NEXT: omp.terminator
+ // CIR-O2-NEXT: }
+ // CIR-O2-NEXT: cir.call @after()
// LLVM: call void @before()
// LLVM: call void @__kmpc_critical(ptr @{{.*}}, i32 %{{.*}}, ptr @.gomp_critical_user_.var)
// LLVM: call void @during(
>From 778e7e546b885e15b5e675f6d37dbc68b048b80e Mon Sep 17 00:00:00 2001
From: Arseniy Obolenskiy <arseniy.obolenskiy at amd.com>
Date: Fri, 13 Nov 2026 11:09:02 +0100
Subject: [PATCH 3/3] address comments
---
clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp | 22 ++++++++-----------
mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td | 5 +++++
mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp | 10 +++++++++
3 files changed, 24 insertions(+), 13 deletions(-)
diff --git a/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp b/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp
index 8173b108ec0143..0dbc88e9bc8ec9 100644
--- a/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp
@@ -217,19 +217,15 @@ CIRGenFunction::emitOMPCriticalDirective(const OMPCriticalDirective &s) {
}
assert(s.clauses().empty() && "Sema rejects hint without a name");
- mlir::Location begin = getLoc(s.getBeginLoc());
- auto criticalOp =
- mlir::omp::CriticalOp::create(builder, begin, mlir::FlatSymbolRefAttr());
-
- mlir::Block &block = criticalOp.getRegion().emplaceBlock();
- mlir::OpBuilder::InsertionGuard guard(builder);
- builder.setInsertionPointToEnd(&block);
-
- LexicalScope ls{*this, begin, builder.getInsertionBlock()};
- mlir::LogicalResult res =
- emitStmt(s.getAssociatedStmt(), /*useCurrentScope=*/true);
- ls.forceCleanup();
- mlir::omp::TerminatorOp::create(builder, getLoc(s.getEndLoc()));
+ mlir::LogicalResult res = mlir::success();
+ mlir::omp::CriticalOp::create(
+ builder, getLoc(s.getBeginLoc()), mlir::FlatSymbolRefAttr(),
+ /*bodyBuilder=*/[&](mlir::OpBuilder &b, mlir::Location loc) {
+ LexicalScope ls{*this, loc, b.getInsertionBlock()};
+ res = emitStmt(s.getAssociatedStmt(), /*useCurrentScope=*/true);
+ ls.forceCleanup();
+ mlir::omp::TerminatorOp::create(b, getLoc(s.getEndLoc()));
+ });
return res;
}
mlir::LogicalResult
diff --git a/mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td b/mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td
index 307b91ebe99af2..95f714d43b354f 100644
--- a/mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td
+++ b/mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td
@@ -1832,6 +1832,11 @@ def CriticalOp : OpenMP_Op<"critical", [
let arguments = (ins OptionalAttr<FlatSymbolRefAttr>:$name);
+ let builders = [
+ OpBuilder<(ins "FlatSymbolRefAttr":$name,
+ "function_ref<void(OpBuilder &, Location)>":$bodyBuilder)>
+ ];
+
let assemblyFormat = [{
(`(` $name^ `)`)? $region attr-dict
}];
diff --git a/mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp b/mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp
index d9b6e40dbcb065..cc566dcc2d6c92 100644
--- a/mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp
+++ b/mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp
@@ -5097,6 +5097,16 @@ LogicalResult CriticalDeclareOp::verify() {
return verifySynchronizationHint(*this, getHint());
}
+void CriticalOp::build(OpBuilder &builder, OperationState &state,
+ FlatSymbolRefAttr name,
+ function_ref<void(OpBuilder &, Location)> bodyBuilder) {
+ assert(bodyBuilder && "expected a body builder callback");
+ build(builder, state, name);
+ OpBuilder::InsertionGuard guard(builder);
+ builder.createBlock(state.regions.front().get());
+ bodyBuilder(builder, state.location);
+}
+
LogicalResult CriticalOp::verify() {
SymbolRefAttr currentName = getNameAttr();
More information about the cfe-commits
mailing list