| 1 |
__copyright__ = "Copyright (C) 2022 Isuru Fernando" |
| 2 |
|
| 3 |
__license__ = """ |
| 4 |
Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 |
of this software and associated documentation files (the "Software"), to deal |
| 6 |
in the Software without restriction, including without limitation the rights |
| 7 |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 |
copies of the Software, and to permit persons to whom the Software is |
| 9 |
furnished to do so, subject to the following conditions: |
| 10 |
|
| 11 |
The above copyright notice and this permission notice shall be included in |
| 12 |
all copies or substantial portions of the Software. |
| 13 |
|
| 14 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 20 |
THE SOFTWARE. |
| 21 |
""" |
| 22 |
|
| 23 |
""" |
| 24 |
cldis.py |
| 25 |
|
| 26 |
A script to compile and print the native code for a OpenCL kernel. |
| 27 |
|
| 28 |
Usage: python cldis.py prog.cl <ptx/sass/asm> <build options> |
| 29 |
""" |
| 30 |
|
| 31 |
import glob |
| 32 |
import os |
| 33 |
import re |
| 34 |
import subprocess |
| 35 |
import sys |
| 36 |
import tempfile |
| 37 |
|
| 38 |
|
| 39 |
def main(ctx, tmp_dir, cl_str, output=None, build_options=()): |
| 40 |
device = ctx.devices[0] |
| 41 |
platform = device.platform |
| 42 |
if platform.name == "NVIDIA CUDA": |
| 43 |
supported_outputs = ["ptx", "sass"] |
| 44 |
elif platform.name == "Portable Computing Language": |
| 45 |
if device.name.startswith("NVIDIA"): |
| 46 |
supported_outputs = ["ptx", "sass"] |
| 47 |
elif device.name.startswith("pthread") or device.name.startswith("cpu"): |
| 48 |
supported_outputs = ["asm"] |
| 49 |
else: |
| 50 |
raise NotImplementedError(f"Unknown pocl device '{device.name}'") |
| 51 |
else: |
| 52 |
raise NotImplementedError(f"Unknown opencl device '{device}'") |
| 53 |
if output is None: |
| 54 |
output = supported_outputs[0] |
| 55 |
else: |
| 56 |
assert output in supported_outputs |
| 57 |
|
| 58 |
prg = cl.Program(ctx, cl_str).build(options=build_options, |
| 59 |
cache_dir=os.path.join(tmp_dir, "cache")) |
| 60 |
|
| 61 |
for binary in prg.binaries: |
| 62 |
if output in ["ptx", "sass"]: |
| 63 |
res = binary[binary.index(b"// Generated"):].decode("utf-8") |
| 64 |
if output == "sass": |
| 65 |
with open(os.path.join(tmp_dir, "cl.ptx"), "w") as f: |
| 66 |
f.write(res) |
| 67 |
tgt = re.findall(r".target sm_[0-9]*", res, re.MULTILINE)[0] |
| 68 |
gpu_name = tgt[8:] |
| 69 |
subprocess.check_call(["ptxas", "cl.ptx", "--verbose", |
| 70 |
f"--gpu-name={gpu_name}", "--warn-on-spills"], cwd=tmp_dir) |
| 71 |
res = subprocess.check_output(["cuobjdump", "-sass", "elf.o"], |
| 72 |
cwd=tmp_dir).decode("utf-8") |
| 73 |
|
| 74 |
elif output == "asm" and platform.name == "Portable Computing Language": |
| 75 |
so = glob.glob(f"{tmp_dir}/**/*.so", recursive=True)[0] |
| 76 |
res = subprocess.check_output(["objdump", "-d", so]).decode("utf-8") |
| 77 |
|
| 78 |
print(res) |
| 79 |
|
| 80 |
|
| 81 |
if __name__ == "__main__": |
| 82 |
with tempfile.TemporaryDirectory() as tmp_dir: |
| 83 |
os.environ["POCL_CACHE_DIR"] = os.path.join(tmp_dir, "pocl_cache") |
| 84 |
import pyopencl as cl |
| 85 |
ctx = cl.create_some_context() |
| 86 |
cl_file = sys.argv[1] |
| 87 |
with open(cl_file) as f: |
| 88 |
cl_str = f.read() |
| 89 |
output = sys.argv[2] if len(sys.argv) >= 3 else None |
| 90 |
build_options = sys.argv[3:] if len(sys.argv) >= 4 else [] |
| 91 |
main(ctx, tmp_dir, cl_str, output, build_options) |