]> Cypherpunks.ru repositories - gostls13.git/blob - src/cmd/compile/internal/noder/unified.go
[dev.typeparams] cmd/compile: add documentation for unified IR pipeline
[gostls13.git] / src / cmd / compile / internal / noder / unified.go
1 // UNREVIEWED
2
3 // Copyright 2021 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
6
7 package noder
8
9 import (
10         "bytes"
11         "fmt"
12         "internal/goversion"
13         "io"
14         "runtime"
15         "sort"
16
17         "cmd/compile/internal/base"
18         "cmd/compile/internal/inline"
19         "cmd/compile/internal/ir"
20         "cmd/compile/internal/typecheck"
21         "cmd/compile/internal/types"
22         "cmd/compile/internal/types2"
23         "cmd/internal/src"
24 )
25
26 // localPkgReader holds the package reader used for reading the local
27 // package. It exists so the unified IR linker can refer back to it
28 // later.
29 var localPkgReader *pkgReader
30
31 // unified construct the local package's IR from syntax's AST.
32 //
33 // The pipeline contains 2 steps:
34 //
35 // (1) Generate package export data "stub".
36 //
37 // (2) Generate package IR from package export data.
38 //
39 // The package data "stub" at step (1) contains everything from the local package,
40 // but nothing that have been imported. When we're actually writing out export data
41 // to the output files (see writeNewExport function), we run the "linker", which does
42 // a few things:
43 //
44 // + Updates compiler extensions data (e.g., inlining cost, escape analysis results).
45 //
46 // + Handles re-exporting any transitive dependencies.
47 //
48 // + Prunes out any unnecessary details (e.g., non-inlineable functions, because any
49 //   downstream importers only care about inlinable functions).
50 //
51 // The source files are typechecked twice, once before writing export data
52 // using types2 checker, once after read export data using gc/typecheck.
53 // This duplication of work will go away once we always use types2 checker,
54 // we can remove the gc/typecheck pass. The reason it is still here:
55 //
56 // + It reduces engineering costs in maintaining a fork of typecheck
57 //   (e.g., no need to backport fixes like CL 327651).
58 //
59 // + It makes it easier to pass toolstash -cmp.
60 //
61 // + Historically, we would always re-run the typechecker after import, even though
62 //   we know the imported data is valid. It's not ideal, but also not causing any
63 //   problem either.
64 //
65 // + There's still transformation that being done during gc/typecheck, like rewriting
66 //   multi-valued function call, or transform ir.OINDEX -> ir.OINDEXMAP.
67 //
68 // Using syntax+types2 tree, which already has a complete representation of generics,
69 // the unified IR has the full typed AST for doing introspection during step (1).
70 // In other words, we have all necessary information to build the generic IR form
71 // (see writer.captureVars for an example).
72 func unified(noders []*noder) {
73         inline.NewInline = InlineCall
74
75         if !quirksMode() {
76                 writeNewExportFunc = writeNewExport
77         }
78
79         newReadImportFunc = func(data string, pkg1 *types.Pkg, check *types2.Checker, packages map[string]*types2.Package) (pkg2 *types2.Package, err error) {
80                 pr := newPkgDecoder(pkg1.Path, data)
81
82                 // Read package descriptors for both types2 and compiler backend.
83                 readPackage(newPkgReader(pr), pkg1)
84                 pkg2 = readPackage2(check, packages, pr)
85                 return
86         }
87
88         data := writePkgStub(noders)
89
90         // We already passed base.Flag.Lang to types2 to handle validating
91         // the user's source code. Bump it up now to the current version and
92         // re-parse, so typecheck doesn't complain if we construct IR that
93         // utilizes newer Go features.
94         base.Flag.Lang = fmt.Sprintf("go1.%d", goversion.Version)
95         types.ParseLangFlag()
96
97         assert(types.LocalPkg.Path == "")
98         types.LocalPkg.Height = 0 // reset so pkgReader.pkgIdx doesn't complain
99         target := typecheck.Target
100
101         typecheck.TypecheckAllowed = true
102
103         localPkgReader = newPkgReader(newPkgDecoder(types.LocalPkg.Path, data))
104         readPackage(localPkgReader, types.LocalPkg)
105
106         r := localPkgReader.newReader(relocMeta, privateRootIdx, syncPrivate)
107         r.ext = r
108         r.pkgInit(types.LocalPkg, target)
109
110         // Don't use range--bodyIdx can add closures to todoBodies.
111         for len(todoBodies) > 0 {
112                 // The order we expand bodies doesn't matter, so pop from the end
113                 // to reduce todoBodies reallocations if it grows further.
114                 fn := todoBodies[len(todoBodies)-1]
115                 todoBodies = todoBodies[:len(todoBodies)-1]
116
117                 pri, ok := bodyReader[fn]
118                 assert(ok)
119                 pri.funcBody(fn)
120
121                 // Instantiated generic function: add to Decls for typechecking
122                 // and compilation.
123                 if len(pri.implicits) != 0 && fn.OClosure == nil {
124                         target.Decls = append(target.Decls, fn)
125                 }
126         }
127         todoBodies = nil
128
129         // Don't use range--typecheck can add closures to Target.Decls.
130         for i := 0; i < len(target.Decls); i++ {
131                 target.Decls[i] = typecheck.Stmt(target.Decls[i])
132         }
133
134         // Don't use range--typecheck can add closures to Target.Decls.
135         for i := 0; i < len(target.Decls); i++ {
136                 if fn, ok := target.Decls[i].(*ir.Func); ok {
137                         if base.Flag.W > 1 {
138                                 s := fmt.Sprintf("\nbefore typecheck %v", fn)
139                                 ir.Dump(s, fn)
140                         }
141                         ir.CurFunc = fn
142                         typecheck.Stmts(fn.Body)
143                         if base.Flag.W > 1 {
144                                 s := fmt.Sprintf("\nafter typecheck %v", fn)
145                                 ir.Dump(s, fn)
146                         }
147                 }
148         }
149
150         base.ExitIfErrors() // just in case
151 }
152
153 // writePkgStub type checks the given parsed source files,
154 // writes an export data package stub representing them,
155 // and returns the result.
156 func writePkgStub(noders []*noder) string {
157         m, pkg, info := checkFiles(noders)
158
159         pw := newPkgWriter(m, pkg, info)
160
161         pw.collectDecls(noders)
162
163         publicRootWriter := pw.newWriter(relocMeta, syncPublic)
164         privateRootWriter := pw.newWriter(relocMeta, syncPrivate)
165
166         assert(publicRootWriter.idx == publicRootIdx)
167         assert(privateRootWriter.idx == privateRootIdx)
168
169         {
170                 w := publicRootWriter
171                 w.pkg(pkg)
172                 w.bool(false) // has init; XXX
173
174                 scope := pkg.Scope()
175                 names := scope.Names()
176                 w.len(len(names))
177                 for _, name := range scope.Names() {
178                         w.obj(scope.Lookup(name), nil)
179                 }
180
181                 w.sync(syncEOF)
182                 w.flush()
183         }
184
185         {
186                 w := privateRootWriter
187                 w.ext = w
188                 w.pkgInit(noders)
189                 w.flush()
190         }
191
192         var sb bytes.Buffer // TODO(mdempsky): strings.Builder after #44505 is resolved
193         pw.dump(&sb)
194
195         // At this point, we're done with types2. Make sure the package is
196         // garbage collected.
197         freePackage(pkg)
198
199         return sb.String()
200 }
201
202 // freePackage ensures the given package is garbage collected.
203 func freePackage(pkg *types2.Package) {
204         // The GC test below relies on a precise GC that runs finalizers as
205         // soon as objects are unreachable. Our implementation provides
206         // this, but other/older implementations may not (e.g., Go 1.4 does
207         // not because of #22350). To avoid imposing unnecessary
208         // restrictions on the GOROOT_BOOTSTRAP toolchain, we skip the test
209         // during bootstrapping.
210         if base.CompilerBootstrap {
211                 return
212         }
213
214         // Set a finalizer on pkg so we can detect if/when it's collected.
215         done := make(chan struct{})
216         runtime.SetFinalizer(pkg, func(*types2.Package) { close(done) })
217
218         // Important: objects involved in cycles are not finalized, so zero
219         // out pkg to break its cycles and allow the finalizer to run.
220         *pkg = types2.Package{}
221
222         // It typically takes just 1 or 2 cycles to release pkg, but it
223         // doesn't hurt to try a few more times.
224         for i := 0; i < 10; i++ {
225                 select {
226                 case <-done:
227                         return
228                 default:
229                         runtime.GC()
230                 }
231         }
232
233         base.Fatalf("package never finalized")
234 }
235
236 func readPackage(pr *pkgReader, importpkg *types.Pkg) {
237         r := pr.newReader(relocMeta, publicRootIdx, syncPublic)
238
239         pkg := r.pkg()
240         assert(pkg == importpkg)
241
242         if r.bool() {
243                 sym := pkg.Lookup(".inittask")
244                 task := ir.NewNameAt(src.NoXPos, sym)
245                 task.Class = ir.PEXTERN
246                 sym.Def = task
247         }
248
249         for i, n := 0, r.len(); i < n; i++ {
250                 r.sync(syncObject)
251                 idx := r.reloc(relocObj)
252                 assert(r.len() == 0)
253
254                 path, name, code, _ := r.p.peekObj(idx)
255                 if code != objStub {
256                         objReader[types.NewPkg(path, "").Lookup(name)] = pkgReaderIndex{pr, idx, nil}
257                 }
258         }
259 }
260
261 func writeNewExport(out io.Writer) {
262         l := linker{
263                 pw: newPkgEncoder(),
264
265                 pkgs:  make(map[string]int),
266                 decls: make(map[*types.Sym]int),
267         }
268
269         publicRootWriter := l.pw.newEncoder(relocMeta, syncPublic)
270         assert(publicRootWriter.idx == publicRootIdx)
271
272         var selfPkgIdx int
273
274         {
275                 pr := localPkgReader
276                 r := pr.newDecoder(relocMeta, publicRootIdx, syncPublic)
277
278                 r.sync(syncPkg)
279                 selfPkgIdx = l.relocIdx(pr, relocPkg, r.reloc(relocPkg))
280
281                 r.bool() // has init
282
283                 for i, n := 0, r.len(); i < n; i++ {
284                         r.sync(syncObject)
285                         idx := r.reloc(relocObj)
286                         assert(r.len() == 0)
287
288                         xpath, xname, xtag, _ := pr.peekObj(idx)
289                         assert(xpath == pr.pkgPath)
290                         assert(xtag != objStub)
291
292                         if types.IsExported(xname) {
293                                 l.relocIdx(pr, relocObj, idx)
294                         }
295                 }
296
297                 r.sync(syncEOF)
298         }
299
300         {
301                 var idxs []int
302                 for _, idx := range l.decls {
303                         idxs = append(idxs, idx)
304                 }
305                 sort.Ints(idxs)
306
307                 w := publicRootWriter
308
309                 w.sync(syncPkg)
310                 w.reloc(relocPkg, selfPkgIdx)
311
312                 w.bool(typecheck.Lookup(".inittask").Def != nil)
313
314                 w.len(len(idxs))
315                 for _, idx := range idxs {
316                         w.sync(syncObject)
317                         w.reloc(relocObj, idx)
318                         w.len(0)
319                 }
320
321                 w.sync(syncEOF)
322                 w.flush()
323         }
324
325         l.pw.dump(out)
326 }