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
+2
View File
@@ -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())
+3
View File
@@ -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
+5 -1
View File
@@ -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)
}
+4 -2
View File
@@ -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),
+3 -1
View File
@@ -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
+3
View File
@@ -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{