Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions python/tvm/libinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,21 @@ def package_lib_paths() -> list[Path]:

Anchored on this file's location (``python/tvm/libinfo.py``), the list
covers the wheel-install layout (``python/tvm/lib/``) and the in-tree dev
build layouts (``<worktree>/build/lib/`` and ``<worktree>/lib/``).
build layouts (``<worktree>/build/lib/``,
``<worktree>/build/<wheel-tag>/lib/``, and ``<worktree>/lib/``).
``TVM_LIBRARY_PATH`` is prepended when set so it takes priority. Callers
pick the basenames they want (e.g. ``libtvm_runtime.so``) and the load
mode; this function only returns the search path.
"""
pkg = _rel_top_directory() # python/tvm/
build = _dev_top_directory() / "build"
paths: list[Path] = []
if os.environ.get("TVM_LIBRARY_PATH"):
paths.append(Path(os.environ["TVM_LIBRARY_PATH"]))
paths += [
pkg / "lib", # wheel layout
_dev_top_directory() / "build" / "lib", # dev: <worktree>/build/lib
build / "lib", # dev: <worktree>/build/lib
*sorted(build.glob("*/lib")), # dev: <worktree>/build/<wheel-tag>/lib
_dev_top_directory() / "lib", # dev: <worktree>/lib
]
Comment on lines +49 to 58

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Unconditionally calling build.glob("*/lib") can be inefficient and potentially problematic in non-development environments (e.g., when TVM is installed as a wheel and the build directory does not exist or refers to an unrelated system path). Since glob is evaluated immediately when package_lib_paths() is called (which happens during import tvm), we should guard this with build.is_dir() to avoid unnecessary filesystem scanning. Additionally, we can cache the result of _dev_top_directory() to avoid calling it twice.

Suggested change
build = _dev_top_directory() / "build"
paths: list[Path] = []
if os.environ.get("TVM_LIBRARY_PATH"):
paths.append(Path(os.environ["TVM_LIBRARY_PATH"]))
paths += [
pkg / "lib", # wheel layout
_dev_top_directory() / "build" / "lib", # dev: <worktree>/build/lib
build / "lib", # dev: <worktree>/build/lib
*sorted(build.glob("*/lib")), # dev: <worktree>/build/<wheel-tag>/lib
_dev_top_directory() / "lib", # dev: <worktree>/lib
]
dev_top = _dev_top_directory()
build = dev_top / "build"
paths: list[Path] = []
if os.environ.get("TVM_LIBRARY_PATH"):
paths.append(Path(os.environ["TVM_LIBRARY_PATH"]))
paths += [
pkg / "lib", # wheel layout
build / "lib", # dev: <worktree>/build/lib
*(sorted(build.glob("*/lib")) if build.is_dir() else []), # dev: <worktree>/build/<wheel-tag>/lib
dev_top / "lib", # dev: <worktree>/lib
]

return paths
Expand Down
Loading