From 245e3ec7b94ad64f2c21768ed7211891c2ecc67c Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Fri, 20 Oct 2017 17:24:53 +1100 Subject: [PATCH] Unroll iterate loops. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit name old speed new speed delta puffs_adler32_10k/clang 2.12GB/s ± 1% 2.42GB/s ± 1% +14.24% (p=0.008 n=5+5) puffs_adler32_100k/clang 2.12GB/s ± 3% 2.43GB/s ± 0% +14.29% (p=0.008 n=5+5) mimic_adler32_10k/clang 2.93GB/s ± 2% 2.96GB/s ± 0% ~ (p=0.310 n=5+5) mimic_adler32_100k/clang 2.89GB/s ± 2% 2.91GB/s ± 0% ~ (p=1.000 n=5+5) puffs_adler32_10k/gcc 2.17GB/s ± 1% 3.23GB/s ± 0% +48.59% (p=0.008 n=5+5) puffs_adler32_100k/gcc 2.18GB/s ± 1% 3.23GB/s ± 0% +48.34% (p=0.008 n=5+5) mimic_adler32_10k/gcc 2.97GB/s ± 0% 2.96GB/s ± 1% ~ (p=1.000 n=5+5) mimic_adler32_100k/gcc 2.91GB/s ± 1% 2.91GB/s ± 1% ~ (p=0.548 n=5+5) --- cmd/puffs-c/internal/cgen/statement.go | 27 +++++++++++++++++----- gen/c/std/flate.c | 31 ++++++++++++++++++++++++-- lang/ast/ast.go | 24 +++++++++++--------- lang/check/type.go | 7 ++++++ lang/parse/parse.go | 22 +++++++++++++++++- std/flate/decode_zlib.puffs | 2 +- 6 files changed, 94 insertions(+), 19 deletions(-) diff --git a/cmd/puffs-c/internal/cgen/statement.go b/cmd/puffs-c/internal/cgen/statement.go index 0f36bf79..5191afdf 100644 --- a/cmd/puffs-c/internal/cgen/statement.go +++ b/cmd/puffs-c/internal/cgen/statement.go @@ -256,17 +256,34 @@ func (g *gen) writeStatement(b *buffer, n *a.Node, depth uint32) error { } b.writes(";\n") b.printf("uint8_t* %s%s = %sslice_%s.ptr;\n", vPrefix, name, iPrefix, name) - b.printf("uint8_t* %send_%s = %sslice_%s.ptr + %sslice_%s.len;\n", + // TODO: look at n.HasContinue() and n.HasBreak(). + + unrollCount := int(n.UnrollCount().ConstValue().Int64()) + if unrollCount != 1 { + b.printf("uint8_t* %send0_%s = %sslice_%s.ptr + (%sslice_%s.len / %d) * %d;\n", + iPrefix, name, iPrefix, name, iPrefix, name, unrollCount, unrollCount) + b.printf("while (%s%s < %send0_%s) {\n", vPrefix, name, iPrefix, name) + for i := 0; i < unrollCount; i++ { + for _, o := range n.Body() { + if err := g.writeStatement(b, o, depth); err != nil { + return err + } + } + b.printf("%s%s++;\n", vPrefix, name) + } + b.writes("}\n") + } + + b.printf("uint8_t* %send1_%s = %sslice_%s.ptr + %sslice_%s.len;\n", iPrefix, name, iPrefix, name, iPrefix, name) - // TODO: look at n.HasContinue(). - b.printf("while (%s%s < %send_%s) {\n", vPrefix, name, iPrefix, name) + b.printf("while (%s%s < %send1_%s) {\n", vPrefix, name, iPrefix, name) for _, o := range n.Body() { if err := g.writeStatement(b, o, depth); err != nil { return err } } - b.printf("%s%s++;\n}\n", vPrefix, name) - // TODO: look at n.HasBreak(). + b.printf("%s%s++;\n", vPrefix, name) + b.writes("}\n") b.writes("}\n") return nil diff --git a/gen/c/std/flate.c b/gen/c/std/flate.c index 1255d3c1..f9328599 100644 --- a/gen/c/std/flate.c +++ b/gen/c/std/flate.c @@ -2437,8 +2437,35 @@ uint32_t puffs_flate_adler32_update(puffs_flate_adler32* self, { puffs_base_slice_u8 i_slice_p = a_x; uint8_t* v_p = i_slice_p.ptr; - uint8_t* i_end_p = i_slice_p.ptr + i_slice_p.len; - while (v_p < i_end_p) { + uint8_t* i_end0_p = i_slice_p.ptr + (i_slice_p.len / 8) * 8; + while (v_p < i_end0_p) { + v_s1 += ((uint32_t)(*v_p)); + v_s2 += v_s1; + v_p++; + v_s1 += ((uint32_t)(*v_p)); + v_s2 += v_s1; + v_p++; + v_s1 += ((uint32_t)(*v_p)); + v_s2 += v_s1; + v_p++; + v_s1 += ((uint32_t)(*v_p)); + v_s2 += v_s1; + v_p++; + v_s1 += ((uint32_t)(*v_p)); + v_s2 += v_s1; + v_p++; + v_s1 += ((uint32_t)(*v_p)); + v_s2 += v_s1; + v_p++; + v_s1 += ((uint32_t)(*v_p)); + v_s2 += v_s1; + v_p++; + v_s1 += ((uint32_t)(*v_p)); + v_s2 += v_s1; + v_p++; + } + uint8_t* i_end1_p = i_slice_p.ptr + i_slice_p.len; + while (v_p < i_end1_p) { v_s1 += ((uint32_t)(*v_p)); v_s2 += v_s1; v_p++; diff --git a/lang/ast/ast.go b/lang/ast/ast.go index d3c3b3fa..7146313e 100644 --- a/lang/ast/ast.go +++ b/lang/ast/ast.go @@ -393,7 +393,7 @@ func NewField(name t.ID, xType *TypeExpr, defaultValue *Expr) *Field { } } -// Iterate is "iterate:ID1 (vars), List1 { List2 }": +// Iterate is "iterate.LHS:ID1 (vars), List1 { List2 }": // - FlagsHasBreak is the iterate has an explicit break // - FlagsHasContinue is the iterate has an explicit continue // - ID1: <0|label> @@ -406,6 +406,7 @@ func (n *Iterate) Node() *Node { return (*Node)(n) } func (n *Iterate) HasBreak() bool { return n.flags&FlagsHasBreak != 0 } func (n *Iterate) HasContinue() bool { return n.flags&FlagsHasContinue != 0 } func (n *Iterate) Label() t.ID { return n.id1 } +func (n *Iterate) UnrollCount() *Expr { return n.lhs.Expr() } func (n *Iterate) Variables() []*Node { return n.list0 } func (n *Iterate) Asserts() []*Node { return n.list1 } func (n *Iterate) Body() []*Node { return n.list2 } @@ -413,30 +414,33 @@ func (n *Iterate) Body() []*Node { return n.list2 } func (n *Iterate) SetHasBreak() { n.flags |= FlagsHasBreak } func (n *Iterate) SetHasContinue() { n.flags |= FlagsHasContinue } -func NewIterate(label t.ID, variables []*Node, asserts []*Node, body []*Node) *Iterate { +func NewIterate(label t.ID, unrollCount *Expr, variables []*Node, asserts []*Node, body []*Node) *Iterate { return &Iterate{ kind: KIterate, id1: label, + lhs: unrollCount.Node(), list0: variables, list1: asserts, list2: body, } } -// While is "while:ID1 LHS, List1 { List2 }": +// While is "while:ID1 MHS, List1 { List2 }": // - FlagsHasBreak is the while has an explicit break // - FlagsHasContinue is the while has an explicit continue // - ID1: <0|label> -// - LHS: +// - MHS: // - List1: asserts // - List2: loop body +// +// TODO: should we be able to unroll while loops too? type While Node func (n *While) Node() *Node { return (*Node)(n) } func (n *While) HasBreak() bool { return n.flags&FlagsHasBreak != 0 } func (n *While) HasContinue() bool { return n.flags&FlagsHasContinue != 0 } func (n *While) Label() t.ID { return n.id1 } -func (n *While) Condition() *Expr { return n.lhs.Expr() } +func (n *While) Condition() *Expr { return n.mhs.Expr() } func (n *While) Asserts() []*Node { return n.list1 } func (n *While) Body() []*Node { return n.list2 } @@ -447,21 +451,21 @@ func NewWhile(label t.ID, condition *Expr, asserts []*Node, body []*Node) *While return &While{ kind: KWhile, id1: label, - lhs: condition.Node(), + mhs: condition.Node(), list1: asserts, list2: body, } } -// If is "if LHS { List0 } else RHS" or "if LHS { List0 } else { List1 }": -// - LHS: +// If is "if MHS { List0 } else RHS" or "if MHS { List0 } else { List1 }": +// - MHS: // - RHS: // - List0: if-true body // - List1: if-false body type If Node func (n *If) Node() *Node { return (*Node)(n) } -func (n *If) Condition() *Expr { return n.lhs.Expr() } +func (n *If) Condition() *Expr { return n.mhs.Expr() } func (n *If) ElseIf() *If { return n.rhs.If() } func (n *If) BodyIfTrue() []*Node { return n.list0 } func (n *If) BodyIfFalse() []*Node { return n.list1 } @@ -469,7 +473,7 @@ func (n *If) BodyIfFalse() []*Node { return n.list1 } func NewIf(condition *Expr, elseIf *If, bodyIfTrue []*Node, bodyIfFalse []*Node) *If { return &If{ kind: KIf, - lhs: condition.Node(), + mhs: condition.Node(), rhs: elseIf.Node(), list0: bodyIfTrue, list1: bodyIfFalse, diff --git a/lang/check/type.go b/lang/check/type.go index eb3f3c26..4bac87b2 100644 --- a/lang/check/type.go +++ b/lang/check/type.go @@ -203,6 +203,13 @@ func (q *checker) tcheckStatement(n *a.Node) error { case a.KIterate: n := n.Iterate() + unroll := n.UnrollCount() + if err := q.tcheckExpr(unroll, 0); err != nil { + return err + } + if cv := unroll.ConstValue(); cv == nil { + return fmt.Errorf("check: unroll count %q is not constant", unroll.String(q.tm)) + } for _, o := range n.Variables() { if err := q.tcheckStatement(o); err != nil { return err diff --git a/lang/parse/parse.go b/lang/parse/parse.go index c195a6f5..ab0ce69e 100644 --- a/lang/parse/parse.go +++ b/lang/parse/parse.go @@ -570,6 +570,26 @@ func (p *parser) parseStatement1() (*a.Node, error) { case t.KeyIterate: p.src = p.src[1:] + if x := p.peek1().Key(); x != t.KeyDot { + got := p.tm.ByKey(x) + return nil, fmt.Errorf(`parse: expected ".", got %q at %s:%d`, got, p.filename, p.line()) + } + p.src = p.src[1:] + unrollID := p.peek1() + unrollStr := p.tm.ByKey(unrollID.Key()) + if !unrollID.IsLiteral() { + return nil, fmt.Errorf(`parse: expected literal unroll count, got %q at %s:%d`, + unrollStr, p.filename, p.line()) + } + p.src = p.src[1:] + switch unrollStr { + default: + return nil, fmt.Errorf(`parse: expected power-of-2 unroll count in [1..256], got %q at %s:%d`, + unrollStr, p.filename, p.line()) + case "1", "2", "4", "8", "16", "32", "64", "128", "256": + } + unroll := a.NewExpr(0, 0, unrollID, nil, nil, nil, nil) + label, err := p.parseLabel() if err != nil { return nil, err @@ -586,7 +606,7 @@ func (p *parser) parseStatement1() (*a.Node, error) { if err != nil { return nil, err } - return a.NewIterate(label, vars, asserts, body).Node(), nil + return a.NewIterate(label, unroll, vars, asserts, body).Node(), nil case t.KeyWhile: p.src = p.src[1:] diff --git a/std/flate/decode_zlib.puffs b/std/flate/decode_zlib.puffs index 33594f80..acb5f7b2 100644 --- a/std/flate/decode_zlib.puffs +++ b/std/flate/decode_zlib.puffs @@ -61,7 +61,7 @@ pri func adler32.update(x[] u8)(checksum u32) { in.x = in.x[:5552] } - iterate (p ptr u8:in.x) { + iterate.8(p ptr u8:in.x) { s1 ~+= deref p as u32 s2 ~+= s1 }