Skip to content

Add a util to print tag easily #9789

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
Apr 1, 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
14 changes: 14 additions & 0 deletions runtime/core/tag.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,20 @@ enum class Tag : uint32_t {
#undef DEFINE_TAG
};

#if ET_ENABLE_ENUM_STRINGS
inline const char* tag_to_string(Tag tag) {
switch (tag) {
#define CASE_TAG(x) \
case Tag::x: \
return #x;
EXECUTORCH_FORALL_TAGS(CASE_TAG)
#undef CASE_TAG
default:
return "Unknown";
}
}
#endif // ET_ENABLE_ENUM_STRINGS

/**
* Convert a tag value to a string representation. If ET_ENABLE_ENUM_STRINGS is
* set (it is on by default), this will return a string name (for example,
Expand Down
14 changes: 14 additions & 0 deletions runtime/core/test/tag_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,20 @@ TEST(TagToString, TagValues) {
EXPECT_STREQ("Bool", name.data());
}

TEST(TagToString, PrintTag) {
const char* name = tag_to_string(Tag::Tensor);
EXPECT_STREQ("Tensor", name);

name = tag_to_string(Tag::Int);
EXPECT_STREQ("Int", name);

name = tag_to_string(Tag::Double);
EXPECT_STREQ("Double", name);

name = tag_to_string(Tag::Bool);
EXPECT_STREQ("Bool", name);
}

TEST(TagToString, TagNameBufferSize) {
// Validate that kTagNameBufferSize is large enough to hold the all tag
// strings without truncation.
Expand Down
3 changes: 3 additions & 0 deletions runtime/core/test/targets.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ def define_common_targets():
deps = [
"//executorch/runtime/core:tag",
],
preprocessor_flags = [
"-DET_ENABLE_ENUM_STRINGS"
],
)

if True in get_aten_mode_options():
Expand Down
Loading