1 // Copyright 2021 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
15 "cmd/compile/internal/base"
16 "cmd/compile/internal/inline"
17 "cmd/compile/internal/ir"
18 "cmd/compile/internal/pgo"
19 "cmd/compile/internal/typecheck"
20 "cmd/compile/internal/types"
21 "cmd/compile/internal/types2"
25 // localPkgReader holds the package reader used for reading the local
26 // package. It exists so the unified IR linker can refer back to it
28 var localPkgReader *pkgReader
30 // LookupMethodFunc returns the ir.Func for an arbitrary full symbol name if
31 // that function exists in the set of available export data.
33 // This allows lookup of arbitrary functions and methods that aren't otherwise
34 // referenced by the local package and thus haven't been read yet.
36 // TODO(prattmic): Does not handle instantiation of generic types. Currently
37 // profiles don't contain the original type arguments, so we won't be able to
38 // create the runtime dictionaries.
40 // TODO(prattmic): Hit rate of this function is usually fairly low, and errors
41 // are only used when debug logging is enabled. Consider constructing cheaper
43 func LookupFunc(fullName string) (*ir.Func, error) {
44 pkgPath, symName, err := ir.ParseLinkFuncName(fullName)
46 return nil, fmt.Errorf("error parsing symbol name %q: %v", fullName, err)
49 pkg, ok := types.PkgMap()[pkgPath]
51 return nil, fmt.Errorf("pkg %s doesn't exist in %v", pkgPath, types.PkgMap())
54 // Symbol naming is ambiguous. We can't necessarily distinguish between
55 // a method and a closure. e.g., is foo.Bar.func1 a closure defined in
56 // function Bar, or a method on type Bar? Thus we must simply attempt
59 fn, err := lookupFunction(pkg, symName)
64 fn, mErr := lookupMethod(pkg, symName)
69 return nil, fmt.Errorf("%s is not a function (%v) or method (%v)", fullName, err, mErr)
72 func lookupFunction(pkg *types.Pkg, symName string) (*ir.Func, error) {
73 sym := pkg.Lookup(symName)
75 // TODO(prattmic): Enclosed functions (e.g., foo.Bar.func1) are not
76 // present in objReader, only as OCLOSURE nodes in the enclosing
78 pri, ok := objReader[sym]
80 return nil, fmt.Errorf("func sym %v missing objReader", sym)
83 name := pri.pr.objIdx(pri.idx, nil, nil, false).(*ir.Name)
84 if name.Op() != ir.ONAME || name.Class != ir.PFUNC {
85 return nil, fmt.Errorf("func sym %v refers to non-function name: %v", sym, name)
90 func lookupMethod(pkg *types.Pkg, symName string) (*ir.Func, error) {
91 // N.B. readPackage creates a Sym for every object in the package to
92 // initialize objReader and importBodyReader, even if the object isn't
95 // However, objReader is only initialized for top-level objects, so we
96 // must first lookup the type and use that to find the method rather
97 // than looking for the method directly.
98 typ, meth, err := ir.LookupMethodSelector(pkg, symName)
100 return nil, fmt.Errorf("error looking up method symbol %q: %v", symName, err)
103 pri, ok := objReader[typ]
105 return nil, fmt.Errorf("type sym %v missing objReader", typ)
108 name := pri.pr.objIdx(pri.idx, nil, nil, false).(*ir.Name)
109 if name.Op() != ir.OTYPE {
110 return nil, fmt.Errorf("type sym %v refers to non-type name: %v", typ, name)
113 return nil, fmt.Errorf("type sym %v refers to alias", typ)
116 for _, m := range name.Type().Methods() {
118 fn := m.Nname.(*ir.Name).Func
123 return nil, fmt.Errorf("method %s missing from method set of %v", symName, typ)
126 // unified constructs the local package's Internal Representation (IR)
127 // from its syntax tree (AST).
129 // The pipeline contains 2 steps:
131 // 1. Generate the export data "stub".
133 // 2. Generate the IR from the export data above.
135 // The package data "stub" at step (1) contains everything from the local package,
136 // but nothing that has been imported. When we're actually writing out export data
137 // to the output files (see writeNewExport), we run the "linker", which:
139 // - Updates compiler extensions data (e.g. inlining cost, escape analysis results).
141 // - Handles re-exporting any transitive dependencies.
143 // - Prunes out any unnecessary details (e.g. non-inlineable functions, because any
144 // downstream importers only care about inlinable functions).
146 // The source files are typechecked twice: once before writing the export data
147 // using types2, and again after reading the export data using gc/typecheck.
148 // The duplication of work will go away once we only use the types2 type checker,
149 // removing the gc/typecheck step. For now, it is kept because:
151 // - It reduces the engineering costs in maintaining a fork of typecheck
152 // (e.g. no need to backport fixes like CL 327651).
154 // - It makes it easier to pass toolstash -cmp.
156 // - Historically, we would always re-run the typechecker after importing a package,
157 // even though we know the imported data is valid. It's not ideal, but it's
158 // not causing any problems either.
160 // - gc/typecheck is still in charge of some transformations, such as rewriting
161 // multi-valued function calls or transforming ir.OINDEX to ir.OINDEXMAP.
163 // Using the syntax tree with types2, which has a complete representation of generics,
164 // the unified IR has the full typed AST needed for introspection during step (1).
165 // In other words, we have all the necessary information to build the generic IR form
166 // (see writer.captureVars for an example).
167 func unified(m posMap, noders []*noder) {
168 inline.InlineCall = unifiedInlineCall
169 typecheck.HaveInlineBody = unifiedHaveInlineBody
170 pgo.LookupFunc = LookupFunc
172 data := writePkgStub(m, noders)
174 target := typecheck.Target
176 localPkgReader = newPkgReader(pkgbits.NewPkgDecoder(types.LocalPkg.Path, data))
177 readPackage(localPkgReader, types.LocalPkg, true)
179 r := localPkgReader.newReader(pkgbits.RelocMeta, pkgbits.PrivateRootIdx, pkgbits.SyncPrivate)
180 r.pkgInit(types.LocalPkg, target)
182 readBodies(target, false)
184 // Check that nothing snuck past typechecking.
185 for _, fn := range target.Funcs {
186 if fn.Typecheck() == 0 {
187 base.FatalfAt(fn.Pos(), "missed typecheck: %v", fn)
190 // For functions, check that at least their first statement (if
191 // any) was typechecked too.
192 if len(fn.Body) != 0 {
193 if stmt := fn.Body[0]; stmt.Typecheck() == 0 {
194 base.FatalfAt(stmt.Pos(), "missed typecheck: %v", stmt)
199 // For functions originally came from package runtime,
200 // mark as norace to prevent instrumenting, see issue #60439.
201 for _, fn := range target.Funcs {
202 if !base.Flag.CompilingRuntime && types.RuntimeSymName(fn.Sym()) != "" {
203 fn.Pragma |= ir.Norace
207 base.ExitIfErrors() // just in case
210 // readBodies iteratively expands all pending dictionaries and
213 // If duringInlining is true, then the inline.InlineDecls is called as
214 // necessary on instantiations of imported generic functions, so their
215 // inlining costs can be computed.
216 func readBodies(target *ir.Package, duringInlining bool) {
217 var inlDecls []*ir.Func
219 // Don't use range--bodyIdx can add closures to todoBodies.
221 // The order we expand dictionaries and bodies doesn't matter, so
222 // pop from the end to reduce todoBodies reallocations if it grows
225 // However, we do at least need to flush any pending dictionaries
226 // before reading bodies, because bodies might reference the
229 if len(todoDicts) > 0 {
230 fn := todoDicts[len(todoDicts)-1]
231 todoDicts = todoDicts[:len(todoDicts)-1]
236 if len(todoBodies) > 0 {
237 fn := todoBodies[len(todoBodies)-1]
238 todoBodies = todoBodies[:len(todoBodies)-1]
240 pri, ok := bodyReader[fn]
244 // Instantiated generic function: add to Decls for typechecking
246 if fn.OClosure == nil && len(pri.dict.targs) != 0 {
247 // cmd/link does not support a type symbol referencing a method symbol
248 // across DSO boundary, so force re-compiling methods on a generic type
249 // even it was seen from imported package in linkshared mode, see #58966.
250 canSkipNonGenericMethod := !(base.Ctxt.Flag_linkshared && ir.IsMethod(fn))
251 if duringInlining && canSkipNonGenericMethod {
252 inlDecls = append(inlDecls, fn)
254 target.Funcs = append(target.Funcs, fn)
267 if len(inlDecls) != 0 {
268 // If we instantiated any generic functions during inlining, we need
269 // to call CanInline on them so they'll be transitively inlined
270 // correctly (#56280).
272 // We know these functions were already compiled in an imported
273 // package though, so we don't need to actually apply InlineCalls or
274 // save the function bodies any further than this.
276 // We can also lower the -m flag to 0, to suppress duplicate "can
277 // inline" diagnostics reported against the imported package. Again,
278 // we already reported those diagnostics in the original package, so
279 // it's pointless repeating them here.
281 oldLowerM := base.Flag.LowerM
283 inline.InlineDecls(nil, inlDecls, false)
284 base.Flag.LowerM = oldLowerM
286 for _, fn := range inlDecls {
287 fn.Body = nil // free memory
292 // writePkgStub type checks the given parsed source files,
293 // writes an export data package stub representing them,
294 // and returns the result.
295 func writePkgStub(m posMap, noders []*noder) string {
296 pkg, info := checkFiles(m, noders)
298 pw := newPkgWriter(m, pkg, info)
300 pw.collectDecls(noders)
302 publicRootWriter := pw.newWriter(pkgbits.RelocMeta, pkgbits.SyncPublic)
303 privateRootWriter := pw.newWriter(pkgbits.RelocMeta, pkgbits.SyncPrivate)
305 assert(publicRootWriter.Idx == pkgbits.PublicRootIdx)
306 assert(privateRootWriter.Idx == pkgbits.PrivateRootIdx)
309 w := publicRootWriter
311 w.Bool(false) // TODO(mdempsky): Remove; was "has init"
314 names := scope.Names()
316 for _, name := range names {
317 w.obj(scope.Lookup(name), nil)
320 w.Sync(pkgbits.SyncEOF)
325 w := privateRootWriter
330 var sb strings.Builder
333 // At this point, we're done with types2. Make sure the package is
334 // garbage collected.
340 // freePackage ensures the given package is garbage collected.
341 func freePackage(pkg *types2.Package) {
342 // The GC test below relies on a precise GC that runs finalizers as
343 // soon as objects are unreachable. Our implementation provides
344 // this, but other/older implementations may not (e.g., Go 1.4 does
345 // not because of #22350). To avoid imposing unnecessary
346 // restrictions on the GOROOT_BOOTSTRAP toolchain, we skip the test
347 // during bootstrapping.
348 if base.CompilerBootstrap || base.Debug.GCCheck == 0 {
349 *pkg = types2.Package{}
353 // Set a finalizer on pkg so we can detect if/when it's collected.
354 done := make(chan struct{})
355 runtime.SetFinalizer(pkg, func(*types2.Package) { close(done) })
357 // Important: objects involved in cycles are not finalized, so zero
358 // out pkg to break its cycles and allow the finalizer to run.
359 *pkg = types2.Package{}
361 // It typically takes just 1 or 2 cycles to release pkg, but it
362 // doesn't hurt to try a few more times.
363 for i := 0; i < 10; i++ {
372 base.Fatalf("package never finalized")
375 // readPackage reads package export data from pr to populate
378 // localStub indicates whether pr is reading the stub export data for
379 // the local package, as opposed to relocated export data for an
381 func readPackage(pr *pkgReader, importpkg *types.Pkg, localStub bool) {
383 r := pr.newReader(pkgbits.RelocMeta, pkgbits.PublicRootIdx, pkgbits.SyncPublic)
386 base.Assertf(pkg == importpkg, "have package %q (%p), want package %q (%p)", pkg.Path, pkg, importpkg.Path, importpkg)
388 r.Bool() // TODO(mdempsky): Remove; was "has init"
390 for i, n := 0, r.Len(); i < n; i++ {
391 r.Sync(pkgbits.SyncObject)
393 idx := r.Reloc(pkgbits.RelocObj)
396 path, name, code := r.p.PeekObj(idx)
397 if code != pkgbits.ObjStub {
398 objReader[types.NewPkg(path, "").Lookup(name)] = pkgReaderIndex{pr, idx, nil, nil, nil}
402 r.Sync(pkgbits.SyncEOF)
406 r := pr.newReader(pkgbits.RelocMeta, pkgbits.PrivateRootIdx, pkgbits.SyncPrivate)
409 sym := importpkg.Lookup(".inittask")
410 task := ir.NewNameAt(src.NoXPos, sym, nil)
411 task.Class = ir.PEXTERN
415 for i, n := 0, r.Len(); i < n; i++ {
418 idx := r.Reloc(pkgbits.RelocBody)
420 sym := types.NewPkg(path, "").Lookup(name)
421 if _, ok := importBodyReader[sym]; !ok {
422 importBodyReader[sym] = pkgReaderIndex{pr, idx, nil, nil, nil}
426 r.Sync(pkgbits.SyncEOF)
430 // writeUnifiedExport writes to `out` the finalized, self-contained
431 // Unified IR export data file for the current compilation unit.
432 func writeUnifiedExport(out io.Writer) {
434 pw: pkgbits.NewPkgEncoder(base.Debug.SyncFrames),
436 pkgs: make(map[string]pkgbits.Index),
437 decls: make(map[*types.Sym]pkgbits.Index),
438 bodies: make(map[*types.Sym]pkgbits.Index),
441 publicRootWriter := l.pw.NewEncoder(pkgbits.RelocMeta, pkgbits.SyncPublic)
442 privateRootWriter := l.pw.NewEncoder(pkgbits.RelocMeta, pkgbits.SyncPrivate)
443 assert(publicRootWriter.Idx == pkgbits.PublicRootIdx)
444 assert(privateRootWriter.Idx == pkgbits.PrivateRootIdx)
446 var selfPkgIdx pkgbits.Index
450 r := pr.NewDecoder(pkgbits.RelocMeta, pkgbits.PublicRootIdx, pkgbits.SyncPublic)
452 r.Sync(pkgbits.SyncPkg)
453 selfPkgIdx = l.relocIdx(pr, pkgbits.RelocPkg, r.Reloc(pkgbits.RelocPkg))
455 r.Bool() // TODO(mdempsky): Remove; was "has init"
457 for i, n := 0, r.Len(); i < n; i++ {
458 r.Sync(pkgbits.SyncObject)
460 idx := r.Reloc(pkgbits.RelocObj)
463 xpath, xname, xtag := pr.PeekObj(idx)
464 assert(xpath == pr.PkgPath())
465 assert(xtag != pkgbits.ObjStub)
467 if types.IsExported(xname) {
468 l.relocIdx(pr, pkgbits.RelocObj, idx)
472 r.Sync(pkgbits.SyncEOF)
476 var idxs []pkgbits.Index
477 for _, idx := range l.decls {
478 idxs = append(idxs, idx)
480 sort.Slice(idxs, func(i, j int) bool { return idxs[i] < idxs[j] })
482 w := publicRootWriter
484 w.Sync(pkgbits.SyncPkg)
485 w.Reloc(pkgbits.RelocPkg, selfPkgIdx)
486 w.Bool(false) // TODO(mdempsky): Remove; was "has init"
489 for _, idx := range idxs {
490 w.Sync(pkgbits.SyncObject)
492 w.Reloc(pkgbits.RelocObj, idx)
496 w.Sync(pkgbits.SyncEOF)
506 for sym, idx := range l.bodies {
507 bodies = append(bodies, symIdx{sym, idx})
509 sort.Slice(bodies, func(i, j int) bool { return bodies[i].idx < bodies[j].idx })
511 w := privateRootWriter
513 w.Bool(typecheck.Lookup(".inittask").Def != nil)
516 for _, body := range bodies {
517 w.String(body.sym.Pkg.Path)
518 w.String(body.sym.Name)
519 w.Reloc(pkgbits.RelocBody, body.idx)
522 w.Sync(pkgbits.SyncEOF)
526 base.Ctxt.Fingerprint = l.pw.DumpTo(out)