From b5d77cc8fa37e1eb8791565cd2f65ba7385786f9 Mon Sep 17 00:00:00 2001 From: Jack Styles Date: Wed, 5 Nov 2025 15:43:19 +0000 Subject: [PATCH] [Flang][OpenMP] Add Lowering support for lastprivate in taskloops lastprivate support already exists within the DataSharingProcessor, so this can be extended to include the use of taskloops to enable AST->FIR lowering. The related error message has also been updated to indicate that taskloops are now supported. --- .../lib/Lower/OpenMP/DataSharingProcessor.cpp | 5 ++-- flang/lib/Lower/OpenMP/OpenMP.cpp | 4 +-- .../Lower/OpenMP/taskloop-lastprivate.f90 | 25 +++++++++++++++++++ 3 files changed, 30 insertions(+), 4 deletions(-) create mode 100644 flang/test/Lower/OpenMP/taskloop-lastprivate.f90 diff --git a/flang/lib/Lower/OpenMP/DataSharingProcessor.cpp b/flang/lib/Lower/OpenMP/DataSharingProcessor.cpp index 146a252b049ec..83c2eda0a2dc7 100644 --- a/flang/lib/Lower/OpenMP/DataSharingProcessor.cpp +++ b/flang/lib/Lower/OpenMP/DataSharingProcessor.cpp @@ -342,7 +342,8 @@ void DataSharingProcessor::insertLastPrivateCompare(mlir::Operation *op) { if (!hasLastPrivate) return; - if (mlir::isa(op) || mlir::isa(op)) { + if (mlir::isa(op) || mlir::isa(op) || + mlir::isa(op)) { mlir::omp::LoopRelatedClauseOps result; llvm::SmallVector iv; collectLoopRelatedInfo(converter, converter.getCurrentLocation(), eval, @@ -408,7 +409,7 @@ void DataSharingProcessor::insertLastPrivateCompare(mlir::Operation *op) { } else { TODO(converter.getCurrentLocation(), "lastprivate clause in constructs other than " - "simd/worksharing-loop"); + "simd/worksharing-loop/taskloop"); } } diff --git a/flang/lib/Lower/OpenMP/OpenMP.cpp b/flang/lib/Lower/OpenMP/OpenMP.cpp index 51170a39d272b..8c56480390e58 100644 --- a/flang/lib/Lower/OpenMP/OpenMP.cpp +++ b/flang/lib/Lower/OpenMP/OpenMP.cpp @@ -1776,8 +1776,8 @@ static void genTaskloopClauses( cp.processTODO( + clause::Mergeable, clause::Nogroup, clause::Priority, + clause::Shared, clause::Untied>( loc, llvm::omp::Directive::OMPD_taskloop); } diff --git a/flang/test/Lower/OpenMP/taskloop-lastprivate.f90 b/flang/test/Lower/OpenMP/taskloop-lastprivate.f90 new file mode 100644 index 0000000000000..a3562a971bb59 --- /dev/null +++ b/flang/test/Lower/OpenMP/taskloop-lastprivate.f90 @@ -0,0 +1,25 @@ +! Test the lastprivate clause when used with the taskloop construct +! RUN: bbc -emit-hlfir -fopenmp -fopenmp-version=45 %s -o - 2>&1 | FileCheck %s +! RUN: %flang_fc1 -emit-hlfir -fopenmp -fopenmp-version=45 %s -o - 2>&1 | FileCheck %s + +! CHECK-LABEL: omp.private +! CHECK-SAME: {type = private} @[[I_PRIVATE:.*]] : i32 +! CHECK-LABEL: omp.private +! CHECK-SAME: {type = private} @[[LAST_I_PRIVATE:.*]] : i32 + +! CHECK: %[[ALLOCA_I:.*]] = fir.alloca i32 {bindc_name = "i", uniq_name = "_QFlastprivateEi"} +! CHECK: %[[DECLARE_I:.*]]:2 = hlfir.declare %[[ALLOCA_I]] {uniq_name = "_QFlastprivateEi"} : (!fir.ref) -> (!fir.ref, !fir.ref) +! CHECK: %[[ALLOCA_LAST_I:.*]] = fir.alloca i32 {bindc_name = "last_i", uniq_name = "_QFlastprivateElast_i"} +! CHECK: %[[DECLARE_LAST_I:.*]]:2 = hlfir.declare %[[ALLOCA_LAST_I]] {uniq_name = "_QFlastprivateElast_i"} : (!fir.ref) -> (!fir.ref, !fir.ref) + +subroutine lastprivate() + integer :: i, last_i + + ! CHECK: omp.taskloop + ! CHECK-SAME: private(@[[LAST_I_PRIVATE]] %[[DECLARE_LAST_I]]#0 -> %arg0, @[[I_PRIVATE]] %[[DECLARE_I]]#0 -> %arg1 : !fir.ref, !fir.ref) { + !$omp taskloop lastprivate(last_i) + do i=1,10 + last_i = i + end do + !$omp end taskloop +end \ No newline at end of file