In case this helps : the automatic differentiation implemented in the MCMC package is reverse mode AD which is really well suited for gradient code generation (O(1) computation time). You may find some inspiration there. I hope to find the time in the near future to pull a change in MCMC that will separate the differentiation part into a separate module and will allow other uses than MCMC.
Sorry for being late to the conversation !