-// UNREVIEWED
-
// Copyright 2021 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package noder
import (
- "bytes"
"fmt"
- "internal/goversion"
+ "internal/pkgbits"
"io"
"runtime"
"sort"
+ "strings"
"cmd/compile/internal/base"
"cmd/compile/internal/inline"
"cmd/compile/internal/ir"
+ "cmd/compile/internal/pgo"
"cmd/compile/internal/typecheck"
"cmd/compile/internal/types"
"cmd/compile/internal/types2"
// later.
var localPkgReader *pkgReader
-// unified construct the local package's IR from syntax's AST.
+// LookupMethodFunc returns the ir.Func for an arbitrary full symbol name if
+// that function exists in the set of available export data.
+//
+// This allows lookup of arbitrary functions and methods that aren't otherwise
+// referenced by the local package and thus haven't been read yet.
+//
+// TODO(prattmic): Does not handle instantiation of generic types. Currently
+// profiles don't contain the original type arguments, so we won't be able to
+// create the runtime dictionaries.
+//
+// TODO(prattmic): Hit rate of this function is usually fairly low, and errors
+// are only used when debug logging is enabled. Consider constructing cheaper
+// errors by default.
+func LookupFunc(fullName string) (*ir.Func, error) {
+ pkgPath, symName, err := ir.ParseLinkFuncName(fullName)
+ if err != nil {
+ return nil, fmt.Errorf("error parsing symbol name %q: %v", fullName, err)
+ }
+
+ pkg, ok := types.PkgMap()[pkgPath]
+ if !ok {
+ return nil, fmt.Errorf("pkg %s doesn't exist in %v", pkgPath, types.PkgMap())
+ }
+
+ // Symbol naming is ambiguous. We can't necessarily distinguish between
+ // a method and a closure. e.g., is foo.Bar.func1 a closure defined in
+ // function Bar, or a method on type Bar? Thus we must simply attempt
+ // to lookup both.
+
+ fn, err := lookupFunction(pkg, symName)
+ if err == nil {
+ return fn, nil
+ }
+
+ fn, mErr := lookupMethod(pkg, symName)
+ if mErr == nil {
+ return fn, nil
+ }
+
+ return nil, fmt.Errorf("%s is not a function (%v) or method (%v)", fullName, err, mErr)
+}
+
+func lookupFunction(pkg *types.Pkg, symName string) (*ir.Func, error) {
+ sym := pkg.Lookup(symName)
+
+ // TODO(prattmic): Enclosed functions (e.g., foo.Bar.func1) are not
+ // present in objReader, only as OCLOSURE nodes in the enclosing
+ // function.
+ pri, ok := objReader[sym]
+ if !ok {
+ return nil, fmt.Errorf("func sym %v missing objReader", sym)
+ }
+
+ name := pri.pr.objIdx(pri.idx, nil, nil, false).(*ir.Name)
+ if name.Op() != ir.ONAME || name.Class != ir.PFUNC {
+ return nil, fmt.Errorf("func sym %v refers to non-function name: %v", sym, name)
+ }
+ return name.Func, nil
+}
+
+func lookupMethod(pkg *types.Pkg, symName string) (*ir.Func, error) {
+ // N.B. readPackage creates a Sym for every object in the package to
+ // initialize objReader and importBodyReader, even if the object isn't
+ // read.
+ //
+ // However, objReader is only initialized for top-level objects, so we
+ // must first lookup the type and use that to find the method rather
+ // than looking for the method directly.
+ typ, meth, err := ir.LookupMethodSelector(pkg, symName)
+ if err != nil {
+ return nil, fmt.Errorf("error looking up method symbol %q: %v", symName, err)
+ }
+
+ pri, ok := objReader[typ]
+ if !ok {
+ return nil, fmt.Errorf("type sym %v missing objReader", typ)
+ }
+
+ name := pri.pr.objIdx(pri.idx, nil, nil, false).(*ir.Name)
+ if name.Op() != ir.OTYPE {
+ return nil, fmt.Errorf("type sym %v refers to non-type name: %v", typ, name)
+ }
+ if name.Alias() {
+ return nil, fmt.Errorf("type sym %v refers to alias", typ)
+ }
+
+ for _, m := range name.Type().Methods() {
+ if m.Sym == meth {
+ fn := m.Nname.(*ir.Name).Func
+ return fn, nil
+ }
+ }
+
+ return nil, fmt.Errorf("method %s missing from method set of %v", symName, typ)
+}
+
+// unified constructs the local package's Internal Representation (IR)
+// from its syntax tree (AST).
//
// The pipeline contains 2 steps:
//
-// (1) Generate package export data "stub".
+// 1. Generate the export data "stub".
//
-// (2) Generate package IR from package export data.
+// 2. Generate the IR from the export data above.
//
// The package data "stub" at step (1) contains everything from the local package,
-// but nothing that have been imported. When we're actually writing out export data
-// to the output files (see writeNewExport function), we run the "linker", which does
-// a few things:
+// but nothing that has been imported. When we're actually writing out export data
+// to the output files (see writeNewExport), we run the "linker", which:
//
-// + Updates compiler extensions data (e.g., inlining cost, escape analysis results).
+// - Updates compiler extensions data (e.g. inlining cost, escape analysis results).
//
-// + Handles re-exporting any transitive dependencies.
+// - Handles re-exporting any transitive dependencies.
//
-// + Prunes out any unnecessary details (e.g., non-inlineable functions, because any
-// downstream importers only care about inlinable functions).
+// - Prunes out any unnecessary details (e.g. non-inlineable functions, because any
+// downstream importers only care about inlinable functions).
//
-// The source files are typechecked twice, once before writing export data
-// using types2 checker, once after read export data using gc/typecheck.
-// This duplication of work will go away once we always use types2 checker,
-// we can remove the gc/typecheck pass. The reason it is still here:
+// The source files are typechecked twice: once before writing the export data
+// using types2, and again after reading the export data using gc/typecheck.
+// The duplication of work will go away once we only use the types2 type checker,
+// removing the gc/typecheck step. For now, it is kept because:
//
-// + It reduces engineering costs in maintaining a fork of typecheck
-// (e.g., no need to backport fixes like CL 327651).
+// - It reduces the engineering costs in maintaining a fork of typecheck
+// (e.g. no need to backport fixes like CL 327651).
//
-// + It makes it easier to pass toolstash -cmp.
+// - It makes it easier to pass toolstash -cmp.
//
-// + Historically, we would always re-run the typechecker after import, even though
-// we know the imported data is valid. It's not ideal, but also not causing any
-// problem either.
+// - Historically, we would always re-run the typechecker after importing a package,
+// even though we know the imported data is valid. It's not ideal, but it's
+// not causing any problems either.
//
-// + There's still transformation that being done during gc/typecheck, like rewriting
-// multi-valued function call, or transform ir.OINDEX -> ir.OINDEXMAP.
+// - gc/typecheck is still in charge of some transformations, such as rewriting
+// multi-valued function calls or transforming ir.OINDEX to ir.OINDEXMAP.
//
-// Using syntax+types2 tree, which already has a complete representation of generics,
-// the unified IR has the full typed AST for doing introspection during step (1).
-// In other words, we have all necessary information to build the generic IR form
+// Using the syntax tree with types2, which has a complete representation of generics,
+// the unified IR has the full typed AST needed for introspection during step (1).
+// In other words, we have all the necessary information to build the generic IR form
// (see writer.captureVars for an example).
-func unified(noders []*noder) {
- inline.NewInline = InlineCall
+func unified(m posMap, noders []*noder) {
+ inline.InlineCall = unifiedInlineCall
+ typecheck.HaveInlineBody = unifiedHaveInlineBody
+ pgo.LookupFunc = LookupFunc
- if !quirksMode() {
- writeNewExportFunc = writeNewExport
- } else if base.Flag.G != 0 {
- base.Errorf("cannot use -G and -d=quirksmode together")
- }
-
- newReadImportFunc = func(data string, pkg1 *types.Pkg, check *types2.Checker, packages map[string]*types2.Package) (pkg2 *types2.Package, err error) {
- pr := newPkgDecoder(pkg1.Path, data)
+ data := writePkgStub(m, noders)
- // Read package descriptors for both types2 and compiler backend.
- readPackage(newPkgReader(pr), pkg1)
- pkg2 = readPackage2(check, packages, pr)
- return
- }
-
- data := writePkgStub(noders)
-
- // We already passed base.Flag.Lang to types2 to handle validating
- // the user's source code. Bump it up now to the current version and
- // re-parse, so typecheck doesn't complain if we construct IR that
- // utilizes newer Go features.
- base.Flag.Lang = fmt.Sprintf("go1.%d", goversion.Version)
- types.ParseLangFlag()
-
- assert(types.LocalPkg.Path == "")
- types.LocalPkg.Height = 0 // reset so pkgReader.pkgIdx doesn't complain
target := typecheck.Target
- typecheck.TypecheckAllowed = true
-
- localPkgReader = newPkgReader(newPkgDecoder(types.LocalPkg.Path, data))
- readPackage(localPkgReader, types.LocalPkg)
+ localPkgReader = newPkgReader(pkgbits.NewPkgDecoder(types.LocalPkg.Path, data))
+ readPackage(localPkgReader, types.LocalPkg, true)
- r := localPkgReader.newReader(relocMeta, privateRootIdx, syncPrivate)
- r.ext = r
+ r := localPkgReader.newReader(pkgbits.RelocMeta, pkgbits.PrivateRootIdx, pkgbits.SyncPrivate)
r.pkgInit(types.LocalPkg, target)
- // Type-check any top-level assignments. We ignore non-assignments
- // here because other declarations are typechecked as they're
- // constructed.
- for i, ndecls := 0, len(target.Decls); i < ndecls; i++ {
- switch n := target.Decls[i]; n.Op() {
- case ir.OAS, ir.OAS2:
- target.Decls[i] = typecheck.Stmt(n)
- }
- }
-
- // Don't use range--bodyIdx can add closures to todoBodies.
- for len(todoBodies) > 0 {
- // The order we expand bodies doesn't matter, so pop from the end
- // to reduce todoBodies reallocations if it grows further.
- fn := todoBodies[len(todoBodies)-1]
- todoBodies = todoBodies[:len(todoBodies)-1]
-
- pri, ok := bodyReader[fn]
- assert(ok)
- pri.funcBody(fn)
-
- // Instantiated generic function: add to Decls for typechecking
- // and compilation.
- if fn.OClosure == nil && len(pri.dict.targs) != 0 {
- target.Decls = append(target.Decls, fn)
- }
- }
- todoBodies = nil
-
- if !quirksMode() {
- // TODO(mdempsky): Investigate generating wrappers in quirks mode too.
- r.wrapTypes(target)
- }
+ readBodies(target, false)
// Check that nothing snuck past typechecking.
- for _, n := range target.Decls {
- if n.Typecheck() == 0 {
- base.FatalfAt(n.Pos(), "missed typecheck: %v", n)
+ for _, fn := range target.Funcs {
+ if fn.Typecheck() == 0 {
+ base.FatalfAt(fn.Pos(), "missed typecheck: %v", fn)
}
// For functions, check that at least their first statement (if
// any) was typechecked too.
- if fn, ok := n.(*ir.Func); ok && len(fn.Body) != 0 {
+ if len(fn.Body) != 0 {
if stmt := fn.Body[0]; stmt.Typecheck() == 0 {
base.FatalfAt(stmt.Pos(), "missed typecheck: %v", stmt)
}
}
}
+ // For functions originally came from package runtime,
+ // mark as norace to prevent instrumenting, see issue #60439.
+ for _, fn := range target.Funcs {
+ if !base.Flag.CompilingRuntime && types.RuntimeSymName(fn.Sym()) != "" {
+ fn.Pragma |= ir.Norace
+ }
+ }
+
base.ExitIfErrors() // just in case
}
+// readBodies iteratively expands all pending dictionaries and
+// function bodies.
+//
+// If duringInlining is true, then the inline.InlineDecls is called as
+// necessary on instantiations of imported generic functions, so their
+// inlining costs can be computed.
+func readBodies(target *ir.Package, duringInlining bool) {
+ var inlDecls []*ir.Func
+
+ // Don't use range--bodyIdx can add closures to todoBodies.
+ for {
+ // The order we expand dictionaries and bodies doesn't matter, so
+ // pop from the end to reduce todoBodies reallocations if it grows
+ // further.
+ //
+ // However, we do at least need to flush any pending dictionaries
+ // before reading bodies, because bodies might reference the
+ // dictionaries.
+
+ if len(todoDicts) > 0 {
+ fn := todoDicts[len(todoDicts)-1]
+ todoDicts = todoDicts[:len(todoDicts)-1]
+ fn()
+ continue
+ }
+
+ if len(todoBodies) > 0 {
+ fn := todoBodies[len(todoBodies)-1]
+ todoBodies = todoBodies[:len(todoBodies)-1]
+
+ pri, ok := bodyReader[fn]
+ assert(ok)
+ pri.funcBody(fn)
+
+ // Instantiated generic function: add to Decls for typechecking
+ // and compilation.
+ if fn.OClosure == nil && len(pri.dict.targs) != 0 {
+ // cmd/link does not support a type symbol referencing a method symbol
+ // across DSO boundary, so force re-compiling methods on a generic type
+ // even it was seen from imported package in linkshared mode, see #58966.
+ canSkipNonGenericMethod := !(base.Ctxt.Flag_linkshared && ir.IsMethod(fn))
+ if duringInlining && canSkipNonGenericMethod {
+ inlDecls = append(inlDecls, fn)
+ } else {
+ target.Funcs = append(target.Funcs, fn)
+ }
+ }
+
+ continue
+ }
+
+ break
+ }
+
+ todoDicts = nil
+ todoBodies = nil
+
+ if len(inlDecls) != 0 {
+ // If we instantiated any generic functions during inlining, we need
+ // to call CanInline on them so they'll be transitively inlined
+ // correctly (#56280).
+ //
+ // We know these functions were already compiled in an imported
+ // package though, so we don't need to actually apply InlineCalls or
+ // save the function bodies any further than this.
+ //
+ // We can also lower the -m flag to 0, to suppress duplicate "can
+ // inline" diagnostics reported against the imported package. Again,
+ // we already reported those diagnostics in the original package, so
+ // it's pointless repeating them here.
+
+ oldLowerM := base.Flag.LowerM
+ base.Flag.LowerM = 0
+ inline.InlineDecls(nil, inlDecls, false)
+ base.Flag.LowerM = oldLowerM
+
+ for _, fn := range inlDecls {
+ fn.Body = nil // free memory
+ }
+ }
+}
+
// writePkgStub type checks the given parsed source files,
// writes an export data package stub representing them,
// and returns the result.
-func writePkgStub(noders []*noder) string {
- m, pkg, info := checkFiles(noders)
+func writePkgStub(m posMap, noders []*noder) string {
+ pkg, info := checkFiles(m, noders)
pw := newPkgWriter(m, pkg, info)
pw.collectDecls(noders)
- publicRootWriter := pw.newWriter(relocMeta, syncPublic)
- privateRootWriter := pw.newWriter(relocMeta, syncPrivate)
+ publicRootWriter := pw.newWriter(pkgbits.RelocMeta, pkgbits.SyncPublic)
+ privateRootWriter := pw.newWriter(pkgbits.RelocMeta, pkgbits.SyncPrivate)
- assert(publicRootWriter.idx == publicRootIdx)
- assert(privateRootWriter.idx == privateRootIdx)
+ assert(publicRootWriter.Idx == pkgbits.PublicRootIdx)
+ assert(privateRootWriter.Idx == pkgbits.PrivateRootIdx)
{
w := publicRootWriter
w.pkg(pkg)
- w.bool(false) // has init; XXX
+ w.Bool(false) // TODO(mdempsky): Remove; was "has init"
scope := pkg.Scope()
names := scope.Names()
- w.len(len(names))
- for _, name := range scope.Names() {
+ w.Len(len(names))
+ for _, name := range names {
w.obj(scope.Lookup(name), nil)
}
- w.sync(syncEOF)
- w.flush()
+ w.Sync(pkgbits.SyncEOF)
+ w.Flush()
}
{
w := privateRootWriter
- w.ext = w
w.pkgInit(noders)
- w.flush()
+ w.Flush()
}
- var sb bytes.Buffer // TODO(mdempsky): strings.Builder after #44505 is resolved
- pw.dump(&sb)
+ var sb strings.Builder
+ pw.DumpTo(&sb)
// At this point, we're done with types2. Make sure the package is
// garbage collected.
// not because of #22350). To avoid imposing unnecessary
// restrictions on the GOROOT_BOOTSTRAP toolchain, we skip the test
// during bootstrapping.
- if base.CompilerBootstrap {
+ if base.CompilerBootstrap || base.Debug.GCCheck == 0 {
+ *pkg = types2.Package{}
return
}
base.Fatalf("package never finalized")
}
-func readPackage(pr *pkgReader, importpkg *types.Pkg) {
- r := pr.newReader(relocMeta, publicRootIdx, syncPublic)
+// readPackage reads package export data from pr to populate
+// importpkg.
+//
+// localStub indicates whether pr is reading the stub export data for
+// the local package, as opposed to relocated export data for an
+// import.
+func readPackage(pr *pkgReader, importpkg *types.Pkg, localStub bool) {
+ {
+ r := pr.newReader(pkgbits.RelocMeta, pkgbits.PublicRootIdx, pkgbits.SyncPublic)
+
+ pkg := r.pkg()
+ base.Assertf(pkg == importpkg, "have package %q (%p), want package %q (%p)", pkg.Path, pkg, importpkg.Path, importpkg)
+
+ r.Bool() // TODO(mdempsky): Remove; was "has init"
- pkg := r.pkg()
- assert(pkg == importpkg)
+ for i, n := 0, r.Len(); i < n; i++ {
+ r.Sync(pkgbits.SyncObject)
+ assert(!r.Bool())
+ idx := r.Reloc(pkgbits.RelocObj)
+ assert(r.Len() == 0)
+
+ path, name, code := r.p.PeekObj(idx)
+ if code != pkgbits.ObjStub {
+ objReader[types.NewPkg(path, "").Lookup(name)] = pkgReaderIndex{pr, idx, nil, nil, nil}
+ }
+ }
- if r.bool() {
- sym := pkg.Lookup(".inittask")
- task := ir.NewNameAt(src.NoXPos, sym)
- task.Class = ir.PEXTERN
- sym.Def = task
+ r.Sync(pkgbits.SyncEOF)
}
- for i, n := 0, r.len(); i < n; i++ {
- r.sync(syncObject)
- idx := r.reloc(relocObj)
- assert(r.len() == 0)
+ if !localStub {
+ r := pr.newReader(pkgbits.RelocMeta, pkgbits.PrivateRootIdx, pkgbits.SyncPrivate)
- path, name, code, _ := r.p.peekObj(idx)
- if code != objStub {
- objReader[types.NewPkg(path, "").Lookup(name)] = pkgReaderIndex{pr, idx, nil}
+ if r.Bool() {
+ sym := importpkg.Lookup(".inittask")
+ task := ir.NewNameAt(src.NoXPos, sym, nil)
+ task.Class = ir.PEXTERN
+ sym.Def = task
}
+
+ for i, n := 0, r.Len(); i < n; i++ {
+ path := r.String()
+ name := r.String()
+ idx := r.Reloc(pkgbits.RelocBody)
+
+ sym := types.NewPkg(path, "").Lookup(name)
+ if _, ok := importBodyReader[sym]; !ok {
+ importBodyReader[sym] = pkgReaderIndex{pr, idx, nil, nil, nil}
+ }
+ }
+
+ r.Sync(pkgbits.SyncEOF)
}
}
-func writeNewExport(out io.Writer) {
+// writeUnifiedExport writes to `out` the finalized, self-contained
+// Unified IR export data file for the current compilation unit.
+func writeUnifiedExport(out io.Writer) {
l := linker{
- pw: newPkgEncoder(),
+ pw: pkgbits.NewPkgEncoder(base.Debug.SyncFrames),
- pkgs: make(map[string]int),
- decls: make(map[*types.Sym]int),
+ pkgs: make(map[string]pkgbits.Index),
+ decls: make(map[*types.Sym]pkgbits.Index),
+ bodies: make(map[*types.Sym]pkgbits.Index),
}
- publicRootWriter := l.pw.newEncoder(relocMeta, syncPublic)
- assert(publicRootWriter.idx == publicRootIdx)
+ publicRootWriter := l.pw.NewEncoder(pkgbits.RelocMeta, pkgbits.SyncPublic)
+ privateRootWriter := l.pw.NewEncoder(pkgbits.RelocMeta, pkgbits.SyncPrivate)
+ assert(publicRootWriter.Idx == pkgbits.PublicRootIdx)
+ assert(privateRootWriter.Idx == pkgbits.PrivateRootIdx)
- var selfPkgIdx int
+ var selfPkgIdx pkgbits.Index
{
pr := localPkgReader
- r := pr.newDecoder(relocMeta, publicRootIdx, syncPublic)
+ r := pr.NewDecoder(pkgbits.RelocMeta, pkgbits.PublicRootIdx, pkgbits.SyncPublic)
- r.sync(syncPkg)
- selfPkgIdx = l.relocIdx(pr, relocPkg, r.reloc(relocPkg))
+ r.Sync(pkgbits.SyncPkg)
+ selfPkgIdx = l.relocIdx(pr, pkgbits.RelocPkg, r.Reloc(pkgbits.RelocPkg))
- r.bool() // has init
+ r.Bool() // TODO(mdempsky): Remove; was "has init"
- for i, n := 0, r.len(); i < n; i++ {
- r.sync(syncObject)
- idx := r.reloc(relocObj)
- assert(r.len() == 0)
+ for i, n := 0, r.Len(); i < n; i++ {
+ r.Sync(pkgbits.SyncObject)
+ assert(!r.Bool())
+ idx := r.Reloc(pkgbits.RelocObj)
+ assert(r.Len() == 0)
- xpath, xname, xtag, _ := pr.peekObj(idx)
- assert(xpath == pr.pkgPath)
- assert(xtag != objStub)
+ xpath, xname, xtag := pr.PeekObj(idx)
+ assert(xpath == pr.PkgPath())
+ assert(xtag != pkgbits.ObjStub)
if types.IsExported(xname) {
- l.relocIdx(pr, relocObj, idx)
+ l.relocIdx(pr, pkgbits.RelocObj, idx)
}
}
- r.sync(syncEOF)
+ r.Sync(pkgbits.SyncEOF)
}
{
- var idxs []int
+ var idxs []pkgbits.Index
for _, idx := range l.decls {
idxs = append(idxs, idx)
}
- sort.Ints(idxs)
+ sort.Slice(idxs, func(i, j int) bool { return idxs[i] < idxs[j] })
w := publicRootWriter
- w.sync(syncPkg)
- w.reloc(relocPkg, selfPkgIdx)
-
- w.bool(typecheck.Lookup(".inittask").Def != nil)
+ w.Sync(pkgbits.SyncPkg)
+ w.Reloc(pkgbits.RelocPkg, selfPkgIdx)
+ w.Bool(false) // TODO(mdempsky): Remove; was "has init"
- w.len(len(idxs))
+ w.Len(len(idxs))
for _, idx := range idxs {
- w.sync(syncObject)
- w.reloc(relocObj, idx)
- w.len(0)
+ w.Sync(pkgbits.SyncObject)
+ w.Bool(false)
+ w.Reloc(pkgbits.RelocObj, idx)
+ w.Len(0)
+ }
+
+ w.Sync(pkgbits.SyncEOF)
+ w.Flush()
+ }
+
+ {
+ type symIdx struct {
+ sym *types.Sym
+ idx pkgbits.Index
+ }
+ var bodies []symIdx
+ for sym, idx := range l.bodies {
+ bodies = append(bodies, symIdx{sym, idx})
+ }
+ sort.Slice(bodies, func(i, j int) bool { return bodies[i].idx < bodies[j].idx })
+
+ w := privateRootWriter
+
+ w.Bool(typecheck.Lookup(".inittask").Def != nil)
+
+ w.Len(len(bodies))
+ for _, body := range bodies {
+ w.String(body.sym.Pkg.Path)
+ w.String(body.sym.Name)
+ w.Reloc(pkgbits.RelocBody, body.idx)
}
- w.sync(syncEOF)
- w.flush()
+ w.Sync(pkgbits.SyncEOF)
+ w.Flush()
}
- l.pw.dump(out)
+ base.Ctxt.Fingerprint = l.pw.DumpTo(out)
}