anv: get rid of ilog2_round_up

__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 <lionel.g.landwerlin@intel.com>
Fixes: f68d64dac0 ("anv: Add support for vkCmdSetRayTracingPipelineStackSizeKHR")
Reviewed-by: Marcin Ślusarz <marcin.slusarz@intel.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/19781>
This commit is contained in:
Lionel Landwerlin
2022-11-16 20:34:24 +02:00
committed by Marge Bot
parent a61378859c
commit 440da44a84
2 changed files with 6 additions and 26 deletions

View File

@@ -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));

View File

@@ -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;