]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: allow multi-field structs to be stored directly in interfaces
authorKeith Randall <khr@golang.org>
Sun, 15 Jun 2025 03:10:50 +0000 (20:10 -0700)
committerKeith Randall <khr@golang.org>
Tue, 5 Aug 2025 16:18:31 +0000 (09:18 -0700)
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 <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: David Chase <drchase@google.com>
Reviewed-by: Keith Randall <khr@google.com>
src/cmd/compile/internal/ssa/_gen/generic.rules
src/cmd/compile/internal/ssa/expand_calls.go
src/cmd/compile/internal/ssa/rewrite.go
src/cmd/compile/internal/ssa/rewritegeneric.go
src/cmd/compile/internal/types/type.go
src/internal/abi/type.go
src/reflect/type.go

index 7ac36c3b3cd3911ad07cf9a21d3737b562ae655f..8f354e977f9af2968c64e0b7cc9a1d71c2b8f1d8 100644 (file)
   @x.Block (Load <v.Type> (OffPtr <v.Type.PtrTo()> [t.FieldOff(int(i))] ptr) mem)
 
 // Putting struct{*byte} and similar into direct interfaces.
-(IMake _typ (StructMake val)) => (IMake _typ val)
-(StructSelect [0] (IData x)) => (IData x)
+(IMake _typ (StructMake val)) => imakeOfStructMake(v)
+(StructSelect [_] (IData x)) => (IData x)
 
 // un-SSAable values use mem->mem copies
 (Store {t} dst (Load src mem) mem) && !CanSSA(t) =>
index 272b653ca3c6cef84c5a9983bcc92fb49a65c714..30606bb8055b76c5133bf6f13733483ee9e0ece5 100644 (file)
@@ -423,7 +423,14 @@ func (x *expandState) decomposeAsNecessary(pos src.XPos, b *Block, a, m0 *Value,
                if a.Op == OpIMake {
                        data := a.Args[1]
                        for data.Op == OpStructMake || data.Op == OpArrayMake1 {
-                               data = data.Args[0]
+                               // A struct make might have a few zero-sized fields.
+                               // Use the pointer-y one we know is there.
+                               for _, a := range data.Args {
+                                       if a.Type.Size() > 0 {
+                                               data = a
+                                               break
+                                       }
+                               }
                        }
                        return x.decomposeAsNecessary(pos, b, data, mem, rc.next(data.Type))
                }
index 200318ceb5b4fff86ef9676ee2fb1766506f2e92..87f7b517c2d3d7547e0393db1dc2365fc8361e45 100644 (file)
@@ -2689,3 +2689,17 @@ func panicBoundsCToAux(p PanicBoundsC) Aux {
 func panicBoundsCCToAux(p PanicBoundsCC) Aux {
        return p
 }
+
+// When v is (IMake typ (StructMake ...)), convert to
+// (IMake typ arg) where arg is the pointer-y argument to
+// the StructMake (there must be exactly one).
+func imakeOfStructMake(v *Value) *Value {
+       var arg *Value
+       for _, a := range v.Args[1].Args {
+               if a.Type.Size() > 0 {
+                       arg = a
+                       break
+               }
+       }
+       return v.Block.NewValue2(v.Pos, OpIMake, v.Type, v.Args[0], arg)
+}
index 3af7f40e684f912f5a74a12173add3c5209a7f82..83be129a7bc49b66ca025d3ce95a95a9d4706e34 100644 (file)
@@ -11201,15 +11201,12 @@ func rewriteValuegeneric_OpIMake(v *Value) bool {
        v_1 := v.Args[1]
        v_0 := v.Args[0]
        // match: (IMake _typ (StructMake val))
-       // result: (IMake _typ val)
+       // result: imakeOfStructMake(v)
        for {
-               _typ := v_0
                if v_1.Op != OpStructMake || len(v_1.Args) != 1 {
                        break
                }
-               val := v_1.Args[0]
-               v.reset(OpIMake)
-               v.AddArg2(_typ, val)
+               v.copyOf(imakeOfStructMake(v))
                return true
        }
        // match: (IMake _typ (ArrayMake1 val))
@@ -32045,10 +32042,10 @@ func rewriteValuegeneric_OpStructSelect(v *Value) bool {
                v0.AddArg2(v1, mem)
                return true
        }
-       // match: (StructSelect [0] (IData x))
+       // match: (StructSelect [_] (IData x))
        // result: (IData x)
        for {
-               if auxIntToInt64(v.AuxInt) != 0 || v_0.Op != OpIData {
+               if v_0.Op != OpIData {
                        break
                }
                x := v_0.Args[0]
index 1c7f0a19e9158da5e9c920390446872dfd563315..d73b5e8d7b407a43861db56fba5a28d9f396d13d 100644 (file)
@@ -1829,26 +1829,7 @@ func IsReflexive(t *Type) bool {
 // Can this type be stored directly in an interface word?
 // Yes, if the representation is a single pointer.
 func IsDirectIface(t *Type) bool {
-       switch t.Kind() {
-       case TPTR:
-               // Pointers to notinheap types must be stored indirectly. See issue 42076.
-               return !t.Elem().NotInHeap()
-       case TCHAN,
-               TMAP,
-               TFUNC,
-               TUNSAFEPTR:
-               return true
-
-       case TARRAY:
-               // Array of 1 direct iface type can be direct.
-               return t.NumElem() == 1 && IsDirectIface(t.Elem())
-
-       case TSTRUCT:
-               // Struct with 1 field of direct iface type can be direct.
-               return t.NumFields() == 1 && IsDirectIface(t.Field(0).Type)
-       }
-
-       return false
+       return t.Size() == int64(PtrSize) && PtrDataSize(t) == int64(PtrSize)
 }
 
 // IsInterfaceMethod reports whether (field) m is
index 1920a8a37fb25530f64d63a8923d524b8d713689..9f783a34c13346cce4e91b383af528d994834237 100644 (file)
@@ -121,8 +121,8 @@ const (
        TFlagGCMaskOnDemand TFlag = 1 << 4
 
        // TFlagDirectIface means that a value of this type is stored directly
-       // in the data field of an interface, instead of indirectly. Normally
-       // this means the type is pointer-ish.
+       // in the data field of an interface, instead of indirectly.
+       // This flag is just a cached computation of Size_ == PtrBytes == goarch.PtrSize.
        TFlagDirectIface TFlag = 1 << 5
 
        // Leaving this breadcrumb behind for dlv. It should not be used, and no
index cec8662c01a28d687e304c09736bb3faab53821e..19a28abfcfef1694cc9df49c2eaa1efa9b8f12a6 100644 (file)
@@ -2524,8 +2524,7 @@ func StructOf(fields []StructField) Type {
        }
 
        switch {
-       case len(fs) == 1 && fs[0].Typ.IsDirectIface():
-               // structs of 1 direct iface type can be direct
+       case typ.Size_ == goarch.PtrSize && typ.PtrBytes == goarch.PtrSize:
                typ.TFlag |= abi.TFlagDirectIface
        default:
                typ.TFlag &^= abi.TFlagDirectIface
@@ -2694,8 +2693,7 @@ func ArrayOf(length int, elem Type) Type {
        }
 
        switch {
-       case length == 1 && typ.IsDirectIface():
-               // array of 1 direct iface type can be direct
+       case array.Size_ == goarch.PtrSize && array.PtrBytes == goarch.PtrSize:
                array.TFlag |= abi.TFlagDirectIface
        default:
                array.TFlag &^= abi.TFlagDirectIface