#5778·tinygo

transform: promoted heap allocations do not share stack slots

Author: neomantraCreated Sep 28, 2026Updated Sep 29, 2026

Even though #5526 really helped with getting the TinyGo+WASM+BubbleTea stack working, some aspects still fail with TinyGo. lipgloss.Style and list.Delegate cause crazy stack pressure.

While I haven't made an issue/PR, I did make a personal branch that mucked with maxDirectAggeregateValue settings, aggressively special-casing it to 64 for WASM. That worked some, but didn't really solve the problem.

These large lipgloss.Styles get duplicated on the stack and fill it up because their lifetime isn't fully tracked. I worked out the following, obviously with LLMs.


OptimizeAllocs moves a heap allocation to the stack when the pointer does not escape. It creates the alloca in the entry block and adds no llvm.lifetime.start or llvm.lifetime.end markers.

Later optimization removes some promoted allocations. For promoted allocations that remain in memory, missing lifetime markers prevent LLVM stack coloring from reusing their slots across disjoint lifetimes. A function that uses several such allocations one after the other holds a frame slot for each of them.

This is especially costly on targets with small fixed goroutine stacks, such as wasm.

Reproduction

go
package main

import "os"

//go:noinline
func fill(b *[512]byte, x byte) byte {
      for i := range b {
              b[i] = x + byte(i)
      }
      return b[int(x)%512]
}

//go:noinline
func eight(x byte) byte {
      var s byte
      s += fill(new([512]byte), x)
      s += fill(new([512]byte), x+1)
      s += fill(new([512]byte), x+2)
      s += fill(new([512]byte), x+3)
      s += fill(new([512]byte), x+4)
      s += fill(new([512]byte), x+5)
      s += fill(new([512]byte), x+6)
      s += fill(new([512]byte), x+7)
      return s
}

func main() {
      x := byte(len(os.Args))
      println("sum:", eight(x))
}

Environment:

  • TinyGo dev at 615335b4, built with Go 1.26.7 and LLVM 22.1.8
  • wasm-opt version 132, wasm-tools 1.257.1
  • macOS arm64

Build and inspect:

GOOS=js GOARCH=wasm tinygo build -opt=2 -o repro.wasm .
wasm-tools print repro.wasm | grep -A4 'func \$main.eight'

The function starts with the stack pointer decrement. The constant is the frame size.

(func $main.eight (;63;) (type 5) (result i32)
  (local i32 i32 i32 i32 i32 i32 i32 i32 i32)
  global.get $__stack_pointer
  i32.const 4096
  i32.sub

The frame is 4096 bytes. Only one 512 byte array is live at a time. A build with GOOS=wasip1 GOARCH=wasm gives the same 4096 bytes.

Proposed change

Add lifetime markers to promoted allocations when all uses, including uses through derived pointers and returned aliases, can be proven to stay in the allocating basic block. Start the lifetime before initialization and end it after the last use. Leave other allocations unchanged. With this change, the frame of main.eight drops from 4096 to 512 bytes. A PR follows.