Skip to content

Conversation

@mlugg
Copy link
Member

@mlugg mlugg commented Oct 28, 2025

The logic for computing reference traces was unintentionally finding the longest possible trace (approximately). I think I already tried to fix this before, but misunderstood how my own code works. Here, we fix it properly: by slightly reworking the logic to use one ArrayHashMap for both the result and the traversal queue, we trivially get a proper breadth-first traversal so that we can find the shortest possible reference trace for every referenced unit.


For instance, let's add a @compileError into the compiler internals (Sema.resolveValue for no particular reason) and look at the reference trace before and after this change.

Before:

referenced by:
    resolvePeerTypes: src/Sema.zig:33175:38
    zirResolveInferredAlloc: src/Sema.zig:4394:60
    analyzeBodyInner: src/Sema.zig:1161:78
    analyzeInlineBody: src/Sema.zig:1062:30
    unionFields: src/Sema.zig:35657:39
    resolveUnionFieldTypes: src/Sema.zig:35059:21
    resolveUnionInner: src/Type.zig:3877:47
    resolveFields: src/Type.zig:3704:55
    bitSizeInner__anon_473482: src/Type.zig:1716:37
    abiSizeInner__anon_473150: src/Type.zig:1340:96
    abiAlignmentInner__anon_473148: src/Type.zig:1003:112
    abiAlignmentSema: src/Type.zig:947:37
    print: src/print_value.zig:83:69
    format: src/print_value.zig:36:17
    format [inlined]: lib/std/fmt.zig:274:25
    printValue__anon_482873: lib/std/Io/Writer.zig:1050:39
    print__anon_333281: lib/std/Io/Writer.zig:701:25
    bufPrint__anon_264139: lib/std/fmt.zig:599:12
    idleProgNode: src/link/Elf2.zig:1635:39
    idle: src/link/Elf2.zig:1556:51
    idle: src/link.zig:868:76
    doIdleTask: src/link.zig:1610:43
    flushTaskQueue: src/link/Queue.zig:239:46
    runFn: lib/std/Thread/Pool.zig:180:13
    Thread.Pool.spawnWgId__anon_227885.Closure: lib/std/Thread/Pool.zig:175:34
    spawnWgId__anon_227885: lib/std/Thread/Pool.zig:172:21
    start: src/link/Queue.zig:112:35
    performAllTheWork: src/Compilation.zig:4617:31
    update: src/Compilation.zig:3094:31
    serve: src/main.zig:4115:32
    buildOutputType: src/main.zig:3547:22
    mainArgs: src/main.zig:252:31
    main: src/main.zig:204:20
    callMain [inlined]: lib/std/start.zig:697:37
    callMainWithArgs [inlined]: lib/std/start.zig:657:20
    posixCallMainAndExit: lib/std/start.zig:612:36
    _start: lib/std/start.zig:499:40
    comptime: lib/std/start.zig:96:71
    start: lib/std/std.zig:108:27
    comptime: lib/std/std.zig:195:9

After:

referenced by:
    resolveConstValue: src/Sema.zig:2267:33
    resolveFinalDeclValue: src/Sema.zig:2312:43
    analyzeNavVal: src/Zcu/PerThread.zig:1176:54
    ensureNavValUpToDate: src/Zcu/PerThread.zig:1008:80
    processOneJob: src/Compilation.zig:5162:58
    performAllTheWork: src/Compilation.zig:5017:30
    update: src/Compilation.zig:3094:31
    jitCmd: src/main.zig:5530:28
    mainArgs: src/main.zig:300:22
    main: src/main.zig:204:20
    callMain [inlined]: lib/std/start.zig:697:37
    callMainWithArgs [inlined]: lib/std/start.zig:657:20
    posixCallMainAndExit: lib/std/start.zig:612:36
    _start: lib/std/start.zig:499:40
    comptime: lib/std/start.zig:96:71
    start: lib/std/std.zig:108:27
    comptime: lib/std/std.zig:195:9

The logic for computing reference traces was unintentionally finding the
*longest* possible trace (approximately). I think I already tried to fix
this before, but misunderstood how my own code works. Here, we fix it
properly: by slightly reworking the logic to use one ArrayHashMap for
both the result and the traversal queue, we trivially get a proper
breadth-first traversal so that we can find the shortest possible
reference trace for every referenced unit.
@mlugg mlugg enabled auto-merge (rebase) October 28, 2025 10:49
@mlugg mlugg merged commit 8907dc8 into ziglang:master Oct 29, 2025
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant