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

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

View as plain text