Add slice base.u8 peek/poke methods

wuffs_png_decode_filter_1_sub/clang9  4.59GB/s ± 0%   4.59GB/s ± 0%      ~     (p=0.151 n=5+5)
wuffs_png_decode_filter_1_sub/gcc10   1.85GB/s ± 0%   4.11GB/s ± 0%  +122.24%  (p=0.016 n=5+4)
This commit is contained in:
Nigel Tao committed 2021-01-17 23:47:37 +11:00
1 parent 602b711ba6
commit 26a7280900
13 files changed
+204 -48

No files matched your search

+39 -15
View File
@@ -1051,6 +1051,8 @@ func (q *checker) bcheckExprCallSpecialCases(n *a.Expr, depth uint32) (bounds, e
recv := lhs.LHS().AsExpr()
method := lhs.Ident()
advance, advanceExpr, update := (*big.Int)(nil), (*a.Expr)(nil), false
if recvTyp := recv.MType(); recvTyp == nil {
return bounds{}, errNotASpecialCase
@@ -1094,8 +1096,6 @@ func (q *checker) bcheckExprCallSpecialCases(n *a.Expr, depth uint32) (bounds, e
}
} else if recvTyp.IsIOTokenType() {
advance, advanceExpr, update := (*big.Int)(nil), (*a.Expr)(nil), false
if method == t.IDUndoByte {
if err := q.canUndoByte(recv); err != nil {
return bounds{}, err
@@ -1146,23 +1146,31 @@ func (q *checker) bcheckExprCallSpecialCases(n *a.Expr, depth uint32) (bounds, e
}
}
if (advance != nil) || (advanceExpr != nil) {
if ok, err := q.optimizeIOMethodAdvance(recv, advance, advanceExpr, update); err != nil {
return bounds{}, err
} else if !ok {
adv := ""
if advance != nil {
adv = advance.String()
} else {
adv = advanceExpr.Str(q.tm)
}
return bounds{}, fmt.Errorf("check: could not prove %s pre-condition: %s.length() >= %s",
method.Str(q.tm), recv.Str(q.tm), adv)
} else if recvTyp.Eq(typeExprSliceU8) {
if method >= t.IDPeekU8 {
if m := method - t.IDPeekU8; m < t.ID(len(ioMethodAdvances)) {
au := ioMethodAdvances[m]
advance, update = au.advance, au.update
}
// TODO: drop other recv-related facts?
}
}
if (advance != nil) || (advanceExpr != nil) {
if ok, err := q.optimizeIOMethodAdvance(recv, advance, advanceExpr, update); err != nil {
return bounds{}, err
} else if !ok {
adv := ""
if advance != nil {
adv = advance.String()
} else {
adv = advanceExpr.Str(q.tm)
}
return bounds{}, fmt.Errorf("check: could not prove %s pre-condition: %s.length() >= %s",
method.Str(q.tm), recv.Str(q.tm), adv)
}
// TODO: drop other recv-related facts?
}
return bounds{}, errNotASpecialCase
}
@@ -1319,6 +1327,22 @@ var ioMethodAdvances = [...]struct {
t.IDPeekU64BE - t.IDPeekU8: {eight, false},
t.IDPeekU64LE - t.IDPeekU8: {eight, false},
t.IDPokeU8 - t.IDPeekU8: {one, false},
t.IDPokeU16BE - t.IDPeekU8: {two, false},
t.IDPokeU16LE - t.IDPeekU8: {two, false},
t.IDPokeU24BE - t.IDPeekU8: {three, false},
t.IDPokeU24LE - t.IDPeekU8: {three, false},
t.IDPokeU32BE - t.IDPeekU8: {four, false},
t.IDPokeU32LE - t.IDPeekU8: {four, false},
t.IDPokeU40BE - t.IDPeekU8: {five, false},
t.IDPokeU40LE - t.IDPeekU8: {five, false},
t.IDPokeU48BE - t.IDPeekU8: {six, false},
t.IDPokeU48LE - t.IDPeekU8: {six, false},
t.IDPokeU56BE - t.IDPeekU8: {seven, false},
t.IDPokeU56LE - t.IDPeekU8: {seven, false},
t.IDPokeU64BE - t.IDPeekU8: {eight, false},
t.IDPokeU64LE - t.IDPeekU8: {eight, false},
t.IDWriteU8Fast - t.IDPeekU8: {one, true},
t.IDWriteU16BEFast - t.IDPeekU8: {two, true},
t.IDWriteU16LEFast - t.IDPeekU8: {two, true},
+9 -4
View File
@@ -91,8 +91,9 @@ func Check(tm *t.Map, files []*a.File, resolveUse func(usePath string) ([]byte,
funcs: map[t.QQID]*a.Func{},
localVars: map[t.QQID]typeMap{},
builtInSliceFuncs: map[t.QQID]*a.Func{},
builtInTableFuncs: map[t.QQID]*a.Func{},
builtInSliceFuncs: map[t.QQID]*a.Func{},
builtInSliceU8Funcs: map[t.QQID]*a.Func{},
builtInTableFuncs: map[t.QQID]*a.Func{},
builtInInterfaces: map[t.QID][]t.QQID{},
builtInInterfaceFuncs: map[t.QQID]*a.Func{},
@@ -105,6 +106,9 @@ func Check(tm *t.Map, files []*a.File, resolveUse func(usePath string) ([]byte,
if err := c.parseBuiltInFuncs(c.builtInSliceFuncs, builtin.SliceFuncs); err != nil {
return nil, err
}
if err := c.parseBuiltInFuncs(c.builtInSliceU8Funcs, builtin.SliceU8Funcs); err != nil {
return nil, err
}
if err := c.parseBuiltInFuncs(c.builtInTableFuncs, builtin.TableFuncs); err != nil {
return nil, err
}
@@ -221,8 +225,9 @@ type Checker struct {
funcs map[t.QQID]*a.Func
localVars map[t.QQID]typeMap
builtInSliceFuncs map[t.QQID]*a.Func
builtInTableFuncs map[t.QQID]*a.Func
builtInSliceFuncs map[t.QQID]*a.Func
builtInSliceU8Funcs map[t.QQID]*a.Func
builtInTableFuncs map[t.QQID]*a.Func
builtInInterfaces map[t.QID][]t.QQID
builtInInterfaceFuncs map[t.QQID]*a.Func
+3 -1
View File
@@ -58,7 +58,9 @@ func (q *checker) optimizeIOMethodAdvance(receiver *a.Expr, advance *big.Int, ad
// receiver.length(), even if they aren't an exact match.
op := x.Operator()
if op != t.IDXBinaryGreaterEq && op != t.IDXBinaryGreaterThan {
if (op != t.IDXBinaryGreaterEq) &&
(op != t.IDXBinaryGreaterThan) &&
(op != t.IDXBinaryEqEq) {
return x, nil
}
+5
View File
@@ -154,6 +154,11 @@ func (c *Checker) resolveFunc(typ *a.TypeExpr) (*a.Func, error) {
if f := c.builtInSliceFuncs[qqid]; f != nil {
return f, nil
}
if lTyp.Eq(typeExprSliceU8) {
if f := c.builtInSliceU8Funcs[qqid]; f != nil {
return f, nil
}
}
} else if lTyp.IsTableType() {
qqid[0] = t.IDBase
+9 -8
View File
@@ -665,20 +665,21 @@ func (q *checker) tcheckDot(n *a.Expr, depth uint32) error {
if lTyp.IsSliceType() {
qqid[0] = t.IDBase
qqid[1] = t.IDDagger1
if q.c.builtInSliceFuncs[qqid] == nil {
return fmt.Errorf("check: no slice method %q", n.Ident().Str(q.tm))
if (q.c.builtInSliceFuncs[qqid] != nil) ||
((q.c.builtInSliceU8Funcs[qqid] != nil) && lTyp.Eq(typeExprSliceU8)) {
n.SetMType(a.NewTypeExpr(t.IDFunc, 0, n.Ident(), lTyp.AsNode(), nil, nil))
return nil
}
n.SetMType(a.NewTypeExpr(t.IDFunc, 0, n.Ident(), lTyp.AsNode(), nil, nil))
return nil
return fmt.Errorf("check: no slice method %q", n.Ident().Str(q.tm))
} else if lTyp.IsTableType() {
qqid[0] = t.IDBase
qqid[1] = t.IDDagger2
if q.c.builtInTableFuncs[qqid] == nil {
return fmt.Errorf("check: no table method %q", n.Ident().Str(q.tm))
if q.c.builtInTableFuncs[qqid] != nil {
n.SetMType(a.NewTypeExpr(t.IDFunc, 0, n.Ident(), lTyp.AsNode(), nil, nil))
return nil
}
n.SetMType(a.NewTypeExpr(t.IDFunc, 0, n.Ident(), lTyp.AsNode(), nil, nil))
return nil
return fmt.Errorf("check: no table method %q", n.Ident().Str(q.tm))
} else if lTyp.Decorator() != 0 {
return fmt.Errorf("check: invalid type %q for dot-expression LHS %q", lTyp.Str(q.tm), lhs.Str(q.tm))