[AVX-512] `splat(u64 & 255) -| u64s_that_fit_in_bytes` should lower to `vpsubusb`
define internal <8 x i64> @example.foo(i64 %0) unnamed_addr align 1 {
Entry:
%1 = and i64 %0, 255
%2 = insertelement <1 x i64> poison, i64 %1, i64 0
%3 = shufflevector <1 x i64> %2, <1 x i64> poison, <8 x i32> zeroinitializer
%4 = tail call <8 x i64> @llvm.usub.sat.v8i64(<8 x i64> %3, <8 x i64> <i64 0, i64 64, i64 128, i64 192, i64 0, i64 64, i64 128, i64 192>)
ret <8 x i64> %4
}
declare <8 x i64> @llvm.usub.sat.v8i64(<8 x i64>, <8 x i64>) #1
Currently becomes:
.LCPI1_0:
.quad 0
.quad 64
.quad 128
.quad 192
example.foo:
vbroadcasti64x4 zmm1, ymmword ptr [rip + .LCPI1_0]
movzx eax, dil
vpbroadcastq zmm0, rax
vpmaxuq zmm0, zmm0, zmm1
vpsubq zmm0, zmm0, zmm1
ret
Should be:
.LCPI0_0:
.byte 0
.byte 255
.byte 255
.byte 255
.byte 255
.byte 255
.byte 255
.byte 255
.byte 64
.byte 255
.byte 255
.byte 255
.byte 255
.byte 255
.byte 255
.byte 255
.byte 128
.byte 255
.byte 255
.byte 255
.byte 255
.byte 255
.byte 255
.byte 255
.byte 192
.byte 255
.byte 255
.byte 255
.byte 255
.byte 255
.byte 255
.byte 255
.byte 0
.byte 255
.byte 255
.byte 255
.byte 255
.byte 255
.byte 255
.byte 255
.byte 64
.byte 255
.byte 255
.byte 255
.byte 255
.byte 255
.byte 255
.byte 255
.byte 128
.byte 255
.byte 255
.byte 255
.byte 255
.byte 255
.byte 255
.byte 255
.byte 192
.byte 255
.byte 255
.byte 255
.byte 255
.byte 255
.byte 255
.byte 255
example.foo:
vpbroadcastq zmm0, rdi
vpsubusb zmm0, zmm0, zmmword ptr [rip + .LCPI0_0]
ret
This is kinda two optimizations for the price of one.
- We can lower
llvm.usub.sat.v8i64tollvm.usub.sat.v64i8because we are operating on u64's that definitely fit within a byte. - We can then fold the
u64 & 255intollvm.usub.sat.v64i8by placing 255's in the upper 7 bytes of each u64 lane.
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. Be aware that LLVM's AI policy forbids the use of AI tools to fix good first issues since it squanders a learning opportunity. Using LLMs to search and learn is permitted, since the goal is learning.
- 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-backend-x86
Author: Niles Salter (Validark)
define internal <8 x i64> @<!-- -->example.foo(i64 %0) unnamed_addr align 1 {
Entry:
%1 = and i64 %0, 255
%2 = insertelement <1 x i64> poison, i64 %1, i64 0
%3 = shufflevector <1 x i64> %2, <1 x i64> poison, <8 x i32> zeroinitializer
%4 = tail call <8 x i64> @<!-- -->llvm.usub.sat.v8i64(<8 x i64> %3, <8 x i64> <i64 0, i64 64, i64 128, i64 192, i64 0, i64 64, i64 128, i64 192>)
ret <8 x i64> %4
}
declare <8 x i64> @<!-- -->llvm.usub.sat.v8i64(<8 x i64>, <8 x i64>) #<!-- -->1
Currently becomes:
.LCPI1_0:
.quad 0
.quad 64
.quad 128
.quad 192
example.foo:
vbroadcasti64x4 zmm1, ymmword ptr [rip + .LCPI1_0]
movzx eax, dil
vpbroadcastq zmm0, rax
vpmaxuq zmm0, zmm0, zmm1
vpsubq zmm0, zmm0, zmm1
ret
Should be:
.LCPI0_0:
.byte 0
.byte 255
.byte 255
.byte 255
.byte 255
.byte 255
.byte 255
.byte 255
.byte 64
.byte 255
.byte 255
.byte 255
.byte 255
.byte 255
.byte 255
.byte 255
.byte 128
.byte 255
.byte 255
.byte 255
.byte 255
.byte 255
.byte 255
.byte 255
.byte 192
.byte 255
.byte 255
.byte 255
.byte 255
.byte 255
.byte 255
.byte 255
.byte 0
.byte 255
.byte 255
.byte 255
.byte 255
.byte 255
.byte 255
.byte 255
.byte 64
.byte 255
.byte 255
.byte 255
.byte 255
.byte 255
.byte 255
.byte 255
.byte 128
.byte 255
.byte 255
.byte 255
.byte 255
.byte 255
.byte 255
.byte 255
.byte 192
.byte 255
.byte 255
.byte 255
.byte 255
.byte 255
.byte 255
.byte 255
example.foo:
vpbroadcastq zmm0, rdi
vpsubusb zmm0, zmm0, zmmword ptr [rip + .LCPI0_0]
ret
This is kinda two optimizations for the price of one.
- We can lower
llvm.usub.sat.v8i64tollvm.usub.sat.v64i8because we are operating on u64's that definitely fit within a byte. - We can then fold the
u64 & 255intollvm.usub.sat.v64i8by placing 255's in the upper 7 bytes of each u64 lane.
@llvm/issue-subscribers-good-first-issue
Author: Niles Salter (Validark)
define internal <8 x i64> @<!-- -->example.foo(i64 %0) unnamed_addr align 1 {
Entry:
%1 = and i64 %0, 255
%2 = insertelement <1 x i64> poison, i64 %1, i64 0
%3 = shufflevector <1 x i64> %2, <1 x i64> poison, <8 x i32> zeroinitializer
%4 = tail call <8 x i64> @<!-- -->llvm.usub.sat.v8i64(<8 x i64> %3, <8 x i64> <i64 0, i64 64, i64 128, i64 192, i64 0, i64 64, i64 128, i64 192>)
ret <8 x i64> %4
}
declare <8 x i64> @<!-- -->llvm.usub.sat.v8i64(<8 x i64>, <8 x i64>) #<!-- -->1
Currently becomes:
.LCPI1_0:
.quad 0
.quad 64
.quad 128
.quad 192
example.foo:
vbroadcasti64x4 zmm1, ymmword ptr [rip + .LCPI1_0]
movzx eax, dil
vpbroadcastq zmm0, rax
vpmaxuq zmm0, zmm0, zmm1
vpsubq zmm0, zmm0, zmm1
ret
Should be:
.LCPI0_0:
.byte 0
.byte 255
.byte 255
.byte 255
.byte 255
.byte 255
.byte 255
.byte 255
.byte 64
.byte 255
.byte 255
.byte 255
.byte 255
.byte 255
.byte 255
.byte 255
.byte 128
.byte 255
.byte 255
.byte 255
.byte 255
.byte 255
.byte 255
.byte 255
.byte 192
.byte 255
.byte 255
.byte 255
.byte 255
.byte 255
.byte 255
.byte 255
.byte 0
.byte 255
.byte 255
.byte 255
.byte 255
.byte 255
.byte 255
.byte 255
.byte 64
.byte 255
.byte 255
.byte 255
.byte 255
.byte 255
.byte 255
.byte 255
.byte 128
.byte 255
.byte 255
.byte 255
.byte 255
.byte 255
.byte 255
.byte 255
.byte 192
.byte 255
.byte 255
.byte 255
.byte 255
.byte 255
.byte 255
.byte 255
example.foo:
vpbroadcastq zmm0, rdi
vpsubusb zmm0, zmm0, zmmword ptr [rip + .LCPI0_0]
ret
This is kinda two optimizations for the price of one.
- We can lower
llvm.usub.sat.v8i64tollvm.usub.sat.v64i8because we are operating on u64's that definitely fit within a byte. - We can then fold the
u64 & 255intollvm.usub.sat.v64i8by placing 255's in the upper 7 bytes of each u64 lane.
We can either handle this in X86ISelLowering.cpp LowerADDSAT_SUBSAT or try to do this generically in a DAG combine or legalisation
Hi @RKSimon. I would love to work on this!
Hey @tharunvk, are you still active on this issue? If not, I’m interested in picking it up!
Yes. Done with the fix, putting the tests together. Will be pushing the PR today
@levi42x https://github.com/llvm/llvm-project/issues?q=is%3Aissue%20state%3Aopen%20label%3A%22good%20first%20issue%22%20no%3Aassignee%20sort%3Aupdated-desc
@RKSimon Thank you so much.
@RKSimon as it had been a long time and their is no response to changes required I will be more than happy to work on this issue .
reopening - #206592 only handles the basic narrowing, not the OR case
Hi @RKSimon,
- Now about the second part as we want to avoid TLI hook would it be acceptable to implement the OR-mask case as a target-specific lowering in X86ISelLowering — gated to AVX512 where the broadcast constant can fold into the OR as a memory operand and there's no extra register cost?
If this plan seems good then I will start working on it
If you can, I'd actually start by testing it as a generic fold. I'm not convinced the broadcast should be a requirement tbh.