Make _fast explicit in copy_n_from_history_fast

This commit is contained in:
Nigel Tao committed 2018-07-08 21:39:18 +10:00
1 parent 8a449fa92e
commit e49d464432
6 files changed
+131 -150

No files matched your search

+7
View File
@@ -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)",
+106 -3
View File
@@ -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
-130
View File
@@ -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
+10 -8
View File
@@ -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.