compiler: no escape analysis at -opt=0

Aperta
#6,645 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
42/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
go

Direzione di ricerca

Start in transform.Optimize and trace how the function-attrs and OptimizeAllocs passes are scheduled at -opt=0; compare with the escape-analysis behavior cited from cmd/compile. Reproduce the supplied //go:noheap, interrupt-handler, and startup randomness cases at -opt=0. Done means these cases build or run without unintended heap allocation while existing optimization behavior remains correct.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

At -opt=0, transform.Optimize does not run OptimizeAllocs. Each local whose address is passed to a call is allocated on the heap. I think this is a correctness problem, because code that is correct at -opt=1 fails at -opt=0.

With -gc=none, the build fails. #6613 reports this for the rp2 clock code, with object allocated on the heap with -gc=none.

In a //go:noheap function, the build also fails. The runtime.alloc_noheap comment says the call "must either be optimized away or throw a linker error". This program builds at -opt=1. At -opt=0 it fails with object allocated on the heap in //go:noheap function.

package main

//go:noheap
func main() {
	var a int
	add(&a)
}

//go:noinline
func add(p *int) { *p++ }

In an interrupt handler, the program aborts. At -opt=0 this handler calls runtime.alloc for c, and alloc in gc_blocks.go calls runtimeFatal("heap alloc in interrupt"). At -opt=1, c is a stack slot.

func handler(interrupt.Interrupt) {
	c := counter{&total, 1}
	c.add() // func (c *counter) add()
}

#5823 is an earlier case. At -opt=0, hardwareRand allocated n with runtime.alloc before initHeap, and linux programs crashed before main. At -opt=0, Go code cannot pass the address of a stack variable to a call. So 94173b29 replaced getrandom with the AT_RANDOM bytes from getauxval.

The generic startupRand still calls hardwareRand. On windows/amd64 at -opt=0, runtime.run calls initRand before initHeap, and hardwareRand calls runtime.alloc for n. I have not run this on Windows.

OptimizeAllocs has run only above -opt=0 since 42196525 added it as an optimisation. f41b6a3b and 85d223c5 later added the interrupt check and //go:noheap, and both depend on this pass.

The gc compiler runs escape analysis when -N disables optimisations. cmd/compile calls escape.Funcs with no check of -N, and its comment says escape analysis is required. See https://github.com/golang/go/blob/go1.27.4/src/cmd/compile/internal/gc/main.go#L285-L294. With -gcflags='-N -l -m', gc reports c does not escape for the receiver above, and main.main does not call runtime.newobject.

I propose that transform.Optimize also runs function-attrs and OptimizeAllocs at -opt=0. I tried this on the three cases above. All three build, and the handler uses a stack slot.

cc @rdon-key @dgryski

Lingua principale
Go
Stelle
17.8k
Fork
1.1k
Merge medio
1g 4h
PR unite (30g)
380

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di tinygo-org/tinygo

Tutte le issue di tinygo-org/tinygo

Issue simili

Altre issue su Go

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.