From e49d4644321f1c72d5ef41ed59f201f9179f219e Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Sun, 8 Jul 2018 21:39:18 +1000 Subject: [PATCH] Make _fast explicit in copy_n_from_history_fast --- cmd/wuffs-c/internal/cgen/builtin.go | 15 ++- lang/builtin/builtin.go | 7 ++ lang/check/bounds.go | 109 ++++++++++++++++++++- lang/check/optimize.go | 130 -------------------------- lang/token/list.go | 18 ++-- std/deflate/decode_huffman_fast.wuffs | 2 +- 6 files changed, 131 insertions(+), 150 deletions(-) diff --git a/cmd/wuffs-c/internal/cgen/builtin.go b/cmd/wuffs-c/internal/cgen/builtin.go index 627a0a3c..b0772b3d 100644 --- a/cmd/wuffs-c/internal/cgen/builtin.go +++ b/cmd/wuffs-c/internal/cgen/builtin.go @@ -70,7 +70,7 @@ func (g *gen) writeBuiltinCall(b *buffer, n *a.Expr, rp replacementPolicy, depth case t.IDIOReader: return g.writeBuiltinIOReader(b, recv, method.Ident(), n.Args(), rp, depth) case t.IDIOWriter: - return g.writeBuiltinIOWriter(b, recv, method.Ident(), n.Args(), rp, depth, n.BoundsCheckOptimized()) + return g.writeBuiltinIOWriter(b, recv, method.Ident(), n.Args(), rp, depth) case t.IDStatus: return g.writeBuiltinStatus(b, recv, method.Ident(), n.Args(), rp, depth) } @@ -165,18 +165,17 @@ func (g *gen) writeBuiltinIOReader(b *buffer, recv *a.Expr, method t.ID, args [] return g.writeBuiltinIO(b, recv, method, args, rp, depth) } -// TODO: remove bcoHack. -func (g *gen) writeBuiltinIOWriter(b *buffer, recv *a.Expr, method t.ID, args []*a.Node, rp replacementPolicy, depth uint32, bcoHack bool) error { +func (g *gen) writeBuiltinIOWriter(b *buffer, recv *a.Expr, method t.ID, args []*a.Node, rp replacementPolicy, depth uint32) error { // TODO: don't hard-code the recv being a_dst. switch method { - case t.IDCopyNFromHistory: - bco := "" - if bcoHack { - bco = "_fast" + case t.IDCopyNFromHistory, t.IDCopyNFromHistoryFast: + suffix := "" + if method == t.IDCopyNFromHistoryFast { + suffix = "_fast" } b.printf("wuffs_base__io_writer__copy_n_from_history%s("+ "&ioptr_dst, %sdst.private_impl.bounds[0], iobounds1_dst", - bco, aPrefix) + suffix, aPrefix) for _, o := range args { b.writeb(',') if err := g.writeExpr(b, o.AsArg().Value(), rp, depth); err != nil { diff --git a/lang/builtin/builtin.go b/lang/builtin/builtin.go index 58bd658f..f2d15cf1 100644 --- a/lang/builtin/builtin.go +++ b/lang/builtin/builtin.go @@ -244,6 +244,13 @@ var Funcs = []string{ "io_writer.copy_n_from_reader!(n u32, r io_reader)(ret u32)", "io_writer.copy_n_from_slice!(n u32, s slice u8)(ret u32)", + // TODO: this should have explicit pre-conditions: + // - n <= this.available() + // - distance > 0 + // - distance <= this.since_mark().length() + // For now, that's all implicitly checked (i.e. hard coded). + "io_writer.copy_n_from_history_fast!(n u32, distance u32)(ret u32)", + // ---- status "status.is_error()(ret bool)", diff --git a/lang/check/bounds.go b/lang/check/bounds.go index 4daaeb01..4ee2890a 100644 --- a/lang/check/bounds.go +++ b/lang/check/bounds.go @@ -712,9 +712,6 @@ func (q *checker) bcheckExpr(n *a.Expr, depth uint32) (a.Bounds, error) { return a.Bounds{}, fmt.Errorf("check: expression %q bounds %v is not within bounds %v", n.Str(q.tm), nb, tb) } - if err := q.optimizeNonSuspendible(n); err != nil { - return a.Bounds{}, err - } n.SetMBounds(nb) return nb, nil @@ -968,6 +965,11 @@ func (q *checker) bcheckExprCallSpecialCases(n *a.Expr, depth uint32) (a.Bounds, return a.Bounds{}, err } + } else if method == t.IDCopyNFromHistoryFast { + if err := q.canCopyNFromHistoryFast(recv, n.Args()); err != nil { + return a.Bounds{}, err + } + } else if method == t.IDSkipFast { args := n.Args() if len(args) != 2 { @@ -1030,6 +1032,107 @@ func (q *checker) canUndoByte(recv *a.Expr) error { return fmt.Errorf("check: could not prove %s.can_undo_byte()", recv.Str(q.tm)) } +func (q *checker) canCopyNFromHistoryFast(recv *a.Expr, args []*a.Node) error { + // As per cgen's base-private.h, there are three pre-conditions: + // - n <= this.available() + // - distance > 0 + // - distance <= this.since_mark().length() + + if len(args) != 2 { + return fmt.Errorf("check: internal error: inconsistent copy_n_from_history_fast arguments") + } + n := args[0].AsArg().Value() + distance := args[1].AsArg().Value() + + // Check "n <= this.available()". +check0: + for { + for _, x := range q.facts { + if x.Operator() != t.IDXBinaryLessEq { + continue + } + + // Check that the LHS is "n as base.u64". + lhs := x.LHS().AsExpr() + if lhs.Operator() != t.IDXBinaryAs { + continue + } + llhs, lrhs := lhs.LHS().AsExpr(), lhs.RHS().AsTypeExpr() + if !llhs.Eq(n) || !lrhs.Eq(typeExprU64) { + continue + } + + // Check that the RHS is "recv.available()". + y, method, yArgs := splitReceiverMethodArgs(x.RHS().AsExpr()) + if method != t.IDAvailable || len(yArgs) != 0 { + continue + } + if !y.Eq(recv) { + continue + } + + break check0 + } + return fmt.Errorf("check: could not prove n <= %s.available()", recv.Str(q.tm)) + } + + // Check "distance > 0". +check1: + for { + for _, x := range q.facts { + if x.Operator() != t.IDXBinaryGreaterThan { + continue + } + if lhs := x.LHS().AsExpr(); !lhs.Eq(distance) { + continue + } + if rcv := x.RHS().AsExpr().ConstValue(); rcv == nil || rcv.Sign() != 0 { + continue + } + break check1 + } + return fmt.Errorf("check: could not prove distance > 0") + } + + // Check "distance <= this.since_mark().length()". +check2: + for { + for _, x := range q.facts { + if x.Operator() != t.IDXBinaryLessEq { + continue + } + + // Check that the LHS is "distance as base.u64". + lhs := x.LHS().AsExpr() + if lhs.Operator() != t.IDXBinaryAs { + continue + } + llhs, lrhs := lhs.LHS().AsExpr(), lhs.RHS().AsTypeExpr() + if !llhs.Eq(distance) || !lrhs.Eq(typeExprU64) { + continue + } + + // Check that the RHS is "recv.since_mark().length()". + y, method, yArgs := splitReceiverMethodArgs(x.RHS().AsExpr()) + if method != t.IDLength || len(yArgs) != 0 { + continue + } + z, method, zArgs := splitReceiverMethodArgs(y) + if method != t.IDSinceMark || len(zArgs) != 0 { + continue + } + if !z.Eq(recv) { + continue + } + + break check2 + } + return fmt.Errorf("check: could not prove distance <= %s.since_mark().length()", recv.Str(q.tm)) + } + + return nil +} + var ioMethodAdvances = [...]struct { advance *big.Int update bool diff --git a/lang/check/optimize.go b/lang/check/optimize.go index d91a164b..ba920a0d 100644 --- a/lang/check/optimize.go +++ b/lang/check/optimize.go @@ -44,136 +44,6 @@ func splitReceiverMethodArgs(n *a.Expr) (receiver *a.Expr, method t.ID, args []* return n.LHS().AsExpr(), n.Ident(), args } -// TODO: should optimizeNonSuspendible be optimizeExpr and check both -// suspendible and non-suspendible expressions? -// -// See also "be principled about checking for provenNotToSuspend". - -func (q *checker) optimizeNonSuspendible(n *a.Expr) error { - // TODO: check n.CallSuspendible()? - // - // Should we have an explicit "foo = call bar?()" syntax? - - nReceiver, nMethod, _ := splitReceiverMethodArgs(n) - if nReceiver == nil { - return nil - } - - // TODO: check that nReceiver's type is actually a io_reader or io_writer. - - switch nMethod { - case t.IDCopyNFromHistory: - return q.optimizeCopyNFromHistory(n) - } - - return nil -} - -// optimizeCopyNFromHistory checks if the code generator can call the Bounds -// Check Optimized version of CopyFromHistory. As per cgen's base-private.h, -// the conditions are: -// - distance > 0 -// - distance <= (*ptr_ptr - start) -// - length <= (end - *ptr_ptr) -func (q *checker) optimizeCopyNFromHistory(n *a.Expr) error { - // The arguments are (distance, length). - nReceiver, _, args := splitReceiverMethodArgs(n) - if len(args) != 2 { - return nil - } - l := args[0].AsArg().Value() - d := args[1].AsArg().Value() - - // Check "distance > 0". -check0: - for { - for _, x := range q.facts { - if x.Operator() != t.IDXBinaryGreaterThan { - continue - } - if lhs := x.LHS().AsExpr(); !lhs.Eq(d) { - continue - } - if rcv := x.RHS().AsExpr().ConstValue(); rcv == nil || rcv.Sign() != 0 { - continue - } - break check0 - } - return nil - } - - // Check "distance <= (*ptr_ptr - start)". -check1: - for { - for _, x := range q.facts { - if x.Operator() != t.IDXBinaryLessEq { - continue - } - - // Check that the LHS is "d as base.u64". - lhs := x.LHS().AsExpr() - if lhs.Operator() != t.IDXBinaryAs { - continue - } - llhs, lrhs := lhs.LHS().AsExpr(), lhs.RHS().AsTypeExpr() - if !llhs.Eq(d) || !lrhs.Eq(typeExprU64) { - continue - } - - // Check that the RHS is "nReceiver.since_mark().length()". - y, method, yArgs := splitReceiverMethodArgs(x.RHS().AsExpr()) - if method != t.IDLength || len(yArgs) != 0 { - continue - } - z, method, zArgs := splitReceiverMethodArgs(y) - if method != t.IDSinceMark || len(zArgs) != 0 { - continue - } - if !z.Eq(nReceiver) { - continue - } - - break check1 - } - return nil - } - - // Check "length <= (end - *ptr_ptr)". -check2: - for { - for _, x := range q.facts { - if x.Operator() != t.IDXBinaryLessEq { - continue - } - - // Check that the LHS is "l as base.u64". - lhs := x.LHS().AsExpr() - if lhs.Operator() != t.IDXBinaryAs { - continue - } - llhs, lrhs := lhs.LHS().AsExpr(), lhs.RHS().AsTypeExpr() - if !llhs.Eq(l) || !lrhs.Eq(typeExprU64) { - continue - } - - // Check that the RHS is "nReceiver.available()". - y, method, yArgs := splitReceiverMethodArgs(x.RHS().AsExpr()) - if method != t.IDAvailable || len(yArgs) != 0 { - continue - } - if !y.Eq(nReceiver) { - continue - } - - break check2 - } - return nil - } - - n.SetBoundsCheckOptimized() - return nil -} - func (q *checker) optimizeIOMethodAdvance(receiver *a.Expr, advance *big.Int, update bool) (retOK bool, retErr error) { // TODO: do two passes? The first one to non-destructively check retOK. The // second one to drop facts if indeed retOK? Otherwise, an advance diff --git a/lang/token/list.go b/lang/token/list.go index 55d6fb57..4494babb 100644 --- a/lang/token/list.go +++ b/lang/token/list.go @@ -517,10 +517,11 @@ const ( IDSkip = ID(0x184) IDSkipFast = ID(0x185) - IDCopyFromSlice = ID(0x190) - IDCopyNFromHistory = ID(0x191) - IDCopyNFromReader = ID(0x192) - IDCopyNFromSlice = ID(0x193) + IDCopyFromSlice = ID(0x190) + IDCopyNFromHistory = ID(0x191) + IDCopyNFromHistoryFast = ID(0x192) + IDCopyNFromReader = ID(0x193) + IDCopyNFromSlice = ID(0x194) // -------- 0x200 block. @@ -794,10 +795,11 @@ var builtInsByID = [nBuiltInIDs]string{ IDSkip: "skip", IDSkipFast: "skip_fast", - IDCopyFromSlice: "copy_from_slice", - IDCopyNFromHistory: "copy_n_from_history", - IDCopyNFromReader: "copy_n_from_reader", - IDCopyNFromSlice: "copy_n_from_slice", + IDCopyFromSlice: "copy_from_slice", + IDCopyNFromHistory: "copy_n_from_history", + IDCopyNFromHistoryFast: "copy_n_from_history_fast", + IDCopyNFromReader: "copy_n_from_reader", + IDCopyNFromSlice: "copy_n_from_slice", // -------- 0x200 block. diff --git a/std/deflate/decode_huffman_fast.wuffs b/std/deflate/decode_huffman_fast.wuffs index 1477d11b..7d83dca9 100644 --- a/std/deflate/decode_huffman_fast.wuffs +++ b/std/deflate/decode_huffman_fast.wuffs @@ -360,7 +360,7 @@ pri func decoder.decode_huffman_fast?(dst base.io_writer, src base.io_reader)() assert (length as base.u64) <= in.dst.available() via "a <= b: a <= c; c <= b"(c:258) // Copy from in.dst. - in.dst.copy_n_from_history!(n:length, distance:(dist_minus_1 + 1)) + in.dst.copy_n_from_history_fast!(n:length, distance:(dist_minus_1 + 1)) break } }