forked from NVIDIA/cuda-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_static_bitcode_input.py
More file actions
executable file
·53 lines (40 loc) · 1.64 KB
/
Copy pathbuild_static_bitcode_input.py
File metadata and controls
executable file
·53 lines (40 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env python3
# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES.
# SPDX-License-Identifier: LicenseRef-NVIDIA-SOFTWARE-LICENSE
"""
Helper to produce static bitcode input for test_nvvm.py.
Usage:
python toolshed/build_static_bitcode_input.py
It will print a ready-to-paste MINIMAL_NVVMIR_BITCODE_STATIC entry for the
current NVVM IR version detected at runtime.
"""
import binascii
import os
import sys
import textwrap
import llvmlite.binding # HINT: pip install llvmlite
from cuda.bindings import nvvm
def get_minimal_nvvmir_txt_template():
cuda_bindings_tests_dir = os.path.normpath("cuda_bindings/tests")
assert os.path.isdir(cuda_bindings_tests_dir), (
"Please run this helper script from the cuda-python top-level directory."
)
sys.path.insert(0, os.path.abspath(cuda_bindings_tests_dir))
import test_nvvm
return test_nvvm.MINIMAL_NVVMIR_TXT_TEMPLATE
def main():
major, _minor, debug_major, _debug_minor = nvvm.ir_version()
txt = get_minimal_nvvmir_txt_template() % (major, debug_major)
bitcode_dynamic = llvmlite.binding.parse_assembly(txt.decode()).as_bitcode()
bitcode_hex = binascii.hexlify(bitcode_dynamic).decode("ascii")
print("\n\nMINIMAL_NVVMIR_BITCODE_STATIC = { # PLEASE ADD TO test_nvvm.py")
print(f" ({major}, {debug_major}): # (major, debug_major)")
lines = textwrap.wrap(bitcode_hex, width=80)
for line in lines[:-1]:
print(f' "{line}"')
print(f' "{lines[-1]}",')
print("}\n", flush=True)
print()
if __name__ == "__main__":
assert len(sys.argv) == 1, "This helper script does not take any arguments."
main()