[flang-commits] [flang] [mlir] [Flang][OpenMP] Add lowering support for the threadset clause (PR #214992)

via flang-commits flang-commits at lists.llvm.org
Sat Aug 8 10:08:13 PDT 2026


https://github.com/Ritanya-B-Bharadwaj created https://github.com/llvm/llvm-project/pull/214992

Adds OpenMP dialect representation and Flang lowering for the OpenMP 6.0 `threadset` clause (spec 14.8) on `task` and `taskloop`.

- Add `ThreadsetPolicy` enum (`omp_pool`/`omp_team`) and `OpenMP_ThreadsetClause` on `omp.task` and `omp.taskloop.context`.
- Lower the clause in Flang via `ClauseProcessor::processThreadset`.
- Add semantics/lowering/dialect tests; remove the obsolete lowering TODO.

Related: https://github.com/llvm/llvm-project/pull/135807, https://github.com/llvm/llvm-project/pull/144409, https://github.com/llvm/llvm-project/pull/169856

>From d9088df4153f2b9b73a47bc0daf911ef289eb060 Mon Sep 17 00:00:00 2001
From: Ritanya-B-Bharadwaj <ritanya.b.bharadwaj at gmail.com>
Date: Sat, 8 Aug 2026 22:25:42 +0530
Subject: [PATCH] [Flang][OpenMP] Add lowering support for the threadset clause

---
 flang/docs/OpenMPSupport.md                   |  2 +-
 flang/lib/Lower/OpenMP/ClauseProcessor.cpp    | 16 ++++++++
 flang/lib/Lower/OpenMP/ClauseProcessor.h      |  1 +
 flang/lib/Lower/OpenMP/OpenMP.cpp             |  3 ++
 flang/test/Lower/OpenMP/Todo/threadset.f90    | 10 -----
 flang/test/Lower/OpenMP/threadset.f90         | 37 +++++++++++++++++
 .../Semantics/OpenMP/threadset-clause-v60.f90 | 38 +++++++++++++++++
 .../Semantics/OpenMP/threadset-clause.f90     |  7 ++++
 .../mlir/Dialect/OpenMP/OpenMPClauses.td      | 27 ++++++++++++
 .../mlir/Dialect/OpenMP/OpenMPEnums.td        | 16 ++++++++
 mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td |  4 +-
 mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp  |  6 ++-
 mlir/test/Dialect/OpenMP/ops.mlir             | 41 +++++++++++++++++++
 13 files changed, 193 insertions(+), 15 deletions(-)
 delete mode 100644 flang/test/Lower/OpenMP/Todo/threadset.f90
 create mode 100644 flang/test/Lower/OpenMP/threadset.f90
 create mode 100644 flang/test/Semantics/OpenMP/threadset-clause-v60.f90

diff --git a/flang/docs/OpenMPSupport.md b/flang/docs/OpenMPSupport.md
index fd6731fd28a2b..ed5479e1987ec 100644
--- a/flang/docs/OpenMPSupport.md
+++ b/flang/docs/OpenMPSupport.md
@@ -224,7 +224,7 @@ Parser/Semantics, MLIR, Lowering, or the OpenMPIRBuilder.
 
 | Feature | Status | Claimed By | Notes | Reviews |
 |:--------|:-------|:-----------|:------|:--------|
-| threadset clause | <span class="part">partial</span> | | Semantics coverage exists (`flang/test/Semantics/OpenMP/threadset-clause.f90`) with ongoing lowering/runtime validation. | [llvm/llvm-project#169856](https://github.com/llvm/llvm-project/pull/169856) |
+| threadset clause | <span class="part">partial</span> | | Frontend (parsing + semantics) and lowering coverage exists (`flang/test/Semantics/OpenMP/threadset-clause.f90`, `flang/test/Semantics/OpenMP/threadset-clause-v60.f90`, `flang/test/Lower/OpenMP/threadset.f90`); the clause is represented on `omp.task`/`omp.taskloop.context`, with LLVM IR translation to the runtime free-agent flag still pending. | [llvm/llvm-project#169856](https://github.com/llvm/llvm-project/pull/169856) |
 | groupprivate directive | <span class="part">partial</span> | | Semantics and lowering coverage exists (`flang/test/Semantics/OpenMP/groupprivate.f90`, `flang/test/Lower/OpenMP/groupprivate.f90`, `flang/test/Lower/OpenMP/groupprivate-modfile.f90`), with remaining limitations in lowering scope/placement (for example currently materialized for `teams`-based paths). | [llvm/llvm-project#166199](https://github.com/llvm/llvm-project/pull/166199), [llvm/llvm-project#166214](https://github.com/llvm/llvm-project/pull/166214), [llvm/llvm-project#180934](https://github.com/llvm/llvm-project/pull/180934) |
 | recording of task graphs | <span class="progress">in progress</span> | | Semantics coverage exists (`flang/test/Semantics/OpenMP/taskgraph.f90`). | |
 | workdistribute construct | <span class="part">partial</span> | | Semantics/lowering/transform coverage exists (`flang/test/Semantics/OpenMP/workdistribute01.f90`, `flang/test/Lower/OpenMP/workdistribute.f90`, `flang/test/Transforms/OpenMP/lower-workdistribute-fission.mlir`) including `target teams` placement updates; some team-nesting combinations still intentionally diagnose as unsupported. | [llvm/llvm-project#154377](https://github.com/llvm/llvm-project/pull/154377), [llvm/llvm-project#154378](https://github.com/llvm/llvm-project/pull/154378), [llvm/llvm-project#140523](https://github.com/llvm/llvm-project/pull/140523), [llvm/llvm-project#199006](https://github.com/llvm/llvm-project/pull/199006) |
diff --git a/flang/lib/Lower/OpenMP/ClauseProcessor.cpp b/flang/lib/Lower/OpenMP/ClauseProcessor.cpp
index 318bdaa4f2f30..42c08fe26849a 100644
--- a/flang/lib/Lower/OpenMP/ClauseProcessor.cpp
+++ b/flang/lib/Lower/OpenMP/ClauseProcessor.cpp
@@ -975,6 +975,22 @@ bool ClauseProcessor::processThreadLimit(
   return false;
 }
 
+bool ClauseProcessor::processThreadset(
+    mlir::omp::ThreadsetClauseOps &result) const {
+  using Threadset = omp::clause::Threadset;
+  if (auto *clause = findUniqueClause<Threadset>()) {
+    fir::FirOpBuilder &firOpBuilder = converter.getFirOpBuilder();
+    mlir::omp::ThreadsetPolicy policy =
+        clause->v == Threadset::ThreadsetPolicy::Omp_Pool
+            ? mlir::omp::ThreadsetPolicy::omp_pool
+            : mlir::omp::ThreadsetPolicy::omp_team;
+    result.threadset =
+        mlir::omp::ThreadsetPolicyAttr::get(firOpBuilder.getContext(), policy);
+    return true;
+  }
+  return false;
+}
+
 bool ClauseProcessor::processUntied(mlir::omp::UntiedClauseOps &result) const {
   return markClauseOccurrence<omp::clause::Untied>(result.untied);
 }
diff --git a/flang/lib/Lower/OpenMP/ClauseProcessor.h b/flang/lib/Lower/OpenMP/ClauseProcessor.h
index cb42b6524e2e7..6647121082c37 100644
--- a/flang/lib/Lower/OpenMP/ClauseProcessor.h
+++ b/flang/lib/Lower/OpenMP/ClauseProcessor.h
@@ -124,6 +124,7 @@ class ClauseProcessor {
   bool processSimd(mlir::omp::OrderedRegionOperands &result) const;
   bool processThreadLimit(lower::StatementContext &stmtCtx,
                           mlir::omp::ThreadLimitClauseOps &result) const;
+  bool processThreadset(mlir::omp::ThreadsetClauseOps &result) const;
   bool processUntied(mlir::omp::UntiedClauseOps &result) const;
   bool processDetach(mlir::omp::DetachClauseOps &result) const;
   // 'Repeatable' clauses: They can appear multiple times in the clause list.
diff --git a/flang/lib/Lower/OpenMP/OpenMP.cpp b/flang/lib/Lower/OpenMP/OpenMP.cpp
index 04df4f6e69377..aca8715b23c8b 100644
--- a/flang/lib/Lower/OpenMP/OpenMP.cpp
+++ b/flang/lib/Lower/OpenMP/OpenMP.cpp
@@ -2587,6 +2587,7 @@ static void genTaskClauses(lower::AbstractConverter &converter,
   cp.processInReduction(loc, clauseOps, inReductionObjects);
   cp.processMergeable(clauseOps);
   cp.processPriority(stmtCtx, clauseOps);
+  cp.processThreadset(clauseOps);
   cp.processUntied(clauseOps);
   cp.processDetach(clauseOps);
 }
@@ -2620,6 +2621,7 @@ static void genTaskloopClauses(
   cp.processNumTasks(stmtCtx, clauseOps);
   cp.processPriority(stmtCtx, clauseOps);
   cp.processReduction(loc, clauseOps, reductionObjects);
+  cp.processThreadset(clauseOps);
   cp.processUntied(clauseOps);
 }
 
@@ -6150,6 +6152,7 @@ static void genOMP(lower::AbstractConverter &converter, lower::SymMap &symTable,
         !std::holds_alternative<clause::Simd>(clause.u) &&
         !std::holds_alternative<clause::ThreadLimit>(clause.u) &&
         !std::holds_alternative<clause::Threads>(clause.u) &&
+        !std::holds_alternative<clause::Threadset>(clause.u) &&
         !std::holds_alternative<clause::UseDeviceAddr>(clause.u) &&
         !std::holds_alternative<clause::UseDevicePtr>(clause.u) &&
         !std::holds_alternative<clause::InReduction>(clause.u) &&
diff --git a/flang/test/Lower/OpenMP/Todo/threadset.f90 b/flang/test/Lower/OpenMP/Todo/threadset.f90
deleted file mode 100644
index b022baf02654b..0000000000000
--- a/flang/test/Lower/OpenMP/Todo/threadset.f90
+++ /dev/null
@@ -1,10 +0,0 @@
-! RUN: %not_todo_cmd %flang_fc1 -emit-fir -fopenmp -fopenmp-version=60 -o - %s 2>&1 | FileCheck %s
-
-! CHECK: not yet implemented: THREADSET clause is not implemented yet
-
-subroutine f00(x)
-  integer :: x(10)
-  !$omp task threadset(omp_pool)
-  x = x + 1
-  !$omp end task
-end
diff --git a/flang/test/Lower/OpenMP/threadset.f90 b/flang/test/Lower/OpenMP/threadset.f90
new file mode 100644
index 0000000000000..c758e160afc01
--- /dev/null
+++ b/flang/test/Lower/OpenMP/threadset.f90
@@ -0,0 +1,37 @@
+!RUN: %flang_fc1 -emit-hlfir -fopenmp -fopenmp-version=60 %s -o - | FileCheck %s
+
+!CHECK-LABEL: func @_QPtask_threadset_pool
+subroutine task_threadset_pool()
+  !CHECK: omp.task threadset(omp_pool) {
+  !$omp task threadset(omp_pool)
+  !CHECK: omp.terminator
+  !$omp end task
+end subroutine task_threadset_pool
+
+!CHECK-LABEL: func @_QPtask_threadset_team
+subroutine task_threadset_team()
+  !CHECK: omp.task threadset(omp_team) {
+  !$omp task threadset(omp_team)
+  !CHECK: omp.terminator
+  !$omp end task
+end subroutine task_threadset_team
+
+!CHECK-LABEL: func @_QPtaskloop_threadset_pool
+subroutine taskloop_threadset_pool()
+  integer :: i
+  !CHECK: omp.taskloop.context threadset(omp_pool)
+  !$omp taskloop threadset(omp_pool)
+  do i = 1, 10
+  end do
+  !$omp end taskloop
+end subroutine taskloop_threadset_pool
+
+!CHECK-LABEL: func @_QPtaskloop_threadset_team
+subroutine taskloop_threadset_team()
+  integer :: i
+  !CHECK: omp.taskloop.context threadset(omp_team)
+  !$omp taskloop threadset(omp_team)
+  do i = 1, 10
+  end do
+  !$omp end taskloop
+end subroutine taskloop_threadset_team
diff --git a/flang/test/Semantics/OpenMP/threadset-clause-v60.f90 b/flang/test/Semantics/OpenMP/threadset-clause-v60.f90
new file mode 100644
index 0000000000000..f5542e4941cc2
--- /dev/null
+++ b/flang/test/Semantics/OpenMP/threadset-clause-v60.f90
@@ -0,0 +1,38 @@
+!RUN: %python %S/../test_errors.py %s %flang -fopenmp -fopenmp-version=60
+
+subroutine f00(x)
+  integer :: x(10)
+  integer :: i
+
+! Valid uses on TASK.
+!$omp task threadset(omp_pool)
+  x = x + 1
+!$omp end task
+
+!$omp task threadset(omp_team)
+  x = x + 1
+!$omp end task
+
+! Valid uses on TASKLOOP.
+!$omp taskloop threadset(omp_pool)
+  do i = 1, 10
+  end do
+!$omp end taskloop
+
+!$omp taskloop threadset(omp_team)
+  do i = 1, 10
+  end do
+!$omp end taskloop
+
+! At most one THREADSET clause is allowed on the directive.
+!ERROR: At most one THREADSET clause can appear on TASK directive
+!$omp task threadset(omp_pool) threadset(omp_team)
+  x = x + 1
+!$omp end task
+
+! THREADSET is only allowed on TASK and TASKLOOP.
+!ERROR: THREADSET clause is not allowed on PARALLEL directive
+!$omp parallel threadset(omp_pool)
+  x = x + 1
+!$omp end parallel
+end
diff --git a/flang/test/Semantics/OpenMP/threadset-clause.f90 b/flang/test/Semantics/OpenMP/threadset-clause.f90
index 9c811d440fb6a..65a7ed74037cc 100644
--- a/flang/test/Semantics/OpenMP/threadset-clause.f90
+++ b/flang/test/Semantics/OpenMP/threadset-clause.f90
@@ -2,8 +2,15 @@
 
 subroutine f00(x)
   integer :: x(10)
+  integer :: i
 !ERROR: THREADSET clause is not allowed on TASK directive in OpenMP v4.5, try -fopenmp-version=60
 !$omp task threadset(omp_pool)
   x = x + 1
 !$omp end task
+
+!ERROR: THREADSET clause is not allowed on TASKLOOP directive in OpenMP v4.5, try -fopenmp-version=60
+!$omp taskloop threadset(omp_team)
+  do i = 1, 10
+  end do
+!$omp end taskloop
 end
diff --git a/mlir/include/mlir/Dialect/OpenMP/OpenMPClauses.td b/mlir/include/mlir/Dialect/OpenMP/OpenMPClauses.td
index cd1223dc1702c..a09bcdec101ec 100644
--- a/mlir/include/mlir/Dialect/OpenMP/OpenMPClauses.td
+++ b/mlir/include/mlir/Dialect/OpenMP/OpenMPClauses.td
@@ -1706,6 +1706,33 @@ class OpenMP_ThreadLimitClauseSkip<
 
 def OpenMP_ThreadLimitClause : OpenMP_ThreadLimitClauseSkip<>;
 
+//===----------------------------------------------------------------------===//
+// V6.0: [14.8] `threadset` clause
+//===----------------------------------------------------------------------===//
+
+class OpenMP_ThreadsetClauseSkip<
+    bit traits = false, bit arguments = false, bit assemblyFormat = false,
+    bit description = false, bit extraClassDeclaration = false
+  > : OpenMP_Clause<traits, arguments, assemblyFormat, description,
+                    extraClassDeclaration> {
+  let arguments = (ins
+    OptionalAttr<ThreadsetPolicyAttr>:$threadset
+  );
+
+  let optAssemblyFormat = [{
+    `threadset` `(` custom<ClauseAttr>($threadset) `)`
+  }];
+
+  let description = [{
+    The `threadset` clause specifies the set of threads that may execute the
+    generated task. When the policy is `omp_pool`, the task is eligible to be
+    executed by a free-agent thread. When the policy is `omp_team`, the task
+    may only be executed by a thread in the current team.
+  }];
+}
+
+def OpenMP_ThreadsetClause : OpenMP_ThreadsetClauseSkip<>;
+
 //===----------------------------------------------------------------------===//
 // V5.2: [9.1.1] `sizes` clause
 //===----------------------------------------------------------------------===//
diff --git a/mlir/include/mlir/Dialect/OpenMP/OpenMPEnums.td b/mlir/include/mlir/Dialect/OpenMP/OpenMPEnums.td
index deda86b26db61..cf7e8d60d25fc 100644
--- a/mlir/include/mlir/Dialect/OpenMP/OpenMPEnums.td
+++ b/mlir/include/mlir/Dialect/OpenMP/OpenMPEnums.td
@@ -377,4 +377,20 @@ def FallbackModifierAttr : OpenMP_EnumAttr<FallbackModifier,
   let assemblyFormat = "`(` $value `)`";
 }
 
+//===----------------------------------------------------------------------===//
+// threadset_policy enum.
+//===----------------------------------------------------------------------===//
+
+def ThreadsetOmpPool : I32EnumAttrCase<"omp_pool", 0>;
+def ThreadsetOmpTeam : I32EnumAttrCase<"omp_team", 1>;
+
+def ThreadsetPolicy : OpenMP_I32EnumAttr<
+    "ThreadsetPolicy",
+    "threadset policy", [
+      ThreadsetOmpPool,
+      ThreadsetOmpTeam
+    ]>;
+
+def ThreadsetPolicyAttr : OpenMP_EnumAttr<ThreadsetPolicy, "threadset_policy">;
+
 #endif // OPENMP_ENUMS
diff --git a/mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td b/mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td
index 98242a66e411f..a655c987cb2fa 100644
--- a/mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td
+++ b/mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td
@@ -1004,7 +1004,7 @@ def TaskOp : OpenMP_Op<"task", traits = [
     OpenMP_FinalClause, OpenMP_IfClause,
     OpenMP_InReductionClauseSkip<assemblyFormat = true>,
     OpenMP_MergeableClause, OpenMP_PriorityClause, OpenMP_PrivateClause,
-    OpenMP_UntiedClause, OpenMP_DetachClause
+    OpenMP_ThreadsetClause, OpenMP_UntiedClause, OpenMP_DetachClause
   ], singleRegion = true> {
   let summary = "task construct";
   let description = [{
@@ -1051,7 +1051,7 @@ def TaskloopContextOp : OpenMP_Op<"taskloop.context", traits = [
     OpenMP_IfClause, OpenMP_InReductionClauseSkip<assemblyFormat = true>,
     OpenMP_MergeableClause, OpenMP_NogroupClause, OpenMP_NumTasksClause,
     OpenMP_PriorityClause, OpenMP_PrivateClause, OpenMP_ReductionClause,
-    OpenMP_UntiedClause
+    OpenMP_ThreadsetClause, OpenMP_UntiedClause
   ], singleRegion = true> {
   let summary = "OutlinableOpenMPOpInterface wrapper for taskloop construct";
   let description = [{
diff --git a/mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp b/mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp
index 67bc2bdf35619..2d386622bfab8 100644
--- a/mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp
+++ b/mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp
@@ -3669,7 +3669,8 @@ void TaskOp::build(OpBuilder &builder, OperationState &state,
       makeArrayAttr(ctx, clauses.inReductionSyms), clauses.mergeable,
       clauses.priority, /*private_vars=*/clauses.privateVars,
       /*private_syms=*/makeArrayAttr(ctx, clauses.privateSyms),
-      clauses.privateNeedsBarrier, clauses.untied, clauses.eventHandle);
+      clauses.privateNeedsBarrier, clauses.threadset, clauses.untied,
+      clauses.eventHandle);
 }
 
 LogicalResult TaskOp::verify() {
@@ -3723,7 +3724,8 @@ void TaskloopContextOp::build(OpBuilder &builder, OperationState &state,
       /*private_syms=*/makeArrayAttr(ctx, clauses.privateSyms),
       clauses.privateNeedsBarrier, clauses.reductionMod, clauses.reductionVars,
       makeDenseBoolArrayAttr(ctx, clauses.reductionByref),
-      makeArrayAttr(ctx, clauses.reductionSyms), clauses.untied);
+      makeArrayAttr(ctx, clauses.reductionSyms), clauses.threadset,
+      clauses.untied);
   state.addAttribute("omp.combined", UnitAttr::get(ctx));
 }
 
diff --git a/mlir/test/Dialect/OpenMP/ops.mlir b/mlir/test/Dialect/OpenMP/ops.mlir
index 9a1874379d2fb..b89d4e866cf28 100644
--- a/mlir/test/Dialect/OpenMP/ops.mlir
+++ b/mlir/test/Dialect/OpenMP/ops.mlir
@@ -2300,6 +2300,23 @@ func.func @omp_task(%bool_var: i1, %i64_var: i64, %i32_var: i32, %data_var: memr
     omp.terminator
   }
 
+  // Checking `threadset` clause
+  // CHECK: omp.task threadset(omp_pool) {
+  omp.task threadset(omp_pool) {
+    // CHECK: "test.foo"() : () -> ()
+    "test.foo"() : () -> ()
+    // CHECK: omp.terminator
+    omp.terminator
+  }
+
+  // CHECK: omp.task threadset(omp_team) {
+  omp.task threadset(omp_team) {
+    // CHECK: "test.foo"() : () -> ()
+    "test.foo"() : () -> ()
+    // CHECK: omp.terminator
+    omp.terminator
+  }
+
   // Checking `in_reduction` clause
   %c1 = arith.constant 1 : i32
   // CHECK: %[[redn_var1:.*]] = llvm.alloca %{{.*}} x f32 : (i32) -> !llvm.ptr
@@ -2829,6 +2846,30 @@ func.func @omp_taskloop(%lb: i32, %ub: i32, %step: i32) -> () {
     omp.terminator
   } {omp.combined}
 
+  // CHECK: omp.taskloop.context threadset(omp_pool) {
+  omp.taskloop.context threadset(omp_pool) {
+    // CHECK: omp.taskloop.wrapper {
+    omp.taskloop.wrapper {
+      omp.loop_nest (%i, %j) : i32 = (%lb, %ub) to (%ub, %lb) step (%step, %step) {
+        // CHECK: omp.yield
+        omp.yield
+      }
+    }
+    omp.terminator
+  } {omp.combined}
+
+  // CHECK: omp.taskloop.context threadset(omp_team) {
+  omp.taskloop.context threadset(omp_team) {
+    // CHECK: omp.taskloop.wrapper {
+    omp.taskloop.wrapper {
+      omp.loop_nest (%i, %j) : i32 = (%lb, %ub) to (%ub, %lb) step (%step, %step) {
+        // CHECK: omp.yield
+        omp.yield
+      }
+    }
+    omp.terminator
+  } {omp.combined}
+
   %testf32 = "test.f32"() : () -> (!llvm.ptr)
   %testf32_2 = "test.f32"() : () -> (!llvm.ptr)
   // CHECK: omp.taskloop.context in_reduction(@add_f32 %{{.+}} -> %{{.+}}, @add_f32 %{{.+}} -> %{{.+}} : !llvm.ptr, !llvm.ptr) {



More information about the flang-commits mailing list