blob: a6df8bd80e1b668d0f783567005d10fa4efe5275 [file]
// Copyright 2022 The IREE Authors
//
// Licensed under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#ifndef IREE_TOOLING_DEVICE_UTIL_H_
#define IREE_TOOLING_DEVICE_UTIL_H_
#include "iree/base/api.h"
#include "iree/hal/api.h"
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
// Returns a driver registry initialized with all linked driver registered.
iree_hal_driver_registry_t* iree_hal_available_driver_registry(void);
// Returns a device URI for a default device to be used when no other devices
// are specified by the user. It may not work; it's always better to specify
// flags and tools should encourage that.
iree_string_view_t iree_hal_default_device_uri(void);
// Returns a reference to the storage of the --device= flag.
// Changes to flags invalidate the storage.
iree_string_view_list_t iree_hal_device_flag_list(void);
// Creates a single device from the --device= flag.
// Uses the |default_device| if no flags were specified.
// Fails if more than one device was specified.
iree_status_t iree_hal_create_device_from_flags(
iree_hal_driver_registry_t* driver_registry,
iree_string_view_t default_device,
const iree_hal_device_create_params_t* create_params,
iree_allocator_t host_allocator, iree_hal_device_t** out_device);
// Creates one or more devices from the repeatable --device= flag.
// Uses the |default_device| if no flags were specified.
iree_status_t iree_hal_create_devices_from_flags(
iree_hal_driver_registry_t* driver_registry,
iree_string_view_t default_device,
const iree_hal_device_create_params_t* create_params,
iree_allocator_t host_allocator, iree_hal_device_list_t** out_device_list);
// Configures the |device| channel provider based on the current environment.
// Today this simply checks to see if the process is running under MPI and
// initializes that unconditionally.
//
// WARNING: not thread-safe and must only be called immediately after device
// creation.
iree_status_t iree_hal_device_set_default_channel_provider(
iree_hal_device_t* device);
// Owns HAL-native profiling, external capture, and periodic flush state.
typedef struct iree_hal_profiling_session_t iree_hal_profiling_session_t;
// Begins a session with explicit options on all devices. The session retains
// the devices and unwinds already-started captures if a later device fails.
// |external_options| may be NULL. Sinks are retained by the HAL producers.
// A nonzero |flush_interval_ms| starts a thread that flushes native profiling
// at that interval; zero flushes only on iree_hal_profiling_session_flush and
// at the end. End the returned session with iree_hal_profiling_session_end,
// including when execution fails. A zero device count returns a NULL session.
iree_status_t iree_hal_profiling_session_begin(
iree_host_size_t device_count, iree_hal_device_t* const* devices,
const iree_hal_device_profiling_options_t* options,
const iree_hal_device_external_capture_options_t* external_options,
iree_duration_t flush_interval_ms, iree_allocator_t host_allocator,
iree_hal_profiling_session_t** out_profiling);
// Begins a session from command line flags on all devices in the list.
iree_status_t iree_hal_begin_device_list_profiling_from_flags(
iree_host_size_t device_count, iree_hal_device_t* const* devices,
iree_allocator_t host_allocator,
iree_hal_profiling_session_t** out_profiling);
// Validates the device profiling and capture flags and returns whether they
// request a session, so tools can reject conflicting modes before anything
// starts.
iree_status_t iree_hal_profiling_from_flags_is_requested(bool* out_requested);
// Returns true when command line flags request profiling data that needs
// command buffers to retain profile metadata from creation time.
iree_status_t
iree_hal_profiling_from_flags_requires_retained_command_buffer_metadata(
bool* out_required);
// Begins any HAL-native profiling and external capture ranges requested by
// command line flags. No-op if neither profiling nor external capture is
// enabled.
//
// |out_profiling| is set to NULL when no profiling or capture state was
// created. Otherwise the returned state must be passed to
// iree_hal_profiling_session_end, even if the profiled operation fails, so
// background flush failures and sink end-session failures can be observed.
iree_status_t iree_hal_begin_profiling_from_flags(
iree_hal_device_t* device, iree_allocator_t host_allocator,
iree_hal_profiling_session_t** out_profiling);
// Begins any HAL-native profiling and external capture ranges requested by
// command line flags on every device in |device_group|. No-op if neither
// profiling nor external capture is enabled.
//
// The returned state owns one tooling session and shares a single profile sink
// across all devices so a multi-device run produces one profile bundle with one
// producer session per device. The returned state must be passed to
// iree_hal_profiling_session_end, even if the profiled operation fails.
iree_status_t iree_hal_begin_device_group_profiling_from_flags(
iree_hal_device_group_t* device_group, iree_allocator_t host_allocator,
iree_hal_profiling_session_t** out_profiling);
// Flushes HAL-native profiling if |profiling| has an active native session.
// No-op for NULL state and external-capture-only sessions.
//
// This serializes with the optional periodic flush thread owned by |profiling|.
// Tools using iree_hal_begin_profiling_from_flags should prefer this helper
// over calling iree_hal_device_profiling_flush directly.
iree_status_t iree_hal_profiling_session_flush(
iree_hal_profiling_session_t* profiling);
// Ends the external capture ranges and HAL-native profiling that |profiling|
// started on each device, stops its periodic flush and frees it. A failure on
// one device does not skip the others. No-op for NULL.
iree_status_t iree_hal_profiling_session_end(
iree_hal_profiling_session_t* profiling);
#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus
#endif // IREE_TOOLING_DEVICE_UTIL_H_