Skip to content

Commit 918ac62

Browse files
authored
[Flang][OpenMP] Add lowering support for DISTRIBUTE SIMD (#97819)
This patch adds support for lowering 'DISTRIBUTE SIMD' constructs to MLIR. Translation of `omp.distribute` operations to LLVM IR is still not supported, so its composition with `omp.simd` isn't either.
1 parent 52d2d82 commit 918ac62

File tree

4 files changed

+351
-9
lines changed

4 files changed

+351
-9
lines changed

flang/lib/Lower/OpenMP/OpenMP.cpp

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1976,7 +1976,43 @@ static void genCompositeDistributeSimd(
19761976
semantics::SemanticsContext &semaCtx, lower::pft::Evaluation &eval,
19771977
mlir::Location loc, const ConstructQueue &queue,
19781978
ConstructQueue::iterator item, DataSharingProcessor &dsp) {
1979-
TODO(loc, "Composite DISTRIBUTE SIMD");
1979+
lower::StatementContext stmtCtx;
1980+
1981+
// Clause processing.
1982+
mlir::omp::DistributeClauseOps distributeClauseOps;
1983+
genDistributeClauses(converter, semaCtx, stmtCtx, item->clauses, loc,
1984+
distributeClauseOps);
1985+
1986+
mlir::omp::SimdClauseOps simdClauseOps;
1987+
genSimdClauses(converter, semaCtx, item->clauses, loc, simdClauseOps);
1988+
1989+
mlir::omp::LoopNestClauseOps loopNestClauseOps;
1990+
llvm::SmallVector<const semantics::Symbol *> iv;
1991+
genLoopNestClauses(converter, semaCtx, eval, item->clauses, loc,
1992+
loopNestClauseOps, iv);
1993+
1994+
// Operation creation.
1995+
// TODO: Populate entry block arguments with private variables.
1996+
auto distributeOp = genWrapperOp<mlir::omp::DistributeOp>(
1997+
converter, loc, distributeClauseOps, /*blockArgTypes=*/{});
1998+
1999+
// TODO: Populate entry block arguments with reduction and private variables.
2000+
auto simdOp = genWrapperOp<mlir::omp::SimdOp>(converter, loc, simdClauseOps,
2001+
/*blockArgTypes=*/{});
2002+
2003+
// Construct wrapper entry block list and associated symbols. It is important
2004+
// that the symbol order and the block argument order match, so that the
2005+
// symbol-value bindings created are correct.
2006+
// TODO: Add omp.distribute private and omp.simd private and reduction args.
2007+
auto wrapperArgs = llvm::to_vector(
2008+
llvm::concat<mlir::BlockArgument>(distributeOp.getRegion().getArguments(),
2009+
simdOp.getRegion().getArguments()));
2010+
2011+
assert(wrapperArgs.empty() &&
2012+
"Block args for omp.simd and omp.distribute currently not expected");
2013+
genLoopNestOp(converter, symTable, semaCtx, eval, loc, queue, item,
2014+
loopNestClauseOps, iv, /*wrapperSyms=*/{}, wrapperArgs,
2015+
llvm::omp::Directive::OMPD_distribute_simd, dsp);
19802016
}
19812017

19822018
static void genCompositeDoSimd(lower::AbstractConverter &converter,
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
! This test checks lowering of OpenMP DISTRIBUTE SIMD composite constructs.
2+
3+
! RUN: bbc -fopenmp -emit-hlfir %s -o - | FileCheck %s
4+
! RUN: %flang_fc1 -fopenmp -emit-hlfir %s -o - | FileCheck %s
5+
6+
! CHECK-LABEL: func.func @_QPdistribute_simd_aligned(
7+
subroutine distribute_simd_aligned(A)
8+
use iso_c_binding
9+
type(c_ptr) :: A
10+
11+
!$omp teams
12+
13+
! CHECK: omp.distribute
14+
! CHECK-NOT: aligned({{.*}})
15+
! CHECK-SAME: {
16+
! CHECK-NEXT: omp.simd
17+
! CHECK-SAME: aligned({{.*}})
18+
!$omp distribute simd aligned(A)
19+
do index_ = 1, 10
20+
call c_test_call(A)
21+
end do
22+
!$omp end distribute simd
23+
24+
!$omp end teams
25+
end subroutine distribute_simd_aligned
26+
27+
! CHECK-LABEL: func.func @_QPdistribute_simd_safelen(
28+
subroutine distribute_simd_safelen()
29+
!$omp teams
30+
31+
! CHECK: omp.distribute
32+
! CHECK-NOT: safelen({{.*}})
33+
! CHECK-SAME: {
34+
! CHECK-NEXT: omp.simd
35+
! CHECK-SAME: safelen({{.*}})
36+
!$omp distribute simd safelen(4)
37+
do index_ = 1, 10
38+
end do
39+
!$omp end distribute simd
40+
41+
!$omp end teams
42+
end subroutine distribute_simd_safelen
43+
44+
! CHECK-LABEL: func.func @_QPdistribute_simd_simdlen(
45+
subroutine distribute_simd_simdlen()
46+
!$omp teams
47+
48+
! CHECK: omp.distribute
49+
! CHECK-NOT: simdlen({{.*}})
50+
! CHECK-SAME: {
51+
! CHECK-NEXT: omp.simd
52+
! CHECK-SAME: simdlen({{.*}})
53+
!$omp distribute simd simdlen(4)
54+
do index_ = 1, 10
55+
end do
56+
!$omp end distribute simd
57+
58+
!$omp end teams
59+
end subroutine distribute_simd_simdlen

flang/test/Lower/OpenMP/if-clause.f90

Lines changed: 215 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,56 @@ program main
1111
! TODO When they are supported, add tests for:
1212
! - DISTRIBUTE PARALLEL DO
1313
! - DISTRIBUTE PARALLEL DO SIMD
14-
! - DISTRIBUTE SIMD
1514
! - PARALLEL SECTIONS
1615
! - PARALLEL WORKSHARE
1716
! - TARGET TEAMS DISTRIBUTE PARALLEL DO
1817
! - TARGET TEAMS DISTRIBUTE PARALLEL DO SIMD
19-
! - TARGET TEAMS DISTRIBUTE SIMD
2018
! - TARGET UPDATE
2119
! - TASKLOOP
2220
! - TASKLOOP SIMD
2321
! - TEAMS DISTRIBUTE PARALLEL DO
2422
! - TEAMS DISTRIBUTE PARALLEL DO SIMD
25-
! - TEAMS DISTRIBUTE SIMD
23+
24+
! ----------------------------------------------------------------------------
25+
! DISTRIBUTE SIMD
26+
! ----------------------------------------------------------------------------
27+
!$omp teams
28+
29+
! CHECK: omp.distribute
30+
! CHECK-NOT: if({{.*}})
31+
! CHECK-SAME: {
32+
! CHECK-NEXT: omp.simd
33+
! CHECK-NOT: if({{.*}})
34+
! CHECK-SAME: {
35+
! CHECK-NEXT: omp.loop_nest
36+
!$omp distribute simd
37+
do i = 1, 10
38+
end do
39+
!$omp end distribute simd
40+
41+
! CHECK: omp.distribute
42+
! CHECK-NOT: if({{.*}})
43+
! CHECK-SAME: {
44+
! CHECK-NEXT: omp.simd
45+
! CHECK-SAME: if({{.*}})
46+
! CHECK-NEXT: omp.loop_nest
47+
!$omp distribute simd if(.true.)
48+
do i = 1, 10
49+
end do
50+
!$omp end distribute simd
51+
52+
! CHECK: omp.distribute
53+
! CHECK-NOT: if({{.*}})
54+
! CHECK-SAME: {
55+
! CHECK-NEXT: omp.simd
56+
! CHECK-SAME: if({{.*}})
57+
! CHECK-NEXT: omp.loop_nest
58+
!$omp distribute simd if(simd: .true.)
59+
do i = 1, 10
60+
end do
61+
!$omp end distribute simd
62+
63+
!$omp end teams
2664

2765
! ----------------------------------------------------------------------------
2866
! DO SIMD
@@ -623,6 +661,108 @@ program main
623661
end do
624662
!$omp end target teams distribute
625663

664+
! ----------------------------------------------------------------------------
665+
! TARGET TEAMS DISTRIBUTE SIMD
666+
! ----------------------------------------------------------------------------
667+
! CHECK: omp.target
668+
! CHECK-NOT: if({{.*}})
669+
! CHECK-SAME: {
670+
! CHECK: omp.teams
671+
! CHECK-NOT: if({{.*}})
672+
! CHECK-SAME: {
673+
! CHECK: omp.distribute
674+
! CHECK-NOT: if({{.*}})
675+
! CHECK-SAME: {
676+
! CHECK-NEXT: omp.simd
677+
! CHECK-NOT: if({{.*}})
678+
! CHECK-SAME: {
679+
! CHECK-NEXT: omp.loop_nest
680+
!$omp target teams distribute simd
681+
do i = 1, 10
682+
end do
683+
!$omp end target teams distribute simd
684+
685+
! CHECK: omp.target
686+
! CHECK-SAME: if({{.*}})
687+
! CHECK: omp.teams
688+
! CHECK-SAME: if({{.*}})
689+
! CHECK: omp.distribute
690+
! CHECK-NOT: if({{.*}})
691+
! CHECK-SAME: {
692+
! CHECK-NEXT: omp.simd
693+
! CHECK-SAME: if({{.*}})
694+
! CHECK-NEXT: omp.loop_nest
695+
!$omp target teams distribute simd if(.true.)
696+
do i = 1, 10
697+
end do
698+
!$omp end target teams distribute simd
699+
700+
! CHECK: omp.target
701+
! CHECK-SAME: if({{.*}})
702+
! CHECK: omp.teams
703+
! CHECK-SAME: if({{.*}})
704+
! CHECK: omp.distribute
705+
! CHECK-NOT: if({{.*}})
706+
! CHECK-SAME: {
707+
! CHECK-NEXT: omp.simd
708+
! CHECK-SAME: if({{.*}})
709+
! CHECK-NEXT: omp.loop_nest
710+
!$omp target teams distribute simd if(target: .true.) if(teams: .false.) if(simd: .false.)
711+
do i = 1, 10
712+
end do
713+
!$omp end target teams distribute simd
714+
715+
! CHECK: omp.target
716+
! CHECK-SAME: if({{.*}})
717+
! CHECK: omp.teams
718+
! CHECK-NOT: if({{.*}})
719+
! CHECK-SAME: {
720+
! CHECK: omp.distribute
721+
! CHECK-NOT: if({{.*}})
722+
! CHECK-SAME: {
723+
! CHECK-NEXT: omp.simd
724+
! CHECK-NOT: if({{.*}})
725+
! CHECK-SAME: {
726+
! CHECK-NEXT: omp.loop_nest
727+
!$omp target teams distribute simd if(target: .true.)
728+
do i = 1, 10
729+
end do
730+
!$omp end target teams distribute simd
731+
732+
! CHECK: omp.target
733+
! CHECK-NOT: if({{.*}})
734+
! CHECK-SAME: {
735+
! CHECK: omp.teams
736+
! CHECK-SAME: if({{.*}})
737+
! CHECK: omp.distribute
738+
! CHECK-NOT: if({{.*}})
739+
! CHECK-SAME: {
740+
! CHECK-NEXT: omp.simd
741+
! CHECK-NOT: if({{.*}})
742+
! CHECK-SAME: {
743+
! CHECK-NEXT: omp.loop_nest
744+
!$omp target teams distribute simd if(teams: .true.)
745+
do i = 1, 10
746+
end do
747+
!$omp end target teams distribute simd
748+
749+
! CHECK: omp.target
750+
! CHECK-NOT: if({{.*}})
751+
! CHECK-SAME: {
752+
! CHECK: omp.teams
753+
! CHECK-NOT: if({{.*}})
754+
! CHECK-SAME: {
755+
! CHECK: omp.distribute
756+
! CHECK-NOT: if({{.*}})
757+
! CHECK-SAME: {
758+
! CHECK-NEXT: omp.simd
759+
! CHECK-SAME: if({{.*}})
760+
! CHECK-NEXT: omp.loop_nest
761+
!$omp target teams distribute simd if(simd: .true.)
762+
do i = 1, 10
763+
end do
764+
!$omp end target teams distribute simd
765+
626766
! ----------------------------------------------------------------------------
627767
! TARGET TEAMS
628768
! ----------------------------------------------------------------------------
@@ -726,6 +866,78 @@ program main
726866
end do
727867
!$omp end teams distribute
728868

869+
! ----------------------------------------------------------------------------
870+
! TEAMS DISTRIBUTE SIMD
871+
! ----------------------------------------------------------------------------
872+
! CHECK: omp.teams
873+
! CHECK-NOT: if({{.*}})
874+
! CHECK-SAME: {
875+
! CHECK: omp.distribute
876+
! CHECK-NOT: if({{.*}})
877+
! CHECK-SAME: {
878+
! CHECK-NEXT: omp.simd
879+
! CHECK-NOT: if({{.*}})
880+
! CHECK-SAME: {
881+
! CHECK-NEXT: omp.loop_nest
882+
!$omp teams distribute simd
883+
do i = 1, 10
884+
end do
885+
!$omp end teams distribute simd
886+
887+
! CHECK: omp.teams
888+
! CHECK-SAME: if({{.*}})
889+
! CHECK: omp.distribute
890+
! CHECK-NOT: if({{.*}})
891+
! CHECK-SAME: {
892+
! CHECK-NEXT: omp.simd
893+
! CHECK-SAME: if({{.*}})
894+
! CHECK-NEXT: omp.loop_nest
895+
!$omp teams distribute simd if(.true.)
896+
do i = 1, 10
897+
end do
898+
!$omp end teams distribute simd
899+
900+
! CHECK: omp.teams
901+
! CHECK-SAME: if({{.*}})
902+
! CHECK: omp.distribute
903+
! CHECK-NOT: if({{.*}})
904+
! CHECK-SAME: {
905+
! CHECK-NEXT: omp.simd
906+
! CHECK-SAME: if({{.*}})
907+
! CHECK-NEXT: omp.loop_nest
908+
!$omp teams distribute simd if(teams: .true.) if(simd: .false.)
909+
do i = 1, 10
910+
end do
911+
!$omp end teams distribute simd
912+
913+
! CHECK: omp.teams
914+
! CHECK-SAME: if({{.*}})
915+
! CHECK: omp.distribute
916+
! CHECK-NOT: if({{.*}})
917+
! CHECK-SAME: {
918+
! CHECK-NEXT: omp.simd
919+
! CHECK-NOT: if({{.*}})
920+
! CHECK-SAME: {
921+
! CHECK-NEXT: omp.loop_nest
922+
!$omp teams distribute simd if(teams: .true.)
923+
do i = 1, 10
924+
end do
925+
!$omp end teams distribute simd
926+
927+
! CHECK: omp.teams
928+
! CHECK-NOT: if({{.*}})
929+
! CHECK-SAME: {
930+
! CHECK: omp.distribute
931+
! CHECK-NOT: if({{.*}})
932+
! CHECK-SAME: {
933+
! CHECK-NEXT: omp.simd
934+
! CHECK-SAME: if({{.*}})
935+
! CHECK-NEXT: omp.loop_nest
936+
!$omp teams distribute simd if(simd: .true.)
937+
do i = 1, 10
938+
end do
939+
!$omp end teams distribute simd
940+
729941
! ----------------------------------------------------------------------------
730942
! TEAMS
731943
! ----------------------------------------------------------------------------

0 commit comments

Comments
 (0)