Skip to content

Commit 38edd9b

Browse files
committed
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 <[email protected]> Reviewed-by: Keith Randall <[email protected]> Run-TryBot: Matthew Dempsky <[email protected]> Reviewed-by: Cuong Manh Le <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent a2c2f06 commit 38edd9b

File tree

8 files changed

+1450
-281
lines changed

8 files changed

+1450
-281
lines changed

src/cmd/compile/internal/devirtualize/devirtualize.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,41 @@ func Call(call *ir.CallExpr) {
4141
return
4242
}
4343

44+
if base.Debug.Unified != 0 {
45+
// N.B., stencil.go converts shape-typed values to interface type
46+
// using OEFACE instead of OCONVIFACE, so devirtualization fails
47+
// above instead. That's why this code is specific to unified IR.
48+
49+
// If typ is a shape type, then it was a type argument originally
50+
// and we'd need an indirect call through the dictionary anyway.
51+
// We're unable to devirtualize this call.
52+
if typ.IsShape() {
53+
return
54+
}
55+
56+
// If typ *has* a shape type, then it's an shaped, instantiated
57+
// type like T[go.shape.int], and its methods (may) have an extra
58+
// dictionary parameter. We could devirtualize this call if we
59+
// could derive an appropriate dictionary argument.
60+
//
61+
// TODO(mdempsky): If typ has has a promoted non-generic method,
62+
// then that method won't require a dictionary argument. We could
63+
// still devirtualize those calls.
64+
//
65+
// TODO(mdempsky): We have the *runtime.itab in recv.TypeWord. It
66+
// should be possible to compute the represented type's runtime
67+
// dictionary from this (e.g., by adding a pointer from T[int]'s
68+
// *runtime._type to .dict.T[int]; or by recognizing static
69+
// references to go:itab.T[int],iface and constructing a direct
70+
// reference to .dict.T[int]).
71+
if typ.HasShape() {
72+
if base.Flag.LowerM != 0 {
73+
base.WarnfAt(call.Pos(), "cannot devirtualize %v: shaped receiver %v", call, typ)
74+
}
75+
return
76+
}
77+
}
78+
4479
dt := ir.NewTypeAssertExpr(sel.Pos(), sel.X, nil)
4580
dt.SetType(typ)
4681
x := typecheck.Callee(ir.NewSelectorExpr(sel.Pos(), ir.OXDOT, dt, sel.Sel))

0 commit comments

Comments
 (0)