This is an archive of the discontinued LLVM Phabricator instance.

Shift intrinsics
ClosedPublic

Authored by tarunprabhu on Jul 7 2022, 11:57 AM.

Details

Summary

This patch implements support for lowering the Shift intrinsics in Fortran 2008.

The SHIFTL, SHIFTR and SHIFTA intrinsics take an integer argument I and a SHIFT argument.

If SHIFT < 0, the intrinsics will always return 0
If SHIFT >= BIT_SIZE(I), the intrinsics will always return 0

The DSHIFTL and DSHIFTR intrinsics are implemented using calls to SHIFTL and SHIFTR as described in the standard.

Diff Detail

Event Timeline

tarunprabhu created this revision.Jul 7 2022, 11:57 AM
Herald added a project: Restricted Project. · View Herald TranscriptJul 7 2022, 11:57 AM
tarunprabhu requested review of this revision.Jul 7 2022, 11:57 AM

If SHIFT < 0, the intrinsics will always return 0
If SHIFT >= BIT_SIZE(I), the intrinsics will always return 0

A call to one of these intrinsics is nonconformant if it violates these program requirements. (Except the SHIFT == BIT_SIZE(I) boundary case is conformant.) The language standard describes what the processor/compiler must do for conformant programs. It does not specify the result of a nonconformant call, so the processor/compiler may generate any result. See F18 Clause 1, paragraph 3, and paragraph 4 point 4. Compare the discussion of constraints in Clause 4.1.2, and conformance in Clause 4.2. Unless there is some justification for generating specific nonstandard results, it is typically best to favor faster code.

Is it necessary to return 0 for nonconformant calls, or would it be more efficient to omit the SelectOp's and just do a simple shift, where "whatever happens, happens" for nonconformant calls? Otherwise, code for nonconformant calls penalizes conformant calls.

Is it necessary to return 0 for nonconformant calls, or would it be more efficient to omit the SelectOp's and just do a simple shift, where "whatever happens, happens" for nonconformant calls?

I may have misunderstood @klausler here (https://discourse.llvm.org/t/dealing-with-invalid-arguments-in-intrinsics/63630/4?u=tarunprabhu).

I took his reply to mean that some degree of uniformity with other compilers is desirable although that might seem to incur a performance penalty in this case. I have limited experience with Fortran, and thus not best placed to comment on the preferred course of action. I am happy to modify this patch as others see fit.

jeanPerier accepted this revision.Jul 13 2022, 3:02 AM

Is it necessary to return 0 for nonconformant calls, or would it be more efficient to omit the SelectOp's and just do a simple shift, where "whatever happens, happens" for nonconformant calls?

I may have misunderstood @klausler here (https://discourse.llvm.org/t/dealing-with-invalid-arguments-in-intrinsics/63630/4?u=tarunprabhu).

I took his reply to mean that some degree of uniformity with other compilers is desirable although that might seem to incur a performance penalty in this case. I have limited experience with Fortran, and thus not best placed to comment on the preferred course of action. I am happy to modify this patch as others see fit.

I think your interpretation is correct in this case. If the behavior is unanimous on the SHIFT intrinsics, let's start by following it.
Please add a comment in the code explaining this choice here. Otherwise LGTM, thanks !

flang/lib/Lower/IntrinsicCall.cpp
3704

Please add a small comment here that the "If SHIFT < 0 or SHIFT >= BIT_SIZE(I), the intrinsics will always return 0" behavior is not a Fortran requirement, but all other compilers do it.

This revision is now accepted and ready to land.Jul 13 2022, 3:02 AM

Added a comment explaining why SHIFT < 0 or SHIFT >= BIT_SIZE(I) returns 0.

tarunprabhu marked an inline comment as done.Jul 13 2022, 7:41 AM

Thank you @vdonaldson and @jeanPerier for reviewing this.

Updated tests to use flang driver in addition to bbc.

This revision was automatically updated to reflect the committed changes.

@tarunprabhu , this implementation is causing failures in the NAG tests. Do you have access to them?

@tarunprabhu , this implementation is causing failures in the NAG tests. Do you have access to them?

@PeteSteinfeld, no I do not. If you can file a bug report, I can investigate the failure.

@tarunprabhu , this implementation is causing failures in the NAG tests. Do you have access to them?

@PeteSteinfeld, no I do not. If you can file a bug report, I can investigate the failure.

Thanks, @tarunprabhu. I'm all set for now.