Skip to content

Commit

Permalink
[flang] Single construct translation from PFT to FIR
Browse files Browse the repository at this point in the history
This patch adds translation for single construct along with nowait
clause from PFT to FIR.

Allocate clause is added as a TODO as handleAllocateClause is added in
D122302.

Reviewed By: kiranchandramohan

Differential Revision: https://reviews.llvm.org/D122324
  • Loading branch information
shraiysh committed Mar 24, 2022
1 parent 582836f commit cd28353
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
16 changes: 16 additions & 0 deletions flang/lib/Lower/OpenMP.cpp
Expand Up @@ -140,6 +140,8 @@ genOMP(Fortran::lower::AbstractConverter &converter,
std::get<Fortran::parser::OmpBeginBlockDirective>(blockConstruct.t);
const auto &blockDirective =
std::get<Fortran::parser::OmpBlockDirective>(beginBlockDirective.t);
const auto &endBlockDirective =
std::get<Fortran::parser::OmpEndBlockDirective>(blockConstruct.t);

auto &firOpBuilder = converter.getFirOpBuilder();
auto currentLocation = converter.getCurrentLocation();
Expand Down Expand Up @@ -200,6 +202,20 @@ genOMP(Fortran::lower::AbstractConverter &converter,
auto masterOp =
firOpBuilder.create<mlir::omp::MasterOp>(currentLocation, argTy);
createBodyOfOp<omp::MasterOp>(masterOp, firOpBuilder, currentLocation);

// Single Construct
} else if (blockDirective.v == llvm::omp::OMPD_single) {
mlir::UnitAttr nowaitAttr;
for (const auto &clause :
std::get<Fortran::parser::OmpClauseList>(endBlockDirective.t).v) {
if (std::get_if<Fortran::parser::OmpClause::Nowait>(&clause.u))
nowaitAttr = firOpBuilder.getUnitAttr();
// TODO: Handle allocate clause (D122302)
}
auto singleOp = firOpBuilder.create<mlir::omp::SingleOp>(
currentLocation, /*allocate_vars=*/ValueRange(),
/*allocators_vars=*/ValueRange(), nowaitAttr);
createBodyOfOp(singleOp, firOpBuilder, currentLocation);
}
}

Expand Down
46 changes: 46 additions & 0 deletions flang/test/Lower/OpenMP/single.f90
@@ -0,0 +1,46 @@
!RUN: %flang_fc1 -emit-fir -fopenmp %s -o - | FileCheck %s --check-prefixes="FIRDialect,OMPDialect"
!RUN: %flang_fc1 -emit-fir -fopenmp %s -o - | fir-opt --cfg-conversion | fir-opt --fir-to-llvm-ir | FileCheck %s --check-prefixes="OMPDialect"

!===============================================================================
! Single construct
!===============================================================================

!FIRDialect-LABEL: func @_QPomp_single
!FIRDialect-SAME: (%[[x:.*]]: !fir.ref<i32> {fir.bindc_name = "x"})
subroutine omp_single(x)
integer, intent(inout) :: x
!OMPDialect: omp.parallel
!$omp parallel
!OMPDialect: omp.single
!$omp single
!FIRDialect: %[[xval:.*]] = fir.load %[[x]] : !fir.ref<i32>
!FIRDialect: %[[res:.*]] = arith.addi %[[xval]], %{{.*}} : i32
!FIRDialect: fir.store %[[res]] to %[[x]] : !fir.ref<i32>
x = x + 12
!OMPDialect: omp.terminator
!$omp end single
!OMPDialect: omp.terminator
!$omp end parallel
end subroutine omp_single

!===============================================================================
! Single construct with nowait
!===============================================================================

!FIRDialect-LABEL: func @_QPomp_single_nowait
!FIRDialect-SAME: (%[[x:.*]]: !fir.ref<i32> {fir.bindc_name = "x"})
subroutine omp_single_nowait(x)
integer, intent(inout) :: x
!OMPDialect: omp.parallel
!$omp parallel
!OMPDialect: omp.single nowait
!$omp single
!FIRDialect: %[[xval:.*]] = fir.load %[[x]] : !fir.ref<i32>
!FIRDialect: %[[res:.*]] = arith.addi %[[xval]], %{{.*}} : i32
!FIRDialect: fir.store %[[res]] to %[[x]] : !fir.ref<i32>
x = x + 12
!OMPDialect: omp.terminator
!$omp end single nowait
!OMPDialect: omp.terminator
!$omp end parallel
end subroutine omp_single_nowait

0 comments on commit cd28353

Please sign in to comment.