Skip to content

Commit 3ffb5d2

Browse files
committed
Update on "Integrate torchgen exception boundary with ExecuTorch"
As of #7746, we build with exceptions by default, so we just need to use them. TODO: presumably we need to manage rollout of the torchgen patch? Differential Revision: [D67904052](https://our.internmc.facebook.com/intern/diff/D67904052/) [ghstack-poisoned]
2 parents de1cc23 + 9a4502f commit 3ffb5d2

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

shim/xplat/executorch/codegen/codegen.bzl

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@ def _prepare_genrule_and_lib(
116116
custom_ops_yaml_path = None,
117117
custom_ops_requires_runtime_registration = True,
118118
manual_registration = False,
119-
aten_mode = False):
119+
aten_mode = False,
120+
support_exceptions = True):
120121
"""
121122
This function returns two dicts `genrules` and `libs`, derived from the arguments being passed
122123
to `executorch_generated_lib`. `genrules` contains all information related to what genrules to
@@ -151,12 +152,15 @@ def _prepare_genrule_and_lib(
151152
"--source-path=$(location //executorch/codegen:templates)",
152153
"--tags-path $(location {})/aten/src/ATen/native/tags.yaml".format(aten_src_path),
153154
"--aten_yaml_path $(location {})/aten/src/ATen/native/native_functions.yaml".format(aten_src_path),
154-
"--add-exception-boundary",
155155
"--install_dir=${OUT}",
156156
# TODO(dbort): Add a second step that verifies that the set of
157157
# actually-generated files matches GENERATED_FILES.
158158
]
159159

160+
if support_exceptions:
161+
genrule_cmd.append("--add-exception-boundary")
162+
163+
160164
# Sources for generated kernel registration lib
161165
sources = MANUAL_REGISTRATION_SOURCES if manual_registration else GENERATED_SOURCES
162166

@@ -218,6 +222,7 @@ def _prepare_genrule_and_lib(
218222
def _prepare_custom_ops_genrule_and_lib(
219223
name,
220224
custom_ops_yaml_path = None,
225+
support_exceptions = True,
221226
deps = [],
222227
kernels = []):
223228
"""Similar to _prepare_genrule_and_lib but for custom ops."""
@@ -248,10 +253,11 @@ def _prepare_custom_ops_genrule_and_lib(
248253
"--tags-path $(location {})/aten/src/ATen/native/tags.yaml".format(aten_src_path),
249254
"--aten_yaml_path $(location {})/aten/src/ATen/native/native_functions.yaml".format(aten_src_path),
250255
"--custom_ops_yaml_path=" + custom_ops_yaml_path,
251-
"--add_exception_boundary",
252256
"--install_dir=${OUT}",
253257
"--op_selection_yaml_path=$(location :{}[selected_operators.yaml])".format(oplist_dir_name),
254258
]
259+
if support_exceptions:
260+
genrule_cmd.append("--add-exception-boundary")
255261

256262
# Determine what sources custom_ops_<name> target should include
257263
custom_ops_sources = CUSTOM_OPS_SCHEMA_REGISTRATION_SOURCES + (
@@ -283,6 +289,7 @@ def exir_custom_ops_aot_lib(
283289
deps = [],
284290
compiler_flags = [],
285291
define_static_target = False,
292+
support_exceptions = True,
286293
platforms = get_default_executorch_platforms()):
287294
"""Generates a C++ library that helps to register the custom ops into PyTorch,
288295
so they are visible to EXIR. To use this, we need to load the generated so file:
@@ -299,11 +306,13 @@ def exir_custom_ops_aot_lib(
299306
visibility: visibility of the generated library.
300307
kernels: C++ kernels for these custom ops. They need to be implemented using ATen/c10 basics.
301308
deps: dependencies of the generated library.
309+
support_exceptions: enable try/catch wrapper around operator implemntations to make sure exceptions thrown will not bring down the process. Disable if your use case disables exceptions in the build.
302310
"""
303311
genrules, libs = _prepare_custom_ops_genrule_and_lib(
304312
name = name,
305313
custom_ops_yaml_path = selects.apply(yaml_target, lambda y: "$(location {})".format(y)),
306314
kernels = kernels,
315+
support_exceptions = support_exceptions,
307316
deps = deps,
308317
)
309318
for genrule in genrules:
@@ -448,7 +457,8 @@ def executorch_generated_lib(
448457
kernel_deps = [],
449458
dtype_selective_build = False,
450459
feature = None,
451-
expose_operator_symbols = False):
460+
expose_operator_symbols = False,
461+
support_exceptions = True):
452462
"""Emits 0-3 C++ library targets (in fbcode or xplat) containing code to
453463
dispatch the operators specified in the provided yaml files.
454464
@@ -497,6 +507,7 @@ def executorch_generated_lib(
497507
compiler_flags: compiler_flags args to runtime.cxx_library
498508
dtype_selective_build: In additional to operator selection, dtype selective build further selects the dtypes for each operator. Can be used with model or dict selective build APIs, where dtypes can be specified. Note: this is only available in xplat.
499509
feature: Product-Feature Hierarchy (PFH). For internal use only, required for FoA in production. See: https://fburl.com/wiki/2wzjpyqy
510+
support_exceptions: enable try/catch wrapper around operator implemntations to make sure exceptions thrown will not bring down the process. Disable if your use case disables exceptions in the build.
500511
"""
501512
if functions_yaml_target and aten_mode:
502513
fail("{} is providing functions_yaml_target in ATen mode, it will be ignored. `native_functions.yaml` will be the source of truth.".format(name))
@@ -536,6 +547,7 @@ def executorch_generated_lib(
536547
custom_ops_requires_runtime_registration = custom_ops_requires_runtime_registration,
537548
aten_mode = aten_mode,
538549
manual_registration = manual_registration,
550+
support_exceptions = support_exceptions,
539551
)
540552

541553
# genrule for selective build from static operator list

0 commit comments

Comments
 (0)