Files
wuffs/cmd/wuffs-c/internal/cgen/var.go
T
Nigel Tao 2b6b0acb4f Unconditionally resume local vars
This commit will immediately be followed by a rollback, but it is
committed anyway so that we can refer to these numbers in the git log.

name                             old speed      new speed      delta
wuffs_deflate_decode_1k/clang     145MB/s ± 1%   141MB/s ± 1%   -2.28%  (p=0.000 n=19+19)
wuffs_deflate_decode_10k/clang    222MB/s ± 0%   202MB/s ± 1%   -9.39%  (p=0.000 n=16+18)
wuffs_deflate_decode_100k/clang   256MB/s ± 0%   237MB/s ± 1%   -7.64%  (p=0.000 n=20+20)
wuffs_deflate_decode_1k/gcc       157MB/s ± 2%   158MB/s ± 1%   +0.73%  (p=0.001 n=20+19)
wuffs_deflate_decode_10k/gcc      247MB/s ± 1%   251MB/s ± 0%   +1.72%  (p=0.000 n=19+18)
wuffs_deflate_decode_100k/gcc     289MB/s ± 0%   286MB/s ± 0%   -0.86%  (p=0.000 n=17+18)
wuffs_lzw_decode_20k/clang        121MB/s ± 1%   122MB/s ± 1%   +1.04%  (p=0.000 n=17+19)
wuffs_lzw_decode_100k/clang       163MB/s ± 1%   164MB/s ± 1%   +0.33%  (p=0.010 n=19+18)
wuffs_gif_decode_1k/clang         348MB/s ± 1%   361MB/s ± 2%   +3.62%  (p=0.000 n=20+20)
wuffs_gif_decode_10k/clang        132MB/s ± 1%   130MB/s ± 1%   -1.49%  (p=0.000 n=20+19)
wuffs_gif_decode_100k/clang       114MB/s ± 0%   116MB/s ± 1%   +1.24%  (p=0.000 n=19+20)
wuffs_gif_decode_1000k/clang      117MB/s ± 0%   118MB/s ± 1%   +1.16%  (p=0.000 n=17+20)
wuffs_lzw_decode_20k/gcc          153MB/s ± 1%   162MB/s ± 1%   +6.05%  (p=0.000 n=18+19)
wuffs_lzw_decode_100k/gcc         216MB/s ± 1%   228MB/s ± 0%   +5.29%  (p=0.000 n=20+19)
wuffs_gif_decode_1k/gcc           444MB/s ± 1%   388MB/s ± 1%  -12.62%  (p=0.000 n=19+18)
wuffs_gif_decode_10k/gcc          148MB/s ± 0%   126MB/s ± 0%  -14.50%  (p=0.000 n=19+19)
wuffs_gif_decode_100k/gcc         137MB/s ± 0%   114MB/s ± 1%  -16.69%  (p=0.000 n=19+18)
wuffs_gif_decode_1000k/gcc        139MB/s ± 1%   117MB/s ± 0%  -16.02%  (p=0.000 n=20+17)
wuffs_adler32_10k/clang          2.44GB/s ± 0%  2.44GB/s ± 1%     ~     (p=0.258 n=18+17)
wuffs_adler32_100k/clang         2.45GB/s ± 0%  2.44GB/s ± 0%   -0.11%  (p=0.005 n=17+16)
wuffs_zlib_decode_10k/clang       204MB/s ± 0%   188MB/s ± 0%   -7.82%  (p=0.000 n=17+18)
wuffs_zlib_decode_100k/clang      232MB/s ± 0%   218MB/s ± 0%   -5.90%  (p=0.000 n=19+17)
wuffs_adler32_10k/gcc            3.22GB/s ± 1%  3.25GB/s ± 0%   +1.01%  (p=0.000 n=19+20)
wuffs_adler32_100k/gcc           3.22GB/s ± 0%  3.25GB/s ± 0%   +0.95%  (p=0.000 n=16+18)
wuffs_zlib_decode_10k/gcc         229MB/s ± 1%   233MB/s ± 0%   +1.71%  (p=0.000 n=18+19)
wuffs_zlib_decode_100k/gcc        262MB/s ± 0%   265MB/s ± 0%   +1.04%  (p=0.000 n=17+18)
2018-02-22 11:18:16 +11:00

407 lines
11 KiB
Go

// Copyright 2017 The Wuffs Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cgen
import (
"errors"
"fmt"
a "github.com/google/wuffs/lang/ast"
t "github.com/google/wuffs/lang/token"
)
var errNeedDerivedVar = errors.New("internal: need derived var")
func (g *gen) needDerivedVar(name t.ID) bool {
for _, o := range g.currFunk.astFunc.Body() {
err := o.Walk(func(p *a.Node) error {
// Look for p matching "in.name.etc(etc)".
if p.Kind() != a.KExpr {
return nil
}
q := p.Expr()
if q.Operator().Key() != t.KeyOpenParen {
return nil
}
q = q.LHS().Expr()
if q.Operator().Key() != t.KeyDot {
return nil
}
q = q.LHS().Expr()
if q.Operator().Key() != t.KeyDot || q.Ident() != name {
return nil
}
q = q.LHS().Expr()
if q.Operator() != 0 || q.Ident().Key() != t.KeyIn {
return nil
}
return errNeedDerivedVar
})
if err == errNeedDerivedVar {
return true
}
}
return false
}
func (g *gen) findDerivedVars() {
for _, o := range g.currFunk.astFunc.In().Fields() {
o := o.Field()
oTyp := o.XType()
if oTyp.Decorator() != 0 {
continue
}
switch oTyp.QID() {
default:
continue
case t.QID{0, t.IDReader1}, t.QID{0, t.IDWriter1}:
// No-op.
}
if !g.needDerivedVar(o.Name()) {
continue
}
if g.currFunk.derivedVars == nil {
g.currFunk.derivedVars = map[t.ID]struct{}{}
}
g.currFunk.derivedVars[o.Name()] = struct{}{}
}
}
func (g *gen) writeLoadDerivedVar(b *buffer, name t.ID, typ *a.TypeExpr, header bool) error {
// TODO: remove this hack. We're picking up the wrong name for "src:r,
// dummy:in.src".
if name.Str(g.tm) == "dummy" {
name = g.tm.ByName("src")
}
if g.currFunk.derivedVars == nil {
return nil
}
if _, ok := g.currFunk.derivedVars[name]; !ok {
return nil
}
nameStr := name.Str(g.tm)
switch typ.QID() {
case t.QID{0, t.IDReader1}:
if header {
b.printf("uint8_t* %srptr_%s = NULL;", bPrefix, nameStr)
b.printf("uint8_t* %srstart_%s = NULL;", bPrefix, nameStr)
b.printf("uint8_t* %srend_%s = NULL;", bPrefix, nameStr)
}
b.printf("if (%s%s.buf) {", aPrefix, nameStr)
b.printf("%srptr_%s = %s%s.buf->ptr + %s%s.buf->ri;",
bPrefix, nameStr, aPrefix, nameStr, aPrefix, nameStr)
if header {
b.printf("%srstart_%s = %srptr_%s;", bPrefix, nameStr, bPrefix, nameStr)
b.printf("uint64_t len = %s%s.buf->wi - %s%s.buf->ri;", aPrefix, nameStr, aPrefix, nameStr)
b.printf("wuffs_base__limit1* lim;\n")
b.printf("for (lim = &%s%s.private_impl.limit; lim; lim = lim->next) {\n", aPrefix, nameStr)
b.printf("if (lim->ptr_to_len && (len > *lim->ptr_to_len)) {\n")
b.printf("len = *lim->ptr_to_len;\n")
b.printf("}\n")
b.printf("}\n")
b.printf("%srend_%s = %srptr_%s + len;", bPrefix, nameStr, bPrefix, nameStr)
}
b.printf("}\n")
case t.QID{0, t.IDWriter1}:
if header {
b.printf("uint8_t* %swptr_%s = NULL;", bPrefix, nameStr)
b.printf("uint8_t* %swstart_%s = NULL;", bPrefix, nameStr)
b.printf("uint8_t* %swend_%s = NULL;", bPrefix, nameStr)
}
b.printf("if (%s%s.buf) {", aPrefix, nameStr)
b.printf("%swptr_%s = %s%s.buf->ptr + %s%s.buf->wi;",
bPrefix, nameStr, aPrefix, nameStr, aPrefix, nameStr)
if header {
b.printf("%swstart_%s = %swptr_%s;", bPrefix, nameStr, bPrefix, nameStr)
b.printf("%swend_%s = %swptr_%s;", bPrefix, nameStr, bPrefix, nameStr)
b.printf("if (!%s%s.buf->closed) {", aPrefix, nameStr)
b.printf("uint64_t len = %s%s.buf->len - %s%s.buf->wi;", aPrefix, nameStr, aPrefix, nameStr)
b.printf("wuffs_base__limit1* lim;\n")
b.printf("for (lim = &%s%s.private_impl.limit; lim; lim = lim->next) {\n", aPrefix, nameStr)
b.printf("if (lim->ptr_to_len && (len > *lim->ptr_to_len)) {\n")
b.printf("len = *lim->ptr_to_len;\n")
b.printf("}\n")
b.printf("}\n")
b.printf("%swend_%s += len;", bPrefix, nameStr)
b.printf("}\n")
}
b.printf("}\n")
}
return nil
}
func (g *gen) writeSaveDerivedVar(b *buffer, name t.ID, typ *a.TypeExpr, footer bool) error {
// TODO: remove this hack. We're picking up the wrong name for "src:r,
// dummy:in.src".
if name.Str(g.tm) == "dummy" {
name = g.tm.ByName("src")
}
if g.currFunk.derivedVars == nil {
return nil
}
if _, ok := g.currFunk.derivedVars[name]; !ok {
return nil
}
nameStr := name.Str(g.tm)
switch typ.QID() {
case t.QID{0, t.IDReader1}:
b.printf("if (%s%s.buf) {", aPrefix, nameStr)
b.printf("size_t n = %srptr_%s - (%s%s.buf->ptr + %s%s.buf->ri);",
bPrefix, nameStr, aPrefix, nameStr, aPrefix, nameStr)
b.printf("%s%s.buf->ri += n;", aPrefix, nameStr)
b.printf("wuffs_base__limit1* lim;\n")
b.printf("for (lim = &%s%s.private_impl.limit; lim; lim = lim->next) {\n", aPrefix, nameStr)
b.printf("if (lim->ptr_to_len) {\n")
b.printf("*lim->ptr_to_len -= n;\n")
b.printf("}\n")
b.printf("}\n")
if footer {
b.printf("WUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(%srstart_%s);", bPrefix, nameStr)
b.printf("WUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(%srend_%s);", bPrefix, nameStr)
}
b.printf("}\n")
case t.QID{0, t.IDWriter1}:
b.printf("if (%s%s.buf) {", aPrefix, nameStr)
b.printf("size_t n = %swptr_%s - (%s%s.buf->ptr + %s%s.buf->wi);",
bPrefix, nameStr, aPrefix, nameStr, aPrefix, nameStr)
b.printf("%s%s.buf->wi += n;", aPrefix, nameStr)
b.printf("wuffs_base__limit1* lim;\n")
b.printf("for (lim = &%s%s.private_impl.limit; lim; lim = lim->next) {\n", aPrefix, nameStr)
b.printf("if (lim->ptr_to_len) {\n")
b.printf("*lim->ptr_to_len -= n;\n")
b.printf("}\n")
b.printf("}\n")
if footer {
b.printf("WUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(%swstart_%s);", bPrefix, nameStr)
b.printf("WUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(%swend_%s);", bPrefix, nameStr)
}
b.printf("}\n")
}
return nil
}
func (g *gen) writeLoadExprDerivedVars(b *buffer, n *a.Expr) error {
if g.currFunk.derivedVars == nil {
return nil
}
if k := n.Operator().Key(); k != t.KeyOpenParen && k != t.KeyTry {
return nil
}
for _, o := range n.Args() {
o := o.Arg()
// TODO: don't hard-code these.
if s := o.Value().Str(g.tm); s != "in.dst" && s != "in.src" {
continue
}
if err := g.writeLoadDerivedVar(b, o.Name(), o.Value().MType(), false); err != nil {
return err
}
}
return nil
}
func (g *gen) writeSaveExprDerivedVars(b *buffer, n *a.Expr) error {
if g.currFunk.derivedVars == nil {
return nil
}
if k := n.Operator().Key(); k != t.KeyOpenParen && k != t.KeyTry {
return nil
}
for _, o := range n.Args() {
o := o.Arg()
// TODO: don't hard-code these.
if s := o.Value().Str(g.tm); s != "in.dst" && s != "in.src" {
continue
}
if err := g.writeSaveDerivedVar(b, o.Name(), o.Value().MType(), false); err != nil {
return err
}
}
return nil
}
func (g *gen) visitVars(b *buffer, block []*a.Node, depth uint32, f func(*gen, *buffer, *a.Var) error) error {
if depth > a.MaxBodyDepth {
return fmt.Errorf("body recursion depth too large")
}
depth++
for _, o := range block {
switch o.Kind() {
case a.KIf:
for o := o.If(); o != nil; o = o.ElseIf() {
if err := g.visitVars(b, o.BodyIfTrue(), depth, f); err != nil {
return err
}
if err := g.visitVars(b, o.BodyIfFalse(), depth, f); err != nil {
return err
}
}
case a.KVar:
if err := f(g, b, o.Var()); err != nil {
return err
}
case a.KIterate:
if err := g.visitVars(b, o.Iterate().Variables(), depth, f); err != nil {
return err
}
if err := g.visitVars(b, o.Iterate().Body(), depth, f); err != nil {
return err
}
case a.KWhile:
if err := g.visitVars(b, o.While().Body(), depth, f); err != nil {
return err
}
}
}
return nil
}
// TODO: initBoolTypedVars is never used.
func (g *gen) writeResumeSuspend1(b *buffer, n *a.Var, suspend bool, initBoolTypedVars bool) error {
local := fmt.Sprintf("%s%s", vPrefix, n.Name().Str(g.tm))
if typ := n.XType(); typ.HasPointers() {
if suspend || initBoolTypedVars {
return nil
}
rhs := ""
switch typ.Decorator().Key() {
case 0:
if qid := typ.QID(); qid[0] == 0 {
if key := qid[1].Key(); key < t.Key(len(cTypeNames)) {
rhs = cTypeNames[key]
}
}
case t.KeyColon:
// TODO: don't assume that the slice is a slice of u8.
rhs = "wuffs_base__slice_u8"
}
if rhs != "" {
b.printf("%s = ((%s){});\n", local, rhs)
return nil
}
} else {
lhs := local
rhs := ""
// TODO: don't hard-code [0], and allow recursive coroutines.
if !initBoolTypedVars {
rhs = fmt.Sprintf("self->private_impl.%s%s[0].%s", cPrefix, g.currFunk.astFunc.FuncName().Str(g.tm), lhs)
} else if typ.QID() != (t.QID{0, t.IDBool}) {
return nil
} else if typ.Decorator() != 0 {
goto fail
} else {
// Explicitly initialize the bool-typed local variable to false.
//
// Otherwise, the variable (in C) can hold an uninitialized value.
// In terms of registers and memory (which work in integers, not
// bools), that uninitialized value could be something like 255,
// not 0 or 1.
//
// In the Wuffs language, a variable "foo" cannot be used before
// initialization, but that's not easily seen by running C language
// tools on Wuffs' generated C code. In C, that uninitialized v_foo
// local variable could be suspended to and then resumed from the
// f_foo coroutine state.
//
// This generated code explicitly initializes bool typed variables,
// in order to avoid ubsan (undefined behavior sanitizer) warnings.
//
// In general, we don't zero-initialize all of the v_etc local
// variables due to the negative performance impact.
rhs = "false"
}
if suspend {
lhs, rhs = rhs, lhs
}
switch typ.Decorator().Key() {
case 0:
b.printf("%s = %s;\n", lhs, rhs)
return nil
case t.KeyOpenBracket:
inner := typ.Inner()
if inner.Decorator() != 0 {
break
}
qid := inner.QID()
if qid[0] != 0 {
break
}
switch qid[1].Key() {
case t.KeyU8, t.KeyU16, t.KeyU32, t.KeyU64:
b.printf("memcpy(%s, %s, sizeof(%s));\n", lhs, rhs, local)
return nil
}
}
}
fail:
return fmt.Errorf("cannot resume or suspend a local variable %q of type %q",
n.Name().Str(g.tm), n.XType().Str(g.tm))
}
func (g *gen) writeResumeSuspend(b *buffer, block []*a.Node, suspend bool, initBoolTypedVars bool) error {
return g.visitVars(b, block, 0, func(g *gen, b *buffer, n *a.Var) error {
return g.writeResumeSuspend1(b, n, suspend, initBoolTypedVars)
})
}
func (g *gen) writeVars(b *buffer, block []*a.Node, skipPointerTypes bool, skipIterateVariables bool) error {
return g.visitVars(b, block, 0, func(g *gen, b *buffer, n *a.Var) error {
if skipPointerTypes && n.XType().HasPointers() {
return nil
}
if skipIterateVariables && n.IterateVariable() {
return nil
}
if err := g.writeCTypeName(b, n.XType(), vPrefix, n.Name().Str(g.tm)); err != nil {
return err
}
b.writes(";\n")
return nil
})
}