From 440da44a84022ec63e87e2c5a55ad03a244697a4 Mon Sep 17 00:00:00 2001 From: Lionel Landwerlin Date: Wed, 16 Nov 2022 20:34:24 +0200 Subject: [PATCH] anv: get rid of ilog2_round_up MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit __builtin_clz(value - 1) is undefined for with value=1 (because __builtin_clz(0) is undefined). Because we set rt_pipeline->stack_size = 1 when a ray tracing pipeline doesn't need any stack allocation to differentiate from a dynamic size (rt_pipeline->stack_size = 0) we can run into this undefinied behavior issue. Signed-off-by: Lionel Landwerlin Fixes: f68d64dac015 ("anv: Add support for vkCmdSetRayTracingPipelineStackSizeKHR") Reviewed-by: Marcin Ślusarz Part-of: --- src/intel/vulkan/anv_allocator.c | 21 ++++----------------- src/intel/vulkan/anv_cmd_buffer.c | 11 ++--------- 2 files changed, 6 insertions(+), 26 deletions(-) diff --git a/src/intel/vulkan/anv_allocator.c b/src/intel/vulkan/anv_allocator.c index 57c554d..12f502f 100644 --- a/src/intel/vulkan/anv_allocator.c +++ b/src/intel/vulkan/anv_allocator.c @@ -113,19 +113,6 @@ #define PAGE_SIZE 4096 #endif -static inline uint32_t -ilog2_round_up(uint32_t value) -{ - assert(value != 0); - return 32 - __builtin_clz(value - 1); -} - -static inline uint32_t -round_to_power_of_two(uint32_t value) -{ - return 1 << ilog2_round_up(value); -} - struct anv_state_table_cleanup { void *map; size_t size; @@ -734,7 +721,7 @@ anv_fixed_size_state_pool_alloc_new(struct anv_fixed_size_state_pool *pool, static uint32_t anv_state_pool_get_bucket(uint32_t size) { - unsigned size_log2 = ilog2_round_up(size); + unsigned size_log2 = util_logbase2_ceil(size); assert(size_log2 <= ANV_MAX_STATE_SIZE_LOG2); if (size_log2 < ANV_MIN_STATE_SIZE_LOG2) size_log2 = ANV_MIN_STATE_SIZE_LOG2; @@ -1023,7 +1010,7 @@ anv_state_stream_alloc(struct anv_state_stream *stream, if (offset + size > stream->block.alloc_size) { uint32_t block_size = stream->block_size; if (block_size < size) - block_size = round_to_power_of_two(size); + block_size = util_next_power_of_two(size); stream->block = anv_state_pool_alloc_no_vg(stream->state_pool, block_size, PAGE_SIZE); @@ -1143,7 +1130,7 @@ VkResult anv_bo_pool_alloc(struct anv_bo_pool *pool, uint32_t size, struct anv_bo **bo_out) { - const unsigned size_log2 = size < 4096 ? 12 : ilog2_round_up(size); + const unsigned size_log2 = size < 4096 ? 12 : util_logbase2_ceil(size); const unsigned pow2_size = 1 << size_log2; const unsigned bucket = size_log2 - 12; assert(bucket < ARRAY_SIZE(pool->free_list)); @@ -1180,7 +1167,7 @@ anv_bo_pool_free(struct anv_bo_pool *pool, struct anv_bo *bo) VG(VALGRIND_MEMPOOL_FREE(pool, bo->map)); assert(util_is_power_of_two_or_zero(bo->size)); - const unsigned size_log2 = ilog2_round_up(bo->size); + const unsigned size_log2 = util_logbase2_ceil(bo->size); const unsigned bucket = size_log2 - 12; assert(bucket < ARRAY_SIZE(pool->free_list)); diff --git a/src/intel/vulkan/anv_cmd_buffer.c b/src/intel/vulkan/anv_cmd_buffer.c index 969d344..220c771 100644 --- a/src/intel/vulkan/anv_cmd_buffer.c +++ b/src/intel/vulkan/anv_cmd_buffer.c @@ -282,13 +282,6 @@ set_dirty_for_bind_map(struct anv_cmd_buffer *cmd_buffer, cmd_buffer->state.push_constants_dirty |= mesa_to_vk_shader_stage(stage); } -static inline uint32_t -ilog2_round_up(uint32_t value) -{ - assert(value != 0); - return 32 - __builtin_clz(value - 1); -} - static void anv_cmd_buffer_set_ray_query_buffer(struct anv_cmd_buffer *cmd_buffer, struct anv_cmd_pipeline_state *pipeline_state, @@ -304,7 +297,7 @@ anv_cmd_buffer_set_ray_query_buffer(struct anv_cmd_buffer *cmd_buffer, if (ray_shadow_size > 0 && (!cmd_buffer->state.ray_query_shadow_bo || cmd_buffer->state.ray_query_shadow_bo->size < ray_shadow_size)) { - unsigned shadow_size_log2 = MAX2(ilog2_round_up(ray_shadow_size), 16); + unsigned shadow_size_log2 = MAX2(util_logbase2_ceil(ray_shadow_size), 16); unsigned bucket = shadow_size_log2 - 16; assert(bucket < ARRAY_SIZE(device->ray_query_shadow_bos)); @@ -1040,7 +1033,7 @@ void anv_CmdSetRayTracingPipelineStackSizeKHR( uint32_t stack_ids_per_dss = 2048; /* TODO */ - unsigned stack_size_log2 = ilog2_round_up(pipelineStackSize); + unsigned stack_size_log2 = util_logbase2_ceil(pipelineStackSize); if (stack_size_log2 < 10) stack_size_log2 = 10; -- 2.7.4