In [mini-RFC] Name mangling in AOT, we discussed ways to accommodate the
runtime::Module tree in an embedded environment, where we prefer to call functions directly in the generated C using their symbol name rather than looking them up at runtime via a string dictionary. Mangling was needed to namespace these functions.
I think initially we viewed mangling as necessary for the top-level function names; therefore, we modified TECompilerImpl::Lower to produce PrimFuncs that contained module names.
Since then, we’ve come to a number of challenges around mangled names:
- tir.Constant may be declared as global variables by codegen; but then these names should really be mangled.
- We introduced a new MetadataModule which defined a PrimFunc outside of TECompilerImpl::Lower which we use to support the PackedFunc-based AOTExecutor. Because this function was defined separately, its name wasn’t mangled. This broke the multiple-model-in-the-same program use case. We had to work around this by conditionally mangling the function name depending which runtime was in use.
Recall that mangling is needed in cases where two models are compiled into the same program–this turns out to surface itself mainly in the microTVM/C runtime use case, where we are not loading shared libraries. We were able to implement AOTExecutor in the C++ runtime because there, modules are compiled into
.so and there is a mechanism to de-conflict symbol names which are duplicated across
.so. In the C runtime we are not so lucky–all functions must be prefixed/mangled to avoid conflict.
I propose we push all mangling out of the central compiler and into the codegen layer. This means that codegen would need to rewrite references to unmangled names as necessary–e.g. to tir.Constant, to tir.Call, and e.g. when generating FuncRegistry. This is somewhat burdensome on the C and LLVM codegens, but there, mangling is truly a property of the runtime environment and not necessarily something that needs to live in the central compiler.
Would love to hear everyone’s thoughts!