2015-03-03 13:38:14 -08:00
|
|
|
// Copyright 2015 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 ssa
|
|
|
|
|
|
2015-08-24 02:16:19 -07:00
|
|
|
import (
|
2021-08-26 14:16:24 -07:00
|
|
|
"testing"
|
|
|
|
|
|
2020-11-17 21:47:56 -05:00
|
|
|
"cmd/compile/internal/ir"
|
2021-08-26 14:16:24 -07:00
|
|
|
"cmd/compile/internal/typecheck"
|
cmd/compile: change ssa.Type into *types.Type
When package ssa was created, Type was in package gc.
To avoid circular dependencies, we used an interface (ssa.Type)
to represent type information in SSA.
In the Go 1.9 cycle, gri extricated the Type type from package gc.
As a result, we can now use it in package ssa.
Now, instead of package types depending on package ssa,
it is the other way.
This is a more sensible dependency tree,
and helps compiler performance a bit.
Though this is a big CL, most of the changes are
mechanical and uninteresting.
Interesting bits:
* Add new singleton globals to package types for the special
SSA types Memory, Void, Invalid, Flags, and Int128.
* Add two new Types, TSSA for the special types,
and TTUPLE, for SSA tuple types.
ssa.MakeTuple is now types.NewTuple.
* Move type comparison result constants CMPlt, CMPeq, and CMPgt
to package types.
* We had picked the name "types" in our rules for the handy
list of types provided by ssa.Config. That conflicted with
the types package name, so change it to "typ".
* Update the type comparison routine to handle tuples and special
types inline.
* Teach gc/fmt.go how to print special types.
* We can now eliminate ElemTypes in favor of just Elem,
and probably also some other duplicated Type methods
designed to return ssa.Type instead of *types.Type.
* The ssa tests were using their own dummy types,
and they were not particularly careful about types in general.
Of necessity, this CL switches them to use *types.Type;
it does not make them more type-accurate.
Unfortunately, using types.Type means initializing a bit
of the types universe.
This is prime for refactoring and improvement.
This shrinks ssa.Value; it now fits in a smaller size class
on 64 bit systems. This doesn't have a giant impact,
though, since most Values are preallocated in a chunk.
name old alloc/op new alloc/op delta
Template 37.9MB ± 0% 37.7MB ± 0% -0.57% (p=0.000 n=10+8)
Unicode 28.9MB ± 0% 28.7MB ± 0% -0.52% (p=0.000 n=10+10)
GoTypes 110MB ± 0% 109MB ± 0% -0.88% (p=0.000 n=10+10)
Flate 24.7MB ± 0% 24.6MB ± 0% -0.66% (p=0.000 n=10+10)
GoParser 31.1MB ± 0% 30.9MB ± 0% -0.61% (p=0.000 n=10+9)
Reflect 73.9MB ± 0% 73.4MB ± 0% -0.62% (p=0.000 n=10+8)
Tar 25.8MB ± 0% 25.6MB ± 0% -0.77% (p=0.000 n=9+10)
XML 41.2MB ± 0% 40.9MB ± 0% -0.80% (p=0.000 n=10+10)
[Geo mean] 40.5MB 40.3MB -0.68%
name old allocs/op new allocs/op delta
Template 385k ± 0% 386k ± 0% ~ (p=0.356 n=10+9)
Unicode 343k ± 1% 344k ± 0% ~ (p=0.481 n=10+10)
GoTypes 1.16M ± 0% 1.16M ± 0% -0.16% (p=0.004 n=10+10)
Flate 238k ± 1% 238k ± 1% ~ (p=0.853 n=10+10)
GoParser 320k ± 0% 320k ± 0% ~ (p=0.720 n=10+9)
Reflect 957k ± 0% 957k ± 0% ~ (p=0.460 n=10+8)
Tar 252k ± 0% 252k ± 0% ~ (p=0.133 n=9+10)
XML 400k ± 0% 400k ± 0% ~ (p=0.796 n=10+10)
[Geo mean] 428k 428k -0.01%
Removing all the interface calls helps non-trivially with CPU, though.
name old time/op new time/op delta
Template 178ms ± 4% 173ms ± 3% -2.90% (p=0.000 n=94+96)
Unicode 85.0ms ± 4% 83.9ms ± 4% -1.23% (p=0.000 n=96+96)
GoTypes 543ms ± 3% 528ms ± 3% -2.73% (p=0.000 n=98+96)
Flate 116ms ± 3% 113ms ± 4% -2.34% (p=0.000 n=96+99)
GoParser 144ms ± 3% 140ms ± 4% -2.80% (p=0.000 n=99+97)
Reflect 344ms ± 3% 334ms ± 4% -3.02% (p=0.000 n=100+99)
Tar 106ms ± 5% 103ms ± 4% -3.30% (p=0.000 n=98+94)
XML 198ms ± 5% 192ms ± 4% -2.88% (p=0.000 n=92+95)
[Geo mean] 178ms 173ms -2.65%
name old user-time/op new user-time/op delta
Template 229ms ± 5% 224ms ± 5% -2.36% (p=0.000 n=95+99)
Unicode 107ms ± 6% 106ms ± 5% -1.13% (p=0.001 n=93+95)
GoTypes 696ms ± 4% 679ms ± 4% -2.45% (p=0.000 n=97+99)
Flate 137ms ± 4% 134ms ± 5% -2.66% (p=0.000 n=99+96)
GoParser 176ms ± 5% 172ms ± 8% -2.27% (p=0.000 n=98+100)
Reflect 430ms ± 6% 411ms ± 5% -4.46% (p=0.000 n=100+92)
Tar 128ms ±13% 123ms ±13% -4.21% (p=0.000 n=100+100)
XML 239ms ± 6% 233ms ± 6% -2.50% (p=0.000 n=95+97)
[Geo mean] 220ms 213ms -2.76%
Change-Id: I15c7d6268347f8358e75066dfdbd77db24e8d0c1
Reviewed-on: https://go-review.googlesource.com/42145
Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
2017-04-28 14:12:28 -07:00
|
|
|
"cmd/compile/internal/types"
|
2015-10-23 12:34:03 -04:00
|
|
|
"cmd/internal/obj"
|
2018-03-05 20:59:40 +01:00
|
|
|
"cmd/internal/obj/arm64"
|
2017-02-21 15:20:38 -05:00
|
|
|
"cmd/internal/obj/s390x"
|
2016-12-03 19:17:16 -05:00
|
|
|
"cmd/internal/obj/x86"
|
2016-12-06 17:08:06 -08:00
|
|
|
"cmd/internal/src"
|
2015-08-24 02:16:19 -07:00
|
|
|
)
|
2015-06-12 11:01:13 -07:00
|
|
|
|
2015-03-03 13:38:14 -08:00
|
|
|
var CheckFunc = checkFunc
|
2015-05-28 16:45:33 -07:00
|
|
|
var Opt = opt
|
2015-03-03 13:38:14 -08:00
|
|
|
var Deadcode = deadcode
|
2016-04-27 16:58:50 -07:00
|
|
|
var Copyelim = copyelim
|
2015-06-04 15:18:27 -07:00
|
|
|
|
2017-03-18 22:00:28 -07:00
|
|
|
var testCtxts = map[string]*obj.Link{
|
|
|
|
|
"amd64": obj.Linknew(&x86.Linkamd64),
|
|
|
|
|
"s390x": obj.Linknew(&s390x.Links390x),
|
2018-03-05 20:59:40 +01:00
|
|
|
"arm64": obj.Linknew(&arm64.Linkarm64),
|
2015-07-30 11:03:05 -07:00
|
|
|
}
|
|
|
|
|
|
2017-03-18 22:00:28 -07:00
|
|
|
func testConfig(tb testing.TB) *Conf { return testConfigArch(tb, "amd64") }
|
|
|
|
|
func testConfigS390X(tb testing.TB) *Conf { return testConfigArch(tb, "s390x") }
|
2018-05-25 16:08:13 -04:00
|
|
|
func testConfigARM64(tb testing.TB) *Conf { return testConfigArch(tb, "arm64") }
|
2017-03-18 22:00:28 -07:00
|
|
|
|
|
|
|
|
func testConfigArch(tb testing.TB, arch string) *Conf {
|
|
|
|
|
ctxt, ok := testCtxts[arch]
|
|
|
|
|
if !ok {
|
|
|
|
|
tb.Fatalf("unknown arch %s", arch)
|
|
|
|
|
}
|
2017-04-21 18:44:34 -07:00
|
|
|
if ctxt.Arch.PtrSize != 8 {
|
2020-11-24 22:09:57 -05:00
|
|
|
tb.Fatal("testTypes is 64-bit only")
|
2017-03-18 22:00:28 -07:00
|
|
|
}
|
|
|
|
|
c := &Conf{
|
2021-06-09 20:14:15 -04:00
|
|
|
config: NewConfig(arch, testTypes, ctxt, true, false),
|
2017-03-18 22:00:28 -07:00
|
|
|
tb: tb,
|
|
|
|
|
}
|
|
|
|
|
return c
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Conf struct {
|
|
|
|
|
config *Config
|
|
|
|
|
tb testing.TB
|
|
|
|
|
fe Frontend
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *Conf) Frontend() Frontend {
|
|
|
|
|
if c.fe == nil {
|
2020-11-24 22:09:57 -05:00
|
|
|
c.fe = TestFrontend{t: c.tb, ctxt: c.config.ctxt}
|
2017-03-18 22:00:28 -07:00
|
|
|
}
|
|
|
|
|
return c.fe
|
2017-02-21 15:20:38 -05:00
|
|
|
}
|
|
|
|
|
|
2020-11-24 22:09:57 -05:00
|
|
|
// TestFrontend is a test-only frontend.
|
2015-07-30 11:03:05 -07:00
|
|
|
// It assumes 64 bit integers and pointers.
|
2020-11-24 22:09:57 -05:00
|
|
|
type TestFrontend struct {
|
2017-03-18 22:00:28 -07:00
|
|
|
t testing.TB
|
|
|
|
|
ctxt *obj.Link
|
2015-06-12 11:01:13 -07:00
|
|
|
}
|
2015-06-04 15:18:27 -07:00
|
|
|
|
2020-11-24 22:09:57 -05:00
|
|
|
func (TestFrontend) StringData(s string) *obj.LSym {
|
2015-06-04 15:18:27 -07:00
|
|
|
return nil
|
|
|
|
|
}
|
2020-12-06 12:02:22 -08:00
|
|
|
func (TestFrontend) Auto(pos src.XPos, t *types.Type) *ir.Name {
|
2020-11-17 21:47:56 -05:00
|
|
|
n := ir.NewNameAt(pos, &types.Sym{Name: "aFakeAuto"})
|
2021-01-03 20:14:00 -08:00
|
|
|
n.Class = ir.PAUTO
|
2020-11-17 21:47:56 -05:00
|
|
|
return n
|
2015-08-24 02:16:19 -07:00
|
|
|
}
|
2020-11-24 22:09:57 -05:00
|
|
|
func (d TestFrontend) SplitSlot(parent *LocalSlot, suffix string, offset int64, t *types.Type) LocalSlot {
|
2020-08-17 16:57:22 -04:00
|
|
|
return LocalSlot{N: parent.N, Type: t, Off: offset}
|
|
|
|
|
}
|
2020-11-24 22:09:57 -05:00
|
|
|
func (TestFrontend) Line(_ src.XPos) string {
|
2016-01-14 16:02:23 -08:00
|
|
|
return "unknown.go:0"
|
|
|
|
|
}
|
2020-11-24 22:09:57 -05:00
|
|
|
func (TestFrontend) AllocFrame(f *Func) {
|
2016-10-03 12:26:25 -07:00
|
|
|
}
|
2020-11-24 22:09:57 -05:00
|
|
|
func (d TestFrontend) Syslook(s string) *obj.LSym {
|
2017-04-20 07:13:02 -07:00
|
|
|
return d.ctxt.Lookup(s)
|
2016-10-13 06:57:00 -04:00
|
|
|
}
|
2020-11-24 22:09:57 -05:00
|
|
|
func (TestFrontend) UseWriteBarrier() bool {
|
2017-02-05 23:43:31 -05:00
|
|
|
return true // only writebarrier_test cares
|
|
|
|
|
}
|
2020-11-24 22:09:57 -05:00
|
|
|
func (TestFrontend) SetWBPos(pos src.XPos) {
|
2017-10-24 17:10:02 -04:00
|
|
|
}
|
2015-06-12 11:01:13 -07:00
|
|
|
|
2020-11-24 22:09:57 -05:00
|
|
|
func (d TestFrontend) Logf(msg string, args ...interface{}) { d.t.Logf(msg, args...) }
|
|
|
|
|
func (d TestFrontend) Log() bool { return true }
|
2016-01-13 11:14:57 -08:00
|
|
|
|
2020-11-24 22:09:57 -05:00
|
|
|
func (d TestFrontend) Fatalf(_ src.XPos, msg string, args ...interface{}) { d.t.Fatalf(msg, args...) }
|
|
|
|
|
func (d TestFrontend) Warnl(_ src.XPos, msg string, args ...interface{}) { d.t.Logf(msg, args...) }
|
|
|
|
|
func (d TestFrontend) Debug_checknil() bool { return false }
|
2015-07-30 11:03:05 -07:00
|
|
|
|
2020-11-24 22:09:57 -05:00
|
|
|
func (d TestFrontend) MyImportPath() string {
|
2020-08-05 10:26:57 -04:00
|
|
|
return "my/import/path"
|
|
|
|
|
}
|
cmd/compile: implement jump tables
Performance is kind of hard to exactly quantify.
One big difference between jump tables and the old binary search
scheme is that there's only 1 branch statement instead of O(n) of
them. That can be both a blessing and a curse, and can make evaluating
jump tables very hard to do.
The single branch can become a choke point for the hardware branch
predictor. A branch table jump must fit all of its state in a single
branch predictor entry (technically, a branch target predictor entry).
With binary search that predictor state can be spread among lots of
entries. In cases where the case selection is repetitive and thus
predictable, binary search can perform better.
The big win for a jump table is that it doesn't consume so much of the
branch predictor's resources. But that benefit is essentially never
observed in microbenchmarks, because the branch predictor can easily
keep state for all the binary search branches in a microbenchmark. So
that benefit is really hard to measure.
So predictable switch microbenchmarks are ~useless - they will almost
always favor the binary search scheme. Fully unpredictable switch
microbenchmarks are better, as they aren't lying to us quite so
much. In a perfectly unpredictable situation, a jump table will expect
to incur 1-1/N branch mispredicts, where a binary search would incur
lg(N)/2 of them. That makes the crossover point at about N=4. But of
course switches in real programs are seldom fully unpredictable, so
we'll use a higher crossover point.
Beyond the branch predictor, jump tables tend to execute more
instructions per switch but have no additional instructions per case,
which also argues for a larger crossover.
As far as code size goes, with this CL cmd/go has a slightly smaller
code segment and a slightly larger overall size (from the jump tables
themselves which live in the data segment).
This is a case where some FDO (feedback-directed optimization) would
be really nice to have. #28262
Some large-program benchmarks might help make the case for this
CL. Especially if we can turn on branch mispredict counters so we can
see how much using jump tables can free up branch prediction resources
that can be gainfully used elsewhere in the program.
name old time/op new time/op delta
Switch8Predictable 1.89ns ± 2% 1.27ns ± 3% -32.58% (p=0.000 n=9+10)
Switch8Unpredictable 9.33ns ± 1% 7.50ns ± 1% -19.60% (p=0.000 n=10+9)
Switch32Predictable 2.20ns ± 2% 1.64ns ± 1% -25.39% (p=0.000 n=10+9)
Switch32Unpredictable 10.0ns ± 2% 7.6ns ± 2% -24.04% (p=0.000 n=10+10)
Fixes #5496
Update #34381
Change-Id: I3ff56011d02be53f605ca5fd3fb96b905517c34f
Reviewed-on: https://go-review.googlesource.com/c/go/+/357330
Run-TryBot: Keith Randall <khr@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Cherry Mui <cherryyz@google.com>
Reviewed-by: Keith Randall <khr@google.com>
2021-10-04 12:17:46 -07:00
|
|
|
func (d TestFrontend) LSym() string {
|
|
|
|
|
return "my/import/path.function"
|
|
|
|
|
}
|
2020-08-05 10:26:57 -04:00
|
|
|
|
2020-11-24 22:09:57 -05:00
|
|
|
var testTypes Types
|
cmd/compile: change ssa.Type into *types.Type
When package ssa was created, Type was in package gc.
To avoid circular dependencies, we used an interface (ssa.Type)
to represent type information in SSA.
In the Go 1.9 cycle, gri extricated the Type type from package gc.
As a result, we can now use it in package ssa.
Now, instead of package types depending on package ssa,
it is the other way.
This is a more sensible dependency tree,
and helps compiler performance a bit.
Though this is a big CL, most of the changes are
mechanical and uninteresting.
Interesting bits:
* Add new singleton globals to package types for the special
SSA types Memory, Void, Invalid, Flags, and Int128.
* Add two new Types, TSSA for the special types,
and TTUPLE, for SSA tuple types.
ssa.MakeTuple is now types.NewTuple.
* Move type comparison result constants CMPlt, CMPeq, and CMPgt
to package types.
* We had picked the name "types" in our rules for the handy
list of types provided by ssa.Config. That conflicted with
the types package name, so change it to "typ".
* Update the type comparison routine to handle tuples and special
types inline.
* Teach gc/fmt.go how to print special types.
* We can now eliminate ElemTypes in favor of just Elem,
and probably also some other duplicated Type methods
designed to return ssa.Type instead of *types.Type.
* The ssa tests were using their own dummy types,
and they were not particularly careful about types in general.
Of necessity, this CL switches them to use *types.Type;
it does not make them more type-accurate.
Unfortunately, using types.Type means initializing a bit
of the types universe.
This is prime for refactoring and improvement.
This shrinks ssa.Value; it now fits in a smaller size class
on 64 bit systems. This doesn't have a giant impact,
though, since most Values are preallocated in a chunk.
name old alloc/op new alloc/op delta
Template 37.9MB ± 0% 37.7MB ± 0% -0.57% (p=0.000 n=10+8)
Unicode 28.9MB ± 0% 28.7MB ± 0% -0.52% (p=0.000 n=10+10)
GoTypes 110MB ± 0% 109MB ± 0% -0.88% (p=0.000 n=10+10)
Flate 24.7MB ± 0% 24.6MB ± 0% -0.66% (p=0.000 n=10+10)
GoParser 31.1MB ± 0% 30.9MB ± 0% -0.61% (p=0.000 n=10+9)
Reflect 73.9MB ± 0% 73.4MB ± 0% -0.62% (p=0.000 n=10+8)
Tar 25.8MB ± 0% 25.6MB ± 0% -0.77% (p=0.000 n=9+10)
XML 41.2MB ± 0% 40.9MB ± 0% -0.80% (p=0.000 n=10+10)
[Geo mean] 40.5MB 40.3MB -0.68%
name old allocs/op new allocs/op delta
Template 385k ± 0% 386k ± 0% ~ (p=0.356 n=10+9)
Unicode 343k ± 1% 344k ± 0% ~ (p=0.481 n=10+10)
GoTypes 1.16M ± 0% 1.16M ± 0% -0.16% (p=0.004 n=10+10)
Flate 238k ± 1% 238k ± 1% ~ (p=0.853 n=10+10)
GoParser 320k ± 0% 320k ± 0% ~ (p=0.720 n=10+9)
Reflect 957k ± 0% 957k ± 0% ~ (p=0.460 n=10+8)
Tar 252k ± 0% 252k ± 0% ~ (p=0.133 n=9+10)
XML 400k ± 0% 400k ± 0% ~ (p=0.796 n=10+10)
[Geo mean] 428k 428k -0.01%
Removing all the interface calls helps non-trivially with CPU, though.
name old time/op new time/op delta
Template 178ms ± 4% 173ms ± 3% -2.90% (p=0.000 n=94+96)
Unicode 85.0ms ± 4% 83.9ms ± 4% -1.23% (p=0.000 n=96+96)
GoTypes 543ms ± 3% 528ms ± 3% -2.73% (p=0.000 n=98+96)
Flate 116ms ± 3% 113ms ± 4% -2.34% (p=0.000 n=96+99)
GoParser 144ms ± 3% 140ms ± 4% -2.80% (p=0.000 n=99+97)
Reflect 344ms ± 3% 334ms ± 4% -3.02% (p=0.000 n=100+99)
Tar 106ms ± 5% 103ms ± 4% -3.30% (p=0.000 n=98+94)
XML 198ms ± 5% 192ms ± 4% -2.88% (p=0.000 n=92+95)
[Geo mean] 178ms 173ms -2.65%
name old user-time/op new user-time/op delta
Template 229ms ± 5% 224ms ± 5% -2.36% (p=0.000 n=95+99)
Unicode 107ms ± 6% 106ms ± 5% -1.13% (p=0.001 n=93+95)
GoTypes 696ms ± 4% 679ms ± 4% -2.45% (p=0.000 n=97+99)
Flate 137ms ± 4% 134ms ± 5% -2.66% (p=0.000 n=99+96)
GoParser 176ms ± 5% 172ms ± 8% -2.27% (p=0.000 n=98+100)
Reflect 430ms ± 6% 411ms ± 5% -4.46% (p=0.000 n=100+92)
Tar 128ms ±13% 123ms ±13% -4.21% (p=0.000 n=100+100)
XML 239ms ± 6% 233ms ± 6% -2.50% (p=0.000 n=95+97)
[Geo mean] 220ms 213ms -2.76%
Change-Id: I15c7d6268347f8358e75066dfdbd77db24e8d0c1
Reviewed-on: https://go-review.googlesource.com/42145
Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
2017-04-28 14:12:28 -07:00
|
|
|
|
|
|
|
|
func init() {
|
2021-08-26 14:16:24 -07:00
|
|
|
// TODO(mdempsky): Push into types.InitUniverse or typecheck.InitUniverse.
|
|
|
|
|
types.PtrSize = 8
|
|
|
|
|
types.RegSize = 8
|
|
|
|
|
types.MaxWidth = 1 << 50
|
|
|
|
|
|
|
|
|
|
typecheck.InitUniverse()
|
2020-11-24 22:09:57 -05:00
|
|
|
testTypes.SetTypPtrs()
|
2017-03-17 16:04:46 -07:00
|
|
|
}
|
|
|
|
|
|
2020-11-24 22:09:57 -05:00
|
|
|
func (d TestFrontend) DerefItab(sym *obj.LSym, off int64) *obj.LSym { return nil }
|
2015-09-18 22:58:10 -07:00
|
|
|
|
2020-11-24 22:09:57 -05:00
|
|
|
func (d TestFrontend) CanSSA(t *types.Type) bool {
|
|
|
|
|
// There are no un-SSAable types in test land.
|
2015-09-18 22:58:10 -07:00
|
|
|
return true
|
|
|
|
|
}
|