Skip to content

Commit b45bac2

Browse files
committed
find_libnvvm_so_via_proc_self_maps() Proof Of Concept
1 parent b0f9a16 commit b45bac2

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

cuda_bindings/tests/test_nvvm.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,68 @@
1010

1111
from cuda.bindings import nvvm
1212

13+
if True: # Trick to stop ruff from moving these.
14+
import glob
15+
import os
16+
import pathlib
17+
18+
19+
def get_proc_self_maps_paths():
20+
paths = set()
21+
with open("/proc/self/maps") as f:
22+
for line in f:
23+
parts = line.strip().split(maxsplit=5)
24+
if len(parts) == 6:
25+
path = parts[5]
26+
if path.startswith("/"):
27+
paths.add(path)
28+
return tuple(sorted(paths))
29+
30+
31+
def find_libnvvm_so_in_dir(dirpath):
32+
if not os.path.isdir(dirpath):
33+
return None
34+
longest_so_path = ""
35+
for so_path in sorted(glob.glob(f"{dirpath}/libnvvm.so*")):
36+
if len(so_path) > len(longest_so_path) and os.path.isfile(so_path):
37+
longest_so_path = so_path
38+
return longest_so_path if longest_so_path else None
39+
40+
41+
def find_libnvvm_so_via_proc_self_maps():
42+
# Traverse /proc/self/maps to look for one of these:
43+
# **/lib/nvvm/lib64/libnvvm.so*
44+
# **/site-packages/nvidia/cuda_nvcc/nvvm/lib64/libnvvm.so*
45+
# using "lib" or "site-packages" as anchor points.
46+
dirs_inspected = set()
47+
for maps_path in get_proc_self_maps_paths():
48+
dir_parts = pathlib.Path(maps_path).parts[1:-1] # Strip leading slash and filename.
49+
if dir_parts not in dirs_inspected:
50+
for ix in range(len(dir_parts), 0, -1):
51+
parent_dir = dir_parts[:ix]
52+
if parent_dir in dirs_inspected:
53+
continue
54+
dirs_inspected.add(parent_dir)
55+
if parent_dir[-1] == "lib":
56+
trialdir = parent_dir[:-1] + ("nvvm", "lib64")
57+
elif parent_dir[-1] == "site-packages":
58+
trialdir = parent_dir + ("nvidia", "cuda_nvcc", "nvvm", "lib64")
59+
else:
60+
continue
61+
so_path = find_libnvvm_so_in_dir(os.sep + os.sep.join(trialdir))
62+
if so_path is not None:
63+
return so_path
64+
return None
65+
66+
67+
so_path = find_libnvvm_so_via_proc_self_maps()
68+
print(f"\nfind_libnvvm_so_via_proc_self_maps() {so_path=}", flush=True)
69+
if so_path is not None:
70+
import ctypes
71+
72+
ctypes.CDLL(so_path)
73+
74+
1375
MINIMAL_NVVMIR_TXT = b"""\
1476
target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-i128:128:128-f32:32:32-f64:64:64-v16:16:16-v32:32:32-v64:64:64-v128:128:128-n16:32:64"
1577

0 commit comments

Comments
 (0)