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.
This commit is contained in:
Ayke van Laethem
2022-04-19 15:31:23 +02:00
committed by Ron Evans
parent ea3b5dc689
commit 777d3f3ea5
13 changed files with 49 additions and 6 deletions
+8
View File
@@ -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)")
+2
View File
@@ -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)