Skip to content

Device lane spill: a loop at the exact fill depth spills and restores the whole stack on every call (~200x slower) #1790

Open

Description

@Lorenzobattistela

Since #1439, a ! job whose lane stack fills moves the words below the calling def to a heap block and moves them all back when it returns to that point. If code keeps calling right at the depth where the stack fills, each call spills about 2,048 words and each return restores them, so the lane comes back full every time.

Repro (Apple M4 Pro, Metal, -o build):

import Base

def inc(n: Nat) -> U32:
  match n:
    case 0n:
      0
    case 1n+p:
      (inc(p) + 1 : U32)

def loop(m: Nat, +acc: U32) -> U32:
  match m:
    case 0n:
      acc
    case 1n+q:
      a = inc(1n)
      loop(q, (acc + a : U32))

def down(n: Nat, +m: Nat) -> U32:
  match n:
    case 0n:
      loop(m, 0)
    case 1n+p:
      (down(p, m) + 1 : U32)

def hi(+m: Nat) -> U32:
  down(2042n, m)

def main() -> IO(Unit):
  IO.print(U32.show(hi!(200000n)))
depth in hi GPU --gpu off
2041 0.22 s -
2042 48.7 s 0.00 s
2043 0.22 s -

All three print the right result. Before #1439, depths like this faulted, so this is not a regression; it is the cost model of the spill.

A fix would spill only the bottom part of the stack, so that a restore leaves room. The hard part is that the exit frame must sit on a frame boundary, and at runtime the lane does not know where its frames start and end: each frame's size depends on its segment. Spilling part of the stack needs either frame sizes recorded on the stack or per-segment size tables to walk 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