diff --git a/runtime/core/tag.h b/runtime/core/tag.h index db70c9879a..8ff260cacc 100644 --- a/runtime/core/tag.h +++ b/runtime/core/tag.h @@ -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, diff --git a/runtime/core/test/tag_test.cpp b/runtime/core/test/tag_test.cpp index 4b41aded42..00800df9b9 100644 --- a/runtime/core/test/tag_test.cpp +++ b/runtime/core/test/tag_test.cpp @@ -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. diff --git a/runtime/core/test/targets.bzl b/runtime/core/test/targets.bzl index 180e4eb0a0..7db74475c9 100644 --- a/runtime/core/test/targets.bzl +++ b/runtime/core/test/targets.bzl @@ -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():