From e032118cf310a132a261f09313398379e864c3c1 Mon Sep 17 00:00:00 2001 From: Nuno Cruces Date: Wed, 11 Nov 2026 13:27:35 +0000 Subject: [PATCH] Omit unused Module receivers on provided imports. Co-authored-by: Matthew Suozzo --- fncompiler.go | 3 -- internal/passes/opt.go | 2 +- libc-gen/main.go | 79 +++++++++++++++++++++--------------------- translate.go | 35 +++++++++++++------ 4 files changed, 65 insertions(+), 54 deletions(-) diff --git a/fncompiler.go b/fncompiler.go index c02d9895..3096dd8f 100644 --- a/fncompiler.go +++ b/fncompiler.go @@ -620,9 +620,6 @@ func (fn *funcCompiler) cleanup() { passes.InlineGotoEnd(fn.decl) passes.InlineGotoReturn(fn.decl) passes.UnnestSimple(fn.decl) - if passes.RemoveReceiver(fn.decl) { - fn.call.(*ast.ParenExpr).X = fn.decl.Name - } } type funcBlock struct { diff --git a/internal/passes/opt.go b/internal/passes/opt.go index fa318502..730af30e 100644 --- a/internal/passes/opt.go +++ b/internal/passes/opt.go @@ -310,7 +310,7 @@ func InlineGotoReturn(fn *ast.FuncDecl) { // into a plain function. // Returns true if we need to update call sites. func RemoveReceiver(fn *ast.FuncDecl) bool { - if ast.IsExported(fn.Name.Name) { + if fn.Recv == nil || ast.IsExported(fn.Name.Name) { return false } diff --git a/libc-gen/main.go b/libc-gen/main.go index b2b6d2de..3ed51165 100644 --- a/libc-gen/main.go +++ b/libc-gen/main.go @@ -135,51 +135,21 @@ func main() { } } - queue := append([]string(nil), funcs...) - visited := make(map[string]bool) - var keep []*ast.FuncDecl - // Tree-shake the AST - for len(queue) > 0 { - name := queue[0] - queue = queue[1:] - - if visited[name] { - continue - } - visited[name] = true - - fd, ok := available[name] - if !ok && *wasm == "" { + var keep []*ast.FuncDecl + visited := make(set[string]) + for _, name := range funcs { + if available[name] == nil && *wasm == "" { log.Fatalf("function %s not found in templates", name) - } else if !ok { - continue } - keep = append(keep, fd) - - // Search for other internal dependencies inside this one's scope - ast.Inspect(fd, func(n ast.Node) bool { - if call, ok := n.(*ast.CallExpr); ok { - if id, ok := call.Fun.(*ast.Ident); ok { - if _, exists := available[id.Name]; exists { - queue = append(queue, id.Name) - } - } - } - return true - }) + keep = appendDeps(keep, name, available, visited) } usedImports := make(set[string]) // Execute the rewriting permutations for _, fd := range keep { - fd.Recv = &ast.FieldList{ - List: []*ast.Field{{ - Names: []*ast.Ident{ast.NewIdent("m")}, - Type: &ast.StarExpr{X: ast.NewIdent("Module")}, - }}, - } + fd.Name = mangle.ID(fd.Name.Name, mangle.Internal) astutil.Apply(fd, nil, func(c *astutil.Cursor) bool { if call, ok := c.Node().(*ast.CallExpr); ok { @@ -194,6 +164,7 @@ func main() { return true }) + var recv bool ast.Inspect(fd, func(n ast.Node) bool { switch x := n.(type) { case *ast.SliceExpr: @@ -206,7 +177,12 @@ func main() { } case *ast.CallExpr: if id, ok := x.Fun.(*ast.Ident); ok && available[id.Name] != nil { - x.Fun = &ast.SelectorExpr{X: ast.NewIdent("m"), Sel: mangle.ID(id.Name, mangle.Internal)} + callee := available[id.Name] + if callee.Recv != nil { + x.Fun = &ast.SelectorExpr{X: ast.NewIdent("m"), Sel: callee.Name} + } else { + x.Fun = callee.Name + } } for i, arg := range x.Args { if id, ok := arg.(*ast.Ident); ok && id.Name == "memory" { @@ -222,6 +198,9 @@ func main() { } case *ast.SelectorExpr: if id, ok := x.X.(*ast.Ident); ok { + if id.Name == "m" { + recv = true + } if path, exists := allImports[id.Name]; exists { usedImports.add(path) } @@ -230,8 +209,14 @@ func main() { return true }) - // Finally mangle the function's own name - fd.Name = mangle.ID(fd.Name.Name, mangle.Internal) + if recv { + fd.Recv = &ast.FieldList{ + List: []*ast.Field{{ + Names: []*ast.Ident{ast.NewIdent("m")}, + Type: &ast.StarExpr{X: ast.NewIdent("Module")}, + }}, + } + } } // Write the unified file @@ -273,6 +258,22 @@ func main() { } } +func appendDeps(keep []*ast.FuncDecl, name string, available map[string]*ast.FuncDecl, visited set[string]) []*ast.FuncDecl { + fd := available[name] + if fd == nil || !visited.add(name) { + return keep + } + ast.Inspect(fd, func(n ast.Node) bool { + if call, ok := n.(*ast.CallExpr); ok { + if id, ok := call.Fun.(*ast.Ident); ok { + keep = appendDeps(keep, id.Name, available, visited) + } + } + return true + }) + return append(keep, fd) +} + func makeMemoryExpr(deref bool) ast.Expr { sel := &ast.SelectorExpr{X: ast.NewIdent("m"), Sel: ast.NewIdent("memory")} if deref { diff --git a/translate.go b/translate.go index f309d7c4..38a16802 100644 --- a/translate.go +++ b/translate.go @@ -64,7 +64,7 @@ type translator struct { out ast.File // Dependencies. packages set[string] - provided set[string] + provided map[string]bool helpers set[string] // Sections. types []funcType @@ -94,7 +94,7 @@ func translate(r io.Reader, w io.Writer) error { fset := token.NewFileSet() t.packages = set[string]{} - t.provided = set[string]{} + t.provided = map[string]bool{} t.helpers = set[string]{} helperNames, err := t.findHelpers(fset, helpersSrc, helpersAtomicsSrc) @@ -108,12 +108,12 @@ func translate(r io.Reader, w io.Writer) error { } for _, decl := range f.Decls { if fn, ok := decl.(*ast.FuncDecl); ok { - // Check if the receiver type is *Module. - if fn.Recv != nil { - if star, ok := fn.Recv.List[0].Type.(*ast.StarExpr); ok { - if id, ok := star.X.(*ast.Ident); ok && id.Name == "Module" { - t.provided.add(fn.Name.Name) - } + // Check if fn is a plain function, or if the receiver type is *Module. + if fn.Recv == nil { + t.provided[fn.Name.Name] = false + } else if star, ok := fn.Recv.List[0].Type.(*ast.StarExpr); ok { + if id, ok := star.X.(*ast.Ident); ok && id.Name == "Module" { + t.provided[fn.Name.Name] = true } } // Provided files may call helpers by name. @@ -263,6 +263,15 @@ func translate(r io.Reader, w io.Writer) error { } } + for modified := !*noopt; modified; { + modified = false + for _, fn := range t.functions { + if passes.RemoveReceiver(fn.decl) { + fn.call.(*ast.ParenExpr).X = fn.decl.Name + modified = true + } + } + } passes.RemoveParens(&t.out) // Print Go. @@ -469,11 +478,15 @@ func (t *translator) readImportSection() error { } typ := t.types[index] - if n := mangle.Name(name, mangle.Internal); t.provided.has(n) { - id := ast.NewIdent(n) + if recv, ok := t.provided[mangle.Name(name, mangle.Internal)]; ok { + id := mangle.ID(name, mangle.Internal) + var call ast.Expr = id + if recv { + call = modSel(id) + } fn := funcCompiler{ typ: typ, - call: latecall(id), + call: call, decl: &ast.FuncDecl{Name: id}, provided: true} t.functions = append(t.functions, fn)