This is an archive of the discontinued LLVM Phabricator instance.

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

Authored by FruitClover on Apr 11 2022, 9:46 AM.

Details

Summary

Similarly to LBOUND in https://reviews.llvm.org/D123237, fix UBOUND() folding
for constant arrays (for both w/ and w/o DIM=): convert
GetConstantArrayLboundHelper into common helper class for both lower/upper
bounds.

Diff Detail

Event Timeline

FruitClover created this revision.Apr 11 2022, 9:46 AM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptApr 11 2022, 9:46 AM
FruitClover added inline comments.Apr 11 2022, 9:53 AM
flang/lib/Evaluate/fold-integer.cpp
77–78

Don't know which type is preferred in FE - std::enable_if or maybe constexpr

upd. code comment

Are you missing a dependent patch? Patch application is failing

Are you missing a dependent patch? Patch application is failing

Hello, just wanted to push a patch based on https://reviews.llvm.org/D123237#3443084 (which is still in review) to mention similar problem. (Can I somehow set base to different review HEAD, not main branch?)

rovka added a subscriber: rovka.Apr 12 2022, 12:48 AM

Are you missing a dependent patch? Patch application is failing

Hello, just wanted to push a patch based on https://reviews.llvm.org/D123237#3443084 (which is still in review) to mention similar problem. (Can I somehow set base to different review HEAD, not main branch?)

If you're using the web interface, you can click 'Edit related revisions', then 'Edit parent revisions'. If you're using arc, I think you can add "Depends on D123237" to the commit message. See docs.

FruitClover published this revision for review.Apr 14 2022, 3:06 AM
FruitClover edited the summary of this revision. (Show Details)
jeanPerier added inline comments.
flang/lib/Evaluate/fold-integer.cpp
34

I think the template makes this helper class interface a bit too complex for what it does. I am not sure it is worth making this a template parameter (templates are nice, but they have f18 compile time cost and f18 executable size costs, in this case, I do not think the template adds a lot of type safety).

What about doing having an interface like:

static Expr<T> GetConstantArrayBound::GetLbound(const Expr<SomeType>&, std::optional<int> dim);
and
static Expr<T> GetConstantArrayBound::GetUbound(const Expr<SomeType>&, std::optional<int> dim);

with the implementation looking something like:

template <typename T>
Expr<T> PackageConstantBounds(
    const ConstantSubscripts &&bounds, bool asScalar = false) {
  // .... like  PackageConstant, but returns Expr<T>
}

class GetConstantArrayBound {
public:
   static Expr<T> GetConstantArrayBound::GetLbound(const Expr<SomeType>& array, std::optional<ConstantSubscript> dim) {
     return PackageConstantBounds<T>(GetConstantArrayBound(dim, /*getLbound=*/true)::Get(array), dim.has_value());
  }
  //... same for GetUbound
private:
    // Private ctor ...

    ConstantSubscripts Get(const Constant<T> &x) {
      if (getLbound) { 
        // Return the lower bound
        if (dim_) {
          return {x.lbounds().at(*dim_)};
        } else {
          return x.lbounds();
        }
      } else {
        return  x.ComputeUbounds(dim_);
      }
  }

  // Deal with the const Parentheses<T> &x. (return ones for lbouds, and the left operand shape for ubound).
  
  const std::optional<ConstantSubscript> dim_;
  const bool getLBound_;
}
  
}

Update after review

FruitClover added inline comments.Apr 18 2022, 7:33 AM
flang/lib/Evaluate/fold-integer.cpp
34

Thanks a lot for detailed feedback, suggested change looks much better, updated.

FruitClover edited the summary of this revision. (Show Details)Apr 18 2022, 7:58 AM
FruitClover added inline comments.Apr 18 2022, 8:26 AM
flang/lib/Evaluate/fold-integer.cpp
77–81

Proper UBOUD((x)) is done in https://reviews.llvm.org/D123944 fyi (Did not want to mix changes and simplify review a bit)

clang-format

Other than my inlined comment about UBOUND of (cst), this looks good.

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

The UBOUND case if missing for the Parentheses<T> case (need to return the shape). You can probably use GetConstantExtents if you make the FoldingContext& available here, or you can set a flag that parentheses were met and return x.shape() in Get(const Constant<T> &x) in that case.

FruitClover added inline comments.Apr 19 2022, 6:03 AM
flang/lib/Evaluate/fold-integer.cpp
77

It's very next PR in sequence (Revision Contents -> Stack) - https://reviews.llvm.org/D123944, could combine into 1 patch if that's better.

FruitClover edited the summary of this revision. (Show Details)Apr 20 2022, 11:03 AM
jeanPerier accepted this revision.Apr 22 2022, 2:46 AM

Looks good, thanks for bearing with the comments !

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

Looks good, thanks for bearing with the comments !

Thanks a lot for review, @jeanPerier !

This revision was automatically updated to reflect the committed changes.
FruitClover added a comment.EditedApr 27 2022, 10:19 AM

Windows buildbot failed https://lab.llvm.org/buildbot/#/builders/172/builds/11593 after this patch in flang\test\Semantics\select-rank.f90:

******************** TEST 'Flang :: Semantics/select-rank.f90' FAILED ********************
Script:
--
: 'RUN: at line 1';   "C:\Python310\python.exe" C:\Users\buildbot-worker\minipc-ryzen-win\flang-x86_64-windows\llvm-project\flang\test\Semantics/test_errors.py C:\Users\buildbot-worker\minipc-ryzen-win\flang-x86_64-windows\llvm-project\flang\test\Semantics\select-rank.f90 c:\users\buildbot-worker\minipc-ryzen-win\flang-x86_64-windows\build\bin\flang-new.exe -fc1
--
Exit Code: 1
Command Output (stdout):
--
$ ":" "RUN: at line 1"
$ "C:\Python310\python.exe" "C:\Users\buildbot-worker\minipc-ryzen-win\flang-x86_64-windows\llvm-project\flang\test\Semantics/test_errors.py" "C:\Users\buildbot-worker\minipc-ryzen-win\flang-x86_64-windows\llvm-project\flang\test\Semantics\select-rank.f90" "c:\users\buildbot-worker\minipc-ryzen-win\flang-x86_64-windows\build\bin\flang-new.exe" "-fc1"
# command output:
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.	Program arguments: c:\\users\\buildbot-worker\\minipc-ryzen-win\\flang-x86_64-windows\\build\\bin\\flang-new.exe -fc1 -fsyntax-only C:\\Users\\buildbot-worker\\minipc-ryzen-win\\flang-x86_64-windows\\llvm-project\\flang\\test\\Semantics\\select-rank.f90
#0 0x00007ff786545e9d (c:\users\buildbot-worker\minipc-ryzen-win\flang-x86_64-windows\build\bin\flang-new.exe+0xfc5e9d)
#1 0x00007ff786561245 (c:\users\buildbot-worker\minipc-ryzen-win\flang-x86_64-windows\build\bin\flang-new.exe+0xfe1245)
#2 0x0000000000000068 
#3 0x0000000000000007 
error: command failed with exit status: 1
--
********************

but for now I don't understand the root cause yet (no lbound/ubound in testcase)
(CC @jeanPerier / @klausler )

Windows buildbot failed https://lab.llvm.org/buildbot/#/builders/172/builds/11593 after this patch in flang\test\Semantics\select-rank.f90:
[...]
but for now I don't understand the root cause yet (no lbound/ubound in testcase)
(CC @jeanPerier / @klausler )

I do not understand the failure either. It seems the windows bot is green again: https://lab.llvm.org/buildbot/#/builders/172/builds/11594, so this may have been a glitch...