This is an archive of the discontinued LLVM Phabricator instance.

[Flang] Add Any and All intrinsics to simplify intrinsics pass
ClosedPublic

Authored by SBallantyne on Jan 31 2023, 5:52 AM.

Details

Summary

This patch provides a simplified version of the Any intrinsic as well as the All intrinsic
that can be used for inlining or simpiler use cases. These changes are targeting exchange2, and
provide a ~9% performance increase.

Diff Detail

Event Timeline

SBallantyne created this revision.Jan 31 2023, 5:52 AM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptJan 31 2023, 5:52 AM
SBallantyne requested review of this revision.Jan 31 2023, 5:52 AM
clementval added inline comments.
flang/lib/Optimizer/Transforms/SimplifyIntrinsics.cpp
272
319
342

Fix test/lower/array-derived.f90 fir test and minor changes to style

SBallantyne marked 3 inline comments as done.Jan 31 2023, 7:43 AM
flang/lib/Optimizer/Transforms/SimplifyIntrinsics.cpp
267

As discussed in private confersation, I think we should refactor this to not duplicate quite so much code. Whether we do that the way I suggested, or some other way is less important. Writing this here just so others know what's going on... :)

473

We could porbably have a genAnyOrAll that takes the genBodyOp and has all the common code.

807

Soemthing like "getDimCount returns a rank of zero" is more accurate. The "is set to 0" implies that the compiler gives that value somewhere else. (This applies to other places with same comment)

flang/test/Transforms/simplifyintrinsics.fir
1246

Although it doesn't really matter that much, it helps a little bit if you name the functions differently, rather than having several tests called _QPdimless - if one has to "grep" for the function that caused a problem, for example, it's a bit annoying when you get 5 hits for the same name, then you have to try to figure out which one is the one that you need to actually look at.

ktkachov removed a subscriber: ktkachov.
SBallantyne marked 2 inline comments as done.

Refactor to reduce code copy-paste, other minor changes

flang/lib/Optimizer/Transforms/SimplifyIntrinsics.cpp
473

I named the init function wrong as it returns 1 not 0 (i keep doing this!!). All the lambdas for genRuntimeAll are different than for genRuntimeAny so it would any have code outside as common - could be done but its a only few lines.

Leporacanthicus added inline comments.Feb 1 2023, 7:21 AM
flang/test/Transforms/simplifyintrinsics.fir
1246

So, whilst you have technically fulfilled the "they are not all called the same thing", my intention with the comment was that they should each have a unique name. This could be TestAny1x10Dimless, for example. Feel free to use any scheme that is reasonably consistent, and gives different names for each test-function.

SBallantyne marked an inline comment as done.

Minor change to fir test names to make them unique

This revision is now accepted and ready to land.Feb 3 2023, 6:17 AM
vzakhari requested changes to this revision.Feb 3 2023, 1:14 PM
vzakhari added inline comments.
flang/lib/Optimizer/Transforms/SimplifyIntrinsics.cpp
163

nit typo: of -> or

403

Please verify that your changes work for the following test case:

function test(a)
  logical*1 :: test
  logical*1 :: a(100)
  test = all(a) .or. any(a)
end function test
425

Please compare the logical value to zero instead of one.

This revision now requires changes to proceed.Feb 3 2023, 1:14 PM
SBallantyne marked 2 inline comments as done.

Change genBody for All to check for zero rather than one

SBallantyne added inline comments.Feb 6 2023, 2:42 AM
flang/lib/Optimizer/Transforms/SimplifyIntrinsics.cpp
403

This produces the same output when simplified as when it is not.

Fix failing tests by changing two lines

vzakhari added inline comments.Feb 6 2023, 8:31 AM
flang/lib/Optimizer/Transforms/SimplifyIntrinsics.cpp
403

I guess it may be producing the same result. My main concern is that we are treating the argument as logical*4 in the simplified implementation whereas the actual argument is logical*1. Couldn't that cause reading past the memory area allocated for the actual array?

Change to add proper support for different kinds of logical

SBallantyne added inline comments.Feb 7 2023, 6:35 AM
flang/lib/Optimizer/Transforms/SimplifyIntrinsics.cpp
403

While it doesn't seem like the program was reading past boundaries, it was getting confused sometimes and return the wrong answer for very specific sized arrays, so I've put in changes to properly deal with this.
I didn't initially put in changes for this because of the behaviour for Count where all logicals are converted to logical<4> before passing to the intrinsic, but this is not actually replicated for Any/All.

vzakhari added inline comments.Feb 7 2023, 4:47 PM
flang/lib/Optimizer/Transforms/SimplifyIntrinsics.cpp
743

typo: at

I am not a fan of this type casting and I do not see how it simplifies things, but I can live with it.

782

typo: at

930

Is this change intentional? Why is it needed?

SBallantyne marked an inline comment as done.

Fix Typo

flang/lib/Optimizer/Transforms/SimplifyIntrinsics.cpp
930

Yes, this change was to prevent _FortranAAllocatableInitDerived as well as other 'Allocatable' intrinsics from being caught in the pass.

vzakhari accepted this revision.Feb 8 2023, 6:45 PM

Thank you!

This revision is now accepted and ready to land.Feb 8 2023, 6:45 PM

FYI, the build is failing due to missing dependency: https://lab.llvm.org/buildbot/#/builders/181/builds/14278
I will merge the fix shortly.

FYI, the build is failing due to missing dependency: https://lab.llvm.org/buildbot/#/builders/181/builds/14278
I will merge the fix shortly.

Apologies. Thanks in advance for the fix.

The following code has a regression. Output from this code is now T; the output should be F.

integer z(0,5)        ! integer z(0) is ok
print*, any(z .ne. 1) ! should be false
end

F18 clause 16.9.13 ANY (MASK) or ANY (MASK, DIM), paragraph 5, Result Value Case(i) addresses the case of a zero-size argument:

The result of ANY (MASK) has the value true if any element of MASK is true and has the value false if no elements are true or if MASK has size zero.

The test is ok if the declaration of array z is changed to z(0). The code may need a more general check for the array size.

The following code has a regression. Output from this code is now T; the output should be F.

integer z(0,5)        ! integer z(0) is ok
print*, any(z .ne. 1) ! should be false
end

F18 clause 16.9.13 ANY (MASK) or ANY (MASK, DIM), paragraph 5, Result Value Case(i) addresses the case of a zero-size argument:

The result of ANY (MASK) has the value true if any element of MASK is true and has the value false if no elements are true or if MASK has size zero.

The test is ok if the declaration of array z is changed to z(0). The code may need a more general check for the array size.

Hi, I've put in a fix for this on this patch https://reviews.llvm.org/D143899, sorry about that.