Skip to content

Commit

Permalink
undo reinterpret_cast
Browse files Browse the repository at this point in the history
  • Loading branch information
makslevental committed Oct 17, 2024
1 parent 00e1b36 commit 626057f
Show file tree
Hide file tree
Showing 10 changed files with 138 additions and 103 deletions.
18 changes: 13 additions & 5 deletions runtime/src/iree-amd-aie/driver/xrt-lite/allocator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ static iree_status_t iree_hal_xrt_lite_allocator_allocate_buffer(
IREE_TRACE_ZONE_BEGIN(z0);

iree_hal_xrt_lite_allocator* allocator =
reinterpret_cast<iree_hal_xrt_lite_allocator*>(base_allocator);
IREE_HAL_XRT_LITE_CHECKED_VTABLE_CAST(base_allocator,
iree_hal_xrt_lite_allocator_vtable,
iree_hal_xrt_lite_allocator);
iree_hal_buffer_params_t compat_params = *params;
iree_hal_buffer_compatibility_t compatibility =
iree_hal_xrt_lite_allocator_query_buffer_compatibility(
Expand Down Expand Up @@ -118,7 +120,9 @@ static void iree_hal_xrt_lite_allocator_deallocate_buffer(
IREE_TRACE_ZONE_BEGIN(z0);

iree_hal_xrt_lite_allocator* allocator =
reinterpret_cast<iree_hal_xrt_lite_allocator*>(base_allocator);
IREE_HAL_XRT_LITE_CHECKED_VTABLE_CAST(base_allocator,
iree_hal_xrt_lite_allocator_vtable,
iree_hal_xrt_lite_allocator);
bool was_imported = false;
if (!was_imported) {
IREE_STATISTICS(iree_hal_allocator_statistics_record_free(
Expand Down Expand Up @@ -157,10 +161,12 @@ iree_status_t iree_hal_xrt_lite_allocator_create(
static void iree_hal_xrt_lite_allocator_destroy(
iree_hal_allocator_t* base_allocator) {
IREE_ASSERT_ARGUMENT(base_allocator);
iree_hal_xrt_lite_allocator* allocator =
reinterpret_cast<iree_hal_xrt_lite_allocator*>(base_allocator);
IREE_TRACE_ZONE_BEGIN(z0);

iree_hal_xrt_lite_allocator* allocator =
IREE_HAL_XRT_LITE_CHECKED_VTABLE_CAST(base_allocator,
iree_hal_xrt_lite_allocator_vtable,
iree_hal_xrt_lite_allocator);
iree_hal_resource_release(&allocator->resource);
iree_allocator_free(allocator->host_allocator, allocator);

Expand All @@ -172,7 +178,9 @@ static iree_allocator_t iree_hal_xrt_lite_allocator_host_allocator(
IREE_TRACE_ZONE_BEGIN(z0);

const iree_hal_xrt_lite_allocator* allocator =
reinterpret_cast<const iree_hal_xrt_lite_allocator*>(base_allocator);
IREE_HAL_XRT_LITE_CHECKED_VTABLE_CAST(base_allocator,
iree_hal_xrt_lite_allocator_vtable,
const iree_hal_xrt_lite_allocator);

IREE_TRACE_ZONE_END(z0);
return allocator->host_allocator;
Expand Down
26 changes: 13 additions & 13 deletions runtime/src/iree-amd-aie/driver/xrt-lite/buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ static iree_status_t iree_hal_xrt_lite_buffer_invalidate_range(
iree_device_size_t local_byte_length) {
IREE_TRACE_ZONE_BEGIN(z0);

iree_hal_xrt_lite_buffer* buffer =
reinterpret_cast<iree_hal_xrt_lite_buffer*>(base_buffer);
iree_hal_xrt_lite_buffer* buffer = IREE_HAL_XRT_LITE_CHECKED_VTABLE_CAST(
base_buffer, iree_hal_xrt_lite_buffer_vtable, iree_hal_xrt_lite_buffer);
if (IREE_UNLIKELY(!buffer->bo)) {
IREE_TRACE_ZONE_END(z0);
return iree_make_status(
Expand All @@ -46,8 +46,8 @@ static iree_status_t iree_hal_xrt_lite_buffer_map_range(
iree_hal_buffer_mapping_t* mapping) {
IREE_TRACE_ZONE_BEGIN(z0);

iree_hal_xrt_lite_buffer* buffer =
reinterpret_cast<iree_hal_xrt_lite_buffer*>(base_buffer);
iree_hal_xrt_lite_buffer* buffer = IREE_HAL_XRT_LITE_CHECKED_VTABLE_CAST(
base_buffer, iree_hal_xrt_lite_buffer_vtable, iree_hal_xrt_lite_buffer);
IREE_RETURN_AND_END_ZONE_IF_ERROR(
z0, iree_hal_buffer_validate_memory_type(
iree_hal_buffer_memory_type(
Expand Down Expand Up @@ -87,8 +87,8 @@ static iree_status_t iree_hal_xrt_lite_buffer_flush_range(
iree_device_size_t local_byte_length) {
IREE_TRACE_ZONE_BEGIN(z0);

iree_hal_xrt_lite_buffer* buffer =
reinterpret_cast<iree_hal_xrt_lite_buffer*>(base_buffer);
iree_hal_xrt_lite_buffer* buffer = IREE_HAL_XRT_LITE_CHECKED_VTABLE_CAST(
base_buffer, iree_hal_xrt_lite_buffer_vtable, iree_hal_xrt_lite_buffer);
if (IREE_UNLIKELY(!buffer->bo)) {
IREE_TRACE_ZONE_END(z0);
return iree_make_status(
Expand Down Expand Up @@ -123,8 +123,8 @@ iree_status_t iree_hal_xrt_lite_buffer_wrap(

iree_hal_xrt_lite_buffer* buffer = nullptr;
IREE_RETURN_AND_END_ZONE_IF_ERROR(
z0,
iree_allocator_malloc(host_allocator, sizeof(*buffer), (void**)&buffer));
z0, iree_allocator_malloc(host_allocator, sizeof(*buffer),
reinterpret_cast<void**>(&buffer)));
iree_hal_buffer_initialize(host_allocator, allocator, &buffer->base,
allocation_size, byte_offset, byte_length,
memory_type, allowed_access, allowed_usage,
Expand All @@ -138,11 +138,11 @@ iree_status_t iree_hal_xrt_lite_buffer_wrap(
}

static void iree_hal_xrt_lite_buffer_destroy(iree_hal_buffer_t* base_buffer) {
iree_hal_xrt_lite_buffer* buffer =
reinterpret_cast<iree_hal_xrt_lite_buffer*>(base_buffer);
iree_allocator_t host_allocator = base_buffer->host_allocator;
iree_hal_xrt_lite_buffer* buffer = IREE_HAL_XRT_LITE_CHECKED_VTABLE_CAST(
base_buffer, iree_hal_xrt_lite_buffer_vtable, iree_hal_xrt_lite_buffer);
IREE_TRACE_ZONE_BEGIN(z0);

iree_allocator_t host_allocator = base_buffer->host_allocator;
if (buffer->release_callback.fn) {
buffer->release_callback.fn(buffer->release_callback.user_data,
base_buffer);
Expand All @@ -157,8 +157,8 @@ static void iree_hal_xrt_lite_buffer_destroy(iree_hal_buffer_t* base_buffer) {
shim_xdna::bo* iree_hal_xrt_lite_buffer_handle(iree_hal_buffer_t* base_buffer) {
IREE_TRACE_ZONE_BEGIN(z0);

iree_hal_xrt_lite_buffer* buffer =
reinterpret_cast<iree_hal_xrt_lite_buffer*>(base_buffer);
iree_hal_xrt_lite_buffer* buffer = IREE_HAL_XRT_LITE_CHECKED_VTABLE_CAST(
base_buffer, iree_hal_xrt_lite_buffer_vtable, iree_hal_xrt_lite_buffer);

IREE_TRACE_ZONE_END(z0);
return buffer->bo;
Expand Down
74 changes: 38 additions & 36 deletions runtime/src/iree-amd-aie/driver/xrt-lite/device.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ extern const iree_hal_device_vtable_t iree_hal_xrt_lite_device_vtable;

struct iree_hal_xrt_lite_device {
iree_hal_resource_t resource;
iree_string_view_t identifier;
iree_allocator_t host_allocator;
// TODO(max): not used because "device allocations" are performed through
// device
Expand All @@ -32,6 +31,9 @@ struct iree_hal_xrt_lite_device {
// since command buffers can contain inlined data
iree_arena_block_pool_t block_pool;
shim_xdna::device* shim_device;
// should come last; see the definition of total_size below in
// iree_hal_xrt_lite_device_create
iree_string_view_t identifier;

iree_hal_xrt_lite_device(const iree_hal_xrt_lite_device_options* options,
iree_allocator_t host_allocator) {
Expand All @@ -53,12 +55,12 @@ struct iree_hal_xrt_lite_device {
};

static iree_status_t iree_hal_xrt_lite_device_create_executable_cache(
iree_hal_device_t* base_value, iree_string_view_t identifier,
iree_hal_device_t* base_device, iree_string_view_t identifier,
iree_loop_t loop, iree_hal_executable_cache_t** out_executable_cache) {
IREE_TRACE_ZONE_BEGIN(z0);

iree_hal_xrt_lite_device* device =
reinterpret_cast<iree_hal_xrt_lite_device*>(base_value);
iree_hal_xrt_lite_device* device = IREE_HAL_XRT_LITE_CHECKED_VTABLE_CAST(
base_device, iree_hal_xrt_lite_device_vtable, iree_hal_xrt_lite_device);

IREE_TRACE_ZONE_END(z0);
return iree_hal_xrt_lite_nop_executable_cache_create(
Expand All @@ -67,7 +69,7 @@ static iree_status_t iree_hal_xrt_lite_device_create_executable_cache(
}

static iree_status_t iree_hal_xrt_lite_device_create_command_buffer(
iree_hal_device_t* base_value, iree_hal_command_buffer_mode_t mode,
iree_hal_device_t* base_device, iree_hal_command_buffer_mode_t mode,
iree_hal_command_category_t command_categories,
iree_hal_queue_affinity_t queue_affinity, iree_host_size_t binding_capacity,
iree_hal_command_buffer_t** out_command_buffer) {
Expand All @@ -79,8 +81,8 @@ static iree_status_t iree_hal_xrt_lite_device_create_command_buffer(
"unimplmented multi-shot command buffer");
}

iree_hal_xrt_lite_device* device =
reinterpret_cast<iree_hal_xrt_lite_device*>(base_value);
iree_hal_xrt_lite_device* device = IREE_HAL_XRT_LITE_CHECKED_VTABLE_CAST(
base_device, iree_hal_xrt_lite_device_vtable, iree_hal_xrt_lite_device);

IREE_TRACE_ZONE_END(z0);
return iree_hal_deferred_command_buffer_create(
Expand All @@ -89,29 +91,29 @@ static iree_status_t iree_hal_xrt_lite_device_create_command_buffer(
}

static iree_status_t iree_hal_xrt_lite_device_create_semaphore(
iree_hal_device_t* base_value, uint64_t initial_value,
iree_hal_device_t* base_device, uint64_t initial_value,
iree_hal_semaphore_flags_t flags, iree_hal_semaphore_t** out_semaphore) {
IREE_TRACE_ZONE_BEGIN(z0);

iree_hal_xrt_lite_device* device =
reinterpret_cast<iree_hal_xrt_lite_device*>(base_value);
iree_hal_xrt_lite_device* device = IREE_HAL_XRT_LITE_CHECKED_VTABLE_CAST(
base_device, iree_hal_xrt_lite_device_vtable, iree_hal_xrt_lite_device);

IREE_TRACE_ZONE_END(z0);
return iree_hal_xrt_lite_semaphore_create(device->host_allocator,
initial_value, out_semaphore);
}

static iree_status_t iree_hal_xrt_lite_device_queue_execute(
iree_hal_device_t* base_value, iree_hal_queue_affinity_t queue_affinity,
iree_hal_device_t* base_device, iree_hal_queue_affinity_t queue_affinity,
const iree_hal_semaphore_list_t wait_semaphore_list,
const iree_hal_semaphore_list_t signal_semaphore_list,
iree_host_size_t command_buffer_count,
iree_hal_command_buffer_t* const* command_buffers,
iree_hal_buffer_binding_table_t const* binding_tables) {
IREE_TRACE_ZONE_BEGIN(z0);

iree_hal_xrt_lite_device* device =
reinterpret_cast<iree_hal_xrt_lite_device*>(base_value);
iree_hal_xrt_lite_device* device = IREE_HAL_XRT_LITE_CHECKED_VTABLE_CAST(
base_device, iree_hal_xrt_lite_device_vtable, iree_hal_xrt_lite_device);

for (iree_host_size_t i = 0; i < command_buffer_count; i++) {
iree_hal_command_buffer_t* xrt_command_buffer = nullptr;
Expand All @@ -136,26 +138,26 @@ static iree_status_t iree_hal_xrt_lite_device_queue_execute(
}

static void iree_hal_xrt_lite_device_replace_device_allocator(
iree_hal_device_t* base_value, iree_hal_allocator_t* new_allocator) {
iree_hal_device_t* base_device, iree_hal_allocator_t* new_allocator) {
IREE_TRACE_ZONE_BEGIN(z0);

iree_hal_allocator_retain(new_allocator);
iree_hal_xrt_lite_device* device =
reinterpret_cast<iree_hal_xrt_lite_device*>(base_value);
iree_hal_xrt_lite_device* device = IREE_HAL_XRT_LITE_CHECKED_VTABLE_CAST(
base_device, iree_hal_xrt_lite_device_vtable, iree_hal_xrt_lite_device);
device->device_allocator = new_allocator;
iree_hal_allocator_release(device->device_allocator);

IREE_TRACE_ZONE_END(z0);
}

static iree_status_t iree_hal_xrt_lite_device_query_i64(
iree_hal_device_t* base_value, iree_string_view_t category,
iree_hal_device_t* base_device, iree_string_view_t category,
iree_string_view_t key, int64_t* out_value) {
IREE_TRACE_ZONE_BEGIN(z0);

*out_value = 0;
iree_hal_xrt_lite_device* device =
reinterpret_cast<iree_hal_xrt_lite_device*>(base_value);
iree_hal_xrt_lite_device* device = IREE_HAL_XRT_LITE_CHECKED_VTABLE_CAST(
base_device, iree_hal_xrt_lite_device_vtable, iree_hal_xrt_lite_device);
if (iree_string_view_equal(category, IREE_SV("hal.device.id"))) {
*out_value =
iree_string_view_match_pattern(device->identifier, key) ? 1 : 0;
Expand All @@ -172,16 +174,16 @@ static iree_status_t iree_hal_xrt_lite_device_query_i64(
}

static iree_status_t iree_hal_xrt_lite_device_queue_alloca(
iree_hal_device_t* base_value, iree_hal_queue_affinity_t queue_affinity,
iree_hal_device_t* base_device, iree_hal_queue_affinity_t queue_affinity,
const iree_hal_semaphore_list_t wait_semaphore_list,
const iree_hal_semaphore_list_t signal_semaphore_list,
iree_hal_allocator_pool_t pool, iree_hal_buffer_params_t params,
iree_device_size_t allocation_size,
iree_hal_buffer_t** IREE_RESTRICT out_buffer) {
IREE_TRACE_ZONE_BEGIN(z0);

iree_hal_xrt_lite_device* device =
reinterpret_cast<iree_hal_xrt_lite_device*>(base_value);
iree_hal_xrt_lite_device* device = IREE_HAL_XRT_LITE_CHECKED_VTABLE_CAST(
base_device, iree_hal_xrt_lite_device_vtable, iree_hal_xrt_lite_device);
IREE_RETURN_AND_END_ZONE_IF_ERROR(
z0, iree_hal_semaphore_list_wait(wait_semaphore_list,
iree_infinite_timeout()));
Expand All @@ -196,21 +198,21 @@ static iree_status_t iree_hal_xrt_lite_device_queue_alloca(
}

static iree_string_view_t iree_hal_xrt_lite_device_id(
iree_hal_device_t* base_value) {
iree_hal_device_t* base_device) {
IREE_TRACE_ZONE_BEGIN(z0);

iree_hal_xrt_lite_device* device =
reinterpret_cast<iree_hal_xrt_lite_device*>(base_value);
iree_hal_xrt_lite_device* device = IREE_HAL_XRT_LITE_CHECKED_VTABLE_CAST(
base_device, iree_hal_xrt_lite_device_vtable, iree_hal_xrt_lite_device);

IREE_TRACE_ZONE_END(z0);
return device->identifier;
}

static void iree_hal_xrt_lite_device_destroy(iree_hal_device_t* base_value) {
static void iree_hal_xrt_lite_device_destroy(iree_hal_device_t* base_device) {
IREE_TRACE_ZONE_BEGIN(z0);

iree_hal_xrt_lite_device* device =
reinterpret_cast<iree_hal_xrt_lite_device*>(base_value);
iree_hal_xrt_lite_device* device = IREE_HAL_XRT_LITE_CHECKED_VTABLE_CAST(
base_device, iree_hal_xrt_lite_device_vtable, iree_hal_xrt_lite_device);

iree_hal_allocator_release(device->device_allocator);
delete device->shim_device;
Expand All @@ -220,22 +222,22 @@ static void iree_hal_xrt_lite_device_destroy(iree_hal_device_t* base_value) {
};

static iree_allocator_t iree_hal_xrt_lite_device_host_allocator(
iree_hal_device_t* base_value) {
iree_hal_device_t* base_device) {
IREE_TRACE_ZONE_BEGIN(z0);

iree_hal_xrt_lite_device* device =
reinterpret_cast<iree_hal_xrt_lite_device*>(base_value);
iree_hal_xrt_lite_device* device = IREE_HAL_XRT_LITE_CHECKED_VTABLE_CAST(
base_device, iree_hal_xrt_lite_device_vtable, iree_hal_xrt_lite_device);

IREE_TRACE_ZONE_END(z0);
return device->host_allocator;
}

static iree_hal_allocator_t* iree_hal_xrt_lite_device_device_allocator(
iree_hal_device_t* base_value) {
iree_hal_device_t* base_device) {
IREE_TRACE_ZONE_BEGIN(z0);

iree_hal_xrt_lite_device* device =
reinterpret_cast<iree_hal_xrt_lite_device*>(base_value);
iree_hal_xrt_lite_device* device = IREE_HAL_XRT_LITE_CHECKED_VTABLE_CAST(
base_device, iree_hal_xrt_lite_device_vtable, iree_hal_xrt_lite_device);

IREE_TRACE_ZONE_END(z0);
return device->device_allocator;
Expand All @@ -262,8 +264,8 @@ iree_status_t iree_hal_xrt_lite_device_create(

iree_hal_xrt_lite_device* device = nullptr;
iree_host_size_t total_size = iree_sizeof_struct(*device) + identifier.size;
IREE_RETURN_IF_ERROR(
iree_allocator_malloc(host_allocator, total_size, (void**)&device));
IREE_RETURN_IF_ERROR(iree_allocator_malloc(
host_allocator, total_size, reinterpret_cast<void**>(&device)));
device = new (device) iree_hal_xrt_lite_device(options, host_allocator);
iree_string_view_append_to_buffer(
identifier, &device->identifier,
Expand Down
Loading

0 comments on commit 626057f

Please sign in to comment.