]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: optimize loads from abi.Type.{Size_,PtrBytes,Kind_}
authorJake Bailey <jacob.b.bailey@gmail.com>
Fri, 5 Sep 2025 20:36:47 +0000 (13:36 -0700)
committerGopher Robot <gobot@golang.org>
Mon, 8 Sep 2025 20:34:06 +0000 (13:34 -0700)
With the previous CL in place, we can now pretty easily optimize a few
more loads from abi.Type. I've done Size_, PtrBytes, and Kind_, which
are easily calculated.

Among std/cmd, this rule fires a number of times:

     75 abi.Type field Kind_
     50 abi.PtrType field Elem
     14 abi.Type field Hash
      4 abi.Type field Size_
      2 abi.Type field PtrBytes

The other ones that show up when compiling std/cmd are TFlag and GCData,
but these are not trivially calculated. Doing TFlag would probably be a
decent help given it's often used in things like switches where
statically knowing the kind could eliminate a bunch of dead code.

Change-Id: Ic7fd2113fa7479af914d06916edbca60cc71819f
Reviewed-on: https://go-review.googlesource.com/c/go/+/701298
Reviewed-by: Mark Freeman <markfreeman@google.com>
Reviewed-by: Keith Randall <khr@google.com>
Auto-Submit: Keith Randall <khr@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>

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

index c561d527a7d1616f623088426c7453d52a870a1a..2849d4ee40f4af8300cfdbc7ca87e3109f42a809 100644 (file)
@@ -414,6 +414,10 @@ var kinds = []abi.Kind{
        types.TUNSAFEPTR:  abi.UnsafePointer,
 }
 
+func ABIKindOfType(t *types.Type) abi.Kind {
+       return kinds[t.Kind()]
+}
+
 var (
        memhashvarlen  *obj.LSym
        memequalvarlen *obj.LSym
@@ -512,8 +516,7 @@ func dcommontype(c rttype.Cursor, t *types.Type) {
        c.Field("Align_").WriteUint8(uint8(t.Alignment()))
        c.Field("FieldAlign_").WriteUint8(uint8(t.Alignment()))
 
-       kind := kinds[t.Kind()]
-       c.Field("Kind_").WriteUint8(uint8(kind))
+       c.Field("Kind_").WriteUint8(uint8(ABIKindOfType(t)))
 
        c.Field("Equal").WritePtr(eqfunc)
        c.Field("GCData").WritePtr(gcsym)
index bc66c91a35db8635a5f798fc23a3da9f8ea243d0..576f25a497d8538ebed7ce97f847497767114afc 100644 (file)
@@ -2005,7 +2005,7 @@ func isFixedLoad(v *Value, sym Sym, off int64) bool {
                for _, f := range rttype.Type.Fields() {
                        if f.Offset == off && copyCompatibleType(v.Type, f.Type) {
                                switch f.Sym.Name {
-                               case "Hash":
+                               case "Size_", "PtrBytes", "Hash", "Kind_":
                                        return true
                                default:
                                        // fmt.Println("unknown field", f.Sym.Name)
@@ -2061,13 +2061,30 @@ func rewriteFixedLoad(v *Value, sym Sym, sb *Value, off int64) *Value {
 
                t := (*lsym.Extra).(*obj.TypeInfo).Type.(*types.Type)
 
+               ptrSizedOpConst := OpConst64
+               if f.Config.PtrSize == 4 {
+                       ptrSizedOpConst = OpConst32
+               }
+
                for _, f := range rttype.Type.Fields() {
                        if f.Offset == off && copyCompatibleType(v.Type, f.Type) {
                                switch f.Sym.Name {
+                               case "Size_":
+                                       v.reset(ptrSizedOpConst)
+                                       v.AuxInt = int64(t.Size())
+                                       return v
+                               case "PtrBytes":
+                                       v.reset(ptrSizedOpConst)
+                                       v.AuxInt = int64(types.PtrDataSize(t))
+                                       return v
                                case "Hash":
                                        v.reset(OpConst32)
                                        v.AuxInt = int64(types.TypeHash(t))
                                        return v
+                               case "Kind_":
+                                       v.reset(OpConst8)
+                                       v.AuxInt = int64(reflectdata.ABIKindOfType(t))
+                                       return v
                                default:
                                        base.Fatalf("unknown field %s for fixedLoad of %s at offset %d", f.Sym.Name, lsym.Name, off)
                                }