Help needed with mangling derivative function names

Hi compiler experts,

I'm working on differentiable programming in Swift and I'd like advice on implementing robust mangling for compiler-generated derivative functions/thunks. These mangled names will eventually be used in SIL differentiability witness tables.

There are a few components to mangle:

  • Name of the original SIL function: a StringRef.
  • Derivative function kind: jvp, differential, etc.
  • Differentiable parameter indices: a bit vector. (e.g. wrt_0_1)
  • Differentiation result indices: a bit vector. (e.g. src_0)
  • Generic requirements (optional): ArrayRef<Requirement>.

Currently, we've been using a hacky scheme, but we'd like to make the scheme robust and implement it in the mangler/demangler.

I took a look at Mangling.rst and ASTMangler but I'm not sure how to start. Could anyone please help me get started or point to related existing code? Any advice would be appreciated!

1 Like

A more pointed question: how can I approach mangling starting from a SILFunction?

The SIL differentiation transform should be able to generate mangle derivative functions given the original SILFunction. But many mangling utilities (ASTMangler methods, SILDeclRef::mangle, etc) take some sort of Decl *, and I'm not sure how to recover the original Decl * corresponding to a SILFunction (or whether it's possible).

I'll continue looking into mangling within SIL for more hints. I found SpecializationMangler which stores a SILFunction *, so that seems like a good starting point.