Skip to content

[SandboxVec][Interval] Implement intersection and difference operations #110549

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,64 @@ template <typename T> class Interval {
const_iterator end() const {
return const_iterator(To != nullptr ? To->getNextNode() : nullptr, *this);
}
/// Equality.
bool operator==(const Interval &Other) const {
return From == Other.From && To == Other.To;
}
/// Inequality.
bool operator!=(const Interval &Other) const { return !(*this == Other); }
/// \Returns true if this and \p Other have nothing in common.
bool disjoint(const Interval &Other) const {
if (Other.empty())
return true;
if (empty())
return true;
return Other.To->comesBefore(From) || To->comesBefore(Other.From);
}
/// \Returns the intersection between this and \p Other.
// Example:
// |----| this
// |---| Other
// |-| this->getIntersection(Other)
Interval intersection(const Interval &Other) const {
if (empty())
return *this;
if (Other.empty())
return Interval();
// 1. No overlap
// A---B this
// C--D Other
if (To->comesBefore(Other.From) || Other.To->comesBefore(From))
return Interval();
// 2. Overlap.
// A---B this
// C--D Other
auto NewFromI = From->comesBefore(Other.From) ? Other.From : From;
auto NewToI = To->comesBefore(Other.To) ? To : Other.To;
return Interval(NewFromI, NewToI);
}
/// Difference operation. This returns up to two intervals.
// Example:
// |--------| this
// |-| Other
// |-| |--| this - Other
SmallVector<Interval, 2> operator-(const Interval &Other) {
if (disjoint(Other))
return {*this};
if (Other.empty())
return {*this};
if (*this == Other)
return {Interval()};
Interval Intersection = intersection(Other);
SmallVector<Interval, 2> Result;
// Part 1, skip if empty.
if (From != Intersection.From)
Result.emplace_back(From, Intersection.From->getPrevNode());
// Part 2, skip if empty.
if (Intersection.To != To)
Result.emplace_back(Intersection.To->getNextNode(), To);
return Result;
}
};

} // namespace llvm::sandboxir
Expand Down
158 changes: 158 additions & 0 deletions llvm/unittests/Transforms/Vectorize/SandboxVectorizer/IntervalTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "llvm/SandboxIR/Function.h"
#include "llvm/SandboxIR/Instruction.h"
#include "llvm/Support/SourceMgr.h"
#include "gmock/gmock-matchers.h"
#include "gtest/gtest.h"

using namespace llvm;
Expand Down Expand Up @@ -90,4 +91,161 @@ define void @foo(i8 %v0) {
auto BBIt = BB->begin();
for (auto &I : Intvl)
EXPECT_EQ(&I, &*BBIt++);
{
// Check equality.
EXPECT_TRUE(Empty == Empty);
EXPECT_FALSE(Empty == One);
EXPECT_TRUE(One == One);
sandboxir::Interval<sandboxir::Instruction> Intvl1(I0, I2);
sandboxir::Interval<sandboxir::Instruction> Intvl2(I0, I2);
EXPECT_TRUE(Intvl1 == Intvl1);
EXPECT_TRUE(Intvl1 == Intvl2);
}
{
// Check inequality.
EXPECT_FALSE(Empty != Empty);
EXPECT_TRUE(Empty != One);
EXPECT_FALSE(One != One);
sandboxir::Interval<sandboxir::Instruction> Intvl1(I0, I2);
sandboxir::Interval<sandboxir::Instruction> Intvl2(I0, I2);
EXPECT_FALSE(Intvl1 != Intvl1);
EXPECT_FALSE(Intvl1 != Intvl2);
}
{
// Check disjoint().
EXPECT_TRUE(Empty.disjoint(Empty));
EXPECT_TRUE(One.disjoint(Empty));
EXPECT_TRUE(Empty.disjoint(One));
sandboxir::Interval<sandboxir::Instruction> Intvl1(I0, I2);
sandboxir::Interval<sandboxir::Instruction> Intvl2(I1, Ret);
EXPECT_FALSE(Intvl1.disjoint(Intvl2));
sandboxir::Interval<sandboxir::Instruction> Intvl3(I2, I2);
EXPECT_FALSE(Intvl1.disjoint(Intvl3));
EXPECT_TRUE(Intvl1.disjoint(Empty));
}
}

// Helper function for returning a vector of instruction pointers from a range
// of references.
template <typename RangeT>
static SmallVector<sandboxir::Instruction *> getPtrVec(RangeT Range) {
SmallVector<sandboxir::Instruction *> PtrVec;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like this might be useful outside of tests, and therefore maybe should go in a utility class?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, but I think this should be avoided in actual code. We should iterate through the references instead of generating a vector of pointers.

for (sandboxir::Instruction &I : Range)
PtrVec.push_back(&I);
return PtrVec;
}

TEST_F(IntervalTest, Difference) {
parseIR(C, R"IR(
define void @foo(i8 %v0) {
%I0 = add i8 %v0, %v0
%I1 = add i8 %v0, %v0
%I2 = add i8 %v0, %v0
ret void
}
)IR");
Function &LLVMF = *M->getFunction("foo");
sandboxir::Context Ctx(C);
auto &F = *Ctx.createFunction(&LLVMF);
auto *BB = &*F.begin();
auto It = BB->begin();
auto *I0 = &*It++;
auto *I1 = &*It++;
auto *I2 = &*It++;
auto *Ret = &*It++;

{
// Check [I0,Ret] - []
sandboxir::Interval<sandboxir::Instruction> I0Ret(I0, Ret);
sandboxir::Interval<sandboxir::Instruction> Empty;
auto Diffs = I0Ret - Empty;
EXPECT_EQ(Diffs.size(), 1u);
const sandboxir::Interval<sandboxir::Instruction> &Diff = Diffs[0];
EXPECT_THAT(getPtrVec(Diff), testing::ElementsAre(I0, I1, I2, Ret));
}
{
// Check [] - [I0,Ret]
sandboxir::Interval<sandboxir::Instruction> Empty;
sandboxir::Interval<sandboxir::Instruction> I0Ret(I0, Ret);
auto Diffs = Empty - I0Ret;
EXPECT_EQ(Diffs.size(), 1u);
const sandboxir::Interval<sandboxir::Instruction> &Diff = Diffs[0];
EXPECT_TRUE(Diff.empty());
}
{
// Check [I0,Ret] - [I0].
sandboxir::Interval<sandboxir::Instruction> I0Ret(I0, Ret);
sandboxir::Interval<sandboxir::Instruction> I0I0(I0, I0);
auto Diffs = I0Ret - I0I0;
EXPECT_EQ(Diffs.size(), 1u);
const sandboxir::Interval<sandboxir::Instruction> &Diff = Diffs[0];
EXPECT_THAT(getPtrVec(Diff), testing::ElementsAre(I1, I2, Ret));
}
{
// Check [I0,Ret] - [I1].
sandboxir::Interval<sandboxir::Instruction> I0Ret(I0, Ret);
sandboxir::Interval<sandboxir::Instruction> I1I1(I1, I1);
auto Diffs = I0Ret - I1I1;
EXPECT_EQ(Diffs.size(), 2u);
const sandboxir::Interval<sandboxir::Instruction> &Diff0 = Diffs[0];
EXPECT_THAT(getPtrVec(Diff0), testing::ElementsAre(I0));
const sandboxir::Interval<sandboxir::Instruction> &Diff1 = Diffs[1];
EXPECT_THAT(getPtrVec(Diff1), testing::ElementsAre(I2, Ret));
}
}

TEST_F(IntervalTest, Intersection) {
parseIR(C, R"IR(
define void @foo(i8 %v0) {
%I0 = add i8 %v0, %v0
%I1 = add i8 %v0, %v0
%I2 = add i8 %v0, %v0
ret void
}
)IR");
Function &LLVMF = *M->getFunction("foo");
sandboxir::Context Ctx(C);
auto &F = *Ctx.createFunction(&LLVMF);
auto *BB = &*F.begin();
auto It = BB->begin();
auto *I0 = &*It++;
auto *I1 = &*It++;
[[maybe_unused]] auto *I2 = &*It++;
auto *Ret = &*It++;

{
// Check [I0,Ret] ^ []
sandboxir::Interval<sandboxir::Instruction> I0Ret(I0, Ret);
sandboxir::Interval<sandboxir::Instruction> Empty;
auto Intersection = I0Ret.intersection(Empty);
EXPECT_TRUE(Intersection.empty());
}
{
// Check [] ^ [I0,Ret]
sandboxir::Interval<sandboxir::Instruction> Empty;
sandboxir::Interval<sandboxir::Instruction> I0Ret(I0, Ret);
auto Intersection = Empty.intersection(I0Ret);
EXPECT_TRUE(Intersection.empty());
}
{
// Check [I0,Ret] ^ [I0]
sandboxir::Interval<sandboxir::Instruction> I0Ret(I0, Ret);
sandboxir::Interval<sandboxir::Instruction> I0I0(I0, I0);
auto Intersection = I0Ret.intersection(I0I0);
EXPECT_THAT(getPtrVec(Intersection), testing::ElementsAre(I0));
}
{
// Check [I0] ^ [I0,Ret]
sandboxir::Interval<sandboxir::Instruction> I0I0(I0, I0);
sandboxir::Interval<sandboxir::Instruction> I0Ret(I0, Ret);
auto Intersection = I0I0.intersection(I0Ret);
EXPECT_THAT(getPtrVec(Intersection), testing::ElementsAre(I0));
}
{
// Check [I0,Ret] ^ [I1].
sandboxir::Interval<sandboxir::Instruction> I0Ret(I0, Ret);
sandboxir::Interval<sandboxir::Instruction> I1I1(I1, I1);
auto Intersection = I0Ret.intersection(I1I1);
EXPECT_THAT(getPtrVec(Intersection), testing::ElementsAre(I1));
}
}
Loading