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

Compiler crash bit-shifting undefined at comptime #21266

Open
Validark opened this issue Aug 31, 2024 · 1 comment
Open

Compiler crash bit-shifting undefined at comptime #21266

Validark opened this issue Aug 31, 2024 · 1 comment
Labels
bug Observed behavior contradicts documented or intended behavior frontend Tokenization, parsing, AstGen, Sema, and Liveness.
Milestone

Comments

@Validark
Copy link
Contributor

Validark commented Aug 31, 2024

Zig Version

0.14.0-dev.1366+d997ddaa1

Steps to Reproduce and Observed Behavior

Running bits_to_nibbles at compile-time crashes the compiler. Godbolt link

const std = @import("std");

export fn bits_to_nibbles(i: u16) u64 {
    const bit_positions = @as(@Vector(8, u8), @splat(1)) << std.simd.iota(u3, 8);
    const bit_positions_ext = std.simd.join(bit_positions, bit_positions);
    const v = std.simd.join(@as(@Vector(2, u8), @bitCast(i)), @as(@Vector(14, u8), @splat(0)));
    return @bitCast(vshrn_n_u16(@bitCast(@select(u8, (@shuffle(u8, v, undefined, [_]i32{ 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1 }) & bit_positions_ext) == bit_positions_ext,
        @as(@Vector(16, u8), @splat(0xff)),
        @as(@Vector(16, u8), @splat(0)),
    )), 4));
}

export fn main() void {
    const i: u16 = 0b0000100001001010;

    @setEvalBranchQuota(10000000);
    @compileLog(comptime bits_to_nibbles(i));
}

const Chunk = @Vector(16, u8);

fn vshrn_n_u16(a: @Vector(@sizeOf(Chunk) / 2, u16), comptime c: u8) @Vector(@sizeOf(Chunk) / 2, u8) {
    // Workaround for https://github.com/llvm/llvm-project/issues/88227#issuecomment-2048490807
    const b = @shuffle(u16, a, undefined, std.simd.join(std.simd.iota(i32, @sizeOf(Chunk) / 2), @as(@Vector(@sizeOf(Chunk) / 2, i32), @splat(-1))));
    return @bitCast(@as(std.meta.Int(.unsigned, @sizeOf(Chunk) * 4), @truncate(@as(std.meta.Int(.unsigned, @bitSizeOf(Chunk)), @bitCast(@shuffle(u8, @as(@Vector(@sizeOf(Chunk) * 2, u8), @bitCast(b >> @splat(c))), undefined, std.simd.iota(i32, @sizeOf(Chunk)) << @splat(1)))))));
}

Expected Behavior

no crash

@Validark Validark added the bug Observed behavior contradicts documented or intended behavior label Aug 31, 2024
@Vexu Vexu added the frontend Tokenization, parsing, AstGen, Sema, and Liveness. label Sep 1, 2024
@Vexu Vexu added this to the 0.15.0 milestone Sep 1, 2024
@mlugg
Copy link
Member

mlugg commented Sep 15, 2024

Reduction:

comptime {
    const a: @Vector(1, u8) = .{undefined};
    _ = a >> @splat(4);
}

Missing undefined check in Value.shrScalar. Same applies to shlScalar and probably a bunch more arithmetic functions -- we should check all of these.

@mlugg mlugg changed the title Comptime execution segfaults compiler Compiler crash bit-shifting undefined at comptime Sep 15, 2024
@mlugg mlugg modified the milestones: 0.15.0, 0.14.0 Sep 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior frontend Tokenization, parsing, AstGen, Sema, and Liveness.
Projects
None yet
Development

No branches or pull requests

3 participants