Skip to content

Commit 9c45ef7

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. Alternative solution 1: Add the operator dependency explicitly, eg. adding `//executorch/kernels/portable/cpu:op_cat`, to the dep list, to allow C++ usage. This causes duplicate symbols for dependencies in op_cat, because dtype selective build re-builds the portable lib. Without dtype selective build, this would be OK, as we use `//executorch/kernels/portable:operators` that aggregates the individual operator targets under the hood; this would be deduplicated with the explicit operator dependency. Alternative solution 2: 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 9c45ef7

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

shim/xplat/executorch/codegen/codegen.bzl

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ def copy_portable_header_files(name):
367367
default_outs = ["."],
368368
)
369369

370-
def build_portable_lib(name, oplist_header_name, feature = None):
370+
def build_portable_lib(name, oplist_header_name, feature = None, expose_operator_symbols = False):
371371
"""Build portable lib from source. We build from source so that the generated header file,
372372
selected_op_variants.h, can be used to selectively build the lib for different dtypes.
373373
"""
@@ -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
@@ -584,7 +590,7 @@ def executorch_generated_lib(
584590

585591
# Build portable lib.
586592
portable_lib_name = name + "_portable_lib"
587-
build_portable_lib(portable_lib_name, oplist_header_name, feature)
593+
build_portable_lib(portable_lib_name, oplist_header_name, feature, expose_operator_symbols)
588594
portable_lib = [":{}".format(portable_lib_name)]
589595

590596
# Exports headers that declare the function signatures of the C++ functions

0 commit comments

Comments
 (0)