This is an archive of the discontinued LLVM Phabricator instance.

[flang] Handle special case for SHIFTA intrinsic
ClosedPublic

Authored by clementval on Sep 1 2022, 5:22 AM.

Details

Summary

This patch update the lowering of the shifta intrinsic to match
the behvior of gfortran. When the SHIFT value is equal to the
integer bitwidth then we handle it differently.
This is due to the operation used in lowering (mlir::arith::ShRSIOp)
that lowers to ashr.

Before this patch we have the following results:

SHIFTA(  -1, 8) =  0
SHIFTA(  -2, 8) =  0
SHIFTA( -30, 8) =  0
SHIFTA( -31, 8) =  0
SHIFTA( -32, 8) =  0
SHIFTA( -33, 8) =  0
SHIFTA(-126, 8) =  0
SHIFTA(-127, 8) =  0
SHIFTA(-128, 8) =  0

While gfortran is giving this:

SHIFTA(  -1, 8) = -1
SHIFTA(  -2, 8) = -1
SHIFTA( -30, 8) = -1
SHIFTA( -31, 8) = -1
SHIFTA( -32, 8) = -1
SHIFTA( -33, 8) = -1
SHIFTA(-126, 8) = -1
SHIFTA(-127, 8) = -1
SHIFTA(-128, 8) = -1

With this patch flang and gfortran have the same behavior.

Diff Detail

Event Timeline

clementval created this revision.Sep 1 2022, 5:22 AM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptSep 1 2022, 5:22 AM
clementval requested review of this revision.Sep 1 2022, 5:22 AM
jeanPerier accepted this revision.Sep 1 2022, 5:42 AM
jeanPerier added inline comments.
flang/lib/Lower/IntrinsicCall.cpp
4075

Do you know if using a fir.if vs a SelectOp impacts the generated code from a performance point of view ? I tend to avoid branches in intrinsic code, but I do not have any data to prove this is a better choice either, and probably easy to later update if this turns out to be sub-optimal.

This revision is now accepted and ready to land.Sep 1 2022, 5:42 AM
clementval updated this revision to Diff 457246.Sep 1 2022, 6:08 AM

Use select

clementval marked an inline comment as done.Sep 1 2022, 6:09 AM
clementval added inline comments.
flang/lib/Lower/IntrinsicCall.cpp
4075

I think it is better to use SelectOp here. I had a bit more complicated logic at first but with the current test a select is fine. I just updated the patch with this change.

This revision was automatically updated to reflect the committed changes.
clementval marked an inline comment as done.