Skip to content

Commit fb4056c

Browse files
lucylqfacebook-github-bot
authored andcommitted
Allow operators to be called outside of the kernel registry
Summary: Usecase from Whatsapp (D67169920) to: - use dtype selective build to create executorch_generated_lib - use portable lib operators directly in C++ - ^cover these usecases in the same dependency This requires removing the compiler flag '-fvisibility=hidden', so that the operator symbols are exposed in the archive file. Otherwise, we get missing operator symbols, like in P1697209425. Duplicate symbols occur when we add the operator dependency explicitly, eg. adding `//executorch/kernels/portable/cpu:op_cat`, to the dep list. This is because dtype selective build re-builds the portable lib, and does not use the dependency `//executorch/kernels/portable:operators`, which uses the individual operator targets under the hood. Another alternative is to expose the operator registry and add a helper utility to return the function ptr to the kernel, though this could be an intrusive change that opens operator registry to external clients outside the core runtime. cc tarun292. Differential Revision: D67229096
1 parent 7924942 commit fb4056c

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

shim/xplat/executorch/codegen/codegen.bzl

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,20 @@ def build_portable_lib(name, oplist_header_name, feature = None):
389389
# Include dtype header.
390390
portable_header_files["selected_op_variants.h"] = ":{}[selected_op_variants]".format(oplist_header_name)
391391

392+
# For shared library build, we don't want to expose symbols of
393+
# kernel implementation (ex torch::executor::native::tanh_out)
394+
# to library users. They should use kernels through registry only.
395+
# With visibility=hidden, linker won't expose kernel impl symbols
396+
# so it can prune unregistered kernels.
397+
# Currently fbcode links all dependent libraries through shared
398+
# library, and it blocks users like unit tests to use kernel
399+
# implementation directly. So we enable this for xplat only.
400+
compiler_flags = ["-Wno-missing-prototypes", "-fvisibility=hidden"]
401+
if expose_operator_symbols:
402+
# Removing '-fvisibility=hidden' exposes operator symbols.
403+
# This allows operators to be called outside of the kernel registry.
404+
compiler_flags = ["-Wno-missing-prototypes"]
405+
392406
# Build portable lib.
393407
runtime.cxx_library(
394408
name = name,
@@ -398,16 +412,7 @@ def build_portable_lib(name, oplist_header_name, feature = None):
398412
deps = ["//executorch/kernels/portable/cpu/pattern:all_deps", "//executorch/kernels/portable/cpu/util:all_deps"],
399413
# header_namespace is only available in xplat. See https://fburl.com/code/we2gvopk
400414
header_namespace = "executorch/kernels/portable/cpu",
401-
compiler_flags = ["-Wno-missing-prototypes"] +
402-
# For shared library build, we don't want to expose symbols of
403-
# kernel implementation (ex torch::executor::native::tanh_out)
404-
# to library users. They should use kernels through registry only.
405-
# With visibility=hidden, linker won't expose kernel impl symbols
406-
# so it can prune unregistered kernels.
407-
# Currently fbcode links all dependent libraries through shared
408-
# library, and it blocks users like unit tests to use kernel
409-
# implementation directly. So we enable this for xplat only.
410-
["-fvisibility=hidden"],
415+
compiler_flags = compiler_flags,
411416
# WARNING: using a deprecated API to avoid being built into a shared
412417
# library. In the case of dynamically loading so library we don't want
413418
# it to depend on other so libraries because that way we have to
@@ -440,7 +445,8 @@ def executorch_generated_lib(
440445
compiler_flags = [],
441446
kernel_deps = [],
442447
dtype_selective_build = False,
443-
feature = None):
448+
feature = None,
449+
expose_operator_symbols = False):
444450
"""Emits 0-3 C++ library targets (in fbcode or xplat) containing code to
445451
dispatch the operators specified in the provided yaml files.
446452

0 commit comments

Comments
 (0)