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

Merged
merged 1 commit into from
Jun 5, 2025
Merged
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
4 changes: 2 additions & 2 deletions .github/workflows/pull.yml
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ jobs:
size=${arr[4]}
# threshold=48120 on devserver with gcc11.4
# todo(lfq): update once binary size is below 50kb.
threshold="55504"
threshold="55584"
Copy link
Member Author

@GregoryComer GregoryComer Jun 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that the "true" size increase is around ~80 bytes, due to some instability in the size test on older toolchains. See https://github.com/pytorch/executorch/pull/11217/files. This is confirmed with manual builds on newer toolchains and Meta app build size checks.

if [[ "$size" -le "$threshold" ]]; then
echo "Success $size <= $threshold"
else
Expand Down Expand Up @@ -406,7 +406,7 @@ jobs:
output=$(ls -la cmake-out/test/size_test)
arr=($output)
size=${arr[4]}
threshold="51656"
threshold="51728"
if [[ "$size" -le "$threshold" ]]; then
echo "Success $size <= $threshold"
else
Expand Down
4 changes: 2 additions & 2 deletions backends/xnnpack/runtime/profiling/XNNProfiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ Error XNNProfiler::start(EventTracer* event_tracer) {
state_ = XNNProfilerState::Running;

// Log the start of execution timestamp.
start_time_ = et_pal_current_ticks();
start_time_ = runtime::pal_current_ticks();

return Error::Ok;
}
Expand Down Expand Up @@ -187,7 +187,7 @@ void XNNProfiler::log_operator_timings() {

void XNNProfiler::submit_trace() {
// Retrieve the system tick rate (ratio between ticks and nanoseconds).
auto tick_ns_conv_multiplier = et_pal_ticks_to_ns_multiplier();
auto tick_ns_conv_multiplier = runtime::pal_ticks_to_ns_multiplier();

ET_CHECK(op_timings_.size() == op_count_);
size_t name_len = 0;
Expand Down
8 changes: 4 additions & 4 deletions devtools/etdump/etdump_flatcc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ EventTracerEntry ETDumpGen::start_profiling(
prof_entry.chain_id = chain_id;
prof_entry.debug_handle = debug_handle;
}
prof_entry.start_time = et_pal_current_ticks();
prof_entry.start_time = runtime::pal_current_ticks();
return prof_entry;
}

Expand All @@ -246,15 +246,15 @@ EventTracerEntry ETDumpGen::start_profiling_delegate(
prof_entry.event_id = delegate_debug_index == kUnsetDelegateDebugIntId
? create_string_entry(name)
: delegate_debug_index;
prof_entry.start_time = et_pal_current_ticks();
prof_entry.start_time = runtime::pal_current_ticks();
return prof_entry;
}

void ETDumpGen::end_profiling_delegate(
EventTracerEntry event_tracer_entry,
const void* metadata,
size_t metadata_len) {
et_timestamp_t end_time = et_pal_current_ticks();
et_timestamp_t end_time = runtime::pal_current_ticks();
check_ready_to_add_events();

// Start building the ProfileEvent entry.
Expand Down Expand Up @@ -469,7 +469,7 @@ Result<bool> ETDumpGen::log_intermediate_output_delegate_helper(
}

void ETDumpGen::end_profiling(EventTracerEntry prof_entry) {
et_timestamp_t end_time = et_pal_current_ticks();
et_timestamp_t end_time = runtime::pal_current_ticks();
ET_CHECK_MSG(
prof_entry.delegate_event_id_type == DelegateDebugIdType::kNone,
"Delegate events must use end_profiling_delegate to mark the end of a delegate profiling event.");
Expand Down
6 changes: 4 additions & 2 deletions examples/portable/executor_runner/executor_runner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,9 +269,11 @@ int main(int argc, char** argv) {
(uint32_t)inputs.error());
ET_LOG(Debug, "Inputs prepared.");

const et_timestamp_t before_execute = et_pal_current_ticks();
const et_timestamp_t before_execute =
executorch::runtime::pal_current_ticks();
Error status = method->execute();
const et_timestamp_t after_execute = et_pal_current_ticks();
const et_timestamp_t after_execute =
executorch::runtime::pal_current_ticks();
time_spent_executing += after_execute - before_execute;
ET_CHECK_MSG(
status == Error::Ok,
Expand Down
4 changes: 2 additions & 2 deletions runtime/executor/platform_memory_allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class PlatformMemoryAllocator final : public MemoryAllocator {

// Allocate enough memory for the node, the data and the alignment bump.
size_t alloc_size = sizeof(AllocationNode) + size + alignment;
void* node_memory = et_pal_allocate(alloc_size);
void* node_memory = runtime::pal_allocate(alloc_size);

// If allocation failed, log message and return nullptr.
if (node_memory == nullptr) {
Expand Down Expand Up @@ -87,7 +87,7 @@ class PlatformMemoryAllocator final : public MemoryAllocator {
AllocationNode* current = head_;
while (current != nullptr) {
AllocationNode* next = current->next;
et_pal_free(current);
runtime::pal_free(current);
current = next;
}
head_ = nullptr;
Expand Down
2 changes: 1 addition & 1 deletion runtime/platform/abort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace runtime {
* up, and set an abnormal exit status (platform-defined).
*/
ET_NORETURN void runtime_abort() {
et_pal_abort();
pal_abort();
}

} // namespace runtime
Expand Down
4 changes: 2 additions & 2 deletions runtime/platform/log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace internal {
* @retval Monotonically non-decreasing timestamp in system ticks.
*/
et_timestamp_t get_log_timestamp() {
return et_pal_current_ticks();
return pal_current_ticks();
}

// Double-check that the log levels are ordered from lowest to highest severity.
Expand Down Expand Up @@ -96,7 +96,7 @@ void vlogf(
? kLevelToPal[size_t(level)]
: et_pal_log_level_t::kUnknown;

et_pal_emit_log_message(
pal_emit_log_message(
timestamp, pal_level, filename, function, line, buf, len);

#endif // ET_LOG_ENABLED
Expand Down
160 changes: 160 additions & 0 deletions runtime/platform/platform.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
/*
* 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/log.h>
#include <executorch/runtime/platform/platform.h>
#include <cstdlib>

namespace executorch::runtime {

namespace {
/**
* The singleton instance of the PAL function table.
*/
PalImpl pal_impl = {
et_pal_init,
et_pal_abort,
et_pal_current_ticks,
et_pal_ticks_to_ns_multiplier,
et_pal_emit_log_message,
et_pal_allocate,
et_pal_free,
__FILE__};

/**
* Tracks whether the PAL has been overridden. This is used to warn when
* multiple callers override the PAL.
*/
bool is_pal_overridden = false;
} // namespace

PalImpl PalImpl::create(
pal_emit_log_message_method emit_log_message,
const char* source_filename) {
return PalImpl::create(
nullptr, // init
nullptr, // abort
nullptr, // current_ticks
nullptr, // ticks_to_ns_multiplier
emit_log_message,
nullptr, // allocate
nullptr, // free
source_filename);
}

PalImpl PalImpl::create(
pal_init_method init,
pal_abort_method abort,
pal_current_ticks_method current_ticks,
pal_ticks_to_ns_multiplier_method ticks_to_ns_multiplier,
pal_emit_log_message_method emit_log_message,
pal_allocate_method allocate,
pal_free_method free,
const char* source_filename) {
return PalImpl{
init,
abort,
current_ticks,
ticks_to_ns_multiplier,
emit_log_message,
allocate,
free,
source_filename};
}

/**
* Override the PAL functions with user implementations. Any null entries in the
* table are unchanged and will keep the default implementation.
*/
bool register_pal(PalImpl impl) {
if (is_pal_overridden) {
ET_LOG(
Error,
"register_pal() called multiple times. Subsequent calls will override the previous implementation. Previous implementation was registered from %s.",
impl.source_filename != nullptr ? impl.source_filename : "unknown");
}
is_pal_overridden = true;

if (impl.abort != nullptr) {
pal_impl.abort = impl.abort;
}

if (impl.current_ticks != nullptr) {
pal_impl.current_ticks = impl.current_ticks;
}

if (impl.ticks_to_ns_multiplier != nullptr) {
pal_impl.ticks_to_ns_multiplier = impl.ticks_to_ns_multiplier;
}

if (impl.emit_log_message != nullptr) {
pal_impl.emit_log_message = impl.emit_log_message;
}

if (impl.allocate != nullptr) {
pal_impl.allocate = impl.allocate;
}

if (impl.free != nullptr) {
pal_impl.free = impl.free;
}

if (impl.init != nullptr) {
pal_impl.init = impl.init;
if (pal_impl.init != nullptr) {
pal_impl.init();
}
}

return true;
}

const PalImpl* get_pal_impl() {
return &pal_impl;
}

void pal_init() {
pal_impl.init();
}

ET_NORETURN void pal_abort() {
pal_impl.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 pal_impl.current_ticks();
}

et_tick_ratio_t pal_ticks_to_ns_multiplier() {
return pal_impl.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) {
pal_impl.emit_log_message(
timestamp, level, filename, function, line, message, length);
}

void* pal_allocate(size_t size) {
return pal_impl.allocate(size);
}

void pal_free(void* ptr) {
pal_impl.free(ptr);
}

} // namespace executorch::runtime
Loading
Loading