Skip to content

Return result from get_backend_name #9465

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 4 commits into from
Mar 20, 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
15 changes: 9 additions & 6 deletions runtime/executor/method_meta.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,12 +245,15 @@ size_t MethodMeta::num_backends() const {
return delegates ? delegates->size() : 0;
}

const char* MethodMeta::get_backend_name(size_t index) const {
const auto delegates = s_plan_->delegates();
if (delegates && index < delegates->size()) {
return delegates->Get(index)->id()->c_str();
}
return nullptr;
Result<const char*> MethodMeta::get_backend_name(size_t index) const {
const auto count = num_backends();
ET_CHECK_OR_RETURN_ERROR(
index < count,
InvalidArgument,
"Index %zu out of range. num_backends: %zu",
index,
count);
return s_plan_->delegates()->Get(index)->id()->c_str();
}

size_t MethodMeta::num_instructions() const {
Expand Down
6 changes: 3 additions & 3 deletions runtime/executor/method_meta.h
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,10 @@ class MethodMeta final {
* Get the backend name at the given index.
*
* @param[in] index The index of the backend name.
* @returns The backend name as a C-style string, or nullptr if the index is
* invalid.
* @returns A Result wrapping the backend name as a C-style string
* on success, or an error if the index is invalid.
*/
const char* get_backend_name(size_t index) const;
Result<const char*> get_backend_name(size_t index) const;

/**
* Get the number of instructions in this method.
Expand Down
12 changes: 7 additions & 5 deletions runtime/executor/test/backend_integration_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -362,18 +362,20 @@ TEST_P(BackendIntegrationTest, GetBackendNamesSuccess) {
EXPECT_TRUE(method_meta->uses_backend(StubBackend::kName));

// Retrieve the number of backends.
size_t num_backends = method_meta->num_backends();
const size_t num_backends = method_meta->num_backends();
EXPECT_GT(num_backends, 0u);

// Iterate through each backend and verify its name.
for (size_t i = 0; i < num_backends; ++i) {
const char* name = method_meta->get_backend_name(i);
EXPECT_NE(name, nullptr);
auto backend_name_result = method_meta->get_backend_name(i);
ASSERT_TRUE(backend_name_result.ok());
const char* name = backend_name_result.get();
// For this test, we expect that the only backend is StubBackend.
EXPECT_STREQ(name, StubBackend::kName);
}
// Check that an out-of-range index returns nullptr.
EXPECT_EQ(method_meta->get_backend_name(num_backends), nullptr);
// Check that an out-of-range index returns an error.
auto out_of_range_result = method_meta->get_backend_name(num_backends);
EXPECT_FALSE(out_of_range_result.ok());
}

TEST_P(BackendIntegrationTest, FreeingProcessedBufferSucceeds) {
Expand Down
Loading