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