https://gcc.gnu.org/bugzilla/show_bug.cgi?id=125138
Bug ID: 125138
Summary: Instrinsic vec_permx generates incorrect code
Product: gcc
Version: 14.2.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: middle-end
Assignee: unassigned at gcc dot gnu.org
Reporter: munroesj at gcc dot gnu.org
Target Milestone: ---
Created attachment 64342
--> https://gcc.gnu.org/bugzilla/attachment.cgi?id=64342&action=edit
Compile tests for vec_permx intrinsic
Was trying to use the Power10 (PowerISA 3.1) Vector Permute Extended
instruction and vec_permx intrinsic to perform parallel 16-way 64-256-entry
byte table lookups.
Examples are provided in the PowerISA 3.1 description of xxpermx as Programming
Notes.
So tried to compile the C source:
vui8_t
test_vec_permx_c0_PWR10 (vui8_t vra, vui8_t vrb, vui8_t vrc)
{
return vec_permx (vra, vrb, vrc, 0);
}
vui8_t
test_vec_permx_c5_PWR10 (vui8_t vra, vui8_t vrb, vui8_t vrc)
{
return vec_permx (vra, vrb, vrc, 5);
}
vui8_t
test_vec_permx_c7_PWR10 (vui8_t vra, vui8_t vrb, vui8_t vrc)
{
return vec_permx (vra, vrb, vrc, 7);
}
vui8_t
test_permx2_c01_PWR10 (vui8_t vra0, vui8_t vrb0, vui8_t vra1, vui8_t vrb1,
vui8_t vrc)
{
vui8_t perm0, perm1;
perm0 = vec_permx (vra0, vrb0, vrc, 0);
perm1 = vec_permx (vra1, vrb1, vrc, 1);
return vec_or (perm0, perm1);
}
vui8_t
test_permx2_c0123_PWR10 (vui8_t vra0, vui8_t vrb0, vui8_t vra1, vui8_t vrb1,
vui8_t vra2, vui8_t vrb2, vui8_t vra3, vui8_t vrb3, vui8_t
vrc)
{
vui8_t perm0, perm1, perm2, perm3;
perm0 = vec_permx (vra0, vrb0, vrc, 0);
perm1 = vec_permx (vra1, vrb1, vrc, 1);
perm2 = vec_permx (vra2, vrb2, vrc, 2);
perm3 = vec_permx (vra3, vrb3, vrc, 3);
perm0 = vec_or (perm0, perm1);
perm2 = vec_or (perm2, perm3);
return vec_or (perm0, perm2);
}
For the power64le -mcpu=power10 target compiler with -O3 I see:
0000000000000020 <test_vec_permx_c0_PWR10>:
xxlnot vs36,vs36
xxpermx vs34,vs35,vs34,vs36,7
blr
0000000000000040 <test_vec_permx_c5_PWR10>:
xxlnot vs36,vs36
xxpermx vs34,vs35,vs34,vs36,2
blr
0000000000000060 <test_vec_permx_c7_PWR10>:
xxlnot vs36,vs36
xxpermx vs34,vs35,vs34,vs36,0
blr
This is expected for the bi-endian vec_permx intrinsic when generating for
power64le. The permute control vector (PCV) is inverted (xxlnot) and the XA,XB
operands are swapped (XB,XA).
But things get weird (ie a code gen bug) then the lookup is extended to 64-128
bytes.
For the test_permx2_c01_PWR10 example I was expecting something like:
0000000000000080 <test_vec_permx2_c01_PWR10>:
xxlnot vs38,vs38
xxpermx vs34,vs35,vs34,vs38,7
xxpermx vs36,vs37,vs36,vs38,6
xxlor vs34,vs36,vs34
blr
The PCV is a common input to both vec_permx's and the invertion should be
handled as a common sub-expression. But instead I see:
00000000000000b0 <test_permx2_c01_PWR10>:
xxlnot vs38,vs38
xxlnot vs32,vs38
xxpermx vs33,vs35,vs34,vs38,7
xxpermx vs45,vs37,vs36,vs32,6
xxlor vs34,vs33,vs45
blr
The inverted PCV is inverted again and used in the 2nd xxpermx (effectively
big-endian permute).
And the test_permx2_c0123_PWR10 example gets worse. Expecting:
00000000000000b0 <test_permx2_c0123_PWR10>:
xxlnot vs42,vs42
xxpermx vs38,vs39,vs38,vs42,5
xxpermx vs40,vs41,vs40,vs42,4
xxlor vs40,vs40,vs38
xxpermx vs35,vs35,vs34,vs42,7
xxpermx vs32,vs37,vs36,vs42,6
xxlor vs34,vs35,vs32
xxlor vs34,vs34,vs40
blr
But I see:
00000000000000e0 <test_permx2_c0123_PWR10>:
xxlnot vs42,vs42
xxlnot vs45,vs42
xxpermx vs43,vs35,vs34,vs42,7
xxlnot vs44,vs45
xxpermx vs33,vs37,vs36,vs45,6
xxlnot vs45,vs44
xxpermx vs32,vs39,vs38,vs44,5
xxpermx vs51,vs41,vs40,vs45,4
xxlor vs32,vs32,vs51
vor v2,v11,v0
vor v2,v2,v1
Effectively toggling the PCV between little/big Endian for each xxpermx.