Files
wuffs/cmd/wuffs-c/internal/cgen/var.go
T
Nigel Tao 6ebe99ced9 Don't suspend/resume unnecessary local variables
name                                    old speed      new speed      delta

wuffs_adler32_10k/clang                 2.42GB/s ± 1%  2.41GB/s ± 0%  -0.43%  (p=0.000 n=49+10)
wuffs_adler32_100k/clang                2.42GB/s ± 1%  2.41GB/s ± 1%  -0.56%  (p=0.000 n=48+10)

wuffs_adler32_10k/gcc                   3.22GB/s ± 1%  3.21GB/s ± 0%  -0.35%  (p=0.000 n=48+10)
wuffs_adler32_100k/gcc                  3.22GB/s ± 0%  3.20GB/s ± 0%  -0.69%  (p=0.000 n=48+10)

wuffs_crc32_ieee_10k/clang              2.86GB/s ± 0%  2.85GB/s ± 0%  -0.44%  (p=0.000 n=48+9)
wuffs_crc32_ieee_100k/clang             2.88GB/s ± 1%  2.87GB/s ± 0%  -0.45%  (p=0.000 n=49+10)

wuffs_crc32_ieee_10k/gcc                3.35GB/s ± 1%  3.35GB/s ± 0%    ~     (p=0.094 n=48+9)
wuffs_crc32_ieee_100k/gcc               3.38GB/s ± 1%  3.38GB/s ± 1%    ~     (p=0.371 n=45+10)

wuffs_deflate_decode_1k/clang            147MB/s ± 1%   137MB/s ± 1%  -6.84%  (p=0.000 n=50+10)
wuffs_deflate_decode_10k/clang           241MB/s ± 0%   227MB/s ± 0%  -5.74%  (p=0.000 n=48+9)
wuffs_deflate_decode_100k/clang          269MB/s ± 0%   258MB/s ± 0%  -3.92%  (p=0.000 n=49+9)

wuffs_deflate_decode_1k/gcc              159MB/s ± 0%   161MB/s ± 1%  +1.51%  (p=0.000 n=49+10)
wuffs_deflate_decode_10k/gcc             269MB/s ± 0%   276MB/s ± 0%  +2.51%  (p=0.000 n=49+10)
wuffs_deflate_decode_100k/gcc            320MB/s ± 0%   316MB/s ± 0%  -1.29%  (p=0.000 n=46+10)

wuffs_gif_decode_1k_bw/clang             252MB/s ± 1%   264MB/s ± 0%  +4.56%  (p=0.000 n=49+10)
wuffs_gif_decode_1k_color/clang          127MB/s ± 1%   122MB/s ± 1%  -3.84%  (p=0.000 n=47+10)
wuffs_gif_decode_10k/clang               152MB/s ± 0%   148MB/s ± 0%  -3.12%  (p=0.000 n=47+9)
wuffs_gif_decode_100k_artificial/clang   431MB/s ± 0%   443MB/s ± 1%  +2.95%  (p=0.000 n=42+10)
wuffs_gif_decode_100k_realistic/clang    170MB/s ± 0%   173MB/s ± 0%  +1.39%  (p=0.000 n=48+10)
wuffs_gif_decode_1000k/clang             172MB/s ± 0%   174MB/s ± 0%  +1.27%  (p=0.000 n=48+10)
wuffs_gif_decode_anim_screencap/clang    917MB/s ± 1%   949MB/s ± 0%  +3.44%  (p=0.000 n=47+9)

wuffs_gif_decode_1k_bw/gcc               265MB/s ± 1%   278MB/s ± 2%  +4.87%  (p=0.000 n=48+10)
wuffs_gif_decode_1k_color/gcc            135MB/s ± 0%   140MB/s ± 2%  +4.10%  (p=0.000 n=46+10)
wuffs_gif_decode_10k/gcc                 157MB/s ± 1%   166MB/s ± 0%  +5.84%  (p=0.000 n=48+8)
wuffs_gif_decode_100k_artificial/gcc     446MB/s ± 1%   459MB/s ± 1%  +2.89%  (p=0.000 n=48+10)
wuffs_gif_decode_100k_realistic/gcc      176MB/s ± 0%   186MB/s ± 2%  +5.82%  (p=0.000 n=46+10)
wuffs_gif_decode_1000k/gcc               178MB/s ± 0%   189MB/s ± 1%  +6.11%  (p=0.000 n=45+9)
wuffs_gif_decode_anim_screencap/gcc      962MB/s ± 1%   989MB/s ± 1%  +2.83%  (p=0.000 n=48+9)

wuffs_gzip_decode_10k/clang              212MB/s ± 0%   208MB/s ± 0%  -1.67%  (p=0.000 n=47+10)
wuffs_gzip_decode_100k/clang             240MB/s ± 0%   236MB/s ± 0%  -1.68%  (p=0.000 n=47+10)

wuffs_gzip_decode_10k/gcc                252MB/s ± 0%   246MB/s ± 0%  -2.41%  (p=0.000 n=47+10)
wuffs_gzip_decode_100k/gcc               290MB/s ± 0%   288MB/s ± 0%  -0.70%  (p=0.000 n=44+10)

wuffs_lzw_decode_20k/clang               202MB/s ± 0%   202MB/s ± 0%    ~     (p=0.069 n=48+8)
wuffs_lzw_decode_100k/clang              332MB/s ± 1%   334MB/s ± 1%  +0.54%  (p=0.000 n=46+10)

wuffs_lzw_decode_20k/gcc                 215MB/s ± 1%   223MB/s ± 0%  +3.60%  (p=0.000 n=49+9)
wuffs_lzw_decode_100k/gcc                361MB/s ± 1%   368MB/s ± 0%  +2.05%  (p=0.000 n=47+10)

wuffs_zlib_decode_10k/clang              218MB/s ± 0%   206MB/s ± 0%  -5.53%  (p=0.000 n=48+9)
wuffs_zlib_decode_100k/clang             244MB/s ± 0%   234MB/s ± 0%  -4.14%  (p=0.000 n=45+10)

wuffs_zlib_decode_10k/gcc                252MB/s ± 0%   252MB/s ± 0%    ~     (p=0.960 n=49+10)
wuffs_zlib_decode_100k/gcc               290MB/s ± 0%   292MB/s ± 0%  +0.52%  (p=0.000 n=49+10)
2018-11-10 17:04:40 +11:00

421 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.AsExpr()
if q.Operator() != t.IDOpenParen {
return nil
}
q = q.LHS().AsExpr()
if q.Operator() != t.IDDot {
return nil
}
q = q.LHS().AsExpr()
if q.Operator() != t.IDDot || q.Ident() != name {
return nil
}
q = q.LHS().AsExpr()
if q.Operator() != 0 || q.Ident() != t.IDArgs {
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.AsField()
oTyp := o.XType()
if oTyp.Decorator() != 0 {
continue
}
switch oTyp.QID() {
default:
continue
case t.QID{t.IDBase, t.IDIOReader}, t.QID{t.IDBase, t.IDIOWriter}:
// 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, hack string, prefix string, name t.ID, typ *a.TypeExpr, header bool) error {
// TODO: remove this hack. We're picking up the wrong name for "src:r,
// dummy:args.src".
if name.Str(g.tm) == "dummy" {
name = g.tm.ByName("src")
}
// TODO: also remove this hack.
if hack == "w" {
b.printf("%s%sw = %sw.data.ptr + %sw.meta.wi;\n", iopPrefix, vPrefix, uPrefix, uPrefix)
return nil
}
if !typ.IsIOType() {
return nil
}
if g.currFunk.derivedVars == nil {
return nil
}
if _, ok := g.currFunk.derivedVars[name]; !ok {
return nil
}
preName := prefix + name.Str(g.tm)
i0, i1 := "meta.ri", "meta.wi"
if typ.QID()[1] == t.IDIOWriter {
i0, i1 = "meta.wi", "data.len"
}
if header {
b.printf("uint8_t* %s%s = NULL;", iopPrefix, preName)
b.printf("uint8_t* %s%s = NULL;", io0Prefix, preName)
b.printf("uint8_t* %s%s = NULL;", io1Prefix, preName)
b.printf("WUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(%s%s);", io0Prefix, preName)
b.printf("WUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(%s%s);", io1Prefix, preName)
}
b.printf("if (%s.private_impl.buf) {", preName)
b.printf("%s%s = %s.private_impl.buf->data.ptr + %s.private_impl.buf->%s;",
iopPrefix, preName, preName, preName, i0)
if header {
b.printf("if (!%s.private_impl.mark) {", preName)
b.printf("%s.private_impl.mark = %s%s;", preName, iopPrefix, preName)
b.printf("%s.private_impl.limit = %s.private_impl.buf->data.ptr + %s.private_impl.buf->%s;",
preName, preName, preName, i1)
b.printf("}\n")
if typ.QID()[1] == t.IDIOWriter {
b.printf("if (%s.private_impl.buf->meta.closed) {", preName)
b.printf("%s.private_impl.limit = %s%s;", preName, iopPrefix, preName)
b.printf("}\n")
}
b.printf("%s%s = %s.private_impl.mark;", io0Prefix, preName, preName)
b.printf("%s%s = %s.private_impl.limit;", io1Prefix, preName, preName)
}
b.printf("}\n")
return nil
}
func (g *gen) writeSaveDerivedVar(b *buffer, hack string, prefix string, name t.ID, typ *a.TypeExpr) error {
// TODO: remove this hack. We're picking up the wrong name for "src:r,
// dummy:args.src".
if name.Str(g.tm) == "dummy" {
name = g.tm.ByName("src")
}
// TODO: also remove this hack.
if hack == "w" {
b.printf("%sw.meta.wi = %s%sw - %sw.data.ptr;\n", uPrefix, iopPrefix, vPrefix, uPrefix)
return nil
}
if !typ.IsIOType() {
return nil
}
if g.currFunk.derivedVars == nil {
return nil
}
if _, ok := g.currFunk.derivedVars[name]; !ok {
return nil
}
preName := prefix + name.Str(g.tm)
i0 := "ri"
if typ.QID()[1] == t.IDIOWriter {
i0 = "wi"
}
b.printf("if (%s.private_impl.buf) {", preName)
b.printf("%s.private_impl.buf->meta.%s = %s%s - %s.private_impl.buf->data.ptr;",
preName, i0, iopPrefix, preName, preName)
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(); k != t.IDOpenParen && k != t.IDTry {
return nil
}
for _, o := range n.Args() {
o := o.AsArg()
prefix := aPrefix
// TODO: don't hard-code these.
hack := ""
if s := o.Value().Str(g.tm); s != "args.dst" && s != "args.src" && s != "w" {
continue
} else if s == "w" {
prefix = vPrefix
hack = "w"
}
if err := g.writeLoadDerivedVar(b, hack, prefix, 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(); k != t.IDOpenParen && k != t.IDTry {
return nil
}
for _, o := range n.Args() {
o := o.AsArg()
prefix := aPrefix
// TODO: don't hard-code these.
hack := ""
if s := o.Value().Str(g.tm); s != "args.dst" && s != "args.src" && s != "w" {
continue
} else if s == "w" {
prefix = vPrefix
hack = "w"
}
if err := g.writeSaveDerivedVar(b, hack, prefix, o.Name(), o.Value().MType()); 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.KIOBind:
if err := g.visitVars(b, o.AsIOBind().Body(), depth, f); err != nil {
return err
}
case a.KIf:
for o := o.AsIf(); 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.KIterate:
if err := g.visitVars(b, o.AsIterate().Variables(), depth, f); err != nil {
return err
}
if err := g.visitVars(b, o.AsIterate().Body(), depth, f); err != nil {
return err
}
case a.KVar:
if err := f(g, b, o.AsVar()); err != nil {
return err
}
case a.KWhile:
if err := g.visitVars(b, o.AsWhile().Body(), depth, f); err != nil {
return err
}
}
}
return nil
}
func (g *gen) writeResumeSuspend1(b *buffer, f *funk, 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 {
return nil
}
rhs := ""
// TODO: move this initialization to writeVars?
switch typ.Decorator() {
case 0:
if qid := typ.QID(); qid[0] == t.IDBase {
if key := qid[1]; key < t.ID(len(cTypeNames)) {
rhs = cTypeNames[key]
}
}
case t.IDSlice:
// TODO: don't assume that the slice is a slice of base.u8.
rhs = "wuffs_base__slice_u8"
case t.IDTable:
// TODO: don't assume that the table is a table of base.u8.
rhs = "wuffs_base__table_u8"
}
if rhs != "" {
b.printf("%s = ((%s){});\n", local, rhs)
return nil
}
} else if f.varResumables == nil || !f.varResumables[n.Name()] {
if !suspend {
if initBoolTypedVars && typ.QID() == (t.QID{t.IDBase, t.IDBool}) {
b.printf("%s = false;\n", local)
return nil
}
// TODO: drop this (after we stop emitting unnecessary
// WUFFS_BASE__COROUTINE_SUSPENSION_POINT lines); we shouldn't need
// to zero-initialize these variables.
if !initBoolTypedVars {
zero := "0"
if typ.QID() == (t.QID{t.IDBase, t.IDBool}) {
zero = "false"
}
b.printf("%s = %s;\n", local, zero)
}
}
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{t.IDBase, 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 initialize (set to zero or assign from
// "this" fields) all of the v_etc local variables when not
// resuming a coroutine, due to the performance impact. Commit
// 2b6b0ac "Unconditionally resume local vars" has some numbers.
rhs = "false"
}
if suspend {
lhs, rhs = rhs, lhs
}
switch typ.Decorator() {
case 0:
b.printf("%s = %s;\n", lhs, rhs)
return nil
case t.IDArray:
inner := typ.Inner()
if inner.Decorator() != 0 {
break
}
qid := inner.QID()
if qid[0] != t.IDBase {
break
}
switch qid[1] {
case t.IDU8, t.IDU16, t.IDU32, t.IDU64:
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, f *funk, suspend bool, initBoolTypedVars bool) error {
return g.visitVars(b, f.astFunc.Body(), 0, func(g *gen, b *buffer, n *a.Var) error {
return g.writeResumeSuspend1(b, f, n, suspend, initBoolTypedVars)
})
}
func (g *gen) writeVars(b *buffer, f *funk, skipPointerTypes bool, skipIterateVariables bool, skipNonresumable bool) error {
return g.visitVars(b, f.astFunc.Body(), 0, func(g *gen, b *buffer, n *a.Var) error {
typ := n.XType()
if skipPointerTypes && typ.HasPointers() {
return nil
}
if skipIterateVariables && n.IterateVariable() {
return nil
}
if skipNonresumable && (f.varResumables == nil || !f.varResumables[n.Name()]) {
return nil
}
name := n.Name().Str(g.tm)
if err := g.writeCTypeName(b, typ, vPrefix, name); err != nil {
return err
}
b.writes(";\n")
if typ.IsIOType() {
b.printf("wuffs_base__io_buffer %s%s;\n", uPrefix, name)
preName := vPrefix + name
// TODO: io0_etc variables?
b.printf("uint8_t* %s%s = NULL;\n", iopPrefix, preName)
b.printf("uint8_t* %s%s = NULL;\n", io1Prefix, preName)
b.printf("WUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(%s%s);\n", uPrefix, name)
b.printf("WUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(%s%s);\n", iopPrefix, preName)
b.printf("WUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(%s%s);\n", io1Prefix, preName)
}
return nil
})
}