Skip to content

Commit 29f5cac

Browse files
authored
Add num_instructions program function
Differential Revision: D66504180 Pull Request resolved: #7206
1 parent 4adbdce commit 29f5cac

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

runtime/executor/method_meta.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,5 +210,25 @@ Result<int64_t> MethodMeta::memory_planned_buffer_size(size_t index) const {
210210
return s_plan_->non_const_buffer_sizes()->Get(index + 1);
211211
}
212212

213+
size_t MethodMeta::num_instructions() const {
214+
const auto chains = s_plan_->chains();
215+
if (chains == nullptr) {
216+
return 0;
217+
}
218+
const auto num_chains = chains->size();
219+
auto num_instructions = 0;
220+
for (size_t i = 0; i < num_chains; ++i) {
221+
auto s_chain = chains->Get(i);
222+
if (s_chain == nullptr) {
223+
continue;
224+
}
225+
auto s_instructions = s_chain->instructions();
226+
if (s_instructions != nullptr) {
227+
num_instructions += s_instructions->size();
228+
}
229+
}
230+
return num_instructions;
231+
}
232+
213233
} // namespace runtime
214234
} // namespace executorch

runtime/executor/method_meta.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,13 @@ class MethodMeta final {
185185
*/
186186
Result<int64_t> memory_planned_buffer_size(size_t index) const;
187187

188+
/**
189+
* Get the number of instructions in this method.
190+
*
191+
* @returns The number of instructions.
192+
*/
193+
ET_EXPERIMENTAL size_t num_instructions() const;
194+
188195
/**
189196
* DEPRECATED: Use num_memory_planned_buffers() instead.
190197
*/

runtime/executor/test/method_meta_test.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ TEST_F(MethodMetaTest, MethodMetaApi) {
9292
method_meta->non_const_buffer_size(1).error(),
9393
Error::InvalidArgument); // Deprecated API
9494

95+
// Number instructions in method is nonzero
96+
EXPECT_NE(method_meta->num_instructions(), 0);
97+
9598
// Missing method fails
9699
EXPECT_EQ(
97100
program_->method_meta("not_a_method").error(), Error::InvalidArgument);

0 commit comments

Comments
 (0)