Provide a generic graph cycle detector
Summary:
It looks like we need this logic in a few places and it's tricky to get right,
so provide a generic implementation.
This should make some stuff like D595 much less grotesque to build.
Test Plan: See unit tests, which should provide pretty thorough coverage.
Reviewed By: jungejason
Reviewers: jungejason, tuomaspelkonen, aran, cadamo
CC: aran, epriestley, jungejason
Differential Revision: 670