This is an archive of the discontinued LLVM Phabricator instance.

[flang] Fix LBOUND() folding for constant arrays
ClosedPublic

Authored by FruitClover on Apr 6 2022, 10:28 AM.

Details

Summary

Previously constant folding uses 'dim' without checks which leads to ICE if we
do not have DIM= parameter. And for inputs without DIM= we need to form an
array of rank size with computed bounds instead of single value.

Add additional PackageConstant function to simplify 'if (dim)' handling since we
need to distinguish between scalar initialization in case of DIM= argument and
rank=1 array.

Also add a few more tests with 'parameter' type to verify folding for constant
arrays.

Diff Detail

Event Timeline

FruitClover created this revision.Apr 6 2022, 10:28 AM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptApr 6 2022, 10:28 AM
FruitClover published this revision for review.Apr 6 2022, 10:29 AM
klausler added inline comments.Apr 6 2022, 11:54 AM
flang/lib/Evaluate/fold-integer.cpp
100

In the functional style used in the f18 frontend we prefer to use 'else' when there's an 'if' statement and following code that both end in returns.

The implementation might be clearer if GetConstantArrayLboundHelper were changed to return ConstantSubscripts (plural); the case with a DIM= argument could then extract its result from them, and the case without would simply return all of them.

Update GetConstantArrayLboundHelper impl., add PackageConstant variant.

FruitClover edited the summary of this revision. (Show Details)Apr 8 2022, 6:08 PM
FruitClover added inline comments.
flang/lib/Evaluate/fold-integer.cpp
100

Thanks, I've tried to switch to ConstantSubscripts variant, don't know if it's optimal solution with PackageConstant usage (since I'm not very confident in overall types structure yet).

UBOUND() has similar problem with constant arrays, created patch based on current changes (will rebase & update after current review) - https://reviews.llvm.org/D123520

Thanks for working on a fix. I think there is a second issue that would be worth fixing while you are working on this. The PackageConstant looks good to me, but I would not expose it in a tools.h header when it is only really needed here. Please see my comment on your UBOUND patch. I think there is an opportunity to centralize more in GetConstantArrayHelper and make its interface even clearer.

flang/include/flang/Evaluate/tools.h
1044

I think PackageConstant is very specific to the LBOUND/UBOUND folding usage. I am not sure it is worth exposing it in this widely included header.

Looking at GetConstantArrayLboundHelper, it is only ever used in folding, so maybe PackageConstant could be moved inside it, or next to it if that brings too many templates.

flang/lib/Evaluate/fold-integer.cpp
40

Since you are fixing LBOUND and UBOUND bugs, I think this part of the previous code is incorrect, as far as I can tell, the lower bounds of (cst) are ones, not the ones of cst.

e.g:

   integer, parameter :: a3(2:4,4:6) = 0
   print *, LBOUND((a3), 1)
end

Should print 1, not 2, and I think returning GetLbound(x.left()) will cause it to return 2.

Apply review changes, update tests

Sync with main

FruitClover added inline comments.Apr 18 2022, 7:22 AM
flang/include/flang/Evaluate/tools.h
1044

Thanks a lot for details, moved the class closer.

flang/lib/Evaluate/fold-integer.cpp
40

Oh, that's an interesting case, submitted https://reviews.llvm.org/D123838 based on current PR.

jeanPerier accepted this revision.Apr 19 2022, 5:22 AM

LGTM aside of the two style nits.

flang/lib/Evaluate/fold-integer.cpp
24

Prefer {} init instead of = init in semantics code.

98

Prefer {} init instead of = init in semantics code.

This revision is now accepted and ready to land.Apr 19 2022, 5:22 AM

Switch = to {}

Please use braced initialization. It is safer.

FruitClover marked 2 inline comments as done.Apr 19 2022, 11:11 AM

Please use braced initialization. It is safer.

Sorry, already updated in previous push, other PR's in stack need only rebase.

This revision was automatically updated to reflect the committed changes.