runtime: map; llgo/ssa: MapUpdate

This commit is contained in:
xushiwei
2024-06-14 21:57:34 +08:00
parent 7a54967bee
commit 47b20b01d0
12 changed files with 491 additions and 46 deletions

2
c/c.go
View File

@@ -46,7 +46,7 @@ type integer interface {
func Str(string) *Char
// llgo:link Advance llgo.advance
func Advance[PtrT any](ptr PtrT, offset int) PtrT { return ptr }
func Advance[PtrT any, I integer](ptr PtrT, offset I) PtrT { return ptr }
// llgo:link Index llgo.index
func Index[T any, I integer](ptr *T, offset I) T { return *ptr }

368
internal/runtime/alg.go Normal file
View File

@@ -0,0 +1,368 @@
// Copyright 2014 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package runtime
import (
"unsafe"
"github.com/goplus/llgo/internal/abi"
"github.com/goplus/llgo/internal/runtime/c"
)
const (
c0 = uintptr((8-goarchPtrSize)/4*2860486313 + (goarchPtrSize-4)/4*33054211828000289)
c1 = uintptr((8-goarchPtrSize)/4*3267000013 + (goarchPtrSize-4)/4*23344194077549503)
)
/*
func memhash0(p unsafe.Pointer, h uintptr) uintptr {
return h
}
func memhash8(p unsafe.Pointer, h uintptr) uintptr {
return memhash(p, h, 1)
}
func memhash16(p unsafe.Pointer, h uintptr) uintptr {
return memhash(p, h, 2)
}
func memhash128(p unsafe.Pointer, h uintptr) uintptr {
return memhash(p, h, 16)
}
/*
//go:nosplit
func memhash_varlen(p unsafe.Pointer, h uintptr) uintptr {
ptr := getclosureptr()
size := *(*uintptr)(unsafe.Pointer(ptr + unsafe.Sizeof(h)))
return memhash(p, h, size)
}
*/
func memhash(p unsafe.Pointer, h, s uintptr) uintptr {
h ^= c0
for s > 0 {
s--
h = h*c1 + uintptr(*(*uint8)(c.Advance(p, s)))
}
return h
}
func memhash32(p unsafe.Pointer, h uintptr) uintptr {
return (h^c0)*c1 + uintptr(*(*uint32)(p))
}
func memhash64(p unsafe.Pointer, h uintptr) uintptr {
return (h^c0)*c1 + uintptr(*(*uint64)(p))
}
func strhash(p unsafe.Pointer, h uintptr) uintptr {
x := (*String)(p)
return memhash(x.data, h, uintptr(x.len))
}
// NOTE: Because NaN != NaN, a map can contain any
// number of (mostly useless) entries keyed with NaNs.
// To avoid long hash chains, we assign a random number
// as the hash value for a NaN.
func f32hash(p unsafe.Pointer, h uintptr) uintptr {
f := *(*float32)(p)
switch {
case f == 0:
return c1 * (c0 ^ h) // +0, -0
case f != f:
return c1 * (c0 ^ h ^ uintptr(fastrand())) // any kind of NaN
default:
return memhash(p, h, 4)
}
}
func f64hash(p unsafe.Pointer, h uintptr) uintptr {
f := *(*float64)(p)
switch {
case f == 0:
return c1 * (c0 ^ h) // +0, -0
case f != f:
return c1 * (c0 ^ h ^ uintptr(fastrand())) // any kind of NaN
default:
return memhash(p, h, 8)
}
}
func c64hash(p unsafe.Pointer, h uintptr) uintptr {
x := (*[2]float32)(p)
return f32hash(unsafe.Pointer(&x[1]), f32hash(unsafe.Pointer(&x[0]), h))
}
func c128hash(p unsafe.Pointer, h uintptr) uintptr {
x := (*[2]float64)(p)
return f64hash(unsafe.Pointer(&x[1]), f64hash(unsafe.Pointer(&x[0]), h))
}
func interhash(p unsafe.Pointer, h uintptr) uintptr {
a := (*iface)(p)
tab := a.tab
if tab == nil {
return h
}
t := tab._type
if t.Equal == nil {
// Check hashability here. We could do this check inside
// typehash, but we want to report the topmost type in
// the error text (e.g. in a struct with a field of slice type
// we want to report the struct, not the slice).
panic(errorString("hash of unhashable type " + t.Name()))
}
if isDirectIface(t) {
return c1 * typehash(t, unsafe.Pointer(&a.data), h^c0)
} else {
return c1 * typehash(t, a.data, h^c0)
}
}
func nilinterhash(p unsafe.Pointer, h uintptr) uintptr {
a := (*eface)(p)
t := a._type
if t == nil {
return h
}
if t.Equal == nil {
// See comment in interhash above.
panic(errorString("hash of unhashable type " + t.Name()))
}
if isDirectIface(t) {
return c1 * typehash(t, unsafe.Pointer(&a.data), h^c0)
} else {
return c1 * typehash(t, a.data, h^c0)
}
}
// typehash computes the hash of the object of type t at address p.
// h is the seed.
// This function is seldom used. Most maps use for hashing either
// fixed functions (e.g. f32hash) or compiler-generated functions
// (e.g. for a type like struct { x, y string }). This implementation
// is slower but more general and is used for hashing interface types
// (called from interhash or nilinterhash, above) or for hashing in
// maps generated by reflect.MapOf (reflect_typehash, below).
// Note: this function must match the compiler generated
// functions exactly. See issue 37716.
func typehash(t *_type, p unsafe.Pointer, h uintptr) uintptr {
if t.TFlag&abi.TFlagRegularMemory != 0 {
// Handle ptr sizes specially, see issue 37086.
switch t.Size_ {
case 4:
return memhash32(p, h)
case 8:
return memhash64(p, h)
default:
return memhash(p, h, t.Size_)
}
}
switch t.Kind() {
case abi.Float32:
return f32hash(p, h)
case abi.Float64:
return f64hash(p, h)
case abi.Complex64:
return c64hash(p, h)
case abi.Complex128:
return c128hash(p, h)
case abi.String:
return strhash(p, h)
case abi.Interface:
i := (*interfacetype)(unsafe.Pointer(t))
if len(i.Methods) == 0 {
return nilinterhash(p, h)
}
return interhash(p, h)
case abi.Array:
a := (*abi.ArrayType)(unsafe.Pointer(t))
for i := uintptr(0); i < a.Len; i++ {
h = typehash(a.Elem, add(p, i*a.Elem.Size_), h)
}
return h
case abi.Struct:
s := (*abi.StructType)(unsafe.Pointer(t))
for _, f := range s.Fields {
/* TODO(xsw): skip blank field
if f.Name.IsBlank() {
continue
}
*/
h = typehash(f.Typ, add(p, f.Offset), h)
}
return h
default:
// Should never happen, as typehash should only be called
// with comparable types.
panic(errorString("hash of unhashable type " + t.Name()))
}
}
/*
//go:linkname reflect_typehash reflect.typehash
func reflect_typehash(t *_type, p unsafe.Pointer, h uintptr) uintptr {
return typehash(t, p, h)
}
func memequal0(p, q unsafe.Pointer) bool {
return true
}
func memequal8(p, q unsafe.Pointer) bool {
return *(*int8)(p) == *(*int8)(q)
}
func memequal16(p, q unsafe.Pointer) bool {
return *(*int16)(p) == *(*int16)(q)
}
func memequal32(p, q unsafe.Pointer) bool {
return *(*int32)(p) == *(*int32)(q)
}
func memequal64(p, q unsafe.Pointer) bool {
return *(*int64)(p) == *(*int64)(q)
}
func memequal128(p, q unsafe.Pointer) bool {
return *(*[2]int64)(p) == *(*[2]int64)(q)
}
func f32equal(p, q unsafe.Pointer) bool {
return *(*float32)(p) == *(*float32)(q)
}
func f64equal(p, q unsafe.Pointer) bool {
return *(*float64)(p) == *(*float64)(q)
}
func c64equal(p, q unsafe.Pointer) bool {
return *(*complex64)(p) == *(*complex64)(q)
}
func c128equal(p, q unsafe.Pointer) bool {
return *(*complex128)(p) == *(*complex128)(q)
}
func strequal(p, q unsafe.Pointer) bool {
return *(*string)(p) == *(*string)(q)
}
func interequal(p, q unsafe.Pointer) bool {
x := *(*iface)(p)
y := *(*iface)(q)
return x.tab == y.tab && ifaceeq(x.tab, x.data, y.data)
}
func nilinterequal(p, q unsafe.Pointer) bool {
x := *(*eface)(p)
y := *(*eface)(q)
return x._type == y._type && efaceeq(x._type, x.data, y.data)
}
func efaceeq(t *_type, x, y unsafe.Pointer) bool {
if t == nil {
return true
}
eq := t.Equal
if eq == nil {
panic(errorString("comparing uncomparable type " + t.Name()))
}
if isDirectIface(t) {
// Direct interface types are ptr, chan, map, func, and single-element structs/arrays thereof.
// Maps and funcs are not comparable, so they can't reach here.
// Ptrs, chans, and single-element items can be compared directly using ==.
return x == y
}
return eq(x, y)
}
func ifaceeq(tab *itab, x, y unsafe.Pointer) bool {
if tab == nil {
return true
}
t := tab._type
eq := t.Equal
if eq == nil {
panic(errorString("comparing uncomparable type " + t.Name()))
}
if isDirectIface(t) {
// See comment in efaceeq.
return x == y
}
return eq(x, y)
}
// Testing adapters for hash quality tests (see hash_test.go)
func stringHash(s string, seed uintptr) uintptr {
return strhash(unsafe.Pointer(&s), seed)
}
func bytesHash(b []byte, seed uintptr) uintptr {
s := (*slice)(unsafe.Pointer(&b))
return memhash(s.data, seed, uintptr(s.len))
}
func int32Hash(i uint32, seed uintptr) uintptr {
return memhash32(unsafe.Pointer(&i), seed)
}
func int64Hash(i uint64, seed uintptr) uintptr {
return memhash64(unsafe.Pointer(&i), seed)
}
func efaceHash(i any, seed uintptr) uintptr {
return nilinterhash(unsafe.Pointer(&i), seed)
}
func ifaceHash(i interface {
F()
}, seed uintptr) uintptr {
return interhash(unsafe.Pointer(&i), seed)
}
/*
const hashRandomBytes = goarch.PtrSize / 4 * 64
// used in asm_{386,amd64,arm64}.s to seed the hash function
var aeskeysched [hashRandomBytes]byte
// used in hash{32,64}.go to seed the hash function
var hashkey [4]uintptr
func alginit() {
// Install AES hash algorithms if the instructions needed are present.
if (GOARCH == "386" || GOARCH == "amd64") &&
cpu.X86.HasAES && // AESENC
cpu.X86.HasSSSE3 && // PSHUFB
cpu.X86.HasSSE41 { // PINSR{D,Q}
initAlgAES()
return
}
if GOARCH == "arm64" && cpu.ARM64.HasAES {
initAlgAES()
return
}
getRandomData((*[len(hashkey) * goarch.PtrSize]byte)(unsafe.Pointer(&hashkey))[:])
hashkey[0] |= 1 // make sure these numbers are odd
hashkey[1] |= 1
hashkey[2] |= 1
hashkey[3] |= 1
}
func initAlgAES() {
useAeshash = true
// Initialize with random data so hash collisions will be hard to engineer.
getRandomData(aeskeysched[:])
}
// Note: These routines perform the read with a native endianness.
func readUnaligned32(p unsafe.Pointer) uint32 {
q := (*[4]byte)(p)
if goarch.BigEndian {
return uint32(q[3]) | uint32(q[2])<<8 | uint32(q[1])<<16 | uint32(q[0])<<24
}
return uint32(q[0]) | uint32(q[1])<<8 | uint32(q[2])<<16 | uint32(q[3])<<24
}
func readUnaligned64(p unsafe.Pointer) uint64 {
q := (*[8]byte)(p)
if goarch.BigEndian {
return uint64(q[7]) | uint64(q[6])<<8 | uint64(q[5])<<16 | uint64(q[4])<<24 |
uint64(q[3])<<32 | uint64(q[2])<<40 | uint64(q[1])<<48 | uint64(q[0])<<56
}
return uint64(q[0]) | uint64(q[1])<<8 | uint64(q[2])<<16 | uint64(q[3])<<24 | uint64(q[4])<<32 | uint64(q[5])<<40 | uint64(q[6])<<48 | uint64(q[7])<<56
}
*/

View File

@@ -30,11 +30,15 @@ type (
FilePtr = unsafe.Pointer
)
type integer interface {
~int | ~uint | ~uintptr | ~int32 | ~uint32 | ~int64 | ~uint64
}
//go:linkname Str llgo.cstr
func Str(string) *Char
// llgo:link Advance llgo.advance
func Advance[PtrT any](ptr PtrT, offset int) PtrT { return ptr }
func Advance[PtrT any, I integer](ptr PtrT, offset I) PtrT { return ptr }
//go:linkname Alloca llgo.alloca
func Alloca(size uintptr) Pointer

View File

@@ -8,6 +8,10 @@ import (
"unsafe"
)
const (
bigAlloc = 1 << (goarchPtrSize*8 - 6)
)
// implementation of new builtin
// compiler (both frontend and SSA backend) knows the signature
// of this function.

View File

@@ -308,15 +308,14 @@ func makemap_small() *hmap {
return h
}
/*
// makemap implements Go map creation for make(map[k]v, hint).
// If the compiler has determined that the map or the first bucket
// can be created on the stack, h and/or bucket may be non-nil.
// If h != nil, the map can be created directly in h.
// If h.buckets != nil, bucket pointed to can be used as the first bucket.
func makemap(t *maptype, hint int, h *hmap) *hmap {
mem, overflow := math.MulUintptr(uintptr(hint), t.Bucket.Size_)
if overflow || mem > maxAlloc {
mem, overflow := mathMulUintptr(uintptr(hint), t.Bucket.Size_)
if overflow || mem > bigAlloc {
hint = 0
}
@@ -348,7 +347,6 @@ func makemap(t *maptype, hint int, h *hmap) *hmap {
return h
}
*/
// makeBucketArray initializes a backing array for map buckets.
// 1<<b is the minimum number of buckets to allocate.

View File

@@ -193,8 +193,6 @@ func memequal(a, b unsafe.Pointer, size uintptr) bool
// output depends on the input. noescape is inlined and currently
// compiles down to zero instructions.
// USE CAREFULLY!
//
//go:nosplit
func noescape(p unsafe.Pointer) unsafe.Pointer {
x := uintptr(p)
return unsafe.Pointer(x ^ 0)

View File

@@ -1,31 +0,0 @@
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Runtime type representation.
package runtime
/*
import (
"github.com/goplus/llgo/internal/abi"
)
type _type = abi.Type
type maptype = abi.MapType
type arraytype = abi.ArrayType
type chantype = abi.ChanType
type slicetype = abi.SliceType
type functype = abi.FuncType
type ptrtype = abi.PtrType
type name = abi.Name
type structtype = abi.StructType
*/

View File

@@ -25,6 +25,11 @@ import (
type _type = abi.Type
// isDirectIface reports whether t is stored directly in an interface value.
func isDirectIface(t *_type) bool {
return t.Kind_&abi.KindDirectIface != 0
}
type eface struct {
_type *_type
data unsafe.Pointer

View File

@@ -24,13 +24,105 @@ import (
// Map represents a Go map.
type Map = hmap
type MapType = abi.MapType
// MakeSmallMap creates a new small map.
func MakeSmallMap() *Map {
return makemap_small()
}
// Mapassign finds a key in map m and returns the elem address to assign.
func Mapassign(t *abi.MapType, m *Map, key unsafe.Pointer) unsafe.Pointer {
// MakeMap creates a new map.
func MakeMap(t *MapType, hint int, at *Map) *Map {
return makemap(t, hint, at)
}
// MapAssign finds a key in map m and returns the elem address to assign.
func MapAssign(t *MapType, m *Map, key unsafe.Pointer) unsafe.Pointer {
return mapassign(t, m, key)
}
func isReflexive(key *Type) bool {
return true // TODO(xsw): false for float/complex type
}
func hashOf(t *Type) func(key unsafe.Pointer, hash0 uintptr) uintptr {
if t.TFlag&abi.TFlagRegularMemory != 0 {
switch t.Size_ {
case 4:
return memhash32
case 8:
return memhash64
}
return func(key unsafe.Pointer, hash0 uintptr) uintptr {
return memhash(key, hash0, t.Size_)
}
}
switch t.Kind() {
case abi.Float32:
return f32hash
case abi.Float64:
return f64hash
case abi.Complex64:
return c64hash
case abi.Complex128:
return c128hash
case abi.String:
return strhash
case abi.Interface:
i := (*interfacetype)(unsafe.Pointer(t))
if len(i.Methods) == 0 {
return nilinterhash
}
return interhash
}
return func(key unsafe.Pointer, hash0 uintptr) uintptr {
return typehash(t, key, hash0)
}
}
// MapOf creates a new map type.
func MapOf(key, elem *Type) *MapType {
var flags uint32
keySlot, elemSlot := key, elem
ptrTy := Basic(abi.UnsafePointer)
if keySlot.Size_ > 128 {
keySlot = ptrTy
flags |= 1
}
if elemSlot.Size_ > 128 {
elemSlot = ptrTy
flags |= 2
}
if isReflexive(key) {
flags |= 4
}
tophashTy := ArrayOf(bucketCnt, Basic(abi.Uint8))
keysTy := ArrayOf(bucketCnt, keySlot)
elemsTy := ArrayOf(bucketCnt, elemSlot)
tophash := StructField("tophash", tophashTy, 0, "", false)
keys := StructField("keys", keysTy, tophashTy.Size_, "", false)
elems := StructField("elems", elemsTy, keys.Offset+keysTy.Size_, "", false)
overflow := StructField("overflow", ptrTy, elems.Offset+elemsTy.Size_, "", false)
bucket := Struct("", overflow.Offset+ptrTy.Size_, tophash, keys, elems, overflow)
ret := &abi.MapType{
Type: abi.Type{
Size_: unsafe.Sizeof(uintptr(0)),
Hash: uint32(abi.Map),
Kind_: uint8(abi.Map),
},
Key: key,
Elem: elem,
Bucket: bucket,
Hasher: hashOf(key),
KeySize: uint8(keySlot.Size_), // size of key slot
ValueSize: uint8(elemSlot.Size_), // size of elem slot
BucketSize: uint16(bucket.Size_), // size of bucket
Flags: flags,
}
return ret
}

View File

@@ -25,7 +25,9 @@ import (
// -----------------------------------------------------------------------------
// Slice is the runtime representation of a slice.
type Slice struct {
type Slice = slice
type slice struct {
data unsafe.Pointer
len int
cap int

View File

@@ -19,10 +19,10 @@ To run the demos in directory `_demo`, you need to set the `LLGO_LIB_PYTHON` env
export LLGO_LIB_PYTHON=/foo/bar/python3.12
```
For example, `/opt/homebrew/Frameworks/Python.framework/Versions/3.12/libpython3.12.dylib` is a typical python lib location under macOS. So we should set it like this:
For example, `/opt/homebrew/Frameworks/Python.framework/Versions/3.12/lib/libpython3.12.dylib` is a typical python lib location under macOS. So we should set it like this:
```sh
export LLGO_LIB_PYTHON=/opt/homebrew/Frameworks/Python.framework/Versions/3.12/python3.12
export LLGO_LIB_PYTHON=/opt/homebrew/Frameworks/Python.framework/Versions/3.12/lib/python3.12
```
Then you can run the demos in directory `_demo`:

View File

@@ -360,8 +360,13 @@ func (b Builder) MapUpdate(m, k, v Expr) {
if debugInstr {
log.Printf("MapUpdate %v[%v] = %v\n", m.impl, k.impl, v.impl)
}
// TODO(xsw)
// panic("todo")
t := m.Type
if t.kind != vkMap {
panic("TODO: not a map")
}
tabi := b.abiType(t.raw.Type)
ptr := b.InlineCall(b.Pkg.rtFunc("MapAssign"), tabi, m, k)
b.Store(ptr, v) // TODO(xsw): indirect store
}
// -----------------------------------------------------------------------------