Skip to content

Commit

Permalink
Fix tuple output bounds checks (#7345)
Browse files Browse the repository at this point in the history
Fix #7343

Tuple outputs weren't getting appropriate bounds checks due to
overzealous culling of uninteresting code in the add_image_checks pass.
  • Loading branch information
abadams authored Feb 14, 2023
1 parent 22aed20 commit 8bd07fb
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/AddImageChecks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,21 @@ class TrimStmtToPartsThatAccessBuffers : public IRMutator {
using IRMutator::visit;

Expr visit(const Call *op) override {
touches_buffer |= (buffers.count(op->name) > 0);
touches_buffer |=
(buffers.count(op->name) > 0) ||
(buffers.count(op->name + "." + std::to_string(op->value_index)));
// Output Tuple params are in the buffers map under their qualified
// tuple name, not the Func name.
return IRMutator::visit(op);
}
Stmt visit(const Provide *op) override {
touches_buffer |= (buffers.find(op->name) != buffers.end());
if (op->values.size() == 1) {
touches_buffer |= (buffers.find(op->name) != buffers.end());
} else {
// It's a Tuple. Just check if the first Tuple component corresponds
// to an output buffer. If it does, they all do.
touches_buffer |= (buffers.find(op->name + ".0") != buffers.end());
}
return IRMutator::visit(op);
}
Expr visit(const Variable *op) override {
Expand Down
1 change: 1 addition & 0 deletions test/error/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ tests(GROUPS error
thread_id_outside_block_id.cpp
too_many_args.cpp
tuple_arg_select_undef.cpp
tuple_output_bounds_check.cpp
tuple_val_select_undef.cpp
unbounded_input.cpp
unbounded_output.cpp
Expand Down
26 changes: 26 additions & 0 deletions test/error/tuple_output_bounds_check.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include "Halide.h"

using namespace Halide;

int main(int argc, char **argv) {
// The code below used to not inject appropriate bounds checks.
// See https://github.com/halide/Halide/issues/7343

Var x;

const int size = 1024;

Func h;
h(x) = {0, 0};
RDom r(0, size);
h(r) = {h(r - 100)[0], 0};

Var xo, xi;
h.split(x, xo, xi, 16, TailStrategy::RoundUp);

Buffer<int> r0(size);
Buffer<int> r1(size);
h.realize({r0, r1});

return 0;
}

0 comments on commit 8bd07fb

Please # to comment.