Skip to content

Commit cf6daa9

Browse files
authored
Add short function support (pytorch#19846)
Summary: Currently, __builtin_FUNCTION is used opportunistically if it exists. However, for heavily templated code, this results in extremely long string which adds .rodata which can be wasteful on embedded targets. This commit adds an override which uses the shorter __FUNCTION__ even if __bultin_FUNCTION exists and exposes as a BUCK constraint. Integration into CMake intentially left out for now. Differential Revision: D106668077
1 parent b0441b5 commit cf6daa9

4 files changed

Lines changed: 64 additions & 5 deletions

File tree

runtime/executor/targets.bzl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,14 @@ def _program_preprocessor_flags():
1616
if enable_verification == "false":
1717
return ["-DET_ENABLE_PROGRAM_VERIFICATION=0"]
1818
elif enable_verification == "true":
19-
# Enabled by default.
20-
return []
19+
# Enabled by default; allow opt-out via constraint
20+
if not runtime.is_oss:
21+
return select({
22+
"DEFAULT": [],
23+
"fbsource//xplat/executorch/tools/buck/constraints:executorch-program-verification-disabled": ["-DET_ENABLE_PROGRAM_VERIFICATION=0"],
24+
})
25+
else:
26+
return []
2127
else:
2228
fail("executorch.enable_program_verification must be one of 'true' or 'false'; saw '" +
2329
enable_verification + "'")

runtime/platform/compiler.h

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,14 @@
138138
#define __has_builtin(x) (0)
139139
#endif
140140

141-
#if __has_builtin(__builtin_strrchr)
141+
#if defined(__FILE_NAME__)
142+
/// __FILE_NAME__ provides just the filename at
143+
/// compile time, avoiding embedding full paths in the binary
144+
#define ET_SHORT_FILENAME __FILE_NAME__
145+
#elif __has_builtin(__builtin_strrchr)
142146
/// Name of the source file without a directory string.
147+
/// Note: This approach embeds the full path in .rodata even though only the
148+
/// basename is used at runtime. __FILE_NAME__ is preferred when available.
143149
#define ET_SHORT_FILENAME (__builtin_strrchr("/" __FILE__, '/') + 1)
144150
#else
145151
#define ET_SHORT_FILENAME __FILE__
@@ -152,12 +158,17 @@
152158
#define ET_LINE __LINE__
153159
#endif // __has_builtin(__builtin_LINE)
154160

155-
#if __has_builtin(__builtin_FUNCTION)
161+
#if defined(ET_USE_BUILTIN_FUNCTION_NAME) && ET_USE_BUILTIN_FUNCTION_NAME == 0
162+
/// __FUNCTION__ provides a short undecorated name, saving .rodata space
163+
/// compared to __builtin_FUNCTION() which includes the full signature
164+
/// (namespace, parameters, return type).
165+
#define ET_FUNCTION __FUNCTION__
166+
#elif __has_builtin(__builtin_FUNCTION)
156167
/// Name of the current function as a const char[].
157168
#define ET_FUNCTION __builtin_FUNCTION()
158169
#else
159170
#define ET_FUNCTION __FUNCTION__
160-
#endif // __has_builtin(__builtin_FUNCTION)
171+
#endif
161172

162173
// As of G3 RJ-2024.3 toolchain, zu format specifier is not supported for Xtensa
163174
#if defined(__XTENSA__)

runtime/platform/targets.bzl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,5 +116,9 @@ def define_common_targets():
116116
exported_headers = [
117117
"compiler.h",
118118
],
119+
exported_preprocessor_flags = select({
120+
"DEFAULT": [],
121+
"fbsource//xplat/executorch/tools/buck/constraints:executorch-builtin-function-name-disabled": ["-DET_USE_BUILTIN_FUNCTION_NAME=0"],
122+
}) if not runtime.is_oss else [],
119123
visibility = ["PUBLIC"],
120124
)

tools/buck/constraints/BUCK

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,41 @@ fb_native.constraint_value(
6161
constraint_setting = ":executorch-event-tracer",
6262
visibility = ["PUBLIC"],
6363
)
64+
65+
fb_native.config_setting(
66+
name = "executorch-program-verification-disabled",
67+
constraint_values = [
68+
":program-verification-disabled",
69+
],
70+
visibility = ["PUBLIC"],
71+
)
72+
73+
fb_native.constraint_setting(
74+
name = "executorch-program-verification",
75+
visibility = ["PUBLIC"],
76+
)
77+
78+
fb_native.constraint_value(
79+
name = "program-verification-disabled",
80+
constraint_setting = ":executorch-program-verification",
81+
visibility = ["PUBLIC"],
82+
)
83+
84+
fb_native.config_setting(
85+
name = "executorch-builtin-function-name-disabled",
86+
constraint_values = [
87+
":builtin-function-name-disabled",
88+
],
89+
visibility = ["PUBLIC"],
90+
)
91+
92+
fb_native.constraint_setting(
93+
name = "executorch-builtin-function-name",
94+
visibility = ["PUBLIC"],
95+
)
96+
97+
fb_native.constraint_value(
98+
name = "builtin-function-name-disabled",
99+
constraint_setting = ":executorch-builtin-function-name",
100+
visibility = ["PUBLIC"],
101+
)

0 commit comments

Comments
 (0)