https://github.com/jryans approved this pull request. A little bit tricky as indicated by your PR summary, but overall the logic makes sense and appears to be covered with tests. Thanks for working on this! 😄 https://github.com/llvm/llvm-project/pull/72884