From c8531a594b84031bd02339fc6fe8381663cd517c Mon Sep 17 00:00:00 2001 From: Steven Johnson Date: Wed, 4 May 2022 18:17:40 -0700 Subject: [PATCH] Silence "may be used uninitialized" in Buffer::for_each_element() (#6747) 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.) --- src/runtime/HalideBuffer.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/runtime/HalideBuffer.h b/src/runtime/HalideBuffer.h index c0191f2af272..1cae329f4a00 100644 --- a/src/runtime/HalideBuffer.h +++ b/src/runtime/HalideBuffer.h @@ -2286,7 +2286,11 @@ class Buffer { template()((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(f), pos); }