[llvm] [SandboxIR] Preserve the order of switch cases after revert. (PR #115577)

via llvm-commits llvm-commits at lists.llvm.org
Mon Nov 11 14:57:18 PST 2024


================
@@ -170,7 +170,19 @@ void CatchSwitchAddHandler::revert(Tracker &Tracker) {
   LLVMCSI->removeHandler(LLVMCSI->handler_begin() + HandlerIdx);
 }
 
-void SwitchRemoveCase::revert(Tracker &Tracker) { Switch->addCase(Val, Dest); }
+void SwitchRemoveCase::revert(Tracker &Tracker) {
+  // removeCase swaps the last case with the deleted one. To revert it, we use
+  // addCase (which adds the new case to the end), and swap the newly-added
+  // value and successor operands to the positions for the original case index.
+  Switch->addCase(Val, Dest);
+  auto ValUseA = Switch->getOperandUse(2 + Index * 2);
+  auto SucUseA = Switch->getOperandUse(2 + Index * 2 + 1);
+  unsigned NumOps = Switch->getNumOperands();
+  auto ValUseB = Switch->getOperandUse(NumOps - 2);
+  auto SucUseB = Switch->getOperandUse(NumOps - 2 + 1);
+  ValUseA.swap(ValUseB);
----------------
vporpo wrote:

Oh I hadn't realized that this is how it works, makes sense though. Hmm but should we rely on this behavior ? it seems too implementation specific, so if someone changes the implementation in LLVM IR, our tests will break. I think a comment that explains how this works would be really helpful.

The alternative would be to record the order of all cases when we create the change object. Then when we want to revert we remove them all and add them in again. Wdyt?


https://github.com/llvm/llvm-project/pull/115577


More information about the llvm-commits mailing list