@@ -29,24 +29,37 @@ safe_name(x) = safe_name(repr(x))
2929# we generate function names that look like C++ functions, because many tools, like NVIDIA's
3030# profilers, support them (grouping different instantiations of the same kernel together).
3131
32- function mangle_param (t, substitutions= String [])
32+ function mangle_param (t, substitutions= Any [])
3333 t == Nothing && return " v"
3434
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+
3547 if isa (t, DataType) && t <: Ptr
3648 tn = mangle_param (eltype (t), substitutions)
3749 " P$tn "
3850 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
4056
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)
4461 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)
5063 end
5164
5265 # encode typevars as template parameters
@@ -56,21 +69,24 @@ function mangle_param(t, substitutions=String[])
5669 str *= mangle_param (t, substitutions)
5770 end
5871 str *= " E"
72+
73+ push! (substitutions, t)
5974 end
6075
6176 str
6277 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
6483
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"
6888 str = " $(length (tn))$tn "
6989 push! (substitutions, tn)
70- elseif sub == 1
71- str = " S_"
72- else
73- str = " S$(sub- 2 ) _"
7490 end
7591
7692 # encode union types as template parameters
@@ -80,9 +96,13 @@ function mangle_param(t, substitutions=String[])
8096 str *= mangle_param (t, substitutions)
8197 end
8298 str *= " E"
99+
100+ push! (substitutions, t)
83101 end
84102
85103 str
104+ elseif isa (t, UnionAll)
105+ mangle_param (t. body, substitutions)
86106 elseif isa (t, Union{Bool, Cchar, Cuchar, Cshort, Cushort, Cint, Cuint, Clong, Culong, Clonglong, Culonglong, Int128, UInt128})
87107 ts = t isa Bool ? ' b' : # bool
88108 t isa Cchar ? ' a' : # signed char
@@ -99,10 +119,20 @@ function mangle_param(t, substitutions=String[])
99119 t isa UInt128 ? ' o' : # unsigned __int128
100120 error (" Invalid type" )
101121 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
102126 if t < 0
103127 tn = ' n' * tn
104128 end
105129 " 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"
106136 else
107137 tn = safe_name (t) # TODO : actually does support digits...
108138 if startswith (tn, r" \d " )
@@ -121,7 +151,7 @@ function mangle_sig(sig)
121151 str = " _Z$(length (fn))$fn "
122152
123153 # mangle each parameter
124- substitutions = String []
154+ substitutions = []
125155 for t in tt
126156 str *= mangle_param (t, substitutions)
127157 end
0 commit comments