2
2
#
3
3
# SPDX-License-Identifier: Apache-2.0
4
4
5
+ """
6
+ An implementation of ``print`` for use in a kernel for the SPIRVKernelTarget.
7
+ """
8
+
5
9
from functools import singledispatch
6
10
7
11
import llvmlite .ir as llvmir
14
18
lower = registry .lower
15
19
16
20
17
- def declare_print (lmod ):
21
+ def declare_print (lmod : llvmir .Module ):
22
+ """Inserts declaration for C printf into the given LLVM module
23
+
24
+ Args:
25
+ lmod (llvmir.Module): LLVM module into which the function declaration
26
+ needs to be inserted.
27
+
28
+ Returns:
29
+ An LLVM IR Function object for the inserted C printf function.
30
+ """
18
31
voidptrty = llvmir .PointerType (
19
32
llvmir .IntType (8 ), addrspace = address_space .GENERIC .value
20
33
)
@@ -32,33 +45,34 @@ def print_item(ty, context, builder, val):
32
45
A (format string, [list of arguments]) is returned that will allow
33
46
forming the final printf()-like call.
34
47
"""
35
- raise NotImplementedError (
36
- "printing unimplemented for values of type %s" % (ty ,)
37
- )
48
+ raise NotImplementedError (f"printing unimplemented for values of type { ty } " )
38
49
39
50
40
51
@print_item .register (types .Integer )
41
52
@print_item .register (types .IntegerLiteral )
42
53
def int_print_impl (ty , context , builder , val ):
54
+ """Implements printing an integer value."""
43
55
if ty in types .unsigned_domain :
44
56
rawfmt = "%llu"
45
57
dsttype = types .uint64
46
58
else :
47
59
rawfmt = "%lld"
48
60
dsttype = types .int64
49
- fmt = context .insert_const_string (builder .module , rawfmt ) # noqa
61
+ context .insert_const_string (builder .module , rawfmt )
50
62
lld = context .cast (builder , val , ty , dsttype )
51
63
return rawfmt , [lld ]
52
64
53
65
54
66
@print_item .register (types .Float )
55
67
def real_print_impl (ty , context , builder , val ):
68
+ """Implements printing a real number value."""
56
69
lld = context .cast (builder , val , ty , types .float64 )
57
70
return "%f" , [lld ]
58
71
59
72
60
73
@print_item .register (types .StringLiteral )
61
74
def const_print_impl (ty , context , builder , sigval ):
75
+ """Implements printing a string value."""
62
76
pyval = ty .literal_value
63
77
assert isinstance (pyval , str ) # Ensured by lowering
64
78
rawfmt = "%s"
@@ -76,7 +90,7 @@ def print_varargs(context, builder, sig, args):
76
90
values = []
77
91
78
92
only_str = True
79
- for i , (argtype , argval ) in enumerate (zip (sig .args , args )):
93
+ for _ , (argtype , argval ) in enumerate (zip (sig .args , args )):
80
94
argfmt , argvals = print_item (argtype , context , builder , argval )
81
95
formats .append (argfmt )
82
96
values .extend (argvals )
0 commit comments