This is an archive of the discontinued LLVM Phabricator instance.

[flang] Support intrinsic `selected_int_kind` for variables
ClosedPublic

Authored by peixin on Jul 17 2022, 1:07 AM.

Details

Summary

As Fortran 2018 16.9.169, the argument of selected_int_kind is integer
scalar, and result is default integer scalar. The constant expression in
this intrinsic has been supported by folding the constant expression.
This supports lowering and runtime for variables in this intrinsic.

Diff Detail

Event Timeline

peixin created this revision.Jul 17 2022, 1:07 AM
Herald added a project: Restricted Project. · View Herald TranscriptJul 17 2022, 1:07 AM
peixin requested review of this revision.Jul 17 2022, 1:07 AM
peixin updated this revision to Diff 445300.
peixin added a reviewer: sscalpone.
peixin updated this revision to Diff 445305.Jul 17 2022, 1:39 AM

Fix the bot fail on windows.

peixin updated this revision to Diff 445308.Jul 17 2022, 2:15 AM
peixin retitled this revision from [flang] Support intrinsic `selected_int_kind` for non-named constant to [flang] Support intrinsic `selected_int_kind` for variables.
peixin edited the summary of this revision. (Show Details)

Trying to fix windows bot fail by adding #ifdef __SIZEOF_INT128__ in flang/lib/Optimizer/Builder/Runtime/Numeric.cpp.

peixin updated this revision to Diff 445310.Jul 17 2022, 4:00 AM

Fix CI fail for test case by adding ! REQUIRES: shell.

peixin updated this revision to Diff 445495.Jul 18 2022, 7:40 AM

Add #ifdef __SIZEOF_INT128__ in runtime to fix the runtime error on windows.

jeanPerier added inline comments.Jul 19 2022, 2:58 AM
flang/lib/Optimizer/Builder/Runtime/Numeric.cpp
385

To workaround the fact that the host may not support in128 but the target might, the int128 runtime interface are usually manually defined here, see ForcedRRSpacing16 for example (with floating point instead of integers).

flang/runtime/numeric.cpp
146

The result type could also be CppTypeFor<TypeCategory::Integer, 4> here regardless of T.

147

Why are you handling negative arguments as if they were positive ? I am not seeing a special case for negative R in 16.9.169.

peixin updated this revision to Diff 445790.Jul 19 2022, 5:35 AM

Address the comments and fix two typos for intrinsic SetExponent.

peixin added inline comments.Jul 19 2022, 5:41 AM
flang/lib/Optimizer/Builder/Runtime/Numeric.cpp
385

Thanks for the direction. Fixed.

flang/runtime/numeric.cpp
146

Right. This as one typo.

147
peixin updated this revision to Diff 445807.Jul 19 2022, 7:00 AM

Fix clang-format.

jeanPerier added inline comments.Jul 20 2022, 9:16 AM
flang/runtime/numeric.cpp
147

If I am following the classic-flang correctly, the abs call is quite different, it is made on the descriptor kind code for some reason specific to classic-flang format, not the "x" value itself.

jeanPerier added inline comments.Jul 20 2022, 9:21 AM
flang/unittests/Runtime/Numeric.cpp
124
 print *, selected_int_kind(-10)
end

gfortran, nvfortran, ifort all return 1 here, I think the abs is wrong.

peixin updated this revision to Diff 446185.Jul 20 2022, 9:43 AM

Fix the wrong computation.

Thanks @jeanPerier for the correction.

flang/unittests/Runtime/Numeric.cpp
124

You are right. According to the standard, the value is in the range −pow(10, R) < n < pow(10, R) (I don't know how to type it here. In Fortran 2018 16.9.169), where R is the input argument. When R is negative, pow(10, R) is always less than 1. So, the result is 1.

peixin updated this revision to Diff 447345.Jul 25 2022, 8:10 AM

Update the approach to follow the style of SELECTED_REAL_KIND as @jeanPerier suggested.

jeanPerier accepted this revision.Jul 25 2022, 8:29 AM

LGTM, thanks

This revision is now accepted and ready to land.Jul 25 2022, 8:29 AM
rogfer01 added inline comments.
flang/lib/Lower/IntrinsicCall.cpp
926

I have a couple of small questions around here.

Does the string of the argument have to be the dummy argument name as specified in the spec? If so, in my draft of Fortran 2018 this is called r instead of scalar.

Also, why passing the argument asAddr, could it be passed asValue instead? I imagine that would require converting the value into some specific integer type before passing it to the runtime function. Do we really expect a user to pass an integer so large that forces us to cast the address to the proper integer type.

peixin added inline comments.Jul 26 2022, 7:41 PM
flang/lib/Lower/IntrinsicCall.cpp
926

Does the string of the argument have to be the dummy argument name as specified in the spec? If so, in my draft of Fortran 2018 this is called r instead of scalar.

I don't think the string must be totally same as that in the standard. Usually it's better to keep the same since the argument name in the standard means something. scalar has better meaning than r, so I use it.

Also, why passing the argument asAddr, could it be passed asValue instead? I imagine that would require converting the value into some specific integer type before passing it to the runtime function. Do we really expect a user to pass an integer so large that forces us to cast the address to the proper integer type.

This intrinsic is not performance-sensitive. It is recommended to use the current approach in https://reviews.llvm.org/D130183#3667603.

rogfer01 added inline comments.Jul 27 2022, 12:23 AM
flang/lib/Lower/IntrinsicCall.cpp
926

Thanks for the clarifications!