Skip to content

Commit cd55f86

Browse files
committed
cmd/compile: allow multi-field structs to be stored directly in interfaces
If the struct is a bunch of 0-sized fields and one pointer field. Fixes #74092 Change-Id: I87c5d162c8c9fdba812420d7f9d21de97295b62c Reviewed-on: https://go-review.googlesource.com/c/go/+/681937 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: David Chase <[email protected]> Reviewed-by: Keith Randall <[email protected]>
1 parent 21ab012 commit cd55f86

File tree

7 files changed

+33
-36
lines changed

7 files changed

+33
-36
lines changed

src/cmd/compile/internal/ssa/_gen/generic.rules

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -921,8 +921,8 @@
921921
@x.Block (Load <v.Type> (OffPtr <v.Type.PtrTo()> [t.FieldOff(int(i))] ptr) mem)
922922

923923
// Putting struct{*byte} and similar into direct interfaces.
924-
(IMake _typ (StructMake val)) => (IMake _typ val)
925-
(StructSelect [0] (IData x)) => (IData x)
924+
(IMake _typ (StructMake val)) => imakeOfStructMake(v)
925+
(StructSelect [_] (IData x)) => (IData x)
926926

927927
// un-SSAable values use mem->mem copies
928928
(Store {t} dst (Load src mem) mem) && !CanSSA(t) =>

src/cmd/compile/internal/ssa/expand_calls.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,14 @@ func (x *expandState) decomposeAsNecessary(pos src.XPos, b *Block, a, m0 *Value,
423423
if a.Op == OpIMake {
424424
data := a.Args[1]
425425
for data.Op == OpStructMake || data.Op == OpArrayMake1 {
426-
data = data.Args[0]
426+
// A struct make might have a few zero-sized fields.
427+
// Use the pointer-y one we know is there.
428+
for _, a := range data.Args {
429+
if a.Type.Size() > 0 {
430+
data = a
431+
break
432+
}
433+
}
427434
}
428435
return x.decomposeAsNecessary(pos, b, data, mem, rc.next(data.Type))
429436
}

src/cmd/compile/internal/ssa/rewrite.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2689,3 +2689,17 @@ func panicBoundsCToAux(p PanicBoundsC) Aux {
26892689
func panicBoundsCCToAux(p PanicBoundsCC) Aux {
26902690
return p
26912691
}
2692+
2693+
// When v is (IMake typ (StructMake ...)), convert to
2694+
// (IMake typ arg) where arg is the pointer-y argument to
2695+
// the StructMake (there must be exactly one).
2696+
func imakeOfStructMake(v *Value) *Value {
2697+
var arg *Value
2698+
for _, a := range v.Args[1].Args {
2699+
if a.Type.Size() > 0 {
2700+
arg = a
2701+
break
2702+
}
2703+
}
2704+
return v.Block.NewValue2(v.Pos, OpIMake, v.Type, v.Args[0], arg)
2705+
}

src/cmd/compile/internal/ssa/rewritegeneric.go

Lines changed: 4 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/cmd/compile/internal/types/type.go

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1829,26 +1829,7 @@ func IsReflexive(t *Type) bool {
18291829
// Can this type be stored directly in an interface word?
18301830
// Yes, if the representation is a single pointer.
18311831
func IsDirectIface(t *Type) bool {
1832-
switch t.Kind() {
1833-
case TPTR:
1834-
// Pointers to notinheap types must be stored indirectly. See issue 42076.
1835-
return !t.Elem().NotInHeap()
1836-
case TCHAN,
1837-
TMAP,
1838-
TFUNC,
1839-
TUNSAFEPTR:
1840-
return true
1841-
1842-
case TARRAY:
1843-
// Array of 1 direct iface type can be direct.
1844-
return t.NumElem() == 1 && IsDirectIface(t.Elem())
1845-
1846-
case TSTRUCT:
1847-
// Struct with 1 field of direct iface type can be direct.
1848-
return t.NumFields() == 1 && IsDirectIface(t.Field(0).Type)
1849-
}
1850-
1851-
return false
1832+
return t.Size() == int64(PtrSize) && PtrDataSize(t) == int64(PtrSize)
18521833
}
18531834

18541835
// IsInterfaceMethod reports whether (field) m is

src/internal/abi/type.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@ const (
121121
TFlagGCMaskOnDemand TFlag = 1 << 4
122122

123123
// TFlagDirectIface means that a value of this type is stored directly
124-
// in the data field of an interface, instead of indirectly. Normally
125-
// this means the type is pointer-ish.
124+
// in the data field of an interface, instead of indirectly.
125+
// This flag is just a cached computation of Size_ == PtrBytes == goarch.PtrSize.
126126
TFlagDirectIface TFlag = 1 << 5
127127

128128
// Leaving this breadcrumb behind for dlv. It should not be used, and no

src/reflect/type.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2524,8 +2524,7 @@ func StructOf(fields []StructField) Type {
25242524
}
25252525

25262526
switch {
2527-
case len(fs) == 1 && fs[0].Typ.IsDirectIface():
2528-
// structs of 1 direct iface type can be direct
2527+
case typ.Size_ == goarch.PtrSize && typ.PtrBytes == goarch.PtrSize:
25292528
typ.TFlag |= abi.TFlagDirectIface
25302529
default:
25312530
typ.TFlag &^= abi.TFlagDirectIface
@@ -2694,8 +2693,7 @@ func ArrayOf(length int, elem Type) Type {
26942693
}
26952694

26962695
switch {
2697-
case length == 1 && typ.IsDirectIface():
2698-
// array of 1 direct iface type can be direct
2696+
case array.Size_ == goarch.PtrSize && array.PtrBytes == goarch.PtrSize:
26992697
array.TFlag |= abi.TFlagDirectIface
27002698
default:
27012699
array.TFlag &^= abi.TFlagDirectIface

0 commit comments

Comments
 (0)