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

[AVX-512] Random unneeded vector zeroing? #115091

Closed
Validark opened this issue Nov 5, 2024 · 2 comments
Closed

[AVX-512] Random unneeded vector zeroing? #115091

Validark opened this issue Nov 5, 2024 · 2 comments
Labels
backend:X86 question A question, not bug report. Check out https://llvm.org/docs/GettingInvolved.html instead!

Comments

@Validark
Copy link

Validark commented Nov 5, 2024

Godbolt

const std = @import("std");
const builtin = @import("builtin");

fn unmovemask64(x: u64) @Vector(64, bool) {
    if (comptime std.Target.x86.featureSetHas(builtin.cpu.features, .avx512bw)) {
        return @bitCast(x);
    }

    const bit_positions = comptime std.simd.repeat(64, @as(@Vector(8, u8), @splat(1)) << std.simd.iota(u3, 8));
    const shuffled_x = @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)));
    const T = [2]@Vector(32, u8);
    var bit_positions_and_shuffled_x: T = undefined;

    // Works around https://github.com/llvm/llvm-project/issues/110875
    // Helps sandybridge (avx) and goldmont (sse4_2) targets too
    for (&bit_positions_and_shuffled_x, @as(T, @bitCast(bit_positions)), @as(T, @bitCast(shuffled_x))) |*slot, a, b| {
        slot.* = a & b;
    }

    return bit_positions == @as(@Vector(64, u8), @bitCast(bit_positions_and_shuffled_x));
}

fn prefixSum(vec_: @Vector(64, u8)) @Vector(64, u8) {
    var vec = vec_;
    vec = @bitCast(@as(@Vector(16, u32), @bitCast(vec)) * @as(@Vector(16, u32), @splat((1 << 0*8) + (1 << 1*8) + (1 << 2*8))));
    //vec += std.simd.shiftElementsRight(vec, 1, 0); vec += std.simd.shiftElementsRight(vec, 2, 0);
    vec += std.simd.shiftElementsRight(vec, 4, 0);
    vec += std.simd.shiftElementsRight(vec, 8, 0);
    vec += std.simd.shiftElementsRight(vec, 16, 0);
    return vec + std.simd.shiftElementsRight(vec, 32, 0);
}

export fn unmovemask64u8(x: u64) @Vector(64, u8) {
    const zeroes: @Vector(64, u8) = @splat(0);
    const ones: @Vector(64, u8) = @splat(0xFF);
    return prefixSum(@select(u8, unmovemask64(x), ones, zeroes));
}

Godbolt

define dso_local <64 x i8> @unmovemask64u8(i64 %0) local_unnamed_addr {
Entry:
  %1 = insertelement <1 x i64> poison, i64 %0, i64 0
  %2 = shufflevector <1 x i64> %1, <1 x i64> poison, <8 x i32> <i32 0, i32 poison, i32 0, i32 poison, i32 0, i32 poison, i32 0, i32 poison>
  %3 = bitcast <8 x i64> %2 to <64 x i8>
  %4 = shufflevector <64 x i8> %3, <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>
  %.sroa.03.0.vec.extract.i = shufflevector <64 x i8> %4, <64 x i8> poison, <32 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>
  %5 = and <32 x i8> %.sroa.03.0.vec.extract.i, <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>
  %.sroa.03.32.vec.extract.i = shufflevector <64 x i8> %4, <64 x i8> poison, <32 x i32> <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>
  %6 = and <32 x i8> %.sroa.03.32.vec.extract.i, <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>
  %.sroa.0.32.vecblend.i = shufflevector <32 x i8> %5, <32 x i8> %6, <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>
  %7 = icmp eq <64 x i8> %.sroa.0.32.vecblend.i, <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>
  %8 = sext <64 x i1> %7 to <64 x i8>
  %9 = bitcast <64 x i8> %8 to <16 x i32>
  %10 = mul nuw <16 x i32> %9, <i32 65793, i32 65793, i32 65793, i32 65793, i32 65793, i32 65793, i32 65793, i32 65793, i32 65793, i32 65793, i32 65793, i32 65793, i32 65793, i32 65793, i32 65793, i32 65793>
  %11 = bitcast <16 x i32> %10 to <64 x i8>
  %12 = shufflevector <64 x i8> <i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 0, i8 0, i8 0, i8 0>, <64 x i8> %11, <64 x i32> <i32 60, i32 61, i32 62, i32 63, i32 64, i32 65, i32 66, i32 67, i32 68, i32 69, i32 70, i32 71, i32 72, i32 73, i32 74, i32 75, i32 76, i32 77, i32 78, i32 79, i32 80, i32 81, i32 82, i32 83, i32 84, i32 85, i32 86, i32 87, i32 88, i32 89, i32 90, i32 91, i32 92, i32 93, i32 94, i32 95, i32 96, i32 97, i32 98, i32 99, i32 100, i32 101, i32 102, i32 103, i32 104, i32 105, i32 106, i32 107, i32 108, i32 109, i32 110, i32 111, i32 112, i32 113, i32 114, i32 115, i32 116, i32 117, i32 118, i32 119, i32 120, i32 121, i32 122, i32 123>
  %13 = add nuw <64 x i8> %12, %11
  %14 = shufflevector <64 x i8> <i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0>, <64 x i8> %13, <64 x i32> <i32 56, i32 57, i32 58, i32 59, i32 60, i32 61, i32 62, i32 63, i32 64, i32 65, i32 66, i32 67, i32 68, i32 69, i32 70, i32 71, i32 72, i32 73, i32 74, i32 75, i32 76, i32 77, i32 78, i32 79, i32 80, i32 81, i32 82, i32 83, i32 84, i32 85, i32 86, i32 87, i32 88, i32 89, i32 90, i32 91, i32 92, i32 93, i32 94, i32 95, i32 96, i32 97, i32 98, i32 99, i32 100, i32 101, i32 102, i32 103, i32 104, i32 105, i32 106, i32 107, i32 108, i32 109, i32 110, i32 111, i32 112, i32 113, i32 114, i32 115, i32 116, i32 117, i32 118, i32 119>
  %15 = add nuw <64 x i8> %14, %13
  %16 = shufflevector <64 x i8> <i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0>, <64 x i8> %15, <64 x i32> <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, i32 64, i32 65, i32 66, i32 67, i32 68, i32 69, i32 70, i32 71, i32 72, i32 73, i32 74, i32 75, i32 76, i32 77, i32 78, i32 79, i32 80, i32 81, i32 82, i32 83, i32 84, i32 85, i32 86, i32 87, i32 88, i32 89, i32 90, i32 91, i32 92, i32 93, i32 94, i32 95, i32 96, i32 97, i32 98, i32 99, i32 100, i32 101, i32 102, i32 103, i32 104, i32 105, i32 106, i32 107, i32 108, i32 109, i32 110, i32 111>
  %17 = add nuw <64 x i8> %16, %15
  %18 = shufflevector <64 x i8> <i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0>, <64 x i8> %17, <64 x i32> <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, i32 64, i32 65, i32 66, i32 67, i32 68, i32 69, i32 70, i32 71, i32 72, i32 73, i32 74, i32 75, i32 76, i32 77, i32 78, i32 79, i32 80, i32 81, i32 82, i32 83, i32 84, i32 85, i32 86, i32 87, i32 88, i32 89, i32 90, i32 91, i32 92, i32 93, i32 94, i32 95>
  %19 = add nuw <64 x i8> %18, %17
  ret <64 x i8> %19
}

Compiled for Zen 3:

.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
.LCPI0_2:
        .byte   1
        .byte   2
        .byte   4
        .byte   8
        .byte   16
        .byte   32
        .byte   64
        .byte   128
.LCPI0_3:
        .long   65793
unmovemask64u8:
.Lunmovemask64u8$local:
        vmovq   xmm0, rdi
        vpbroadcastq    ymm2, qword ptr [rip + .LCPI0_2]
        vpbroadcastq    ymm0, xmm0
        vpshufb ymm1, ymm0, ymmword ptr [rip + .LCPI0_0]
        vpshufb ymm0, ymm0, ymmword ptr [rip + .LCPI0_1]
        vpand   ymm0, ymm0, ymm2
        vpand   ymm1, ymm1, ymm2
        vpcmpeqb        ymm0, ymm0, ymm2
        vpcmpeqb        ymm1, ymm1, ymm2
        vpbroadcastd    ymm2, dword ptr [rip + .LCPI0_3]
        vpmulld ymm0, ymm0, ymm2
        vpmulld ymm1, ymm1, ymm2
        vperm2i128      ymm2, ymm0, ymm1, 33
        vperm2i128      ymm3, ymm0, ymm0, 40
        vpalignr        ymm2, ymm1, ymm2, 12
        vpalignr        ymm3, ymm0, ymm3, 12
        vpaddb  ymm0, ymm3, ymm0
        vpaddb  ymm1, ymm2, ymm1
-       vpxor   xmm3, xmm3, xmm3
        vpblendd        ymm2, ymm1, ymm0, 192
        vpblendd        ymm3, ymm0, ymm3, 192
        vpermq  ymm2, ymm2, 147
        vpermq  ymm3, ymm3, 147
        vpaddb  ymm0, ymm3, ymm0
        vpaddb  ymm1, ymm2, ymm1
        vperm2i128      ymm2, ymm0, ymm1, 33
        vperm2i128      ymm3, ymm0, ymm0, 40
        vpaddb  ymm1, ymm2, ymm1
        vpaddb  ymm0, ymm3, ymm0
        vpaddb  ymm1, ymm0, ymm1
        ret
@Validark
Copy link
Author

Validark commented Nov 5, 2024

Made a mistake, sorry

@EugeneZelenko EugeneZelenko added question A question, not bug report. Check out https://llvm.org/docs/GettingInvolved.html instead! backend:X86 and removed new issue labels Nov 5, 2024
@llvmbot
Copy link
Collaborator

llvmbot commented Nov 5, 2024

@llvm/issue-subscribers-backend-x86

Author: Niles Salter (Validark)

[Godbolt](https://zig.godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYgATKVpMGoAF55gpJfWQE8Ayo3QBhVLQCuLBhNJOAMngMm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KoSiuK4BlSQHJJCSD0NmwwnAQmkDCWlSJ0StGcASSAJJUbSDpMQCAVZuSIAcDCsiVAYV8AKFEMQdAYVJIeEwGFZgbAFAcBqfkyIwRWCrAxV1EpDA8RjNecAegYUFBFUKG4TZvB1KhtFfocpZpIhhW1Dcn4eyd1Hr2cQMK/Tu41JoMe294z3DADCoEfgj6Km3sCGMEpZqGBcsVLG7UTBH2yEheIKQMhBCKBUOoZZyL9WAZlS4NwDQfD%2BCddMKIeb4hqpSPawmDArW4f0ByFDbQLV1AwyMKjhrOhkZVaMI1dH5VjCYzhlj8ximYEwOVatpBvTECYHEXpXqOBqKib6zgfg/BUgALKbAAJKJ02P8wFkR/XAHnUGiAIa6ThpEq4C42ThomKYpGz5CbogaClESVxXi7NulzRoVx6UC3BJ9csuJVaa02a83oUJ4Sy2yfiaQZJqT5gPKSA4SQQA%3D%3D)
const std = @<!-- -->import("std");
const builtin = @<!-- -->import("builtin");

fn unmovemask64(x: u64) @<!-- -->Vector(64, bool) {
    if (comptime std.Target.x86.featureSetHas(builtin.cpu.features, .avx512bw)) {
        return @<!-- -->bitCast(x);
    }

    const bit_positions = comptime std.simd.repeat(64, @<!-- -->as(@<!-- -->Vector(8, u8), @<!-- -->splat(1)) &lt;&lt; std.simd.iota(u3, 8));
    const shuffled_x = @<!-- -->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)));
    const T = [2]@<!-- -->Vector(32, u8);
    var bit_positions_and_shuffled_x: T = undefined;

    // Works around https://github.com/llvm/llvm-project/issues/110875
    // Helps sandybridge (avx) and goldmont (sse4_2) targets too
    for (&amp;bit_positions_and_shuffled_x, @<!-- -->as(T, @<!-- -->bitCast(bit_positions)), @<!-- -->as(T, @<!-- -->bitCast(shuffled_x))) |*slot, a, b| {
        slot.* = a &amp; b;
    }

    return bit_positions == @<!-- -->as(@<!-- -->Vector(64, u8), @<!-- -->bitCast(bit_positions_and_shuffled_x));
}

fn prefixSum(vec_: @<!-- -->Vector(64, u8)) @<!-- -->Vector(64, u8) {
    var vec = vec_;
    vec = @<!-- -->bitCast(@<!-- -->as(@<!-- -->Vector(16, u32), @<!-- -->bitCast(vec)) * @<!-- -->as(@<!-- -->Vector(16, u32), @<!-- -->splat((1 &lt;&lt; 0*8) + (1 &lt;&lt; 1*8) + (1 &lt;&lt; 2*8))));
    //vec += std.simd.shiftElementsRight(vec, 1, 0); vec += std.simd.shiftElementsRight(vec, 2, 0);
    vec += std.simd.shiftElementsRight(vec, 4, 0);
    vec += std.simd.shiftElementsRight(vec, 8, 0);
    vec += std.simd.shiftElementsRight(vec, 16, 0);
    return vec + std.simd.shiftElementsRight(vec, 32, 0);
}

export fn unmovemask64u8(x: u64) @<!-- -->Vector(64, u8) {
    const zeroes: @<!-- -->Vector(64, u8) = @<!-- -->splat(0);
    const ones: @<!-- -->Vector(64, u8) = @<!-- -->splat(0xFF);
    return prefixSum(@<!-- -->select(u8, unmovemask64(x), ones, zeroes));
}

Godbolt

define dso_local &lt;64 x i8&gt; @<!-- -->unmovemask64u8(i64 %0) local_unnamed_addr {
Entry:
  %1 = insertelement &lt;1 x i64&gt; poison, i64 %0, i64 0
  %2 = shufflevector &lt;1 x i64&gt; %1, &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;
  %3 = bitcast &lt;8 x i64&gt; %2 to &lt;64 x i8&gt;
  %4 = shufflevector &lt;64 x i8&gt; %3, &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;
  %.sroa.03.0.vec.extract.i = shufflevector &lt;64 x i8&gt; %4, &lt;64 x i8&gt; poison, &lt;32 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&gt;
  %5 = and &lt;32 x i8&gt; %.sroa.03.0.vec.extract.i, &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&gt;
  %.sroa.03.32.vec.extract.i = shufflevector &lt;64 x i8&gt; %4, &lt;64 x i8&gt; poison, &lt;32 x i32&gt; &lt;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;
  %6 = and &lt;32 x i8&gt; %.sroa.03.32.vec.extract.i, &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&gt;
  %.sroa.0.32.vecblend.i = shufflevector &lt;32 x i8&gt; %5, &lt;32 x i8&gt; %6, &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;
  %7 = icmp eq &lt;64 x i8&gt; %.sroa.0.32.vecblend.i, &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;
  %8 = sext &lt;64 x i1&gt; %7 to &lt;64 x i8&gt;
  %9 = bitcast &lt;64 x i8&gt; %8 to &lt;16 x i32&gt;
  %10 = mul nuw &lt;16 x i32&gt; %9, &lt;i32 65793, i32 65793, i32 65793, i32 65793, i32 65793, i32 65793, i32 65793, i32 65793, i32 65793, i32 65793, i32 65793, i32 65793, i32 65793, i32 65793, i32 65793, i32 65793&gt;
  %11 = bitcast &lt;16 x i32&gt; %10 to &lt;64 x i8&gt;
  %12 = shufflevector &lt;64 x i8&gt; &lt;i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 0, i8 0, i8 0, i8 0&gt;, &lt;64 x i8&gt; %11, &lt;64 x i32&gt; &lt;i32 60, i32 61, i32 62, i32 63, i32 64, i32 65, i32 66, i32 67, i32 68, i32 69, i32 70, i32 71, i32 72, i32 73, i32 74, i32 75, i32 76, i32 77, i32 78, i32 79, i32 80, i32 81, i32 82, i32 83, i32 84, i32 85, i32 86, i32 87, i32 88, i32 89, i32 90, i32 91, i32 92, i32 93, i32 94, i32 95, i32 96, i32 97, i32 98, i32 99, i32 100, i32 101, i32 102, i32 103, i32 104, i32 105, i32 106, i32 107, i32 108, i32 109, i32 110, i32 111, i32 112, i32 113, i32 114, i32 115, i32 116, i32 117, i32 118, i32 119, i32 120, i32 121, i32 122, i32 123&gt;
  %13 = add nuw &lt;64 x i8&gt; %12, %11
  %14 = shufflevector &lt;64 x i8&gt; &lt;i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0&gt;, &lt;64 x i8&gt; %13, &lt;64 x i32&gt; &lt;i32 56, i32 57, i32 58, i32 59, i32 60, i32 61, i32 62, i32 63, i32 64, i32 65, i32 66, i32 67, i32 68, i32 69, i32 70, i32 71, i32 72, i32 73, i32 74, i32 75, i32 76, i32 77, i32 78, i32 79, i32 80, i32 81, i32 82, i32 83, i32 84, i32 85, i32 86, i32 87, i32 88, i32 89, i32 90, i32 91, i32 92, i32 93, i32 94, i32 95, i32 96, i32 97, i32 98, i32 99, i32 100, i32 101, i32 102, i32 103, i32 104, i32 105, i32 106, i32 107, i32 108, i32 109, i32 110, i32 111, i32 112, i32 113, i32 114, i32 115, i32 116, i32 117, i32 118, i32 119&gt;
  %15 = add nuw &lt;64 x i8&gt; %14, %13
  %16 = shufflevector &lt;64 x i8&gt; &lt;i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0&gt;, &lt;64 x i8&gt; %15, &lt;64 x i32&gt; &lt;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, i32 64, i32 65, i32 66, i32 67, i32 68, i32 69, i32 70, i32 71, i32 72, i32 73, i32 74, i32 75, i32 76, i32 77, i32 78, i32 79, i32 80, i32 81, i32 82, i32 83, i32 84, i32 85, i32 86, i32 87, i32 88, i32 89, i32 90, i32 91, i32 92, i32 93, i32 94, i32 95, i32 96, i32 97, i32 98, i32 99, i32 100, i32 101, i32 102, i32 103, i32 104, i32 105, i32 106, i32 107, i32 108, i32 109, i32 110, i32 111&gt;
  %17 = add nuw &lt;64 x i8&gt; %16, %15
  %18 = shufflevector &lt;64 x i8&gt; &lt;i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0&gt;, &lt;64 x i8&gt; %17, &lt;64 x i32&gt; &lt;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, i32 64, i32 65, i32 66, i32 67, i32 68, i32 69, i32 70, i32 71, i32 72, i32 73, i32 74, i32 75, i32 76, i32 77, i32 78, i32 79, i32 80, i32 81, i32 82, i32 83, i32 84, i32 85, i32 86, i32 87, i32 88, i32 89, i32 90, i32 91, i32 92, i32 93, i32 94, i32 95&gt;
  %19 = add nuw &lt;64 x i8&gt; %18, %17
  ret &lt;64 x i8&gt; %19
}

Compiled for Zen 3:

.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
.LCPI0_2:
        .byte   1
        .byte   2
        .byte   4
        .byte   8
        .byte   16
        .byte   32
        .byte   64
        .byte   128
.LCPI0_3:
        .long   65793
unmovemask64u8:
.Lunmovemask64u8$local:
        vmovq   xmm0, rdi
        vpbroadcastq    ymm2, qword ptr [rip + .LCPI0_2]
        vpbroadcastq    ymm0, xmm0
        vpshufb ymm1, ymm0, ymmword ptr [rip + .LCPI0_0]
        vpshufb ymm0, ymm0, ymmword ptr [rip + .LCPI0_1]
        vpand   ymm0, ymm0, ymm2
        vpand   ymm1, ymm1, ymm2
        vpcmpeqb        ymm0, ymm0, ymm2
        vpcmpeqb        ymm1, ymm1, ymm2
        vpbroadcastd    ymm2, dword ptr [rip + .LCPI0_3]
        vpmulld ymm0, ymm0, ymm2
        vpmulld ymm1, ymm1, ymm2
        vperm2i128      ymm2, ymm0, ymm1, 33
        vperm2i128      ymm3, ymm0, ymm0, 40
        vpalignr        ymm2, ymm1, ymm2, 12
        vpalignr        ymm3, ymm0, ymm3, 12
        vpaddb  ymm0, ymm3, ymm0
        vpaddb  ymm1, ymm2, ymm1
-       vpxor   xmm3, xmm3, xmm3
        vpblendd        ymm2, ymm1, ymm0, 192
        vpblendd        ymm3, ymm0, ymm3, 192
        vpermq  ymm2, ymm2, 147
        vpermq  ymm3, ymm3, 147
        vpaddb  ymm0, ymm3, ymm0
        vpaddb  ymm1, ymm2, ymm1
        vperm2i128      ymm2, ymm0, ymm1, 33
        vperm2i128      ymm3, ymm0, ymm0, 40
        vpaddb  ymm1, ymm2, ymm1
        vpaddb  ymm0, ymm3, ymm0
        vpaddb  ymm1, ymm0, ymm1
        ret

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend:X86 question A question, not bug report. Check out https://llvm.org/docs/GettingInvolved.html instead!
Projects
None yet
Development

No branches or pull requests

3 participants