-
Notifications
You must be signed in to change notification settings - Fork 607
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
Introduce PAL function table #10675
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
|
||
GregoryComer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return true; | ||
GregoryComer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
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 | ||
GregoryComer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// 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); | ||
GregoryComer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
} // namespace executorch::runtime |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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.