[llvm] [SandboxIR] Preserve the order of switch cases after revert. (PR #115577)
Jorge Gorbe Moya via llvm-commits
llvm-commits at lists.llvm.org
Fri Nov 8 19:21:54 PST 2024
https://github.com/slackito created https://github.com/llvm/llvm-project/pull/115577
Preserving the case order is not strictly necessary to preserve semantics (for example, operations like SwitchInst::removeCase will happily swap cases around). However, I'm planning to introduce an optional verification step for SandboxIR that will use StructuralHash to compare IR after a revert to the original IR to help catch tracker bugs, and the order difference triggers a difference there.
>From b266ea739a7678804c14c4752dbfd8f7d1beeebe Mon Sep 17 00:00:00 2001
From: Jorge Gorbe Moya <jgorbe at google.com>
Date: Fri, 8 Nov 2024 18:58:59 -0800
Subject: [PATCH] [SandboxIR] Preserve the order of switch cases after revert.
Preserving the case order is not strictly necessary to preserve
semantics (for example, operations like SwitchInst::removeCase will
happily swap cases around). However, I'm planning to introduce an
optional verification step for SandboxIR that will use StructuralHash to
compare IR after a revert to the original IR to help catch tracker bugs,
and the order difference triggers a difference there.
---
llvm/include/llvm/SandboxIR/Tracker.h | 6 ++++--
llvm/lib/SandboxIR/Instruction.cpp | 2 +-
llvm/lib/SandboxIR/Tracker.cpp | 14 +++++++++++++-
llvm/unittests/SandboxIR/TrackerTest.cpp | 13 +++++++++++++
4 files changed, 31 insertions(+), 4 deletions(-)
diff --git a/llvm/include/llvm/SandboxIR/Tracker.h b/llvm/include/llvm/SandboxIR/Tracker.h
index 3e3e539a8c7c16..7db0fc23c75fb9 100644
--- a/llvm/include/llvm/SandboxIR/Tracker.h
+++ b/llvm/include/llvm/SandboxIR/Tracker.h
@@ -315,12 +315,14 @@ class SwitchAddCase : public IRChangeBase {
class SwitchRemoveCase : public IRChangeBase {
SwitchInst *Switch;
+ unsigned Index;
ConstantInt *Val;
BasicBlock *Dest;
public:
- SwitchRemoveCase(SwitchInst *Switch, ConstantInt *Val, BasicBlock *Dest)
- : Switch(Switch), Val(Val), Dest(Dest) {}
+ SwitchRemoveCase(SwitchInst *Switch, unsigned Index, ConstantInt *Val,
+ BasicBlock *Dest)
+ : Switch(Switch), Index(Index), Val(Val), Dest(Dest) {}
void revert(Tracker &Tracker) final;
void accept() final {}
#ifndef NDEBUG
diff --git a/llvm/lib/SandboxIR/Instruction.cpp b/llvm/lib/SandboxIR/Instruction.cpp
index df941b2fa81efe..2df51a5947c173 100644
--- a/llvm/lib/SandboxIR/Instruction.cpp
+++ b/llvm/lib/SandboxIR/Instruction.cpp
@@ -1133,7 +1133,7 @@ void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
SwitchInst::CaseIt SwitchInst::removeCase(CaseIt It) {
auto &Case = *It;
Ctx.getTracker().emplaceIfTracking<SwitchRemoveCase>(
- this, Case.getCaseValue(), Case.getCaseSuccessor());
+ this, Case.getCaseIndex(), Case.getCaseValue(), Case.getCaseSuccessor());
auto *LLVMSwitch = cast<llvm::SwitchInst>(Val);
unsigned CaseNum = It - case_begin();
diff --git a/llvm/lib/SandboxIR/Tracker.cpp b/llvm/lib/SandboxIR/Tracker.cpp
index abcad39330094d..a6f25670c55207 100644
--- a/llvm/lib/SandboxIR/Tracker.cpp
+++ b/llvm/lib/SandboxIR/Tracker.cpp
@@ -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);
+ SucUseA.swap(SucUseB);
+}
#ifndef NDEBUG
void SwitchRemoveCase::dump() const {
diff --git a/llvm/unittests/SandboxIR/TrackerTest.cpp b/llvm/unittests/SandboxIR/TrackerTest.cpp
index 6d060854949e1f..71c4c3d0ea9210 100644
--- a/llvm/unittests/SandboxIR/TrackerTest.cpp
+++ b/llvm/unittests/SandboxIR/TrackerTest.cpp
@@ -963,6 +963,19 @@ define void @foo(i32 %cond0, i32 %cond1) {
EXPECT_EQ(Switch->getNumCases(), 2u);
EXPECT_EQ(Switch->findCaseDest(BB0), Zero);
EXPECT_EQ(Switch->findCaseDest(BB1), One);
+
+ // Check order is preserved after reverting multiple removeCase invocations.
+ Ctx.save();
+ Switch->removeCase(Switch->findCaseValue(Zero));
+ Switch->removeCase(Switch->findCaseValue(One));
+ EXPECT_EQ(Switch->getNumCases(), 0u);
+ Ctx.revert();
+ EXPECT_EQ(Switch->getNumCases(), 2u);
+ EXPECT_EQ(Switch->findCaseDest(BB0), Zero);
+ EXPECT_EQ(Switch->findCaseDest(BB1), One);
+ EXPECT_EQ(Switch->getSuccessor(0), OrigDefaultDest);
+ EXPECT_EQ(Switch->getSuccessor(1), BB0);
+ EXPECT_EQ(Switch->getSuccessor(2), BB1);
}
TEST_F(TrackerTest, SelectInst) {
More information about the llvm-commits
mailing list