This is an archive of the discontinued LLVM Phabricator instance.

[Flang][OpenMP] Add support for integer multiplication reduction in worksharing-loop
ClosedPublic

Authored by DylanFleming-arm on Jul 29 2022, 5:53 AM.

Details

Summary

Adds support for reduction of multiplcation
by extending OpenMP.cpp::genOpenMPReduction()
and altering the identity constant emitted in
OpenMP.cpp::createReductionDelc()

This patch builds D130077 and as such,
only supports reductions for interger types in
worksharping loops.

Diff Detail

Event Timeline

Herald added projects: Restricted Project, Restricted Project. · View Herald Transcript
DylanFleming-arm requested review of this revision.Jul 29 2022, 5:53 AM
tschuett added inline comments.
flang/lib/Lower/OpenMP.cpp
811

Could this be get_identity(name)?

824

You misuse the identity to find the operator?

Could this be get_op(name)?

clementval added inline comments.
flang/lib/Lower/OpenMP.cpp
1687–1708

This part is copy/pasted from the code above. Please make it a function to avoid code duplication.

I moved the code for getting the identity to a seperate function as suggested.

However, I haven't created a function for creating the operations because it seems a little strange to me to use the StringRef (which was created from the operatior) to then find the operatior.
I do agree using the identiy was messy, so I've instead changed the createReductionDecl() to take in the operator, as that can then be used instead.
I am of course open to suggestions for changing this again, if anyone has problems with my current approch.

I've added a templated function, updateReduction(), in order to deal with the code duplication issue raised and updated genOpenMPReduction() to call it.

peixin added inline comments.Jul 29 2022, 6:40 PM
flang/lib/Lower/OpenMP.cpp
814

Can this be extracted out as to the function as getReductionInitValue(loc, type, name)?

tschuett added inline comments.Jul 30 2022, 12:18 PM
flang/lib/Lower/OpenMP.cpp
786

For consistency, you could add a statichere.

Created getReductionInitValue(), and made getOperationIdentity static

Thanks for working on this @DylanFleming-arm , some comments inline. I've not had a chance to scan the tests yet - will try later :)

flang/lib/Lower/OpenMP.cpp
786–792

It wasn't immediately obvious to me what this method is for (I assumed that you meant "identity" as somebody's name). I would actually fold this method into getReductionInitValue (that's the only that this is currently needed).

1707

The name of this method is not accurate - it only updates the reduction conditionally. And the condition verification is split across updateReduction and genOpenMPReduction. This comment above is key:

// This implementation finds the chain : load reduction var -> reduction_operation -> store reduction var and replaces it with the reduction operation.

It would make much more sense to check for a chain first (in genOpenMPReduction, perhaps in a dedicated method to make the implementation cleaner) and only once that condition is satisfied, have this method to update things unconditionally. Could you try that?

1708

symVal is a rather enigmatic name. IIUC, this is an accumulator and I would use that instead as a more descriptive name.

It would make much more sense to check for a chain first (in genOpenMPReduction, perhaps in a dedicated method to make the implementation cleaner) and only once that condition is satisfied, have this method to update things unconditionally. Could you try that?

I think this makes a lot of sense, however it's not directly related to adding multiplication support, so I've implemented these changes in a separate patch instead here: D131161

I'll rebase this patch on top of it shortly!

"reduction-multiply.f90" is inconsistent with "wsloop-reduction-int.f90". I would rename this files as "wsloop-reduction-int-mul.f90" and "wsloop-reduction-int-add.f90", respectively.

flang/lib/Lower/OpenMP.cpp
786–792

Can you either fold this method or add a comment that explains what "identity" means in this context.

tschuett added a comment.EditedAug 4 2022, 1:04 PM

I like named functions and named constants and values. Idk whether folding the two functions and adding comments or having two functions is better in terms of understanding the intent.

1 * 5 = 5
0 + 5 = 5

So 1 or 0 is the identity value. Maybe getIdentityValue + some doxygen is easier to understand.

Added comment to getOperationIdentity() to explain it's usage.
Renamed "name" to "reductionOpName" for clarity.

awarzynski accepted this revision.Aug 8 2022, 8:33 AM

LGTM

From what I can see, you've addressed all comments from all reviewers, thanks! But just in case I missed something, please wait till tomorrow before merging this.

This revision is now accepted and ready to land.Aug 8 2022, 8:33 AM
peixin added a comment.Aug 8 2022, 6:04 PM

clang-format error in x64 debian.

I've re-ran clang-format on OpenMP.cpp, hopefully that fixes the problem!

flang/lib/Lower/OpenMP.cpp