@@ -367,7 +367,7 @@ def copy_portable_header_files(name):
367
367
default_outs = ["." ],
368
368
)
369
369
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 ):
371
371
"""Build portable lib from source. We build from source so that the generated header file,
372
372
selected_op_variants.h, can be used to selectively build the lib for different dtypes.
373
373
"""
@@ -389,6 +389,20 @@ def build_portable_lib(name, oplist_header_name, feature = None):
389
389
# Include dtype header.
390
390
portable_header_files ["selected_op_variants.h" ] = ":{}[selected_op_variants]" .format (oplist_header_name )
391
391
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
+
392
406
# Build portable lib.
393
407
runtime .cxx_library (
394
408
name = name ,
@@ -398,16 +412,7 @@ def build_portable_lib(name, oplist_header_name, feature = None):
398
412
deps = ["//executorch/kernels/portable/cpu/pattern:all_deps" , "//executorch/kernels/portable/cpu/util:all_deps" ],
399
413
# header_namespace is only available in xplat. See https://fburl.com/code/we2gvopk
400
414
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 ,
411
416
# WARNING: using a deprecated API to avoid being built into a shared
412
417
# library. In the case of dynamically loading so library we don't want
413
418
# it to depend on other so libraries because that way we have to
@@ -440,7 +445,8 @@ def executorch_generated_lib(
440
445
compiler_flags = [],
441
446
kernel_deps = [],
442
447
dtype_selective_build = False ,
443
- feature = None ):
448
+ feature = None ,
449
+ expose_operator_symbols = False ):
444
450
"""Emits 0-3 C++ library targets (in fbcode or xplat) containing code to
445
451
dispatch the operators specified in the provided yaml files.
446
452
@@ -584,7 +590,7 @@ def executorch_generated_lib(
584
590
585
591
# Build portable lib.
586
592
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 )
588
594
portable_lib = [":{}" .format (portable_lib_name )]
589
595
590
596
# Exports headers that declare the function signatures of the C++ functions
0 commit comments