[Mesa-dev] [PATCH 2/3] anv: Refactor reloc handling in execbuf_add_bo

Jason Ekstrand jason at jlekstrand.net
Fri Jun 1 23:12:25 UTC 2018


This just separates the reloc list vs. BO set cases and lets us avoid an
allocation if relocs->deps->entries == 0.
---
 src/intel/vulkan/anv_batch_chain.c | 78 ++++++++++++++++++++------------------
 1 file changed, 42 insertions(+), 36 deletions(-)

diff --git a/src/intel/vulkan/anv_batch_chain.c b/src/intel/vulkan/anv_batch_chain.c
index 5100450..de629c1 100644
--- a/src/intel/vulkan/anv_batch_chain.c
+++ b/src/intel/vulkan/anv_batch_chain.c
@@ -1091,52 +1091,58 @@ anv_execbuf_add_bo(struct anv_execbuf *exec,
       obj->rsvd2 = 0;
    }
 
-   if (relocs != NULL && obj->relocation_count == 0) {
-      /* This is the first time we've ever seen a list of relocations for
-       * this BO.  Go ahead and set the relocations and then walk the list
-       * of relocations and add them all.
-       */
-      obj->relocation_count = relocs->num_relocs;
-      obj->relocs_ptr = (uintptr_t) relocs->relocs;
+   if (relocs != NULL) {
+      assert(obj->relocation_count == 0);
 
-      for (size_t i = 0; i < relocs->num_relocs; i++) {
-         VkResult result;
+      if (relocs->num_relocs > 0) {
+         /* This is the first time we've ever seen a list of relocations for
+          * this BO.  Go ahead and set the relocations and then walk the list
+          * of relocations and add them all.
+          */
+         obj->relocation_count = relocs->num_relocs;
+         obj->relocs_ptr = (uintptr_t) relocs->relocs;
 
-         /* A quick sanity check on relocations */
-         assert(relocs->relocs[i].offset < bo->size);
-         result = anv_execbuf_add_bo(exec, relocs->reloc_bos[i], NULL,
-                                     extra_flags, alloc);
+         for (size_t i = 0; i < relocs->num_relocs; i++) {
+            VkResult result;
 
-         if (result != VK_SUCCESS)
-            return result;
+            /* A quick sanity check on relocations */
+            assert(relocs->relocs[i].offset < bo->size);
+            result = anv_execbuf_add_bo(exec, relocs->reloc_bos[i], NULL,
+                                        extra_flags, alloc);
+
+            if (result != VK_SUCCESS)
+               return result;
+         }
       }
 
-      const uint32_t entries = relocs->deps->entries;
-      struct anv_bo **bos =
-         vk_alloc(alloc, entries * sizeof(*bos),
-                  8, VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
-      if (bos == NULL)
-         return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
+      if (relocs->deps && relocs->deps->entries > 0) {
+         const uint32_t entries = relocs->deps->entries;
+         struct anv_bo **bos =
+            vk_alloc(alloc, entries * sizeof(*bos),
+                     8, VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
+         if (bos == NULL)
+            return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
 
-      struct set_entry *entry;
-      struct anv_bo **bo = bos;
-      set_foreach(relocs->deps, entry) {
-         *bo++ = (void *)entry->key;
-      }
+         struct set_entry *entry;
+         struct anv_bo **bo = bos;
+         set_foreach(relocs->deps, entry) {
+            *bo++ = (void *)entry->key;
+         }
 
-      qsort(bos, entries, sizeof(struct anv_bo*), _compare_bo_handles);
+         qsort(bos, entries, sizeof(struct anv_bo*), _compare_bo_handles);
 
-      VkResult result = VK_SUCCESS;
-      for (bo = bos; bo < bos + entries; bo++) {
-         result = anv_execbuf_add_bo(exec, *bo, NULL, extra_flags, alloc);
-         if (result != VK_SUCCESS)
-            break;
-      }
+         VkResult result = VK_SUCCESS;
+         for (bo = bos; bo < bos + entries; bo++) {
+            result = anv_execbuf_add_bo(exec, *bo, NULL, extra_flags, alloc);
+            if (result != VK_SUCCESS)
+               break;
+         }
 
-      vk_free(alloc, bos);
+         vk_free(alloc, bos);
 
-      if (result != VK_SUCCESS)
-         return result;
+         if (result != VK_SUCCESS)
+            return result;
+      }
    }
 
    return VK_SUCCESS;
-- 
2.5.0.400.gff86faf



More information about the mesa-dev mailing list