Skip to content

Commit

Permalink
Silence "may be used uninitialized" in Buffer::for_each_element() (#6747
Browse files Browse the repository at this point in the history
)

In at least one version of GCC (Debian 11.2.0-16+build1), an optimized build using `Buffer::for_each_element(int *pos)` will give (incorrect) compiler warnings/errors that "pos may be used uninitialized).

From inspection of the code I feel pretty sure this is a false positive -- i.e., the optimizer is confused -- and since no other compiler we've encountered issues a similar warning (nor do we see actual misbehavior), I'm inclined not to worry -- but the warning does break some build configurations.

Rather than try to fight with selectively disabling this warning, I'm going to propose inserting a memset() here to reassure the compiler that the memory really is initialized; while it's unnnecessary, it's likely to be insignificant compared to the cost of usual calls to for_each_element().

(BTW, this is not a new issue, I've seen it for quite a while as this GCC is the default on one of my Linux machines... it just finally annoyed me enough to want to make it shut up.)
  • Loading branch information
steven-johnson committed May 5, 2022
1 parent 1606039 commit c8531a5
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/runtime/HalideBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -2286,7 +2286,11 @@ class Buffer {
template<typename Fn,
typename = decltype(std::declval<Fn>()((const int *)nullptr))>
static void for_each_element(int, int dims, const for_each_element_task_dim *t, Fn &&f, int check = 0) {
int *pos = (int *)HALIDE_ALLOCA(dims * sizeof(int));
const int size = dims * sizeof(int);
int *pos = (int *)HALIDE_ALLOCA(size);
// At least one version of GCC will (incorrectly) report that pos "may be used uninitialized".
// Add this memset to silence it.
memset(pos, 0, size);
for_each_element_array(dims - 1, t, std::forward<Fn>(f), pos);
}

Expand Down

0 comments on commit c8531a5

Please sign in to comment.