Skip to content

Add num_instructions program function #7206

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
Dec 9, 2024
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
20 changes: 20 additions & 0 deletions runtime/executor/method_meta.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,5 +210,25 @@ Result<int64_t> MethodMeta::memory_planned_buffer_size(size_t index) const {
return s_plan_->non_const_buffer_sizes()->Get(index + 1);
}

size_t MethodMeta::num_instructions() const {
const auto chains = s_plan_->chains();
if (chains == nullptr) {
return 0;
}
const auto num_chains = chains->size();
auto num_instructions = 0;
for (size_t i = 0; i < num_chains; ++i) {
auto s_chain = chains->Get(i);
if (s_chain == nullptr) {
continue;
}
auto s_instructions = s_chain->instructions();
if (s_instructions != nullptr) {
num_instructions += s_instructions->size();
}
}
return num_instructions;
}

} // namespace runtime
} // namespace executorch
7 changes: 7 additions & 0 deletions runtime/executor/method_meta.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,13 @@ class MethodMeta final {
*/
Result<int64_t> memory_planned_buffer_size(size_t index) const;

/**
* Get the number of instructions in this method.
*
* @returns The number of instructions.
*/
ET_EXPERIMENTAL size_t num_instructions() const;

/**
* DEPRECATED: Use num_memory_planned_buffers() instead.
*/
Expand Down
3 changes: 3 additions & 0 deletions runtime/executor/test/method_meta_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ TEST_F(MethodMetaTest, MethodMetaApi) {
method_meta->non_const_buffer_size(1).error(),
Error::InvalidArgument); // Deprecated API

// Number instructions in method is nonzero
EXPECT_NE(method_meta->num_instructions(), 0);

// Missing method fails
EXPECT_EQ(
program_->method_meta("not_a_method").error(), Error::InvalidArgument);
Expand Down