From f9477770d84becad51f34eb149b8333a73bedd0f Mon Sep 17 00:00:00 2001 From: Paulo Zanoni Date: Thu, 12 Jan 2023 14:27:23 -0800 Subject: [PATCH] anv: use vk_realloc for the anv_execbuf arrays Three reasons for that: 0. The operation we're doing here is actually a reallocation. 1. The newer code is, IMHO, easier to read. 2. Realloc has this property where sometimes, when possible, it will expand your array without moving it somewhere else, so it doesn't need to copy the memory contents, returning the original pointer back to you. I did some analysis and while that case is not common, it does happen sometimes in real world applications (I could see it happening in Shootergame and Aztec Ruins, but not Dota 2), so we're able to save a few CPU cycles. v2: Rebase. Reviewed-by: Ivan Briano Reviewed-by: Lionel Landwerlin Signed-off-by: Paulo Zanoni Part-of: --- src/intel/vulkan/i915/anv_batch_chain.c | 46 +++++++++---------------- 1 file changed, 16 insertions(+), 30 deletions(-) diff --git a/src/intel/vulkan/i915/anv_batch_chain.c b/src/intel/vulkan/i915/anv_batch_chain.c index ece13cfbc05..43fa371b698 100644 --- a/src/intel/vulkan/i915/anv_batch_chain.c +++ b/src/intel/vulkan/i915/anv_batch_chain.c @@ -109,28 +109,19 @@ anv_execbuf_add_bo(struct anv_device *device, uint32_t new_len = exec->objects ? exec->bo_array_length * 2 : 64; struct drm_i915_gem_exec_object2 *new_objects = - vk_alloc(exec->alloc, new_len * sizeof(*new_objects), 8, exec->alloc_scope); + vk_realloc(exec->alloc, exec->objects, + new_len * sizeof(*new_objects), 8, exec->alloc_scope); if (new_objects == NULL) return vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY); - struct anv_bo **new_bos = - vk_alloc(exec->alloc, new_len * sizeof(*new_bos), 8, exec->alloc_scope); - if (new_bos == NULL) { - vk_free(exec->alloc, new_objects); - return vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY); - } - - if (exec->objects) { - memcpy(new_objects, exec->objects, - exec->bo_count * sizeof(*new_objects)); - memcpy(new_bos, exec->bos, - exec->bo_count * sizeof(*new_bos)); - } - - vk_free(exec->alloc, exec->objects); - vk_free(exec->alloc, exec->bos); - exec->objects = new_objects; + + struct anv_bo **new_bos = + vk_realloc(exec->alloc, exec->bos, new_len * sizeof(*new_bos), 8, + exec->alloc_scope); + if (new_bos == NULL) + return vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY); + exec->bos = new_bos; exec->bo_array_length = new_len; } @@ -200,26 +191,21 @@ anv_execbuf_add_syncobj(struct anv_device *device, uint32_t new_len = MAX2(exec->syncobj_array_length * 2, 16); struct drm_i915_gem_exec_fence *new_syncobjs = - vk_alloc(exec->alloc, new_len * sizeof(*new_syncobjs), - 8, exec->alloc_scope); - if (!new_syncobjs) + vk_realloc(exec->alloc, exec->syncobjs, + new_len * sizeof(*new_syncobjs), 8, exec->alloc_scope); + if (new_syncobjs == NULL) return vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY); - if (exec->syncobjs) - typed_memcpy(new_syncobjs, exec->syncobjs, exec->syncobj_count); - exec->syncobjs = new_syncobjs; if (exec->syncobj_values) { uint64_t *new_syncobj_values = - vk_alloc(exec->alloc, new_len * sizeof(*new_syncobj_values), - 8, exec->alloc_scope); - if (!new_syncobj_values) + vk_realloc(exec->alloc, exec->syncobj_values, + new_len * sizeof(*new_syncobj_values), 8, + exec->alloc_scope); + if (new_syncobj_values == NULL) return vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY); - typed_memcpy(new_syncobj_values, exec->syncobj_values, - exec->syncobj_count); - exec->syncobj_values = new_syncobj_values; }