@@ -29,24 +29,37 @@ 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= String [])
32
+ function mangle_param (t, substitutions= Any [])
33
33
t == Nothing && return " v"
34
34
35
+ function find_substitution (x)
36
+ sub = findfirst (isequal (x), substitutions)
37
+ if sub === nothing
38
+ nothing
39
+ elseif sub == 1
40
+ " S_"
41
+ else
42
+ seq_id = uppercase (string (sub- 2 ; base= 36 ))
43
+ " S$(seq_id) _"
44
+ end
45
+ end
46
+
35
47
if isa (t, DataType) && t <: Ptr
36
48
tn = mangle_param (eltype (t), substitutions)
37
49
" P$tn "
38
50
elseif isa (t, DataType)
39
- tn = safe_name (t)
51
+ # check if we already know this type
52
+ str = find_substitution (t)
53
+ if str != = nothing
54
+ return str
55
+ end
40
56
41
- # handle substitutions
42
- sub = findfirst (isequal (tn), substitutions)
43
- if sub === nothing
57
+ # check if we already know this base type
58
+ str = find_substitution (t. name. wrapper)
59
+ if str === nothing
60
+ tn = safe_name (t)
44
61
str = " $(length (tn))$tn "
45
- push! (substitutions, tn)
46
- elseif sub == 1
47
- str = " S_"
48
- else
49
- str = " S$(sub- 2 ) _"
62
+ push! (substitutions, t. name. wrapper)
50
63
end
51
64
52
65
# encode typevars as template parameters
@@ -56,21 +69,24 @@ function mangle_param(t, substitutions=String[])
56
69
str *= mangle_param (t, substitutions)
57
70
end
58
71
str *= " E"
72
+
73
+ push! (substitutions, t)
59
74
end
60
75
61
76
str
62
77
elseif isa (t, Union)
63
- tn = " Union"
78
+ # check if we already know this union type
79
+ str = find_substitution (t)
80
+ if str != = nothing
81
+ return str
82
+ end
64
83
65
- # handle substitutions
66
- sub = findfirst (isequal (tn), substitutions)
67
- if sub === nothing
84
+ # check if we already know the Union name
85
+ str = find_substitution (Union)
86
+ if str === nothing
87
+ tn = " Union"
68
88
str = " $(length (tn))$tn "
69
89
push! (substitutions, tn)
70
- elseif sub == 1
71
- str = " S_"
72
- else
73
- str = " S$(sub- 2 ) _"
74
90
end
75
91
76
92
# encode union types as template parameters
@@ -80,9 +96,13 @@ function mangle_param(t, substitutions=String[])
80
96
str *= mangle_param (t, substitutions)
81
97
end
82
98
str *= " E"
99
+
100
+ push! (substitutions, t)
83
101
end
84
102
85
103
str
104
+ elseif isa (t, UnionAll)
105
+ mangle_param (t. body, substitutions)
86
106
elseif isa (t, Union{Bool, Cchar, Cuchar, Cshort, Cushort, Cint, Cuint, Clong, Culong, Clonglong, Culonglong, Int128, UInt128})
87
107
ts = t isa Bool ? ' b' : # bool
88
108
t isa Cchar ? ' a' : # signed char
@@ -99,10 +119,20 @@ function mangle_param(t, substitutions=String[])
99
119
t isa UInt128 ? ' o' : # unsigned __int128
100
120
error (" Invalid type" )
101
121
tn = string (abs (t), base= 10 )
122
+ # for legibility, encode Julia-native integers as C-native integers, if possible
123
+ if t isa Int && typemin (Cint) <= t <= typemax (Cint)
124
+ ts = ' i'
125
+ end
102
126
if t < 0
103
127
tn = ' n' * tn
104
128
end
105
129
" L$(ts)$(tn) E"
130
+ elseif t isa Float32
131
+ bits = string (reinterpret (UInt32, t); base= 16 )
132
+ " Lf$(bits) E"
133
+ elseif t isa Float64
134
+ bits = string (reinterpret (UInt64, t); base= 16 )
135
+ " Ld$(bits) E"
106
136
else
107
137
tn = safe_name (t) # TODO : actually does support digits...
108
138
if startswith (tn, r" \d " )
@@ -121,7 +151,7 @@ function mangle_sig(sig)
121
151
str = " _Z$(length (fn))$fn "
122
152
123
153
# mangle each parameter
124
- substitutions = String []
154
+ substitutions = []
125
155
for t in tt
126
156
str *= mangle_param (t, substitutions)
127
157
end
0 commit comments