aboutsummaryrefslogtreecommitdiffhomepage
path: root/builder
diff options
context:
space:
mode:
authorAyke van Laethem <[email protected]>2022-04-19 15:31:23 +0200
committerRon Evans <[email protected]>2022-05-30 07:53:28 +0200
commit777d3f3ea5a570615b615e323c977f73d6674507 (patch)
treeac5bf4b4e169f37b81cec5dfcddfc6e1b0814899 /builder
parentea3b5dc68979da59e6a62ed64bce7369ad1f0e22 (diff)
downloadtinygo-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 'builder')
-rw-r--r--builder/build.go11
1 files changed, 11 insertions, 0 deletions
diff --git a/builder/build.go b/builder/build.go
index 77a4e1647..629121398 100644
--- a/builder/build.go
+++ b/builder/build.go
@@ -188,6 +188,7 @@ func Build(pkgName, outpath string, config *compileopts.Config, action func(Buil
if err != nil {
return err
}
+ defer machine.Dispose()
// Load entire program AST into memory.
lprogram, err := loader.Load(config, []string{pkgName}, config.ClangHeaders, types.Config{
@@ -287,6 +288,8 @@ func Build(pkgName, outpath string, config *compileopts.Config, action func(Buil
// Compile AST to IR. The compiler.CompilePackage function will
// build the SSA as needed.
mod, errs := compiler.CompilePackage(pkg.ImportPath, pkg, program.Package(pkg.Pkg), machine, compilerConfig, config.DumpSSA())
+ defer mod.Context().Dispose()
+ defer mod.Dispose()
if errs != nil {
return newMultiError(errs)
}
@@ -432,6 +435,13 @@ func Build(pkgName, outpath string, config *compileopts.Config, action func(Buil
// Add job that links and optimizes all packages together.
var mod llvm.Module
+ defer func() {
+ if !mod.IsNil() {
+ ctx := mod.Context()
+ mod.Dispose()
+ ctx.Dispose()
+ }
+ }()
var stackSizeLoads []string
programJob := &compileJob{
description: "link+optimize packages (LTO)",
@@ -534,6 +544,7 @@ func Build(pkgName, outpath string, config *compileopts.Config, action func(Buil
if err != nil {
return err
}
+ defer llvmBuf.Dispose()
return ioutil.WriteFile(outpath, llvmBuf.Bytes(), 0666)
case ".bc":
var buf llvm.MemoryBuffer