Skip to content

Commit 81199da

Browse files
niaowaykevl
authored andcommitted
add code to handle programs which use heap allocations but never hit the GC
1 parent 3cedebd commit 81199da

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

compiler/gc.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ func (c *Compiler) makeGCStackSlots() bool {
122122
stackChainStart.SetInitializer(llvm.ConstNull(stackChainStart.Type().ElementType()))
123123
stackChainStart.SetGlobalConstant(true)
124124
}
125+
return false
125126
}
126127

127128
trackPointer := c.mod.NamedFunction("runtime.trackPointer")
@@ -180,7 +181,13 @@ func (c *Compiler) makeGCStackSlots() bool {
180181
// Collect some variables used below in the loop.
181182
stackChainStart := c.mod.NamedGlobal("runtime.stackChainStart")
182183
if stackChainStart.IsNil() {
183-
panic("stack chain start not found!")
184+
// This may be reached in a weird scenario where we call runtime.alloc but the garbage collector is unreachable.
185+
// This can be accomplished by allocating 0 bytes.
186+
// There is no point in tracking anything.
187+
for _, use := range getUses(trackPointer) {
188+
use.EraseFromParentAsInstruction()
189+
}
190+
return false
184191
}
185192
stackChainStartType := stackChainStart.Type().ElementType()
186193
stackChainStart.SetInitializer(llvm.ConstNull(stackChainStartType))

testdata/zeroalloc.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package main
2+
func main() {
3+
p := []byte{}
4+
for len(p) >= 1 {
5+
p = p[1:]
6+
}
7+
}

testdata/zeroalloc.txt

Whitespace-only changes.

0 commit comments

Comments
 (0)