Source file src/cmd/compile/internal/ssa/_gen/allocators.go

     1  // Copyright 2022 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package main
     6  
     7  // TODO: should we share backing storage for similarly-shaped types?
     8  // e.g. []*Value and []*Block, or even []int32 and []bool.
     9  
    10  import (
    11  	"bytes"
    12  	"fmt"
    13  	"go/format"
    14  	"io"
    15  	"log"
    16  	"os"
    17  )
    18  
    19  type allocator struct {
    20  	name     string // name for alloc/free functions
    21  	typ      string // the type they return/accept
    22  	mak      string // code to make a new object (takes power-of-2 size as fmt arg)
    23  	capacity string // code to calculate the capacity of an object. Should always report a power of 2.
    24  	resize   string // code to shrink to sub-power-of-two size (takes size as fmt arg)
    25  	clear    string // code for clearing object before putting it on the free list
    26  	minLog   int    // log_2 of minimum allocation size
    27  	maxLog   int    // log_2 of maximum allocation size
    28  }
    29  
    30  type derived struct {
    31  	name string // name for alloc/free functions
    32  	typ  string // the type they return/accept
    33  	base string // underlying allocator
    34  }
    35  
    36  func genAllocators() {
    37  	allocators := []allocator{
    38  		{
    39  			name:     "ValueSlice",
    40  			typ:      "[]*Value",
    41  			capacity: "cap(%s)",
    42  			mak:      "make([]*Value, %s)",
    43  			resize:   "%s[:%s]",
    44  			clear:    "clear(%s)",
    45  			minLog:   5,
    46  			maxLog:   32,
    47  		},
    48  		{
    49  			name:     "LimitSlice",
    50  			typ:      "[]Limit", // the limit type is basically [4]uint64.
    51  			capacity: "cap(%s)",
    52  			mak:      "make([]Limit, %s)",
    53  			resize:   "%s[:%s]",
    54  			clear:    "clear(%s)",
    55  			minLog:   3,
    56  			maxLog:   30,
    57  		},
    58  		{
    59  			name:     "SparseSet",
    60  			typ:      "*SparseSet",
    61  			capacity: "%s.cap()",
    62  			mak:      "NewSparseSet(%s)",
    63  			resize:   "", // larger-sized sparse sets are ok
    64  			clear:    "%s.Clear()",
    65  			minLog:   5,
    66  			maxLog:   32,
    67  		},
    68  		{
    69  			name:     "SparseMap",
    70  			typ:      "*SparseMap",
    71  			capacity: "%s.cap()",
    72  			mak:      "NewSparseMap(%s)",
    73  			resize:   "", // larger-sized sparse maps are ok
    74  			clear:    "%s.Clear()",
    75  			minLog:   5,
    76  			maxLog:   32,
    77  		},
    78  		{
    79  			name:     "SparseMapPos",
    80  			typ:      "*SparseMapPos",
    81  			capacity: "%s.cap()",
    82  			mak:      "newSparseMapPos(%s)",
    83  			resize:   "", // larger-sized sparse maps are ok
    84  			clear:    "%s.Clear()",
    85  			minLog:   5,
    86  			maxLog:   32,
    87  		},
    88  		{
    89  			name:     "SparseMapValue",
    90  			typ:      "*SparseMapValue",
    91  			capacity: "%s.cap()",
    92  			mak:      "NewSparseMapValue(%s)",
    93  			resize:   "", // larger-sized sparse maps are ok
    94  			clear:    "%s.ClearDense()",
    95  			minLog:   5,
    96  			maxLog:   32,
    97  		},
    98  	}
    99  	deriveds := []derived{
   100  		{
   101  			name: "BlockSlice",
   102  			typ:  "[]*Block",
   103  			base: "ValueSlice",
   104  		},
   105  		{
   106  			name: "Int64",
   107  			typ:  "[]int64",
   108  			base: "LimitSlice",
   109  		},
   110  		{
   111  			name: "IntSlice",
   112  			typ:  "[]int",
   113  			base: "LimitSlice",
   114  		},
   115  		{
   116  			name: "Int32Slice",
   117  			typ:  "[]int32",
   118  			base: "LimitSlice",
   119  		},
   120  		{
   121  			name: "Int8Slice",
   122  			typ:  "[]int8",
   123  			base: "LimitSlice",
   124  		},
   125  		{
   126  			name: "BoolSlice",
   127  			typ:  "[]bool",
   128  			base: "LimitSlice",
   129  		},
   130  		{
   131  			name: "IDSlice",
   132  			typ:  "[]ID",
   133  			base: "LimitSlice",
   134  		},
   135  		{
   136  			name: "UintSlice",
   137  			typ:  "[]uint",
   138  			base: "LimitSlice",
   139  		},
   140  		{
   141  			name: "KnownBitsEntriesSlice",
   142  			typ:  "[]knownBitsEntry",
   143  			base: "LimitSlice",
   144  		},
   145  	}
   146  
   147  	w := new(bytes.Buffer)
   148  	fmt.Fprintf(w, "// Code generated from _gen/allocators.go using 'go generate'; DO NOT EDIT.\n")
   149  	fmt.Fprintln(w)
   150  	fmt.Fprintf(w, "package %s\n", splitCorePkg)
   151  
   152  	fmt.Fprintln(w, "import (")
   153  	fmt.Fprintln(w, "\"internal/unsafeheader\"")
   154  	fmt.Fprintln(w, "\"math/bits\"")
   155  	fmt.Fprintln(w, "\"sync\"")
   156  	fmt.Fprintln(w, "\"unsafe\"")
   157  	fmt.Fprintln(w, ")")
   158  	for _, a := range allocators {
   159  		genAllocator(w, a)
   160  	}
   161  	for _, d := range deriveds {
   162  		for _, base := range allocators {
   163  			if base.name == d.base {
   164  				genDerived(w, d, base)
   165  				break
   166  			}
   167  		}
   168  	}
   169  	// gofmt result
   170  	b := w.Bytes()
   171  	var err error
   172  	b, err = format.Source(b)
   173  	if err != nil {
   174  		fmt.Printf("%s\n", w.Bytes())
   175  		panic(err)
   176  	}
   177  
   178  	mkdirOutFile(allocatorsFile)
   179  	if err := os.WriteFile(outFile(allocatorsFile), b, 0666); err != nil {
   180  		log.Fatalf("can't write output: %v\n", err)
   181  	}
   182  }
   183  func genAllocator(w io.Writer, a allocator) {
   184  	fmt.Fprintf(w, "var poolFree%s [%d]sync.Pool\n", a.name, a.maxLog-a.minLog)
   185  	fmt.Fprintf(w, "func (c *Cache) %s%s(n int) %s {\n", simpleTitle("alloc"), a.name, a.typ)
   186  	fmt.Fprintf(w, "var s %s\n", a.typ)
   187  	fmt.Fprintf(w, "n2 := n\n")
   188  	fmt.Fprintf(w, "if n2 < %d { n2 = %d }\n", 1<<a.minLog, 1<<a.minLog)
   189  	fmt.Fprintf(w, "b := bits.Len(uint(n2-1))\n")
   190  	fmt.Fprintf(w, "v := poolFree%s[b-%d].Get()\n", a.name, a.minLog)
   191  	fmt.Fprintf(w, "if v == nil {\n")
   192  	fmt.Fprintf(w, "  s = %s\n", fmt.Sprintf(a.mak, "1<<b"))
   193  	fmt.Fprintf(w, "} else {\n")
   194  	if a.typ[0] == '*' {
   195  		fmt.Fprintf(w, "s = v.(%s)\n", a.typ)
   196  	} else {
   197  		fmt.Fprintf(w, "sp := v.(*%s)\n", a.typ)
   198  		fmt.Fprintf(w, "s = *sp\n")
   199  		fmt.Fprintf(w, "*sp = nil\n")
   200  		fmt.Fprintf(w, "c.hdr%s = append(c.hdr%s, sp)\n", a.name, a.name)
   201  	}
   202  	fmt.Fprintf(w, "}\n")
   203  	if a.resize != "" {
   204  		fmt.Fprintf(w, "s = %s\n", fmt.Sprintf(a.resize, "s", "n"))
   205  	}
   206  	fmt.Fprintf(w, "return s\n")
   207  	fmt.Fprintf(w, "}\n")
   208  	fmt.Fprintf(w, "func (c *Cache) %s%s(s %s) {\n", simpleTitle("free"), a.name, a.typ)
   209  	fmt.Fprintf(w, "%s\n", fmt.Sprintf(a.clear, "s"))
   210  	fmt.Fprintf(w, "b := bits.Len(uint(%s) - 1)\n", fmt.Sprintf(a.capacity, "s"))
   211  	if a.typ[0] == '*' {
   212  		fmt.Fprintf(w, "poolFree%s[b-%d].Put(s)\n", a.name, a.minLog)
   213  	} else {
   214  		fmt.Fprintf(w, "var sp *%s\n", a.typ)
   215  		fmt.Fprintf(w, "if len(c.hdr%s) == 0 {\n", a.name)
   216  		fmt.Fprintf(w, "  sp = new(%s)\n", a.typ)
   217  		fmt.Fprintf(w, "} else {\n")
   218  		fmt.Fprintf(w, "  sp = c.hdr%s[len(c.hdr%s)-1]\n", a.name, a.name)
   219  		fmt.Fprintf(w, "  c.hdr%s[len(c.hdr%s)-1] = nil\n", a.name, a.name)
   220  		fmt.Fprintf(w, "  c.hdr%s = c.hdr%s[:len(c.hdr%s)-1]\n", a.name, a.name, a.name)
   221  		fmt.Fprintf(w, "}\n")
   222  		fmt.Fprintf(w, "*sp = s\n")
   223  		fmt.Fprintf(w, "poolFree%s[b-%d].Put(sp)\n", a.name, a.minLog)
   224  	}
   225  	fmt.Fprintf(w, "}\n")
   226  }
   227  func genDerived(w io.Writer, d derived, base allocator) {
   228  	fmt.Fprintf(w, "func (c *Cache) %s%s(n int) %s {\n", simpleTitle("alloc"), d.name, d.typ)
   229  	if d.typ[:2] != "[]" || base.typ[:2] != "[]" {
   230  		panic(fmt.Sprintf("bad derived types: %s %s", d.typ, base.typ))
   231  	}
   232  	fmt.Fprintf(w, "var base %s\n", base.typ[2:])
   233  	fmt.Fprintf(w, "var derived %s\n", d.typ[2:])
   234  	fmt.Fprintf(w, "if unsafe.Sizeof(base)%%unsafe.Sizeof(derived) != 0 { panic(\"bad\") }\n")
   235  	fmt.Fprintf(w, "scale := unsafe.Sizeof(base)/unsafe.Sizeof(derived)\n")
   236  	fmt.Fprintf(w, "b := c.%s%s(int((uintptr(n)+scale-1)/scale))\n", simpleTitle("alloc"), base.name)
   237  	fmt.Fprintf(w, "s := unsafeheader.Slice {\n")
   238  	fmt.Fprintf(w, "  Data: unsafe.Pointer(&b[0]),\n")
   239  	fmt.Fprintf(w, "  Len: n,\n")
   240  	fmt.Fprintf(w, "  Cap: cap(b)*int(scale),\n")
   241  	fmt.Fprintf(w, "  }\n")
   242  	fmt.Fprintf(w, "return *(*%s)(unsafe.Pointer(&s))\n", d.typ)
   243  	fmt.Fprintf(w, "}\n")
   244  	fmt.Fprintf(w, "func (c *Cache) %s%s(s %s) {\n", simpleTitle("free"), d.name, d.typ)
   245  	fmt.Fprintf(w, "var base %s\n", base.typ[2:])
   246  	fmt.Fprintf(w, "var derived %s\n", d.typ[2:])
   247  	fmt.Fprintf(w, "scale := unsafe.Sizeof(base)/unsafe.Sizeof(derived)\n")
   248  	fmt.Fprintf(w, "b := unsafeheader.Slice {\n")
   249  	fmt.Fprintf(w, "  Data: unsafe.Pointer(&s[0]),\n")
   250  	fmt.Fprintf(w, "  Len: int((uintptr(len(s))+scale-1)/scale),\n")
   251  	fmt.Fprintf(w, "  Cap: int((uintptr(cap(s))+scale-1)/scale),\n")
   252  	fmt.Fprintf(w, "  }\n")
   253  	fmt.Fprintf(w, "c.%s%s(*(*%s)(unsafe.Pointer(&b)))\n", simpleTitle("free"), base.name, base.typ)
   254  	fmt.Fprintf(w, "}\n")
   255  }
   256  

View as plain text