This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] Use known bits to determine exact int->fp cast
ClosedPublic

Authored by Allen on Jun 15 2022, 6:34 AM.

Diff Detail

Event Timeline

Allen created this revision.Jun 15 2022, 6:34 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 15 2022, 6:34 AM
Herald added a subscriber: hiraditya. · View Herald Transcript
Allen requested review of this revision.Jun 15 2022, 6:34 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 15 2022, 6:34 AM
nikic retitled this revision from [InstCombne] Find if the source integer value has less significant bits to [InstCombine] Use known bits to determine exact int->fp cast.Jun 16 2022, 12:25 AM

It would be good to add tests for the sitofp case as well.

llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
1760

Can use IC.computeKnownBits(Src, 0, &I) to also make use of AC/DT, I believe.

llvm/test/Transforms/InstCombine/sitofp.ll
245

According to alive, this mask would be fine: https://alive2.llvm.org/ce/z/PdpF6F This is because this is a mask of the form 0b1000... rather than 0b1111.... The right mask to test would be: https://alive2.llvm.org/ce/z/GYxTRu (We could also make use of these low zero bits, but I think that would be better as a separate change.)

Allen updated this revision to Diff 437491.Jun 16 2022, 3:12 AM
Allen marked an inline comment as done.Jun 16 2022, 3:21 AM
Allen added inline comments.
llvm/test/Transforms/InstCombine/sitofp.ll
245

good catch. Thanks @nikic for the detailed explanations, I'll address this after this change.

spatel added inline comments.Jun 16 2022, 7:42 AM
llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
1946

We should have at least one test providing coverage for this path (and fpext too?).

IIUC, this patch will improve a case like this:

define half @masked_int_to_fp_trunc(i32 %A) {
  %m = and i32 %A, 16777215
  %B = sitofp i32 %m to float
  %C = fptrunc float %B to half
  ret half %C
}

But we don't actually need a mask:
https://alive2.llvm.org/ce/z/iaEX2i
...so the fold conditions still aren't quite right.

Allen added inline comments.Jun 16 2022, 7:50 PM
llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
1946

hi @spatel, I think the case in your link https://alive2.llvm.org/ce/z/iaEX2i is related to another top, maybe make use of the fptrunc?

  • In this patch, we hope fold the case
%m = and i25 %A, smallMaskValue
%B = uitofp i25 %m to float
%C = fptoui float %B to i25
  • base on the bt we can see, it has both FPToUI and FPtoI.
#0  isKnownExactCastIntToFP (I=..., IC=...)
    at /home/zhongyunde/llvm-project-init-dev_12x/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp:1762
#1  0x0000aaaaae7ee18c in llvm::InstCombinerImpl::foldItoFPtoI (this=0xffffffffbce0, FI=...)
    at /home/zhongyunde/llvm-project-init-dev_12x/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp:1986
#2  0x0000aaaaae7ee398 in llvm::InstCombinerImpl::visitFPToUI (this=0xffffffffbce0, FI=...)
    at /home/zhongyunde/llvm-project-init-dev_12x/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp:2011
spatel added inline comments.Jun 17 2022, 6:20 AM
llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
1946

I understand that we are not optimizing ideally for this pattern, but that's not my point.
These kinds of transforms are hard to get right (see for example D124692).
This patch affects 3 caller code paths, so I'd like to have tests that provide some coverage for those paths.
Please rebase with test diffs after:
a5040860412f

Allen updated this revision to Diff 437879.Jun 17 2022, 6:48 AM

rebase

llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
1946

Done, thanks!

rebase

I don't think this was updated correctly. There should be test diffs from the file fpcast.ll?

Allen updated this revision to Diff 437913.Jun 17 2022, 8:11 AM
Allen added a comment.EditedJun 17 2022, 8:14 AM

rebase

I don't think this was updated correctly. There should be test diffs from the file fpcast.ll?

sorry, my bad. Now it is ok

Allen marked 2 inline comments as done.Jun 20 2022, 5:15 AM
Allen marked an inline comment as done.Jun 21 2022, 6:12 AM

ping ?

Allen edited the summary of this revision. (Show Details)Jun 24 2022, 1:00 AM
spatel accepted this revision.Jun 29 2022, 1:06 PM

LGTM - clearly, we can still do better, but this seems like a safe improvement.

llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
1758

Please leave the TODO note. We could still check sign bits rather than zeros or do some other kind of refinement.

This revision is now accepted and ready to land.Jun 29 2022, 1:06 PM
Allen updated this revision to Diff 441225.Jun 29 2022, 6:35 PM
This revision was landed with ongoing or failed builds.Jun 29 2022, 6:48 PM
This revision was automatically updated to reflect the committed changes.
Allen marked an inline comment as done.