This is an archive of the discontinued LLVM Phabricator instance.

[LangRef] Update shufflevector's semantics to return poison if the mask is undef
ClosedPublic

Authored by ManuelJBrito on Apr 18 2023, 8:12 AM.

Details

Summary

This patch changes the shufflevector's semantics to yield poison if the mask is undefined. This allows the extraction of shufflevectors while also opening the door for more optimization opportunities due to the fact that poison is more undefined than undef.

This is a continuation of the work done by @aqjune in D93818.

As described at https://github.com/llvm/llvm-project/issues/43530, here is a quick overview:

Undef in the shufflevector mask yielding undef even when the shuffled value is poison makes the following transformation invalid:

define <4 x float> @insert_not_undef_shuffle_translate_commute(float %x, <4 x float> %y, <4 x float> %q) {
  %xv = insertelement <4 x float> %q, float %x, i32 2
  %r = shufflevector <4 x float> %y, <4 x float> %xv, <4 x i32> { 0, 6, 2, undef }
  ret <4 x float> %r
}
=>
define <4 x float> @insert_not_undef_shuffle_translate_commute(float %x, <4 x float> %y, <4 x float> %q) {
  %r = insertelement <4 x float> %y, float %x, i32 1
  ret <4 x float> %r
}

float %x = poison
<4 x float> %y = < poison, poison, poison, poison >
<4 x float> %q = < poison, poison, poison, poison >

Source:
<4 x float> %xv = < poison, poison, poison, poison >
<4 x float> %r = < poison, poison, poison, undef >

Target:
<4 x float> %r = < poison, poison, poison, poison >

Source value: < poison, poison, poison, undef >
Target value: < poison, poison, poison, poison >

The motivation for the current semantics was to be able to introduce shufflevectors when building a vector with a sequence of insertelements starting from an undef vector:

%t = insertelement <4 x float> undef, float %arg, i32 1
%t4 = insertelement <4 x float> %t, float %arg, i32 1
%t5 = insertelement <4 x float> %t4, float %arg, i32 2
%t6 = insertelement <4 x float> %t5, float %arg, i32 3
ret <4 x float> %t6
=>
%t = insertelement <4 x float> undef, float %arg, i32 0
%t2 = shufflevector <4 x float> %t, <4 x float> undef, <4 x i32> <i32 undef, i32 0, i32 0, i32 0>
ret <4 x float> %t2

With the soft deprecation of undef these cases should become more rare. So these new semantics in combination with disabling these transformations when an undef placeholder is used should prevent miscompilations without meaningful regressions.

Theses cases are identified at https://web.ist.utl.pt/nuno.lopes/alive2/index.php?hash=a9bc405e427c007f with 'LLVM PR44185'.

Diff Detail

Event Timeline

ManuelJBrito created this revision.Apr 18 2023, 8:12 AM
Herald added a project: Restricted Project. · View Herald TranscriptApr 18 2023, 8:12 AM
ManuelJBrito requested review of this revision.Apr 18 2023, 8:12 AM
Herald added a project: Restricted Project. · View Herald TranscriptApr 18 2023, 8:12 AM
nlopes accepted this revision.Apr 19 2023, 7:44 AM

Looks great, thanks!

Please wait for at least another reviewer.

This revision is now accepted and ready to land.Apr 19 2023, 7:44 AM
aqjune added inline comments.Apr 23 2023, 6:27 PM
llvm/docs/LangRef.rst
10102

Hi, it seems the LLVM assembly printer hasn't been updated yet: https://godbolt.org/z/ndEdos9Mx
Should we make this change after the assembly printer is updated in the future?

ManuelJBrito added inline comments.
llvm/docs/LangRef.rst
10102

Yes, I was just finishing updating the tests. Here is the patch D149210.
Thanks

nikic added a subscriber: nikic.Apr 26 2023, 12:42 AM

I'm fine with the semantics change, but I think there are too many words here.

llvm/docs/LangRef.rst
10036

I don't think this sentence really adds anything. It just introduces a new ill-defined term "don't-care vector".

10036

(The recommendation to use poison instead of undef is a general one, that applies not only to insertelement.)

10091–10101

I feel like we could use a lot less words here and be less confusing at the same time.

ManuelJBrito added inline comments.Apr 26 2023, 6:08 AM
llvm/docs/LangRef.rst
10091–10101

I agree your wording is easier to follow, but shouldn't we still mention that selection of an undef value produces undef just to make it clear?

Address comments:

  • simplify wordings
  • remove undef to poison recommendation
nikic accepted this revision.Apr 27 2023, 6:01 AM

LGTM

This revision was landed with ongoing or failed builds.Apr 28 2023, 2:52 AM
This revision was automatically updated to reflect the committed changes.