mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-07-26 06:38:42 +00:00
+1
-9
@@ -13,14 +13,6 @@ import (
|
||||
"tinygo.org/x/go-llvm"
|
||||
)
|
||||
|
||||
// maxStackAlloc is the maximum size of an object that will be allocated on the
|
||||
// stack. Bigger objects have increased risk of stack overflows and thus will
|
||||
// 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
|
||||
// whenever possible. It relies on the LLVM 'nocapture' flag for interprocedural
|
||||
// escape analysis, and within a function looks whether an allocation can escape
|
||||
@@ -28,7 +20,7 @@ const maxStackAlloc = 256
|
||||
// If printAllocs is non-nil, it indicates the regexp of functions for which a
|
||||
// heap allocation explanation should be printed (why the object can't be stack
|
||||
// allocated).
|
||||
func OptimizeAllocs(mod llvm.Module, printAllocs *regexp.Regexp, logger func(token.Position, string)) {
|
||||
func OptimizeAllocs(mod llvm.Module, printAllocs *regexp.Regexp, maxStackAlloc uint64, logger func(token.Position, string)) {
|
||||
allocator := mod.NamedFunction("runtime.alloc")
|
||||
if allocator.IsNil() {
|
||||
// nothing to optimize
|
||||
|
||||
@@ -17,7 +17,7 @@ import (
|
||||
func TestAllocs(t *testing.T) {
|
||||
t.Parallel()
|
||||
testTransform(t, "testdata/allocs", func(mod llvm.Module) {
|
||||
transform.OptimizeAllocs(mod, nil, nil)
|
||||
transform.OptimizeAllocs(mod, nil, 256, nil)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -47,7 +47,7 @@ func TestAllocs2(t *testing.T) {
|
||||
|
||||
// Run heap to stack transform.
|
||||
var testOutputs []allocsTestOutput
|
||||
transform.OptimizeAllocs(mod, regexp.MustCompile("."), func(pos token.Position, msg string) {
|
||||
transform.OptimizeAllocs(mod, regexp.MustCompile("."), 256, func(pos token.Position, msg string) {
|
||||
testOutputs = append(testOutputs, allocsTestOutput{
|
||||
filename: filepath.Base(pos.Filename),
|
||||
line: pos.Line,
|
||||
|
||||
@@ -64,7 +64,8 @@ func Optimize(mod llvm.Module, config *compileopts.Config) []error {
|
||||
// Run TinyGo-specific optimization passes.
|
||||
OptimizeStringToBytes(mod)
|
||||
OptimizeReflectImplements(mod)
|
||||
OptimizeAllocs(mod, nil, nil)
|
||||
maxStackSize := config.MaxStackAlloc()
|
||||
OptimizeAllocs(mod, nil, maxStackSize, nil)
|
||||
err = LowerInterfaces(mod, config)
|
||||
if err != nil {
|
||||
return []error{err}
|
||||
@@ -84,7 +85,7 @@ func Optimize(mod llvm.Module, config *compileopts.Config) []error {
|
||||
}
|
||||
|
||||
// Run TinyGo-specific interprocedural optimizations.
|
||||
OptimizeAllocs(mod, config.Options.PrintAllocs, func(pos token.Position, msg string) {
|
||||
OptimizeAllocs(mod, config.Options.PrintAllocs, maxStackSize, func(pos token.Position, msg string) {
|
||||
fmt.Fprintln(os.Stderr, pos.String()+": "+msg)
|
||||
})
|
||||
OptimizeStringToBytes(mod)
|
||||
|
||||
Reference in New Issue
Block a user