tensorflow/third_party/implib_so/implib_so.BUILD
Peter Hawkins 5b7a8b07e6 Generate CUDA stubs using implib.so, rather than by writing C++ stubs.
We load CUDA libraries lazily using dlopen()/dlsym() primarily to comply with the manylinux rules for Python wheels, which require that libraries in a wheel only link directly against an allowlist of libraries. In order to access CUDA using dlopen()/dlsym() without changing any of our CUDA-using code, TSL contains stub implementations of CUDA APIs that, when invoked, load the relevant library, obtain the requested symbol using dlsym(), and call into that symbol.

The current CUDA stub libraries were constructed using a tool based on clang inside Google, which parses the CUDA headers and generates stub code for each API. It is therefore difficult to update the stubs without access to that tool. Each stub generated by the tool is a C++ function, which is very verbose. Further, a number of manual edits are required to the generated code, making maintenance tedious.

However, there is a better way. implib.so
(https://github.com/yugr/Implib.so) is a tool for automatically generating stubs from a .so file. This tool is considerably simpler because it generates a stub using assembly language, in which it turns out we do not need to know the type signature of the function being called. A completely generic trampoline function will do, with little or no bespoke knowledge of each function. We can adapt it to solve our CUDA stub problem.

implib-gen.py, which is the tool implib.so provides, isn't perfect for our needs, because it requires access to the .so file at stub generation time, which we don't have in our Bazel build. Instead, we can split it into two phases:

get_symbols.py, which, given a .so file extracts a list of public symbols that should be present in a stub. That list of symbols is checked into the TSL tree.
make_stub.py, which, given a list of symbols, generates trampolines for each function.
This change changes the TSL CUDA build to use make_stub.py to generate stubs from the list of symbols at Bazel build time, allowing us to delete over 130k lines of autogenerated C++ stub code.

PiperOrigin-RevId: 567635940
2023-09-22 09:09:08 -07:00

21 lines
391 B
Plaintext

# Description:
# Implib.so is a simple equivalent of Windows DLL import libraries for POSIX
# shared libraries.
package(default_visibility = ["//visibility:public"])
licenses(["notice"]) # MIT
exports_files([
"LICENSE.txt",
])
py_library(
name = "implib_gen_lib",
srcs = ["implib-gen.py"],
data = glob([
"arch/**/*.S.tpl",
"arch/**/*.ini",
]),
)