[functorch] fix UB in interpreter stack (#101568)
The UB was:
- We grab a reference to the last element in the interpreter stack
(DynamicLayerStack)
- Then, we pop the last element in the interpreter stack
- Finally, we continue to use the reference to the last element.
The fix is to stop using that reference and instead use the popped
element.
Test Plan:
- It's difficult to write a test for this PR so I didn't
- Patched in https://github.com/pytorch/pytorch/pull/101409 and verified
that this PR fixes the bad_variant_access it was experiencing under
clang compilers.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/101568
Approved by: https://github.com/ezyang, https://github.com/Skylion007