This paper is an old favorite of mine. It shows how to transform a program that computes the value of a multivariate function into a program that computes that value and all of its first derivatives. The resulting program requires at most 7x more instructions as the original. Spoiler: it's just the rules of differentiation, and the constant 7 comes from the quotient rule.
https://courses.cs.washington.edu/courses/cse446/18wi/slides...