mesa/src/vulkan/runtime/vk_sync_binary.c
Paulo Zanoni b0653370d0 vulkan: don't zero-initialize STACK_ARRAY()'s stack array
STACK_ARRAY() is used in a lot of places. When games are running we
see STACK_ARRAY() arrays being used all the time: each queue
submission uses 6, WaitSemaphores and syncobj waiting also uses them:
they're constantly present in Vulkan runtime.

There's no need for STACK_ARRAY()'s stack array to be initialized,
callers cannot not depend on it. If the number of elements is greater
than STACK_ARRAY_SIZE, then STACK_ARRAY() will just malloc() the array
and return it not initialized: anybody depending of
zero-initialization is going to break when the array is big.

The reason why we're zero-intializing STACK_ARRAY()'s stack array is
to silence -Wmaybe-uninitialized warnings: see commit d7957df318
("vulkan: fix uninitialized variables"). I don't think that commit is
the ideal way to deal with the problem, so this patch proposes a
better solution.

The problem here is that zero-initializing it adds code we don't need
for every single caller. STACK_ARRAY() already has 63 callers and only
3 of them are affected by the -Wmaybe-uninitialized warining. So here
we undo what commit d7957df318 did and instead we fix the 3 cases
that actually generate the -Wmaybe-uninitialized warnings.

Gcc is only emitting those warinings because it knows that the number
of elements in the array may be zero, so the loops we have that set
elements to the array may end up do nothing, and then we pass the
array uninitialized to other functions.

For the cases related to vk_sync this is just returning VK_SUCCESS
earlier, instead of relying on the check that eventually happens at
__vk_sync_wait_many(). For the vkCmdWaitEvents() function, the Vulkan
spec says that "eventCount must be greater than 0", so the early
return doesn't hurt anybody either. In both cases we make the zero
case faster by not defining an 8-sized array, zero-initializing it,
then returning success without using it.

Reference: d7957df318 ("vulkan: fix uninitialized variables")
Acked-by: Yonggang Luo <luoyonggang@gmail.com>
Reviewed-by: Yiwei Zhang <zzyiwei@chromium.org>
Signed-off-by: Paulo Zanoni <paulo.r.zanoni@intel.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/28288>
2024-04-08 17:23:25 +00:00

141 lines
4.5 KiB
C

/*
* Copyright © 2021 Intel Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include "vk_sync_binary.h"
#include "vk_util.h"
static struct vk_sync_binary *
to_vk_sync_binary(struct vk_sync *sync)
{
assert(sync->type->init == vk_sync_binary_init);
return container_of(sync, struct vk_sync_binary, sync);
}
VkResult
vk_sync_binary_init(struct vk_device *device,
struct vk_sync *sync,
uint64_t initial_value)
{
struct vk_sync_binary *binary = to_vk_sync_binary(sync);
const struct vk_sync_binary_type *btype =
container_of(binary->sync.type, struct vk_sync_binary_type, sync);
assert(!(sync->flags & VK_SYNC_IS_TIMELINE));
assert(!(sync->flags & VK_SYNC_IS_SHAREABLE));
binary->next_point = (initial_value == 0);
return vk_sync_init(device, &binary->timeline, btype->timeline_type,
VK_SYNC_IS_TIMELINE, 0 /* initial_value */);
}
static void
vk_sync_binary_finish(struct vk_device *device,
struct vk_sync *sync)
{
struct vk_sync_binary *binary = to_vk_sync_binary(sync);
vk_sync_finish(device, &binary->timeline);
}
static VkResult
vk_sync_binary_reset(struct vk_device *device,
struct vk_sync *sync)
{
struct vk_sync_binary *binary = to_vk_sync_binary(sync);
binary->next_point++;
return VK_SUCCESS;
}
static VkResult
vk_sync_binary_signal(struct vk_device *device,
struct vk_sync *sync,
uint64_t value)
{
struct vk_sync_binary *binary = to_vk_sync_binary(sync);
assert(value == 0);
return vk_sync_signal(device, &binary->timeline, binary->next_point);
}
static VkResult
vk_sync_binary_wait_many(struct vk_device *device,
uint32_t wait_count,
const struct vk_sync_wait *waits,
enum vk_sync_wait_flags wait_flags,
uint64_t abs_timeout_ns)
{
if (wait_count == 0)
return VK_SUCCESS;
STACK_ARRAY(struct vk_sync_wait, timeline_waits, wait_count);
for (uint32_t i = 0; i < wait_count; i++) {
struct vk_sync_binary *binary = to_vk_sync_binary(waits[i].sync);
timeline_waits[i] = (struct vk_sync_wait) {
.sync = &binary->timeline,
.stage_mask = waits[i].stage_mask,
.wait_value = binary->next_point,
};
}
VkResult result = vk_sync_wait_many(device, wait_count, timeline_waits,
wait_flags, abs_timeout_ns);
STACK_ARRAY_FINISH(timeline_waits);
return result;
}
struct vk_sync_binary_type
vk_sync_binary_get_type(const struct vk_sync_type *timeline_type)
{
assert(timeline_type->features & VK_SYNC_FEATURE_TIMELINE);
return (struct vk_sync_binary_type) {
.sync = {
.size = offsetof(struct vk_sync_binary, timeline) +
timeline_type->size,
.features = VK_SYNC_FEATURE_BINARY |
VK_SYNC_FEATURE_GPU_WAIT |
VK_SYNC_FEATURE_CPU_WAIT |
VK_SYNC_FEATURE_CPU_RESET |
VK_SYNC_FEATURE_CPU_SIGNAL |
VK_SYNC_FEATURE_WAIT_ANY |
VK_SYNC_FEATURE_WAIT_PENDING,
.init = vk_sync_binary_init,
.finish = vk_sync_binary_finish,
.reset = vk_sync_binary_reset,
.signal = vk_sync_binary_signal,
.wait_many = vk_sync_binary_wait_many,
},
.timeline_type = timeline_type,
};
}