[clang] [mlir] [CIR][OpenMP] Add support for omp teams (PR #231982)

Jan Leyonberg via cfe-commits cfe-commits at lists.llvm.org
Sun Dec 6 10:58:53 PST 2026


https://github.com/jsjodin updated https://github.com/llvm/llvm-project/pull/231982

>From de6dc081ae4b154a113ab0caa9b755501467afc8 Mon Sep 17 00:00:00 2001
From: Jan Leyonberg <jan_sjodin at yahoo.com>
Date: Fri, 2 Oct 2026 13:07:09 -0400
Subject: [PATCH] [CIR][OpenMP] Add support for omp teams

This patch adds support for the OpenMP 'teams' directive, both standalone
and as the combined `target teams` construct. isCombinableLeaf now also
recognizes teams, so an explicitly-nested `target` + `teams` pair gets
the same omp.combined marking as the combined case.

Assisted-by: Cursor / Claude Sonnet 5 High
---
 clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp    |  99 ++++++++-
 .../CIR/CodeGenOpenMP/not-yet-implemented.c   |  16 +-
 .../CIR/CodeGenOpenMP/region-cleanups-O2.c    |  24 +++
 clang/test/CIR/CodeGenOpenMP/target-teams.c   | 201 ++++++++++++++++++
 clang/test/CIR/CodeGenOpenMP/teams.c          | 112 ++++++++++
 .../OpenMP/Transforms/StackToShared.cpp       |  30 ++-
 mlir/test/Dialect/OpenMP/stack-to-shared.mlir |  35 +++
 7 files changed, 501 insertions(+), 16 deletions(-)
 create mode 100644 clang/test/CIR/CodeGenOpenMP/target-teams.c
 create mode 100644 clang/test/CIR/CodeGenOpenMP/teams.c

diff --git a/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp b/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp
index a634f0ea297b4d..f654a312a1c20d 100644
--- a/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp
@@ -70,6 +70,7 @@ static bool isCombinableLeaf(llvm::omp::Directive dir) {
   switch (dir) {
   case llvm::omp::OMPD_target:
   case llvm::omp::OMPD_parallel:
+  case llvm::omp::OMPD_teams:
   case llvm::omp::OMPD_for:
     return true;
   default:
@@ -218,6 +219,62 @@ static mlir::LogicalResult emitParallel(
                         emitBody);
 }
 
+/// Evaluates the clauses allowed on an omp.teams leaf; none are supported
+/// yet, so every eligible clause is reported as NYI.
+static mlir::LogicalResult
+emitTeamsClauses(CIRGenFunction &cgf, CIRGenModule &cgm,
+                 CIRGenBuilderTy &builder, mlir::Location loc,
+                 llvm::ArrayRef<const OMPClause *> clauses,
+                 mlir::omp::TeamsOperands &clauseOps) {
+  OpenMPClauseEmitter ce(cgf, cgm, builder, loc, clauses);
+  return ce.emitNYI</*supported=*/>(
+      /*nyi=*/OpenMPNYIClauseList<
+          OMPAllocateClause, OMPDefaultClause, OMPDynGroupprivateClause,
+          OMPFirstprivateClause, OMPIfClause, OMPMessageClause,
+          OMPNumTeamsClause, OMPPrivateClause, OMPReductionClause,
+          OMPSharedClause, OMPThreadLimitClause, OMPXAttributeClause>{},
+      llvm::omp::Directive::OMPD_teams);
+}
+
+template <typename DirectiveTy>
+static mlir::LogicalResult
+emitTeamsOp(CIRGenFunction &cgf, const DirectiveTy &s,
+            const omp::ConstructQueue &queue,
+            omp::ConstructQueue::const_iterator item, mlir::Location begin,
+            mlir::Location end, const mlir::omp::TeamsOperands &clauseOps,
+            llvm::function_ref<mlir::LogicalResult()> emitBody) {
+  CIRGenBuilderTy &builder = cgf.getBuilder();
+
+  auto teamsOp = mlir::omp::TeamsOp::create(builder, begin, clauseOps);
+  if (!omp::isLastItemInQueue(item, queue) ||
+      hasCombinableNestedLeaf(static_cast<const OMPExecutableDirective &>(s)))
+    teamsOp.setCombined(true);
+
+  mlir::Block &block = teamsOp.getRegion().emplaceBlock();
+  return emitOMPRegionBody<mlir::omp::TerminatorOp>(cgf, block, begin, end,
+                                                    emitBody);
+}
+
+/// Checks, emits clauses for, and emits an omp.teams for the `teams`
+/// directive at \p teamsItem, deferring to \p emitBody for the region's
+/// contents.
+template <typename DirectiveTy>
+static mlir::LogicalResult emitTeams(
+    CIRGenFunction &cgf, const DirectiveTy &s, const omp::ConstructQueue &queue,
+    omp::ConstructQueue::const_iterator teamsItem, mlir::Location begin,
+    mlir::Location end, llvm::function_ref<mlir::LogicalResult()> emitBody) {
+  if (mlir::failed(checkSynthesizedClauses(cgf, s, teamsItem)))
+    return mlir::failure();
+
+  mlir::omp::TeamsOperands teamsOps;
+  if (mlir::failed(emitTeamsClauses(cgf, cgf.getCIRGenModule(),
+                                    cgf.getBuilder(), begin, teamsItem->clauses,
+                                    teamsOps)))
+    return mlir::failure();
+
+  return emitTeamsOp(cgf, s, queue, teamsItem, begin, end, teamsOps, emitBody);
+}
+
 mlir::LogicalResult
 CIRGenFunction::emitOMPParallelDirective(const OMPParallelDirective &s) {
   mlir::Location begin = getLoc(s.getBeginLoc());
@@ -1085,8 +1142,19 @@ CIRGenFunction::emitOMPTargetDirective(const OMPTargetDirective &s) {
 }
 mlir::LogicalResult
 CIRGenFunction::emitOMPTeamsDirective(const OMPTeamsDirective &s) {
-  getCIRGenModule().errorNYI(s.getSourceRange(), "OpenMP OMPTeamsDirective");
-  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 item = queue.begin();
+
+  return emitTeams(
+      *this, s, queue, item, begin, end, [&]() -> mlir::LogicalResult {
+        const CapturedStmt *cs = s.getCapturedStmt(llvm::omp::OMPD_teams);
+        return emitStmt(cs->getCapturedStmt(),
+                        /*useCurrentScope=*/true);
+      });
 }
 mlir::LogicalResult CIRGenFunction::emitOMPCancellationPointDirective(
     const OMPCancellationPointDirective &s) {
@@ -1339,9 +1407,30 @@ mlir::LogicalResult CIRGenFunction::emitOMPTeamsGenericLoopDirective(
 }
 mlir::LogicalResult
 CIRGenFunction::emitOMPTargetTeamsDirective(const OMPTargetTeamsDirective &s) {
-  getCIRGenModule().errorNYI(s.getSourceRange(),
-                             "OpenMP OMPTargetTeamsDirective");
-  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 targetItem = queue.begin();
+  assert(targetItem->id == llvm::omp::OMPD_target &&
+         "expected 'target' to be the outermost directive");
+
+  return emitTarget(
+      *this, s, queue, targetItem, begin, end, [&]() -> mlir::LogicalResult {
+        omp::ConstructQueue::const_iterator teamsItem = std::next(targetItem);
+        assert(teamsItem != queue.end() &&
+               teamsItem->id == llvm::omp::OMPD_teams &&
+               "expected a 'teams' directive nested in 'target'");
+
+        return emitTeams(*this, s, queue, teamsItem, begin, end,
+                         [&]() -> mlir::LogicalResult {
+                           const CapturedStmt *cs =
+                               s.getCapturedStmt(llvm::omp::OMPD_teams);
+                           return emitStmt(cs->getCapturedStmt(),
+                                           /*useCurrentScope=*/true);
+                         });
+      });
 }
 mlir::LogicalResult CIRGenFunction::emitOMPTargetTeamsDistributeDirective(
     const OMPTargetTeamsDistributeDirective &s) {
diff --git a/clang/test/CIR/CodeGenOpenMP/not-yet-implemented.c b/clang/test/CIR/CodeGenOpenMP/not-yet-implemented.c
index 9b5e31eb0a5720..f5ddfd234ec38e 100644
--- a/clang/test/CIR/CodeGenOpenMP/not-yet-implemented.c
+++ b/clang/test/CIR/CodeGenOpenMP/not-yet-implemented.c
@@ -4,8 +4,8 @@ void clash(void);
 
 void do_things() {
   int i;
-  // A leaf that reports a not-yet-implemented clause emits no op at all, rather
-  // than one that silently ignores the clause.
+  // A directive that reports a not-yet-implemented clause emits no op at all,
+  // rather than one that silently ignores the clause.
   int a, b;
   // expected-error at +1{{ClangIR code gen Not Yet Implemented: OpenMP PARALLEL 'firstprivate' clause}}
 #pragma omp parallel shared(a) firstprivate(b)
@@ -16,7 +16,7 @@ void do_things() {
   {}
 
   // A clause routed through construct decomposition but not yet emittable must
-  // still be diagnosed by the leaf emitter's NYI handling.
+  // still be diagnosed by the directive emitter's NYI handling.
   // expected-error at +1{{ClangIR code gen Not Yet Implemented: OpenMP TARGET 'private' clause}}
 #pragma omp target private(i)
   {}
@@ -26,6 +26,16 @@ void do_things() {
   // expected-error at +1{{ClangIR code gen Not Yet Implemented: OpenMP synthesized 'firstprivate' clause from construct decomposition}}
 #pragma omp target
   { i = 1; }
+
+  // The teams construct supports no clauses yet, so every `teams` clause is
+  // reported as not-yet-implemented.
+  // expected-error at +1{{ClangIR code gen Not Yet Implemented: OpenMP TEAMS 'num_teams' clause}}
+#pragma omp teams num_teams(4)
+  {}
+
+  // expected-error at +1{{ClangIR code gen Not Yet Implemented: OpenMP TEAMS 'thread_limit' clause}}
+#pragma omp teams thread_limit(8)
+  {}
 }
 
 void use_clash() {
diff --git a/clang/test/CIR/CodeGenOpenMP/region-cleanups-O2.c b/clang/test/CIR/CodeGenOpenMP/region-cleanups-O2.c
index 477e69b5e6603d..1e5a68a97a2562 100644
--- a/clang/test/CIR/CodeGenOpenMP/region-cleanups-O2.c
+++ b/clang/test/CIR/CodeGenOpenMP/region-cleanups-O2.c
@@ -178,3 +178,27 @@ void local_in_parallel_for(int *a, int n) {
 
 // LLVM-LABEL: define{{.*}} void @local_in_parallel_for(
 // LLVM:         call void {{.*}}@__kmpc_fork_call(
+
+// A local in the teams region is ended inside the region, before its
+// terminator.
+void local_in_teams(void) {
+#pragma omp target teams
+  {
+    int t = 2;
+    use(t);
+  }
+}
+
+// CIR-LABEL: cir.func{{.*}}@local_in_teams
+// CIR:         omp.target
+// CIR:           omp.teams {
+// CIR:             %[[T:.*]] = cir.alloca "t"
+// CIR:             cir.lifetime.start %[[T]]
+// CIR:             cir.cleanup.scope {
+// CIR:             } cleanup normal {
+// CIR:               cir.lifetime.end %[[T]]
+// CIR:             }
+// CIR-NEXT:        omp.terminator
+
+// LLVM-LABEL: define{{.*}} void @local_in_teams(
+// LLVM:         call void {{.*}}@__kmpc_fork_teams(
diff --git a/clang/test/CIR/CodeGenOpenMP/target-teams.c b/clang/test/CIR/CodeGenOpenMP/target-teams.c
new file mode 100644
index 00000000000000..62b633c5e2ffeb
--- /dev/null
+++ b/clang/test/CIR/CodeGenOpenMP/target-teams.c
@@ -0,0 +1,201 @@
+// REQUIRES: amdgpu-registered-target
+
+// Host compilation (x86 host, AMDGPU offload target).
+// RUN: %clang_cc1 -fopenmp -fopenmp-targets=amdgcn-amd-amdhsa -emit-cir -fclangir %s -o - \
+// RUN:   | FileCheck %s --check-prefix=CIR-HOST
+
+// Device compilation (AMDGPU).
+// RUN: %clang_cc1 -triple amdgcn-amd-amdhsa -fopenmp -fopenmp-is-target-device \
+// RUN:   -emit-cir -fclangir %s -o - \
+// RUN:   | FileCheck %s --check-prefix=CIR-DEVICE
+
+// Host-side lowering to LLVM IR: identical on both paths, so LLVM-HOST
+// checks both. The only difference is where the second function's helper
+// functions end up relative to the first function's, checked below under
+// LLVM-HOST-CIR/LLVM-HOST-OGCG.
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fopenmp \
+// RUN:   -fopenmp-targets=amdgcn-amd-amdhsa -fclangir -emit-llvm %s -o - \
+// RUN:   | FileCheck %s --check-prefixes=LLVM-HOST,LLVM-HOST-CIR
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fopenmp \
+// RUN:   -fopenmp-targets=amdgcn-amd-amdhsa -emit-llvm %s -o - \
+// RUN:   | FileCheck %s --check-prefixes=LLVM-HOST,LLVM-HOST-OGCG
+
+// Device-side lowering to LLVM IR: compiled in two steps to mirror the
+// offloading driver.
+// RUN: %clang_cc1 -fopenmp -triple x86_64-linux-gnu -fopenmp-targets=amdgcn-amd-amdhsa \
+// RUN:   -fclangir -emit-llvm-bc %s -o %t-cir-host.bc
+// RUN: %clang_cc1 -fopenmp -triple x86_64-linux-gnu -fopenmp-targets=amdgcn-amd-amdhsa \
+// RUN:   -emit-llvm-bc %s -o %t-ogcg-host.bc
+// RUN: %clang_cc1 -triple amdgcn-amd-amdhsa -fopenmp -fopenmp-is-target-device \
+// RUN:   -fopenmp-host-ir-file-path %t-cir-host.bc \
+// RUN:   -fclangir -emit-llvm %s -o - \
+// RUN:   | FileCheck %s --check-prefix=LLVM-DEVICE
+// RUN: %clang_cc1 -triple amdgcn-amd-amdhsa -fopenmp -fopenmp-is-target-device \
+// RUN:   -fopenmp-host-ir-file-path %t-ogcg-host.bc \
+// RUN:   -emit-llvm %s -o - \
+// RUN:   | FileCheck %s --check-prefix=LLVM-DEVICE
+
+// The same device checks apply to NVPTX.
+// RUN: %if nvptx-registered-target %{ \
+// RUN:   %clang_cc1 -triple nvptx64-nvidia-cuda -fopenmp -fopenmp-is-target-device \
+// RUN:   -emit-cir -fclangir %s -o - \
+// RUN:   | FileCheck %s --check-prefix=CIR-DEVICE %}
+// RUN: %if nvptx-registered-target %{ \
+// RUN:   %clang_cc1 -fopenmp -triple x86_64-linux-gnu -fopenmp-targets=nvptx64-nvidia-cuda \
+// RUN:   -fclangir -emit-llvm-bc %s -o %t-cir-nvptx-host.bc %}
+// RUN: %if nvptx-registered-target %{ \
+// RUN:   %clang_cc1 -fopenmp -triple x86_64-linux-gnu -fopenmp-targets=nvptx64-nvidia-cuda \
+// RUN:   -emit-llvm-bc %s -o %t-ogcg-nvptx-host.bc %}
+// RUN: %if nvptx-registered-target %{ \
+// RUN:   %clang_cc1 -triple nvptx64-nvidia-cuda -fopenmp -fopenmp-is-target-device \
+// RUN:   -fopenmp-host-ir-file-path %t-cir-nvptx-host.bc \
+// RUN:   -fclangir -emit-llvm %s -o - \
+// RUN:   | FileCheck %s --check-prefix=LLVM-DEVICE %}
+// RUN: %if nvptx-registered-target %{ \
+// RUN:   %clang_cc1 -triple nvptx64-nvidia-cuda -fopenmp -fopenmp-is-target-device \
+// RUN:   -fopenmp-host-ir-file-path %t-ogcg-nvptx-host.bc \
+// RUN:   -emit-llvm %s -o - \
+// RUN:   | FileCheck %s --check-prefix=LLVM-DEVICE %}
+
+void use(int);
+
+// Explicit `target` + `teams` nesting lowers the same as the combined
+// `target teams` spelling below, so both get omp.combined.
+void explicit_target_teams(int x) {
+  // CIR-HOST: cir.func{{.*}}@explicit_target_teams
+  // CIR-HOST: %[[MAP:.*]] = omp.map.info {{.*}} map_clauses(tofrom) {{.*}} name("x")
+  // CIR-HOST: omp.target kernel_type(generic) map_entries(%[[MAP]] -> %[[ARG:.*]] : !cir.ptr<!s32i>) {
+  // CIR-HOST: omp.teams {
+  // CIR-HOST: %[[LOAD:.*]] = cir.load align(4) %[[ARG]]
+  // CIR-HOST: cir.call @use(%[[LOAD]])
+  // CIR-HOST: omp.terminator
+  // CIR-HOST: }
+  // CIR-HOST: omp.terminator
+  // CIR-HOST: } {omp.combined}
+
+  // CIR-DEVICE: cir.func{{.*}}@explicit_target_teams
+  // CIR-DEVICE: omp.target kernel_type(generic) {{.*}} {
+  // CIR-DEVICE: omp.teams {
+  // CIR-DEVICE: cir.call @use
+  // CIR-DEVICE: omp.terminator
+  // CIR-DEVICE: }
+  // CIR-DEVICE: omp.terminator
+  // CIR-DEVICE: } {omp.combined}
+#pragma omp target map(tofrom : x)
+#pragma omp teams
+  {
+    use(x);
+  }
+}
+
+// Combined `target teams` lowers the same as the explicit nesting above,
+// with omp.combined on the omp.target.
+void target_teams(int x) {
+  // CIR-HOST: cir.func{{.*}}@target_teams
+  // CIR-HOST: %[[MAP:.*]] = omp.map.info {{.*}} map_clauses(tofrom) {{.*}} name("x")
+  // CIR-HOST: omp.target kernel_type(generic) map_entries(%[[MAP]] -> %[[ARG:.*]] : !cir.ptr<!s32i>) {
+  // CIR-HOST: omp.teams {
+  // CIR-HOST: %[[LOAD:.*]] = cir.load align(4) %[[ARG]]
+  // CIR-HOST: cir.call @use(%[[LOAD]])
+  // CIR-HOST: omp.terminator
+  // CIR-HOST: }
+  // CIR-HOST: omp.terminator
+  // CIR-HOST: } {omp.combined}
+
+  // CIR-DEVICE: cir.func{{.*}}@target_teams
+  // CIR-DEVICE: omp.target kernel_type(generic) {{.*}} {
+  // CIR-DEVICE: omp.teams {
+  // CIR-DEVICE: cir.call @use
+  // CIR-DEVICE: omp.terminator
+  // CIR-DEVICE: }
+  // CIR-DEVICE: omp.terminator
+  // CIR-DEVICE: } {omp.combined}
+#pragma omp target teams map(tofrom : x)
+  {
+    use(x);
+  }
+}
+
+// LLVM-HOST-LABEL: define {{.*}} void @explicit_target_teams(
+// LLVM-HOST-SAME:  i32 noundef %[[ARG:[^,)]+]]
+// LLVM-HOST:         %[[X_ADDR:.*]] = alloca i32, align 4
+// LLVM-HOST:         store i32 {{.*}}, ptr %[[X_ADDR]], align 4
+// LLVM-HOST:         call i32 @__tgt_target_kernel(
+// LLVM-HOST:         br i1 %{{.*}}, label %[[FAILED:[^,]+]], label %{{.*}}
+// LLVM-HOST:       {{^}}[[FAILED]]:
+// LLVM-HOST-NEXT:    call void @__omp_offloading_{{.*}}_explicit_target_teams_l{{[0-9]+}}(ptr %[[X_ADDR]], ptr null)
+
+// CIR groups all its helper functions together up front, since the
+// MLIR-to-LLVM translation emits them during the lowering pass itself;
+// OGCG's direct AST walk emits each one in source order instead,
+// interleaved with other constructs. The checks are the same either way,
+// just placed where each path actually emits them (compare the
+// LLVM-HOST-OGCG copy of this block further down).
+// LLVM-HOST-CIR-LABEL: define {{.*}} void @target_teams(
+// LLVM-HOST-CIR-SAME:  i32 noundef %[[ARG:[^,)]+]]
+// LLVM-HOST-CIR:         %[[X_ADDR:.*]] = alloca i32, align 4
+// LLVM-HOST-CIR:         store i32 {{.*}}, ptr %[[X_ADDR]], align 4
+// LLVM-HOST-CIR:         call i32 @__tgt_target_kernel(
+// LLVM-HOST-CIR:         br i1 %{{.*}}, label %[[FAILED2:[^,]+]], label %{{.*}}
+// LLVM-HOST-CIR:       {{^}}[[FAILED2]]:
+// LLVM-HOST-CIR-NEXT:    call void @__omp_offloading_{{.*}}_target_teams_l{{[0-9]+}}(ptr %[[X_ADDR]], ptr null)
+
+// LLVM-HOST-LABEL: define internal void @__omp_offloading_{{.*}}_explicit_target_teams_l{{[0-9]+}}(
+// LLVM-HOST:         call void (ptr, i32, ptr, ...) @__kmpc_fork_teams(ptr {{.*}}, i32 1, ptr @[[OUTLINED:[^,]+]], ptr {{.*}})
+
+// LLVM-HOST:       define internal void @[[OUTLINED]](
+// LLVM-HOST:         %[[P:.*]] = load ptr, ptr %{{.*}}, align 8
+// LLVM-HOST:         %[[V:.*]] = load i32, ptr %[[P]], align 4
+// LLVM-HOST:         call void @use(i32 {{.*}} %[[V]])
+// LLVM-HOST:         ret void
+
+// OGCG's copy of target_teams's host wrapper: identical content to the
+// LLVM-HOST-CIR block above, just emitted after explicit_target_teams's
+// outlined functions instead of before them.
+// LLVM-HOST-OGCG-LABEL: define {{.*}} void @target_teams(
+// LLVM-HOST-OGCG-SAME:  i32 noundef %[[ARG:[^,)]+]]
+// LLVM-HOST-OGCG:         %[[X_ADDR:.*]] = alloca i32, align 4
+// LLVM-HOST-OGCG:         store i32 {{.*}}, ptr %[[X_ADDR]], align 4
+// LLVM-HOST-OGCG:         call i32 @__tgt_target_kernel(
+// LLVM-HOST-OGCG:         br i1 %{{.*}}, label %[[FAILED2:[^,]+]], label %{{.*}}
+// LLVM-HOST-OGCG:       {{^}}[[FAILED2]]:
+// LLVM-HOST-OGCG-NEXT:    call void @__omp_offloading_{{.*}}_target_teams_l{{[0-9]+}}(ptr %[[X_ADDR]], ptr null)
+
+// LLVM-HOST-LABEL: define internal void @__omp_offloading_{{.*}}_target_teams_l{{[0-9]+}}(
+// LLVM-HOST:         call void (ptr, i32, ptr, ...) @__kmpc_fork_teams(ptr {{.*}}, i32 1, ptr @[[OUTLINED2:[^,]+]], ptr {{.*}})
+
+// LLVM-HOST:       define internal void @[[OUTLINED2]](
+// LLVM-HOST:         %[[P:.*]] = load ptr, ptr %{{.*}}, align 8
+// LLVM-HOST:         %[[V:.*]] = load i32, ptr %[[P]], align 4
+// LLVM-HOST:         call void @use(i32 {{.*}} %[[V]])
+// LLVM-HOST:         ret void
+
+// Device kernel entry: after __kmpc_target_init, the user code calls the
+// teams-outlined function directly; there is no fork call on the device.
+// LLVM-DEVICE-LABEL: define weak_odr protected {{amdgpu|ptx}}_kernel void @__omp_offloading_{{.*}}_explicit_target_teams_l{{[0-9]+}}(
+// LLVM-DEVICE:         call i32 @__kmpc_target_init(
+// LLVM-DEVICE:         br i1 %{{.*}}, label %[[USER_CODE:[^,]+]], label %{{.*}}
+// LLVM-DEVICE:       {{^}}[[USER_CODE]]:
+// LLVM-DEVICE:         call void @[[DEV_OUTLINED:__omp_offloading_[^(]+]](ptr {{.*}}, ptr {{.*}}, ptr {{.*}})
+// LLVM-DEVICE:         call void @__kmpc_target_deinit()
+// LLVM-DEVICE:         ret void
+
+// LLVM-DEVICE:       define internal void @[[DEV_OUTLINED]](
+// LLVM-DEVICE:         %[[P:.*]] = load ptr, ptr %{{.*}}, align 8
+// LLVM-DEVICE:         %[[V:.*]] = load i32, ptr %[[P]], align 4
+// LLVM-DEVICE:         call void @use(i32 {{.*}} %[[V]])
+// LLVM-DEVICE:         ret void
+
+// LLVM-DEVICE-LABEL: define weak_odr protected {{amdgpu|ptx}}_kernel void @__omp_offloading_{{.*}}_target_teams_l{{[0-9]+}}(
+// LLVM-DEVICE:         call i32 @__kmpc_target_init(
+// LLVM-DEVICE:         br i1 %{{.*}}, label %[[USER_CODE2:[^,]+]], label %{{.*}}
+// LLVM-DEVICE:       {{^}}[[USER_CODE2]]:
+// LLVM-DEVICE:         call void @[[DEV_OUTLINED2:__omp_offloading_[^(]+]](ptr {{.*}}, ptr {{.*}}, ptr {{.*}})
+// LLVM-DEVICE:         call void @__kmpc_target_deinit()
+// LLVM-DEVICE:         ret void
+
+// LLVM-DEVICE:       define internal void @[[DEV_OUTLINED2]](
+// LLVM-DEVICE:         %[[P:.*]] = load ptr, ptr %{{.*}}, align 8
+// LLVM-DEVICE:         %[[V:.*]] = load i32, ptr %[[P]], align 4
+// LLVM-DEVICE:         call void @use(i32 {{.*}} %[[V]])
+// LLVM-DEVICE:         ret void
diff --git a/clang/test/CIR/CodeGenOpenMP/teams.c b/clang/test/CIR/CodeGenOpenMP/teams.c
new file mode 100644
index 00000000000000..47af8f21b1b4c3
--- /dev/null
+++ b/clang/test/CIR/CodeGenOpenMP/teams.c
@@ -0,0 +1,112 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fopenmp -emit-cir -fclangir %s -o - \
+// RUN:   | FileCheck %s --check-prefix=CIR
+
+// Lowering to LLVM IR: mostly identical between CIR and OGCG; they just name
+// the outlined functions differently.
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fopenmp -fclangir -emit-llvm %s -o - \
+// RUN:   | FileCheck %s --check-prefixes=LLVM,LLVM-CIR
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fopenmp -emit-llvm %s -o - \
+// RUN:   | FileCheck %s --check-prefixes=LLVM,LLVM-OGCG
+
+void use(int);
+
+// A standalone `teams` directive lowers to an omp.teams op whose region holds
+// the associated structured block.
+void host_teams(int x) {
+  // CIR: cir.func{{.*}}@host_teams
+  // CIR: omp.teams {
+  // CIR: cir.call @use
+  // CIR: omp.terminator
+  // CIR: }
+#pragma omp teams
+  {
+    use(x);
+  }
+}
+
+// LLVM-LABEL: define {{.*}} void @host_teams(
+// LLVM:         call void (ptr, i32, ptr, ...) @__kmpc_fork_teams(ptr {{.*}}, i32 1, ptr @[[OUTLINED:[^,]+]], ptr {{.*}})
+
+// LLVM:       define internal void @[[OUTLINED]](
+// LLVM:         %[[V:.*]] = load i32, ptr %{{.*}}, align 4
+// LLVM:         call void @use(i32 {{.*}} %[[V]])
+// LLVM:         ret void
+
+void teams_nested_parallel_for(void) {
+  // CIR: cir.func{{.*}}@teams_nested_parallel_for
+  // CIR: omp.teams {
+  // CIR: omp.parallel {
+  // CIR: omp.wsloop
+  // CIR-NEXT: omp.loop_nest
+  // CIR: cir.call @use
+  // CIR: omp.yield
+  // CIR: }
+  // CIR: omp.terminator
+  // CIR: } {omp.combined}
+  // CIR: omp.terminator
+  // CIR: } {omp.combined}
+#pragma omp teams
+  {
+#pragma omp parallel for
+    for (int i = 0; i < 10; i++) {
+      use(i);
+    }
+  }
+}
+
+// The nested `parallel for` forks its own threads via __kmpc_fork_call from
+// inside the teams-outlined function, so the wsloop's init/fini/barrier
+// live in a second, further-outlined function.
+// LLVM-LABEL: define {{.*}} void @teams_nested_parallel_for(
+// LLVM:         call void (ptr, i32, ptr, ...) @__kmpc_fork_teams(ptr {{.*}}, i32 0, ptr @[[TEAMS_OUTLINED:[^)]+]])
+
+// LLVM:       define internal void @[[TEAMS_OUTLINED]](
+// LLVM:         call void (ptr, i32, ptr, ...) @__kmpc_fork_call(ptr {{.*}}, i32 0, ptr @[[PAR_OUTLINED:[^)]+]])
+
+// OGCG skips the trailing barrier after the worksharing loop (the
+// implicit join at the end of the parallel region already synchronizes);
+// CIR doesn't yet apply that optimization, so it emits one either way.
+// LLVM-CIR:       define internal void @[[PAR_OUTLINED]](
+// LLVM-CIR:         %[[LASTITER:.*]] = alloca i32, align 4
+// LLVM-CIR-NEXT:    %[[LB:.*]] = alloca i32, align 4
+// LLVM-CIR-NEXT:    %[[UB:.*]] = alloca i32, align 4
+// LLVM-CIR-NEXT:    %[[STRIDE:.*]] = alloca i32, align 4
+// LLVM-CIR:         store i32 0, ptr %[[LB]], align 4
+// LLVM-CIR-NEXT:    store i32 9, ptr %[[UB]], align 4
+// LLVM-CIR-NEXT:    store i32 1, ptr %[[STRIDE]], align 4
+// LLVM-CIR-NEXT:    %[[TID:.*]] = call i32 @__kmpc_global_thread_num(ptr {{.*}})
+// LLVM-CIR-NEXT:    call void @__kmpc_for_static_init_4u(
+// LLVM-CIR-SAME:      ptr {{.*}}, i32 %[[TID]], i32 34,
+// LLVM-CIR-SAME:      ptr %[[LASTITER]], ptr %[[LB]], ptr %[[UB]], ptr %[[STRIDE]],
+// LLVM-CIR-SAME:      i32 1, i32 0)
+// LLVM-CIR:         %[[IV:.*]] = phi i32 [ 0, %{{.*}} ], [ %[[NEXT:[^,]+]], %{{.*}} ]
+// LLVM-CIR:         %[[LOOP_CMP:.*]] = icmp ult i32 %[[IV]], %{{.*}}
+// LLVM-CIR-NEXT:    br i1 %[[LOOP_CMP]], label %[[LOOP_BODY:[^,]+]], label %[[LOOP_EXIT:[^ ,]+]]
+// LLVM-CIR:       {{^}}[[LOOP_EXIT]]:
+// LLVM-CIR-NEXT:    call void @__kmpc_for_static_fini(ptr {{.*}}, i32 %[[TID]])
+// LLVM-CIR:         call void @__kmpc_barrier(ptr {{.*}}, i32 %{{.*}})
+// LLVM-CIR:       {{^}}[[LOOP_BODY]]:
+// LLVM-CIR:         call void @use(i32
+// LLVM-CIR:         %[[NEXT]] = add nuw i32 %[[IV]], 1
+
+// LLVM-OGCG:       define internal void @[[PAR_OUTLINED]](
+// LLVM-OGCG:         store i32 0, ptr %[[OMP_LB:.*]], align 4
+// LLVM-OGCG-NEXT:    store i32 9, ptr %[[OMP_UB:.*]], align 4
+// LLVM-OGCG-NEXT:    store i32 1, ptr %[[OMP_STRIDE:.*]], align 4
+// LLVM-OGCG-NEXT:    store i32 0, ptr %[[OMP_IS_LAST:.*]], align 4
+// LLVM-OGCG:         %[[TID2:.*]] = load i32, ptr %{{.*}}, align 4
+// LLVM-OGCG-NEXT:    call void @__kmpc_for_static_init_4(
+// LLVM-OGCG-SAME:      ptr {{.*}}, i32 %[[TID2]], i32 34,
+// LLVM-OGCG-SAME:      ptr %[[OMP_IS_LAST]], ptr %[[OMP_LB]], ptr %[[OMP_UB]], ptr %[[OMP_STRIDE]],
+// LLVM-OGCG-SAME:      i32 1, i32 1)
+// LLVM-OGCG:         %[[UB_VAL:.*]] = load i32, ptr %[[OMP_UB]], align 4
+// LLVM-OGCG-NEXT:    %[[CMP:.*]] = icmp sgt i32 %[[UB_VAL]], 9
+// LLVM-OGCG-NEXT:    br i1 %[[CMP]], label %[[COND_TRUE:[^,]+]], label %[[COND_FALSE:[^ ,]+]]
+// LLVM-OGCG:         %[[COND:.*]] = phi i32 [ 9, %[[COND_TRUE]] ], [ %{{.*}}, %[[COND_FALSE]] ]
+// LLVM-OGCG-NEXT:    store i32 %[[COND]], ptr %[[OMP_UB]], align 4
+// LLVM-OGCG:         %[[CMP1:.*]] = icmp sle i32 %{{.*}}, %{{.*}}
+// LLVM-OGCG-NEXT:    br i1 %[[CMP1]], label %[[FOR_BODY:[^,]+]], label %{{.*}}
+// LLVM-OGCG:       {{^}}[[FOR_BODY]]:
+// LLVM-OGCG:         call void @use(i32
+// LLVM-OGCG:         call void @__kmpc_for_static_fini(ptr {{.*}}, i32 %[[TID2]])
+// LLVM-OGCG-NEXT:    ret void
diff --git a/mlir/lib/Dialect/OpenMP/Transforms/StackToShared.cpp b/mlir/lib/Dialect/OpenMP/Transforms/StackToShared.cpp
index c5034e9e555306..ab51ab179762ee 100644
--- a/mlir/lib/Dialect/OpenMP/Transforms/StackToShared.cpp
+++ b/mlir/lib/Dialect/OpenMP/Transforms/StackToShared.cpp
@@ -14,6 +14,7 @@
 #include "mlir/Dialect/OpenMP/Transforms/Passes.h"
 
 #include "mlir/Dialect/LLVMIR/LLVMConversionOps.h"
+#include "mlir/Dialect/LLVMIR/LLVMMemoryIntrinsicOps.h"
 #include "mlir/Dialect/LLVMIR/LLVMMemoryOps.h"
 #include "mlir/Dialect/LLVMIR/LLVMModuleOps.h"
 #include "mlir/Dialect/OpenMP/OpenMPDialect.h"
@@ -87,31 +88,44 @@ class StackToSharedPass
       // TODO: The handling of non-default address spaces might need to be
       // improved. This currently only handles the case where an alloca to
       // non-default address space is only used by a single addrspacecast to
-      // default address space.
+      // default address space, plus any lifetime markers.
       bool nonDefaultAddrSpace = false;
       if (auto llvmPtrType = dyn_cast<LLVM::LLVMPointerType>(resultType))
         nonDefaultAddrSpace = llvmPtrType.getAddressSpace() != 0;
 
+      // Lifetime markers must refer to an alloca, so they're dropped along with
+      // it. Delete them later, together with the alloca, because the walk may
+      // not have visited them yet.
+      llvm::SmallVector<Operation *> otherUsers;
+      for (Operation *user : allocaOp->getUsers()) {
+        if (isa<LLVM::LifetimeStartOp, LLVM::LifetimeEndOp>(user))
+          toBeDeleted.push_back(user);
+        else
+          otherUsers.push_back(user);
+      }
+
       builder.setInsertionPoint(allocaOp);
       auto sharedAllocOp = omp::AllocSharedMemOp::create(
           builder, allocaOp->getLoc(), LLVM::LLVMPointerType::get(context),
           allocaOp.getElemTypeAttr(), allocaOp.getArraySize(),
           allocaOp.getAlignmentAttr());
       if (nonDefaultAddrSpace) {
-        assert(allocaOp->hasOneUse() && " unsupported non-default address "
-                                        "space alloca with multiple uses");
-        auto asCastOp =
-            cast<LLVM::AddrSpaceCastOp>(*allocaOp->getUsers().begin());
+        assert(otherUsers.size() == 1 && " unsupported non-default address "
+                                         "space alloca with multiple uses");
+        auto asCastOp = cast<LLVM::AddrSpaceCastOp>(otherUsers.front());
         asCastOp.replaceAllUsesWith(sharedAllocOp.getOperation());
         // Delete later because we can't delete the cast op before the top-level
         // iteration visits it. Also, the alloca can't be deleted before because
         // it's used by it.
         toBeDeleted.push_back(asCastOp);
-        toBeDeleted.push_back(allocaOp);
       } else {
-        allocaOp.replaceAllUsesWith(sharedAllocOp.getOperation());
-        allocaOp.erase();
+        allocaOp.getResult().replaceUsesWithIf(
+            sharedAllocOp.getResult(), [](OpOperand &use) {
+              return !isa<LLVM::LifetimeStartOp, LLVM::LifetimeEndOp>(
+                  use.getOwner());
+            });
       }
+      toBeDeleted.push_back(allocaOp);
 
       // Create a new omp.free_shared_mem for the allocated buffer prior to
       // exiting the region.
diff --git a/mlir/test/Dialect/OpenMP/stack-to-shared.mlir b/mlir/test/Dialect/OpenMP/stack-to-shared.mlir
index 2f71674e6257f6..e62d304eb7bc87 100644
--- a/mlir/test/Dialect/OpenMP/stack-to-shared.mlir
+++ b/mlir/test/Dialect/OpenMP/stack-to-shared.mlir
@@ -115,6 +115,41 @@ llvm.func @host_func(%arg0: i64) {
   llvm.return
 }
 
+// Lifetime markers on a replaced alloca are dropped, since they must refer to
+// an alloca.
+// CHECK-LABEL: llvm.func @lifetime_markers(
+llvm.func @lifetime_markers() {
+  // CHECK: omp.target kernel_type(generic)
+  omp.target kernel_type(generic) {
+    %c = llvm.mlir.constant(1 : i64) : i64
+    // CHECK: %[[ALLOC0:.*]] = omp.alloc_shared_mem {{.*}} x i32 : (i64) -> !llvm.ptr
+    // CHECK-NOT: llvm.addrspacecast
+    %0 = llvm.alloca %c x i32 : (i64) -> !llvm.ptr<5>
+    %1 = llvm.addrspacecast %0 : !llvm.ptr<5> to !llvm.ptr
+    // CHECK: %[[ALLOC1:.*]] = omp.alloc_shared_mem {{.*}} x i32 : (i64) -> !llvm.ptr
+    %2 = llvm.alloca %c x i32 : (i64) -> !llvm.ptr
+    // CHECK-NOT: llvm.intr.lifetime.start
+    llvm.intr.lifetime.start %0 : !llvm.ptr<5>
+    llvm.intr.lifetime.start %2 : !llvm.ptr
+    // CHECK: omp.parallel
+    omp.parallel {
+      // CHECK: llvm.call @foo(%[[ALLOC0]]) : (!llvm.ptr) -> ()
+      llvm.call @foo(%1) : (!llvm.ptr) -> ()
+      // CHECK: llvm.call @foo(%[[ALLOC1]]) : (!llvm.ptr) -> ()
+      llvm.call @foo(%2) : (!llvm.ptr) -> ()
+      omp.terminator
+    }
+    // CHECK-NOT: llvm.intr.lifetime.end
+    llvm.intr.lifetime.end %2 : !llvm.ptr
+    llvm.intr.lifetime.end %0 : !llvm.ptr<5>
+    // CHECK: omp.free_shared_mem {{.*}} %[[ALLOC0]] : !llvm.ptr
+    // CHECK: omp.free_shared_mem {{.*}} %[[ALLOC1]] : !llvm.ptr
+    // CHECK-NEXT: omp.terminator
+    omp.terminator
+  }
+  llvm.return
+}
+
 // CHECK-LABEL: llvm.func @target_spmd(
 llvm.func @target_spmd() {
   // CHECK-NOT: omp.alloc_shared_mem



More information about the cfe-commits mailing list