2020-12-24 16:03:47 -08:00
|
|
|
// Copyright 2020 The Go Authors. All rights reserved.
|
2020-12-24 15:42:37 -08:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
2023-05-12 16:39:43 -04:00
|
|
|
// Package devirtualize implements two "devirtualization" optimization passes:
|
|
|
|
|
//
|
2023-06-29 11:44:04 -04:00
|
|
|
// - "Static" devirtualization which replaces interface method calls with
|
|
|
|
|
// direct concrete-type method calls where possible.
|
|
|
|
|
// - "Profile-guided" devirtualization which replaces indirect calls with a
|
|
|
|
|
// conditional direct call to the hottest concrete callee from a profile, as
|
|
|
|
|
// well as a fallback using the original indirect call.
|
2020-12-24 15:42:37 -08:00
|
|
|
package devirtualize
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"cmd/compile/internal/base"
|
|
|
|
|
"cmd/compile/internal/ir"
|
|
|
|
|
"cmd/compile/internal/typecheck"
|
|
|
|
|
"cmd/compile/internal/types"
|
|
|
|
|
)
|
|
|
|
|
|
2023-05-12 16:39:43 -04:00
|
|
|
// Static devirtualizes calls within fn where possible when the concrete callee
|
|
|
|
|
// is available statically.
|
|
|
|
|
func Static(fn *ir.Func) {
|
2020-12-24 15:42:37 -08:00
|
|
|
ir.CurFunc = fn
|
2022-09-06 12:54:54 +07:00
|
|
|
|
|
|
|
|
// For promoted methods (including value-receiver methods promoted to pointer-receivers),
|
|
|
|
|
// the interface method wrapper may contain expressions that can panic (e.g., ODEREF, ODOTPTR, ODOTINTER).
|
|
|
|
|
// Devirtualization involves inlining these expressions (and possible panics) to the call site.
|
|
|
|
|
// This normally isn't a problem, but for go/defer statements it can move the panic from when/where
|
|
|
|
|
// the call executes to the go/defer statement itself, which is a visible change in semantics (e.g., #52072).
|
|
|
|
|
// To prevent this, we skip devirtualizing calls within go/defer statements altogether.
|
|
|
|
|
goDeferCall := make(map[*ir.CallExpr]bool)
|
2020-12-24 15:42:37 -08:00
|
|
|
ir.VisitList(fn.Body, func(n ir.Node) {
|
2022-09-06 12:54:54 +07:00
|
|
|
switch n := n.(type) {
|
|
|
|
|
case *ir.GoDeferStmt:
|
|
|
|
|
if call, ok := n.Call.(*ir.CallExpr); ok {
|
|
|
|
|
goDeferCall[call] = true
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
case *ir.CallExpr:
|
|
|
|
|
if !goDeferCall[n] {
|
2023-05-12 16:39:43 -04:00
|
|
|
staticCall(n)
|
2022-09-06 12:54:54 +07:00
|
|
|
}
|
2020-12-24 15:42:37 -08:00
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-12 16:39:43 -04:00
|
|
|
// staticCall devirtualizes the given call if possible when the concrete callee
|
|
|
|
|
// is available statically.
|
|
|
|
|
func staticCall(call *ir.CallExpr) {
|
2020-12-24 16:03:47 -08:00
|
|
|
if call.Op() != ir.OCALLINTER {
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-10-04 15:22:49 -07:00
|
|
|
sel := call.Fun.(*ir.SelectorExpr)
|
2020-12-24 15:42:37 -08:00
|
|
|
r := ir.StaticValue(sel.X)
|
|
|
|
|
if r.Op() != ir.OCONVIFACE {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
recv := r.(*ir.ConvExpr)
|
|
|
|
|
|
|
|
|
|
typ := recv.X.Type()
|
|
|
|
|
if typ.IsInterface() {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-01 16:14:11 -08:00
|
|
|
// If typ is a shape type, then it was a type argument originally
|
|
|
|
|
// and we'd need an indirect call through the dictionary anyway.
|
|
|
|
|
// We're unable to devirtualize this call.
|
|
|
|
|
if typ.IsShape() {
|
|
|
|
|
return
|
|
|
|
|
}
|
cmd/compile/internal/noder: shape-based stenciling for unified IR
This CL switches unified IR to use shape-based stenciling with runtime
dictionaries, like the existing non-unified frontend. Specifically,
when instantiating generic functions and types `X[T]`, we now also
instantiated shaped variants `X[shapify(T)]` that can be shared by
`T`'s with common underlying types.
For example, for generic function `F`, `F[int](args...)` will be
rewritten to `F[go.shape.int](&.dict.F[int], args...)`.
For generic type `T` with method `M` and value `t` of type `T[int]`,
`t.M(args...)` will be rewritten to `T[go.shape.int].M(t,
&.dict.T[int], args...)`.
Two notable distinctions from the non-unified frontend:
1. For simplicity, currently shaping is limited to simply converting
type arguments to their underlying type. Subsequent CLs will implement
more aggressive shaping.
2. For generic types, a single dictionary is generated to be shared by
all methods, rather than separate dictionaries for each method. I
originally went with this design because I have an idea of changing
interface calls to pass the itab pointer via the closure
register (which should have zero overhead), and then the interface
wrappers for generic methods could use the *runtime.itab to find the
runtime dictionary that corresponds to the dynamic type. This would
allow emitting fewer method wrappers.
However, this choice does have the consequence that currently even if
a method is unused and its code is pruned by the linker, it may have
produced runtime dictionary entries that need to be kept alive anyway.
I'm open to changing this to generate per-method dictionaries, though
this would require changing the unified IR export data format; so it
would be best to make this decision before Go 1.20.
The other option is making the linker smarter about pruning unneeded
dictionary entries, like how it already prunes itab entries. For
example, the runtime dictionary for `T[int]` could have a `R_DICTTYPE`
meta-relocation against symbol `.dicttype.T[go.shape.int]` that
declares it's a dictionary associated with that type; and then each
method on `T[go.shape.T]` could have `R_DICTUSE` meta-relocations
against `.dicttype.T[go.shape.T]+offset` indicating which fields
within dictionaries of that type need to be preserved.
Change-Id: I369580b1d93d19640a4b5ecada4f6231adcce3fd
Reviewed-on: https://go-review.googlesource.com/c/go/+/421821
Reviewed-by: David Chase <drchase@google.com>
Reviewed-by: Keith Randall <khr@golang.org>
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
2022-08-06 16:40:56 -07:00
|
|
|
|
2022-12-01 16:14:11 -08:00
|
|
|
// If typ *has* a shape type, then it's an shaped, instantiated
|
|
|
|
|
// type like T[go.shape.int], and its methods (may) have an extra
|
|
|
|
|
// dictionary parameter. We could devirtualize this call if we
|
|
|
|
|
// could derive an appropriate dictionary argument.
|
|
|
|
|
//
|
|
|
|
|
// TODO(mdempsky): If typ has has a promoted non-generic method,
|
|
|
|
|
// then that method won't require a dictionary argument. We could
|
|
|
|
|
// still devirtualize those calls.
|
|
|
|
|
//
|
|
|
|
|
// TODO(mdempsky): We have the *runtime.itab in recv.TypeWord. It
|
|
|
|
|
// should be possible to compute the represented type's runtime
|
|
|
|
|
// dictionary from this (e.g., by adding a pointer from T[int]'s
|
|
|
|
|
// *runtime._type to .dict.T[int]; or by recognizing static
|
|
|
|
|
// references to go:itab.T[int],iface and constructing a direct
|
|
|
|
|
// reference to .dict.T[int]).
|
|
|
|
|
if typ.HasShape() {
|
|
|
|
|
if base.Flag.LowerM != 0 {
|
|
|
|
|
base.WarnfAt(call.Pos(), "cannot devirtualize %v: shaped receiver %v", call, typ)
|
cmd/compile/internal/noder: shape-based stenciling for unified IR
This CL switches unified IR to use shape-based stenciling with runtime
dictionaries, like the existing non-unified frontend. Specifically,
when instantiating generic functions and types `X[T]`, we now also
instantiated shaped variants `X[shapify(T)]` that can be shared by
`T`'s with common underlying types.
For example, for generic function `F`, `F[int](args...)` will be
rewritten to `F[go.shape.int](&.dict.F[int], args...)`.
For generic type `T` with method `M` and value `t` of type `T[int]`,
`t.M(args...)` will be rewritten to `T[go.shape.int].M(t,
&.dict.T[int], args...)`.
Two notable distinctions from the non-unified frontend:
1. For simplicity, currently shaping is limited to simply converting
type arguments to their underlying type. Subsequent CLs will implement
more aggressive shaping.
2. For generic types, a single dictionary is generated to be shared by
all methods, rather than separate dictionaries for each method. I
originally went with this design because I have an idea of changing
interface calls to pass the itab pointer via the closure
register (which should have zero overhead), and then the interface
wrappers for generic methods could use the *runtime.itab to find the
runtime dictionary that corresponds to the dynamic type. This would
allow emitting fewer method wrappers.
However, this choice does have the consequence that currently even if
a method is unused and its code is pruned by the linker, it may have
produced runtime dictionary entries that need to be kept alive anyway.
I'm open to changing this to generate per-method dictionaries, though
this would require changing the unified IR export data format; so it
would be best to make this decision before Go 1.20.
The other option is making the linker smarter about pruning unneeded
dictionary entries, like how it already prunes itab entries. For
example, the runtime dictionary for `T[int]` could have a `R_DICTTYPE`
meta-relocation against symbol `.dicttype.T[go.shape.int]` that
declares it's a dictionary associated with that type; and then each
method on `T[go.shape.T]` could have `R_DICTUSE` meta-relocations
against `.dicttype.T[go.shape.T]+offset` indicating which fields
within dictionaries of that type need to be preserved.
Change-Id: I369580b1d93d19640a4b5ecada4f6231adcce3fd
Reviewed-on: https://go-review.googlesource.com/c/go/+/421821
Reviewed-by: David Chase <drchase@google.com>
Reviewed-by: Keith Randall <khr@golang.org>
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
2022-08-06 16:40:56 -07:00
|
|
|
}
|
2022-12-01 16:14:11 -08:00
|
|
|
return
|
|
|
|
|
}
|
cmd/compile: fix devirtualization bug with unified IR
As a consistency check in devirtualization, when we determine `i` (of
interface type `I`) always has dynamic type `T`, we insert a type
assertion `i.(T)`. This emits an itab check for `go:itab.T,I`, but
it's always true (and so SSA optimizes it away).
However, if `I` is instead the generic interface type `I[T]`, then
`go:itab.T,I[int]` and `go:itab.T,I[go.shape.int]` are equivalent but
distinct itabs. And notably, we'll have originally created the
interface value using the former; but the (non-dynamic) TypeAssertExpr
created by devirtualization would ultimately emit a comparison against
the latter. This comparison would then evaluate false, leading to a
spurious type assertion panic at runtime.
The comparison is just meant as an extra safety check, so it should be
safe to just disable. But for now, it's simpler/safer to just punt on
devirtualization in this case. (The non-unified frontend doesn't
devirtualize this either.)
Change-Id: I6a8809bcfebc9571f32e289fa4bc6a8b0d21ca46
Reviewed-on: https://go-review.googlesource.com/c/go/+/424774
Reviewed-by: Keith Randall <khr@golang.org>
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
Reviewed-by: Keith Randall <khr@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
2022-08-18 04:56:10 -07:00
|
|
|
|
2022-12-01 16:14:11 -08:00
|
|
|
// Further, if sel.X's type has a shape type, then it's a shaped
|
|
|
|
|
// interface type. In this case, the (non-dynamic) TypeAssertExpr
|
|
|
|
|
// we construct below would attempt to create an itab
|
|
|
|
|
// corresponding to this shaped interface type; but the actual
|
|
|
|
|
// itab pointer in the interface value will correspond to the
|
|
|
|
|
// original (non-shaped) interface type instead. These are
|
|
|
|
|
// functionally equivalent, but they have distinct pointer
|
|
|
|
|
// identities, which leads to the type assertion failing.
|
|
|
|
|
//
|
|
|
|
|
// TODO(mdempsky): We know the type assertion here is safe, so we
|
|
|
|
|
// could instead set a flag so that walk skips the itab check. For
|
|
|
|
|
// now, punting is easy and safe.
|
|
|
|
|
if sel.X.Type().HasShape() {
|
|
|
|
|
if base.Flag.LowerM != 0 {
|
|
|
|
|
base.WarnfAt(call.Pos(), "cannot devirtualize %v: shaped interface %v", call, sel.X.Type())
|
cmd/compile: fix devirtualization bug with unified IR
As a consistency check in devirtualization, when we determine `i` (of
interface type `I`) always has dynamic type `T`, we insert a type
assertion `i.(T)`. This emits an itab check for `go:itab.T,I`, but
it's always true (and so SSA optimizes it away).
However, if `I` is instead the generic interface type `I[T]`, then
`go:itab.T,I[int]` and `go:itab.T,I[go.shape.int]` are equivalent but
distinct itabs. And notably, we'll have originally created the
interface value using the former; but the (non-dynamic) TypeAssertExpr
created by devirtualization would ultimately emit a comparison against
the latter. This comparison would then evaluate false, leading to a
spurious type assertion panic at runtime.
The comparison is just meant as an extra safety check, so it should be
safe to just disable. But for now, it's simpler/safer to just punt on
devirtualization in this case. (The non-unified frontend doesn't
devirtualize this either.)
Change-Id: I6a8809bcfebc9571f32e289fa4bc6a8b0d21ca46
Reviewed-on: https://go-review.googlesource.com/c/go/+/424774
Reviewed-by: Keith Randall <khr@golang.org>
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
Reviewed-by: Keith Randall <khr@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
2022-08-18 04:56:10 -07:00
|
|
|
}
|
2022-12-01 16:14:11 -08:00
|
|
|
return
|
cmd/compile/internal/noder: shape-based stenciling for unified IR
This CL switches unified IR to use shape-based stenciling with runtime
dictionaries, like the existing non-unified frontend. Specifically,
when instantiating generic functions and types `X[T]`, we now also
instantiated shaped variants `X[shapify(T)]` that can be shared by
`T`'s with common underlying types.
For example, for generic function `F`, `F[int](args...)` will be
rewritten to `F[go.shape.int](&.dict.F[int], args...)`.
For generic type `T` with method `M` and value `t` of type `T[int]`,
`t.M(args...)` will be rewritten to `T[go.shape.int].M(t,
&.dict.T[int], args...)`.
Two notable distinctions from the non-unified frontend:
1. For simplicity, currently shaping is limited to simply converting
type arguments to their underlying type. Subsequent CLs will implement
more aggressive shaping.
2. For generic types, a single dictionary is generated to be shared by
all methods, rather than separate dictionaries for each method. I
originally went with this design because I have an idea of changing
interface calls to pass the itab pointer via the closure
register (which should have zero overhead), and then the interface
wrappers for generic methods could use the *runtime.itab to find the
runtime dictionary that corresponds to the dynamic type. This would
allow emitting fewer method wrappers.
However, this choice does have the consequence that currently even if
a method is unused and its code is pruned by the linker, it may have
produced runtime dictionary entries that need to be kept alive anyway.
I'm open to changing this to generate per-method dictionaries, though
this would require changing the unified IR export data format; so it
would be best to make this decision before Go 1.20.
The other option is making the linker smarter about pruning unneeded
dictionary entries, like how it already prunes itab entries. For
example, the runtime dictionary for `T[int]` could have a `R_DICTTYPE`
meta-relocation against symbol `.dicttype.T[go.shape.int]` that
declares it's a dictionary associated with that type; and then each
method on `T[go.shape.T]` could have `R_DICTUSE` meta-relocations
against `.dicttype.T[go.shape.T]+offset` indicating which fields
within dictionaries of that type need to be preserved.
Change-Id: I369580b1d93d19640a4b5ecada4f6231adcce3fd
Reviewed-on: https://go-review.googlesource.com/c/go/+/421821
Reviewed-by: David Chase <drchase@google.com>
Reviewed-by: Keith Randall <khr@golang.org>
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
2022-08-06 16:40:56 -07:00
|
|
|
}
|
|
|
|
|
|
2020-12-24 15:42:37 -08:00
|
|
|
dt := ir.NewTypeAssertExpr(sel.Pos(), sel.X, nil)
|
|
|
|
|
dt.SetType(typ)
|
2023-08-18 19:40:33 -07:00
|
|
|
x := typecheck.XDotMethod(sel.Pos(), dt, sel.Sel, true)
|
2020-12-24 15:42:37 -08:00
|
|
|
switch x.Op() {
|
|
|
|
|
case ir.ODOTMETH:
|
|
|
|
|
if base.Flag.LowerM != 0 {
|
|
|
|
|
base.WarnfAt(call.Pos(), "devirtualizing %v to %v", sel, typ)
|
|
|
|
|
}
|
|
|
|
|
call.SetOp(ir.OCALLMETH)
|
2023-10-04 15:22:49 -07:00
|
|
|
call.Fun = x
|
2020-12-24 15:42:37 -08:00
|
|
|
case ir.ODOTINTER:
|
|
|
|
|
// Promoted method from embedded interface-typed field (#42279).
|
|
|
|
|
if base.Flag.LowerM != 0 {
|
|
|
|
|
base.WarnfAt(call.Pos(), "partially devirtualizing %v to %v", sel, typ)
|
|
|
|
|
}
|
|
|
|
|
call.SetOp(ir.OCALLINTER)
|
2023-10-04 15:22:49 -07:00
|
|
|
call.Fun = x
|
2020-12-24 15:42:37 -08:00
|
|
|
default:
|
2023-08-19 22:32:08 -07:00
|
|
|
base.FatalfAt(call.Pos(), "failed to devirtualize %v (%v)", x, x.Op())
|
2020-12-24 15:42:37 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Duplicated logic from typecheck for function call return
|
|
|
|
|
// value types.
|
|
|
|
|
//
|
|
|
|
|
// Receiver parameter size may have changed; need to update
|
|
|
|
|
// call.Type to get correct stack offsets for result
|
|
|
|
|
// parameters.
|
|
|
|
|
types.CheckSize(x.Type())
|
|
|
|
|
switch ft := x.Type(); ft.NumResults() {
|
|
|
|
|
case 0:
|
|
|
|
|
case 1:
|
2023-08-20 10:05:29 -07:00
|
|
|
call.SetType(ft.Result(0).Type)
|
2020-12-24 15:42:37 -08:00
|
|
|
default:
|
2023-08-20 10:05:29 -07:00
|
|
|
call.SetType(ft.ResultsTuple())
|
2020-12-24 15:42:37 -08:00
|
|
|
}
|
2022-12-14 11:48:02 -08:00
|
|
|
|
|
|
|
|
// Desugar OCALLMETH, if we created one (#57309).
|
|
|
|
|
typecheck.FixMethodCall(call)
|
2020-12-24 15:42:37 -08:00
|
|
|
}
|