llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[AVX-512] `splat(u64 & 255) -| u64s_that_fit_in_bytes` should lower to `vpsubusb`

Open Validark opened this issue 7 months ago • 13 comments

Zig Godbolt LLVM Godbolt

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.

  1. We can lower llvm.usub.sat.v8i64 to llvm.usub.sat.v64i8 because we are operating on u64's that definitely fit within a byte.
  2. We can then fold the u64 & 255 into llvm.usub.sat.v64i8 by placing 255's in the upper 7 bytes of each u64 lane.

Validark avatar May 02 '26 15:05 Validark

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:

  1. 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.
  2. 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.
  3. 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.
  4. Run the test suite locally. Remember that the subdirectories under test/ create fine-grained testing targets, so you can e.g. use make check-clang-ast to only run Clang's AST tests.
  5. Create a Git commit.
  6. Run git clang-format HEAD~1 to format your changes.
  7. 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)

[Zig Godbolt](https://zig.godbo.lt/#g:!((g:!((g:!((h:codeEditor,i:(filename:'1',fontScale:16,fontUsePx:'0',j:1,lang:zig,selection:(endColumn:24,endLineNumber:5,positionColumn:9,positionLineNumber:5,selectionStartColumn:24,selectionStartLineNumber:5,startColumn:9,startLineNumber:5),source:'const+std+%3D+@import(%22std%22)%3B%0A%0Aexport+fn+foo(len:+u64)+@Vector(8,+u64)+%7B%0A++++return+@as(@Vector(8,+u64),+@splat(len+%26+0xFF))+-%7C%0A++++++++@Vector(8,+u64)%7B+0,+64,+128,+192,+0,+64,+128,+192+%7D%3B%0A%7D%0A%0Aexport+fn+bar(len:+u64)+@Vector(8,+u64)+%7B%0A++++const+C+%3D+~@as(u64,+0)+%3C%3C+8%3B%0A++++return+@bitCast(@as(@Vector(64,+u8),+@bitCast(@as(@Vector(8,+u64),+@splat(len))))+-%7C%0A++++++++@as(@Vector(64,+u8),+@bitCast(@as(@Vector(8,+u64),+@splat(C))+%7C+@Vector(8,+u64)%7B+0,+64,+128,+192,+0,+64,+128,+192+%7D)))%3B%0A%7D'),l:'5',n:'0',o:'Zig+source+%231',t:'0')),k:51.88636954553198,l:'4',m:100,n:'0',o:'',s:0,t:'0'),(g:!((g:!((h:compiler,i:(compiler:ztrunk,filters:(b:'0',binary:'1',binaryObject:'1',commentOnly:'0',debugCalls:'1',demangle:'0',directives:'0',execute:'1',intel:'0',libraryCode:'0',trim:'1',verboseDemangling:'0'),flagsViewOpen:'1',fontScale:16,fontUsePx:'0',j:2,lang:zig,libs:!(),options:'-O+ReleaseFast+-target+x86_64-linux+-mcpu%3Dznver5+-fomit-frame-pointer',overrides:!(),selection:(endColumn:17,endLineNumber:68,positionColumn:17,positionLineNumber:68,selectionStartColumn:9,selectionStartLineNumber:68,startColumn:9,startLineNumber:68),source:1),l:'5',n:'0',o:'+zig+trunk+(Editor+%231)',t:'0')),header:(),k:48.11363045446803,l:'4',m:50,n:'0',o:'',s:0,t:'0'),(g:!((h:ir,i:('-fno-discard-value-names':'0',compilerName:'zig+trunk',demangle-symbols:'0',editorid:1,filter-attributes:'0',filter-comments:'0',filter-debug-info:'0',filter-declarations:'1',filter-instruction-metadata:'0',filter-library-functions:'1',fontScale:14,fontUsePx:'0',j:2,selection:(endColumn:1,endLineNumber:1,positionColumn:1,positionLineNumber:1,selectionStartColumn:1,selectionStartLineNumber:1,startColumn:1,startLineNumber:1),show-optimized:'0',treeid:0,wrap:'1'),l:'5',n:'0',o:'LLVM+IR+Viewer+zig+trunk+(Editor+%231,+Compiler+%232)',t:'0')),header:(),l:'4',m:50,n:'0',o:'',s:0,t:'0')),k:48.11363045446803,l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wtrmKnZDLuABaKMKp2drwzajqmu6zqBv6vrep6q9pEGkauqmobRsmsbZumhav2W1b5vW4a1s2waP22va5v2paNoOu4WqOw7FsumbrrG3aTvO%2B6rq2x6VuOh73qet7Lruj6bq%2Bv7hr/YyBVdIKCBCjSyoyirC3AyKwvi7sdkAyH0RssCBUdSDoKomEsGQBDMFq6H6snbKMLy7DCskYrSqcOrKuweyGb/fkKI4RZaE4AVeC8DgtFIVBOACAIADUAFkdgASQAJR2UMVjWNmeFIAhNA5xYAGtvC4cFJA0J8NA0LgnwFJ4n18Lgnn0ThJF59XBc4XgFBAW01f5jnSDgWAkDQYk6GichKD9hIA5iEwbGIMVNb4Oh6OIF2IAiB2guYPVOBV1O6l1AB5CJtCzDPeD99Sc4YWh9Q90gsAiNxgCcMRaBd7heCwFhDGAcQq/wQCqkzZuBeBSo3HoovyAZLmq9EiJiGzlwsAd/48BYMfM2ICJkkwI4vQ70SjHVxZ0yYYAFFFvBMAAdxzhJGDH/hBBEMR2CkGRBEUFR1Cr3QowMffsv0PAEQXaQEWKgP4aRm6tRzomVqLBkAJDcGaRsDA1480FmvYgeAsDAIgIsCoVR7AQEcKMLwUZAjTGKKUPQuRUgCBIdQ5ItCGC9EoQMKM%2BCOhdBGK4JoegOHVEmCw/oMR2GTHoaI7oQjZgiLwcsVYz9Obc3tlXIWHAdgRw%2BP8aOIoIC4EIFZciXB5i8HdloeYWsQAChuOCAUhsnxPhuBoSQBwNDFhuAcAUNsOB21ICvLgRtSB8wFqo52rtVYH0URwWIyjglO3CR7cxpA14pHsJIIAA%3D%3D)
define internal &lt;8 x i64&gt; @<!-- -->example.foo(i64 %0) unnamed_addr align 1 {
Entry:
  %1 = and i64 %0, 255
  %2 = insertelement &lt;1 x i64&gt; poison, i64 %1, i64 0
  %3 = shufflevector &lt;1 x i64&gt; %2, &lt;1 x i64&gt; poison, &lt;8 x i32&gt; zeroinitializer
  %4 = tail call &lt;8 x i64&gt; @<!-- -->llvm.usub.sat.v8i64(&lt;8 x i64&gt; %3, &lt;8 x i64&gt; &lt;i64 0, i64 64, i64 128, i64 192, i64 0, i64 64, i64 128, i64 192&gt;)
  ret &lt;8 x i64&gt; %4
}

declare &lt;8 x i64&gt; @<!-- -->llvm.usub.sat.v8i64(&lt;8 x i64&gt;, &lt;8 x i64&gt;) #<!-- -->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.

  1. We can lower llvm.usub.sat.v8i64 to llvm.usub.sat.v64i8 because we are operating on u64's that definitely fit within a byte.
  2. We can then fold the u64 &amp; 255 into llvm.usub.sat.v64i8 by placing 255's in the upper 7 bytes of each u64 lane.

@llvm/issue-subscribers-good-first-issue

Author: Niles Salter (Validark)

[Zig Godbolt](https://zig.godbo.lt/#g:!((g:!((g:!((h:codeEditor,i:(filename:'1',fontScale:16,fontUsePx:'0',j:1,lang:zig,selection:(endColumn:24,endLineNumber:5,positionColumn:9,positionLineNumber:5,selectionStartColumn:24,selectionStartLineNumber:5,startColumn:9,startLineNumber:5),source:'const+std+%3D+@import(%22std%22)%3B%0A%0Aexport+fn+foo(len:+u64)+@Vector(8,+u64)+%7B%0A++++return+@as(@Vector(8,+u64),+@splat(len+%26+0xFF))+-%7C%0A++++++++@Vector(8,+u64)%7B+0,+64,+128,+192,+0,+64,+128,+192+%7D%3B%0A%7D%0A%0Aexport+fn+bar(len:+u64)+@Vector(8,+u64)+%7B%0A++++const+C+%3D+~@as(u64,+0)+%3C%3C+8%3B%0A++++return+@bitCast(@as(@Vector(64,+u8),+@bitCast(@as(@Vector(8,+u64),+@splat(len))))+-%7C%0A++++++++@as(@Vector(64,+u8),+@bitCast(@as(@Vector(8,+u64),+@splat(C))+%7C+@Vector(8,+u64)%7B+0,+64,+128,+192,+0,+64,+128,+192+%7D)))%3B%0A%7D'),l:'5',n:'0',o:'Zig+source+%231',t:'0')),k:51.88636954553198,l:'4',m:100,n:'0',o:'',s:0,t:'0'),(g:!((g:!((h:compiler,i:(compiler:ztrunk,filters:(b:'0',binary:'1',binaryObject:'1',commentOnly:'0',debugCalls:'1',demangle:'0',directives:'0',execute:'1',intel:'0',libraryCode:'0',trim:'1',verboseDemangling:'0'),flagsViewOpen:'1',fontScale:16,fontUsePx:'0',j:2,lang:zig,libs:!(),options:'-O+ReleaseFast+-target+x86_64-linux+-mcpu%3Dznver5+-fomit-frame-pointer',overrides:!(),selection:(endColumn:17,endLineNumber:68,positionColumn:17,positionLineNumber:68,selectionStartColumn:9,selectionStartLineNumber:68,startColumn:9,startLineNumber:68),source:1),l:'5',n:'0',o:'+zig+trunk+(Editor+%231)',t:'0')),header:(),k:48.11363045446803,l:'4',m:50,n:'0',o:'',s:0,t:'0'),(g:!((h:ir,i:('-fno-discard-value-names':'0',compilerName:'zig+trunk',demangle-symbols:'0',editorid:1,filter-attributes:'0',filter-comments:'0',filter-debug-info:'0',filter-declarations:'1',filter-instruction-metadata:'0',filter-library-functions:'1',fontScale:14,fontUsePx:'0',j:2,selection:(endColumn:1,endLineNumber:1,positionColumn:1,positionLineNumber:1,selectionStartColumn:1,selectionStartLineNumber:1,startColumn:1,startLineNumber:1),show-optimized:'0',treeid:0,wrap:'1'),l:'5',n:'0',o:'LLVM+IR+Viewer+zig+trunk+(Editor+%231,+Compiler+%232)',t:'0')),header:(),l:'4',m:50,n:'0',o:'',s:0,t:'0')),k:48.11363045446803,l:'3',n:'0',o:'',t:'0')),l:'2',m:100,n:'0',o:'',t:'0')),version:4) [LLVM Godbolt](https://llvm.godbo.lt/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1C1aANxakl9ZATwDKjdAGFUtAK4sGISQHZSjgBk8BkwAOQ8AI0xiEAAmUgAHVAVCOwYXd09vPySU2wEgkPCWKJj4q0wbNKECJmICDI8vX0tMa3yGGrqCQrDI6LjLWvrGrJaFYZ7gvpKB2IBKS1Q3YmR2DgABflQAagBSAGYAER2xPCYFfYOnAA4d1R28ADZJQ%2BwdiGfJRZ2EgmIdhtMKpWAl6AA6bZ7DQAQQ2ETqVxOZwuV1u90eLzeHy%2BPz%2BAKBIJYYMw4IRxGhMMpWBoIUegmizFoaLuDy%2B2MJoIh20%2BL32sQArBp5js3AxmGx0AB9JjodAAs7ABg7Lj7HwAIUp2EExAAniBKTt%2BQLVYdkQx0JjJMaNKQdoKBYbjbEkfSlPU2pg2II0aq2ViDu8kngFAI7V9jVxw3yNE69oKDq6FAg3FQqPRTJUiADDk4/VbsfGBfFfRj2YHfqgQ2GWWWDrFsQAvaJVhipM7Nimwo1F61mna1Og7UQmWv%2B14VjYmczgtwKN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9Obc3tlXIWHAdgRw%2BP8aOIoIC4EIFZciXB5i8HdloeYWsQAChuOCAUhsnxPhuBoSQBwNDFhuAcAUNsOB21ICvLgRtSB8wFqo52rtVYH0URwWIyjglO3CR7cxpA14pHsJIIAA%3D%3D)
define internal &lt;8 x i64&gt; @<!-- -->example.foo(i64 %0) unnamed_addr align 1 {
Entry:
  %1 = and i64 %0, 255
  %2 = insertelement &lt;1 x i64&gt; poison, i64 %1, i64 0
  %3 = shufflevector &lt;1 x i64&gt; %2, &lt;1 x i64&gt; poison, &lt;8 x i32&gt; zeroinitializer
  %4 = tail call &lt;8 x i64&gt; @<!-- -->llvm.usub.sat.v8i64(&lt;8 x i64&gt; %3, &lt;8 x i64&gt; &lt;i64 0, i64 64, i64 128, i64 192, i64 0, i64 64, i64 128, i64 192&gt;)
  ret &lt;8 x i64&gt; %4
}

declare &lt;8 x i64&gt; @<!-- -->llvm.usub.sat.v8i64(&lt;8 x i64&gt;, &lt;8 x i64&gt;) #<!-- -->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.

  1. We can lower llvm.usub.sat.v8i64 to llvm.usub.sat.v64i8 because we are operating on u64's that definitely fit within a byte.
  2. We can then fold the u64 &amp; 255 into llvm.usub.sat.v64i8 by 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

RKSimon avatar May 03 '26 11:05 RKSimon

Hi @RKSimon. I would love to work on this!

tharunvk avatar May 03 '26 11:05 tharunvk

Hey @tharunvk, are you still active on this issue? If not, I’m interested in picking it up!

levi42x avatar May 06 '26 13:05 levi42x

Yes. Done with the fix, putting the tests together. Will be pushing the PR today

tharunvk avatar May 06 '26 16:05 tharunvk

@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 avatar May 06 '26 16:05 RKSimon

@RKSimon Thank you so much.

levi42x avatar May 06 '26 18:05 levi42x

@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 .

sipher-01 avatar Jun 28 '26 12:06 sipher-01

reopening - #206592 only handles the basic narrowing, not the OR case

RKSimon avatar Aug 19 '26 10:08 RKSimon

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

sipher-01 avatar Aug 20 '26 19:08 sipher-01

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.

RKSimon avatar Aug 20 '26 20:08 RKSimon