Skip to content

Commit 3c4fc6e

Browse files
larryliu0820kirklandsign
authored andcommitted
Add a util to print tag easily
Differential Revision: D72208521 Pull Request resolved: #9789
1 parent 494b0ee commit 3c4fc6e

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

runtime/core/tag.h

+14
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,20 @@ enum class Tag : uint32_t {
3838
#undef DEFINE_TAG
3939
};
4040

41+
#if ET_ENABLE_ENUM_STRINGS
42+
inline const char* tag_to_string(Tag tag) {
43+
switch (tag) {
44+
#define CASE_TAG(x) \
45+
case Tag::x: \
46+
return #x;
47+
EXECUTORCH_FORALL_TAGS(CASE_TAG)
48+
#undef CASE_TAG
49+
default:
50+
return "Unknown";
51+
}
52+
}
53+
#endif // ET_ENABLE_ENUM_STRINGS
54+
4155
/**
4256
* Convert a tag value to a string representation. If ET_ENABLE_ENUM_STRINGS is
4357
* set (it is on by default), this will return a string name (for example,

runtime/core/test/tag_test.cpp

+14
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,20 @@ TEST(TagToString, TagValues) {
3737
EXPECT_STREQ("Bool", name.data());
3838
}
3939

40+
TEST(TagToString, PrintTag) {
41+
const char* name = tag_to_string(Tag::Tensor);
42+
EXPECT_STREQ("Tensor", name);
43+
44+
name = tag_to_string(Tag::Int);
45+
EXPECT_STREQ("Int", name);
46+
47+
name = tag_to_string(Tag::Double);
48+
EXPECT_STREQ("Double", name);
49+
50+
name = tag_to_string(Tag::Bool);
51+
EXPECT_STREQ("Bool", name);
52+
}
53+
4054
TEST(TagToString, TagNameBufferSize) {
4155
// Validate that kTagNameBufferSize is large enough to hold the all tag
4256
// strings without truncation.

runtime/core/test/targets.bzl

+3
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ def define_common_targets():
9191
deps = [
9292
"//executorch/runtime/core:tag",
9393
],
94+
preprocessor_flags = [
95+
"-DET_ENABLE_ENUM_STRINGS"
96+
],
9497
)
9598

9699
if True in get_aten_mode_options():

0 commit comments

Comments
 (0)