Skip to content

Commit 6d8abd5

Browse files
slackitocjdb
authored andcommitted
[SandboxIR] Add tracking for ShuffleVectorInst::setShuffleMask. (llvm#105590)
1 parent 085cbc6 commit 6d8abd5

File tree

5 files changed

+61
-3
lines changed

5 files changed

+61
-3
lines changed

llvm/include/llvm/SandboxIR/SandboxIR.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,9 +1024,7 @@ class ShuffleVectorInst final
10241024
static Constant *convertShuffleMaskForBitcode(ArrayRef<int> Mask,
10251025
Type *ResultTy, Context &Ctx);
10261026

1027-
void setShuffleMask(ArrayRef<int> Mask) {
1028-
cast<llvm::ShuffleVectorInst>(Val)->setShuffleMask(Mask);
1029-
}
1027+
void setShuffleMask(ArrayRef<int> Mask);
10301028

10311029
ArrayRef<int> getShuffleMask() const {
10321030
return cast<llvm::ShuffleVectorInst>(Val)->getShuffleMask();

llvm/include/llvm/SandboxIR/Tracker.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ class AllocaInst;
6262
class CatchSwitchInst;
6363
class SwitchInst;
6464
class ConstantInt;
65+
class ShuffleVectorInst;
6566

6667
/// The base class for IR Change classes.
6768
class IRChangeBase {
@@ -355,6 +356,20 @@ class CreateAndInsertInst final : public IRChangeBase {
355356
#endif
356357
};
357358

359+
class ShuffleVectorSetMask final : public IRChangeBase {
360+
ShuffleVectorInst *SVI;
361+
SmallVector<int, 8> PrevMask;
362+
363+
public:
364+
ShuffleVectorSetMask(ShuffleVectorInst *SVI);
365+
void revert(Tracker &Tracker) final;
366+
void accept() final {}
367+
#ifndef NDEBUG
368+
void dump(raw_ostream &OS) const final { OS << "ShuffleVectorSetMask"; }
369+
LLVM_DUMP_METHOD void dump() const final;
370+
#endif
371+
};
372+
358373
/// The tracker collects all the change objects and implements the main API for
359374
/// saving / reverting / accepting.
360375
class Tracker {

llvm/lib/SandboxIR/SandboxIR.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1868,6 +1868,11 @@ Value *ShuffleVectorInst::create(Value *V1, Value *V2, ArrayRef<int> Mask,
18681868
return Ctx.getOrCreateConstant(cast<llvm::Constant>(NewV));
18691869
}
18701870

1871+
void ShuffleVectorInst::setShuffleMask(ArrayRef<int> Mask) {
1872+
Ctx.getTracker().emplaceIfTracking<ShuffleVectorSetMask>(this);
1873+
cast<llvm::ShuffleVectorInst>(Val)->setShuffleMask(Mask);
1874+
}
1875+
18711876
Constant *ShuffleVectorInst::getShuffleMaskForBitcode() const {
18721877
return Ctx.getOrCreateConstant(
18731878
cast<llvm::ShuffleVectorInst>(Val)->getShuffleMaskForBitcode());

llvm/lib/SandboxIR/Tracker.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,20 @@ void CreateAndInsertInst::dump() const {
234234
}
235235
#endif
236236

237+
ShuffleVectorSetMask::ShuffleVectorSetMask(ShuffleVectorInst *SVI)
238+
: SVI(SVI), PrevMask(SVI->getShuffleMask()) {}
239+
240+
void ShuffleVectorSetMask::revert(Tracker &Tracker) {
241+
SVI->setShuffleMask(PrevMask);
242+
}
243+
244+
#ifndef NDEBUG
245+
void ShuffleVectorSetMask::dump() const {
246+
dump(dbgs());
247+
dbgs() << "\n";
248+
}
249+
#endif
250+
237251
void Tracker::save() { State = TrackerState::Record; }
238252

239253
void Tracker::revert() {

llvm/unittests/SandboxIR/TrackerTest.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "llvm/IR/Module.h"
1414
#include "llvm/SandboxIR/SandboxIR.h"
1515
#include "llvm/Support/SourceMgr.h"
16+
#include "gmock/gmock-matchers.h"
1617
#include "gtest/gtest.h"
1718

1819
using namespace llvm;
@@ -792,6 +793,31 @@ define void @foo(i32 %cond0, i32 %cond1) {
792793
EXPECT_EQ(Switch->findCaseDest(BB1), One);
793794
}
794795

796+
TEST_F(TrackerTest, ShuffleVectorInstSetters) {
797+
parseIR(C, R"IR(
798+
define void @foo(<2 x i8> %v1, <2 x i8> %v2) {
799+
%shuf = shufflevector <2 x i8> %v1, <2 x i8> %v2, <2 x i32> <i32 1, i32 2>
800+
ret void
801+
}
802+
)IR");
803+
Function &LLVMF = *M->getFunction("foo");
804+
sandboxir::Context Ctx(C);
805+
806+
auto *F = Ctx.createFunction(&LLVMF);
807+
auto *BB = &*F->begin();
808+
auto It = BB->begin();
809+
auto *SVI = cast<sandboxir::ShuffleVectorInst>(&*It++);
810+
811+
// Check setShuffleMask.
812+
SmallVector<int, 2> OrigMask(SVI->getShuffleMask());
813+
Ctx.save();
814+
SVI->setShuffleMask(ArrayRef<int>({0, 0}));
815+
EXPECT_THAT(SVI->getShuffleMask(),
816+
testing::Not(testing::ElementsAreArray(OrigMask)));
817+
Ctx.revert();
818+
EXPECT_THAT(SVI->getShuffleMask(), testing::ElementsAreArray(OrigMask));
819+
}
820+
795821
TEST_F(TrackerTest, AtomicRMWSetters) {
796822
parseIR(C, R"IR(
797823
define void @foo(ptr %ptr, i8 %arg) {

0 commit comments

Comments
 (0)