This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] bitcast (extractelement <1 x elt>, dest) -> bitcast(<1 x elt>, dest)
ClosedPublic

Authored by Chenbing.Zheng on May 19 2022, 12:51 AM.

Details

Summary

bitcast (extractelement <1 x elt>, dest) -> bitcast(<1 x elt>, dest)

Only solve dest type is vector to avoid inverse transform in visitBitCast.

Diff Detail

Event Timeline

Herald added a project: Restricted Project. · View Herald TranscriptMay 19 2022, 12:51 AM
Chenbing.Zheng requested review of this revision.May 19 2022, 12:51 AM
spatel added inline comments.
llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
2375

One-use should not be here - this transform replaces a value with an existing value, so it is always profitable.

llvm/test/Transforms/InstCombine/bitcast-inseltpoison.ll
361

This extractelement of a 1-element vector doesn't seem like the best canonicalization - it could just be a bitcast from vector to scalar. But we have this unusual inverse transform:
https://github.com/llvm/llvm-project/blob/cefe472c51fbcd1aed4d4a090709f25a12a8bc2c/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp#L2750

That potentially creates 2 instructions (extract + bitcast) from a single bitcast. I'm not sure why we do that, but there is some potential regression with the x86 MMX type (see a bit above that in the source code for a similar transform). @craig.topper or @RKSimon might have some ideas on that.

Ideally, we would transform any <1 x Elt> vector extract instruction in the other direction (extelt --> bitcast). Then we would not need this patch because all of the bitcasts would collapse with existing folds.

If that is not possible, then it would at least be better to have this patch moved over to InstSimplify - we are not creating any new instructions.

Chenbing.Zheng retitled this revision from [InstCombine] bitcast (extractelement (bitcast x), index) -> X to [InstCombine] bitcast (extractelement <1 x elt>, dest) -> bitcast(<1 x elt>, dest).
Chenbing.Zheng edited the summary of this revision. (Show Details)
Chenbing.Zheng added a reviewer: craig.topper.

address comments and add more tests

spatel requested changes to this revision.May 26 2022, 9:07 AM

Please pre-commit the baseline tests.

But we also need tests with scalable vectors - the patch will crash on this example:

define <2 x i32> @bitcast_extelt5_scalable(<vscale x 1 x i64> %A) {
  %ext = extractelement <vscale x 1 x i64> %A, i32 0
  %bc = bitcast i64 %ext to <2 x i32>
  ret <2 x i32> %bc
}
This revision now requires changes to proceed.May 26 2022, 9:07 AM

address comment and rebase tests

spatel added inline comments.May 27 2022, 5:14 AM
llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
2375

Is there an advantage to adding a new API for this?
Since we need the casted object anyway, this seems better:

auto *FixedVType = dyn_cast<FixedVectorType>(VecType);
if (DestType->isVectorTy() && FixedVType && FixedVType->getNumElements() == 1)

address comments

Chenbing.Zheng marked 2 inline comments as done.May 28 2022, 9:31 PM
Chenbing.Zheng added inline comments.
llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
2375

Done and thanks for your advice~

spatel accepted this revision.May 29 2022, 6:22 AM

LGTM

This revision is now accepted and ready to land.May 29 2022, 6:22 AM
This revision was landed with ongoing or failed builds.May 29 2022, 7:18 PM
This revision was automatically updated to reflect the committed changes.
Chenbing.Zheng marked an inline comment as done.