2015-02-13 14:40:36 -05:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package gc
|
|
|
|
|
cmd/compile: avoid giant init functions due to many user inits
We generate code that calls each user init function one at a time.
When there are lots of user init functions,
usually due to generated code, like test/rotate* or
github.com/juju/govmomi/vim25/types,
we can end up with a giant function,
which can be slow to compile.
This CL puts in an escape valve.
When there are more than 500 functions, instead of doing:
init.0()
init.1()
// ...
we construct a static array of functions:
var fns = [...]func(){init.0, init.1, ... }
and call them in a loop.
This generates marginally bigger, marginally worse code,
so we restrict it to cases in which it might start to matter.
500 was selected as a mostly arbitrary threshold for "lots".
Each call uses two Progs, one for PCDATA and one for the call,
so at 500 calls we use ~1000 Progs.
At concurrency==8, we get a Prog cache of about
1000 Progs per worker.
So a threshold of 500 should more or less avoid
exhausting the Prog cache in most cases.
Change-Id: I276b887173ddbf65b2164ec9f9b5eb04d8c753c2
Reviewed-on: https://go-review.googlesource.com/41500
Reviewed-by: Keith Randall <khr@golang.org>
2017-04-23 17:31:15 -07:00
|
|
|
import (
|
|
|
|
"cmd/compile/internal/types"
|
2019-02-05 16:22:38 -08:00
|
|
|
"cmd/internal/obj"
|
cmd/compile: avoid giant init functions due to many user inits
We generate code that calls each user init function one at a time.
When there are lots of user init functions,
usually due to generated code, like test/rotate* or
github.com/juju/govmomi/vim25/types,
we can end up with a giant function,
which can be slow to compile.
This CL puts in an escape valve.
When there are more than 500 functions, instead of doing:
init.0()
init.1()
// ...
we construct a static array of functions:
var fns = [...]func(){init.0, init.1, ... }
and call them in a loop.
This generates marginally bigger, marginally worse code,
so we restrict it to cases in which it might start to matter.
500 was selected as a mostly arbitrary threshold for "lots".
Each call uses two Progs, one for PCDATA and one for the call,
so at 500 calls we use ~1000 Progs.
At concurrency==8, we get a Prog cache of about
1000 Progs per worker.
So a threshold of 500 should more or less avoid
exhausting the Prog cache in most cases.
Change-Id: I276b887173ddbf65b2164ec9f9b5eb04d8c753c2
Reviewed-on: https://go-review.googlesource.com/41500
Reviewed-by: Keith Randall <khr@golang.org>
2017-04-23 17:31:15 -07:00
|
|
|
)
|
cmd/compile: factor out Pkg, Sym, and Type into package types
- created new package cmd/compile/internal/types
- moved Pkg, Sym, Type to new package
- to break cycles, for now we need the (ugly) types/utils.go
file which contains a handful of functions that must be installed
early by the gc frontend
- to break cycles, for now we need two functions to convert between
*gc.Node and *types.Node (the latter is a dummy type)
- adjusted the gc's code to use the new package and the conversion
functions as needed
- made several Pkg, Sym, and Type methods functions as needed
- renamed constructors typ, typPtr, typArray, etc. to types.New,
types.NewPtr, types.NewArray, etc.
Passes toolstash-check -all.
Change-Id: I8adfa5e85c731645d0a7fd2030375ed6ebf54b72
Reviewed-on: https://go-review.googlesource.com/39855
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
2017-04-04 17:54:02 -07:00
|
|
|
|
2017-04-23 16:34:35 -07:00
|
|
|
// A function named init is a special case.
|
|
|
|
// It is called by the initialization before main is run.
|
|
|
|
// To make it unique within a package and also uncallable,
|
|
|
|
// the name, normally "pkg.init", is altered to "pkg.init.0".
|
|
|
|
var renameinitgen int
|
2015-02-13 14:40:36 -05:00
|
|
|
|
2020-11-24 22:09:57 -05:00
|
|
|
// Function collecting autotmps generated during typechecking,
|
|
|
|
// to be included in the package-level init function.
|
|
|
|
var initTodo = nod(ODCLFUNC, nil, nil)
|
cmd/compile: rewrite f(g()) for multi-value g() during typecheck
This is a re-attempt at CL 153841, which caused two regressions:
1. crypto/ecdsa failed to build with -gcflags=-l=4. This was because
when "t1, t2, ... := g(); f(t1, t2, ...)" was exported, we were losing
the first assignment from the call's Ninit field.
2. net/http/pprof failed to run with -gcflags=-N. This is due to a
conflict with CL 159717: as of that CL, package-scope initialization
statements are executed within the "init.ializer" function, rather
than the "init" function, and the generated temp variables need to be
moved accordingly too.
[Rest of description is as before.]
This CL moves order.go's copyRet logic for rewriting f(g()) into t1,
t2, ... := g(); f(t1, t2, ...) earlier into typecheck. This allows the
rest of the compiler to stop worrying about multi-value functions
appearing outside of OAS2FUNC nodes.
This changes compiler behavior in a few observable ways:
1. Typechecking error messages for builtin functions now use general
case error messages rather than unnecessarily differing ones.
2. Because f(g()) is rewritten before inlining, saved inline bodies
now see the rewritten form too. This could be addressed, but doesn't
seem worthwhile.
3. Most notably, this simplifies escape analysis and fixes a memory
corruption issue in esc.go. See #29197 for details.
Fixes #15992.
Fixes #29197.
Change-Id: I930b10f7e27af68a0944d6c9bfc8707c3fab27a4
Reviewed-on: https://go-review.googlesource.com/c/go/+/166983
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
2018-12-12 11:15:37 -08:00
|
|
|
|
cmd/compile: factor out Pkg, Sym, and Type into package types
- created new package cmd/compile/internal/types
- moved Pkg, Sym, Type to new package
- to break cycles, for now we need the (ugly) types/utils.go
file which contains a handful of functions that must be installed
early by the gc frontend
- to break cycles, for now we need two functions to convert between
*gc.Node and *types.Node (the latter is a dummy type)
- adjusted the gc's code to use the new package and the conversion
functions as needed
- made several Pkg, Sym, and Type methods functions as needed
- renamed constructors typ, typPtr, typArray, etc. to types.New,
types.NewPtr, types.NewArray, etc.
Passes toolstash-check -all.
Change-Id: I8adfa5e85c731645d0a7fd2030375ed6ebf54b72
Reviewed-on: https://go-review.googlesource.com/39855
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
2017-04-04 17:54:02 -07:00
|
|
|
func renameinit() *types.Sym {
|
2017-04-23 16:34:35 -07:00
|
|
|
s := lookupN("init.", renameinitgen)
|
|
|
|
renameinitgen++
|
|
|
|
return s
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
2019-02-05 16:22:38 -08:00
|
|
|
// fninit makes an initialization record for the package.
|
|
|
|
// See runtime/proc.go:initTask for its layout.
|
|
|
|
// The 3 tasks for initialization are:
|
|
|
|
// 1) Initialize all of the packages the current package depends on.
|
|
|
|
// 2) Initialize all the variables that have initializers.
|
|
|
|
// 3) Run any init functions.
|
|
|
|
func fninit(n []*Node) {
|
2019-03-28 14:35:49 -07:00
|
|
|
nf := initOrder(n)
|
2015-02-13 14:40:36 -05:00
|
|
|
|
2019-02-05 16:22:38 -08:00
|
|
|
var deps []*obj.LSym // initTask records for packages the current package depends on
|
|
|
|
var fns []*obj.LSym // functions to call for package initialization
|
2015-02-13 14:40:36 -05:00
|
|
|
|
2019-02-05 16:22:38 -08:00
|
|
|
// Find imported packages with init tasks.
|
2019-05-21 01:01:12 -04:00
|
|
|
for _, s := range types.InitSyms {
|
|
|
|
deps = append(deps, s.Linksym())
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
2019-01-25 10:21:40 -08:00
|
|
|
// Make a function that contains all the initialization statements.
|
|
|
|
if len(nf) > 0 {
|
|
|
|
lineno = nf[0].Pos // prolog/epilog gets line number of first init stmt
|
2019-03-18 13:17:35 -07:00
|
|
|
initializers := lookup("init")
|
2019-01-25 10:21:40 -08:00
|
|
|
fn := dclfunc(initializers, nod(OTFUNC, nil, nil))
|
2020-11-24 22:09:57 -05:00
|
|
|
for _, dcl := range initTodo.Func.Dcl {
|
cmd/compile: rewrite f(g()) for multi-value g() during typecheck
This is a re-attempt at CL 153841, which caused two regressions:
1. crypto/ecdsa failed to build with -gcflags=-l=4. This was because
when "t1, t2, ... := g(); f(t1, t2, ...)" was exported, we were losing
the first assignment from the call's Ninit field.
2. net/http/pprof failed to run with -gcflags=-N. This is due to a
conflict with CL 159717: as of that CL, package-scope initialization
statements are executed within the "init.ializer" function, rather
than the "init" function, and the generated temp variables need to be
moved accordingly too.
[Rest of description is as before.]
This CL moves order.go's copyRet logic for rewriting f(g()) into t1,
t2, ... := g(); f(t1, t2, ...) earlier into typecheck. This allows the
rest of the compiler to stop worrying about multi-value functions
appearing outside of OAS2FUNC nodes.
This changes compiler behavior in a few observable ways:
1. Typechecking error messages for builtin functions now use general
case error messages rather than unnecessarily differing ones.
2. Because f(g()) is rewritten before inlining, saved inline bodies
now see the rewritten form too. This could be addressed, but doesn't
seem worthwhile.
3. Most notably, this simplifies escape analysis and fixes a memory
corruption issue in esc.go. See #29197 for details.
Fixes #15992.
Fixes #29197.
Change-Id: I930b10f7e27af68a0944d6c9bfc8707c3fab27a4
Reviewed-on: https://go-review.googlesource.com/c/go/+/166983
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
2018-12-12 11:15:37 -08:00
|
|
|
dcl.Name.Curfn = fn
|
|
|
|
}
|
2020-11-24 22:09:57 -05:00
|
|
|
fn.Func.Dcl = append(fn.Func.Dcl, initTodo.Func.Dcl...)
|
|
|
|
initTodo.Func.Dcl = nil
|
cmd/compile: rewrite f(g()) for multi-value g() during typecheck
This is a re-attempt at CL 153841, which caused two regressions:
1. crypto/ecdsa failed to build with -gcflags=-l=4. This was because
when "t1, t2, ... := g(); f(t1, t2, ...)" was exported, we were losing
the first assignment from the call's Ninit field.
2. net/http/pprof failed to run with -gcflags=-N. This is due to a
conflict with CL 159717: as of that CL, package-scope initialization
statements are executed within the "init.ializer" function, rather
than the "init" function, and the generated temp variables need to be
moved accordingly too.
[Rest of description is as before.]
This CL moves order.go's copyRet logic for rewriting f(g()) into t1,
t2, ... := g(); f(t1, t2, ...) earlier into typecheck. This allows the
rest of the compiler to stop worrying about multi-value functions
appearing outside of OAS2FUNC nodes.
This changes compiler behavior in a few observable ways:
1. Typechecking error messages for builtin functions now use general
case error messages rather than unnecessarily differing ones.
2. Because f(g()) is rewritten before inlining, saved inline bodies
now see the rewritten form too. This could be addressed, but doesn't
seem worthwhile.
3. Most notably, this simplifies escape analysis and fixes a memory
corruption issue in esc.go. See #29197 for details.
Fixes #15992.
Fixes #29197.
Change-Id: I930b10f7e27af68a0944d6c9bfc8707c3fab27a4
Reviewed-on: https://go-review.googlesource.com/c/go/+/166983
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
2018-12-12 11:15:37 -08:00
|
|
|
|
2019-01-25 10:21:40 -08:00
|
|
|
fn.Nbody.Set(nf)
|
|
|
|
funcbody()
|
|
|
|
|
|
|
|
fn = typecheck(fn, ctxStmt)
|
|
|
|
Curfn = fn
|
|
|
|
typecheckslice(nf, ctxStmt)
|
|
|
|
Curfn = nil
|
2020-09-14 12:56:37 -07:00
|
|
|
xtop = append(xtop, fn)
|
2019-02-05 16:22:38 -08:00
|
|
|
fns = append(fns, initializers.Linksym())
|
2019-01-25 10:21:40 -08:00
|
|
|
}
|
2020-11-24 22:09:57 -05:00
|
|
|
if initTodo.Func.Dcl != nil {
|
|
|
|
// We only generate temps using initTodo if there
|
cmd/compile: rewrite f(g()) for multi-value g() during typecheck
This is a re-attempt at CL 153841, which caused two regressions:
1. crypto/ecdsa failed to build with -gcflags=-l=4. This was because
when "t1, t2, ... := g(); f(t1, t2, ...)" was exported, we were losing
the first assignment from the call's Ninit field.
2. net/http/pprof failed to run with -gcflags=-N. This is due to a
conflict with CL 159717: as of that CL, package-scope initialization
statements are executed within the "init.ializer" function, rather
than the "init" function, and the generated temp variables need to be
moved accordingly too.
[Rest of description is as before.]
This CL moves order.go's copyRet logic for rewriting f(g()) into t1,
t2, ... := g(); f(t1, t2, ...) earlier into typecheck. This allows the
rest of the compiler to stop worrying about multi-value functions
appearing outside of OAS2FUNC nodes.
This changes compiler behavior in a few observable ways:
1. Typechecking error messages for builtin functions now use general
case error messages rather than unnecessarily differing ones.
2. Because f(g()) is rewritten before inlining, saved inline bodies
now see the rewritten form too. This could be addressed, but doesn't
seem worthwhile.
3. Most notably, this simplifies escape analysis and fixes a memory
corruption issue in esc.go. See #29197 for details.
Fixes #15992.
Fixes #29197.
Change-Id: I930b10f7e27af68a0944d6c9bfc8707c3fab27a4
Reviewed-on: https://go-review.googlesource.com/c/go/+/166983
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
2018-12-12 11:15:37 -08:00
|
|
|
// are package-scope initialization statements, so
|
|
|
|
// something's weird if we get here.
|
2020-11-24 22:09:57 -05:00
|
|
|
Fatalf("initTodo still has declarations")
|
cmd/compile: rewrite f(g()) for multi-value g() during typecheck
This is a re-attempt at CL 153841, which caused two regressions:
1. crypto/ecdsa failed to build with -gcflags=-l=4. This was because
when "t1, t2, ... := g(); f(t1, t2, ...)" was exported, we were losing
the first assignment from the call's Ninit field.
2. net/http/pprof failed to run with -gcflags=-N. This is due to a
conflict with CL 159717: as of that CL, package-scope initialization
statements are executed within the "init.ializer" function, rather
than the "init" function, and the generated temp variables need to be
moved accordingly too.
[Rest of description is as before.]
This CL moves order.go's copyRet logic for rewriting f(g()) into t1,
t2, ... := g(); f(t1, t2, ...) earlier into typecheck. This allows the
rest of the compiler to stop worrying about multi-value functions
appearing outside of OAS2FUNC nodes.
This changes compiler behavior in a few observable ways:
1. Typechecking error messages for builtin functions now use general
case error messages rather than unnecessarily differing ones.
2. Because f(g()) is rewritten before inlining, saved inline bodies
now see the rewritten form too. This could be addressed, but doesn't
seem worthwhile.
3. Most notably, this simplifies escape analysis and fixes a memory
corruption issue in esc.go. See #29197 for details.
Fixes #15992.
Fixes #29197.
Change-Id: I930b10f7e27af68a0944d6c9bfc8707c3fab27a4
Reviewed-on: https://go-review.googlesource.com/c/go/+/166983
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
2018-12-12 11:15:37 -08:00
|
|
|
}
|
2020-11-24 22:09:57 -05:00
|
|
|
initTodo = nil
|
2019-01-25 10:21:40 -08:00
|
|
|
|
2019-02-05 16:22:38 -08:00
|
|
|
// Record user init functions.
|
|
|
|
for i := 0; i < renameinitgen; i++ {
|
|
|
|
s := lookupN("init.", i)
|
2019-10-13 23:06:29 +07:00
|
|
|
fn := asNode(s.Def).Name.Defn
|
|
|
|
// Skip init functions with empty bodies.
|
2020-09-14 12:56:37 -07:00
|
|
|
if fn.Nbody.Len() == 1 && fn.Nbody.First().Op == OEMPTY {
|
2019-10-13 23:06:29 +07:00
|
|
|
continue
|
|
|
|
}
|
2019-02-05 16:22:38 -08:00
|
|
|
fns = append(fns, s.Linksym())
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
2019-02-05 16:22:38 -08:00
|
|
|
if len(deps) == 0 && len(fns) == 0 && localpkg.Name != "main" && localpkg.Name != "runtime" {
|
|
|
|
return // nothing to initialize
|
2019-01-25 10:21:40 -08:00
|
|
|
}
|
2015-02-13 14:40:36 -05:00
|
|
|
|
2019-02-05 16:22:38 -08:00
|
|
|
// Make an .inittask structure.
|
|
|
|
sym := lookup(".inittask")
|
|
|
|
nn := newname(sym)
|
2020-11-24 22:09:57 -05:00
|
|
|
nn.Type = types.Types[TUINT8] // fake type
|
2019-02-05 16:22:38 -08:00
|
|
|
nn.SetClass(PEXTERN)
|
|
|
|
sym.Def = asTypesNode(nn)
|
|
|
|
exportsym(nn)
|
|
|
|
lsym := sym.Linksym()
|
|
|
|
ot := 0
|
|
|
|
ot = duintptr(lsym, ot, 0) // state: not initialized yet
|
|
|
|
ot = duintptr(lsym, ot, uint64(len(deps)))
|
|
|
|
ot = duintptr(lsym, ot, uint64(len(fns)))
|
|
|
|
for _, d := range deps {
|
|
|
|
ot = dsymptr(lsym, ot, d, 0)
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
2019-02-05 16:22:38 -08:00
|
|
|
for _, f := range fns {
|
|
|
|
ot = dsymptr(lsym, ot, f, 0)
|
|
|
|
}
|
|
|
|
// An initTask has pointers, but none into the Go heap.
|
|
|
|
// It's not quite read only, the state field must be modifiable.
|
|
|
|
ggloblsym(lsym, int32(ot), obj.NOPTR)
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|