Skip to content

Commit ce03799

Browse files
committed
[executorch] Remove flatbuffer forward declarations from method.h
By hiding this, we are compatible with flatbuffer versions from v1.12 up to the latest release (v23.5.26).
1 parent ed1e3b4 commit ce03799

File tree

2 files changed

+24
-32
lines changed

2 files changed

+24
-32
lines changed

runtime/executor/method.cpp

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -257,11 +257,19 @@ bool parse_cond_value(const EValue& cond_value) {
257257

258258
} // namespace
259259

260-
Error Method::parse_values(
261-
const flatbuffers::Vector<
262-
flatbuffers::Offset<executorch_flatbuffer::EValue>>* flatbuffer_values,
263-
size_t* num_parsed) {
264-
for (size_t i = 0; i < n_value_; ++i) {
260+
Error Method::parse_values() {
261+
auto flatbuffer_values = serialization_plan_->values();
262+
ET_CHECK(flatbuffer_values != nullptr);
263+
size_t n_value = flatbuffer_values->size();
264+
values_ = ET_ALLOCATE_LIST_OR_RETURN_ERROR(
265+
memory_manager_->get_runtime_allocator(), EValue, n_value);
266+
267+
// n_value_ counts the number of successfully-initialized values for ~Method()
268+
// to clean up, and is incremented at the bottom of the loop. This makes it
269+
// safe for errors to return without updating any state.
270+
n_value_ = 0;
271+
272+
for (size_t i = 0; i < n_value; ++i) {
265273
auto serialization_value = flatbuffer_values->Get(i);
266274
switch (serialization_value->val_type()) {
267275
case executorch_flatbuffer::KernelTypes::Null: {
@@ -329,7 +337,6 @@ Error Method::parse_values(
329337
"Failed parsing tensor at index %zu: %" PRIu32,
330338
i,
331339
t.error());
332-
*num_parsed = i;
333340
return t.error();
334341
}
335342
new (&values_[i]) EValue(t.get());
@@ -347,7 +354,6 @@ Error Method::parse_values(
347354
"Failed parsing tensor list at index %zu: %" PRIu32,
348355
i,
349356
tensors.error());
350-
*num_parsed = i;
351357
return tensors.error();
352358
}
353359
new (&values_[i]) EValue(tensors.get());
@@ -365,7 +371,6 @@ Error Method::parse_values(
365371
"Failed parsing optional tensor list at index %zu: %" PRIu32,
366372
i,
367373
tensors.error());
368-
*num_parsed = i;
369374
return tensors.error();
370375
}
371376
new (&values_[i]) EValue(tensors.get());
@@ -383,8 +388,12 @@ Error Method::parse_values(
383388
"to see which type this is.",
384389
static_cast<uint32_t>(serialization_value->val_type()) - 1);
385390
}
391+
392+
// ~Method() will try to clean up n_value_ entries in the values_ array.
393+
// Only increment this once we know the entry is valid, so that we don't try
394+
// to clean up an uninitialized entry.
395+
n_value_ = i + 1;
386396
}
387-
*num_parsed = n_value_;
388397
return Error::Ok;
389398
}
390399

@@ -502,18 +511,9 @@ Error Method::init(executorch_flatbuffer::ExecutionPlan* s_plan) {
502511
auto runtime_allocator = memory_manager_->get_runtime_allocator();
503512

504513
{
505-
// Load values
506-
auto plan_values = serialization_plan_->values();
507-
ET_CHECK(plan_values != nullptr);
508-
n_value_ = plan_values->size();
509-
values_ =
510-
ET_ALLOCATE_LIST_OR_RETURN_ERROR(runtime_allocator, EValue, n_value_);
511-
size_t num_parsed;
512-
Error err = parse_values(plan_values, &num_parsed);
514+
// Parse the elements of the values_ array.
515+
Error err = parse_values();
513516
if (err != Error::Ok) {
514-
// ~Method() will try to clean up entries in this list. Ensure that it
515-
// doesn't look at any uninitialized entries.
516-
n_value_ = num_parsed;
517517
return err;
518518
}
519519
}

runtime/executor/method.h

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,6 @@
1515

1616
// Forward declare flatbuffer types. This is a public header and must not
1717
// include the generated flatbuffer header.
18-
namespace flatbuffers {
19-
template <typename T>
20-
class Vector;
21-
template <typename T>
22-
struct Offset;
23-
} // namespace flatbuffers
2418
namespace executorch_flatbuffer {
2519
struct Chain;
2620
struct ExecutionPlan;
@@ -262,13 +256,11 @@ class Method final {
262256
bool pre_allocated_input_;
263257

264258
/**
265-
* Initialize the EValue table of the program. *num_parsed is the number
266-
* of elements actually parsed, which may be less than n_value_ on failure.
259+
* Parses the elements of the values_ array. On error, n_value_ will be set to
260+
* the number of successfully-initialized entries so that ~Method doesn't try
261+
* to clean up uninitialized entries.
267262
*/
268-
__ET_NODISCARD Error parse_values(
269-
const flatbuffers::Vector<
270-
flatbuffers::Offset<executorch_flatbuffer::EValue>>* fb_values,
271-
size_t* num_parsed);
263+
__ET_NODISCARD Error parse_values();
272264

273265
__ET_NODISCARD Error resolve_operator(
274266
int32_t op_index,

0 commit comments

Comments
 (0)