@@ -29,31 +29,33 @@ safe_name(x) = safe_name(repr(x))
29
29
# we generate function names that look like C++ functions, because many tools, like NVIDIA's
30
30
# profilers, support them (grouping different instantiations of the same kernel together).
31
31
32
- function mangle_param (t, substitutions= Any[])
32
+ function mangle_param (t, substitutions = Any[], top = false )
33
33
t == Nothing && return " v"
34
34
35
35
function find_substitution (x)
36
36
sub = findfirst (isequal (x), substitutions)
37
- if sub === nothing
37
+ res = if sub === nothing
38
38
nothing
39
39
elseif sub == 1
40
40
" S_"
41
41
else
42
42
seq_id = uppercase (string (sub- 2 ; base= 36 ))
43
43
" S$(seq_id) _"
44
44
end
45
+ return res
46
+ end
47
+
48
+ # check if we already know this type
49
+ str = find_substitution (t)
50
+ if str != = nothing
51
+ return str
45
52
end
46
53
47
54
if isa (t, DataType) && t <: Ptr
48
55
tn = mangle_param (eltype (t), substitutions)
56
+ push! (substitutions, t)
49
57
" P$tn "
50
58
elseif isa (t, DataType)
51
- # check if we already know this type
52
- str = find_substitution (t)
53
- if str != = nothing
54
- return str
55
- end
56
-
57
59
# check if we already know this base type
58
60
str = find_substitution (t. name. wrapper)
59
61
if str === nothing
@@ -62,47 +64,66 @@ function mangle_param(t, substitutions=Any[])
62
64
push! (substitutions, t. name. wrapper)
63
65
end
64
66
65
- # encode typevars as template parameters
66
- if ! isempty (t. parameters)
67
- str *= " I"
68
- for t in t. parameters
69
- str *= mangle_param (t, substitutions)
67
+ if t. name. wrapper == t && ! isa (t. name. wrapper, UnionAll)
68
+ # a type with no typevars
69
+ str
70
+ else
71
+ # encode typevars as template parameters
72
+ if isempty (t. parameters)
73
+ w_types = t. name. wrapper. types
74
+ if ! isempty (w_types) && ! isempty (w_types[end ] isa Core. TypeofVararg)
75
+ # If the type accepts a variable amount of parameters,
76
+ # e.g. `Tuple{}`, then we mark it as empty: "Tuple<>"
77
+ str *= " IJEE"
78
+ end
79
+ else
80
+ str *= " I"
81
+ for tp in t. parameters
82
+ str *= mangle_param (tp, substitutions)
83
+ end
84
+ str *= " E"
70
85
end
71
- str *= " E"
72
-
73
86
push! (substitutions, t)
87
+ str
74
88
end
75
-
76
- str
77
89
elseif isa (t, Union)
78
- # check if we already know this union type
79
- str = find_substitution (t)
80
- if str != = nothing
81
- return str
82
- end
83
-
84
90
# check if we already know the Union name
85
91
str = find_substitution (Union)
86
92
if str === nothing
87
93
tn = " Union"
88
94
str = " $(length (tn))$tn "
89
- push! (substitutions, tn )
95
+ push! (substitutions, Union )
90
96
end
91
97
92
98
# encode union types as template parameters
93
- if ! isempty (Base. uniontypes (t))
94
- str *= " I"
95
- for t in Base. uniontypes (t)
96
- str *= mangle_param (t, substitutions)
97
- end
98
- str *= " E"
99
-
100
- push! (substitutions, t)
99
+ str *= " I"
100
+ for tp in Base. uniontypes (t) # cannot be empty as `Union{}` is not a `Union`
101
+ str *= mangle_param (tp, substitutions)
101
102
end
103
+ str *= " E"
102
104
105
+ push! (substitutions, t)
103
106
str
104
107
elseif isa (t, UnionAll)
105
- mangle_param (t. body, substitutions)
108
+ mangle_param (Base. unwrap_unionall (t), substitutions)
109
+ elseif isa (t, Core. TypeofVararg)
110
+ T = isdefined (t, :T ) ? t. T : Any
111
+ if isdefined (t, :N )
112
+ # For NTuple, repeat the type as needed
113
+ str = " "
114
+ for _ in 1 : t. N
115
+ str *= mangle_param (T, substitutions)
116
+ end
117
+ str
118
+ elseif top
119
+ # Variadic arguments only make sense for function arguments
120
+ mangle_param (T, substitutions) * " z" # T...
121
+ else
122
+ # Treat variadic arguments for a type as no arguments
123
+ " "
124
+ end
125
+ elseif isa (t, Char)
126
+ mangle_param (UInt32 (t), substitutions)
106
127
elseif isa (t, Union{Bool, Cchar, Cuchar, Cshort, Cushort, Cint, Cuint, Clong, Culong, Clonglong, Culonglong, Int128, UInt128})
107
128
ts = t isa Bool ? ' b' : # bool
108
129
t isa Cchar ? ' a' : # signed char
@@ -153,7 +174,7 @@ function mangle_sig(sig)
153
174
# mangle each parameter
154
175
substitutions = []
155
176
for t in tt
156
- str *= mangle_param (t, substitutions)
177
+ str *= mangle_param (t, substitutions, true )
157
178
end
158
179
159
180
return str
0 commit comments