From 4b2edc9a26a84b4870d61ddf09f70369ab9f6f1e Mon Sep 17 00:00:00 2001 From: Damian Gryski Date: Thu, 10 Feb 2022 19:28:34 -0800 Subject: [PATCH] compiler: move allocations > 256 bytes to the heap --- compiler/compiler.go | 5 +++-- transform/allocs.go | 1 + 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/compiler/compiler.go b/compiler/compiler.go index da7d752b..75aed427 100644 --- a/compiler/compiler.go +++ b/compiler/compiler.go @@ -1571,8 +1571,9 @@ func (b *builder) createExpr(expr ssa.Value) (llvm.Value, error) { switch expr := expr.(type) { case *ssa.Alloc: typ := b.getLLVMType(expr.Type().Underlying().(*types.Pointer).Elem()) - if expr.Heap { - size := b.targetData.TypeAllocSize(typ) + size := b.targetData.TypeAllocSize(typ) + // Move all "large" allocations to the heap. This value is also transform.maxStackAlloc. + if expr.Heap || size > 256 { // Calculate ^uintptr(0) maxSize := llvm.ConstNot(llvm.ConstInt(b.uintptrType, 0, false)).ZExtValue() if size > maxSize { diff --git a/transform/allocs.go b/transform/allocs.go index ef5467bc..b1f5ea35 100644 --- a/transform/allocs.go +++ b/transform/allocs.go @@ -18,6 +18,7 @@ import ( // always be heap allocated. // // TODO: tune this, this is just a random value. +// This value is also used in the compiler when translating ssa.Alloc nodes. const maxStackAlloc = 256 // OptimizeAllocs tries to replace heap allocations with stack allocations