tls: add gc-aware pthread slots
This commit is contained in:
93
ssa/di.go
93
ssa/di.go
@@ -53,6 +53,96 @@ func newDIBuilder(prog Program, pkg Package, positioner Positioner) diBuilder {
|
||||
return b
|
||||
}
|
||||
|
||||
func hasTypeParam(typ types.Type) bool {
|
||||
visited := make(map[types.Type]bool)
|
||||
var visit func(types.Type) bool
|
||||
visit = func(tt types.Type) bool {
|
||||
if tt == nil {
|
||||
return false
|
||||
}
|
||||
if visited[tt] {
|
||||
return false
|
||||
}
|
||||
visited[tt] = true
|
||||
switch t := tt.(type) {
|
||||
case *types.TypeParam:
|
||||
return true
|
||||
case *types.Named:
|
||||
if tp := t.TypeParams(); tp != nil && tp.Len() > 0 {
|
||||
if ta := t.TypeArgs(); ta == nil || ta.Len() == 0 {
|
||||
return true
|
||||
}
|
||||
}
|
||||
if ta := t.TypeArgs(); ta != nil {
|
||||
for i := 0; i < ta.Len(); i++ {
|
||||
if visit(ta.At(i)) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
return visit(t.Underlying())
|
||||
case *types.Pointer:
|
||||
return visit(t.Elem())
|
||||
case *types.Slice:
|
||||
return visit(t.Elem())
|
||||
case *types.Array:
|
||||
return visit(t.Elem())
|
||||
case *types.Map:
|
||||
return visit(t.Key()) || visit(t.Elem())
|
||||
case *types.Chan:
|
||||
return visit(t.Elem())
|
||||
case *types.Signature:
|
||||
if tp := t.TypeParams(); tp != nil && tp.Len() > 0 {
|
||||
return true
|
||||
}
|
||||
if params := t.Params(); params != nil {
|
||||
for i := 0; i < params.Len(); i++ {
|
||||
if visit(params.At(i).Type()) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
if results := t.Results(); results != nil {
|
||||
for i := 0; i < results.Len(); i++ {
|
||||
if visit(results.At(i).Type()) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
case *types.Tuple:
|
||||
for i := 0; i < t.Len(); i++ {
|
||||
if visit(t.At(i).Type()) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
case *types.Struct:
|
||||
for i := 0; i < t.NumFields(); i++ {
|
||||
if visit(t.Field(i).Type()) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
case *types.Interface:
|
||||
for i := 0; i < t.NumMethods(); i++ {
|
||||
if visit(t.Method(i).Type()) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
for i := 0; i < t.NumEmbeddeds(); i++ {
|
||||
if visit(t.EmbeddedType(i)) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
return visit(typ)
|
||||
}
|
||||
|
||||
// New method to add named metadata operand
|
||||
func (b diBuilder) addNamedMetadataOperand(name string, intValue int, stringValue string, intValue2 int) {
|
||||
ctx := b.m.Context()
|
||||
@@ -522,6 +612,9 @@ func (b diBuilder) dbgValue(v Expr, dv DIVar, scope DIScope, pos token.Position,
|
||||
}
|
||||
|
||||
func (b diBuilder) diType(t Type, pos token.Position) DIType {
|
||||
if hasTypeParam(t.RawType()) {
|
||||
return &aDIType{}
|
||||
}
|
||||
name := t.RawType().String()
|
||||
return b.diTypeEx(name, t, pos)
|
||||
}
|
||||
|
||||
124
ssa/di_test.go
Normal file
124
ssa/di_test.go
Normal file
@@ -0,0 +1,124 @@
|
||||
//go:build !llgo
|
||||
|
||||
package ssa
|
||||
|
||||
import (
|
||||
"go/token"
|
||||
"go/types"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestHasTypeParam(t *testing.T) {
|
||||
generic := newGenericNamedType("Box")
|
||||
instantiated, err := types.Instantiate(types.NewContext(), generic, []types.Type{types.Typ[types.String]}, true)
|
||||
if err != nil {
|
||||
t.Fatalf("Instantiate: %v", err)
|
||||
}
|
||||
|
||||
arrayType := func() types.Type {
|
||||
tp := newTypeParam("ArrayElem")
|
||||
return types.NewArray(tp, 3)
|
||||
}()
|
||||
|
||||
chanType := types.NewChan(types.SendRecv, newTypeParam("ChanElem"))
|
||||
|
||||
tupleType := func() types.Type {
|
||||
tp := newTypeParam("TupleElem")
|
||||
elem := types.NewVar(token.NoPos, nil, "v", tp)
|
||||
return types.NewTuple(elem)
|
||||
}()
|
||||
|
||||
structWithParam := func() types.Type {
|
||||
tp := newTypeParam("StructElem")
|
||||
field := types.NewVar(token.NoPos, nil, "value", tp)
|
||||
return types.NewStruct([]*types.Var{field}, nil)
|
||||
}()
|
||||
|
||||
signatureWithParam := func() types.Type {
|
||||
tp := newTypeParam("SigParam")
|
||||
params := types.NewTuple(types.NewVar(token.NoPos, nil, "x", tp))
|
||||
return types.NewSignatureType(nil, nil, []*types.TypeParam{tp}, params, types.NewTuple(), false)
|
||||
}()
|
||||
|
||||
signatureWithResult := func() types.Type {
|
||||
tp := newTypeParam("SigResult")
|
||||
results := types.NewTuple(types.NewVar(token.NoPos, nil, "res", tp))
|
||||
return types.NewSignatureType(nil, nil, []*types.TypeParam{tp}, types.NewTuple(), results, false)
|
||||
}()
|
||||
|
||||
interfaceWithParam := func() types.Type {
|
||||
tp := newTypeParam("IfaceParam")
|
||||
params := types.NewTuple(types.NewVar(token.NoPos, nil, "v", tp))
|
||||
method := types.NewFunc(token.NoPos, nil, "Do", types.NewSignatureType(nil, nil, []*types.TypeParam{tp}, params, types.NewTuple(), false))
|
||||
iface := types.NewInterfaceType([]*types.Func{method}, nil)
|
||||
iface.Complete()
|
||||
return iface
|
||||
}()
|
||||
|
||||
interfaceWithEmbed := func() types.Type {
|
||||
base := interfaceWithParam
|
||||
tp := newTypeParam("EmbedParam")
|
||||
embedMethod := types.NewFunc(token.NoPos, nil, "Run", types.NewSignatureType(nil, nil, []*types.TypeParam{tp}, types.NewTuple(), types.NewTuple(), false))
|
||||
iface := types.NewInterfaceType([]*types.Func{embedMethod}, []types.Type{base})
|
||||
iface.Complete()
|
||||
return iface
|
||||
}()
|
||||
|
||||
selfRecursive := func() types.Type {
|
||||
typeName := types.NewTypeName(token.NoPos, nil, "Node", nil)
|
||||
placeholder := types.NewStruct(nil, nil)
|
||||
named := types.NewNamed(typeName, placeholder, nil)
|
||||
field := types.NewVar(token.NoPos, nil, "next", types.NewPointer(named))
|
||||
structType := types.NewStruct([]*types.Var{field}, nil)
|
||||
named.SetUnderlying(structType)
|
||||
return named
|
||||
}()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
typ types.Type
|
||||
want bool
|
||||
}{
|
||||
{"basic", types.Typ[types.Int], false},
|
||||
{"typeParam", newTypeParam("T"), true},
|
||||
{"pointerToTypeParam", types.NewPointer(newTypeParam("PtrT")), true},
|
||||
{"sliceOfTypeParam", types.NewSlice(newTypeParam("SliceT")), true},
|
||||
{"arrayOfTypeParam", arrayType, true},
|
||||
{"mapWithTypeParam", types.NewMap(newTypeParam("MapKey"), types.Typ[types.String]), true},
|
||||
{"chanOfTypeParam", chanType, true},
|
||||
{"tupleWithTypeParam", tupleType, true},
|
||||
{"structWithTypeParam", structWithParam, true},
|
||||
{"signatureWithTypeParam", signatureWithParam, true},
|
||||
{"signatureWithResultTypeParam", signatureWithResult, true},
|
||||
{"interfaceWithTypeParam", interfaceWithParam, true},
|
||||
{"interfaceWithEmbeddedTypeParam", interfaceWithEmbed, true},
|
||||
{"namedGeneric", generic, true},
|
||||
{"pointerToNamedGeneric", types.NewPointer(generic), true},
|
||||
{"namedInstanceNoParam", instantiated, false},
|
||||
{"selfRecursiveStruct", selfRecursive, false},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
tc := tc
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
if got := hasTypeParam(tc.typ); got != tc.want {
|
||||
t.Fatalf("hasTypeParam(%s) = %v, want %v", tc.name, got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func newTypeParam(name string) *types.TypeParam {
|
||||
iface := types.NewInterfaceType(nil, nil)
|
||||
iface.Complete()
|
||||
return types.NewTypeParam(types.NewTypeName(token.NoPos, nil, name, nil), iface)
|
||||
}
|
||||
|
||||
func newGenericNamedType(name string) *types.Named {
|
||||
tp := newTypeParam("T")
|
||||
field := types.NewVar(token.NoPos, nil, "value", tp)
|
||||
structType := types.NewStruct([]*types.Var{field}, nil)
|
||||
named := types.NewNamed(types.NewTypeName(token.NoPos, nil, name, nil), structType, nil)
|
||||
named.SetTypeParams([]*types.TypeParam{tp})
|
||||
return named
|
||||
}
|
||||
Reference in New Issue
Block a user