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 /interp | |
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 'interp')
-rw-r--r-- | interp/interp.go | 8 | ||||
-rw-r--r-- | interp/interp_test.go | 2 |
2 files changed, 10 insertions, 0 deletions
diff --git a/interp/interp.go b/interp/interp.go index 58de4a3b6..87aadf37e 100644 --- a/interp/interp.go +++ b/interp/interp.go @@ -50,10 +50,17 @@ func newRunner(mod llvm.Module, debug bool) *runner { return &r } +// Dispose deallocates all alloated LLVM resources. +func (r *runner) dispose() { + r.targetData.Dispose() + r.targetData = llvm.TargetData{} +} + // Run evaluates runtime.initAll function as much as possible at compile time. // Set debug to true if it should print output while running. func Run(mod llvm.Module, debug bool) error { r := newRunner(mod, debug) + defer r.dispose() initAll := mod.NamedFunction("runtime.initAll") bb := initAll.EntryBasicBlock() @@ -196,6 +203,7 @@ func RunFunc(fn llvm.Value, debug bool) error { // Create and initialize *runner object. mod := fn.GlobalParent() r := newRunner(mod, debug) + defer r.dispose() initName := fn.Name() if !strings.HasSuffix(initName, ".init") { return errorAt(fn, "interp: unexpected function name (expected *.init)") diff --git a/interp/interp_test.go b/interp/interp_test.go index a1bea65ef..7b2bb6197 100644 --- a/interp/interp_test.go +++ b/interp/interp_test.go @@ -40,6 +40,7 @@ func TestInterp(t *testing.T) { func runTest(t *testing.T, pathPrefix string) { // 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 { @@ -49,6 +50,7 @@ func runTest(t *testing.T, pathPrefix string) { if err != nil { t.Fatalf("could not load module:\n%v", err) } + defer mod.Dispose() // Perform the transform. err = Run(mod, false) |