[llvm-branch-commits] [flang] [mlir] [Flang][OpenMP] Add lowering support for DO SIMD (PR #97718)

2024-07-08 Thread Sergio Afonso via llvm-branch-commits

https://github.com/skatrak updated 
https://github.com/llvm/llvm-project/pull/97718

>From 69253a2e1d7a53f4cae907eed30a1c2bdf12c223 Mon Sep 17 00:00:00 2001
From: Sergio Afonso 
Date: Thu, 4 Jul 2024 12:56:43 +0100
Subject: [PATCH] [Flang][OpenMP] Add lowering support for DO SIMD

This patch adds support for lowering 'DO SIMD' constructs to MLIR. SIMD
information is now stored in an `omp.simd` loop wrapper, which is currently
ignored by the OpenMP dialect to LLVM IR translation stage.

The end result is that runtime behavior of compiled 'DO SIMD' constructs does
not change after this patch, so 'DO SIMD' still runs like 'DO' (i.e. SIMD width
= 1). However, all of the required information is now present in the resulting
MLIR representation.

To avoid confusion, the previous wsloop-simd.f90 lit test is renamed to
wsloop-schedule.f90 and a new wsloop-simd.f90 test is created to check the
addition of SIMD clauses to the `omp.simd` operation produced when a 'DO SIMD'
construct is lowered to MLIR.
---
 flang/lib/Lower/OpenMP/OpenMP.cpp | 51 +
 .../Lower/OpenMP/Todo/omp-do-simd-aligned.f90 | 16 
 .../Lower/OpenMP/Todo/omp-do-simd-linear.f90  |  2 +-
 .../Lower/OpenMP/Todo/omp-do-simd-safelen.f90 | 14 
 .../Lower/OpenMP/Todo/omp-do-simd-simdlen.f90 | 14 
 flang/test/Lower/OpenMP/if-clause.f90 | 31 
 flang/test/Lower/OpenMP/loop-compound.f90 |  3 +
 flang/test/Lower/OpenMP/wsloop-schedule.f90   | 37 ++
 flang/test/Lower/OpenMP/wsloop-simd.f90   | 74 +++
 .../OpenMP/OpenMPToLLVMIRTranslation.cpp  |  3 +
 10 files changed, 155 insertions(+), 90 deletions(-)
 delete mode 100644 flang/test/Lower/OpenMP/Todo/omp-do-simd-aligned.f90
 delete mode 100644 flang/test/Lower/OpenMP/Todo/omp-do-simd-safelen.f90
 delete mode 100644 flang/test/Lower/OpenMP/Todo/omp-do-simd-simdlen.f90
 create mode 100644 flang/test/Lower/OpenMP/wsloop-schedule.f90

diff --git a/flang/lib/Lower/OpenMP/OpenMP.cpp 
b/flang/lib/Lower/OpenMP/OpenMP.cpp
index 3dac44ac63691..b1bb4c11f86dd 100644
--- a/flang/lib/Lower/OpenMP/OpenMP.cpp
+++ b/flang/lib/Lower/OpenMP/OpenMP.cpp
@@ -1986,19 +1986,44 @@ static void genCompositeDoSimd(lower::AbstractConverter 
,
const ConstructQueue ,
ConstructQueue::iterator item,
DataSharingProcessor ) {
-  ClauseProcessor cp(converter, semaCtx, item->clauses);
-  cp.processTODO(loc,
-   llvm::omp::OMPD_do_simd);
-  // TODO: Add support for vectorization - add vectorization hints inside loop
-  // body.
-  // OpenMP standard does not specify the length of vector instructions.
-  // Currently we safely assume that for !$omp do simd pragma the SIMD length
-  // is equal to 1 (i.e. we generate standard workshare loop).
-  // When support for vectorization is enabled, then we need to add handling of
-  // if clause. Currently if clause can be skipped because we always assume
-  // SIMD length = 1.
-  genStandaloneDo(converter, symTable, semaCtx, eval, loc, queue, item, dsp);
+  lower::StatementContext stmtCtx;
+
+  // Clause processing.
+  mlir::omp::WsloopClauseOps wsloopClauseOps;
+  llvm::SmallVector wsloopReductionSyms;
+  llvm::SmallVector wsloopReductionTypes;
+  genWsloopClauses(converter, semaCtx, stmtCtx, item->clauses, loc,
+   wsloopClauseOps, wsloopReductionTypes, wsloopReductionSyms);
+
+  mlir::omp::SimdClauseOps simdClauseOps;
+  genSimdClauses(converter, semaCtx, item->clauses, loc, simdClauseOps);
+
+  mlir::omp::LoopNestClauseOps loopNestClauseOps;
+  llvm::SmallVector iv;
+  genLoopNestClauses(converter, semaCtx, eval, item->clauses, loc,
+ loopNestClauseOps, iv);
+
+  // Operation creation.
+  // TODO: Add private variables to entry block arguments.
+  auto wsloopOp = genWrapperOp(
+  converter, loc, wsloopClauseOps, wsloopReductionTypes);
+
+  // TODO: Populate entry block arguments with reduction and private variables.
+  auto simdOp = genWrapperOp(converter, loc, simdClauseOps,
+/*blockArgTypes=*/{});
+
+  // Construct wrapper entry block list and associated symbols. It is important
+  // that the symbol and block argument order match, so that the symbol-value
+  // bindings created are correct.
+  // TODO: Add omp.wsloop private and omp.simd private and reduction args.
+  auto wrapperArgs = llvm::to_vector(llvm::concat(
+  wsloopOp.getRegion().getArguments(), simdOp.getRegion().getArguments()));
+
+  assert(wsloopReductionSyms.size() == wrapperArgs.size() &&
+ "Number of symbols and wrapper block arguments must match");
+  genLoopNestOp(converter, symTable, semaCtx, eval, loc, queue, item,
+loopNestClauseOps, iv, wsloopReductionSyms, wrapperArgs,
+llvm::omp::Directive::OMPD_do_simd, dsp);
 }
 
 static void genCompositeTaskloopSimd(
diff --git 

[llvm-branch-commits] [flang] [mlir] [Flang][OpenMP] Add lowering support for DO SIMD (PR #97718)

2024-07-08 Thread Tom Eccles via llvm-branch-commits

https://github.com/tblah approved this pull request.

LGTM

https://github.com/llvm/llvm-project/pull/97718
___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [flang] [mlir] [Flang][OpenMP] Add lowering support for DO SIMD (PR #97718)

2024-07-08 Thread Michael Klemm via llvm-branch-commits

https://github.com/mjklemm approved this pull request.

LGTM

https://github.com/llvm/llvm-project/pull/97718
___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [flang] [mlir] [Flang][OpenMP] Add lowering support for DO SIMD (PR #97718)

2024-07-05 Thread Sergio Afonso via llvm-branch-commits

https://github.com/skatrak updated 
https://github.com/llvm/llvm-project/pull/97718

>From 27490fbac8e08ed6c11437ef28efd8298fb120b1 Mon Sep 17 00:00:00 2001
From: Sergio Afonso 
Date: Thu, 4 Jul 2024 12:56:43 +0100
Subject: [PATCH 1/3] [Flang][OpenMP] Add lowering support for DO SIMD

This patch adds support for lowering 'DO SIMD' constructs to MLIR. SIMD
information is now stored in an `omp.simd` loop wrapper, which is currently
ignored by the OpenMP dialect to LLVM IR translation stage.

The end result is that runtime behavior of compiled 'DO SIMD' constructs does
not change after this patch, so 'DO SIMD' still runs like 'DO' (i.e. SIMD width
= 1). However, all of the required information is now present in the resulting
MLIR representation.

To avoid confusion, the previous wsloop-simd.f90 lit test is renamed to
wsloop-schedule.f90 and a new wsloop-simd.f90 test is created to check the
addition of SIMD clauses to the `omp.simd` operation produced when a 'DO SIMD'
construct is lowered to MLIR.
---
 flang/lib/Lower/OpenMP/OpenMP.cpp | 49 
 .../Lower/OpenMP/Todo/omp-do-simd-aligned.f90 | 16 
 .../Lower/OpenMP/Todo/omp-do-simd-linear.f90  |  2 +-
 .../Lower/OpenMP/Todo/omp-do-simd-safelen.f90 | 14 
 .../Lower/OpenMP/Todo/omp-do-simd-simdlen.f90 | 14 
 flang/test/Lower/OpenMP/if-clause.f90 | 31 
 flang/test/Lower/OpenMP/loop-compound.f90 |  3 +
 flang/test/Lower/OpenMP/wsloop-schedule.f90   | 37 ++
 flang/test/Lower/OpenMP/wsloop-simd.f90   | 74 +++
 .../OpenMP/OpenMPToLLVMIRTranslation.cpp  |  3 +
 10 files changed, 153 insertions(+), 90 deletions(-)
 delete mode 100644 flang/test/Lower/OpenMP/Todo/omp-do-simd-aligned.f90
 delete mode 100644 flang/test/Lower/OpenMP/Todo/omp-do-simd-safelen.f90
 delete mode 100644 flang/test/Lower/OpenMP/Todo/omp-do-simd-simdlen.f90
 create mode 100644 flang/test/Lower/OpenMP/wsloop-schedule.f90

diff --git a/flang/lib/Lower/OpenMP/OpenMP.cpp 
b/flang/lib/Lower/OpenMP/OpenMP.cpp
index f29d3517bb39b..67bfdb150a7f2 100644
--- a/flang/lib/Lower/OpenMP/OpenMP.cpp
+++ b/flang/lib/Lower/OpenMP/OpenMP.cpp
@@ -1986,19 +1986,42 @@ static void genCompositeDoSimd(lower::AbstractConverter 
,
const ConstructQueue ,
ConstructQueue::iterator item,
DataSharingProcessor ) {
-  ClauseProcessor cp(converter, semaCtx, item->clauses);
-  cp.processTODO(loc,
-   llvm::omp::OMPD_do_simd);
-  // TODO: Add support for vectorization - add vectorization hints inside loop
-  // body.
-  // OpenMP standard does not specify the length of vector instructions.
-  // Currently we safely assume that for !$omp do simd pragma the SIMD length
-  // is equal to 1 (i.e. we generate standard workshare loop).
-  // When support for vectorization is enabled, then we need to add handling of
-  // if clause. Currently if clause can be skipped because we always assume
-  // SIMD length = 1.
-  genStandaloneDo(converter, symTable, semaCtx, eval, loc, queue, item, dsp);
+  lower::StatementContext stmtCtx;
+
+  // Clause processing.
+  mlir::omp::WsloopClauseOps wsloopClauseOps;
+  llvm::SmallVector wsloopReductionSyms;
+  llvm::SmallVector wsloopReductionTypes;
+  genWsloopClauses(converter, semaCtx, stmtCtx, item->clauses, loc,
+   wsloopClauseOps, wsloopReductionTypes, wsloopReductionSyms);
+
+  mlir::omp::SimdClauseOps simdClauseOps;
+  genSimdClauses(converter, semaCtx, item->clauses, loc, simdClauseOps);
+
+  mlir::omp::LoopNestClauseOps loopNestClauseOps;
+  llvm::SmallVector iv;
+  genLoopNestClauses(converter, semaCtx, eval, item->clauses, loc,
+ loopNestClauseOps, iv);
+
+  // Operation creation.
+  auto wsloopOp =
+  genWsloopWrapperOp(converter, semaCtx, eval, loc, wsloopClauseOps,
+ wsloopReductionSyms, wsloopReductionTypes);
+
+  auto simdOp = genSimdWrapperOp(converter, semaCtx, eval, loc, simdClauseOps);
+
+  // Construct wrapper entry block list and associated symbols. It is important
+  // that the symbol and block argument order match, so that the symbol-value
+  // bindings created are correct.
+  // TODO: Add omp.wsloop private and omp.simd private and reduction args.
+  auto wrapperArgs = llvm::to_vector(llvm::concat(
+  wsloopOp.getRegion().getArguments(), simdOp.getRegion().getArguments()));
+
+  assert(wsloopReductionSyms.size() == wrapperArgs.size() &&
+ "Number of symbols and wrapper block arguments must match");
+  genLoopNestOp(converter, symTable, semaCtx, eval, loc, queue, item,
+loopNestClauseOps, iv, wsloopReductionSyms, wrapperArgs,
+llvm::omp::Directive::OMPD_do_simd, dsp);
 }
 
 static void genCompositeTaskloopSimd(
diff --git a/flang/test/Lower/OpenMP/Todo/omp-do-simd-aligned.f90 
b/flang/test/Lower/OpenMP/Todo/omp-do-simd-aligned.f90
deleted file 

[llvm-branch-commits] [flang] [mlir] [Flang][OpenMP] Add lowering support for DO SIMD (PR #97718)

2024-07-05 Thread Sergio Afonso via llvm-branch-commits

https://github.com/skatrak updated 
https://github.com/llvm/llvm-project/pull/97718

>From 27490fbac8e08ed6c11437ef28efd8298fb120b1 Mon Sep 17 00:00:00 2001
From: Sergio Afonso 
Date: Thu, 4 Jul 2024 12:56:43 +0100
Subject: [PATCH 1/2] [Flang][OpenMP] Add lowering support for DO SIMD

This patch adds support for lowering 'DO SIMD' constructs to MLIR. SIMD
information is now stored in an `omp.simd` loop wrapper, which is currently
ignored by the OpenMP dialect to LLVM IR translation stage.

The end result is that runtime behavior of compiled 'DO SIMD' constructs does
not change after this patch, so 'DO SIMD' still runs like 'DO' (i.e. SIMD width
= 1). However, all of the required information is now present in the resulting
MLIR representation.

To avoid confusion, the previous wsloop-simd.f90 lit test is renamed to
wsloop-schedule.f90 and a new wsloop-simd.f90 test is created to check the
addition of SIMD clauses to the `omp.simd` operation produced when a 'DO SIMD'
construct is lowered to MLIR.
---
 flang/lib/Lower/OpenMP/OpenMP.cpp | 49 
 .../Lower/OpenMP/Todo/omp-do-simd-aligned.f90 | 16 
 .../Lower/OpenMP/Todo/omp-do-simd-linear.f90  |  2 +-
 .../Lower/OpenMP/Todo/omp-do-simd-safelen.f90 | 14 
 .../Lower/OpenMP/Todo/omp-do-simd-simdlen.f90 | 14 
 flang/test/Lower/OpenMP/if-clause.f90 | 31 
 flang/test/Lower/OpenMP/loop-compound.f90 |  3 +
 flang/test/Lower/OpenMP/wsloop-schedule.f90   | 37 ++
 flang/test/Lower/OpenMP/wsloop-simd.f90   | 74 +++
 .../OpenMP/OpenMPToLLVMIRTranslation.cpp  |  3 +
 10 files changed, 153 insertions(+), 90 deletions(-)
 delete mode 100644 flang/test/Lower/OpenMP/Todo/omp-do-simd-aligned.f90
 delete mode 100644 flang/test/Lower/OpenMP/Todo/omp-do-simd-safelen.f90
 delete mode 100644 flang/test/Lower/OpenMP/Todo/omp-do-simd-simdlen.f90
 create mode 100644 flang/test/Lower/OpenMP/wsloop-schedule.f90

diff --git a/flang/lib/Lower/OpenMP/OpenMP.cpp 
b/flang/lib/Lower/OpenMP/OpenMP.cpp
index f29d3517bb39bc..67bfdb150a7f22 100644
--- a/flang/lib/Lower/OpenMP/OpenMP.cpp
+++ b/flang/lib/Lower/OpenMP/OpenMP.cpp
@@ -1986,19 +1986,42 @@ static void genCompositeDoSimd(lower::AbstractConverter 
,
const ConstructQueue ,
ConstructQueue::iterator item,
DataSharingProcessor ) {
-  ClauseProcessor cp(converter, semaCtx, item->clauses);
-  cp.processTODO(loc,
-   llvm::omp::OMPD_do_simd);
-  // TODO: Add support for vectorization - add vectorization hints inside loop
-  // body.
-  // OpenMP standard does not specify the length of vector instructions.
-  // Currently we safely assume that for !$omp do simd pragma the SIMD length
-  // is equal to 1 (i.e. we generate standard workshare loop).
-  // When support for vectorization is enabled, then we need to add handling of
-  // if clause. Currently if clause can be skipped because we always assume
-  // SIMD length = 1.
-  genStandaloneDo(converter, symTable, semaCtx, eval, loc, queue, item, dsp);
+  lower::StatementContext stmtCtx;
+
+  // Clause processing.
+  mlir::omp::WsloopClauseOps wsloopClauseOps;
+  llvm::SmallVector wsloopReductionSyms;
+  llvm::SmallVector wsloopReductionTypes;
+  genWsloopClauses(converter, semaCtx, stmtCtx, item->clauses, loc,
+   wsloopClauseOps, wsloopReductionTypes, wsloopReductionSyms);
+
+  mlir::omp::SimdClauseOps simdClauseOps;
+  genSimdClauses(converter, semaCtx, item->clauses, loc, simdClauseOps);
+
+  mlir::omp::LoopNestClauseOps loopNestClauseOps;
+  llvm::SmallVector iv;
+  genLoopNestClauses(converter, semaCtx, eval, item->clauses, loc,
+ loopNestClauseOps, iv);
+
+  // Operation creation.
+  auto wsloopOp =
+  genWsloopWrapperOp(converter, semaCtx, eval, loc, wsloopClauseOps,
+ wsloopReductionSyms, wsloopReductionTypes);
+
+  auto simdOp = genSimdWrapperOp(converter, semaCtx, eval, loc, simdClauseOps);
+
+  // Construct wrapper entry block list and associated symbols. It is important
+  // that the symbol and block argument order match, so that the symbol-value
+  // bindings created are correct.
+  // TODO: Add omp.wsloop private and omp.simd private and reduction args.
+  auto wrapperArgs = llvm::to_vector(llvm::concat(
+  wsloopOp.getRegion().getArguments(), simdOp.getRegion().getArguments()));
+
+  assert(wsloopReductionSyms.size() == wrapperArgs.size() &&
+ "Number of symbols and wrapper block arguments must match");
+  genLoopNestOp(converter, symTable, semaCtx, eval, loc, queue, item,
+loopNestClauseOps, iv, wsloopReductionSyms, wrapperArgs,
+llvm::omp::Directive::OMPD_do_simd, dsp);
 }
 
 static void genCompositeTaskloopSimd(
diff --git a/flang/test/Lower/OpenMP/Todo/omp-do-simd-aligned.f90 
b/flang/test/Lower/OpenMP/Todo/omp-do-simd-aligned.f90
deleted file 

[llvm-branch-commits] [flang] [mlir] [Flang][OpenMP] Add lowering support for DO SIMD (PR #97718)

2024-07-04 Thread via llvm-branch-commits

llvmbot wrote:



@llvm/pr-subscribers-flang-openmp

@llvm/pr-subscribers-mlir-openmp

Author: Sergio Afonso (skatrak)


Changes

This patch adds support for lowering 'DO SIMD' constructs to MLIR. SIMD 
information is now stored in an `omp.simd` loop wrapper, which is currently 
ignored by the OpenMP dialect to LLVM IR translation stage.

The end result is that runtime behavior of compiled 'DO SIMD' constructs does 
not change after this patch, so 'DO SIMD' still runs like 'DO' (i.e. SIMD width 
= 1). However, all of the required information is now present in the resulting 
MLIR representation.

To avoid confusion, the previous wsloop-simd.f90 lit test is renamed to 
wsloop-schedule.f90 and a new wsloop-simd.f90 test is created to check the 
addition of SIMD clauses to the `omp.simd` operation produced when a 'DO SIMD' 
construct is lowered to MLIR.

---
Full diff: https://github.com/llvm/llvm-project/pull/97718.diff


10 Files Affected:

- (modified) flang/lib/Lower/OpenMP/OpenMP.cpp (+36-13) 
- (removed) flang/test/Lower/OpenMP/Todo/omp-do-simd-aligned.f90 (-16) 
- (modified) flang/test/Lower/OpenMP/Todo/omp-do-simd-linear.f90 (+1-1) 
- (removed) flang/test/Lower/OpenMP/Todo/omp-do-simd-safelen.f90 (-14) 
- (removed) flang/test/Lower/OpenMP/Todo/omp-do-simd-simdlen.f90 (-14) 
- (modified) flang/test/Lower/OpenMP/if-clause.f90 (+31) 
- (modified) flang/test/Lower/OpenMP/loop-compound.f90 (+3) 
- (added) flang/test/Lower/OpenMP/wsloop-schedule.f90 (+37) 
- (modified) flang/test/Lower/OpenMP/wsloop-simd.f90 (+42-32) 
- (modified) 
mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp (+3) 


``diff
diff --git a/flang/lib/Lower/OpenMP/OpenMP.cpp 
b/flang/lib/Lower/OpenMP/OpenMP.cpp
index 1830e31349cfb..9c23888a87173 100644
--- a/flang/lib/Lower/OpenMP/OpenMP.cpp
+++ b/flang/lib/Lower/OpenMP/OpenMP.cpp
@@ -2060,19 +2060,42 @@ static void genCompositeDoSimd(lower::AbstractConverter 
,
const ConstructQueue ,
ConstructQueue::iterator item,
DataSharingProcessor ) {
-  ClauseProcessor cp(converter, semaCtx, item->clauses);
-  cp.processTODO(loc,
-   llvm::omp::OMPD_do_simd);
-  // TODO: Add support for vectorization - add vectorization hints inside loop
-  // body.
-  // OpenMP standard does not specify the length of vector instructions.
-  // Currently we safely assume that for !$omp do simd pragma the SIMD length
-  // is equal to 1 (i.e. we generate standard workshare loop).
-  // When support for vectorization is enabled, then we need to add handling of
-  // if clause. Currently if clause can be skipped because we always assume
-  // SIMD length = 1.
-  genStandaloneDo(converter, symTable, semaCtx, eval, loc, queue, item, dsp);
+  lower::StatementContext stmtCtx;
+
+  // Clause processing.
+  mlir::omp::WsloopClauseOps wsloopClauseOps;
+  llvm::SmallVector wsloopReductionSyms;
+  llvm::SmallVector wsloopReductionTypes;
+  genWsloopClauses(converter, semaCtx, stmtCtx, item->clauses, loc,
+   wsloopClauseOps, wsloopReductionTypes, wsloopReductionSyms);
+
+  mlir::omp::SimdClauseOps simdClauseOps;
+  genSimdClauses(converter, semaCtx, item->clauses, loc, simdClauseOps);
+
+  mlir::omp::LoopNestClauseOps loopNestClauseOps;
+  llvm::SmallVector iv;
+  genLoopNestClauses(converter, semaCtx, eval, item->clauses, loc,
+ loopNestClauseOps, iv);
+
+  // Operation creation.
+  auto wsloopOp =
+  genWsloopWrapperOp(converter, semaCtx, eval, loc, wsloopClauseOps,
+ wsloopReductionSyms, wsloopReductionTypes);
+
+  auto simdOp = genSimdWrapperOp(converter, semaCtx, eval, loc, simdClauseOps);
+
+  // Construct wrapper entry block list and associated symbols. It is important
+  // that the symbol and block argument order match, so that the symbol-value
+  // bindings created are correct.
+  // TODO: Add omp.wsloop private and omp.simd private and reduction args.
+  auto wrapperArgs = llvm::to_vector(llvm::concat(
+  wsloopOp.getRegion().getArguments(), simdOp.getRegion().getArguments()));
+
+  assert(wsloopReductionSyms.size() == wrapperArgs.size() &&
+ "Number of symbols and wrapper block arguments must match");
+  genLoopNestOp(converter, symTable, semaCtx, eval, loc, queue, item,
+loopNestClauseOps, iv, wsloopReductionSyms, wrapperArgs,
+llvm::omp::Directive::OMPD_do_simd, dsp);
 }
 
 static void genCompositeTaskloopSimd(
diff --git a/flang/test/Lower/OpenMP/Todo/omp-do-simd-aligned.f90 
b/flang/test/Lower/OpenMP/Todo/omp-do-simd-aligned.f90
deleted file mode 100644
index b62c54182442a..0
--- a/flang/test/Lower/OpenMP/Todo/omp-do-simd-aligned.f90
+++ /dev/null
@@ -1,16 +0,0 @@
-! This test checks lowering of OpenMP do simd aligned() pragma
-
-! RUN: %not_todo_cmd bbc -emit-fir -fopenmp -o - %s 2>&1 | FileCheck %s
-! RUN: %not_todo_cmd 

[llvm-branch-commits] [flang] [mlir] [Flang][OpenMP] Add lowering support for DO SIMD (PR #97718)

2024-07-04 Thread Sergio Afonso via llvm-branch-commits

https://github.com/skatrak created 
https://github.com/llvm/llvm-project/pull/97718

This patch adds support for lowering 'DO SIMD' constructs to MLIR. SIMD 
information is now stored in an `omp.simd` loop wrapper, which is currently 
ignored by the OpenMP dialect to LLVM IR translation stage.

The end result is that runtime behavior of compiled 'DO SIMD' constructs does 
not change after this patch, so 'DO SIMD' still runs like 'DO' (i.e. SIMD width 
= 1). However, all of the required information is now present in the resulting 
MLIR representation.

To avoid confusion, the previous wsloop-simd.f90 lit test is renamed to 
wsloop-schedule.f90 and a new wsloop-simd.f90 test is created to check the 
addition of SIMD clauses to the `omp.simd` operation produced when a 'DO SIMD' 
construct is lowered to MLIR.

>From cca42d31ed43dee5521605f50f3fed380d034f70 Mon Sep 17 00:00:00 2001
From: Sergio Afonso 
Date: Thu, 4 Jul 2024 12:56:43 +0100
Subject: [PATCH] [Flang][OpenMP] Add lowering support for DO SIMD

This patch adds support for lowering 'DO SIMD' constructs to MLIR. SIMD
information is now stored in an `omp.simd` loop wrapper, which is currently
ignored by the OpenMP dialect to LLVM IR translation stage.

The end result is that runtime behavior of compiled 'DO SIMD' constructs does
not change after this patch, so 'DO SIMD' still runs like 'DO' (i.e. SIMD width
= 1). However, all of the required information is now present in the resulting
MLIR representation.

To avoid confusion, the previous wsloop-simd.f90 lit test is renamed to
wsloop-schedule.f90 and a new wsloop-simd.f90 test is created to check the
addition of SIMD clauses to the `omp.simd` operation produced when a 'DO SIMD'
construct is lowered to MLIR.
---
 flang/lib/Lower/OpenMP/OpenMP.cpp | 49 
 .../Lower/OpenMP/Todo/omp-do-simd-aligned.f90 | 16 
 .../Lower/OpenMP/Todo/omp-do-simd-linear.f90  |  2 +-
 .../Lower/OpenMP/Todo/omp-do-simd-safelen.f90 | 14 
 .../Lower/OpenMP/Todo/omp-do-simd-simdlen.f90 | 14 
 flang/test/Lower/OpenMP/if-clause.f90 | 31 
 flang/test/Lower/OpenMP/loop-compound.f90 |  3 +
 flang/test/Lower/OpenMP/wsloop-schedule.f90   | 37 ++
 flang/test/Lower/OpenMP/wsloop-simd.f90   | 74 +++
 .../OpenMP/OpenMPToLLVMIRTranslation.cpp  |  3 +
 10 files changed, 153 insertions(+), 90 deletions(-)
 delete mode 100644 flang/test/Lower/OpenMP/Todo/omp-do-simd-aligned.f90
 delete mode 100644 flang/test/Lower/OpenMP/Todo/omp-do-simd-safelen.f90
 delete mode 100644 flang/test/Lower/OpenMP/Todo/omp-do-simd-simdlen.f90
 create mode 100644 flang/test/Lower/OpenMP/wsloop-schedule.f90

diff --git a/flang/lib/Lower/OpenMP/OpenMP.cpp 
b/flang/lib/Lower/OpenMP/OpenMP.cpp
index 1830e31349cfb..9c23888a87173 100644
--- a/flang/lib/Lower/OpenMP/OpenMP.cpp
+++ b/flang/lib/Lower/OpenMP/OpenMP.cpp
@@ -2060,19 +2060,42 @@ static void genCompositeDoSimd(lower::AbstractConverter 
,
const ConstructQueue ,
ConstructQueue::iterator item,
DataSharingProcessor ) {
-  ClauseProcessor cp(converter, semaCtx, item->clauses);
-  cp.processTODO(loc,
-   llvm::omp::OMPD_do_simd);
-  // TODO: Add support for vectorization - add vectorization hints inside loop
-  // body.
-  // OpenMP standard does not specify the length of vector instructions.
-  // Currently we safely assume that for !$omp do simd pragma the SIMD length
-  // is equal to 1 (i.e. we generate standard workshare loop).
-  // When support for vectorization is enabled, then we need to add handling of
-  // if clause. Currently if clause can be skipped because we always assume
-  // SIMD length = 1.
-  genStandaloneDo(converter, symTable, semaCtx, eval, loc, queue, item, dsp);
+  lower::StatementContext stmtCtx;
+
+  // Clause processing.
+  mlir::omp::WsloopClauseOps wsloopClauseOps;
+  llvm::SmallVector wsloopReductionSyms;
+  llvm::SmallVector wsloopReductionTypes;
+  genWsloopClauses(converter, semaCtx, stmtCtx, item->clauses, loc,
+   wsloopClauseOps, wsloopReductionTypes, wsloopReductionSyms);
+
+  mlir::omp::SimdClauseOps simdClauseOps;
+  genSimdClauses(converter, semaCtx, item->clauses, loc, simdClauseOps);
+
+  mlir::omp::LoopNestClauseOps loopNestClauseOps;
+  llvm::SmallVector iv;
+  genLoopNestClauses(converter, semaCtx, eval, item->clauses, loc,
+ loopNestClauseOps, iv);
+
+  // Operation creation.
+  auto wsloopOp =
+  genWsloopWrapperOp(converter, semaCtx, eval, loc, wsloopClauseOps,
+ wsloopReductionSyms, wsloopReductionTypes);
+
+  auto simdOp = genSimdWrapperOp(converter, semaCtx, eval, loc, simdClauseOps);
+
+  // Construct wrapper entry block list and associated symbols. It is important
+  // that the symbol and block argument order match, so that the symbol-value
+  // bindings created are correct.
+