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
|
|
|
|
|
|
|
|
|
|
import (
|
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
|
|
|
"cmd/compile/internal/types"
|
2015-02-13 14:40:36 -05:00
|
|
|
"fmt"
|
|
|
|
|
)
|
|
|
|
|
|
2015-10-22 09:51:12 +09:00
|
|
|
// function literals aka closures
|
2015-02-13 14:40:36 -05:00
|
|
|
func closurehdr(ntype *Node) {
|
2016-09-16 11:00:54 +10:00
|
|
|
n := nod(OCLOSURE, nil, nil)
|
2017-03-23 15:26:10 -07:00
|
|
|
n.Func.SetIsHiddenClosure(Curfn != nil)
|
2015-05-27 00:44:05 -04:00
|
|
|
n.Func.Ntype = ntype
|
2016-09-16 00:33:29 +10:00
|
|
|
n.Func.Depth = funcdepth
|
2015-03-25 19:33:01 -07:00
|
|
|
n.Func.Outerfunc = Curfn
|
2015-02-13 14:40:36 -05:00
|
|
|
|
2017-04-07 19:50:31 +00:00
|
|
|
funchdr(n)
|
2015-02-13 14:40:36 -05:00
|
|
|
|
|
|
|
|
// steal ntype's argument names and
|
|
|
|
|
// leave a fresh copy in their place.
|
|
|
|
|
// references to these variables need to
|
|
|
|
|
// refer to the variables in the external
|
|
|
|
|
// function declared below; see walkclosure.
|
2016-03-08 15:10:26 -08:00
|
|
|
n.List.Set(ntype.List.Slice())
|
2015-02-13 14:40:36 -05:00
|
|
|
|
2016-03-08 15:10:26 -08:00
|
|
|
n.Rlist.Set(ntype.Rlist.Slice())
|
|
|
|
|
ntype.List.Set(nil)
|
|
|
|
|
ntype.Rlist.Set(nil)
|
|
|
|
|
for _, n1 := range n.List.Slice() {
|
2016-08-31 09:54:00 +10:00
|
|
|
name := n1.Left
|
2015-02-13 14:40:36 -05:00
|
|
|
if name != nil {
|
|
|
|
|
name = newname(name.Sym)
|
|
|
|
|
}
|
2016-09-16 11:00:54 +10:00
|
|
|
a := nod(ODCLFIELD, name, n1.Right)
|
2017-02-27 19:56:38 +02:00
|
|
|
a.SetIsddd(n1.Isddd())
|
2015-02-13 14:40:36 -05:00
|
|
|
if name != nil {
|
2017-02-27 19:56:38 +02:00
|
|
|
name.SetIsddd(a.Isddd())
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
2016-03-08 15:10:26 -08:00
|
|
|
ntype.List.Append(a)
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
2016-03-08 15:10:26 -08:00
|
|
|
for _, n2 := range n.Rlist.Slice() {
|
2016-08-31 09:54:00 +10:00
|
|
|
name := n2.Left
|
2015-02-13 14:40:36 -05:00
|
|
|
if name != nil {
|
|
|
|
|
name = newname(name.Sym)
|
|
|
|
|
}
|
2016-09-16 11:00:54 +10:00
|
|
|
ntype.Rlist.Append(nod(ODCLFIELD, name, n2.Right))
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-09 20:29:21 -08:00
|
|
|
func closurebody(body []*Node) *Node {
|
|
|
|
|
if len(body) == 0 {
|
2016-09-16 11:00:54 +10:00
|
|
|
body = []*Node{nod(OEMPTY, nil, nil)}
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
2015-02-23 16:07:24 -05:00
|
|
|
func_ := Curfn
|
2016-03-09 20:29:21 -08:00
|
|
|
func_.Nbody.Set(body)
|
2015-03-25 19:33:01 -07:00
|
|
|
func_.Func.Endlineno = lineno
|
2017-04-07 19:50:31 +00:00
|
|
|
funcbody(func_)
|
2015-02-13 14:40:36 -05:00
|
|
|
|
|
|
|
|
// closure-specific variables are hanging off the
|
|
|
|
|
// ordinary ones in the symbol table; see oldname.
|
|
|
|
|
// unhook them.
|
|
|
|
|
// make the list of pointers for the closure call.
|
2016-02-26 17:03:58 -08:00
|
|
|
for _, v := range func_.Func.Cvars.Slice() {
|
2016-05-27 00:56:19 -04:00
|
|
|
// Unlink from v1; see comment in syntax.go type Param for these fields.
|
|
|
|
|
v1 := v.Name.Defn
|
|
|
|
|
v1.Name.Param.Innermost = v.Name.Param.Outer
|
2016-05-27 14:24:26 -04:00
|
|
|
|
2016-05-27 00:56:19 -04:00
|
|
|
// If the closure usage of v is not dense,
|
|
|
|
|
// we need to make it dense; now that we're out
|
|
|
|
|
// of the function in which v appeared,
|
|
|
|
|
// look up v.Sym in the enclosing function
|
|
|
|
|
// and keep it around for use in the compiled code.
|
|
|
|
|
//
|
|
|
|
|
// That is, suppose we just finished parsing the innermost
|
|
|
|
|
// closure f4 in this code:
|
|
|
|
|
//
|
|
|
|
|
// func f() {
|
|
|
|
|
// v := 1
|
|
|
|
|
// func() { // f2
|
|
|
|
|
// use(v)
|
|
|
|
|
// func() { // f3
|
|
|
|
|
// func() { // f4
|
|
|
|
|
// use(v)
|
|
|
|
|
// }()
|
|
|
|
|
// }()
|
|
|
|
|
// }()
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// At this point v.Outer is f2's v; there is no f3's v.
|
|
|
|
|
// To construct the closure f4 from within f3,
|
|
|
|
|
// we need to use f3's v and in this case we need to create f3's v.
|
|
|
|
|
// We are now in the context of f3, so calling oldname(v.Sym)
|
|
|
|
|
// obtains f3's v, creating it if necessary (as it is in the example).
|
|
|
|
|
//
|
|
|
|
|
// capturevars will decide whether to use v directly or &v.
|
|
|
|
|
v.Name.Param.Outer = oldname(v.Sym)
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return func_
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func typecheckclosure(func_ *Node, top int) {
|
2016-02-26 17:03:58 -08:00
|
|
|
for _, ln := range func_.Func.Cvars.Slice() {
|
2016-05-27 00:56:19 -04:00
|
|
|
n := ln.Name.Defn
|
2017-02-27 19:56:38 +02:00
|
|
|
if !n.Name.Captured() {
|
|
|
|
|
n.Name.SetCaptured(true)
|
2015-05-15 10:02:19 -07:00
|
|
|
if n.Name.Decldepth == 0 {
|
2016-09-09 21:08:46 -07:00
|
|
|
Fatalf("typecheckclosure: var %S does not have decldepth assigned", n)
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Ignore assignments to the variable in straightline code
|
|
|
|
|
// preceding the first capturing by a closure.
|
2015-05-15 10:02:19 -07:00
|
|
|
if n.Name.Decldepth == decldepth {
|
2017-02-27 19:56:38 +02:00
|
|
|
n.SetAssigned(false)
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-25 10:35:19 -08:00
|
|
|
for _, ln := range func_.Func.Dcl {
|
|
|
|
|
if ln.Op == ONAME && (ln.Class == PPARAM || ln.Class == PPARAMOUT) {
|
|
|
|
|
ln.Name.Decldepth = 1
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-23 16:07:24 -05:00
|
|
|
oldfn := Curfn
|
cmd/compile: reduce use of **Node parameters
Escape analysis has a hard time with tree-like
structures (see #13493 and #14858).
This is unlikely to change.
As a result, when invoking a function that accepts
a **Node parameter, we usually allocate a *Node
on the heap. This happens a whole lot.
This CL changes functions from taking a **Node
to acting more like append: It both modifies
the input and returns a replacement for it.
Because of the cascading nature of escape analysis,
in order to get the benefits, I had to modify
almost all such functions. The remaining functions
are in racewalk and the backend. I would be happy
to update them as well in a separate CL.
This CL was created by manually updating the
function signatures and the directly impacted
bits of code. The callsites were then automatically
updated using a bespoke script:
https://gist.github.com/josharian/046b1be7aceae244de39
For ease of reviewing and future understanding,
this CL is also broken down into four CLs,
mailed separately, which show the manual
and the automated changes separately.
They are CLs 20990, 20991, 20992, and 20993.
Passes toolstash -cmp.
name old time/op new time/op delta
Template 335ms ± 5% 324ms ± 5% -3.35% (p=0.000 n=23+24)
Unicode 176ms ± 9% 165ms ± 6% -6.12% (p=0.000 n=23+24)
GoTypes 1.10s ± 4% 1.07s ± 2% -2.77% (p=0.000 n=24+24)
Compiler 5.31s ± 3% 5.15s ± 3% -2.95% (p=0.000 n=24+24)
MakeBash 41.6s ± 1% 41.7s ± 2% ~ (p=0.586 n=23+23)
name old alloc/op new alloc/op delta
Template 63.3MB ± 0% 62.4MB ± 0% -1.36% (p=0.000 n=25+23)
Unicode 42.4MB ± 0% 41.6MB ± 0% -1.99% (p=0.000 n=24+25)
GoTypes 220MB ± 0% 217MB ± 0% -1.11% (p=0.000 n=25+25)
Compiler 994MB ± 0% 973MB ± 0% -2.08% (p=0.000 n=24+25)
name old allocs/op new allocs/op delta
Template 681k ± 0% 574k ± 0% -15.71% (p=0.000 n=24+25)
Unicode 518k ± 0% 413k ± 0% -20.34% (p=0.000 n=25+24)
GoTypes 2.08M ± 0% 1.78M ± 0% -14.62% (p=0.000 n=25+25)
Compiler 9.26M ± 0% 7.64M ± 0% -17.48% (p=0.000 n=25+25)
name old text-bytes new text-bytes delta
HelloSize 578k ± 0% 578k ± 0% ~ (all samples are equal)
CmdGoSize 6.46M ± 0% 6.46M ± 0% ~ (all samples are equal)
name old data-bytes new data-bytes delta
HelloSize 128k ± 0% 128k ± 0% ~ (all samples are equal)
CmdGoSize 281k ± 0% 281k ± 0% ~ (all samples are equal)
name old exe-bytes new exe-bytes delta
HelloSize 921k ± 0% 921k ± 0% ~ (all samples are equal)
CmdGoSize 9.86M ± 0% 9.86M ± 0% ~ (all samples are equal)
Change-Id: I277d95bd56d51c166ef7f560647aeaa092f3f475
Reviewed-on: https://go-review.googlesource.com/20959
Reviewed-by: Dave Cheney <dave@cheney.net>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2016-03-20 08:03:31 -07:00
|
|
|
func_.Func.Ntype = typecheck(func_.Func.Ntype, Etype)
|
2015-05-27 00:44:05 -04:00
|
|
|
func_.Type = func_.Func.Ntype.Type
|
|
|
|
|
func_.Func.Top = top
|
2015-02-13 14:40:36 -05:00
|
|
|
|
|
|
|
|
// Type check the body now, but only if we're inside a function.
|
|
|
|
|
// At top level (in a variable initialization: curfn==nil) we're not
|
|
|
|
|
// ready to type check code yet; we'll check it later, because the
|
|
|
|
|
// underlying closure function we create is added to xtop.
|
|
|
|
|
if Curfn != nil && func_.Type != nil {
|
|
|
|
|
Curfn = func_
|
2015-02-23 16:07:24 -05:00
|
|
|
olddd := decldepth
|
2015-02-13 14:40:36 -05:00
|
|
|
decldepth = 1
|
2016-03-19 17:02:01 -07:00
|
|
|
typecheckslice(func_.Nbody.Slice(), Etop)
|
2015-02-13 14:40:36 -05:00
|
|
|
decldepth = olddd
|
|
|
|
|
Curfn = oldfn
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create top-level function
|
2016-03-09 20:29:21 -08:00
|
|
|
xtop = append(xtop, makeclosure(func_))
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
2015-02-20 13:54:45 -05:00
|
|
|
// closurename returns name for OCLOSURE n.
|
|
|
|
|
// It is not as simple as it ought to be, because we typecheck nested closures
|
|
|
|
|
// starting from the innermost one. So when we check the inner closure,
|
|
|
|
|
// we don't yet have name for the outer closure. This function uses recursion
|
|
|
|
|
// to generate names all the way up if necessary.
|
|
|
|
|
|
|
|
|
|
var closurename_closgen int
|
|
|
|
|
|
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 closurename(n *Node) *types.Sym {
|
2015-02-20 13:54:45 -05:00
|
|
|
if n.Sym != nil {
|
|
|
|
|
return n.Sym
|
|
|
|
|
}
|
2015-02-23 16:07:24 -05:00
|
|
|
gen := 0
|
|
|
|
|
outer := ""
|
|
|
|
|
prefix := ""
|
2016-08-31 09:54:00 +10:00
|
|
|
switch {
|
|
|
|
|
case n.Func.Outerfunc == nil:
|
2015-02-20 13:54:45 -05:00
|
|
|
// Global closure.
|
2016-06-27 08:21:55 -07:00
|
|
|
outer = "glob."
|
2015-02-20 13:54:45 -05:00
|
|
|
|
|
|
|
|
prefix = "func"
|
|
|
|
|
closurename_closgen++
|
|
|
|
|
gen = closurename_closgen
|
2016-08-31 09:54:00 +10:00
|
|
|
case n.Func.Outerfunc.Op == ODCLFUNC:
|
2015-02-20 13:54:45 -05:00
|
|
|
// The outermost closure inside of a named function.
|
2015-05-27 10:42:55 -04:00
|
|
|
outer = n.Func.Outerfunc.Func.Nname.Sym.Name
|
2015-02-20 13:54:45 -05:00
|
|
|
|
|
|
|
|
prefix = "func"
|
|
|
|
|
|
|
|
|
|
// Yes, functions can be named _.
|
|
|
|
|
// Can't use function closgen in such case,
|
|
|
|
|
// because it would lead to name clashes.
|
2015-05-27 10:42:55 -04:00
|
|
|
if !isblank(n.Func.Outerfunc.Func.Nname) {
|
2015-03-25 19:33:01 -07:00
|
|
|
n.Func.Outerfunc.Func.Closgen++
|
|
|
|
|
gen = n.Func.Outerfunc.Func.Closgen
|
2015-02-20 13:54:45 -05:00
|
|
|
} else {
|
|
|
|
|
closurename_closgen++
|
|
|
|
|
gen = closurename_closgen
|
|
|
|
|
}
|
2016-08-31 09:54:00 +10:00
|
|
|
case n.Func.Outerfunc.Op == OCLOSURE:
|
2015-02-20 13:54:45 -05:00
|
|
|
// Nested closure, recurse.
|
2015-03-25 19:33:01 -07:00
|
|
|
outer = closurename(n.Func.Outerfunc).Name
|
2015-02-20 13:54:45 -05:00
|
|
|
|
|
|
|
|
prefix = ""
|
2015-03-25 19:33:01 -07:00
|
|
|
n.Func.Outerfunc.Func.Closgen++
|
|
|
|
|
gen = n.Func.Outerfunc.Func.Closgen
|
2016-08-31 09:54:00 +10:00
|
|
|
default:
|
2016-09-09 21:08:46 -07:00
|
|
|
Fatalf("closurename called for %S", n)
|
2015-02-20 13:54:45 -05:00
|
|
|
}
|
2017-03-30 15:44:12 -07:00
|
|
|
n.Sym = lookup(fmt.Sprintf("%s.%s%d", outer, prefix, gen))
|
2015-02-20 13:54:45 -05:00
|
|
|
return n.Sym
|
|
|
|
|
}
|
2015-02-13 14:40:36 -05:00
|
|
|
|
|
|
|
|
func makeclosure(func_ *Node) *Node {
|
2015-10-22 09:51:12 +09:00
|
|
|
// wrap body in external function
|
|
|
|
|
// that begins by reading closure parameters.
|
2016-09-16 11:00:54 +10:00
|
|
|
xtype := nod(OTFUNC, nil, nil)
|
2015-02-13 14:40:36 -05:00
|
|
|
|
2016-03-08 15:10:26 -08:00
|
|
|
xtype.List.Set(func_.List.Slice())
|
|
|
|
|
xtype.Rlist.Set(func_.Rlist.Slice())
|
2015-02-13 14:40:36 -05:00
|
|
|
|
|
|
|
|
// create the function
|
2016-09-16 11:00:54 +10:00
|
|
|
xfunc := nod(ODCLFUNC, nil, nil)
|
2017-03-23 15:26:10 -07:00
|
|
|
xfunc.Func.SetIsHiddenClosure(Curfn != nil)
|
2015-02-13 14:40:36 -05:00
|
|
|
|
2015-05-27 10:42:55 -04:00
|
|
|
xfunc.Func.Nname = newfuncname(closurename(func_))
|
2017-03-06 20:00:54 +02:00
|
|
|
xfunc.Func.Nname.Sym.SetExported(true) // disable export
|
2015-05-27 10:42:55 -04:00
|
|
|
xfunc.Func.Nname.Name.Param.Ntype = xtype
|
|
|
|
|
xfunc.Func.Nname.Name.Defn = xfunc
|
|
|
|
|
declare(xfunc.Func.Nname, PFUNC)
|
|
|
|
|
xfunc.Func.Nname.Name.Funcdepth = func_.Func.Depth
|
2015-05-27 00:44:05 -04:00
|
|
|
xfunc.Func.Depth = func_.Func.Depth
|
2015-03-25 19:33:01 -07:00
|
|
|
xfunc.Func.Endlineno = func_.Func.Endlineno
|
2016-10-13 22:56:38 +03:00
|
|
|
if Ctxt.Flag_dynlink {
|
|
|
|
|
makefuncsym(xfunc.Func.Nname.Sym)
|
|
|
|
|
}
|
2015-02-13 14:40:36 -05:00
|
|
|
|
2016-02-27 14:31:33 -08:00
|
|
|
xfunc.Nbody.Set(func_.Nbody.Slice())
|
2016-02-25 10:35:19 -08:00
|
|
|
xfunc.Func.Dcl = append(func_.Func.Dcl, xfunc.Func.Dcl...)
|
|
|
|
|
func_.Func.Dcl = nil
|
2016-04-24 13:50:26 -07:00
|
|
|
if xfunc.Nbody.Len() == 0 {
|
2015-08-30 23:10:03 +02:00
|
|
|
Fatalf("empty body - won't generate any code")
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
cmd/compile: reduce use of **Node parameters
Escape analysis has a hard time with tree-like
structures (see #13493 and #14858).
This is unlikely to change.
As a result, when invoking a function that accepts
a **Node parameter, we usually allocate a *Node
on the heap. This happens a whole lot.
This CL changes functions from taking a **Node
to acting more like append: It both modifies
the input and returns a replacement for it.
Because of the cascading nature of escape analysis,
in order to get the benefits, I had to modify
almost all such functions. The remaining functions
are in racewalk and the backend. I would be happy
to update them as well in a separate CL.
This CL was created by manually updating the
function signatures and the directly impacted
bits of code. The callsites were then automatically
updated using a bespoke script:
https://gist.github.com/josharian/046b1be7aceae244de39
For ease of reviewing and future understanding,
this CL is also broken down into four CLs,
mailed separately, which show the manual
and the automated changes separately.
They are CLs 20990, 20991, 20992, and 20993.
Passes toolstash -cmp.
name old time/op new time/op delta
Template 335ms ± 5% 324ms ± 5% -3.35% (p=0.000 n=23+24)
Unicode 176ms ± 9% 165ms ± 6% -6.12% (p=0.000 n=23+24)
GoTypes 1.10s ± 4% 1.07s ± 2% -2.77% (p=0.000 n=24+24)
Compiler 5.31s ± 3% 5.15s ± 3% -2.95% (p=0.000 n=24+24)
MakeBash 41.6s ± 1% 41.7s ± 2% ~ (p=0.586 n=23+23)
name old alloc/op new alloc/op delta
Template 63.3MB ± 0% 62.4MB ± 0% -1.36% (p=0.000 n=25+23)
Unicode 42.4MB ± 0% 41.6MB ± 0% -1.99% (p=0.000 n=24+25)
GoTypes 220MB ± 0% 217MB ± 0% -1.11% (p=0.000 n=25+25)
Compiler 994MB ± 0% 973MB ± 0% -2.08% (p=0.000 n=24+25)
name old allocs/op new allocs/op delta
Template 681k ± 0% 574k ± 0% -15.71% (p=0.000 n=24+25)
Unicode 518k ± 0% 413k ± 0% -20.34% (p=0.000 n=25+24)
GoTypes 2.08M ± 0% 1.78M ± 0% -14.62% (p=0.000 n=25+25)
Compiler 9.26M ± 0% 7.64M ± 0% -17.48% (p=0.000 n=25+25)
name old text-bytes new text-bytes delta
HelloSize 578k ± 0% 578k ± 0% ~ (all samples are equal)
CmdGoSize 6.46M ± 0% 6.46M ± 0% ~ (all samples are equal)
name old data-bytes new data-bytes delta
HelloSize 128k ± 0% 128k ± 0% ~ (all samples are equal)
CmdGoSize 281k ± 0% 281k ± 0% ~ (all samples are equal)
name old exe-bytes new exe-bytes delta
HelloSize 921k ± 0% 921k ± 0% ~ (all samples are equal)
CmdGoSize 9.86M ± 0% 9.86M ± 0% ~ (all samples are equal)
Change-Id: I277d95bd56d51c166ef7f560647aeaa092f3f475
Reviewed-on: https://go-review.googlesource.com/20959
Reviewed-by: Dave Cheney <dave@cheney.net>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2016-03-20 08:03:31 -07:00
|
|
|
xfunc = typecheck(xfunc, Etop)
|
2015-02-13 14:40:36 -05:00
|
|
|
|
2015-05-27 00:44:05 -04:00
|
|
|
xfunc.Func.Closure = func_
|
|
|
|
|
func_.Func.Closure = xfunc
|
2015-02-13 14:40:36 -05:00
|
|
|
|
2016-02-27 14:31:33 -08:00
|
|
|
func_.Nbody.Set(nil)
|
2016-03-08 15:10:26 -08:00
|
|
|
func_.List.Set(nil)
|
|
|
|
|
func_.Rlist.Set(nil)
|
2015-02-13 14:40:36 -05:00
|
|
|
|
|
|
|
|
return xfunc
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// capturevars is called in a separate phase after all typechecking is done.
|
|
|
|
|
// It decides whether each variable captured by a closure should be captured
|
|
|
|
|
// by value or by reference.
|
|
|
|
|
// We use value capturing for values <= 128 bytes that are never reassigned
|
|
|
|
|
// after capturing (effectively constant).
|
|
|
|
|
func capturevars(xfunc *Node) {
|
2016-03-02 17:34:42 -08:00
|
|
|
lno := lineno
|
2016-12-07 17:40:46 -08:00
|
|
|
lineno = xfunc.Pos
|
2015-02-13 14:40:36 -05:00
|
|
|
|
2015-05-27 00:44:05 -04:00
|
|
|
func_ := xfunc.Func.Closure
|
2016-02-26 14:28:48 -08:00
|
|
|
func_.Func.Enter.Set(nil)
|
2016-02-26 17:03:58 -08:00
|
|
|
for _, v := range func_.Func.Cvars.Slice() {
|
2015-02-13 14:40:36 -05:00
|
|
|
if v.Type == nil {
|
|
|
|
|
// if v->type is nil, it means v looked like it was
|
|
|
|
|
// going to be used in the closure but wasn't.
|
|
|
|
|
// this happens because when parsing a, b, c := f()
|
|
|
|
|
// the a, b, c gets parsed as references to older
|
|
|
|
|
// a, b, c before the parser figures out this is a
|
|
|
|
|
// declaration.
|
|
|
|
|
v.Op = OXXX
|
|
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// type check the & of closed variables outside the closure,
|
|
|
|
|
// so that the outer frame also grabs them and knows they escape.
|
|
|
|
|
dowidth(v.Type)
|
|
|
|
|
|
2016-05-27 00:56:19 -04:00
|
|
|
outer := v.Name.Param.Outer
|
|
|
|
|
outermost := v.Name.Defn
|
2015-02-13 14:40:36 -05:00
|
|
|
|
|
|
|
|
// out parameters will be assigned to implicitly upon return.
|
2017-02-27 19:56:38 +02:00
|
|
|
if outer.Class != PPARAMOUT && !outermost.Addrtaken() && !outermost.Assigned() && v.Type.Width <= 128 {
|
|
|
|
|
v.Name.SetByval(true)
|
2015-02-13 14:40:36 -05:00
|
|
|
} else {
|
2017-02-27 19:56:38 +02:00
|
|
|
outermost.SetAddrtaken(true)
|
2016-09-16 11:00:54 +10:00
|
|
|
outer = nod(OADDR, outer, nil)
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if Debug['m'] > 1 {
|
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
|
|
|
var name *types.Sym
|
2015-05-27 10:42:55 -04:00
|
|
|
if v.Name.Curfn != nil && v.Name.Curfn.Func.Nname != nil {
|
|
|
|
|
name = v.Name.Curfn.Func.Nname.Sym
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
2015-02-23 16:07:24 -05:00
|
|
|
how := "ref"
|
2017-02-27 19:56:38 +02:00
|
|
|
if v.Name.Byval() {
|
2015-02-13 14:40:36 -05:00
|
|
|
how = "value"
|
|
|
|
|
}
|
2017-02-27 19:56:38 +02:00
|
|
|
Warnl(v.Pos, "%v capturing by %s: %v (addr=%v assign=%v width=%d)", name, how, v.Sym, outermost.Addrtaken(), outermost.Assigned(), int32(v.Type.Width))
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
cmd/compile: reduce use of **Node parameters
Escape analysis has a hard time with tree-like
structures (see #13493 and #14858).
This is unlikely to change.
As a result, when invoking a function that accepts
a **Node parameter, we usually allocate a *Node
on the heap. This happens a whole lot.
This CL changes functions from taking a **Node
to acting more like append: It both modifies
the input and returns a replacement for it.
Because of the cascading nature of escape analysis,
in order to get the benefits, I had to modify
almost all such functions. The remaining functions
are in racewalk and the backend. I would be happy
to update them as well in a separate CL.
This CL was created by manually updating the
function signatures and the directly impacted
bits of code. The callsites were then automatically
updated using a bespoke script:
https://gist.github.com/josharian/046b1be7aceae244de39
For ease of reviewing and future understanding,
this CL is also broken down into four CLs,
mailed separately, which show the manual
and the automated changes separately.
They are CLs 20990, 20991, 20992, and 20993.
Passes toolstash -cmp.
name old time/op new time/op delta
Template 335ms ± 5% 324ms ± 5% -3.35% (p=0.000 n=23+24)
Unicode 176ms ± 9% 165ms ± 6% -6.12% (p=0.000 n=23+24)
GoTypes 1.10s ± 4% 1.07s ± 2% -2.77% (p=0.000 n=24+24)
Compiler 5.31s ± 3% 5.15s ± 3% -2.95% (p=0.000 n=24+24)
MakeBash 41.6s ± 1% 41.7s ± 2% ~ (p=0.586 n=23+23)
name old alloc/op new alloc/op delta
Template 63.3MB ± 0% 62.4MB ± 0% -1.36% (p=0.000 n=25+23)
Unicode 42.4MB ± 0% 41.6MB ± 0% -1.99% (p=0.000 n=24+25)
GoTypes 220MB ± 0% 217MB ± 0% -1.11% (p=0.000 n=25+25)
Compiler 994MB ± 0% 973MB ± 0% -2.08% (p=0.000 n=24+25)
name old allocs/op new allocs/op delta
Template 681k ± 0% 574k ± 0% -15.71% (p=0.000 n=24+25)
Unicode 518k ± 0% 413k ± 0% -20.34% (p=0.000 n=25+24)
GoTypes 2.08M ± 0% 1.78M ± 0% -14.62% (p=0.000 n=25+25)
Compiler 9.26M ± 0% 7.64M ± 0% -17.48% (p=0.000 n=25+25)
name old text-bytes new text-bytes delta
HelloSize 578k ± 0% 578k ± 0% ~ (all samples are equal)
CmdGoSize 6.46M ± 0% 6.46M ± 0% ~ (all samples are equal)
name old data-bytes new data-bytes delta
HelloSize 128k ± 0% 128k ± 0% ~ (all samples are equal)
CmdGoSize 281k ± 0% 281k ± 0% ~ (all samples are equal)
name old exe-bytes new exe-bytes delta
HelloSize 921k ± 0% 921k ± 0% ~ (all samples are equal)
CmdGoSize 9.86M ± 0% 9.86M ± 0% ~ (all samples are equal)
Change-Id: I277d95bd56d51c166ef7f560647aeaa092f3f475
Reviewed-on: https://go-review.googlesource.com/20959
Reviewed-by: Dave Cheney <dave@cheney.net>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2016-03-20 08:03:31 -07:00
|
|
|
outer = typecheck(outer, Erv)
|
2016-02-26 14:28:48 -08:00
|
|
|
func_.Func.Enter.Append(outer)
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
2016-03-02 17:34:42 -08:00
|
|
|
lineno = lno
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// transformclosure is called in a separate phase after escape analysis.
|
|
|
|
|
// It transform closure bodies to properly reference captured variables.
|
|
|
|
|
func transformclosure(xfunc *Node) {
|
2016-03-02 17:34:42 -08:00
|
|
|
lno := lineno
|
2016-12-07 17:40:46 -08:00
|
|
|
lineno = xfunc.Pos
|
2015-05-27 00:44:05 -04:00
|
|
|
func_ := xfunc.Func.Closure
|
2015-02-13 14:40:36 -05:00
|
|
|
|
2015-05-27 00:44:05 -04:00
|
|
|
if func_.Func.Top&Ecall != 0 {
|
2015-02-13 14:40:36 -05:00
|
|
|
// If the closure is directly called, we transform it to a plain function call
|
|
|
|
|
// with variables passed as args. This avoids allocation of a closure object.
|
|
|
|
|
// Here we do only a part of the transformation. Walk of OCALLFUNC(OCLOSURE)
|
|
|
|
|
// will complete the transformation later.
|
|
|
|
|
// For illustration, the following closure:
|
|
|
|
|
// func(a int) {
|
|
|
|
|
// println(byval)
|
|
|
|
|
// byref++
|
|
|
|
|
// }(42)
|
|
|
|
|
// becomes:
|
|
|
|
|
// func(a int, byval int, &byref *int) {
|
|
|
|
|
// println(byval)
|
|
|
|
|
// (*&byref)++
|
2015-07-23 14:17:07 -04:00
|
|
|
// }(byval, &byref, 42)
|
2015-02-13 14:40:36 -05:00
|
|
|
|
|
|
|
|
// f is ONAME of the actual function.
|
2015-05-27 10:42:55 -04:00
|
|
|
f := xfunc.Func.Nname
|
2015-02-13 14:40:36 -05:00
|
|
|
|
2015-07-23 14:17:07 -04:00
|
|
|
// We are going to insert captured variables before input args.
|
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
|
|
|
var params []*types.Field
|
2016-03-10 19:05:45 -08:00
|
|
|
var decls []*Node
|
2016-02-26 17:03:58 -08:00
|
|
|
for _, v := range func_.Func.Cvars.Slice() {
|
2015-02-13 14:40:36 -05:00
|
|
|
if v.Op == OXXX {
|
|
|
|
|
continue
|
|
|
|
|
}
|
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
|
|
|
fld := types.NewField()
|
|
|
|
|
fld.Funarg = types.FunargParams
|
2017-02-27 19:56:38 +02:00
|
|
|
if v.Name.Byval() {
|
2015-02-13 14:40:36 -05:00
|
|
|
// If v is captured by value, we merely downgrade it to PPARAM.
|
|
|
|
|
v.Class = PPARAM
|
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
|
|
|
fld.Nname = asTypesNode(v)
|
2015-02-13 14:40:36 -05:00
|
|
|
} else {
|
|
|
|
|
// If v of type T is captured by reference,
|
|
|
|
|
// we introduce function param &v *T
|
2016-05-25 10:29:50 -04:00
|
|
|
// and v remains PAUTOHEAP with &v heapaddr
|
2015-02-13 14:40:36 -05:00
|
|
|
// (accesses will implicitly deref &v).
|
2017-03-30 15:44:12 -07:00
|
|
|
addr := newname(lookup("&" + v.Sym.Name))
|
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
|
|
|
addr.Type = types.NewPtr(v.Type)
|
2015-02-13 14:40:36 -05:00
|
|
|
addr.Class = PPARAM
|
2016-10-28 11:42:40 -07:00
|
|
|
v.Name.Param.Heapaddr = addr
|
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
|
|
|
fld.Nname = asTypesNode(addr)
|
2015-02-13 14:40:36 -05: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
|
|
|
fld.Type = asNode(fld.Nname).Type
|
|
|
|
|
fld.Sym = asNode(fld.Nname).Sym
|
2015-02-13 14:40:36 -05:00
|
|
|
|
2016-03-10 19:05:45 -08:00
|
|
|
params = append(params, fld)
|
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
|
|
|
decls = append(decls, asNode(fld.Nname))
|
2016-03-10 19:05:45 -08:00
|
|
|
}
|
2015-02-13 14:40:36 -05:00
|
|
|
|
2016-03-10 19:05:45 -08:00
|
|
|
if len(params) > 0 {
|
|
|
|
|
// Prepend params and decls.
|
|
|
|
|
f.Type.Params().SetFields(append(params, f.Type.Params().FieldSlice()...))
|
|
|
|
|
xfunc.Func.Dcl = append(decls, xfunc.Func.Dcl...)
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dowidth(f.Type)
|
|
|
|
|
xfunc.Type = f.Type // update type of ODCLFUNC
|
|
|
|
|
} else {
|
|
|
|
|
// The closure is not called, so it is going to stay as closure.
|
2016-02-26 14:28:48 -08:00
|
|
|
var body []*Node
|
2015-02-23 16:07:24 -05:00
|
|
|
offset := int64(Widthptr)
|
2016-02-26 17:03:58 -08:00
|
|
|
for _, v := range func_.Func.Cvars.Slice() {
|
2015-02-13 14:40:36 -05:00
|
|
|
if v.Op == OXXX {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// cv refers to the field inside of closure OSTRUCTLIT.
|
2016-09-16 11:00:54 +10:00
|
|
|
cv := nod(OCLOSUREVAR, nil, nil)
|
2015-02-13 14:40:36 -05:00
|
|
|
|
|
|
|
|
cv.Type = v.Type
|
2017-02-27 19:56:38 +02:00
|
|
|
if !v.Name.Byval() {
|
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
|
|
|
cv.Type = types.NewPtr(v.Type)
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
offset = Rnd(offset, int64(cv.Type.Align))
|
|
|
|
|
cv.Xoffset = offset
|
|
|
|
|
offset += cv.Type.Width
|
|
|
|
|
|
2017-02-27 19:56:38 +02:00
|
|
|
if v.Name.Byval() && v.Type.Width <= int64(2*Widthptr) {
|
2015-06-29 13:56:27 -04:00
|
|
|
// If it is a small variable captured by value, downgrade it to PAUTO.
|
2015-02-13 14:40:36 -05:00
|
|
|
v.Class = PAUTO
|
2016-02-25 10:35:19 -08:00
|
|
|
xfunc.Func.Dcl = append(xfunc.Func.Dcl, v)
|
2016-09-16 11:00:54 +10:00
|
|
|
body = append(body, nod(OAS, v, cv))
|
2015-02-13 14:40:36 -05:00
|
|
|
} else {
|
|
|
|
|
// Declare variable holding addresses taken from closure
|
|
|
|
|
// and initialize in entry prologue.
|
2017-03-30 15:44:12 -07:00
|
|
|
addr := newname(lookup("&" + v.Sym.Name))
|
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
|
|
|
addr.Type = types.NewPtr(v.Type)
|
2015-02-13 14:40:36 -05:00
|
|
|
addr.Class = PAUTO
|
2017-02-27 19:56:38 +02:00
|
|
|
addr.SetUsed(true)
|
2015-05-27 07:31:56 -04:00
|
|
|
addr.Name.Curfn = xfunc
|
2016-02-25 10:35:19 -08:00
|
|
|
xfunc.Func.Dcl = append(xfunc.Func.Dcl, addr)
|
2016-10-28 11:42:40 -07:00
|
|
|
v.Name.Param.Heapaddr = addr
|
2017-02-27 19:56:38 +02:00
|
|
|
if v.Name.Byval() {
|
2016-09-16 11:00:54 +10:00
|
|
|
cv = nod(OADDR, cv, nil)
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
2016-09-16 11:00:54 +10:00
|
|
|
body = append(body, nod(OAS, addr, cv))
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-26 14:28:48 -08:00
|
|
|
if len(body) > 0 {
|
|
|
|
|
typecheckslice(body, Etop)
|
2016-03-07 22:54:46 -08:00
|
|
|
walkstmtlist(body)
|
2016-02-26 14:28:48 -08:00
|
|
|
xfunc.Func.Enter.Set(body)
|
2017-02-27 19:56:38 +02:00
|
|
|
xfunc.Func.SetNeedctxt(true)
|
2016-02-26 14:28:48 -08:00
|
|
|
}
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
2016-03-02 17:34:42 -08:00
|
|
|
lineno = lno
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
cmd/compile: ignore OXXX nodes in closure captured vars list
Added a debug flag "-d closure" to explain compilation of
closures (should this be done some other way? Should we
rewrite the "-m" flag to "-d escapes"?) Used this to
discover that cause was an OXXX node in the captured vars
list, and in turn noticed that OXXX nodes are explicitly
ignored in all other processing of captured variables.
Couldn't figure out a reproducer, did verify that this OXXX
was not caused by an unnamed return value (which is one use
of these). Verified lack of heap allocation by examining -S
output.
Assembly:
(runtime/mgc.go:1371) PCDATA $0, $2
(runtime/mgc.go:1371) CALL "".notewakeup(SB)
(runtime/mgc.go:1377) LEAQ "".gcBgMarkWorker.func1·f(SB), AX
(runtime/mgc.go:1404) MOVQ AX, (SP)
(runtime/mgc.go:1404) MOVQ "".autotmp_2242+88(SP), CX
(runtime/mgc.go:1404) MOVQ CX, 8(SP)
(runtime/mgc.go:1404) LEAQ go.string."GC worker (idle)"(SB), AX
(runtime/mgc.go:1404) MOVQ AX, 16(SP)
(runtime/mgc.go:1404) MOVQ $16, 24(SP)
(runtime/mgc.go:1404) MOVB $20, 32(SP)
(runtime/mgc.go:1404) MOVQ $0, 40(SP)
(runtime/mgc.go:1404) PCDATA $0, $2
(runtime/mgc.go:1404) CALL "".gopark(SB)
Added a check for compiling_runtime to ensure that this is
caught in the future. Added a test to test the check.
Verified that 1.5.3 did NOT reject the test case when
compiled with -+ flag, so this is not a recently added bug.
Cause of bug is two-part -- there was no leaking closure
detection ever, and instead it relied on capture-of-variables
to trigger compiling_runtime test, but closures improved in
1.5.3 so that mere capture of a value did not also capture
the variable, which thus allowed closures to escape, as well
as this case where the escape was spurious. In
fixedbugs/issue14999.go, compare messages for f and g;
1.5.3 would reject g, but not f. 1.4 rejects both because
1.4 heap-allocates parameter x for both.
Fixes #14999.
Change-Id: I40bcdd27056810628e96763a44f2acddd503aee1
Reviewed-on: https://go-review.googlesource.com/21322
Run-TryBot: David Chase <drchase@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
2016-03-30 14:14:00 -04:00
|
|
|
// hasemptycvars returns true iff closure func_ has an
|
|
|
|
|
// empty list of captured vars. OXXX nodes don't count.
|
|
|
|
|
func hasemptycvars(func_ *Node) bool {
|
|
|
|
|
for _, v := range func_.Func.Cvars.Slice() {
|
|
|
|
|
if v.Op == OXXX {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// closuredebugruntimecheck applies boilerplate checks for debug flags
|
|
|
|
|
// and compiling runtime
|
|
|
|
|
func closuredebugruntimecheck(r *Node) {
|
|
|
|
|
if Debug_closure > 0 {
|
|
|
|
|
if r.Esc == EscHeap {
|
2016-12-07 17:40:46 -08:00
|
|
|
Warnl(r.Pos, "heap closure, captured vars = %v", r.Func.Cvars)
|
cmd/compile: ignore OXXX nodes in closure captured vars list
Added a debug flag "-d closure" to explain compilation of
closures (should this be done some other way? Should we
rewrite the "-m" flag to "-d escapes"?) Used this to
discover that cause was an OXXX node in the captured vars
list, and in turn noticed that OXXX nodes are explicitly
ignored in all other processing of captured variables.
Couldn't figure out a reproducer, did verify that this OXXX
was not caused by an unnamed return value (which is one use
of these). Verified lack of heap allocation by examining -S
output.
Assembly:
(runtime/mgc.go:1371) PCDATA $0, $2
(runtime/mgc.go:1371) CALL "".notewakeup(SB)
(runtime/mgc.go:1377) LEAQ "".gcBgMarkWorker.func1·f(SB), AX
(runtime/mgc.go:1404) MOVQ AX, (SP)
(runtime/mgc.go:1404) MOVQ "".autotmp_2242+88(SP), CX
(runtime/mgc.go:1404) MOVQ CX, 8(SP)
(runtime/mgc.go:1404) LEAQ go.string."GC worker (idle)"(SB), AX
(runtime/mgc.go:1404) MOVQ AX, 16(SP)
(runtime/mgc.go:1404) MOVQ $16, 24(SP)
(runtime/mgc.go:1404) MOVB $20, 32(SP)
(runtime/mgc.go:1404) MOVQ $0, 40(SP)
(runtime/mgc.go:1404) PCDATA $0, $2
(runtime/mgc.go:1404) CALL "".gopark(SB)
Added a check for compiling_runtime to ensure that this is
caught in the future. Added a test to test the check.
Verified that 1.5.3 did NOT reject the test case when
compiled with -+ flag, so this is not a recently added bug.
Cause of bug is two-part -- there was no leaking closure
detection ever, and instead it relied on capture-of-variables
to trigger compiling_runtime test, but closures improved in
1.5.3 so that mere capture of a value did not also capture
the variable, which thus allowed closures to escape, as well
as this case where the escape was spurious. In
fixedbugs/issue14999.go, compare messages for f and g;
1.5.3 would reject g, but not f. 1.4 rejects both because
1.4 heap-allocates parameter x for both.
Fixes #14999.
Change-Id: I40bcdd27056810628e96763a44f2acddd503aee1
Reviewed-on: https://go-review.googlesource.com/21322
Run-TryBot: David Chase <drchase@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
2016-03-30 14:14:00 -04:00
|
|
|
} else {
|
2016-12-07 17:40:46 -08:00
|
|
|
Warnl(r.Pos, "stack closure, captured vars = %v", r.Func.Cvars)
|
cmd/compile: ignore OXXX nodes in closure captured vars list
Added a debug flag "-d closure" to explain compilation of
closures (should this be done some other way? Should we
rewrite the "-m" flag to "-d escapes"?) Used this to
discover that cause was an OXXX node in the captured vars
list, and in turn noticed that OXXX nodes are explicitly
ignored in all other processing of captured variables.
Couldn't figure out a reproducer, did verify that this OXXX
was not caused by an unnamed return value (which is one use
of these). Verified lack of heap allocation by examining -S
output.
Assembly:
(runtime/mgc.go:1371) PCDATA $0, $2
(runtime/mgc.go:1371) CALL "".notewakeup(SB)
(runtime/mgc.go:1377) LEAQ "".gcBgMarkWorker.func1·f(SB), AX
(runtime/mgc.go:1404) MOVQ AX, (SP)
(runtime/mgc.go:1404) MOVQ "".autotmp_2242+88(SP), CX
(runtime/mgc.go:1404) MOVQ CX, 8(SP)
(runtime/mgc.go:1404) LEAQ go.string."GC worker (idle)"(SB), AX
(runtime/mgc.go:1404) MOVQ AX, 16(SP)
(runtime/mgc.go:1404) MOVQ $16, 24(SP)
(runtime/mgc.go:1404) MOVB $20, 32(SP)
(runtime/mgc.go:1404) MOVQ $0, 40(SP)
(runtime/mgc.go:1404) PCDATA $0, $2
(runtime/mgc.go:1404) CALL "".gopark(SB)
Added a check for compiling_runtime to ensure that this is
caught in the future. Added a test to test the check.
Verified that 1.5.3 did NOT reject the test case when
compiled with -+ flag, so this is not a recently added bug.
Cause of bug is two-part -- there was no leaking closure
detection ever, and instead it relied on capture-of-variables
to trigger compiling_runtime test, but closures improved in
1.5.3 so that mere capture of a value did not also capture
the variable, which thus allowed closures to escape, as well
as this case where the escape was spurious. In
fixedbugs/issue14999.go, compare messages for f and g;
1.5.3 would reject g, but not f. 1.4 rejects both because
1.4 heap-allocates parameter x for both.
Fixes #14999.
Change-Id: I40bcdd27056810628e96763a44f2acddd503aee1
Reviewed-on: https://go-review.googlesource.com/21322
Run-TryBot: David Chase <drchase@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
2016-03-30 14:14:00 -04:00
|
|
|
}
|
|
|
|
|
}
|
2016-04-13 18:37:18 -07:00
|
|
|
if compiling_runtime && r.Esc == EscHeap {
|
2016-12-07 17:40:46 -08:00
|
|
|
yyerrorl(r.Pos, "heap-allocated closure, not allowed in runtime.")
|
cmd/compile: ignore OXXX nodes in closure captured vars list
Added a debug flag "-d closure" to explain compilation of
closures (should this be done some other way? Should we
rewrite the "-m" flag to "-d escapes"?) Used this to
discover that cause was an OXXX node in the captured vars
list, and in turn noticed that OXXX nodes are explicitly
ignored in all other processing of captured variables.
Couldn't figure out a reproducer, did verify that this OXXX
was not caused by an unnamed return value (which is one use
of these). Verified lack of heap allocation by examining -S
output.
Assembly:
(runtime/mgc.go:1371) PCDATA $0, $2
(runtime/mgc.go:1371) CALL "".notewakeup(SB)
(runtime/mgc.go:1377) LEAQ "".gcBgMarkWorker.func1·f(SB), AX
(runtime/mgc.go:1404) MOVQ AX, (SP)
(runtime/mgc.go:1404) MOVQ "".autotmp_2242+88(SP), CX
(runtime/mgc.go:1404) MOVQ CX, 8(SP)
(runtime/mgc.go:1404) LEAQ go.string."GC worker (idle)"(SB), AX
(runtime/mgc.go:1404) MOVQ AX, 16(SP)
(runtime/mgc.go:1404) MOVQ $16, 24(SP)
(runtime/mgc.go:1404) MOVB $20, 32(SP)
(runtime/mgc.go:1404) MOVQ $0, 40(SP)
(runtime/mgc.go:1404) PCDATA $0, $2
(runtime/mgc.go:1404) CALL "".gopark(SB)
Added a check for compiling_runtime to ensure that this is
caught in the future. Added a test to test the check.
Verified that 1.5.3 did NOT reject the test case when
compiled with -+ flag, so this is not a recently added bug.
Cause of bug is two-part -- there was no leaking closure
detection ever, and instead it relied on capture-of-variables
to trigger compiling_runtime test, but closures improved in
1.5.3 so that mere capture of a value did not also capture
the variable, which thus allowed closures to escape, as well
as this case where the escape was spurious. In
fixedbugs/issue14999.go, compare messages for f and g;
1.5.3 would reject g, but not f. 1.4 rejects both because
1.4 heap-allocates parameter x for both.
Fixes #14999.
Change-Id: I40bcdd27056810628e96763a44f2acddd503aee1
Reviewed-on: https://go-review.googlesource.com/21322
Run-TryBot: David Chase <drchase@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
2016-03-30 14:14:00 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-07 22:54:46 -08:00
|
|
|
func walkclosure(func_ *Node, init *Nodes) *Node {
|
2015-02-13 14:40:36 -05:00
|
|
|
// If no closure vars, don't bother wrapping.
|
cmd/compile: ignore OXXX nodes in closure captured vars list
Added a debug flag "-d closure" to explain compilation of
closures (should this be done some other way? Should we
rewrite the "-m" flag to "-d escapes"?) Used this to
discover that cause was an OXXX node in the captured vars
list, and in turn noticed that OXXX nodes are explicitly
ignored in all other processing of captured variables.
Couldn't figure out a reproducer, did verify that this OXXX
was not caused by an unnamed return value (which is one use
of these). Verified lack of heap allocation by examining -S
output.
Assembly:
(runtime/mgc.go:1371) PCDATA $0, $2
(runtime/mgc.go:1371) CALL "".notewakeup(SB)
(runtime/mgc.go:1377) LEAQ "".gcBgMarkWorker.func1·f(SB), AX
(runtime/mgc.go:1404) MOVQ AX, (SP)
(runtime/mgc.go:1404) MOVQ "".autotmp_2242+88(SP), CX
(runtime/mgc.go:1404) MOVQ CX, 8(SP)
(runtime/mgc.go:1404) LEAQ go.string."GC worker (idle)"(SB), AX
(runtime/mgc.go:1404) MOVQ AX, 16(SP)
(runtime/mgc.go:1404) MOVQ $16, 24(SP)
(runtime/mgc.go:1404) MOVB $20, 32(SP)
(runtime/mgc.go:1404) MOVQ $0, 40(SP)
(runtime/mgc.go:1404) PCDATA $0, $2
(runtime/mgc.go:1404) CALL "".gopark(SB)
Added a check for compiling_runtime to ensure that this is
caught in the future. Added a test to test the check.
Verified that 1.5.3 did NOT reject the test case when
compiled with -+ flag, so this is not a recently added bug.
Cause of bug is two-part -- there was no leaking closure
detection ever, and instead it relied on capture-of-variables
to trigger compiling_runtime test, but closures improved in
1.5.3 so that mere capture of a value did not also capture
the variable, which thus allowed closures to escape, as well
as this case where the escape was spurious. In
fixedbugs/issue14999.go, compare messages for f and g;
1.5.3 would reject g, but not f. 1.4 rejects both because
1.4 heap-allocates parameter x for both.
Fixes #14999.
Change-Id: I40bcdd27056810628e96763a44f2acddd503aee1
Reviewed-on: https://go-review.googlesource.com/21322
Run-TryBot: David Chase <drchase@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
2016-03-30 14:14:00 -04:00
|
|
|
if hasemptycvars(func_) {
|
|
|
|
|
if Debug_closure > 0 {
|
2016-12-07 17:40:46 -08:00
|
|
|
Warnl(func_.Pos, "closure converted to global")
|
cmd/compile: ignore OXXX nodes in closure captured vars list
Added a debug flag "-d closure" to explain compilation of
closures (should this be done some other way? Should we
rewrite the "-m" flag to "-d escapes"?) Used this to
discover that cause was an OXXX node in the captured vars
list, and in turn noticed that OXXX nodes are explicitly
ignored in all other processing of captured variables.
Couldn't figure out a reproducer, did verify that this OXXX
was not caused by an unnamed return value (which is one use
of these). Verified lack of heap allocation by examining -S
output.
Assembly:
(runtime/mgc.go:1371) PCDATA $0, $2
(runtime/mgc.go:1371) CALL "".notewakeup(SB)
(runtime/mgc.go:1377) LEAQ "".gcBgMarkWorker.func1·f(SB), AX
(runtime/mgc.go:1404) MOVQ AX, (SP)
(runtime/mgc.go:1404) MOVQ "".autotmp_2242+88(SP), CX
(runtime/mgc.go:1404) MOVQ CX, 8(SP)
(runtime/mgc.go:1404) LEAQ go.string."GC worker (idle)"(SB), AX
(runtime/mgc.go:1404) MOVQ AX, 16(SP)
(runtime/mgc.go:1404) MOVQ $16, 24(SP)
(runtime/mgc.go:1404) MOVB $20, 32(SP)
(runtime/mgc.go:1404) MOVQ $0, 40(SP)
(runtime/mgc.go:1404) PCDATA $0, $2
(runtime/mgc.go:1404) CALL "".gopark(SB)
Added a check for compiling_runtime to ensure that this is
caught in the future. Added a test to test the check.
Verified that 1.5.3 did NOT reject the test case when
compiled with -+ flag, so this is not a recently added bug.
Cause of bug is two-part -- there was no leaking closure
detection ever, and instead it relied on capture-of-variables
to trigger compiling_runtime test, but closures improved in
1.5.3 so that mere capture of a value did not also capture
the variable, which thus allowed closures to escape, as well
as this case where the escape was spurious. In
fixedbugs/issue14999.go, compare messages for f and g;
1.5.3 would reject g, but not f. 1.4 rejects both because
1.4 heap-allocates parameter x for both.
Fixes #14999.
Change-Id: I40bcdd27056810628e96763a44f2acddd503aee1
Reviewed-on: https://go-review.googlesource.com/21322
Run-TryBot: David Chase <drchase@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
2016-03-30 14:14:00 -04:00
|
|
|
}
|
2015-05-27 10:42:55 -04:00
|
|
|
return func_.Func.Closure.Func.Nname
|
cmd/compile: ignore OXXX nodes in closure captured vars list
Added a debug flag "-d closure" to explain compilation of
closures (should this be done some other way? Should we
rewrite the "-m" flag to "-d escapes"?) Used this to
discover that cause was an OXXX node in the captured vars
list, and in turn noticed that OXXX nodes are explicitly
ignored in all other processing of captured variables.
Couldn't figure out a reproducer, did verify that this OXXX
was not caused by an unnamed return value (which is one use
of these). Verified lack of heap allocation by examining -S
output.
Assembly:
(runtime/mgc.go:1371) PCDATA $0, $2
(runtime/mgc.go:1371) CALL "".notewakeup(SB)
(runtime/mgc.go:1377) LEAQ "".gcBgMarkWorker.func1·f(SB), AX
(runtime/mgc.go:1404) MOVQ AX, (SP)
(runtime/mgc.go:1404) MOVQ "".autotmp_2242+88(SP), CX
(runtime/mgc.go:1404) MOVQ CX, 8(SP)
(runtime/mgc.go:1404) LEAQ go.string."GC worker (idle)"(SB), AX
(runtime/mgc.go:1404) MOVQ AX, 16(SP)
(runtime/mgc.go:1404) MOVQ $16, 24(SP)
(runtime/mgc.go:1404) MOVB $20, 32(SP)
(runtime/mgc.go:1404) MOVQ $0, 40(SP)
(runtime/mgc.go:1404) PCDATA $0, $2
(runtime/mgc.go:1404) CALL "".gopark(SB)
Added a check for compiling_runtime to ensure that this is
caught in the future. Added a test to test the check.
Verified that 1.5.3 did NOT reject the test case when
compiled with -+ flag, so this is not a recently added bug.
Cause of bug is two-part -- there was no leaking closure
detection ever, and instead it relied on capture-of-variables
to trigger compiling_runtime test, but closures improved in
1.5.3 so that mere capture of a value did not also capture
the variable, which thus allowed closures to escape, as well
as this case where the escape was spurious. In
fixedbugs/issue14999.go, compare messages for f and g;
1.5.3 would reject g, but not f. 1.4 rejects both because
1.4 heap-allocates parameter x for both.
Fixes #14999.
Change-Id: I40bcdd27056810628e96763a44f2acddd503aee1
Reviewed-on: https://go-review.googlesource.com/21322
Run-TryBot: David Chase <drchase@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
2016-03-30 14:14:00 -04:00
|
|
|
} else {
|
|
|
|
|
closuredebugruntimecheck(func_)
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create closure in the form of a composite literal.
|
|
|
|
|
// supposing the closure captures an int i and a string s
|
|
|
|
|
// and has one float64 argument and no results,
|
|
|
|
|
// the generated code looks like:
|
|
|
|
|
//
|
2015-02-20 13:54:45 -05:00
|
|
|
// clos = &struct{.F uintptr; i *int; s *string}{func.1, &i, &s}
|
2015-02-13 14:40:36 -05:00
|
|
|
//
|
|
|
|
|
// The use of the struct provides type information to the garbage
|
|
|
|
|
// collector so that it can walk the closure. We could use (in this case)
|
|
|
|
|
// [3]unsafe.Pointer instead, but that would leave the gc in the dark.
|
|
|
|
|
// The information appears in the binary in the form of type descriptors;
|
|
|
|
|
// the struct is unnamed so that closures in multiple packages with the
|
|
|
|
|
// same struct type can share the descriptor.
|
|
|
|
|
|
2016-09-16 11:00:54 +10:00
|
|
|
typ := nod(OTSTRUCT, nil, nil)
|
2015-02-13 14:40:36 -05: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
|
|
|
typ.List.Set1(namedfield(".F", types.Types[TUINTPTR]))
|
2016-02-26 17:03:58 -08:00
|
|
|
for _, v := range func_.Func.Cvars.Slice() {
|
2015-02-13 14:40:36 -05:00
|
|
|
if v.Op == OXXX {
|
|
|
|
|
continue
|
|
|
|
|
}
|
2016-08-31 09:54:00 +10:00
|
|
|
typ1 := typenod(v.Type)
|
2017-02-27 19:56:38 +02:00
|
|
|
if !v.Name.Byval() {
|
2016-09-16 11:00:54 +10:00
|
|
|
typ1 = nod(OIND, typ1, nil)
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
2016-09-16 11:00:54 +10:00
|
|
|
typ.List.Append(nod(ODCLFIELD, newname(v.Sym), typ1))
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
2016-09-16 11:00:54 +10:00
|
|
|
clos := nod(OCOMPLIT, nil, nod(OIND, typ, nil))
|
2015-02-13 14:40:36 -05:00
|
|
|
clos.Esc = func_.Esc
|
2017-02-27 19:56:38 +02:00
|
|
|
clos.Right.SetImplicit(true)
|
2016-09-16 11:00:54 +10:00
|
|
|
clos.List.Set(append([]*Node{nod(OCFUNC, func_.Func.Closure.Func.Nname, nil)}, func_.Func.Enter.Slice()...))
|
2015-02-13 14:40:36 -05:00
|
|
|
|
|
|
|
|
// Force type conversion from *struct to the func type.
|
2016-09-16 11:00:54 +10:00
|
|
|
clos = nod(OCONVNOP, clos, nil)
|
2015-02-13 14:40:36 -05:00
|
|
|
|
|
|
|
|
clos.Type = func_.Type
|
|
|
|
|
|
cmd/compile: reduce use of **Node parameters
Escape analysis has a hard time with tree-like
structures (see #13493 and #14858).
This is unlikely to change.
As a result, when invoking a function that accepts
a **Node parameter, we usually allocate a *Node
on the heap. This happens a whole lot.
This CL changes functions from taking a **Node
to acting more like append: It both modifies
the input and returns a replacement for it.
Because of the cascading nature of escape analysis,
in order to get the benefits, I had to modify
almost all such functions. The remaining functions
are in racewalk and the backend. I would be happy
to update them as well in a separate CL.
This CL was created by manually updating the
function signatures and the directly impacted
bits of code. The callsites were then automatically
updated using a bespoke script:
https://gist.github.com/josharian/046b1be7aceae244de39
For ease of reviewing and future understanding,
this CL is also broken down into four CLs,
mailed separately, which show the manual
and the automated changes separately.
They are CLs 20990, 20991, 20992, and 20993.
Passes toolstash -cmp.
name old time/op new time/op delta
Template 335ms ± 5% 324ms ± 5% -3.35% (p=0.000 n=23+24)
Unicode 176ms ± 9% 165ms ± 6% -6.12% (p=0.000 n=23+24)
GoTypes 1.10s ± 4% 1.07s ± 2% -2.77% (p=0.000 n=24+24)
Compiler 5.31s ± 3% 5.15s ± 3% -2.95% (p=0.000 n=24+24)
MakeBash 41.6s ± 1% 41.7s ± 2% ~ (p=0.586 n=23+23)
name old alloc/op new alloc/op delta
Template 63.3MB ± 0% 62.4MB ± 0% -1.36% (p=0.000 n=25+23)
Unicode 42.4MB ± 0% 41.6MB ± 0% -1.99% (p=0.000 n=24+25)
GoTypes 220MB ± 0% 217MB ± 0% -1.11% (p=0.000 n=25+25)
Compiler 994MB ± 0% 973MB ± 0% -2.08% (p=0.000 n=24+25)
name old allocs/op new allocs/op delta
Template 681k ± 0% 574k ± 0% -15.71% (p=0.000 n=24+25)
Unicode 518k ± 0% 413k ± 0% -20.34% (p=0.000 n=25+24)
GoTypes 2.08M ± 0% 1.78M ± 0% -14.62% (p=0.000 n=25+25)
Compiler 9.26M ± 0% 7.64M ± 0% -17.48% (p=0.000 n=25+25)
name old text-bytes new text-bytes delta
HelloSize 578k ± 0% 578k ± 0% ~ (all samples are equal)
CmdGoSize 6.46M ± 0% 6.46M ± 0% ~ (all samples are equal)
name old data-bytes new data-bytes delta
HelloSize 128k ± 0% 128k ± 0% ~ (all samples are equal)
CmdGoSize 281k ± 0% 281k ± 0% ~ (all samples are equal)
name old exe-bytes new exe-bytes delta
HelloSize 921k ± 0% 921k ± 0% ~ (all samples are equal)
CmdGoSize 9.86M ± 0% 9.86M ± 0% ~ (all samples are equal)
Change-Id: I277d95bd56d51c166ef7f560647aeaa092f3f475
Reviewed-on: https://go-review.googlesource.com/20959
Reviewed-by: Dave Cheney <dave@cheney.net>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2016-03-20 08:03:31 -07:00
|
|
|
clos = typecheck(clos, Erv)
|
2015-02-13 14:40:36 -05:00
|
|
|
|
|
|
|
|
// typecheck will insert a PTRLIT node under CONVNOP,
|
|
|
|
|
// tag it with escape analysis result.
|
|
|
|
|
clos.Left.Esc = func_.Esc
|
|
|
|
|
|
|
|
|
|
// non-escaping temp to use, if any.
|
|
|
|
|
// orderexpr did not compute the type; fill it in now.
|
2015-05-26 23:05:35 -04:00
|
|
|
if x := prealloc[func_]; x != nil {
|
|
|
|
|
x.Type = clos.Left.Left.Type
|
|
|
|
|
x.Orig.Type = x.Type
|
|
|
|
|
clos.Left.Right = x
|
|
|
|
|
delete(prealloc, func_)
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
2016-03-23 08:51:38 -07:00
|
|
|
return walkexpr(clos, init)
|
2015-02-13 14:40:36 -05: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 typecheckpartialcall(fn *Node, sym *types.Sym) {
|
2015-02-13 14:40:36 -05:00
|
|
|
switch fn.Op {
|
2015-04-01 09:38:44 -07:00
|
|
|
case ODOTINTER, ODOTMETH:
|
2015-02-13 14:40:36 -05:00
|
|
|
break
|
|
|
|
|
|
|
|
|
|
default:
|
2015-08-30 23:10:03 +02:00
|
|
|
Fatalf("invalid typecheckpartialcall")
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create top-level function.
|
2015-05-27 10:42:55 -04:00
|
|
|
xfunc := makepartialcall(fn, fn.Type, sym)
|
|
|
|
|
fn.Func = xfunc.Func
|
cmd/compile: change ODOT and friends to use Sym, not Right
The Node type ODOT and its variants all represent a selector, with a
simple name to the right of the dot. Before this change this was
represented by using an ONAME Node in the Right field. This ONAME node
served no useful purpose. This CL changes these Node types to store the
symbol in the Sym field instead, thus not requiring allocating a Node
for each selector.
When compiling x/tools/go/types this CL eliminates nearly 5000 calls to
newname and reduces the total number of Nodes allocated by about 6.6%.
It seems to cut compilation time by 1 to 2 percent.
Getting this right was somewhat subtle, and I added two dubious changes
to produce the exact same output as before. One is to ishairy in
inl.go: the ONAME node increased the cost of ODOT and friends by 1, and
I retained that, although really ODOT is not more expensive than any
other node. The other is to varexpr in walk.go: because the ONAME in
the Right field of an ODOT has no class, varexpr would always return
false for an ODOT, although in fact for some ODOT's it seemingly ought
to return true; I added an && false for now. I will send separate CLs,
that will break toolstash -cmp, to clean these up.
This CL passes toolstash -cmp.
Change-Id: I4af8a10cc59078c436130ce472f25abc3a9b2f80
Reviewed-on: https://go-review.googlesource.com/20890
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2016-03-18 16:52:30 -07:00
|
|
|
fn.Right = newname(sym)
|
2015-02-13 14:40:36 -05:00
|
|
|
fn.Op = OCALLPART
|
2015-05-27 10:42:55 -04:00
|
|
|
fn.Type = xfunc.Type
|
2015-02-13 14:40:36 -05: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
|
|
|
var makepartialcall_gopkg *types.Pkg
|
2015-02-13 14:40:36 -05: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 makepartialcall(fn *Node, t0 *types.Type, meth *types.Sym) *Node {
|
2015-02-13 14:40:36 -05:00
|
|
|
var p string
|
|
|
|
|
|
2015-02-23 16:07:24 -05:00
|
|
|
rcvrtype := fn.Left.Type
|
cmd/compile: change ODOT and friends to use Sym, not Right
The Node type ODOT and its variants all represent a selector, with a
simple name to the right of the dot. Before this change this was
represented by using an ONAME Node in the Right field. This ONAME node
served no useful purpose. This CL changes these Node types to store the
symbol in the Sym field instead, thus not requiring allocating a Node
for each selector.
When compiling x/tools/go/types this CL eliminates nearly 5000 calls to
newname and reduces the total number of Nodes allocated by about 6.6%.
It seems to cut compilation time by 1 to 2 percent.
Getting this right was somewhat subtle, and I added two dubious changes
to produce the exact same output as before. One is to ishairy in
inl.go: the ONAME node increased the cost of ODOT and friends by 1, and
I retained that, although really ODOT is not more expensive than any
other node. The other is to varexpr in walk.go: because the ONAME in
the Right field of an ODOT has no class, varexpr would always return
false for an ODOT, although in fact for some ODOT's it seemingly ought
to return true; I added an && false for now. I will send separate CLs,
that will break toolstash -cmp, to clean these up.
This CL passes toolstash -cmp.
Change-Id: I4af8a10cc59078c436130ce472f25abc3a9b2f80
Reviewed-on: https://go-review.googlesource.com/20890
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2016-03-18 16:52:30 -07:00
|
|
|
if exportname(meth.Name) {
|
2016-09-09 21:08:46 -07:00
|
|
|
p = fmt.Sprintf("(%-S).%s-fm", rcvrtype, meth.Name)
|
2015-02-13 14:40:36 -05:00
|
|
|
} else {
|
2016-09-09 21:08:46 -07:00
|
|
|
p = fmt.Sprintf("(%-S).(%-v)-fm", rcvrtype, meth)
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
2015-02-23 16:07:24 -05:00
|
|
|
basetype := rcvrtype
|
2016-03-30 15:09:25 -07:00
|
|
|
if rcvrtype.IsPtr() {
|
2016-03-30 10:57:47 -07:00
|
|
|
basetype = basetype.Elem()
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
2016-03-30 14:56:08 -07:00
|
|
|
if !basetype.IsInterface() && basetype.Sym == nil {
|
2015-08-30 23:10:03 +02:00
|
|
|
Fatalf("missing base type for %v", rcvrtype)
|
2015-02-13 14:40:36 -05: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
|
|
|
var spkg *types.Pkg
|
2015-02-13 14:40:36 -05:00
|
|
|
if basetype.Sym != nil {
|
|
|
|
|
spkg = basetype.Sym.Pkg
|
|
|
|
|
}
|
|
|
|
|
if spkg == nil {
|
|
|
|
|
if makepartialcall_gopkg == nil {
|
2017-04-19 10:03:35 -07:00
|
|
|
makepartialcall_gopkg = types.NewPkg("go")
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
spkg = makepartialcall_gopkg
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-30 13:19:18 -07:00
|
|
|
sym := spkg.Lookup(p)
|
2015-02-13 14:40:36 -05:00
|
|
|
|
2017-03-06 20:00:54 +02:00
|
|
|
if sym.Uniq() {
|
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
|
|
|
return asNode(sym.Def)
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
2017-03-06 20:00:54 +02:00
|
|
|
sym.SetUniq(true)
|
2015-02-13 14:40:36 -05:00
|
|
|
|
2015-02-23 16:07:24 -05:00
|
|
|
savecurfn := Curfn
|
2015-02-13 14:40:36 -05:00
|
|
|
Curfn = nil
|
|
|
|
|
|
2016-09-16 11:00:54 +10:00
|
|
|
xtype := nod(OTFUNC, nil, nil)
|
2016-03-07 09:36:24 -08:00
|
|
|
var l []*Node
|
|
|
|
|
var callargs []*Node
|
2015-03-09 16:24:07 +11:00
|
|
|
ddd := false
|
2016-09-16 11:00:54 +10:00
|
|
|
xfunc := nod(ODCLFUNC, nil, nil)
|
2015-02-13 14:40:36 -05:00
|
|
|
Curfn = xfunc
|
2016-08-31 09:54:00 +10:00
|
|
|
for i, t := range t0.Params().Fields().Slice() {
|
2016-09-15 15:45:10 +10:00
|
|
|
n := newname(lookupN("a", i))
|
2015-02-13 14:40:36 -05:00
|
|
|
n.Class = PPARAM
|
2016-02-25 10:35:19 -08:00
|
|
|
xfunc.Func.Dcl = append(xfunc.Func.Dcl, n)
|
2016-03-07 09:36:24 -08:00
|
|
|
callargs = append(callargs, n)
|
2016-09-16 11:00:54 +10:00
|
|
|
fld := nod(ODCLFIELD, n, typenod(t.Type))
|
2017-02-27 19:56:38 +02:00
|
|
|
if t.Isddd() {
|
|
|
|
|
fld.SetIsddd(true)
|
2015-03-09 16:24:07 +11:00
|
|
|
ddd = true
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
2016-03-07 09:36:24 -08:00
|
|
|
l = append(l, fld)
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
2016-03-08 15:10:26 -08:00
|
|
|
xtype.List.Set(l)
|
2015-02-13 14:40:36 -05:00
|
|
|
l = nil
|
2016-03-07 09:36:24 -08:00
|
|
|
var retargs []*Node
|
2016-08-31 09:54:00 +10:00
|
|
|
for i, t := range t0.Results().Fields().Slice() {
|
2016-09-15 15:45:10 +10:00
|
|
|
n := newname(lookupN("r", i))
|
2015-02-13 14:40:36 -05:00
|
|
|
n.Class = PPARAMOUT
|
2016-02-25 10:35:19 -08:00
|
|
|
xfunc.Func.Dcl = append(xfunc.Func.Dcl, n)
|
2016-03-07 09:36:24 -08:00
|
|
|
retargs = append(retargs, n)
|
2016-09-16 11:00:54 +10:00
|
|
|
l = append(l, nod(ODCLFIELD, n, typenod(t.Type)))
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
2016-03-08 15:10:26 -08:00
|
|
|
xtype.Rlist.Set(l)
|
2015-02-13 14:40:36 -05:00
|
|
|
|
2017-02-27 19:56:38 +02:00
|
|
|
xfunc.Func.SetDupok(true)
|
2015-05-27 10:42:55 -04:00
|
|
|
xfunc.Func.Nname = newfuncname(sym)
|
2017-03-06 20:00:54 +02:00
|
|
|
xfunc.Func.Nname.Sym.SetExported(true) // disable export
|
2015-05-27 10:42:55 -04:00
|
|
|
xfunc.Func.Nname.Name.Param.Ntype = xtype
|
|
|
|
|
xfunc.Func.Nname.Name.Defn = xfunc
|
|
|
|
|
declare(xfunc.Func.Nname, PFUNC)
|
2015-02-13 14:40:36 -05:00
|
|
|
|
|
|
|
|
// Declare and initialize variable holding receiver.
|
|
|
|
|
|
2017-02-27 19:56:38 +02:00
|
|
|
xfunc.Func.SetNeedctxt(true)
|
2016-09-16 11:00:54 +10:00
|
|
|
cv := nod(OCLOSUREVAR, nil, nil)
|
2015-02-13 14:40:36 -05:00
|
|
|
cv.Xoffset = int64(Widthptr)
|
|
|
|
|
cv.Type = rcvrtype
|
|
|
|
|
if int(cv.Type.Align) > Widthptr {
|
|
|
|
|
cv.Xoffset = int64(cv.Type.Align)
|
|
|
|
|
}
|
2017-03-24 15:57:12 -07:00
|
|
|
ptr := newname(lookup("rcvr"))
|
2015-02-13 14:40:36 -05:00
|
|
|
ptr.Class = PAUTO
|
2017-02-27 19:56:38 +02:00
|
|
|
ptr.SetUsed(true)
|
2015-05-27 07:31:56 -04:00
|
|
|
ptr.Name.Curfn = xfunc
|
2016-02-25 10:35:19 -08:00
|
|
|
xfunc.Func.Dcl = append(xfunc.Func.Dcl, ptr)
|
2016-02-27 14:31:33 -08:00
|
|
|
var body []*Node
|
2016-03-30 15:09:25 -07:00
|
|
|
if rcvrtype.IsPtr() || rcvrtype.IsInterface() {
|
2017-04-05 18:52:33 -07:00
|
|
|
ptr.Type = rcvrtype
|
2016-09-16 11:00:54 +10:00
|
|
|
body = append(body, nod(OAS, ptr, cv))
|
2015-02-13 14:40:36 -05:00
|
|
|
} else {
|
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
|
|
|
ptr.Type = types.NewPtr(rcvrtype)
|
2016-09-16 11:00:54 +10:00
|
|
|
body = append(body, nod(OAS, ptr, nod(OADDR, cv, nil)))
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
2016-09-16 11:00:54 +10:00
|
|
|
call := nod(OCALL, nodSym(OXDOT, ptr, meth), nil)
|
2016-03-08 15:10:26 -08:00
|
|
|
call.List.Set(callargs)
|
2017-02-27 19:56:38 +02:00
|
|
|
call.SetIsddd(ddd)
|
2016-03-17 01:47:16 -07:00
|
|
|
if t0.Results().NumFields() == 0 {
|
2016-02-27 14:31:33 -08:00
|
|
|
body = append(body, call)
|
2015-02-13 14:40:36 -05:00
|
|
|
} else {
|
2016-09-16 11:00:54 +10:00
|
|
|
n := nod(OAS2, nil, nil)
|
2016-03-08 15:10:26 -08:00
|
|
|
n.List.Set(retargs)
|
2016-03-10 10:13:42 -08:00
|
|
|
n.Rlist.Set1(call)
|
2016-02-27 14:31:33 -08:00
|
|
|
body = append(body, n)
|
2016-09-16 11:00:54 +10:00
|
|
|
n = nod(ORETURN, nil, nil)
|
2016-02-27 14:31:33 -08:00
|
|
|
body = append(body, n)
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
2016-02-27 14:31:33 -08:00
|
|
|
xfunc.Nbody.Set(body)
|
2015-02-13 14:40:36 -05:00
|
|
|
|
cmd/compile: reduce use of **Node parameters
Escape analysis has a hard time with tree-like
structures (see #13493 and #14858).
This is unlikely to change.
As a result, when invoking a function that accepts
a **Node parameter, we usually allocate a *Node
on the heap. This happens a whole lot.
This CL changes functions from taking a **Node
to acting more like append: It both modifies
the input and returns a replacement for it.
Because of the cascading nature of escape analysis,
in order to get the benefits, I had to modify
almost all such functions. The remaining functions
are in racewalk and the backend. I would be happy
to update them as well in a separate CL.
This CL was created by manually updating the
function signatures and the directly impacted
bits of code. The callsites were then automatically
updated using a bespoke script:
https://gist.github.com/josharian/046b1be7aceae244de39
For ease of reviewing and future understanding,
this CL is also broken down into four CLs,
mailed separately, which show the manual
and the automated changes separately.
They are CLs 20990, 20991, 20992, and 20993.
Passes toolstash -cmp.
name old time/op new time/op delta
Template 335ms ± 5% 324ms ± 5% -3.35% (p=0.000 n=23+24)
Unicode 176ms ± 9% 165ms ± 6% -6.12% (p=0.000 n=23+24)
GoTypes 1.10s ± 4% 1.07s ± 2% -2.77% (p=0.000 n=24+24)
Compiler 5.31s ± 3% 5.15s ± 3% -2.95% (p=0.000 n=24+24)
MakeBash 41.6s ± 1% 41.7s ± 2% ~ (p=0.586 n=23+23)
name old alloc/op new alloc/op delta
Template 63.3MB ± 0% 62.4MB ± 0% -1.36% (p=0.000 n=25+23)
Unicode 42.4MB ± 0% 41.6MB ± 0% -1.99% (p=0.000 n=24+25)
GoTypes 220MB ± 0% 217MB ± 0% -1.11% (p=0.000 n=25+25)
Compiler 994MB ± 0% 973MB ± 0% -2.08% (p=0.000 n=24+25)
name old allocs/op new allocs/op delta
Template 681k ± 0% 574k ± 0% -15.71% (p=0.000 n=24+25)
Unicode 518k ± 0% 413k ± 0% -20.34% (p=0.000 n=25+24)
GoTypes 2.08M ± 0% 1.78M ± 0% -14.62% (p=0.000 n=25+25)
Compiler 9.26M ± 0% 7.64M ± 0% -17.48% (p=0.000 n=25+25)
name old text-bytes new text-bytes delta
HelloSize 578k ± 0% 578k ± 0% ~ (all samples are equal)
CmdGoSize 6.46M ± 0% 6.46M ± 0% ~ (all samples are equal)
name old data-bytes new data-bytes delta
HelloSize 128k ± 0% 128k ± 0% ~ (all samples are equal)
CmdGoSize 281k ± 0% 281k ± 0% ~ (all samples are equal)
name old exe-bytes new exe-bytes delta
HelloSize 921k ± 0% 921k ± 0% ~ (all samples are equal)
CmdGoSize 9.86M ± 0% 9.86M ± 0% ~ (all samples are equal)
Change-Id: I277d95bd56d51c166ef7f560647aeaa092f3f475
Reviewed-on: https://go-review.googlesource.com/20959
Reviewed-by: Dave Cheney <dave@cheney.net>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2016-03-20 08:03:31 -07:00
|
|
|
xfunc = typecheck(xfunc, Etop)
|
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
|
|
|
sym.Def = asTypesNode(xfunc)
|
2016-03-09 20:29:21 -08:00
|
|
|
xtop = append(xtop, xfunc)
|
2015-02-13 14:40:36 -05:00
|
|
|
Curfn = savecurfn
|
|
|
|
|
|
|
|
|
|
return xfunc
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-07 22:54:46 -08:00
|
|
|
func walkpartialcall(n *Node, init *Nodes) *Node {
|
2015-02-13 14:40:36 -05:00
|
|
|
// Create closure in the form of a composite literal.
|
|
|
|
|
// For x.M with receiver (x) type T, the generated code looks like:
|
|
|
|
|
//
|
|
|
|
|
// clos = &struct{F uintptr; R T}{M.T·f, x}
|
|
|
|
|
//
|
|
|
|
|
// Like walkclosure above.
|
|
|
|
|
|
2016-03-30 14:45:47 -07:00
|
|
|
if n.Left.Type.IsInterface() {
|
2015-02-13 14:40:36 -05:00
|
|
|
// Trigger panic for method on nil interface now.
|
|
|
|
|
// Otherwise it happens in the wrapper and is confusing.
|
|
|
|
|
n.Left = cheapexpr(n.Left, init)
|
|
|
|
|
|
|
|
|
|
checknil(n.Left, init)
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-16 11:00:54 +10:00
|
|
|
typ := nod(OTSTRUCT, nil, nil)
|
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
|
|
|
typ.List.Set1(namedfield("F", types.Types[TUINTPTR]))
|
2017-03-28 15:13:19 -07:00
|
|
|
typ.List.Append(namedfield("R", n.Left.Type))
|
2015-02-13 14:40:36 -05:00
|
|
|
|
2016-09-16 11:00:54 +10:00
|
|
|
clos := nod(OCOMPLIT, nil, nod(OIND, typ, nil))
|
2015-02-13 14:40:36 -05:00
|
|
|
clos.Esc = n.Esc
|
2017-02-27 19:56:38 +02:00
|
|
|
clos.Right.SetImplicit(true)
|
2016-09-16 11:00:54 +10:00
|
|
|
clos.List.Set1(nod(OCFUNC, n.Func.Nname, nil))
|
2016-03-08 15:10:26 -08:00
|
|
|
clos.List.Append(n.Left)
|
2015-02-13 14:40:36 -05:00
|
|
|
|
|
|
|
|
// Force type conversion from *struct to the func type.
|
2016-09-16 11:00:54 +10:00
|
|
|
clos = nod(OCONVNOP, clos, nil)
|
2015-02-13 14:40:36 -05:00
|
|
|
|
|
|
|
|
clos.Type = n.Type
|
|
|
|
|
|
cmd/compile: reduce use of **Node parameters
Escape analysis has a hard time with tree-like
structures (see #13493 and #14858).
This is unlikely to change.
As a result, when invoking a function that accepts
a **Node parameter, we usually allocate a *Node
on the heap. This happens a whole lot.
This CL changes functions from taking a **Node
to acting more like append: It both modifies
the input and returns a replacement for it.
Because of the cascading nature of escape analysis,
in order to get the benefits, I had to modify
almost all such functions. The remaining functions
are in racewalk and the backend. I would be happy
to update them as well in a separate CL.
This CL was created by manually updating the
function signatures and the directly impacted
bits of code. The callsites were then automatically
updated using a bespoke script:
https://gist.github.com/josharian/046b1be7aceae244de39
For ease of reviewing and future understanding,
this CL is also broken down into four CLs,
mailed separately, which show the manual
and the automated changes separately.
They are CLs 20990, 20991, 20992, and 20993.
Passes toolstash -cmp.
name old time/op new time/op delta
Template 335ms ± 5% 324ms ± 5% -3.35% (p=0.000 n=23+24)
Unicode 176ms ± 9% 165ms ± 6% -6.12% (p=0.000 n=23+24)
GoTypes 1.10s ± 4% 1.07s ± 2% -2.77% (p=0.000 n=24+24)
Compiler 5.31s ± 3% 5.15s ± 3% -2.95% (p=0.000 n=24+24)
MakeBash 41.6s ± 1% 41.7s ± 2% ~ (p=0.586 n=23+23)
name old alloc/op new alloc/op delta
Template 63.3MB ± 0% 62.4MB ± 0% -1.36% (p=0.000 n=25+23)
Unicode 42.4MB ± 0% 41.6MB ± 0% -1.99% (p=0.000 n=24+25)
GoTypes 220MB ± 0% 217MB ± 0% -1.11% (p=0.000 n=25+25)
Compiler 994MB ± 0% 973MB ± 0% -2.08% (p=0.000 n=24+25)
name old allocs/op new allocs/op delta
Template 681k ± 0% 574k ± 0% -15.71% (p=0.000 n=24+25)
Unicode 518k ± 0% 413k ± 0% -20.34% (p=0.000 n=25+24)
GoTypes 2.08M ± 0% 1.78M ± 0% -14.62% (p=0.000 n=25+25)
Compiler 9.26M ± 0% 7.64M ± 0% -17.48% (p=0.000 n=25+25)
name old text-bytes new text-bytes delta
HelloSize 578k ± 0% 578k ± 0% ~ (all samples are equal)
CmdGoSize 6.46M ± 0% 6.46M ± 0% ~ (all samples are equal)
name old data-bytes new data-bytes delta
HelloSize 128k ± 0% 128k ± 0% ~ (all samples are equal)
CmdGoSize 281k ± 0% 281k ± 0% ~ (all samples are equal)
name old exe-bytes new exe-bytes delta
HelloSize 921k ± 0% 921k ± 0% ~ (all samples are equal)
CmdGoSize 9.86M ± 0% 9.86M ± 0% ~ (all samples are equal)
Change-Id: I277d95bd56d51c166ef7f560647aeaa092f3f475
Reviewed-on: https://go-review.googlesource.com/20959
Reviewed-by: Dave Cheney <dave@cheney.net>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2016-03-20 08:03:31 -07:00
|
|
|
clos = typecheck(clos, Erv)
|
2015-02-13 14:40:36 -05:00
|
|
|
|
|
|
|
|
// typecheck will insert a PTRLIT node under CONVNOP,
|
|
|
|
|
// tag it with escape analysis result.
|
|
|
|
|
clos.Left.Esc = n.Esc
|
|
|
|
|
|
|
|
|
|
// non-escaping temp to use, if any.
|
|
|
|
|
// orderexpr did not compute the type; fill it in now.
|
2015-05-26 23:05:35 -04:00
|
|
|
if x := prealloc[n]; x != nil {
|
|
|
|
|
x.Type = clos.Left.Left.Type
|
|
|
|
|
x.Orig.Type = x.Type
|
|
|
|
|
clos.Left.Right = x
|
|
|
|
|
delete(prealloc, n)
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
2016-03-23 08:51:38 -07:00
|
|
|
return walkexpr(clos, init)
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|