This is an archive of the discontinued LLVM Phabricator instance.

[Flang][OpenMP] Add support for lastprivate clause for worksharing loop.
ClosedPublic

Authored by arnamoy10 on Jul 18 2022, 10:14 AM.

Details

Summary

This patch adds an initial support to the lastprivate clause for worksharing loop. The patch creates necessary control flow to guarantee the store of the value from the logical last iteration of the workshare loop.

Diff Detail

Event Timeline

arnamoy10 created this revision.Jul 18 2022, 10:14 AM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptJul 18 2022, 10:14 AM
arnamoy10 requested review of this revision.Jul 18 2022, 10:14 AM
kiranchandramohan requested changes to this revision.Jul 18 2022, 3:32 PM

The test along with this patch is failing. Please have a look.

flang/lib/Lower/OpenMP.cpp
108–116

Can we use FIR/SCF/CF operations here instead of llvm dialect operations? Did you face any issue if we put the store in an scf.if/fir.if operation close to the omp.yield?

159–190

Here and else where please use MLIR coding guidelines (camelBack syntax). https://mlir.llvm.org/getting_started/DeveloperGuide/#style-guide

This revision now requires changes to proceed.Jul 18 2022, 3:32 PM
arnamoy10 updated this revision to Diff 446290.Jul 20 2022, 3:46 PM
  1. Handling the control flow through scf.if
  2. Trying to fix the test case.
arnamoy10 updated this revision to Diff 446291.Jul 20 2022, 4:06 PM

camelBack-ization!

kiranchandramohan requested changes to this revision.Jul 21 2022, 5:45 AM

Patch looks good. The main concern remaining is the resetting and resetting of insertion points and passing state around that. If we can reduce this it will be great.

flang/lib/Lower/Bridge.cpp
515–530

Nit: remove auto.

516
547
flang/lib/Lower/OpenMP.cpp
89

Can we inline this now, that the code is not big?

116

Can you add a hard TODO for the else case?

117–121

I think the loop is not necessary. Will the following work?

firOpBuilder.setInsertionPoint(&op->region().back().back());

Also, I am assuming that you expect this to go before the Yield instruction, if so it would be good to and get the terminator explicitly and assert that it is the Yield terminator.

125

I guess, this will not work with collapse, Can you add a hard TODO for that?

457–470

Not for this patch: I think we need to clean this up a bit. Can you leave a TODO comment to clean up the setting/resetting of the insertion point?

flang/test/Lower/OpenMP/omp-parallel-lastprivate-clause-scalar.f90
2

Can you add a test with
-> a lastprivate and firstprivate?
-> multiple lastprivate clauses?

4

Nit: You can use the default CHECK prefix.
Nit: Add a driver test as well.

This revision now requires changes to proceed.Jul 21 2022, 5:45 AM

Patch looks good. The main concern remaining is the resetting and resetting of insertion points and passing state around that. If we can reduce this it will be great.

The setting and resetting can be cleaned up in a separate patch. Add a TODO for that.

Can you add a test with
-> a lastprivate and firstprivate?
-> multiple lastprivate clauses?

If these do not work then add appropriate TODOs.

Can you get this ready for submission today?

peixin added inline comments.Jul 25 2022, 5:37 AM
flang/lib/Lower/Bridge.cpp
497–498

Please add a FIXME: Generalize this function.. Ideally, this function should be generic so not to restricted only for OpenMP lastprivate lause.

flang/test/Lower/OpenMP/omp-parallel-lastprivate-clause-scalar.f90
2

Can you add a test with
-> a lastprivate and firstprivate?

You may use the code from D128595.

arnamoy10 updated this revision to Diff 447360.Jul 25 2022, 8:53 AM

Addressing all reviewers comments and adding TODOs and more test cases as per suggestion.

This looks mostly good. A few more comments to address before submission.

flang/include/flang/Lower/AbstractConverter.h
64

Nit: The style in this file seems to be to use the namespace along with the type. Can you use mlir along with Block and remove this using namespace

flang/lib/Lower/OpenMP.cpp
149

Nit: spelling: presence

152

Nit: I missed this before, please use the arithmetic dialect comparison. It is probably very similar to the LLVM one.

flang/test/Lower/OpenMP/omp-parallel-lastprivate-clause-scalar.f90
173

Nit: I meant a test where the list item variable is the same. Also add a check for the barrier between the two. If we finally remove the barrier for firstprivate, the barrier when both are present should exist. If there are issues, please add a TODO.

If a list item appears in both firstprivate and lastprivate clauses, the update required for the lastprivate clause occurs after all initializations for the firstprivate clause.

A list item may not appear in more than one clause on the same directive, except that it may be specified in both firstprivate and lastprivate clauses.

This revision is now accepted and ready to land.Jul 25 2022, 11:42 AM
arnamoy10 added inline comments.Jul 25 2022, 2:19 PM
flang/test/Lower/OpenMP/omp-parallel-lastprivate-clause-scalar.f90
173

I see, thanks for the suggestion. Currently same var being firstprivate and lastprivate has issues. I will submit a separate patch to fix the issue. Adding a TODO here.

arnamoy10 updated this revision to Diff 447473.Jul 25 2022, 2:30 PM

Addressing comments before landing

arnamoy10 updated this revision to Diff 447510.Jul 25 2022, 4:26 PM

fixing clang-format

GCC 9.3.0 is complaining:

flang/lib/Lower/OpenMP.cpp:66:41: error: parameter 'lastPrivBlock' set but not used [-Werror=unused-but-set-parameter]
   66 |                                  Block *lastPrivBlock = nullptr) {
      |                                  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~

Not sure why, in line 516 you will see a use.

shraiysh added inline comments.Jul 27 2022, 12:06 PM
flang/lib/Lower/OpenMP.cpp
66

The complain is from here. The if conditions on line 77 and 79 gets evaluated at compile time and so when T is not Lastprivate, lastPrivBlock isn't used. You can add maybe_unused to handle this, example here