Fix race condition in Function::optimized_graph(). (#27323)
The current logic is buggy, and will fail in the following situation:
Thread A: check optimized_graph_, it is empty.
Thread A: claim the mutex in order to initialize optimized_graph_.
Thread A: copy graph_ into optimized_graph_.
Thread A: start running optimizations on optimized_graph_.
Thread B: check optimized_graph_, it is not empty.
Thread B: start using optimized_graph_.
BUG: Thread B is using the graph while it's still being mutated by
Thread A.
[ghstack-poisoned]
Author
Mikhail Zolotukhin