[AVX-512] Look for vector bit rotates on vectors larger than 16 bytes
define dso_local <2 x i64> @foo(<2 x i64> %0, <2 x i64> %1) local_unnamed_addr {
Entry:
%2 = trunc <2 x i64> %1 to <2 x i6>
%3 = sub <2 x i6> zeroinitializer, %2
%4 = zext <2 x i6> %3 to <2 x i64>
%5 = shl <2 x i64> %0, %4
%6 = and <2 x i64> %1, splat (i64 63)
%7 = lshr <2 x i64> %0, %6
%8 = or <2 x i64> %5, %7
ret <2 x i64> %8
}
define dso_local <4 x i64> @bar(<4 x i64> %0, <4 x i64> %1) local_unnamed_addr {
Entry:
%2 = trunc <4 x i64> %1 to <4 x i6>
%3 = sub <4 x i6> zeroinitializer, %2
%4 = zext <4 x i6> %3 to <4 x i64>
%5 = shl <4 x i64> %0, %4
%6 = and <4 x i64> %1, splat (i64 63)
%7 = lshr <4 x i64> %0, %6
%8 = or <4 x i64> %5, %7
ret <4 x i64> %8
}
define dso_local <8 x i64> @baz(<8 x i64> %0, <8 x i64> %1) local_unnamed_addr {
Entry:
%2 = trunc <8 x i64> %1 to <8 x i6>
%3 = sub <8 x i6> zeroinitializer, %2
%4 = zext <8 x i6> %3 to <8 x i64>
%5 = shl <8 x i64> %0, %4
%6 = and <8 x i64> %1, splat (i64 63)
%7 = lshr <8 x i64> %0, %6
%8 = or <8 x i64> %5, %7
ret <8 x i64> %8
}
Emit:
foo:
vprorvq xmm0, xmm0, xmm1
ret
.LCPI1_0:
.quad 63
bar:
vpbroadcastq ymm4, qword ptr [rip + .LCPI1_0]
vpmovqd xmm2, ymm1
vpxor xmm3, xmm3, xmm3
vpsubd xmm2, xmm3, xmm2
vpmovzxdq ymm2, xmm2
vpand ymm2, ymm2, ymm4
vpand ymm1, ymm1, ymm4
vpsllvq ymm2, ymm0, ymm2
vpsrlvq ymm0, ymm0, ymm1
vpor ymm0, ymm2, ymm0
ret
.LCPI2_0:
.quad 63
baz:
vpmovqw xmm2, zmm1
vpbroadcastq zmm4, qword ptr [rip + .LCPI2_0]
vpxor xmm3, xmm3, xmm3
vpsubw xmm2, xmm3, xmm2
vpmovzxwq zmm2, xmm2
vpandq zmm1, zmm1, zmm4
vpandq zmm2, zmm2, zmm4
vpsllvq zmm2, zmm0, zmm2
vpsrlvq zmm0, zmm0, zmm1
vporq zmm0, zmm2, zmm0
ret
@llvm/issue-subscribers-backend-x86
Author: Niles Salter (Validark)
define dso_local <2 x i64> @<!-- -->foo(<2 x i64> %0, <2 x i64> %1) local_unnamed_addr {
Entry:
%2 = trunc <2 x i64> %1 to <2 x i6>
%3 = sub <2 x i6> zeroinitializer, %2
%4 = zext <2 x i6> %3 to <2 x i64>
%5 = shl <2 x i64> %0, %4
%6 = and <2 x i64> %1, splat (i64 63)
%7 = lshr <2 x i64> %0, %6
%8 = or <2 x i64> %5, %7
ret <2 x i64> %8
}
define dso_local <4 x i64> @<!-- -->bar(<4 x i64> %0, <4 x i64> %1) local_unnamed_addr {
Entry:
%2 = trunc <4 x i64> %1 to <4 x i6>
%3 = sub <4 x i6> zeroinitializer, %2
%4 = zext <4 x i6> %3 to <4 x i64>
%5 = shl <4 x i64> %0, %4
%6 = and <4 x i64> %1, splat (i64 63)
%7 = lshr <4 x i64> %0, %6
%8 = or <4 x i64> %5, %7
ret <4 x i64> %8
}
define dso_local <8 x i64> @<!-- -->baz(<8 x i64> %0, <8 x i64> %1) local_unnamed_addr {
Entry:
%2 = trunc <8 x i64> %1 to <8 x i6>
%3 = sub <8 x i6> zeroinitializer, %2
%4 = zext <8 x i6> %3 to <8 x i64>
%5 = shl <8 x i64> %0, %4
%6 = and <8 x i64> %1, splat (i64 63)
%7 = lshr <8 x i64> %0, %6
%8 = or <8 x i64> %5, %7
ret <8 x i64> %8
}
Emit:
foo:
vprorvq xmm0, xmm0, xmm1
ret
.LCPI1_0:
.quad 63
bar:
vpbroadcastq ymm4, qword ptr [rip + .LCPI1_0]
vpmovqd xmm2, ymm1
vpxor xmm3, xmm3, xmm3
vpsubd xmm2, xmm3, xmm2
vpmovzxdq ymm2, xmm2
vpand ymm2, ymm2, ymm4
vpand ymm1, ymm1, ymm4
vpsllvq ymm2, ymm0, ymm2
vpsrlvq ymm0, ymm0, ymm1
vpor ymm0, ymm2, ymm0
ret
.LCPI2_0:
.quad 63
baz:
vpmovqw xmm2, zmm1
vpbroadcastq zmm4, qword ptr [rip + .LCPI2_0]
vpxor xmm3, xmm3, xmm3
vpsubw xmm2, xmm3, xmm2
vpmovzxwq zmm2, xmm2
vpandq zmm1, zmm1, zmm4
vpandq zmm2, zmm2, zmm4
vpsllvq zmm2, zmm0, zmm2
vpsrlvq zmm0, zmm0, zmm1
vporq zmm0, zmm2, zmm0
ret
Looks like the trunc and zext around the negate are breaking it. This works
define dso_local <4 x i64> @bar(<4 x i64> %0, <4 x i64> %1) {
Entry:
%3 = sub <4 x i64> zeroinitializer, %1
%4 = and <4 x i64> %3, splat (i64 63)
%5 = shl <4 x i64> %0, %4
%6 = and <4 x i64> %1, splat (i64 63)
%7 = lshr <4 x i64> %0, %6
%8 = or <4 x i64> %5, %7
ret <4 x i64> %8
}
define dso_local <8 x i64> @baz(<8 x i64> %0, <8 x i64> %1) {
Entry:
%3 = sub <8 x i64> zeroinitializer, %1
%4 = and <8 x i64> %3, splat (i64 63)
%5 = shl <8 x i64> %0, %4
%6 = and <8 x i64> %1, splat (i64 63)
%7 = lshr <8 x i64> %0, %6
%8 = or <8 x i64> %5, %7
ret <8 x i64> %8
}
We could handle this in InstCombine with:
----------------------------------------
define i64 @src(i64 %a0) {
%trunc = trunc i64 %a0 to i6
%neg = sub i6 0, %trunc
%zext = zext i6 %neg to i64
ret i64 %zext
}
=>
define i64 @tgt(i64 %a0) {
%neg = sub i64 0, %a0
%mask = and i64 %neg, 63
ret i64 %mask
}
Transformation seems to be correct!
There might be a more general sub pattern but the negate might be a good starting point.
Hi!
This issue may be a good introductory issue for people new to working on LLVM. If you would like to work on this issue, your first steps are:
- Check that no other contributor is working on this issue. If someone is assigned to the issue or claimed to be working on it, ping the person. After one week without a response, the assignee may be changed.
- Leave a comment indicating that you are working on the issue, or just create a pull request after following the steps below. Mention this issue in the description of the pull request.
- Fix the issue locally.
- Run the test suite locally. Remember that the subdirectories under
test/create fine-grained testing targets, so you can e.g. usemake check-clang-astto only run Clang's AST tests. - Create a Git commit.
- Run
git clang-format HEAD~1to format your changes. - Open a pull request to the upstream repository on GitHub. Detailed instructions can be found in GitHub's documentation. Mention this issue in the description of the pull request.
If you have any further questions about this issue, don't hesitate to ask via a comment in the thread below.
@llvm/issue-subscribers-good-first-issue
Author: Niles Salter (Validark)
define dso_local <2 x i64> @<!-- -->foo(<2 x i64> %0, <2 x i64> %1) local_unnamed_addr {
Entry:
%2 = trunc <2 x i64> %1 to <2 x i6>
%3 = sub <2 x i6> zeroinitializer, %2
%4 = zext <2 x i6> %3 to <2 x i64>
%5 = shl <2 x i64> %0, %4
%6 = and <2 x i64> %1, splat (i64 63)
%7 = lshr <2 x i64> %0, %6
%8 = or <2 x i64> %5, %7
ret <2 x i64> %8
}
define dso_local <4 x i64> @<!-- -->bar(<4 x i64> %0, <4 x i64> %1) local_unnamed_addr {
Entry:
%2 = trunc <4 x i64> %1 to <4 x i6>
%3 = sub <4 x i6> zeroinitializer, %2
%4 = zext <4 x i6> %3 to <4 x i64>
%5 = shl <4 x i64> %0, %4
%6 = and <4 x i64> %1, splat (i64 63)
%7 = lshr <4 x i64> %0, %6
%8 = or <4 x i64> %5, %7
ret <4 x i64> %8
}
define dso_local <8 x i64> @<!-- -->baz(<8 x i64> %0, <8 x i64> %1) local_unnamed_addr {
Entry:
%2 = trunc <8 x i64> %1 to <8 x i6>
%3 = sub <8 x i6> zeroinitializer, %2
%4 = zext <8 x i6> %3 to <8 x i64>
%5 = shl <8 x i64> %0, %4
%6 = and <8 x i64> %1, splat (i64 63)
%7 = lshr <8 x i64> %0, %6
%8 = or <8 x i64> %5, %7
ret <8 x i64> %8
}
Emit:
foo:
vprorvq xmm0, xmm0, xmm1
ret
.LCPI1_0:
.quad 63
bar:
vpbroadcastq ymm4, qword ptr [rip + .LCPI1_0]
vpmovqd xmm2, ymm1
vpxor xmm3, xmm3, xmm3
vpsubd xmm2, xmm3, xmm2
vpmovzxdq ymm2, xmm2
vpand ymm2, ymm2, ymm4
vpand ymm1, ymm1, ymm4
vpsllvq ymm2, ymm0, ymm2
vpsrlvq ymm0, ymm0, ymm1
vpor ymm0, ymm2, ymm0
ret
.LCPI2_0:
.quad 63
baz:
vpmovqw xmm2, zmm1
vpbroadcastq zmm4, qword ptr [rip + .LCPI2_0]
vpxor xmm3, xmm3, xmm3
vpsubw xmm2, xmm3, xmm2
vpmovzxwq zmm2, xmm2
vpandq zmm1, zmm1, zmm4
vpandq zmm2, zmm2, zmm4
vpsllvq zmm2, zmm0, zmm2
vpsrlvq zmm0, zmm0, zmm1
vporq zmm0, zmm2, zmm0
ret
I would like to work on this issue.
@RKSimon since @Susikrishna is inactive, I'd like to work on this issue.
Will my LLVM Godbolt link start showing the optimization working? Seems to still exhibit the inefficient emit.
Will my LLVM Godbolt link start showing the optimization working? Seems to still exhibit the inefficient emit.
Since instcombine is a middle-end pass, you need to use clang instead of llc.