We should add error checking for overflow in slinky pipeline evaluation.
Adding checked arithmetic on the expression evaluation and buffer operations (e.g. for_each_element) could be expensive due to adding error handling codepaths.
Instead, I think we can ensure that there is no overflow when constructing buffers, and when calling pipelines. We can:
- Add a
validate_buffer(const raw_buffer&) function that verifies that both the min and max address accessible by the buffer do not overflow. This will probably be similar to this function:
|
std::size_t alloc_size(std::size_t rank, std::size_t elem_size, const dim* dims) { |
|
index_t flat_min = 0; |
|
index_t flat_max = 0; |
|
for (std::size_t i = 0; i < rank; ++i) { |
|
if (dims[i].stride() == 0) continue; |
|
index_t extent = alloc_extent(dims[i]); |
|
assert(extent >= 0); |
|
if (extent == 0) return 0; |
|
flat_min += (extent - 1) * std::min<index_t>(0, dims[i].stride()); |
|
flat_max += (extent - 1) * std::max<index_t>(0, dims[i].stride()); |
|
} |
|
return flat_max - flat_min + elem_size; |
|
} |
.
- Call
validate_buffer in evalaute for make_buffer and allocate. These evaluators can return an error code. I think that all other buffer operations cannot cause overflow to occur.
- Add a call expression for
validate_buffer to
and then generate checks for validate_buffer when constructing pipelines (probably here:
|
void check_buffer(const buffer_expr_ptr& b, std::vector<stmt>& checks) { |
)
We should test this in https://github.com/dsharlet/slinky/blob/main/slinky/builder/test/checks.cc (at least).
We should add error checking for overflow in slinky pipeline evaluation.
Adding checked arithmetic on the expression evaluation and buffer operations (e.g.
for_each_element) could be expensive due to adding error handling codepaths.Instead, I think we can ensure that there is no overflow when constructing buffers, and when calling pipelines. We can:
validate_buffer(const raw_buffer&)function that verifies that both the min and max address accessible by the buffer do not overflow. This will probably be similar to this function:slinky/slinky/runtime/buffer.cc
Lines 26 to 38 in e5ef18c
validate_bufferinevalauteformake_bufferandallocate. These evaluators can return an error code. I think that all other buffer operations cannot cause overflow to occur.validate_buffertoslinky/slinky/runtime/expr.h
Line 87 in e5ef18c
validate_bufferwhen constructing pipelines (probably here:slinky/slinky/builder/pipeline.cc
Line 1446 in e5ef18c
We should test this in https://github.com/dsharlet/slinky/blob/main/slinky/builder/test/checks.cc (at least).