Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CONTRIB] Add nm symbol dump #16763

Merged
merged 1 commit into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
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
46 changes: 46 additions & 0 deletions python/tvm/contrib/cc.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

# pylint: disable=invalid-name
import sys
from typing import Dict

from .._ffi.base import py_str
from . import tar as _tar
Expand Down Expand Up @@ -178,6 +179,51 @@ def create_executable(output, objects, options=None, cc=None, cwd=None, ccache_e
raise ValueError("Unsupported platform")


def get_global_symbol_section_map(path, *, nm=None) -> Dict[str, str]:
"""Get global symbols from a library via nm -g

Parameters
----------
path : str
The library path

nm: str
The path to nm command

Returns
-------
symbol_section_map: Dict[str, str]
A map from defined global symbol to their sections
"""
if nm is None:
if not _is_linux_like():
raise ValueError("Unsupported platform")
nm = "nm"

symbol_section_map = {}

if not os.path.isfile(path):
raise FileNotFoundError(f"{path} does not exist")

cmd = [nm, "-gU", path]
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
(out, _) = proc.communicate()

if proc.returncode != 0:
msg = "Runtime error:\n"
msg += py_str(out)
raise RuntimeError(msg)

for line in py_str(out).split("\n"):
data = line.strip().split()
if len(data) != 3:
continue
symbol = data[-1]
section = data[-2]
symbol_section_map[symbol] = section
return symbol_section_map


def get_target_by_dump_machine(compiler):
"""Functor of get_target_triple that can get the target triple using compiler.

Expand Down
31 changes: 30 additions & 1 deletion python/tvm/contrib/ndk.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
import subprocess
import os
import shutil
from typing import Dict

from .._ffi.base import py_str
from . import utils as _utils, tar as _tar
from . import utils as _utils, tar as _tar, cc as _cc
from .cc import get_target_by_dump_machine


Expand Down Expand Up @@ -123,3 +125,30 @@ def create_staticlib(output, inputs):


create_staticlib.output_format = "a"


def get_global_symbol_section_map(path, *, nm=None) -> Dict[str, str]:
"""Get global symbols from a library via nm -gU in NDK

Parameters
----------
path : str
The library path

nm: str
The path to nm command

Returns
-------
symbol_section_map: Dict[str, str]
A map from defined global symbol to their sections
"""
if "TVM_NDK_CC" not in os.environ:
raise RuntimeError(
"Require environment variable TVM_NDK_CC" " to be the NDK standalone compiler"
)
if nm is None:
compiler = os.environ["TVM_NDK_CC"]
base_path = os.path.dirname(compiler)
nm = os.path.join(base_path, "llvm-nm")
return _cc.get_global_symbol_section_map(path, nm=nm)
Loading