diff options
author | Ayke van Laethem <[email protected]> | 2022-04-19 15:31:23 +0200 |
---|---|---|
committer | Ron Evans <[email protected]> | 2022-05-30 07:53:28 +0200 |
commit | 777d3f3ea5a570615b615e323c977f73d6674507 (patch) | |
tree | ac5bf4b4e169f37b81cec5dfcddfc6e1b0814899 /transform | |
parent | ea3b5dc68979da59e6a62ed64bce7369ad1f0e22 (diff) | |
download | tinygo-777d3f3ea5a570615b615e323c977f73d6674507.tar.gz tinygo-777d3f3ea5a570615b615e323c977f73d6674507.zip |
builder: free LLVM objects after use
This reduces the TinyGo memory consumption when running
make tinygo-test
from 5.8GB to around 2GB on my laptop.
Diffstat (limited to 'transform')
-rw-r--r-- | transform/allocs.go | 2 | ||||
-rw-r--r-- | transform/gc.go | 3 | ||||
-rw-r--r-- | transform/interface-lowering.go | 6 | ||||
-rw-r--r-- | transform/reflect.go | 6 | ||||
-rw-r--r-- | transform/rtcalls.go | 4 | ||||
-rw-r--r-- | transform/transform_test.go | 3 |
6 files changed, 20 insertions, 4 deletions
diff --git a/transform/allocs.go b/transform/allocs.go index 35fa1b0bb..af7c70b28 100644 --- a/transform/allocs.go +++ b/transform/allocs.go @@ -36,8 +36,10 @@ func OptimizeAllocs(mod llvm.Module, printAllocs *regexp.Regexp, logger func(tok } targetData := llvm.NewTargetData(mod.DataLayout()) + defer targetData.Dispose() i8ptrType := llvm.PointerType(mod.Context().Int8Type(), 0) builder := mod.Context().NewBuilder() + defer builder.Dispose() for _, heapalloc := range getUses(allocator) { logAllocs := printAllocs != nil && printAllocs.MatchString(heapalloc.InstructionParent().Parent().Name()) diff --git a/transform/gc.go b/transform/gc.go index 190760ace..c8326f699 100644 --- a/transform/gc.go +++ b/transform/gc.go @@ -33,7 +33,9 @@ func MakeGCStackSlots(mod llvm.Module) bool { ctx := mod.Context() builder := ctx.NewBuilder() + defer builder.Dispose() targetData := llvm.NewTargetData(mod.DataLayout()) + defer targetData.Dispose() uintptrType := ctx.IntType(targetData.PointerSize() * 8) // Look at *all* functions to see whether they are free of function pointer @@ -326,6 +328,7 @@ func AddGlobalsBitmap(mod llvm.Module) bool { ctx := mod.Context() targetData := llvm.NewTargetData(mod.DataLayout()) + defer targetData.Dispose() uintptrType := ctx.IntType(targetData.PointerSize() * 8) // Collect all globals that contain pointers (and thus must be scanned by diff --git a/transform/interface-lowering.go b/transform/interface-lowering.go index d2d4b4f48..7a01c2ea7 100644 --- a/transform/interface-lowering.go +++ b/transform/interface-lowering.go @@ -101,19 +101,23 @@ type lowerInterfacesPass struct { // before LLVM can work on them. This is done so that a few cleanup passes can // run before assigning the final type codes. func LowerInterfaces(mod llvm.Module, config *compileopts.Config) error { + targetData := llvm.NewTargetData(mod.DataLayout()) + defer targetData.Dispose() p := &lowerInterfacesPass{ mod: mod, config: config, builder: mod.Context().NewBuilder(), ctx: mod.Context(), - uintptrType: mod.Context().IntType(llvm.NewTargetData(mod.DataLayout()).PointerSize() * 8), + uintptrType: mod.Context().IntType(targetData.PointerSize() * 8), types: make(map[string]*typeInfo), signatures: make(map[string]*signatureInfo), interfaces: make(map[string]*interfaceInfo), } + defer p.builder.Dispose() if config.Debug() { p.dibuilder = llvm.NewDIBuilder(mod) + defer p.dibuilder.Destroy() defer p.dibuilder.Finalize() p.difiles = make(map[string]llvm.Metadata) } diff --git a/transform/reflect.go b/transform/reflect.go index 188b0a3c1..49bd449ac 100644 --- a/transform/reflect.go +++ b/transform/reflect.go @@ -161,10 +161,12 @@ func LowerReflect(mod llvm.Module) { }) // Assign typecodes the way the reflect package expects. - uintptrType := mod.Context().IntType(llvm.NewTargetData(mod.DataLayout()).PointerSize() * 8) + targetData := llvm.NewTargetData(mod.DataLayout()) + defer targetData.Dispose() + uintptrType := mod.Context().IntType(targetData.PointerSize() * 8) state := typeCodeAssignmentState{ fallbackIndex: 1, - uintptrLen: llvm.NewTargetData(mod.DataLayout()).PointerSize() * 8, + uintptrLen: targetData.PointerSize() * 8, namedBasicTypes: make(map[string]int), namedNonBasicTypes: make(map[string]int), arrayTypes: make(map[string]int), diff --git a/transform/rtcalls.go b/transform/rtcalls.go index 49f7f0d94..6cd5cc4c9 100644 --- a/transform/rtcalls.go +++ b/transform/rtcalls.go @@ -114,7 +114,9 @@ func OptimizeReflectImplements(mod llvm.Module) { defer builder.Dispose() // Get a few useful object for use later. - uintptrType := mod.Context().IntType(llvm.NewTargetData(mod.DataLayout()).PointerSize() * 8) + targetData := llvm.NewTargetData(mod.DataLayout()) + defer targetData.Dispose() + uintptrType := mod.Context().IntType(targetData.PointerSize() * 8) // Look up the (reflect.Value).Implements() method. var implementsFunc llvm.Value diff --git a/transform/transform_test.go b/transform/transform_test.go index bde1351ff..9ad724346 100644 --- a/transform/transform_test.go +++ b/transform/transform_test.go @@ -32,6 +32,7 @@ var defaultTestConfig = &compileopts.Config{ func testTransform(t *testing.T, pathPrefix string, transform func(mod llvm.Module)) { // Read the input IR. ctx := llvm.NewContext() + defer ctx.Dispose() buf, err := llvm.NewMemoryBufferFromFile(pathPrefix + ".ll") os.Stat(pathPrefix + ".ll") // make sure this file is tracked by `go test` caching if err != nil { @@ -41,6 +42,7 @@ func testTransform(t *testing.T, pathPrefix string, transform func(mod llvm.Modu if err != nil { t.Fatalf("could not load module:\n%v", err) } + defer mod.Dispose() // Perform the transform. transform(mod) @@ -141,6 +143,7 @@ func compileGoFileForTesting(t *testing.T, filename string) llvm.Module { if err != nil { t.Fatal("failed to create target machine:", err) } + defer machine.Dispose() // Load entire program AST into memory. lprogram, err := loader.Load(config, []string{filename}, config.ClangHeaders, types.Config{ |