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

stage3 fails the behavior tests #11450

Closed
Tracked by #89
andrewrk opened this issue Apr 16, 2022 · 17 comments · Fixed by #12165
Closed
Tracked by #89

stage3 fails the behavior tests #11450

andrewrk opened this issue Apr 16, 2022 · 17 comments · Fixed by #12165
Labels
bug Observed behavior contradicts documented or intended behavior frontend Tokenization, parsing, AstGen, Sema, and Liveness.
Milestone

Comments

@andrewrk
Copy link
Member

andrewrk commented Apr 16, 2022

Zig Version: 0.10.0-dev.1855+a315d51c0

After stage2 rebuilds itself, producing stage3, it no longer passes the behavior tests. In this snippet below you can see:

  • building stage1 successfully
  • building stage2 successfully
  • building stage3 successfully
  • stage2 passing behavior tests with the llvm backend
  • stage3 failing behavior tests with the llvm backend
[nix-shell:~/dev/zig/build-release]$ ninja
[nix-shell:~/dev/zig/build-release]$ ./zig build -p stage2 -Dskip-install-lib-files  --prominent-compile-errors  -Denable-llvm
[nix-shell:~/dev/zig/build-release]$ ./stage2/bin/zig build -p stage3 -Dskip-install-lib-files -Denable-llvm
[nix-shell:~/dev/zig/build-release]$ ./stage2/bin/zig test ../test/behavior.zig -I../test
Test [20/1244] test.generic function with align param... SKIP
Test [21/1244] test.runtime known array index has best alignment possible... SKIP
Test [24/1244] test.read 128-bit field from default aligned struct in global memory... SKIP
Test [26/1244] test.align(@alignOf(T)) T does not force resolution of T... SKIP
Test [27/1244] test.align(N) on functions... SKIP
Test [60/1244] test.simple coroutine suspend and resume... SKIP
Test [61/1244] test.pass parameter to coroutine... SKIP
Test [62/1244] test.suspend at end of function... SKIP
Test [63/1244] test.local variable in async function... SKIP
Test [64/1244] test.calling an inferred async function... SKIP
Test [65/1244] test.@frameSize... SKIP
Test [66/1244] test.coroutine suspend, resume... SKIP
Test [67/1244] test.coroutine suspend with block... SKIP
Test [68/1244] test.coroutine await... SKIP
Test [69/1244] test.coroutine await early return... SKIP
Test [70/1244] test.async function with dot syntax... SKIP
Test [71/1244] test.async fn pointer in a struct field... SKIP
Test [72/1244] test.@asyncCall with return type... SKIP
Test [73/1244] test.async fn with inferred error set... SKIP
Test [74/1244] test.error return trace across suspend points - early return... SKIP
Test [75/1244] test.error return trace across suspend points - async return... SKIP
Test [76/1244] test.break from suspend... SKIP
Test [77/1244] test.heap allocated async function frame... SKIP
Test [78/1244] test.async function call return value... SKIP
Test [79/1244] test.suspension points inside branching control flow... SKIP
Test [80/1244] test.call async function which has struct return type... SKIP
Test [81/1244] test.pass string literal to async function... SKIP
Test [82/1244] test.await inside an errdefer... SKIP
Test [83/1244] test.try in an async function with error union and non-zero-bit payload... SKIP
Test [84/1244] test.returning a const error from async function... SKIP
Test [85/1244] test.async/await typical usage... SKIP
Test [86/1244] test.alignment of local variables in async functions... SKIP
Test [87/1244] test.no reason to resolve frame still works... SKIP
Test [88/1244] test.async call a generic function... SKIP
Test [89/1244] test.return from suspend block... SKIP
Test [90/1244] test.struct parameter to async function is copied to the frame... SKIP
Test [91/1244] test.cast fn to async fn when it is inferred to be async... SKIP
Test [92/1244] test.cast fn to async fn when it is inferred to be async, awaited direct... SKIP
Test [93/1244] test.await does not force async if callee is blocking... SKIP
Test [94/1244] test.recursive async function... SKIP
Test [95/1244] test.@asyncCall with comptime-known function, but not awaited directly... SKIP
Test [96/1244] test.@asyncCall with actual frame instead of byte buffer... SKIP
Test [97/1244] test.@asyncCall using the result location inside the frame... SKIP
Test [98/1244] test.@TypeOf an async function call of generic fn with error union type... SKIP
Test [99/1244] test.using @TypeOf on a generic function call... SKIP
Test [100/1244] test.recursive call of await @asyncCall with struct return type... SKIP
Test [101/1244] test.nosuspend function call... SKIP
Test [102/1244] test.await used in expression and awaiting fn with no suspend but asyn... SKIP
Test [103/1244] test.await used in expression after a fn call... SKIP
Test [104/1244] test.async fn call used in expression after a fn call... SKIP
Test [105/1244] test.suspend in for loop... SKIP
Test [106/1244] test.suspend in while loop... SKIP
Test [107/1244] test.correctly spill when returning the error union result of another a... SKIP
Test [108/1244] test.spill target expr in a for loop... SKIP
Test [109/1244] test.spill target expr in a for loop, with a var decl in the loop body... SKIP
Test [110/1244] test.async call with @call... SKIP
Test [111/1244] test.async function passed 0-bit arg after non-0-bit arg... SKIP
Test [112/1244] test.async function passed align(16) arg after align(8) arg... SKIP
Test [113/1244] test.async function call resolves target fn frame, comptime func... SKIP
Test [114/1244] test.async function call resolves target fn frame, runtime func... SKIP
Test [115/1244] test.properly spill optional payload capture value... SKIP
Test [116/1244] test.handle defer interfering with return value spill... SKIP
Test [117/1244] test.take address of temporary async frame... SKIP
Test [118/1244] test.nosuspend await... SKIP
Test [119/1244] test.nosuspend on function calls... SKIP
Test [120/1244] test.nosuspend on async function calls... SKIP
Test [121/1244] test.nosuspend resume async function calls... SKIP
Test [122/1244] test.avoid forcing frame alignment resolution implicit cast to *anyopaq... SKIP
Test [123/1244] test.@asyncCall with pass-by-value arguments... SKIP
Test [124/1244] test.@asyncCall with arguments having non-standard alignment... SKIP
Test [139/1244] test.coroutine await struct... SKIP
Test [201/1244] test.extern variable with non-pointer opaque type... SKIP
Test [226/1244] test.bitcast passed as tuple element... SKIP
Test [241/1244] test.issue 529 fixed... SKIP
Test [251/1244] test.bug 920 fixed... SKIP
Test [255/1244] test.bug 1120... SKIP
Test [298/1244] test.fixed... SKIP
Test [301/1244] test_0... SKIP
Test [312/1244] test_0... SKIP
Test [317/1244] test_0... SKIP
Test [326/1244] test.basic invocations... SKIP
Test [328/1244] test.comptime call with bound function as parameter... SKIP
Test [366/1244] test.vector casts... SKIP
Test [367/1244] test.@floatCast cast down... SKIP
Test [419/1244] test.type pun signed and unsigned as array pointer... SKIP
Test [420/1244] test.type pun signed and unsigned as offset many pointer... SKIP
Test [423/1244] test.type pun bits... SKIP
Test [424/1244] test.basic pointer preservation... SKIP
Test [425/1244] test.byte copy preserves linker value... SKIP
Test [426/1244] test.unordered byte copy preserves linker value... SKIP
Test [427/1244] test.shuffle chunks of linker value... SKIP
Test [428/1244] test.dance on linker values... SKIP
Test [429/1244] test.offset array ptr by element size... SKIP
Test [430/1244] test.offset instance by field size... SKIP
Test [431/1244] test.offset field ptr by enclosing array element size... SKIP
Test [439/1244] test.errdefer with payload... SKIP
Test [492/1244] test.comptime err to int of error set with only 1 possible value... SKIP
Test [499/1244] test.optional error set is the same size as error set... SKIP
Test [551/1244] test.call method on bound fn referring to var instance... SKIP
Test [555/1244] test.string literal used as comptime slice is memoized... SKIP
Test [556/1244] test.comptime function with mutable pointer is not memoized... SKIP
Test [557/1244] test.const ptr to comptime mutable data is not memoized... SKIP
Test [560/1244] test.setting backward branch quota just before a generic fn call... SKIP
Test [563/1244] test.array concatenation forces comptime... SKIP
Test [564/1244] test.array multiplication forces comptime... SKIP
Test [598/1244] test.negation f80... SKIP
Test [600/1244] test.eval @setFloatMode at compile-time... SKIP
Test [601/1244] test.float literal at compile time not lossy... SKIP
Test [602/1244] test.f128 at compile time is lossy... SKIP
Test [726/1244] test.remainder division... SKIP
Test [727/1244] test.float remainder division using @rem... SKIP
Test [728/1244] test.float modulo division using @mod... SKIP
Test [731/1244] test.@fabs f80... SKIP
Test [732/1244] test.@floor... SKIP
Test [733/1244] test.@floor f80... SKIP
Test [734/1244] test.@floor f128... SKIP
Test [735/1244] test.@ceil... SKIP
Test [736/1244] test.@ceil f80... SKIP
Test [737/1244] test.@ceil f128... SKIP
Test [738/1244] test.@trunc... SKIP
Test [739/1244] test.@trunc f80... SKIP
Test [740/1244] test.@trunc f128... SKIP
Test [741/1244] test.@round... SKIP
Test [742/1244] test.@round f80... SKIP
Test [743/1244] test.@round f128... SKIP
Test [746/1244] test.NaN comparison f80... SKIP
Test [749/1244] test.signed zeros are represented properly... SKIP
Test [759/1244] test.@mulAdd f80... SKIP
Test [764/1244] test.vector f80... SKIP
Test [811/1244] test.assign null directly to C pointer and test null equality... SKIP
Test [819/1244] test.pointer arithmetic affects the alignment... SKIP
Test [854/1244] test.@shuffle bool... SKIP
Test [862/1244] test.@sizeOf(T) == 0 doesn't force resolving struct size... SKIP
Test [1005/1244] test.initializer list expression... SKIP
Test [1051/1244] test.Type.AnyFrame... SKIP
Test [1080/1244] test.type info: anyframe and anyframe->T... SKIP
Test [1085/1244] test.type info for async frames... SKIP
Test [1236/1244] test.module level assembly... SKIP
Test [1237/1244] test.output constraint modifiers... SKIP
Test [1238/1244] test.alternative constraints... SKIP
Test [1240/1244] test.struct/array/union types as input values... SKIP
1103 passed; 141 skipped; 0 failed.

[nix-shell:~/dev/zig/build-release]$ ./stage3/bin/zig test ../test/behavior.zig -I../test
thread 486566 panic: attempt to index out of bounds
Analyzing ../test/behavior/saturating_arithmetic.zig: behavior/saturating_arithmetic.zig:test.saturating shift-left__struct_2689.testSatShl
      %2149 = decl_val("expect") 
      %2150 = dbg_stmt(2, 23)
      %2151 = param_type(%2149, 0)
    > %2152 = shl_sat(%2141, %2143) 
      %2153 = cmp_eq(%2152, %2145) 
      %2154 = call(.auto, %2149, [%2153]) 
      %2155 = is_non_err(%2154) 
      %2156 = condbr(%2155, {
        %2158 = err_union_payload_unsafe(%2154) 
        %2161 = break(%2157, %2158)
      }, {
        %2159 = err_union_code(%2154) 
        %2160 = ret_node(%2159) 
      }) 
    For full context, use the command
      zig ast-check -t ../test/behavior/saturating_arithmetic.zig

  in ../test/behavior/saturating_arithmetic.zig: behavior/saturating_arithmetic.zig:test.saturating shift-left__struct_2689.testSatShl
    > %2157 = block({%2149..%2156}) 
  in ../test/behavior/saturating_arithmetic.zig: behavior/saturating_arithmetic.zig:test.saturating shift-left__struct_2689.doTheTest
    > %1983 = call(.auto, %1975, [@Zir.Inst.Ref.i8_type, @Zir.Inst.Ref.one, %1980, %1982]) 
  in ../test/behavior/saturating_arithmetic.zig: behavior/saturating_arithmetic.zig:test.saturating shift-left__struct_2689.doTheTest
    > %1986 = block({%1975..%1985}) 
  in ../test/behavior/saturating_arithmetic.zig: behavior/saturating_arithmetic.zig:test.saturating shift-left
    > %2210 = call(.compile_time, %2208, []) 
  in ../test/behavior/saturating_arithmetic.zig: behavior/saturating_arithmetic.zig:test.saturating shift-left
    > %2213 = block({%2207..%2212}) 

/home/andy/dev/zig/lib/std/math/big/int.zig:3496:9: 0x377c477 in llshl (zig)
        r[dst_i] = carry | @call(.{ .modifier = .always_inline }, math.shr, .{
        ^
/home/andy/dev/zig/lib/std/math/big/int.zig:1149:14: 0x36384f7 in shiftLeftSat (zig)
        llshl(r.limbs[0..], a.limbs[0..a.limbs.len], shift);
             ^
/home/andy/dev/zig/src/value.zig:3997:35: 0x3637dda in shlSatScalar (zig)
        result_bigint.shiftLeftSat(lhs_bigint, shift, info.signedness, info.bits);
                                  ^
/home/andy/dev/zig/src/value.zig:3971:28: 0x3638958 in shlSat (zig)
        return shlSatScalar(lhs, rhs, ty, arena, target);
                           ^
/home/andy/dev/zig/src/Sema.zig:8765:35: 0x3487140 in zirShl (zig)
                try lhs_val.shlSat(rhs_val, lhs_ty, sema.arena, target),
                                  ^
/home/andy/dev/zig/src/Sema.zig:893:42: 0x32f648b in analyzeBodyInner (zig)
            .shl_sat   => try sema.zirShl(block, inst, .shl_sat),
                                         ^
/home/andy/dev/zig/src/Sema.zig:610:45: 0x31caf67 in analyzeBodyBreak (zig)
    const break_inst = sema.analyzeBodyInner(block, body) catch |err| switch (err) {
                                            ^
/home/andy/dev/zig/src/Sema.zig:1210:62: 0x32faa38 in analyzeBodyInner (zig)
                const break_data = (try sema.analyzeBodyBreak(block, inline_body)) orelse
                                                             ^
/home/andy/dev/zig/src/Sema.zig:593:30: 0x32eadf1 in analyzeBody (zig)
    _ = sema.analyzeBodyInner(block, body) catch |err| switch (err) {
                             ^
/home/andy/dev/zig/src/Sema.zig:5129:33: 0x35dee99 in analyzeCall (zig)
                sema.analyzeBody(&child_block, fn_info.body) catch |err| switch (err) {
                                ^
/home/andy/dev/zig/src/Sema.zig:4718:28: 0x34271bf in zirCall (zig)
    return sema.analyzeCall(block, func, func_src, call_src, modifier, ensure_result_used, resolved_args);
                           ^
/home/andy/dev/zig/src/Sema.zig:708:62: 0x32ec780 in analyzeBodyInner (zig)
            .call                         => try sema.zirCall(block, inst),
                                                             ^
/home/andy/dev/zig/src/Sema.zig:610:45: 0x31caf67 in analyzeBodyBreak (zig)
    const break_inst = sema.analyzeBodyInner(block, body) catch |err| switch (err) {
                                            ^
/home/andy/dev/zig/src/Sema.zig:1210:62: 0x32faa38 in analyzeBodyInner (zig)
                const break_data = (try sema.analyzeBodyBreak(block, inline_body)) orelse
                                                             ^
/home/andy/dev/zig/src/Sema.zig:593:30: 0x32eadf1 in analyzeBody (zig)
    _ = sema.analyzeBodyInner(block, body) catch |err| switch (err) {
                             ^
/home/andy/dev/zig/src/Sema.zig:5129:33: 0x35dee99 in analyzeCall (zig)
                sema.analyzeBody(&child_block, fn_info.body) catch |err| switch (err) {
                                ^
/home/andy/dev/zig/src/Sema.zig:4718:28: 0x34271bf in zirCall (zig)
    return sema.analyzeCall(block, func, func_src, call_src, modifier, ensure_result_used, resolved_args);
                           ^
/home/andy/dev/zig/src/Sema.zig:708:62: 0x32ec780 in analyzeBodyInner (zig)
            .call                         => try sema.zirCall(block, inst),
                                                             ^
/home/andy/dev/zig/src/Sema.zig:4114:34: 0x360b623 in resolveBlockBody (zig)
        if (sema.analyzeBodyInner(child_block, body)) |_| {
                                 ^
/home/andy/dev/zig/src/Sema.zig:4097:33: 0x34a3955 in zirBlock (zig)
    return sema.resolveBlockBody(parent_block, src, &child_block, body, inst, &label.merges);
                                ^
/home/andy/dev/zig/src/Sema.zig:1196:69: 0x32fa70f in analyzeBodyInner (zig)
                if (!block.is_comptime) break :blk try sema.zirBlock(block, inst);
                                                                    ^
/home/andy/dev/zig/src/Sema.zig:593:30: 0x32eadf1 in analyzeBody (zig)
    _ = sema.analyzeBodyInner(block, body) catch |err| switch (err) {
                             ^
/home/andy/dev/zig/src/Module.zig:4838:21: 0x31b4e5e in analyzeFnBody (zig)
    sema.analyzeBody(&inner_block, fn_info.body) catch |err| switch (err) {
                    ^
/home/andy/dev/zig/src/Module.zig:3616:40: 0x30654b6 in ensureFuncBodyAnalyzed (zig)
            var air = mod.analyzeFnBody(decl, func, sema_arena) catch |err| switch (err) {
                                       ^
/home/andy/dev/zig/src/Compilation.zig:2750:42: 0x3063998 in processOneJob (zig)
            module.ensureFuncBodyAnalyzed(func) catch |err| switch (err) {
                                         ^
/home/andy/dev/zig/src/Compilation.zig:2696:30: 0x2f7c417 in performAllTheWork (zig)
            try processOneJob(comp, work_item);
                             ^
/home/andy/dev/zig/src/Compilation.zig:2087:31: 0x2f795ea in update (zig)
    try comp.performAllTheWork();
                              ^
/home/andy/dev/zig/src/main.zig:3101:20: 0x2f950a0 in updateModule (zig)
    try comp.update();
                   ^
/home/andy/dev/zig/src/main.zig:2788:17: 0x2db7f72 in buildOutputType (zig)
    updateModule(gpa, comp, hook) catch |err| switch (err) {
                ^
/home/andy/dev/zig/src/main.zig:216:31: 0x2d8ca3f in mainArgs (zig)
        return buildOutputType(gpa, arena, args, .zig_test);
                              ^
/home/andy/dev/zig/src/main.zig:165:20: 0x2d8c10a in main (zig)
    return mainArgs(gpa, arena, args);
                   ^
/home/andy/dev/zig/lib/std/start.zig:583:37: 0x2d8d9c6 in main (zig)
            const result = root.main() catch |err| {
                                    ^
Aborted (core dumped)
@andrewrk andrewrk added bug Observed behavior contradicts documented or intended behavior frontend Tokenization, parsing, AstGen, Sema, and Liveness. labels Apr 16, 2022
@andrewrk andrewrk added this to the 0.10.0 milestone Apr 16, 2022
@acarrico
Copy link
Contributor

I just got an failure in saturating_arithmetic.zig in a normal compilation (stage 1, I guess, not that I understand the stages):

std.debug.print("\n {}, {} *| {} = {}\n", .{builtin.zig_backend, lhs, rhs, lhs *| rhs});

returns (127 is expected):

 std.builtin.CompilerBackend.stage1, -128 *| -128 = -128
[nix-shell:]$ zig version
0.10.0
[acarrico@silver:~/no-backup/zig/test]$ git describe
0.9.0-1931-gab658e32b

[acarrico@silver:~/no-backup/zig/test]$ git log -n 1
commit ab658e32bdfa234b6c33f0718cac7a23fbd4074f (HEAD -> master, origin/master, origin/HEAD)
Author: Andrew Kelley <andrew@ziglang.org>
Date:   Sun Apr 24 20:07:39 2022 -0700

I was going to open a new issue, but perhaps it is related to this one. Here is the failure:

 Test [852/1256] behavior.saturating_arithmetic.test "saturating multiplication"... FAIL (TestUnexpectedResult)
/nix/store/k1b69xysvlp6qgxjn1msvajdsdk0ap2k-zig/test/behavior/ptrcast.zig:82:41: 0x2a3282 in behavior.ptrcast.test "reinterpret bytes of an extern struct (with under-aligned fields) into another" (test)
    if (builtin.zig_backend == .stage1) return error.SkipZigTest;
                                        ^
/nix/store/k1b69xysvlp6qgxjn1msvajdsdk0ap2k-zig/test/behavior/ptrcast.zig:107:41: 0x2a32a2 in behavior.ptrcast.test "reinterpret bytes of an extern struct into another" (test)
    if (builtin.zig_backend == .stage1) return error.SkipZigTest;
                                        ^
/nix/store/k1b69xysvlp6qgxjn1msvajdsdk0ap2k-zig/test/behavior/ptrcast.zig:133:41: 0x2a32c2 in behavior.ptrcast.test "lower reinterpreted comptime field ptr (with under-aligned fields)" (test)
    if (builtin.zig_backend == .stage1) return error.SkipZigTest;
                                        ^
/nix/store/k1b69xysvlp6qgxjn1msvajdsdk0ap2k-zig/test/behavior/ptrcast.zig:157:41: 0x2a32e2 in behavior.ptrcast.test "lower reinterpreted comptime field ptr" (test)
    if (builtin.zig_backend == .stage1) return error.SkipZigTest;
                                        ^
/build/zig/lib/std/testing.zig:347:14: 0x2b43cb in std.testing.expect (test)
    if (!ok) return error.TestUnexpectedResult;
             ^
/nix/store/k1b69xysvlp6qgxjn1msvajdsdk0ap2k-zig/test/behavior/saturating_arithmetic.zig:138:13: 0x353f66 in behavior.saturating_arithmetic.S.testSatMul (test)
            try expect((lhs *| rhs) == expected);
            ^
/nix/store/k1b69xysvlp6qgxjn1msvajdsdk0ap2k-zig/test/behavior/saturating_arithmetic.zig:127:13: 0x31f6bb in behavior.saturating_arithmetic.S.doTheTest (test)
            try testSatMul(i8, -128, -128, 127);
            ^
/nix/store/k1b69xysvlp6qgxjn1msvajdsdk0ap2k-zig/test/behavior/saturating_arithmetic.zig:146:5: 0x2a3b5f in behavior.saturating_arithmetic.test "saturating multiplication" (test)
    try S.doTheTest();
    ^
/nix/store/k1b69xysvlp6qgxjn1msvajdsdk0ap2k-zig/test/behavior/inttoptr.zig:4:41: 0x29cf42 in behavior.inttoptr.test "casting integer address to function pointer" (test)
    if (builtin.zig_backend == .stage1) return error.SkipZigTest;
                                        ^
/nix/store/k1b69xysvlp6qgxjn1msvajdsdk0ap2k-zig/test/behavior/math.zig:741:41: 0x29f102 in behavior.math.test "allow signed integer division/remainder when values are comptime known and positive or exact" (test)
    if (builtin.zig_backend == .stage1) return error.SkipZigTest;
                                        ^
/nix/store/k1b69xysvlp6qgxjn1msvajdsdk0ap2k-zig/test/behavior/math.zig:1041:9: 0x29f752 in behavior.math.test "@fabs f80" (test)
        return error.SkipZigTest;
        ^
/nix/store/k1b69xysvlp6qgxjn1msvajdsdk0ap2k-zig/test/behavior/math.zig:1073:9: 0x29f842 in behavior.math.test "@floor f80" (test)
        return error.SkipZigTest;
        ^
/nix/store/k1b69xysvlp6qgxjn1msvajdsdk0ap2k-zig/test/behavior/math.zig:1082:9: 0x29f862 in behavior.math.test "@floor f128" (test)
        return error.SkipZigTest;
        ^
/nix/store/k1b69xysvlp6qgxjn1msvajdsdk0ap2k-zig/test/behavior/math.zig:1116:9: 0x29f952 in behavior.math.test "@ceil f80" (test)
        return error.SkipZigTest;
        ^
/nix/store/k1b69xysvlp6qgxjn1msvajdsdk0ap2k-zig/test/behavior/math.zig:1126:9: 0x29f972 in behavior.math.test "@ceil f128" (test)
        return error.SkipZigTest;
        ^
/nix/store/k1b69xysvlp6qgxjn1msvajdsdk0ap2k-zig/test/behavior/math.zig:1160:9: 0x29fa62 in behavior.math.test "@trunc f80" (test)
        return error.SkipZigTest;
        ^
/nix/store/k1b69xysvlp6qgxjn1msvajdsdk0ap2k-zig/test/behavior/math.zig:1176:9: 0x29fa82 in behavior.math.test "@trunc f128" (test)
        return error.SkipZigTest;
        ^
/nix/store/k1b69xysvlp6qgxjn1msvajdsdk0ap2k-zig/test/behavior/math.zig:1218:9: 0x29fb72 in behavior.math.test "@round f80" (test)
        return error.SkipZigTest;
        ^
/nix/store/k1b69xysvlp6qgxjn1msvajdsdk0ap2k-zig/test/behavior/math.zig:1228:9: 0x29fb92 in behavior.math.test "@round f128" (test)
        return error.SkipZigTest;
        ^
/nix/store/k1b69xysvlp6qgxjn1msvajdsdk0ap2k-zig/test/behavior/math.zig:1284:9: 0x29fce2 in behavior.math.test "NaN comparison f80" (test)
        return error.SkipZigTest;
        ^
/nix/store/k1b69xysvlp6qgxjn1msvajdsdk0ap2k-zig/test/behavior/member_func.zig:30:41: 0x29ffa2 in behavior.member_func.test "standard field calls" (test)
    if (builtin.zig_backend == .stage1) return error.SkipZigTest;
                                        ^
/nix/store/k1b69xysvlp6qgxjn1msvajdsdk0ap2k-zig/test/behavior/member_func.zig:74:41: 0x29ffc2 in behavior.member_func.test "@field field calls" (test)
    if (builtin.zig_backend == .stage1) return error.SkipZigTest;
                                        ^
/nix/store/k1b69xysvlp6qgxjn1msvajdsdk0ap2k-zig/test/behavior/merge_error_sets.zig:11:5: 0x31c972 in behavior.merge_error_sets.foo (test)
    return error.NotDir;
    ^
/nix/store/k1b69xysvlp6qgxjn1msvajdsdk0ap2k-zig/test/behavior/muladd.zig:49:9: 0x2a0132 in behavior.muladd.test "@mulAdd f80" (test)
        return error.SkipZigTest;
        ^
/nix/store/k1b69xysvlp6qgxjn1msvajdsdk0ap2k-zig/test/behavior/muladd.zig:105:41: 0x2a01a2 in behavior.muladd.test "vector f16" (test)
    if (builtin.zig_backend == .stage1) return error.SkipZigTest;
                                        ^
/nix/store/k1b69xysvlp6qgxjn1msvajdsdk0ap2k-zig/test/behavior/muladd.zig:129:41: 0x2a01c2 in behavior.muladd.test "vector f32" (test)
    if (builtin.zig_backend == .stage1) return error.SkipZigTest;
                                        ^
/nix/store/k1b69xysvlp6qgxjn1msvajdsdk0ap2k-zig/test/behavior/muladd.zig:153:41: 0x2a01e2 in behavior.muladd.test "vector f64" (test)
    if (builtin.zig_backend == .stage1) return error.SkipZigTest;
                                        ^
/nix/store/k1b69xysvlp6qgxjn1msvajdsdk0ap2k-zig/test/behavior/muladd.zig:178:9: 0x2a0202 in behavior.muladd.test "vector f80" (test)
        return error.SkipZigTest;
        ^
/nix/store/k1b69xysvlp6qgxjn1msvajdsdk0ap2k-zig/test/behavior/muladd.zig:198:41: 0x2a0222 in behavior.muladd.test "vector f128" (test)
    if (builtin.zig_backend == .stage1) return error.SkipZigTest;
                                        ^
/nix/store/k1b69xysvlp6qgxjn1msvajdsdk0ap2k-zig/test/behavior/pointers.zig:147:41: 0x2a1d82 in behavior.pointers.test "peer type resolution with C pointer and const pointer" (test)
    if (builtin.zig_backend == .stage1) return error.SkipZigTest;
                                        ^
/nix/store/k1b69xysvlp6qgxjn1msvajdsdk0ap2k-zig/test/behavior/pointers.zig:179:13: 0x352713 in behavior.pointers.S.foo (test)
            return bar() orelse error.Fail;
            ^
/nix/store/k1b69xysvlp6qgxjn1msvajdsdk0ap2k-zig/test/behavior/ptrcast.zig:24:41: 0x2a31c2 in behavior.ptrcast.test "reinterpret an array over multiple elements, with no well-defined layout" (test)
    if (builtin.zig_backend == .stage1) return error.SkipZigTest;
                                        ^

@andrewrk
Copy link
Member Author

Status update: a debug build of stage3 is now passing the behavior tests for several targets and then failing on aarch64-macos:

$ stage3/bin/zig build test-behavior -fwasmtime --glibc-runtimes /home/andy/Downloads/glibc/multi-2.33/install/glibcs -fqemu -Denable-llvm 
...
thread 426468 panic: attempt to use null value
link/MachO.zig:4138:45: 0x3016b6d in ??? (zig)
link/MachO.zig:3930:50: 0x3adb54e in ??? (zig)
/home/andy/dev/zig/src/link.zig:446:84: 0x3a879f8 in ??? (zig)
            .macho => return @fieldParentPtr(MachO, "base", base).lowerUnnamedConst(tv, decl_index),
                                                                                   ^
arch/aarch64/CodeGen.zig:4598:60: 0x39ecb08 in ??? (zig)
arch/aarch64/CodeGen.zig:4738:34: 0x39062f0 in ??? (zig)
arch/aarch64/CodeGen.zig:4534:57: 0x3779e9c in ??? (zig)
arch/aarch64/CodeGen.zig:3324:41: 0x3789a31 in ??? (zig)
arch/aarch64/CodeGen.zig:598:40: 0x35eb80f in ??? (zig)
arch/aarch64/CodeGen.zig:422:46: 0x35e960f in ??? (zig)
arch/aarch64/CodeGen.zig:315:17: 0x342cc4f in ??? (zig)
/home/andy/dev/zig/src/codegen.zig:87:63: 0x3282cac in ??? (zig)
        => return @import("arch/aarch64/CodeGen.zig").generate(bin_file, src_loc, func, air, liveness, code, debug_output),
                                                              ^
link/MachO.zig:3847:61: 0x328907a in ??? (zig)
/home/andy/dev/zig/src/link.zig:506:77: 0x3141a68 in ??? (zig)
            .macho => return @fieldParentPtr(MachO, "base", base).updateFunc(module, func, air, liveness),
                                                                            ^
/home/andy/dev/zig/src/Module.zig:3801:41: 0x2fe2d37 in ??? (zig)
            mod.comp.bin_file.updateFunc(mod, func, air, liveness) catch |err| switch (err) {
                                        ^
/home/andy/dev/zig/src/Compilation.zig:2911:42: 0x2fe1427 in ??? (zig)
            module.ensureFuncBodyAnalyzed(func) catch |err| switch (err) {
                                         ^
/home/andy/dev/zig/src/Compilation.zig:2843:30: 0x2f1713b in ??? (zig)
            try processOneJob(comp, work_item);
                             ^
/home/andy/dev/zig/src/Compilation.zig:2218:31: 0x2f141f0 in ??? (zig)
    try comp.performAllTheWork(main_progress_node);
                              ^
/home/andy/dev/zig/src/main.zig:3245:20: 0x2f2cb99 in ??? (zig)
    try comp.update();
                   ^
/home/andy/dev/zig/src/main.zig:2934:17: 0x2d83fb7 in ??? (zig)
    updateModule(gpa, comp, hook) catch |err| switch (err) {
                ^
/home/andy/dev/zig/src/main.zig:225:31: 0x2d616b6 in ??? (zig)
        return buildOutputType(gpa, arena, args, .zig_test);
                              ^
/home/andy/dev/zig/src/main.zig:174:20: 0x2d60dcd in ??? (zig)
    return mainArgs(gpa, arena, args);
                   ^
/home/andy/dev/zig/lib/std/start.zig:581:37: 0x2d6274e in ??? (zig)
            const result = root.main() catch |err| {
                                    ^
test...The following command terminated unexpectedly:
/home/andy/dev/zig/build-release/stage3/bin/zig test -fno-stage1 -fno-LLVM /home/andy/dev/zig/test/behavior.zig --test-name-prefix behavior-aarch64-macos-none-Debug--multi-stage2_aarch64  --cache-dir /home/andy/dev/zig/zig-cache --global-cache-dir /home/andy/.cache/zig --name test -fno-single-threaded -target aarch64-macos-none -mcpu generic --test-no-exec -I /home/andy/dev/zig/test -L /home/andy/local/llvm13-release/lib -isystem /home/andy/local/llvm13-release/include --zig-lib-dir /home/andy/dev/zig/lib --enable-cache 

A release build of stage3 is segfaulting:

$ stage3-release/bin/zig test ../test/behavior.zig -I../test
Segmentation fault (core dumped)

Since these bugs are likely to already have test coverage somewhere in the main test suite, the best way to solve this issue I think is to press forward on the full test suite passing. Likely once that happens, this issue will have been solved somewhere along the way.

@andrewrk
Copy link
Member Author

andrewrk commented Jul 5, 2022

export fn foo(x: u32) bool {
    return switch (x) {
        4...4 => false,
        13 => true,
        else => false,
    };
}
$ stage3-release/bin/zig build-obj test.zig
Segmentation fault (core dumped)

The problem is that it's not very helpful to debug this directly; the likely scenario is that a bug unrelated to switch is causing a miscompilation anywhere in the self-hosted compiler that deals with switch expressions.

@topolarity
Copy link
Contributor

topolarity commented Jul 18, 2022

Tests pass if you compile the IR with LLVM 13: 🤔

$ /stage2/bin/zig build -Dlog -Denable-llvm --zig-lib-dir lib --prefix $(pwd)/stage3 --search-prefix ~/local/llvm14-release -Dcpu=baseline -Dconfig_h=./build/config.h -Drelease -Dstrip=true --verbose-llvm-ir 2>stage3.ll
$ opt-13 stage3.ll -o stage3.bc -filetype=obj -O3
$ llc-13 stage3.bc -o stage3.o -filetype=obj -O3
$ ld.lld -error-limit=0 -O3 -z stack-size=16777216 --gc-secti... (extremely long linker command taken from --verbose-link)
$ ./stage3 test ./test/behavior.zig -Itest --zig-lib-dir lib
...
1245 passed; 86 skipped; 0 failed.

Using opt-14 and llc-14 instead show the comptime dereference failures from the usual build.

@topolarity
Copy link
Contributor

Possibly related: llvm/llvm-project#56585

@dgalling
Copy link

I think you're correct about that zero-extension bug being related.

I was looking into the compile issue Andrew was debugging on stream the other day. Here's the test case from the stream:

comptime {
    var x: ?i32 = null;
    x = 2;
    x.? *= 10;
}

A debug build of stage3 accepts this code, but a release build rejects it.

I traced the execution through Sema.zig and was having trouble understanding why a if (deref.pointee) |*tv| branch was not being taken, despite deref.pointee being set.

Zig is performing the optional check with a cmp [stack loc], 1 but it looks like the value on the stack is 0xFF.

This disassembly is from Sema.zig:22583 as of 0e26c61 (I would link to the line but the file is too large for GitHub to render).

0x326611f <Sema.beginComptimePtrLoad+1775>      cmp    BYTE PTR [rsp+0xf0],0x1
0x3266127 <Sema.beginComptimePtrLoad+1783>      lea    r10,[rsp+0x111]
0x326612f <Sema.beginComptimePtrLoad+1791>      jne    0x3266218 <Sema.beginComptimePtrLoad+2024>
---
(gdb) x/1xb $rsp+0xf0
0x7fffffff00c0: 0xff

# Dump the entire ?TypedValue
(gdb) x/3xg $rsp+0xe0
0x7fffffff00b0: 0x0000000009bbb718      0x0000000009bbb738
0x7fffffff00c0: 0x00000000000000ff

Here's the disassembly and contents of memory from a debug build (same source location, same commit):

0x39aed54 <Sema.beginComptimePtrLoad+8132>      test   BYTE PTR [rbp-0x328],0x1
0x39aed5b <Sema.beginComptimePtrLoad+8139>      jne    0x39aed62 <Sema.beginComptimePtrLoad+8146>
---
(gdb) x/1xb $rbp-0x328
0x7ffffffee8e8: 0x01

# Dump the entire ?TypedValue
(gdb) x/3xg $rbp-0x338
0x7ffffffee8d8: 0x000000000a6ca4c8      0x000000000a6ca4e8
0x7ffffffee8e8: 0x000000000a6cf201

@topolarity
Copy link
Contributor

Oh boy. Yep, that matches my observations from that bug exactly.

I observed it on a modified branch, but it sounds like you've just confirmed it on master.

@topolarity
Copy link
Contributor

Sure enough, your hint leads right to it:

; (Optimized IR, LLVM 14)
define internal fastcc void @Sema.beginComptimePtrLoad(...) unnamed_addr #1 {
...
Else5:                                            ; preds = %Then2
  ...
  %94 = bitcast i1* %.sroa.gep923 to i8*
  store i8 -1, i8* %94, align 8
  ...

; (Optimized IR, LLVM 13)
define internal fastcc void @Sema.beginComptimePtrLoad(...) unnamed_addr #1 {
...
Else5:                                            ; preds = %Then2
  ...
  %.sroa.2184.0.sroa_cast = bitcast i8* %.sroa.2184 to i1*
  store i1 true, i1* %.sroa.2184.0.sroa_cast, align 8
  ...

@dgalling
Copy link

One way to work around the LLVM bug would be to do the null check by comparing against 0 instead of 1, right? That's the way I would expect a C compiler to do the check, anyway.

(I'm new here, so apologies if I'm overlooking some history about why Zig does null checks this way).

@andrewrk
Copy link
Member Author

One way to work around the LLVM bug would be to do the null check by comparing against 0 instead of 1, right? That's the way I would expect a C compiler to do the check, anyway.

If we look at a simple optional check in terms of LLVM IR:

var optional_foo: ?i32 = null;

fn doTheTest() !void {
    if (optional_foo) |*foo| {
        var x = foo.*;
        _ = x;
    }
}
@test2.optional_foo = internal unnamed_addr global { i32, i1, [3 x i8] } { i32 undef, i1 false, [3 x i8] undef }, align 4

define internal fastcc i16 @test2.doTheTest() unnamed_addr #0 {
Entry:
  %0 = alloca i32, align 4
  %1 = load { i32, i1, [3 x i8] }, { i32, i1, [3 x i8] }* @test2.optional_foo, align 4
  %2 = extractvalue { i32, i1, [3 x i8] } %1, 1
  br i1 %2, label %Then, label %Else

Then:                                             ; preds = %Entry
  %3 = load i32, i32* getelementptr inbounds ({ i32, i1, [3 x i8] }, { i32, i1, [3 x i8] }* @test2.optional_foo, i32 0, i32 0), align 4
  store i32 %3, i32* %0, align 4
  br label %Block

Else:                                             ; preds = %Entry
  br label %Block

Block:                                            ; preds = %Else, %Then
  ret i16 0
}

Zig is not doing a comparison against 1; it is loading an i1 and then doing a conditional branch on it.

@dgalling
Copy link

I see, thanks for explaining. Looks like LLVM is what is compiling that conditional branch on i1 into a cmp/test against 1, then: godbolt.

Just for comparison, I translated your example to C. clang compiles the if (optional_foo) as an icmp ne against null, which gets compiled into a cmp against 0.

Anyway, I don't have any opinions about what to do with the LLVM bug--just brought up the null check thing because the disassembly was somewhat unusual to me.

@andrewrk
Copy link
Member Author

Just for comparison, I translated your example to C. clang compiles the if (optional_foo) as an icmp ne against null, which gets compiled into a cmp against 0.

That's not quite a one-to-one translation. Here is your C code translated to Zig: https://godbolt.org/z/hPn3bEjhW
You can see it does also generate an icmp ne against null.

Thanks for making the observation! I definitely think you're onto something.

andrewrk added a commit that referenced this issue Jul 19, 2022
This is a possible workaround for
llvm/llvm-project#56585

On my computer it makes stage3-release go from false positive
compilation errors on the behavior tests to "segmentation fault".
Is this forwards progress or backwards progress? I have no idea.

See #11450
@andrewrk
Copy link
Member Author

Here's a data point:

On my computer it makes stage3-release go from false positive compilation errors to "segmentation fault". Is this forwards progress or backwards progress? I have no idea. I will note however than before the LLVM 14 upgrade, I was getting "segmentation fault" when running stage3-release on the behavior tests.

topolarity added a commit to topolarity/zig that referenced this issue Jul 19, 2022
This is a workaround for llvm/llvm-project#56585
which causes writes to i1 in memory to be optimized to an incorrect value.
Unfortunately, this does not save users from running into this bug with u1
in their own types.

However, this does seem to be enough to get the behavior tests working.
This resolves ziglang#11450 on my machine.
andrewrk pushed a commit that referenced this issue Jul 19, 2022
This is a workaround for llvm/llvm-project#56585
which causes writes to i1 in memory to be optimized to an incorrect value.
Unfortunately, this does not save users from running into this bug with u1
in their own types.

However, this does seem to be enough to get the behavior tests working.
This resolves #11450 on my machine.
andrewrk added a commit that referenced this issue Jul 19, 2022
This is a possible workaround for
llvm/llvm-project#56585

On my computer it makes stage3-release go from false positive
compilation errors on the behavior tests to "segmentation fault".
Is this forwards progress or backwards progress? I have no idea.

See #11450
andrewrk added a commit that referenced this issue Jul 19, 2022
This branch originally started out as a potential workaround to
address #11450. It did not solve that problem, however, it did end
up fixing #11498!
andrewrk pushed a commit that referenced this issue Jul 19, 2022
This is a workaround for llvm/llvm-project#56585
which causes writes to i1 in memory to be optimized to an incorrect value.
Unfortunately, this does not save users from running into this bug with u1
in their own types.

However, this does seem to be enough to get the behavior tests working.
This resolves #11450 on my machine.
andrewrk added a commit that referenced this issue Jul 19, 2022
This is a possible workaround for
llvm/llvm-project#56585

On my computer it makes stage3-release go from false positive
compilation errors on the behavior tests to "segmentation fault".
Is this forwards progress or backwards progress? I have no idea.

See #11450
andrewrk added a commit that referenced this issue Jul 19, 2022
This branch originally started out as a potential workaround to
address #11450. It did not solve that problem, however, it did end
up fixing #11498!
@kamidev
Copy link

kamidev commented Jul 19, 2022

Stage3 behavior tests still fail on M1 Mac, using zig version 0.10.0-dev.3034.
Building zig using current zlib workaround:
cmake .. -DCMAKE_PREFIX_PATH=$(brew --prefix llvm) -DZIG_PREFER_CLANG_CPP_DYLIB=true -DZIG_STATIC_ZLIB=ON

> ./stage1/bin/zig build -p stage2 -Denable-llvm
> ./stage2/bin/zig build -p stage3 -Denable-llvm

Stage1 and stage2 behavior tests pass without problems.

@kamidev
Copy link

kamidev commented Jul 19, 2022

➜  build git:(master) stage3 test ../test/behavior.zig -I../test
Semantic Analysis [3701] native_endian... thread 4995624 panic: Segmentation fault at address 0xa0
Analyzing ../test/behavior/translate_c_macros.zig: behavior/translate_c_macros.zig:h
    > %18 = c_import({
        %19 = str("behavior/translate_c_macros.h")
        %20 = as_node(@Zir.Inst.Ref.const_slice_u8_type, %19)
        %21 = extended(c_include(%20))
        %22 = ensure_result_used(@Zir.Inst.Ref.void_value)
        %23 = break_inline(%18, @Zir.Inst.Ref.void_value)
      })
      %24 = break_inline(%17, %18)
    For full context, use the command
      zig ast-check -t ../test/behavior/translate_c_macros.zig

  in ../test/behavior/translate_c_macros.zig: behavior/translate_c_macros.zig:test.casting to void with a macro
    > %28 = decl_ref("h")


Panicked during a panic: reached unreachable code
Inner panic stack:
/Users/jonas/src/zig/zig/build/stage2/lib/zig/std/debug.zig:281:9: 0x10084ba3b in assert (zig)
    if (!ok) unreachable; // assertion failure
        ^
/Users/jonas/src/zig/zig/build/stage2/lib/zig/std/debug.zig:1068:11: 0x10087ee7b in readMachODebugInfo (zig)
    assert(state == .oso_close);
          ^
/Users/jonas/src/zig/zig/build/stage2/lib/zig/std/debug.zig:1237:54: 0x10087debb in lookupModuleDyld (zig)
                    obj_di.* = try readMachODebugInfo(self.allocator, macho_file);
                                                     ^
/Users/jonas/src/zig/zig/build/stage2/lib/zig/std/debug.zig:1181:41: 0x10087da0b in getModuleForAddress (zig)
            return self.lookupModuleDyld(address);
                                        ^
/Users/jonas/src/zig/zig/build/stage2/lib/zig/std/debug.zig:708:50: 0x100895913 in printSourceAtAddress__anon_4491 (zig)
    const module = debug_info.getModuleForAddress(address) catch |err| switch (err) {
                                                 ^
/Users/jonas/src/zig/zig/build/stage2/lib/zig/std/debug.zig:187:29: 0x100d17723 in dumpStackTraceFromBase (zig)
        printSourceAtAddress(debug_info, stderr, ip, tty_config) catch return;
                            ^
/Users/jonas/src/zig/zig/src/crash_report.zig:327:45: 0x100bd16db in dumpStackTrace (zig)
                debug.dumpStackTraceFromBase(ex.bp, ex.ip);
                                            ^
/Users/jonas/src/zig/zig/src/crash_report.zig:479:39: 0x100a31dc7 in reportStack (zig)
        state.panic_ctx.dumpStackTrace();
                                      ^
/Users/jonas/src/zig/zig/src/crash_report.zig:553:9: 0x1008971a7 in initPanic (zig)
        @call(.{}, func, args);
        ^
/Users/jonas/src/zig/zig/src/crash_report.zig:553:9: 0x10084b577 in dispatch (zig)
        @call(.{}, func, args);
        ^
/Users/jonas/src/zig/zig/src/crash_report.zig:254:25: 0x100897617 in handleSegfaultPosix (zig)
    PanicSwitch.dispatch(null, stack_ctx, error_msg);
                        ^
???:?:?: 0x1a81cf4a3 in ??? (???)
???:?:?: 0x795b00010168cc9f in ??? (???)
???:?:?: 0x10168cbab in _ZigClangPreprocessingRecord_iterator_deref (???)
/Users/jonas/src/zig/zig/src/translate_c.zig:452:32: 0x100a695c3 in prepopulateGlobalNameTable (zig)
        const entity = it.deref();
                               ^
/Users/jonas/src/zig/zig/src/translate_c.zig:423:35: 0x100a76bd7 in translate (zig)
    try prepopulateGlobalNameTable(ast_unit, &context);
                                  ^
/Users/jonas/src/zig/zig/src/Compilation.zig:3490:41: 0x101030653 in cImport (zig)
        var tree = translate_c.translate(
                                        ^
/Users/jonas/src/zig/zig/src/Sema.zig:4569:42: 0x100e84ac3 in zirCImport (zig)
    const c_import_res = mod.comp.cImport(c_import_buf.items) catch |err|
                                         ^
/Users/jonas/src/zig/zig/src/Sema.zig:712:65: 0x100d5256b in analyzeBodyInner (zig)
            .c_import                     => try sema.zirCImport(block, inst),
                                                                ^
/Users/jonas/src/zig/zig/src/Sema.zig:615:45: 0x100c276bb in analyzeBodyBreak (zig)
    const break_inst = sema.analyzeBodyInner(block, body) catch |err| switch (err) {
                                            ^
/Users/jonas/src/zig/zig/src/Module.zig:4077:50: 0x100c2511b in semaDecl (zig)
    const result_ref = (try sema.analyzeBodyBreak(&block_scope, body)).?.operand;
                                                 ^
/Users/jonas/src/zig/zig/src/Module.zig:3714:38: 0x100abac73 in ensureDeclAnalyzed (zig)
    const type_changed = mod.semaDecl(decl_index) catch |err| switch (err) {
                                     ^
/Users/jonas/src/zig/zig/src/Sema.zig:23207:32: 0x10105cb27 in ensureDeclAnalyzed (zig)
    sema.mod.ensureDeclAnalyzed(decl_index) catch |err| {
                               ^
/Users/jonas/src/zig/zig/src/Sema.zig:23249:32: 0x100eeb3b7 in analyzeDeclRef (zig)
    try sema.ensureDeclAnalyzed(decl_index);
                               ^
/Users/jonas/src/zig/zig/src/Sema.zig:5147:31: 0x100e877af in zirDeclRef (zig)
    return sema.analyzeDeclRef(decl_index);
                              ^
/Users/jonas/src/zig/zig/src/Sema.zig:722:65: 0x100d52bcb in analyzeBodyInner (zig)
            .decl_ref                     => try sema.zirDeclRef(block, inst),
                                                                ^
/Users/jonas/src/zig/zig/src/Sema.zig:598:30: 0x100d5133b in analyzeBody (zig)
    _ = sema.analyzeBodyInner(block, body) catch |err| switch (err) {
                             ^
/Users/jonas/src/zig/zig/src/Module.zig:5072:21: 0x100c1a473 in analyzeFnBody (zig)
    sema.analyzeBody(&inner_block, fn_info.body) catch |err| switch (err) {
                    ^
/Users/jonas/src/zig/zig/src/Module.zig:3800:40: 0x100aba0df in ensureFuncBodyAnalyzed (zig)
            var air = mod.analyzeFnBody(func, sema_arena) catch |err| switch (err) {
                                       ^
/Users/jonas/src/zig/zig/src/Compilation.zig:2970:42: 0x100ab8c47 in processOneJob (zig)
            module.ensureFuncBodyAnalyzed(func) catch |err| switch (err) {
                                         ^
/Users/jonas/src/zig/zig/src/Compilation.zig:2902:30: 0x1009f2b53 in performAllTheWork (zig)
            try processOneJob(comp, work_item);
                             ^
/Users/jonas/src/zig/zig/src/Compilation.zig:2250:31: 0x1009f02b7 in update (zig)
    try comp.performAllTheWork(main_progress_node);
                              ^
/Users/jonas/src/zig/zig/src/main.zig:3294:20: 0x100a0b92f in updateModule (zig)
    try comp.update();
                   ^
/Users/jonas/src/zig/zig/src/main.zig:2983:17: 0x10086bbe7 in buildOutputType (zig)
    updateModule(gpa, comp, hook) catch |err| switch (err) {
                ^
/Users/jonas/src/zig/zig/src/main.zig:225:31: 0x10084a47b in mainArgs (zig)
        return buildOutputType(gpa, arena, args, .zig_test);
                              ^
/Users/jonas/src/zig/zig/src/main.zig:174:20: 0x100849d53 in main (zig)
    return mainArgs(gpa, arena, args);
                   ^
/Users/jonas/src/zig/zig/build/stage2/lib/zig/std/start.zig:580:37: 0x10084b1b3 in main (zig)
            const result = root.main() catch |err| {
                                    ^
???:?:?: 0x102fb908b in ??? (???)
???:?:?: 0xb85fffffffffffff in ??? (???)
[1]    99051 abort      /Users/jonas/src/zig/zig/build/stage3/bin/zig test ../test/behavior.zig

@kamidev
Copy link

kamidev commented Jul 20, 2022

Stage3 behavior tests still fail on M1, using zig version 0.10.0-dev.3041. Panics for the same test, stacktrace still shows unreachable code in stage2.

@kamidev
Copy link

kamidev commented Jul 20, 2022

➜  build git:(master) stage3 test ../test/behavior.zig -I../test
Semantic Analysis [3735] state... thread 57019 panic: Segmentation fault at address 0x90
Analyzing ../test/behavior/translate_c_macros.zig: behavior/translate_c_macros.zig:h
    > %18 = c_import({
        %19 = str("behavior/translate_c_macros.h")
        %20 = as_node(@Zir.Inst.Ref.const_slice_u8_type, %19)
        %21 = extended(c_include(%20))
        %22 = ensure_result_used(@Zir.Inst.Ref.void_value)
        %23 = break_inline(%18, @Zir.Inst.Ref.void_value)
      })
      %24 = break_inline(%17, %18)
    For full context, use the command
      zig ast-check -t ../test/behavior/translate_c_macros.zig

  in ../test/behavior/translate_c_macros.zig: behavior/translate_c_macros.zig:test.casting to void with a macro
    > %28 = decl_ref("h")


Panicked during a panic: reached unreachable code
Inner panic stack:
/Users/jonas/src/zig/zig/build/stage2/lib/zig/std/debug.zig:281:9: 0x104a42a0b in assert (zig)
    if (!ok) unreachable; // assertion failure
        ^
/Users/jonas/src/zig/zig/build/stage2/lib/zig/std/debug.zig:1068:11: 0x104a75e4b in readMachODebugInfo (zig)
    assert(state == .oso_close);
          ^
/Users/jonas/src/zig/zig/build/stage2/lib/zig/std/debug.zig:1237:54: 0x104a74e8b in lookupModuleDyld (zig)
                    obj_di.* = try readMachODebugInfo(self.allocator, macho_file);
                                                     ^
/Users/jonas/src/zig/zig/build/stage2/lib/zig/std/debug.zig:1181:41: 0x104a749db in getModuleForAddress (zig)
            return self.lookupModuleDyld(address);
                                        ^
/Users/jonas/src/zig/zig/build/stage2/lib/zig/std/debug.zig:708:50: 0x104a8c8e3 in printSourceAtAddress__anon_4491 (zig)
    const module = debug_info.getModuleForAddress(address) catch |err| switch (err) {
                                                 ^
/Users/jonas/src/zig/zig/build/stage2/lib/zig/std/debug.zig:187:29: 0x104f0e6b7 in dumpStackTraceFromBase (zig)
        printSourceAtAddress(debug_info, stderr, ip, tty_config) catch return;
                            ^
/Users/jonas/src/zig/zig/src/crash_report.zig:327:45: 0x104dc866f in dumpStackTrace (zig)
                debug.dumpStackTraceFromBase(ex.bp, ex.ip);
                                            ^
/Users/jonas/src/zig/zig/src/crash_report.zig:479:39: 0x104c28d5b in reportStack (zig)
        state.panic_ctx.dumpStackTrace();
                                      ^
/Users/jonas/src/zig/zig/src/crash_report.zig:553:9: 0x104a8e177 in initPanic (zig)
        @call(.{}, func, args);
        ^
/Users/jonas/src/zig/zig/src/crash_report.zig:553:9: 0x104a42547 in dispatch (zig)
        @call(.{}, func, args);
        ^
/Users/jonas/src/zig/zig/src/crash_report.zig:254:25: 0x104a8e5e7 in handleSegfaultPosix (zig)
    PanicSwitch.dispatch(null, stack_ctx, error_msg);
                        ^
???:?:?: 0x1870cf4a3 in ??? (???)
???:?:?: 0x3a17800105884ca7 in ??? (???)
???:?:?: 0x105884bb3 in _ZigClangPreprocessingRecord_iterator_deref (???)
/Users/jonas/src/zig/zig/src/translate_c.zig:452:32: 0x104c60557 in prepopulateGlobalNameTable (zig)
        const entity = it.deref();
                               ^
/Users/jonas/src/zig/zig/src/translate_c.zig:423:35: 0x104c6db6b in translate (zig)
    try prepopulateGlobalNameTable(ast_unit, &context);
                                  ^
/Users/jonas/src/zig/zig/src/Compilation.zig:3490:41: 0x1052289d7 in cImport (zig)
        var tree = translate_c.translate(
                                        ^
/Users/jonas/src/zig/zig/src/Sema.zig:4569:42: 0x10507c46f in zirCImport (zig)
    const c_import_res = mod.comp.cImport(c_import_buf.items) catch |err|
                                         ^
/Users/jonas/src/zig/zig/src/Sema.zig:712:65: 0x104f494ff in analyzeBodyInner (zig)
            .c_import                     => try sema.zirCImport(block, inst),
                                                                ^
/Users/jonas/src/zig/zig/src/Sema.zig:615:45: 0x104e1e64f in analyzeBodyBreak (zig)
    const break_inst = sema.analyzeBodyInner(block, body) catch |err| switch (err) {
                                            ^
/Users/jonas/src/zig/zig/src/Module.zig:4077:50: 0x104e1c0af in semaDecl (zig)
    const result_ref = (try sema.analyzeBodyBreak(&block_scope, body)).?.operand;
                                                 ^
/Users/jonas/src/zig/zig/src/Module.zig:3714:38: 0x104cb1c07 in ensureDeclAnalyzed (zig)
    const type_changed = mod.semaDecl(decl_index) catch |err| switch (err) {
                                     ^
/Users/jonas/src/zig/zig/src/Sema.zig:23201:32: 0x105254e07 in ensureDeclAnalyzed (zig)
    sema.mod.ensureDeclAnalyzed(decl_index) catch |err| {
                               ^
/Users/jonas/src/zig/zig/src/Sema.zig:23243:32: 0x1050e2e37 in analyzeDeclRef (zig)
    try sema.ensureDeclAnalyzed(decl_index);
                               ^
/Users/jonas/src/zig/zig/src/Sema.zig:5151:31: 0x10507f15b in zirDeclRef (zig)
    return sema.analyzeDeclRef(decl_index);
                              ^
/Users/jonas/src/zig/zig/src/Sema.zig:722:65: 0x104f49b5f in analyzeBodyInner (zig)
            .decl_ref                     => try sema.zirDeclRef(block, inst),
                                                                ^
/Users/jonas/src/zig/zig/src/Sema.zig:598:30: 0x104f482cf in analyzeBody (zig)
    _ = sema.analyzeBodyInner(block, body) catch |err| switch (err) {
                             ^
/Users/jonas/src/zig/zig/src/Module.zig:5072:21: 0x104e11407 in analyzeFnBody (zig)
    sema.analyzeBody(&inner_block, fn_info.body) catch |err| switch (err) {
                    ^
/Users/jonas/src/zig/zig/src/Module.zig:3800:40: 0x104cb1073 in ensureFuncBodyAnalyzed (zig)
            var air = mod.analyzeFnBody(func, sema_arena) catch |err| switch (err) {
                                       ^
/Users/jonas/src/zig/zig/src/Compilation.zig:2970:42: 0x104cafbdb in processOneJob (zig)
            module.ensureFuncBodyAnalyzed(func) catch |err| switch (err) {
                                         ^
/Users/jonas/src/zig/zig/src/Compilation.zig:2902:30: 0x104be9ae7 in performAllTheWork (zig)
            try processOneJob(comp, work_item);
                             ^
/Users/jonas/src/zig/zig/src/Compilation.zig:2250:31: 0x104be724b in update (zig)
    try comp.performAllTheWork(main_progress_node);
                              ^
/Users/jonas/src/zig/zig/src/main.zig:3294:20: 0x104c028c3 in updateModule (zig)
    try comp.update();
                   ^
/Users/jonas/src/zig/zig/src/main.zig:2983:17: 0x104a62bb7 in buildOutputType (zig)
    updateModule(gpa, comp, hook) catch |err| switch (err) {
                ^
/Users/jonas/src/zig/zig/src/main.zig:225:31: 0x104a4144b in mainArgs (zig)
        return buildOutputType(gpa, arena, args, .zig_test);
                              ^
/Users/jonas/src/zig/zig/src/main.zig:174:20: 0x104a40d23 in main (zig)
    return mainArgs(gpa, arena, args);
                   ^
/Users/jonas/src/zig/zig/build/stage2/lib/zig/std/start.zig:580:37: 0x104a42183 in main (zig)
            const result = root.main() catch |err| {
                                    ^
???:?:?: 0x1073b908b in ??? (???)
???:?:?: 0x2506ffffffffffff in ??? (???)
[1]    4745 abort      /Users/jonas/src/zig/zig/build/stage3/bin/zig test ../test/behavior.zig

wooster0 pushed a commit to wooster0/zig that referenced this issue Jul 24, 2022
This is a workaround for llvm/llvm-project#56585
which causes writes to i1 in memory to be optimized to an incorrect value.
Unfortunately, this does not save users from running into this bug with u1
in their own types.

However, this does seem to be enough to get the behavior tests working.
This resolves ziglang#11450 on my machine.
wooster0 pushed a commit to wooster0/zig that referenced this issue Jul 24, 2022
This is a possible workaround for
llvm/llvm-project#56585

On my computer it makes stage3-release go from false positive
compilation errors on the behavior tests to "segmentation fault".
Is this forwards progress or backwards progress? I have no idea.

See ziglang#11450
wooster0 pushed a commit to wooster0/zig that referenced this issue Jul 24, 2022
This branch originally started out as a potential workaround to
address ziglang#11450. It did not solve that problem, however, it did end
up fixing ziglang#11498!
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.

5 participants