| |
| |
| |
|
|
| package gc |
|
|
| import ( |
| "cmp" |
| "internal/race" |
| "math/rand" |
| "slices" |
| "sync" |
|
|
| "cmd/compile/internal/base" |
| "cmd/compile/internal/ir" |
| "cmd/compile/internal/liveness" |
| "cmd/compile/internal/objw" |
| "cmd/compile/internal/pgoir" |
| "cmd/compile/internal/ssagen" |
| "cmd/compile/internal/staticinit" |
| "cmd/compile/internal/types" |
| "cmd/compile/internal/walk" |
| "cmd/internal/obj" |
| ) |
|
|
| |
|
|
| var ( |
| compilequeue []*ir.Func |
| ) |
|
|
| func enqueueFunc(fn *ir.Func, symABIs *ssagen.SymABIs) { |
| if ir.CurFunc != nil { |
| base.FatalfAt(fn.Pos(), "enqueueFunc %v inside %v", fn, ir.CurFunc) |
| } |
|
|
| if ir.FuncName(fn) == "_" { |
| |
| |
| return |
| } |
|
|
| if fn.IsClosure() { |
| return |
| } |
|
|
| if ssagen.CreateWasmImportWrapper(fn) { |
| return |
| } |
|
|
| if len(fn.Body) == 0 { |
| if ir.IsIntrinsicSym(fn.Sym()) && fn.Sym().Linkname == "" && !symABIs.HasDef(fn.Sym()) { |
| |
| |
| |
| |
| ssagen.GenIntrinsicBody(fn) |
| } else { |
| |
| ir.InitLSym(fn, false) |
| types.CalcSize(fn.Type()) |
| a := ssagen.AbiForBodylessFuncStackMap(fn) |
| abiInfo := a.ABIAnalyzeFuncType(fn.Type()) |
| if fn.ABI == obj.ABI0 { |
| |
| |
| |
| |
| liveness.WriteFuncMap(fn, abiInfo) |
|
|
| x := ssagen.EmitArgInfo(fn, abiInfo) |
| objw.Global(x, int32(len(x.P)), obj.RODATA|obj.LOCAL) |
| } |
| return |
| } |
| } |
|
|
| errorsBefore := base.Errors() |
|
|
| todo := []*ir.Func{fn} |
| for len(todo) > 0 { |
| next := todo[len(todo)-1] |
| todo = todo[:len(todo)-1] |
|
|
| prepareFunc(next) |
| todo = append(todo, next.Closures...) |
| } |
|
|
| if base.Errors() > errorsBefore { |
| return |
| } |
|
|
| |
| |
| compilequeue = append(compilequeue, fn) |
| } |
|
|
| |
| |
| func prepareFunc(fn *ir.Func) { |
| |
| |
| |
| ir.InitLSym(fn, true) |
|
|
| |
| |
| if staticinit.MapInitToVar != nil { |
| if _, ok := staticinit.MapInitToVar[fn]; ok { |
| ssagen.RegisterMapInitLsym(fn.Linksym()) |
| } |
| } |
|
|
| |
| types.CalcSize(fn.Type()) |
|
|
| |
| |
| |
| ssagen.GenWasmExportWrapper(fn) |
|
|
| ir.CurFunc = fn |
| walk.Walk(fn) |
| ir.CurFunc = nil |
|
|
| base.Ctxt.DwTextCount++ |
| } |
|
|
| |
| |
| |
| func compileFunctions(profile *pgoir.Profile) { |
| if race.Enabled { |
| |
| tmp := make([]*ir.Func, len(compilequeue)) |
| perm := rand.Perm(len(compilequeue)) |
| for i, v := range perm { |
| tmp[v] = compilequeue[i] |
| } |
| copy(compilequeue, tmp) |
| } else { |
| |
| |
| |
| slices.SortFunc(compilequeue, func(a, b *ir.Func) int { |
| return cmp.Compare(len(b.Body), len(a.Body)) |
| }) |
| } |
|
|
| |
| |
| queue := func(work func(int)) { |
| work(0) |
| } |
|
|
| if nWorkers := base.Flag.LowerC; nWorkers > 1 { |
| |
| |
| |
| workq := make(chan func(int)) |
| done := make(chan int) |
| go func() { |
| ids := make([]int, nWorkers) |
| for i := range ids { |
| ids[i] = i |
| } |
| var pending []func(int) |
| for { |
| select { |
| case work := <-workq: |
| pending = append(pending, work) |
| case id := <-done: |
| ids = append(ids, id) |
| } |
| for len(pending) > 0 && len(ids) > 0 { |
| work := pending[len(pending)-1] |
| id := ids[len(ids)-1] |
| pending = pending[:len(pending)-1] |
| ids = ids[:len(ids)-1] |
| go func() { |
| work(id) |
| done <- id |
| }() |
| } |
| } |
| }() |
| queue = func(work func(int)) { |
| workq <- work |
| } |
| } |
|
|
| var wg sync.WaitGroup |
| var compile func([]*ir.Func) |
| compile = func(fns []*ir.Func) { |
| wg.Add(len(fns)) |
| for _, fn := range fns { |
| fn := fn |
| queue(func(worker int) { |
| ssagen.Compile(fn, worker, profile) |
| compile(fn.Closures) |
| wg.Done() |
| }) |
| } |
| } |
|
|
| types.CalcSizeDisabled = true |
| base.Ctxt.InParallel = true |
|
|
| compile(compilequeue) |
| compilequeue = nil |
| wg.Wait() |
|
|
| base.Ctxt.InParallel = false |
| types.CalcSizeDisabled = false |
| } |
|
|