AST dumping: dump template instantiations only once

Fixes infinite loop in PR15220.

Patch by Philip Craig.