compiler: no escape analysis at -opt=0
维护者通常 1 天内回复
还没有人认领这个 Issue。
评估
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 新手友好度
- 42/100
- Issue 类型
- 缺陷
- 描述清晰度
- 基本清楚
- 活跃度
- 活跃
- 技术栈
- go
调研方向
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.
由索引模型根据 Issue 内容生成。
描述
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
- 主要语言
- Go
- 星标
- 17.8k
- 派生
- 1.1k
- 平均合并
- 1 天 4 小时
- 30 天内合并 PR
- 380
环境准备
这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
tinygo-org/tinygo 的其他 Issue
-
bug
难度 2/5 1-3 小时 新手友好度 78/100
tinygo-org/tinygo#6727 ·
维护者通常 1 天内回复
-
bug
难度 1/5 1 小时以内 新手友好度 86/100
tinygo-org/tinygo#6711 ·
维护者通常 1 天内回复
-
Builds are not reproducible: cgo snippet paths in debug info可能已有人在做 @tridev-tej 于 2 天前认领。 未关闭enhancement good first issue
难度 2/5 1-3 小时 新手友好度 78/100
tinygo-org/tinygo#6619 ·
维护者通常 1 天内回复
-
Fix the targets that tinygo build -target rejects可能已有人在做 @narguis 于 2 天前认领。 未关闭bug good first issue
难度 2/5 1-3 小时 新手友好度 74/100
tinygo-org/tinygo#6617 · 5 条评论 ·
维护者通常 1 天内回复
-
难度 1/5 1 小时以内 新手友好度 62/100
tinygo-org/tinygo#6312 ·
维护者通常 1 天内回复
查看 tinygo-org/tinygo 的全部 Issue
相似的 Issue
-
enhancement
难度 2/5 1-3 小时 新手友好度 78/100
sresarehumantoo/cc-artex#456 ·
-
难度 2/5 1-3 小时 新手友好度 72/100
维护者通常 1 天内回复
-
factory:subtask
难度 1/5 1 小时以内 新手友好度 75/100
维护者通常 1 天内回复
-
release
难度 1/5 1 小时以内 新手友好度 75/100
github/gh-aw-mcpg#17603 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 70/100
bboerst/stratum-work#351 ·
维护者通常 1 天内回复