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

@shrExact omits runtime safety check for compile-time-known RHS vector operand #19992

Open
amp-59 opened this issue May 17, 2024 · 0 comments · May be fixed by #19764
Open

@shrExact omits runtime safety check for compile-time-known RHS vector operand #19992

amp-59 opened this issue May 17, 2024 · 0 comments · May be fixed by #19764
Labels
bug Observed behavior contradicts documented or intended behavior frontend Tokenization, parsing, AstGen, Sema, and Liveness.
Milestone

Comments

@amp-59
Copy link
Contributor

amp-59 commented May 17, 2024

Zig Version

0.13.0-dev.211+6a65561e3

Steps to Reproduce and Observed Behaviour

Compile and run example program with zig run shr_vec_ov_comptime_rhs.zig
shr_vec_ov_comptime_rhs.zig:

pub fn main() void {
    var v: @Vector(4, u32) = @splat(1);

    const pc_before: u16 = @reduce(.Add, @popCount(v));

    v = @shrExact(v, @splat(1));

    const pc_after: u16 = @reduce(.Add, @popCount(v));

    if (pc_after != pc_before) {
        unreachable;
    }
}

Output:

zig run shr_vec_ov_comptime_rhs.zig
thread 9474 panic: reached unreachable code
~/.local/share/test/shr_vec_ov_comptime_rhs.zig:11:9: 0x10344c5 in main (shr_vec_ov_comptime_rhs)
        unreachable;
        ^
~/.local/src/zig-linux-x86_64-0.13.0-dev.211+6a65561e3/lib/std/start.zig:514:22: 0x1033c19 in posixCallMainAndExit (shr_vec_ov_comptime_rhs)
            root.main();
                     ^
~/.local/src/zig-linux-x86_64-0.13.0-dev.211+6a65561e3/lib/std/start.zig:266:5: 0x1033781 in _start (shr_vec_ov_comptime_rhs)
    asm volatile (switch (native_arch) {
    ^
???:?:?: 0x0 in ??? (???)

Expected Behaviour

Compile and run example program with zig run shr_vec_ov_runtime_rhs.zig
shr_vec_ov_runtime_rhs.zig:

var rhs: @Vector(4, u5) = @splat(1);

pub fn main() void {
    var v: @Vector(4, u32) = @splat(1);

    const pc_before: u16 = @reduce(.Add, @popCount(v));

    v = @shrExact(v, rhs);

    const pc_after: u16 = @reduce(.Add, @popCount(v));

    if (pc_after != pc_before) {
        unreachable;
    }
}

Output:

zig run shr_vec_ov_runtime_rhs.zig
thread 9946 panic: right shift overflowed bits
~/.local/share/test/shr_vec_ov_runtime_rhs.zig:8:5: 0x10344ea in main (shr_vec_ov_runtime_rhs)
    v = @shrExact(v, rhs);
    ^
~/.local/src/zig-linux-x86_64-0.13.0-dev.211+6a65561e3/lib/std/start.zig:514:22: 0x1033c19 in posixCallMainAndExit (shr_vec_ov_runtime_rhs)
            root.main();
                     ^
~/.local/src/zig-linux-x86_64-0.13.0-dev.211+6a65561e3/lib/std/start.zig:266:5: 0x1033781 in _start (shr_vec_ov_runtime_rhs)
    asm volatile (switch (native_arch) {
    ^
???:?:?: 0x0 in ??? (???)

The only difference between the two examples is that the shift amount in the latter example is runtime-known. The outcomes should be the same.

@Vexu Vexu added bug Observed behavior contradicts documented or intended behavior frontend Tokenization, parsing, AstGen, Sema, and Liveness. labels May 18, 2024
@Vexu Vexu added this to the 0.13.0 milestone May 18, 2024
@amp-59 amp-59 linked a pull request May 21, 2024 that will close this issue
40 tasks
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

Successfully merging a pull request may close this issue.

2 participants