This is an archive of the discontinued LLVM Phabricator instance.

[VectorUtils] Teach findScalarElement to return splat value.
ClosedPublic

Authored by sdesmalen on Aug 2 2021, 1:57 AM.

Details

Summary

If the vector is a splat of some scalar value, findScalarElement()
can simply return the scalar value if it knows the requested lane
is in the vector.

This is only needed for scalable vectors, because the InsertElement/ShuffleVector
case is already handled explicitly for the fixed-width case.

This helps to recognize an InstCombine fold like:

extractelt(bitcast(splat(%v))) -> bitcast(%v)

Diff Detail

Event Timeline

sdesmalen created this revision.Aug 2 2021, 1:57 AM
sdesmalen requested review of this revision.Aug 2 2021, 1:57 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 2 2021, 1:57 AM
sdesmalen added inline comments.Aug 2 2021, 2:00 AM
llvm/lib/Analysis/VectorUtils.cpp
329

@spatel, I wasn't entirely sure if similar arguments apply as mentioned here: https://reviews.llvm.org/D104867#2840537

frasercrmck added inline comments.Aug 2 2021, 2:04 AM
llvm/test/Transforms/InstCombine/vscale_extractelement.ll
253

Are these undefs supposed to be poison nowadays? I think I've seen both recently so it's as much for my own knowledge :)

sdesmalen added inline comments.Aug 2 2021, 3:08 AM
llvm/test/Transforms/InstCombine/vscale_extractelement.ll
253

I think for this specific example it doesn't really matter, but the commit message of D93586 shows an example where it does. I'll change it to poison anyway to match what's done in other tests.

sdesmalen updated this revision to Diff 363421.Aug 2 2021, 3:09 AM

Changed undef -> poison in test.

LGTM but I think official approval would best come from the other reviewers.

spatel accepted this revision.Aug 12 2021, 2:13 PM

Sorry for the delay - LGTM

This revision is now accepted and ready to land.Aug 12 2021, 2:13 PM
spatel added inline comments.Aug 12 2021, 2:24 PM
llvm/lib/Analysis/VectorUtils.cpp
329

Oops - missed this comment. If it's a splat, then do we care what EltNo is?

Ie, if we have this:
define i64 @ext_lane_from_splat(i64 %v) {

%in = insertelement <vscale x 4 x i64> poison, i64 %v, i32 0
%splat = shufflevector <vscale x 4 x i64> %in, <vscale x 4 x i64> poison, <vscale x 4 x i32> zeroinitializer
%r = extractelement <vscale x 4 x i64> %splat, i32 4 ; what does it mean if there are only 4 elts at runtime?
ret i64 %r

}

Should it simplify to ret i64 %v?

sdesmalen added inline comments.Aug 15 2021, 4:21 AM
llvm/lib/Analysis/VectorUtils.cpp
329

The LangRef states that if there are only 4 elements at runtime, that extracting element 4 results in a poison value. I'm tempted to argue that if executed on a machine with a wider vector width, returning %v would be safe, so we can equally assume returning %v should not lead to undefined behaviour on a machine that has shorter vectors. But my knowledge of poison is a bit lacking here, so I'm not sure if that assumption is safe to make.

This revision was landed with ongoing or failed builds.Sep 6 2021, 2:56 AM
This revision was automatically updated to reflect the committed changes.