Skip to content

2.0.40: on the CPU, every take of a shared node is a call (#1978), +10% instructions on a list-heavy program #2081

Open

Description

@nuxyel

#1978 fixed the M1 Metal miscompile by moving the shared-node half of ctr_take into span_fade and making span_fade FAR. The PR body says "Host and CUDA keep OUTLINE as before, with the last-holder free now inside it", but the C that 2.0.40 emits for the host has FAR u64 span_fade, so on the CPU the change is more than a move:

  • 2.0.39: ctr_take checks the count inline and frees a last-held node in place. Only a node that really is shared calls span_fade, which is OUTLINE (cold, preserve_most).
  • 2.0.40: every take of a node with the RFC bit calls span_fade, a plain noinline with the normal calling convention. The count check runs inside it.

I saw it on a BPE tokenizer ([email protected], 256 merges learned from a 4,096-word text, then a 32,768-word text encoded). The repro and a script that counts both releases are in https://github.com/nuxyel/bend-ml/blob/devel/docs/upstream/bpe_encode.bend and bpe_encode.sh. The setup was one thread pinned to a P-core of a Core Ultra 7 155H, clang 23.1.3 with the -std=c11 -O3 that bend -o uses, and perf stat in user mode. The output is the same on every build.

  • 2.0.39 runs 177.7 G instructions and 2.0.40 runs 194.2 G (+9.3%). Cycles go from 33.7 G to 43.1 G, part of which is a second, separate regression (2.0.40: after #2036, a BPE training loop takes 5.6x the L1 misses (+11% cycles) #2082).
  • I bisected it to On Metal, the shared-node half of a take is a call, not code inlined into the taken match #1978. At that commit the count goes from 177.9 G to 196.3 G instructions (+10.3%), and from 33.6 G to 39.1 G cycles. L1 misses stay where they were.
  • I added a counter to the C. The program calls span_fade 1.21 G times on 2.0.40, against 39.5 M times on 2.0.39. 1.15 G of those calls are takes of a last-held node, which 2.0.39 handles inline.
  • With ctr_take and span_fade put back as in 2.0.39 in comp.ts at v2.0.40 (only that text, built with bun bend2/main.ts), the program runs 173.6 G instructions. That is 10.6% fewer than the release and 2.3% under 2.0.39.
  • Keeping 2.0.40's shape but marking span_fade OUTLINE makes it worse: 209.1 G instructions. preserve_most is then paid on every one of those 1.2 G calls. On the host, the last-holder check has to stay inline.

My suggestion is to keep 2.0.39's ctr_take and span_fade outside Metal, and #1978's shape under __METAL_VERSION__ only. I can't test the M1 side. The CUDA build goes through the same FAR and may want the same split, but I haven't measured it.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions