[PATCH] D95894: [flang] Fix calls to LBOUND() intrinsic for arrays with lower bounds not 1

Tim Keith via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Feb 3 08:55:03 PST 2021


tskeith accepted this revision.
tskeith added inline comments.
This revision is now accepted and ready to land.


================
Comment at: flang/lib/Evaluate/fold-integer.cpp:91
+        return Expr<T>{GetConstantArrayLboundHelper{*dim}.GetLbound(*array)};
+      }
       if (lowerBoundsAreOne) {
----------------
PeteSteinfeld wrote:
> tskeith wrote:
> > I noticed we already have `UnwrapConstantValue`, so I think you could write this as:
> > ```
> >       if (auto *constant{UnwrapConstantValue(*array)}) {
> >         return Expr<T>(constant->lbounds()[*dim]);
> >       }
> > ```
> I have to confess that I find the interaction between the C++ types used in expression processing and template expansion confusing.  But here's why I don't think I can use "UnwrapConstantValue".
> 
> Note that "UnwrapConstantValue" is a template function, so to call it, you need to supply a template parameter, something like:
> 
> ```
>       if (auto *constant{UnwrapConstantValue<T>(*array)}) {
>         return Expr<T>(constant->lbounds()[*dim]);
>       }
> ```
> At this point in the code, the type of "T" is "Type<TypeCategory::Integer, KIND>".  But the type needed to instantiate "UnwrapConstantValue" is the type of the first argument to LBOUND() which could be an array of any type.  So if the type of the array is INTEGER, your suggestion might work.  But it won't work if the argument is an array of type REAL.
> 
> The variable that contains the value of this first argument is "array".  The type of "array" is "evaluate::Expr<SomeType>".  So the type needed to correctly instantiate "UnwrapConstantValue" is wrapped up in the "evaluate::Expr<SomeType>", and I couldn't figure out how to extract it in a way to use it for the instantiation.  So I wrote the class "GetConstantArrayLboundHelper" which extracts the lower bound without knowing the type of the array.
> 
> It's quite possible that there's a C++ technique that would let me avoid writing the class "GetConstantArrayLboundHelper".  But I don't know what it is.
> 
OK, thanks for the explanation.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D95894/new/

https://reviews.llvm.org/D95894



More information about the llvm-commits mailing list