[llvm] [SandboxIR] Add tracking for ShuffleVectorInst::commute. (PR #106644)

Jorge Gorbe Moya via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 3 09:57:16 PDT 2024


https://github.com/slackito updated https://github.com/llvm/llvm-project/pull/106644

>From d516f76eec9e28e2cbd87eeb5f76e276b2232339 Mon Sep 17 00:00:00 2001
From: Jorge Gorbe Moya <jgorbe at google.com>
Date: Thu, 29 Aug 2024 16:49:27 -0700
Subject: [PATCH 1/3] [SandboxIR] Add tracking for ShuffleVectorInst::commute.

Track it as an operand swap + a setShuffleMask and delegate to the
llvm::ShuffleVectorInst implementation.
---
 llvm/include/llvm/SandboxIR/SandboxIR.h  |  2 +-
 llvm/lib/SandboxIR/SandboxIR.cpp         |  7 +++++++
 llvm/unittests/SandboxIR/TrackerTest.cpp | 20 ++++++++++++++++----
 3 files changed, 24 insertions(+), 5 deletions(-)

diff --git a/llvm/include/llvm/SandboxIR/SandboxIR.h b/llvm/include/llvm/SandboxIR/SandboxIR.h
index d4c2efca4ecfa0..f16e900a67b763 100644
--- a/llvm/include/llvm/SandboxIR/SandboxIR.h
+++ b/llvm/include/llvm/SandboxIR/SandboxIR.h
@@ -1057,7 +1057,7 @@ class ShuffleVectorInst final
 
   /// Swap the operands and adjust the mask to preserve the semantics of the
   /// instruction.
-  void commute() { cast<llvm::ShuffleVectorInst>(Val)->commute(); }
+  void commute();
 
   /// Return true if a shufflevector instruction can be formed with the
   /// specified operands.
diff --git a/llvm/lib/SandboxIR/SandboxIR.cpp b/llvm/lib/SandboxIR/SandboxIR.cpp
index b5786cdafd6307..9d42deef2eba32 100644
--- a/llvm/lib/SandboxIR/SandboxIR.cpp
+++ b/llvm/lib/SandboxIR/SandboxIR.cpp
@@ -2134,6 +2134,13 @@ void ShuffleVectorInst::setShuffleMask(ArrayRef<int> Mask) {
   cast<llvm::ShuffleVectorInst>(Val)->setShuffleMask(Mask);
 }
 
+void ShuffleVectorInst::commute() {
+  Ctx.getTracker().emplaceIfTracking<ShuffleVectorSetMask>(this);
+  Ctx.getTracker().emplaceIfTracking<UseSwap>(getOperandUse(0),
+                                              getOperandUse(1));
+  cast<llvm::ShuffleVectorInst>(Val)->commute();
+}
+
 Constant *ShuffleVectorInst::getShuffleMaskForBitcode() const {
   return Ctx.getOrCreateConstant(
       cast<llvm::ShuffleVectorInst>(Val)->getShuffleMaskForBitcode());
diff --git a/llvm/unittests/SandboxIR/TrackerTest.cpp b/llvm/unittests/SandboxIR/TrackerTest.cpp
index ca6effb727bf37..398fe3e9d4553a 100644
--- a/llvm/unittests/SandboxIR/TrackerTest.cpp
+++ b/llvm/unittests/SandboxIR/TrackerTest.cpp
@@ -963,7 +963,7 @@ define void @foo(i32 %cond0, i32 %cond1) {
   EXPECT_EQ(Switch->findCaseDest(BB1), One);
 }
 
-TEST_F(TrackerTest, ShuffleVectorInstSetters) {
+TEST_F(TrackerTest, ShuffleVectorInst) {
   parseIR(C, R"IR(
 define void @foo(<2 x i8> %v1, <2 x i8> %v2) {
   %shuf = shufflevector <2 x i8> %v1, <2 x i8> %v2, <2 x i32> <i32 1, i32 2>
@@ -982,10 +982,22 @@ define void @foo(<2 x i8> %v1, <2 x i8> %v2) {
   SmallVector<int, 2> OrigMask(SVI->getShuffleMask());
   Ctx.save();
   SVI->setShuffleMask(ArrayRef<int>({0, 0}));
-  EXPECT_THAT(SVI->getShuffleMask(),
-              testing::Not(testing::ElementsAreArray(OrigMask)));
+  EXPECT_NE(SVI->getShuffleMask(), ArrayRef<int>(OrigMask));
   Ctx.revert();
-  EXPECT_THAT(SVI->getShuffleMask(), testing::ElementsAreArray(OrigMask));
+  EXPECT_EQ(SVI->getShuffleMask(), ArrayRef<int>(OrigMask));
+
+  // Check commute.
+  auto *Op0 = SVI->getOperand(0);
+  auto *Op1 = SVI->getOperand(1);
+  Ctx.save();
+  SVI->commute();
+  EXPECT_EQ(SVI->getOperand(0), Op1);
+  EXPECT_EQ(SVI->getOperand(1), Op0);
+  EXPECT_NE(SVI->getShuffleMask(), ArrayRef<int>(OrigMask));
+  Ctx.revert();
+  EXPECT_EQ(SVI->getOperand(0), Op0);
+  EXPECT_EQ(SVI->getOperand(1), Op1);
+  EXPECT_EQ(SVI->getShuffleMask(), ArrayRef<int>(OrigMask));
 }
 
 TEST_F(TrackerTest, PossiblyDisjointInstSetters) {

>From f1405704e26efbb2eaf7c94b83a7be3ffd8c02ea Mon Sep 17 00:00:00 2001
From: Jorge Gorbe Moya <jgorbe at google.com>
Date: Tue, 3 Sep 2024 09:52:04 -0700
Subject: [PATCH 2/3] fix bad merge

---
 llvm/lib/SandboxIR/SandboxIR.cpp | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/llvm/lib/SandboxIR/SandboxIR.cpp b/llvm/lib/SandboxIR/SandboxIR.cpp
index b86e35bb57e216..7b91a28d5eb5e5 100644
--- a/llvm/lib/SandboxIR/SandboxIR.cpp
+++ b/llvm/lib/SandboxIR/SandboxIR.cpp
@@ -2180,6 +2180,11 @@ void ShuffleVectorInst::setShuffleMask(ArrayRef<int> Mask) {
   cast<llvm::ShuffleVectorInst>(Val)->setShuffleMask(Mask);
 }
 
+VectorType *ShuffleVectorInst::getType() const {
+    return cast<VectorType>(
+              Ctx.getType(cast<llvm::ShuffleVectorInst>(Val)->getType()));
+}
+
 void ShuffleVectorInst::commute() {
   Ctx.getTracker().emplaceIfTracking<ShuffleVectorSetMask>(this);
   Ctx.getTracker().emplaceIfTracking<UseSwap>(getOperandUse(0),

>From 7cbe2ebbbe9138c613f8538bafc5aca80772fbcb Mon Sep 17 00:00:00 2001
From: Jorge Gorbe Moya <jgorbe at google.com>
Date: Tue, 3 Sep 2024 09:56:51 -0700
Subject: [PATCH 3/3] clang-format

---
 llvm/lib/SandboxIR/SandboxIR.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/SandboxIR/SandboxIR.cpp b/llvm/lib/SandboxIR/SandboxIR.cpp
index 7b91a28d5eb5e5..f95ced880d4cb2 100644
--- a/llvm/lib/SandboxIR/SandboxIR.cpp
+++ b/llvm/lib/SandboxIR/SandboxIR.cpp
@@ -2181,8 +2181,8 @@ void ShuffleVectorInst::setShuffleMask(ArrayRef<int> Mask) {
 }
 
 VectorType *ShuffleVectorInst::getType() const {
-    return cast<VectorType>(
-              Ctx.getType(cast<llvm::ShuffleVectorInst>(Val)->getType()));
+  return cast<VectorType>(
+      Ctx.getType(cast<llvm::ShuffleVectorInst>(Val)->getType()));
 }
 
 void ShuffleVectorInst::commute() {



More information about the llvm-commits mailing list