Debian Codegraph @

src/cmd/compile/internal/gc/main.go

raw
1 // Copyright 2009 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.
4
5 package gc
6
7 import (
8 "bufio"
9 "bytes"
10 "cmd/compile/internal/base"
11 "cmd/compile/internal/bloop"
12 "cmd/compile/internal/coverage"
13 "cmd/compile/internal/deadlocals"
14 "cmd/compile/internal/dwarfgen"
15 "cmd/compile/internal/escape"
16 "cmd/compile/internal/inline"
17 "cmd/compile/internal/inline/interleaved"
18 "cmd/compile/internal/ir"
19 "cmd/compile/internal/logopt"
20 "cmd/compile/internal/loopvar"
21 "cmd/compile/internal/noder"
22 "cmd/compile/internal/pgoir"
23 "cmd/compile/internal/pkginit"
24 "cmd/compile/internal/reflectdata"
25 "cmd/compile/internal/rttype"
26 "cmd/compile/internal/slice"
27 "cmd/compile/internal/ssa"
28 "cmd/compile/internal/ssagen"
29 "cmd/compile/internal/staticinit"
30 "cmd/compile/internal/typecheck"
31 "cmd/compile/internal/types"
32 "cmd/internal/dwarf"
33 "cmd/internal/obj"
34 "cmd/internal/objabi"
35 "cmd/internal/src"
36 "cmd/internal/telemetry/counter"
37 "flag"
38 "fmt"
39 "internal/buildcfg"
40 "log"
41 "os"
42 "runtime"
43 )
44
45 // handlePanic ensures that we print out an "internal compiler error" for any panic
46 // or runtime exception during front-end compiler processing (unless there have
47 // already been some compiler errors). It may also be invoked from the explicit panic in
48 // hcrash(), in which case, we pass the panic on through.
49 func handlePanic() {
50 if err := recover(); err != nil {
51 if err == "-h" {
52 // Force real panic now with -h option (hcrash) - the error
53 // information will have already been printed.
54 panic(err)
55 }
56 base.Fatalf("panic: %v", err)
57 }
58 }
59
60 // Main parses flags and Go source files specified in the command-line
61 // arguments, type-checks the parsed Go package, compiles functions to machine
62 // code, and finally writes the compiled package definition to disk.
63 func Main(archInit func(*ssagen.ArchInfo)) {
64 base.Timer.Start("fe", "init")
65 counter.Open()
66 counter.Inc("compile/invocations")
67
68 defer handlePanic()
69
70 archInit(&ssagen.Arch)
71
72 base.Ctxt = obj.Linknew(ssagen.Arch.LinkArch)
73 base.Ctxt.DiagFunc = base.Errorf
74 base.Ctxt.DiagFlush = base.FlushErrors
75 base.Ctxt.Bso = bufio.NewWriter(os.Stdout)
76
77 // UseBASEntries is preferred because it shaves about 2% off build time, but LLDB, dsymutil, and dwarfdump
78 // on Darwin don't support it properly, especially since macOS 10.14 (Mojave). This is exposed as a flag
79 // to allow testing with LLVM tools on Linux, and to help with reporting this bug to the LLVM project.
80 // See bugs 31188 and 21945 (CLs 170638, 98075, 72371).
81 base.Ctxt.UseBASEntries = base.Ctxt.Headtype != objabi.Hdarwin
82
83 base.DebugSSA = ssa.PhaseOption
84 base.ParseFlags()
85
86 if flagGCStart := base.Debug.GCStart; flagGCStart > 0 || // explicit flags overrides environment variable disable of GC boost
87 os.Getenv("GOGC") == "" && os.Getenv("GOMEMLIMIT") == "" && base.Flag.LowerC != 1 { // explicit GC knobs or no concurrency implies default heap
88 startHeapMB := int64(128)
89 if flagGCStart > 0 {
90 startHeapMB = int64(flagGCStart)
91 }
92 base.AdjustStartingHeap(uint64(startHeapMB)<<20, 0, 0, 0, base.Debug.GCAdjust == 1)
93 }
94
95 types.LocalPkg = types.NewPkg(base.Ctxt.Pkgpath, "")
96
97 // pseudo-package, for scoping
98 types.BuiltinPkg = types.NewPkg("go.builtin", "") // TODO(gri) name this package go.builtin?
99 types.BuiltinPkg.Prefix = "go:builtin"
100
101 // pseudo-package, accessed by import "unsafe"
102 types.UnsafePkg = types.NewPkg("unsafe", "unsafe")
103
104 // Pseudo-package that contains the compiler's builtin
105 // declarations for package runtime. These are declared in a
106 // separate package to avoid conflicts with package runtime's
107 // actual declarations, which may differ intentionally but
108 // insignificantly.
109 ir.Pkgs.Runtime = types.NewPkg("go.runtime", "runtime")
110 ir.Pkgs.Runtime.Prefix = "runtime"
111
112 // Pseudo-package that contains the compiler's builtin
113 // declarations for maps.
114 ir.Pkgs.InternalMaps = types.NewPkg("go.internal/runtime/maps", "internal/runtime/maps")
115 ir.Pkgs.InternalMaps.Prefix = "internal/runtime/maps"
116
117 // pseudo-packages used in symbol tables
118 ir.Pkgs.Itab = types.NewPkg("go.itab", "go.itab")
119 ir.Pkgs.Itab.Prefix = "go:itab"
120
121 // pseudo-package used for methods with anonymous receivers
122 ir.Pkgs.Go = types.NewPkg("go", "")
123
124 // pseudo-package for use with code coverage instrumentation.
125 ir.Pkgs.Coverage = types.NewPkg("go.coverage", "runtime/coverage")
126 ir.Pkgs.Coverage.Prefix = "runtime/coverage"
127
128 // Record flags that affect the build result. (And don't
129 // record flags that don't, since that would cause spurious
130 // changes in the binary.)
131 dwarfgen.RecordFlags("B", "N", "l", "msan", "race", "asan", "shared", "dynlink", "dwarf", "dwarflocationlists", "dwarfbasentries", "smallframes", "spectre")
132
133 if !base.EnableTrace && base.Flag.LowerT {
134 log.Fatalf("compiler not built with support for -t")
135 }
136
137 // Enable inlining (after RecordFlags, to avoid recording the rewritten -l). For now:
138 // default: inlining on. (Flag.LowerL == 1)
139 // -l: inlining off (Flag.LowerL == 0)
140 // -l=2, -l=3: inlining on again, with extra debugging (Flag.LowerL > 1)
141 if base.Flag.LowerL <= 1 {
142 base.Flag.LowerL = 1 - base.Flag.LowerL
143 }
144
145 if base.Flag.SmallFrames {
146 ir.MaxStackVarSize = 64 * 1024
147 ir.MaxImplicitStackVarSize = 16 * 1024
148 }
149
150 if base.Flag.Dwarf {
151 base.Ctxt.DebugInfo = dwarfgen.Info
152 base.Ctxt.GenAbstractFunc = dwarfgen.AbstractFunc
153 base.Ctxt.DwFixups = obj.NewDwarfFixupTable(base.Ctxt)
154 } else {
155 // turn off inline generation if no dwarf at all
156 base.Flag.GenDwarfInl = 0
157 base.Ctxt.Flag_locationlists = false
158 }
159 if base.Ctxt.Flag_locationlists && len(base.Ctxt.Arch.DWARFRegisters) == 0 {
160 log.Fatalf("location lists requested but register mapping not available on %v", base.Ctxt.Arch.Name)
161 }
162
163 types.ParseLangFlag()
164
165 symABIs := ssagen.NewSymABIs()
166 if base.Flag.SymABIs != "" {
167 symABIs.ReadSymABIs(base.Flag.SymABIs)
168 }
169
170 if objabi.LookupPkgSpecial(base.Ctxt.Pkgpath).NoInstrument {
171 base.Flag.Race = false
172 base.Flag.MSan = false
173 base.Flag.ASan = false
174 }
175
176 ssagen.Arch.LinkArch.Init(base.Ctxt)
177 startProfile()
178 if base.Flag.Race || base.Flag.MSan || base.Flag.ASan {
179 base.Flag.Cfg.Instrumenting = true
180 }
181 if base.Flag.Dwarf {
182 dwarf.EnableLogging(base.Debug.DwarfInl != 0)
183 }
184 if base.Debug.SoftFloat != 0 {
185 ssagen.Arch.SoftFloat = true
186 }
187
188 if base.Flag.JSON != "" { // parse version,destination from json logging optimization.
189 logopt.LogJsonOption(base.Flag.JSON)
190 }
191
192 ir.EscFmt = escape.Fmt
193 ir.IsIntrinsicCall = ssagen.IsIntrinsicCall
194 ir.IsIntrinsicSym = ssagen.IsIntrinsicSym
195 inline.SSADumpInline = ssagen.DumpInline
196 ssagen.InitEnv()
197
198 types.PtrSize = ssagen.Arch.LinkArch.PtrSize
199 types.RegSize = ssagen.Arch.LinkArch.RegSize
200 types.MaxWidth = ssagen.Arch.MAXWIDTH
201
202 typecheck.Target = new(ir.Package)
203
204 base.AutogeneratedPos = makePos(src.NewFileBase("<autogenerated>", "<autogenerated>"), 1, 0)
205
206 typecheck.InitUniverse()
207 typecheck.InitRuntime()
208 rttype.Init()
209
210 // Some intrinsics (notably, the simd intrinsics) mention
211 // types "eagerly", thus ssagen must be initialized AFTER
212 // the type system is ready.
213 ssagen.InitTables()
214
215 // Parse and typecheck input.
216 noder.LoadPackage(flag.Args())
217
218 // As a convenience to users (toolchain maintainers, in particular),
219 // when compiling a package named "main", we default the package
220 // path to "main" if the -p flag was not specified.
221 if base.Ctxt.Pkgpath == obj.UnlinkablePkg && types.LocalPkg.Name == "main" {
222 base.Ctxt.Pkgpath = "main"
223 types.LocalPkg.Path = "main"
224 types.LocalPkg.Prefix = "main"
225 }
226
227 dwarfgen.RecordPackageName()
228
229 // Prepare for backend processing.
230 ssagen.InitConfig()
231
232 // Apply coverage fixups, if applicable.
233 coverage.Fixup()
234
235 // Read profile file and build profile-graph and weighted-call-graph.
236 base.Timer.Start("fe", "pgo-load-profile")
237 var profile *pgoir.Profile
238 if base.Flag.PgoProfile != "" {
239 var err error
240 profile, err = pgoir.New(base.Flag.PgoProfile)
241 if err != nil {
242 log.Fatalf("%s: PGO error: %v", base.Flag.PgoProfile, err)
243 }
244 }
245
246 // Apply bloop markings.
247 bloop.BloopWalk(typecheck.Target)
248
249 // Interleaved devirtualization and inlining.
250 base.Timer.Start("fe", "devirtualize-and-inline")
251 interleaved.DevirtualizeAndInlinePackage(typecheck.Target, profile)
252
253 noder.MakeWrappers(typecheck.Target) // must happen after inlining
254
255 // Get variable capture right in for loops.
256 var transformed []loopvar.VarAndLoop
257 for _, fn := range typecheck.Target.Funcs {
258 transformed = append(transformed, loopvar.ForCapture(fn)...)
259 }
260 ir.CurFunc = nil
261
262 // Build init task, if needed.
263 pkginit.MakeTask()
264
265 // Generate ABI wrappers. Must happen before escape analysis
266 // and doesn't benefit from dead-coding or inlining.
267 symABIs.GenABIWrappers()
268
269 deadlocals.Funcs(typecheck.Target.Funcs)
270
271 // Escape analysis.
272 // Required for moving heap allocations onto stack,
273 // which in turn is required by the closure implementation,
274 // which stores the addresses of stack variables into the closure.
275 // If the closure does not escape, it needs to be on the stack
276 // or else the stack copier will not update it.
277 // Large values are also moved off stack in escape analysis;
278 // because large values may contain pointers, it must happen early.
279 base.Timer.Start("fe", "escapes")
280 escape.Funcs(typecheck.Target.Funcs)
281
282 slice.Funcs(typecheck.Target.Funcs)
283
284 loopvar.LogTransformations(transformed)
285
286 // Collect information for go:nowritebarrierrec
287 // checking. This must happen before transforming closures during Walk
288 // We'll do the final check after write barriers are
289 // inserted.
290 if base.Flag.CompilingRuntime {
291 ssagen.EnableNoWriteBarrierRecCheck()
292 }
293
294 ir.CurFunc = nil
295
296 reflectdata.WriteBasicTypes()
297
298 // Compile top-level declarations.
299 //
300 // There are cyclic dependencies between all of these phases, so we
301 // need to iterate all of them until we reach a fixed point.
302 base.Timer.Start("be", "compilefuncs")
303 for nextFunc, nextExtern := 0, 0; ; {
304 reflectdata.WriteRuntimeTypes()
305
306 if nextExtern < len(typecheck.Target.Externs) {
307 switch n := typecheck.Target.Externs[nextExtern]; n.Op() {
308 case ir.ONAME:
309 dumpGlobal(n)
310 case ir.OLITERAL:
311 dumpGlobalConst(n)
312 case ir.OTYPE:
313 reflectdata.NeedRuntimeType(n.Type())
314 }
315 nextExtern++
316 continue
317 }
318
319 if nextFunc < len(typecheck.Target.Funcs) {
320 enqueueFunc(typecheck.Target.Funcs[nextFunc], symABIs)
321 nextFunc++
322 continue
323 }
324
325 // The SSA backend supports using multiple goroutines, so keep it
326 // as late as possible to maximize how much work we can batch and
327 // process concurrently.
328 if len(compilequeue) != 0 {
329 compileFunctions(profile)
330 continue
331 }
332
333 // Finalize DWARF inline routine DIEs, then explicitly turn off
334 // further DWARF inlining generation to avoid problems with
335 // generated method wrappers.
336 //
337 // Note: The DWARF fixup code for inlined calls currently doesn't
338 // allow multiple invocations, so we intentionally run it just
339 // once after everything else. Worst case, some generated
340 // functions have slightly larger DWARF DIEs.
341 if base.Ctxt.DwFixups != nil {
342 base.Ctxt.DwFixups.Finalize(base.Ctxt.Pkgpath, base.Debug.DwarfInl != 0)
343 base.Ctxt.DwFixups = nil
344 base.Flag.GenDwarfInl = 0
345 continue // may have called reflectdata.TypeLinksym (#62156)
346 }
347
348 break
349 }
350
351 base.Timer.AddEvent(int64(len(typecheck.Target.Funcs)), "funcs")
352
353 if base.Flag.CompilingRuntime {
354 // Write barriers are now known. Check the call graph.
355 ssagen.NoWriteBarrierRecCheck()
356 }
357
358 // Add keep relocations for global maps.
359 if base.Debug.WrapGlobalMapCtl != 1 {
360 staticinit.AddKeepRelocations()
361 }
362
363 // Write object data to disk.
364 base.Timer.Start("be", "dumpobj")
365 dumpdata()
366 base.Ctxt.NumberSyms()
367 dumpobj()
368 if base.Flag.AsmHdr != "" {
369 dumpasmhdr()
370 }
371
372 ssagen.CheckLargeStacks()
373 typecheck.CheckFuncStack()
374
375 if len(compilequeue) != 0 {
376 base.Fatalf("%d uncompiled functions", len(compilequeue))
377 }
378
379 logopt.FlushLoggedOpts(base.Ctxt, base.Ctxt.Pkgpath)
380 base.ExitIfErrors()
381
382 base.FlushErrors()
383 base.Timer.Stop()
384
385 if base.Flag.Bench != "" {
386 if err := writebench(base.Flag.Bench); err != nil {
387 log.Fatalf("cannot write benchmark data: %v", err)
388 }
389 }
390 }
391
392 func writebench(filename string) error {
393 f, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666)
394 if err != nil {
395 return err
396 }
397
398 var buf bytes.Buffer
399 fmt.Fprintln(&buf, "commit:", buildcfg.Version)
400 fmt.Fprintln(&buf, "goos:", runtime.GOOS)
401 fmt.Fprintln(&buf, "goarch:", runtime.GOARCH)
402 base.Timer.Write(&buf, "BenchmarkCompile:"+base.Ctxt.Pkgpath+":")
403
404 n, err := f.Write(buf.Bytes())
405 if err != nil {
406 return err
407 }
408 if n != buf.Len() {
409 panic("bad writer")
410 }
411
412 return f.Close()
413 }
414
415 func makePos(b *src.PosBase, line, col uint) src.XPos {
416 return base.Ctxt.PosTable.XPos(src.MakePos(b, line, col))
417 }