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 panic caused by comparing a float vector that contains NaN #21932

Open
kcbanner opened this issue Nov 7, 2024 · 0 comments · May be fixed by #21933
Open

Compiler panic caused by comparing a float vector that contains NaN #21932

kcbanner opened this issue Nov 7, 2024 · 0 comments · May be fixed by #21933
Labels
bug Observed behavior contradicts documented or intended behavior frontend Tokenization, parsing, AstGen, Sema, and Liveness.
Milestone

Comments

@kcbanner
Copy link
Contributor

kcbanner commented Nov 7, 2024

Zig Version

0.14.0-dev.2182+54d0ba418

Steps to Reproduce and Observed Behavior

Compile the following test code (extracted from zmath):

pub inline fn select(mask: anytype, v0: anytype, v1: anytype) @TypeOf(v0, v1) {
    return @select(f32, mask, v0, v1);
}

pub inline fn maxFast(v0: anytype, v1: anytype) @TypeOf(v0, v1) {
    return select(v0 > v1, v0, v1); // maxps
}

pub const F32x4 = @Vector(4, f32);
pub inline fn veclen(comptime T: type) comptime_int {
    return @typeInfo(T).vector.len;
}

const std = @import("std");
const math = std.math;
const expect = std.testing.expect;

pub inline fn splat(comptime T: type, value: f32) T {
    return @splat(value);
}

pub fn all(vb: anytype, comptime len: u32) bool {
    const T = @TypeOf(vb);
    if (len > veclen(T)) {
        @compileError("zmath.all(): 'len' is greater than vector len of type " ++ @typeName(T));
    }
    const loop_len = if (len == 0) veclen(T) else len;
    const ab: [veclen(T)]bool = vb;
    comptime var i: u32 = 0;
    var result = true;
    inline while (i < loop_len) : (i += 1) {
        result = result and ab[i];
    }
    return result;
}

pub inline fn isNearEqual(
    v0: anytype,
    v1: anytype,
    epsilon: anytype,
) @Vector(veclen(@TypeOf(v0)), bool) {
    const T = @TypeOf(v0, v1, epsilon);
    const delta = v0 - v1;
    const temp = maxFast(delta, splat(T, 0.0) - delta);
    return temp <= epsilon;
}

test "zmath.isNearEqual" {
    comptime {
        try expect(all(isNearEqual(
            splat(F32x4, math.inf(f32)),
            splat(F32x4, math.inf(f32)),
            splat(F32x4, 0.0001),
        ), 0) == false);
        try expect(all(isNearEqual(
            splat(F32x4, -math.inf(f32)),
            splat(F32x4, math.inf(f32)),
            splat(F32x4, 0.0001),
        ), 0) == false);
        try expect(all(isNearEqual(
            splat(F32x4, -math.inf(f32)),
            splat(F32x4, -math.inf(f32)),
            splat(F32x4, 0.0001),
        ), 0) == false);
        try expect(all(isNearEqual(
            splat(F32x4, -math.nan(f32)),
            splat(F32x4, math.inf(f32)),
            splat(F32x4, 0.0001),
        ), 0) == false);
    }
}
$ zig-debug.bat test repro.zig -Dtarget=x86_64-windows-gnu
thread 35384 panic: reached unreachable code
Analyzing repro.zig
    > %31 = cmp_gt(%19, %20) node_offset:6:19 to :6:26
      %32 = break_inline(%30, %31)
    For full context, use the command
      zig ast-check -t repro.zig

  in repro.zig
    > %30 = call(.auto, %28, [
        {%31, %32},
        {%33},
        {%34},
      ]) node_offset:6:12 to :6:35
  in repro.zig
    > %207 = call(.auto, %205, [
        {%208},
        {%209..%217},
      ]) node_offset:44:18 to :44:55
  in repro.zig
    > %238 = call(.compile_time, %236, [
        {%239..%249},
        {%250..%260},
        {%261..%268},
      ]) node_offset:50:24 to :50:24
  in repro.zig
    > %235 = call(.compile_time, %233, [
        {%236..%269},
        {%270},
      ]) node_offset:50:20 to :50:24
  in repro.zig
    > %232 = call(.compile_time, %230, [
        {%233..%272},
      ]) node_offset:50:13 to :50:20
  in repro.zig
    > %229 = block_comptime({%230..%422}) node_offset:49:14 to :49:14

The crash is due to reaching the unreachable branch in math.order:

pub fn order(a: anytype, b: anytype) Order {
    if (a == b) {
        return .eq;
    } else if (a < b) {
        return .lt;
    } else if (a > b) {
        return .gt;
    } else {
        unreachable;
    }
}

zig.exe;compilerPanic();7FF6B12967F5;F67F5
zig.exe;order__anon_751457();7FF6B2FF66FD;1E566FD
zig.exe;orderAgainstZeroInner__anon_754912();7FF6B3075EED;1ED5EED
zig.exe;orderAdvanced__anon_810482();7FF6B335898C;21B898C
zig.exe;compareHeteroAdvanced__anon_810476();7FF6B3359070;21B9070
zig.exe;compareHeteroSema();7FF6B3359429;21B9429
zig.exe;compareScalar();7FF6B303870D;1E9870D
zig.exe;compareVector();7FF6B3358666;21B8666
zig.exe;cmpVector();7FF6B301C388;1E7C388
zig.exe;analyzeCmp();7FF6B2C85DBF;1AE5DBF
zig.exe;zirCmp();7FF6B27C3211;1623211
zig.exe;analyzeBodyInner();7FF6B22D4D6E;1134D6E
zig.exe;analyzeInlineBody();7FF6B1E632D3;CC32D3
zig.exe;resolveInlineBody();7FF6B1A3D631;89D631
zig.exe;analyzeArg();7FF6B2D608CA;1BC08CA
zig.exe;analyzeInlineCallArg();7FF6B2D5E7B8;1BBE7B8
zig.exe;analyzeCall();7FF6B2897520;16F7520
zig.exe;zirCall__anon_509823();7FF6B27C12D5;16212D5
zig.exe;analyzeBodyInner();7FF6B22D4B53;1134B53
zig.exe;analyzeFnBody();7FF6B27525C4;15B25C4
zig.exe;analyzeCall();7FF6B2898C26;16F8C26
zig.exe;zirCall__anon_509823();7FF6B27C12D5;16212D5
zig.exe;analyzeBodyInner();7FF6B22D4B53;1134B53
zig.exe;analyzeFnBody();7FF6B27525C4;15B25C4
zig.exe;analyzeCall();7FF6B2898C26;16F8C26
zig.exe;zirCall__anon_509823();7FF6B27C12D5;16212D5
zig.exe;analyzeBodyInner();7FF6B22D4B53;1134B53
zig.exe;analyzeInlineBody();7FF6B1E632D3;CC32D3
zig.exe;resolveInlineBody();7FF6B1A3D631;89D631
zig.exe;analyzeArg();7FF6B2D608CA;1BC08CA
zig.exe;analyzeInlineCallArg();7FF6B2D5E7B8;1BBE7B8
zig.exe;analyzeCall();7FF6B2897520;16F7520
zig.exe;zirCall__anon_509823();7FF6B27C12D5;16212D5
zig.exe;analyzeBodyInner();7FF6B22D4B53;1134B53
zig.exe;analyzeInlineBody();7FF6B1E632D3;CC32D3
zig.exe;resolveInlineBody();7FF6B1A3D631;89D631
zig.exe;analyzeArg();7FF6B2D608CA;1BC08CA
zig.exe;analyzeInlineCallArg();7FF6B2D5ED20;1BBED20
zig.exe;analyzeCall();7FF6B2897520;16F7520
zig.exe;zirCall__anon_509823();7FF6B27C12D5;16212D5
zig.exe;analyzeBodyInner();7FF6B22D4B53;1134B53
zig.exe;analyzeInlineBody();7FF6B1E632D3;CC32D3
zig.exe;resolveInlineBody();7FF6B1A3D631;89D631
zig.exe;resolveBlockBody();7FF6B2CC37F2;1B237F2
zig.exe;zirBlock();7FF6B287A778;16DA778
zig.exe;analyzeBodyInner();7FF6B22E2809;1142809
zig.exe;analyzeFnBody();7FF6B27525C4;15B25C4
zig.exe;analyzeFnBody();7FF6B227B5DD;10DB5DD
zig.exe;ensureFuncBodyAnalyzedInner();7FF6B1E15E49;C75E49
zig.exe;ensureFuncBodyAnalyzed();7FF6B1A099D4;8699D4
zig.exe;processOneJob();7FF6B176C521;5CC521
zig.exe;performAllTheWorkInner();7FF6B1542C98;3A2C98
zig.exe;performAllTheWork();7FF6B13A10EC;2010EC
zig.exe;update();7FF6B139C57F;1FC57F
zig.exe;updateModule();7FF6B13DEBC0;23EBC0
zig.exe;buildOutputType();7FF6B14494F5;2A94F5
zig.exe;mainArgs();7FF6B12994B9;F94B9
zig.exe;main();7FF6B12969AF;F69AF
zig.exe;main();7FF6B12966DB;F66DB
zig.exe;__tmainCRTStartup();7FF6B3E8A1F2;2CEA1F2
zig.exe;mainCRTStartup();7FF6B3E8A24C;2CEA24C

Expected Behavior

Compiles without crashing, test should pass.

@kcbanner kcbanner added the bug Observed behavior contradicts documented or intended behavior label Nov 7, 2024
@Vexu Vexu added this to the 0.14.0 milestone Nov 7, 2024
@Vexu Vexu added the frontend Tokenization, parsing, AstGen, Sema, and Liveness. label Nov 7, 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

Successfully merging a pull request may close this issue.

2 participants