Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[AVX2] vectorized a == (a & b) should not be switched to 0 != (a & b) when it doesn't help #110875

Open
Validark opened this issue Oct 2, 2024 · 1 comment
Assignees

Comments

@Validark
Copy link

Validark commented Oct 2, 2024

In trying to work around #110868, I wrote this code:

LLVM Godbolt
Zig Godbolt

Zig version:

const std = @import("std");

fn unmovemask64(x: u64) @Vector(64, bool) {
    const bit_positions = comptime std.simd.repeat(64, @as(@Vector(8, u8), @splat(1)) << std.simd.iota(u3, 8));
    return bit_positions == (bit_positions & @shuffle(u8, @as(@Vector(64, u8), @bitCast(@as(@Vector(8, u64), @splat(x)))), undefined, (std.simd.iota(u8, 64) >> @splat(4) << @splat(4)) + (std.simd.iota(u8, 64) >> @splat(3))));
}

export fn bar(kinds_1: @Vector(32, u8), kinds_2: @Vector(32, u8), comment_starts: u64) @Vector(64, u8) {
    const kinds = std.simd.join(kinds_1, kinds_2); // helps the C ABI
    return @select(u8, unmovemask64(comment_starts), @as(@Vector(64, u8), @splat(20)), kinds);
}

LLVM version:

define dso_local <64 x i8> @bar(<32 x i8> %0, <32 x i8> %1, i64 %2) local_unnamed_addr {
Entry:
  %3 = shufflevector <32 x i8> %0, <32 x i8> %1, <64 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7, i32 8, i32 9, i32 10, i32 11, i32 12, i32 13, i32 14, i32 15, i32 16, i32 17, i32 18, i32 19, i32 20, i32 21, i32 22, i32 23, i32 24, i32 25, i32 26, i32 27, i32 28, i32 29, i32 30, i32 31, i32 32, i32 33, i32 34, i32 35, i32 36, i32 37, i32 38, i32 39, i32 40, i32 41, i32 42, i32 43, i32 44, i32 45, i32 46, i32 47, i32 48, i32 49, i32 50, i32 51, i32 52, i32 53, i32 54, i32 55, i32 56, i32 57, i32 58, i32 59, i32 60, i32 61, i32 62, i32 63>
  %4 = insertelement <1 x i64> poison, i64 %2, i64 0
  %5 = shufflevector <1 x i64> %4, <1 x i64> poison, <8 x i32> <i32 0, i32 poison, i32 0, i32 poison, i32 0, i32 poison, i32 0, i32 poison>
  %6 = bitcast <8 x i64> %5 to <64 x i8>
  %7 = shufflevector <64 x i8> %6, <64 x i8> poison, <64 x i32> <i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 18, i32 18, i32 18, i32 18, i32 18, i32 18, i32 18, i32 18, i32 19, i32 19, i32 19, i32 19, i32 19, i32 19, i32 19, i32 19, i32 36, i32 36, i32 36, i32 36, i32 36, i32 36, i32 36, i32 36, i32 37, i32 37, i32 37, i32 37, i32 37, i32 37, i32 37, i32 37, i32 54, i32 54, i32 54, i32 54, i32 54, i32 54, i32 54, i32 54, i32 55, i32 55, i32 55, i32 55, i32 55, i32 55, i32 55, i32 55>
  %8 = and <64 x i8> %7, <i8 1, i8 2, i8 4, i8 8, i8 16, i8 32, i8 64, i8 -128, i8 1, i8 2, i8 4, i8 8, i8 16, i8 32, i8 64, i8 -128, i8 1, i8 2, i8 4, i8 8, i8 16, i8 32, i8 64, i8 -128, i8 1, i8 2, i8 4, i8 8, i8 16, i8 32, i8 64, i8 -128, i8 1, i8 2, i8 4, i8 8, i8 16, i8 32, i8 64, i8 -128, i8 1, i8 2, i8 4, i8 8, i8 16, i8 32, i8 64, i8 -128, i8 1, i8 2, i8 4, i8 8, i8 16, i8 32, i8 64, i8 -128, i8 1, i8 2, i8 4, i8 8, i8 16, i8 32, i8 64, i8 -128>
  %.not = icmp eq <64 x i8> %8, zeroinitializer
  %9 = select <64 x i1> %.not, <64 x i8> %3, <64 x i8> <i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20>
  ret <64 x i8> %9
}

The Zen 3 assembly is:

.LCPI0_0:
        .byte   4
        .byte   4
        .byte   4
        .byte   4
        .byte   4
        .byte   4
        .byte   4
        .byte   4
        .byte   5
        .byte   5
        .byte   5
        .byte   5
        .byte   5
        .byte   5
        .byte   5
        .byte   5
        .byte   6
        .byte   6
        .byte   6
        .byte   6
        .byte   6
        .byte   6
        .byte   6
        .byte   6
        .byte   7
        .byte   7
        .byte   7
        .byte   7
        .byte   7
        .byte   7
        .byte   7
        .byte   7
.LCPI0_1:
        .byte   0
        .byte   0
        .byte   0
        .byte   0
        .byte   0
        .byte   0
        .byte   0
        .byte   0
        .byte   1
        .byte   1
        .byte   1
        .byte   1
        .byte   1
        .byte   1
        .byte   1
        .byte   1
        .byte   2
        .byte   2
        .byte   2
        .byte   2
        .byte   2
        .byte   2
        .byte   2
        .byte   2
        .byte   3
        .byte   3
        .byte   3
        .byte   3
        .byte   3
        .byte   3
        .byte   3
        .byte   3
        .zero   32,20
.LCPI0_2:
        .byte   1
        .byte   2
        .byte   4
        .byte   8
        .byte   16
        .byte   32
        .byte   64
        .byte   128
.LCPI0_4:
        .byte   20
bar:
        vmovq   xmm2, rdi
        vpbroadcastq    ymm4, qword ptr [rip + .LCPI0_2]
        vpbroadcastb    ymm5, byte ptr [rip + .LCPI0_4]
        vpbroadcastq    ymm2, xmm2
        vpshufb ymm3, ymm2, ymmword ptr [rip + .LCPI0_0]
        vpshufb ymm2, ymm2, ymmword ptr [rip + .LCPI0_1]
        vpand   ymm2, ymm2, ymm4
        vpand   ymm3, ymm3, ymm4
-       vpxor   xmm4, xmm4, xmm4
        vpcmpeqb        ymm3, ymm3, ymm4
        vpcmpeqb        ymm2, ymm2, ymm4
        vpblendvb       ymm0, ymm0, ymm5, ymm2 ; deletion of the `vpxor` instruction requires that
        vpblendvb       ymm1, ymm1, ymm5, ymm3 ; the 2nd and 3rd arguments should be switched
        ret
@llvmbot
Copy link
Collaborator

llvmbot commented Oct 2, 2024

@llvm/issue-subscribers-backend-x86

Author: Niles Salter (Validark)

In trying to work around https://github.com//issues/110868, I wrote this code:

LLVM Godbolt
[Zig Godbolt](https://zig.godbolt.org/#g:!((g:!((g:!((h:codeEditor,i:(filename:'1',fontScale:13,fontUsePx:'0',j:3,lang:zig,selection:(endColumn:1,endLineNumber:12,positionColumn:1,positionLineNumber:12,selectionStartColumn:1,selectionStartLineNumber:12,startColumn:1,startLineNumber:12),source:'const+std+%3D+@import(%22std%22)%3B%0A%0Afn+unmovemask64(x:+u64)+@Vector(64,+bool)+%7B%0A++++const+bit_positions+%3D+comptime+std.simd.repeat(64,+@as(@Vector(8,+u8),+@splat(1))+%3C%3C+std.simd.iota(u3,+8))%3B%0A++++return+bit_positions+%3D%3D+(bit_positions+%26+@shuffle(u8,+@as(@Vector(64,+u8),+@bitCast(@as(@Vector(8,+u64),+@splat(x)))),+undefined,+(std.simd.iota(u8,+64)+%3E%3E+@splat(4)+%3C%3C+@splat(4))+%2B+(std.simd.iota(u8,+64)+%3E%3E+@splat(3))))%3B%0A%7D%0A%0Aexport+fn+bar(kinds_1:+@Vector(32,+u8),+kinds_2:+@Vector(32,+u8),+comment_starts:+u64)+@Vector(64,+u8)+%7B%0A++++const+kinds+%3D+std.simd.join(kinds_1,+kinds_2)%3B+//+helps+the+C+ABI%0A++++return+@select(u8,+unmovemask64(comment_starts),+@as(@Vector(64,+u8),+@splat(20)),+kinds)%3B%0A%7D%0A'),l:'5',n:'1',o:'Zig+source+%233',t:'0')),header:(),k:54.37591923954271,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:14,fontUsePx:'0',j:1,lang:zig,libs:!(),options:'-O+ReleaseFast+-target+x86_64-linux+-mcpu%3Dznver3',overrides:!(),selection:(endColumn:1,endLineNumber:1,positionColumn:1,positionLineNumber:1,selectionStartColumn:1,selectionStartLineNumber:1,startColumn:1,startLineNumber:1),source:3),l:'5',n:'0',o:'+zig+trunk+(Editor+%233)',t:'0')),header:(),k:45.62408076045729,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:3,filter-attributes:'0',filter-comments:'0',filter-debug-info:'0',filter-instruction-metadata:'0',fontScale:14,fontUsePx:'0',j:1,selection:(endColumn:1,endLineNumber:1,positionColumn:1,positionLineNumber:1,selectionStartColumn:1,selectionStartLineNumber:1,startColumn:1,startLineNumber:1),treeid:0,wrap:'1'),l:'5',n:'0',o:'LLVM+IR+Viewer+zig+trunk+(Editor+%233,+Compiler+%231)',t:'0')),header:(),l:'4',m:50,n:'0',o:'',s:0,t:'0')),k:45.62408076045729,l:'3',n:'0',o:'',t:'0')),l:'2',n:'0',o:'',t:'0')),version:4)

Zig version:

const std = @<!-- -->import("std");

fn unmovemask64(x: u64) @<!-- -->Vector(64, bool) {
    const bit_positions = comptime std.simd.repeat(64, @<!-- -->as(@<!-- -->Vector(8, u8), @<!-- -->splat(1)) &lt;&lt; std.simd.iota(u3, 8));
    return bit_positions == (bit_positions &amp; @<!-- -->shuffle(u8, @<!-- -->as(@<!-- -->Vector(64, u8), @<!-- -->bitCast(@<!-- -->as(@<!-- -->Vector(8, u64), @<!-- -->splat(x)))), undefined, (std.simd.iota(u8, 64) &gt;&gt; @<!-- -->splat(4) &lt;&lt; @<!-- -->splat(4)) + (std.simd.iota(u8, 64) &gt;&gt; @<!-- -->splat(3))));
}

export fn bar(kinds_1: @<!-- -->Vector(32, u8), kinds_2: @<!-- -->Vector(32, u8), comment_starts: u64) @<!-- -->Vector(64, u8) {
    const kinds = std.simd.join(kinds_1, kinds_2); // helps the C ABI
    return @<!-- -->select(u8, unmovemask64(comment_starts), @<!-- -->as(@<!-- -->Vector(64, u8), @<!-- -->splat(20)), kinds);
}

LLVM version:

define dso_local &lt;64 x i8&gt; @<!-- -->bar(&lt;32 x i8&gt; %0, &lt;32 x i8&gt; %1, i64 %2) local_unnamed_addr {
Entry:
  %3 = shufflevector &lt;32 x i8&gt; %0, &lt;32 x i8&gt; %1, &lt;64 x i32&gt; &lt;i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7, i32 8, i32 9, i32 10, i32 11, i32 12, i32 13, i32 14, i32 15, i32 16, i32 17, i32 18, i32 19, i32 20, i32 21, i32 22, i32 23, i32 24, i32 25, i32 26, i32 27, i32 28, i32 29, i32 30, i32 31, i32 32, i32 33, i32 34, i32 35, i32 36, i32 37, i32 38, i32 39, i32 40, i32 41, i32 42, i32 43, i32 44, i32 45, i32 46, i32 47, i32 48, i32 49, i32 50, i32 51, i32 52, i32 53, i32 54, i32 55, i32 56, i32 57, i32 58, i32 59, i32 60, i32 61, i32 62, i32 63&gt;
  %4 = insertelement &lt;1 x i64&gt; poison, i64 %2, i64 0
  %5 = shufflevector &lt;1 x i64&gt; %4, &lt;1 x i64&gt; poison, &lt;8 x i32&gt; &lt;i32 0, i32 poison, i32 0, i32 poison, i32 0, i32 poison, i32 0, i32 poison&gt;
  %6 = bitcast &lt;8 x i64&gt; %5 to &lt;64 x i8&gt;
  %7 = shufflevector &lt;64 x i8&gt; %6, &lt;64 x i8&gt; poison, &lt;64 x i32&gt; &lt;i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 18, i32 18, i32 18, i32 18, i32 18, i32 18, i32 18, i32 18, i32 19, i32 19, i32 19, i32 19, i32 19, i32 19, i32 19, i32 19, i32 36, i32 36, i32 36, i32 36, i32 36, i32 36, i32 36, i32 36, i32 37, i32 37, i32 37, i32 37, i32 37, i32 37, i32 37, i32 37, i32 54, i32 54, i32 54, i32 54, i32 54, i32 54, i32 54, i32 54, i32 55, i32 55, i32 55, i32 55, i32 55, i32 55, i32 55, i32 55&gt;
  %8 = and &lt;64 x i8&gt; %7, &lt;i8 1, i8 2, i8 4, i8 8, i8 16, i8 32, i8 64, i8 -128, i8 1, i8 2, i8 4, i8 8, i8 16, i8 32, i8 64, i8 -128, i8 1, i8 2, i8 4, i8 8, i8 16, i8 32, i8 64, i8 -128, i8 1, i8 2, i8 4, i8 8, i8 16, i8 32, i8 64, i8 -128, i8 1, i8 2, i8 4, i8 8, i8 16, i8 32, i8 64, i8 -128, i8 1, i8 2, i8 4, i8 8, i8 16, i8 32, i8 64, i8 -128, i8 1, i8 2, i8 4, i8 8, i8 16, i8 32, i8 64, i8 -128, i8 1, i8 2, i8 4, i8 8, i8 16, i8 32, i8 64, i8 -128&gt;
  %.not = icmp eq &lt;64 x i8&gt; %8, zeroinitializer
  %9 = select &lt;64 x i1&gt; %.not, &lt;64 x i8&gt; %3, &lt;64 x i8&gt; &lt;i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20, i8 20&gt;
  ret &lt;64 x i8&gt; %9
}

The Zen 3 assembly is:

.LCPI0_0:
        .byte   4
        .byte   4
        .byte   4
        .byte   4
        .byte   4
        .byte   4
        .byte   4
        .byte   4
        .byte   5
        .byte   5
        .byte   5
        .byte   5
        .byte   5
        .byte   5
        .byte   5
        .byte   5
        .byte   6
        .byte   6
        .byte   6
        .byte   6
        .byte   6
        .byte   6
        .byte   6
        .byte   6
        .byte   7
        .byte   7
        .byte   7
        .byte   7
        .byte   7
        .byte   7
        .byte   7
        .byte   7
.LCPI0_1:
        .byte   0
        .byte   0
        .byte   0
        .byte   0
        .byte   0
        .byte   0
        .byte   0
        .byte   0
        .byte   1
        .byte   1
        .byte   1
        .byte   1
        .byte   1
        .byte   1
        .byte   1
        .byte   1
        .byte   2
        .byte   2
        .byte   2
        .byte   2
        .byte   2
        .byte   2
        .byte   2
        .byte   2
        .byte   3
        .byte   3
        .byte   3
        .byte   3
        .byte   3
        .byte   3
        .byte   3
        .byte   3
        .zero   32,20
.LCPI0_2:
        .byte   1
        .byte   2
        .byte   4
        .byte   8
        .byte   16
        .byte   32
        .byte   64
        .byte   128
.LCPI0_4:
        .byte   20
bar:
        vmovq   xmm2, rdi
        vpbroadcastq    ymm4, qword ptr [rip + .LCPI0_2]
        vpbroadcastb    ymm5, byte ptr [rip + .LCPI0_4]
        vpbroadcastq    ymm2, xmm2
        vpshufb ymm3, ymm2, ymmword ptr [rip + .LCPI0_0]
        vpshufb ymm2, ymm2, ymmword ptr [rip + .LCPI0_1]
        vpand   ymm2, ymm2, ymm4
        vpand   ymm3, ymm3, ymm4
-       vpxor   xmm4, xmm4, xmm4
        vpcmpeqb        ymm3, ymm3, ymm4
        vpcmpeqb        ymm2, ymm2, ymm4
        vpblendvb       ymm0, ymm0, ymm5, ymm2 ; deletion of the `vpxor` instruction requires that
        vpblendvb       ymm1, ymm1, ymm5, ymm3 ; the 2nd and 3rd arguments should be switched
        ret

@RKSimon RKSimon self-assigned this Oct 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants