forked from Rust-GPU/Rust-CUDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen_intrinsics.py
64 lines (50 loc) · 2.25 KB
/
gen_intrinsics.py
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
54
55
56
57
58
59
60
61
62
63
64
# Generates rust code for every intrinsic from libdevice.json made in gen_libdevice_json.py
import os
import json
import inspect
dirname = os.path.dirname(__file__)
filename = os.path.join(dirname, 'data/libdevice.json')
out_filename = os.path.join(dirname, 'data/std_intrinsics.rs')
txt = open(filename, "r", encoding="utf8").read()
raw = json.loads(txt)
header = '''//! Raw libdevice math intrinsics.
//!
//! Note that this file was autogenerated using crude text analysis
//! from the libdevice PDF file, therefore many of the descriptions have broken
//! text, especially for math symbols. The link to the libdevice website is provided
//! for every intrinsic so you can view the non-broken description.
//!
//! Most of the intrinsics here have "proper" functions, corresponding f32/f64 functions
//! are already codegenned to libdevice intrinsics by the codegen automatically. This module
//! is mostly for exotic intrinsics that have not been added as proper functions yet.
//!
//! The underlying intrinsic functions have a prefix of `__nv_`, however this prefix
//! is stripped from the functions in this module just for convenience.
// Generated file, do not edit by hand, see scripts/gen_intrinsics.py
'''
out = ""
for intrinsic in raw:
raw_name = intrinsic["name"]
out += f"#[link_name = \"{raw_name}\"]\n"
name = raw_name.removeprefix("__nv_")
description = intrinsic["description"]
returns = intrinsic["returns"]
# There isnt actually any availability which is `No` instead of `Yes`, so including it is useless for now
hyperlink = f"[Nvidia docs](https://docs.nvidia.com/cuda/libdevice-users-guide/{raw_name}.html#{raw_name})"
full_desc = inspect.cleandoc(
f"{description}\n\n{hyperlink}\n\n# Returns\n\n{returns}")
out += f"#[doc = \"{full_desc}\"]\n"
out += f"pub fn {name}("
sig = intrinsic["sig"]
return_ty = sig["returns"]
params = []
for param in sig["params"]:
param_name = param["name"]
param_ty = param["type"]
if param_name == "in":
param_name = "in_"
params.append(f"{param_name}: {param_ty}")
out += ", ".join(params)
out += f") -> {return_ty};\n\n"
out = f"{header}extern \"C\" {{\n{out}}}\n"
open(out_filename, "w", encoding="utf8").write(out)