Make PatternSub register the proper trackings and fix a bunch of call sites of local_optimizer to use the proper calling form.

Most of the tests pass now, except a bunch in the Cuda backend.