How does mangling_args work for types with parameters

Hello Numba developers. I’m hoping to get an explanation of how mangling_args works for types with parameters. I have a Numba class that depends on parameters which I’m currently just putting in the name. However, because my name may end up in the IR, I want to omit these parameters if possible to reduce the lowering time.

I see that Type defines mangling_args, which takes parameters instead, but I’m curious about how exactly it works. How are the parameters used exactly? If the parameters are different does this ensure lowering will pick up the correct function based on the parameters? If parameters are large do these appear in the final IR or is there some compression occurring?

1 Like

The responsibility of mangling_args is to return information that will correctly identify different instances of the types.Type. If we use a C++ template type as example, mangling_args for MyType<int, float> should return ("MyType", (types.int32, types.float32)). That provides enough information to identify the instantiation of the MyType template.

In Numba code, the Tuple.mangling_args source code is a good example.

The result of mangling_args is used by the symbol mangler (see source):

In [1]: from numba import types

In [2]: from numba.core.itanium_mangler import mangle_type_or_value

In [3]: t=types.Tuple.from_types([types.int32, types.float32])

In [4]: mangle_type_or_value(t)
Out[4]: '5TupleIifE'

The resulting string is used as part of the final symbol name. A function foo that takes t might have symbol name like _ZN8__main__7foo_241B42c8tJTIeFCjyCbUFRqqOAK_2f6h0kCng1maAA_3d_3dE5TupleIifE, which c++filt will demangles into __main__::foo_241[abi:c8tJTIeFCjyCbUFRqqOAK_2f6h0kCng1maAA_3d_3d](Tuple<int, float>).

If mangling_args returns the same information for different instances of the type, I would expect LLVM to fail because of symbol collision whenever it loads object-files with the same symbol name into memory.

If you have a type with a lot of type-parameters, you might want to consider doing some compression/substitution inside mangling_args. Currently, no compression is performed by the mangler in Numba.