Skip to content

Introduce PAL function table #10675

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions runtime/platform/platform.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/

#include <executorch/runtime/platform/platform.h>
#include <cstdlib>

namespace executorch {
namespace runtime {

/**
* The singleton instance of the PAL table.
*/
static pal_table global_pal_table = {
.init = et_pal_init,
.abort = et_pal_abort,
.current_ticks = et_pal_current_ticks,
.ticks_to_ns_multiplier = et_pal_ticks_to_ns_multiplier,
.emit_log_message = et_pal_emit_log_message,
.allocate = et_pal_allocate,
.free = et_pal_free,
};

/**
* Retrieve a pointer to the singleton instance of the PAL function table. This
* can be used to override the default implementations of the PAL functions.
*/
pal_table* get_pal_table() {
return &global_pal_table;
}

void pal_init() {
get_pal_table()->init();
}

ET_NORETURN void pal_abort() {
get_pal_table()->abort();
// This should be unreachable, but in case the PAL implementation doesn't
// abort, force it here.
std::abort();
}

et_timestamp_t pal_current_ticks() {
return get_pal_table()->current_ticks();
}

et_tick_ratio_t pal_ticks_to_ns_multiplier() {
return get_pal_table()->ticks_to_ns_multiplier();
}

void pal_emit_log_message(
et_timestamp_t timestamp,
et_pal_log_level_t level,
const char* filename,
const char* function,
size_t line,
const char* message,
size_t length) {
get_pal_table()->emit_log_message(
timestamp, level, filename, function, line, message, length);
}

void* pal_allocate(size_t size) {
return get_pal_table()->allocate(size);
}

void pal_free(void* ptr) {
get_pal_table()->free(ptr);
}

} // namespace runtime
} // namespace executorch
111 changes: 111 additions & 0 deletions runtime/platform/platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
* Platform abstraction layer to allow individual platform libraries to override
* symbols in ExecuTorch. PAL functions are defined as C functions so a platform
* library implementer can use C in lieu of C++.
*
* The et_pal_ methods should not be called directly. Use the corresponding methods
* in the executorch::runtime namespace instead to appropriately dispatch through
* the PAL function table.
*/

#pragma once
Expand Down Expand Up @@ -53,19 +57,22 @@ typedef struct {
* to initialize any global state. Typically overridden by PAL implementer.
*/
void et_pal_init(void) ET_INTERNAL_PLATFORM_WEAKNESS;
typedef void (*et_pal_init_t)(void);

/**
* Immediately abort execution, setting the device into an error state, if
* available.
*/
ET_NORETURN void et_pal_abort(void) ET_INTERNAL_PLATFORM_WEAKNESS;
typedef void (*et_pal_abort_t)(void);

/**
* Return a monotonically non-decreasing timestamp in system ticks.
*
* @retval Timestamp value in system ticks.
*/
et_timestamp_t et_pal_current_ticks(void) ET_INTERNAL_PLATFORM_WEAKNESS;
typedef et_timestamp_t (*et_pal_current_ticks_t)(void);

/**
* Return the conversion rate from system ticks to nanoseconds as a fraction.
Expand All @@ -79,6 +86,7 @@ et_timestamp_t et_pal_current_ticks(void) ET_INTERNAL_PLATFORM_WEAKNESS;
*
* @retval The ratio of nanoseconds to system ticks.
*/
typedef et_tick_ratio_t (*et_pal_ticks_to_ns_multiplier_t)(void);
et_tick_ratio_t et_pal_ticks_to_ns_multiplier(void)
ET_INTERNAL_PLATFORM_WEAKNESS;

Expand Down Expand Up @@ -114,6 +122,14 @@ void et_pal_emit_log_message(
size_t line,
const char* message,
size_t length) ET_INTERNAL_PLATFORM_WEAKNESS;
typedef void (*et_pal_emit_log_message_t)(
et_timestamp_t timestamp,
et_pal_log_level_t level,
const char* filename,
const char* function,
size_t line,
const char* message,
size_t length);

/**
* NOTE: Core runtime code must not call this directly. It may only be called by
Expand All @@ -126,12 +142,107 @@ void et_pal_emit_log_message(
* et_pal_free().
*/
void* et_pal_allocate(size_t size) ET_INTERNAL_PLATFORM_WEAKNESS;
typedef void* (*et_pal_allocate_t)(size_t size);

/**
* Frees memory allocated by et_pal_allocate().
*
* @param[in] ptr Pointer to memory to free. May be nullptr.
*/
void et_pal_free(void* ptr) ET_INTERNAL_PLATFORM_WEAKNESS;
typedef void (*et_pal_free_t)(void* ptr);

} // extern "C"

namespace executorch {
namespace runtime {

/**
* Table of pointers to platform abstraction layer functions.
*/
struct pal_table {
et_pal_init_t init;
et_pal_abort_t abort;
et_pal_current_ticks_t current_ticks;
et_pal_ticks_to_ns_multiplier_t ticks_to_ns_multiplier;
et_pal_emit_log_message_t emit_log_message;
et_pal_allocate_t allocate;
et_pal_free_t free;
};

/**
* Retrieve a pointer to the singleton instance of the PAL function table. This
* can be used to override the default implementations of the PAL functions.
*/
pal_table* get_pal_table(void);

/**
* Initialize the platform abstraction layer.
*
* This function should be called before any other function provided by the PAL
* to initialize any global state. Typically overridden by PAL implementer.
*/
void pal_init();

/**
* Immediately abort execution, setting the device into an error state, if
* available.
*/
ET_NORETURN void pal_abort();

/**
* Return a monotonically non-decreasing timestamp in system ticks.
*
* @retval Timestamp value in system ticks.
*/
et_timestamp_t pal_current_ticks();

/**
* Return the conversion rate from system ticks to nanoseconds as a fraction.
* To convert a system ticks to nanoseconds, multiply the tick count by the
* numerator and then divide by the denominator:
* nanoseconds = ticks * numerator / denominator
*
* The utility method executorch::runtime::ticks_to_ns(et_timestamp_t) can also
* be used to perform the conversion for a given tick count. It is defined in
* torch/executor/runtime/platform/clock.h.
*
* @retval The ratio of nanoseconds to system ticks.
*/
et_tick_ratio_t pal_ticks_to_ns_multiplier();

/**
* Severity level of a log message. Values must map to printable 7-bit ASCII
* uppercase letters.
*/
void pal_emit_log_message(
et_timestamp_t timestamp,
et_pal_log_level_t level,
const char* filename,
const char* function,
size_t line,
const char* message,
size_t length);

/**
* NOTE: Core runtime code must not call this directly. It may only be called by
* a MemoryAllocator wrapper.
*
* Allocates size bytes of memory.
*
* @param[in] size Number of bytes to allocate.
* @returns the allocated memory, or nullptr on failure. Must be freed using
* et_pal_free().
*/
void* pal_allocate(size_t size);

/**
* Frees memory allocated by et_pal_allocate().
*
* @param[in] ptr Pointer to memory to free. May be nullptr.
*/
void pal_free(void* ptr);


} // namespace runtime
} // namespace executorch
1 change: 1 addition & 0 deletions runtime/platform/targets.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ def define_common_targets():
srcs = [
"abort.cpp",
"log.cpp",
"platform.cpp",
"profiler.cpp",
"runtime.cpp",
],
Expand Down
Loading