#9322·Halide

PredicateStores can cause out of bounds reads/writes by producers compute_at the inner var

Author: abadamsCreated Aug 12, 2026Updated Aug 12, 2026

Description

If a Func has a PredicateStores split, and a producer to it is compute_at inside the inner var of the split but stored outside of it, the promise_clamped that applies to the store index guarded by the store predicate can leak into bounds expressions and lie, creating out-of-bounds reads and writes in the producing Funcs due to underallocation of their buffers. I don't have a good fix for this. promise_clamped is a bit janky as a concept, unfortunately, because for it to be correct any other facts you the compiler deduces from the clamp only hold inside the guard the clamp is associated with, and we have no mechanism to enforce that.

Reproducing case

cpp
#include "Halide.h"
#include <cstdio>

using namespace Halide;
using namespace Halide::Internal;

int main() {
    Var y{"y"}, yo{"yo"}, yi{"yi"};
    Func f0{"f0"}, f1{"f1"}, f2{"f2"}, f3{"f3"};
    f0(y) = cast<uint8_t>(y);
    f1(y) = f0(y - 2) + f0(y + 2);
    f2(y) = f1(y * 2);
    f3(y) = f2(y * 2 - 2) + f2(y * 2 + 2);

    f3.bound(y, 0, 15);
    f3.split(y, yo, yi, 4, TailStrategy::PredicateStores);
    f0.compute_root();
    f1.store_root().compute_at(f2, Var::outermost());
    f2.hoist_storage(f3, Var::outermost()).compute_at(f3, yi);

    // promise_clamped leaks into dependent bounds expressions and causes
    // allocation bounds inference to make bad assumptions. Rather than trying
    // to detect memory corruption, unroll everything so the compiler can
    // statically check for out-of-bounds access. The bug will cause the
    // simplifier to detect out-of-bounds access and deduce the entire pipeline
    // is unreachable, deleting all code and leaving the output buffer
    // untouched.
    f0.unroll(y);
    f1.unroll(y);
    f2.unroll(y);
    f3.unroll(yo).unroll(yi);

    Buffer<uint8_t> out{15};
    out.fill(255);
    f3.realize(out);
    if (out(0) != 0) {
        printf("out(0) = %d instead of 0\n", out(0));
        return 1;
    }
    printf("done %d\n", out(0));
    return 0;
}

How did you get Halide?

None

Halide version

No response

Halide commit (if known)

03a18bb9c74adfee30372de9d704619a71e00baa

Target

No response

Operating system

No response

Additional context

No response