Skip to content

[SandboxVec][Interval] Convert InstrInterval class to a class template #110021

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 27, 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 @@ -25,7 +25,7 @@
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/iterator_range.h"
#include "llvm/SandboxIR/SandboxIR.h"
#include "llvm/Transforms/Vectorize/SandboxVectorizer/InstrInterval.h"
#include "llvm/Transforms/Vectorize/SandboxVectorizer/Interval.h"

namespace llvm::sandboxir {

Expand Down Expand Up @@ -85,7 +85,7 @@ class DependencyGraph {
}
/// Build/extend the dependency graph such that it includes \p Instrs. Returns
/// the interval spanning \p Instrs.
InstrInterval extend(ArrayRef<Instruction *> Instrs);
Interval<Instruction> extend(ArrayRef<Instruction *> Instrs);
#ifndef NDEBUG
void print(raw_ostream &OS) const;
LLVM_DUMP_METHOD void dump() const;
Expand Down

This file was deleted.

125 changes: 125 additions & 0 deletions llvm/include/llvm/Transforms/Vectorize/SandboxVectorizer/Interval.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
//===- Interval.h -----------------------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// The Interval class is a generic interval of ordered objects that implement:
// - T * T::getPrevNode()
// - T * T::getNextNode()
// - bool T::comesBefore(const T *) const
//
// This is currently used for Instruction intervals.
// It provides an API for some basic operations on the interval, including some
// simple set operations, like union, interseciton and others.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_TRANSFORMS_VECTORIZE_SANDBOXVECTORIZER_INSTRINTERVAL_H
#define LLVM_TRANSFORMS_VECTORIZE_SANDBOXVECTORIZER_INSTRINTERVAL_H

#include "llvm/SandboxIR/SandboxIR.h"
#include <iterator>

namespace llvm::sandboxir {

/// A simple iterator for iterating the interval.
template <typename T, typename IntervalType> class IntervalIterator {
T *I;
IntervalType &R;

public:
using difference_type = std::ptrdiff_t;
using value_type = T;
using pointer = value_type *;
using reference = T &;
using iterator_category = std::bidirectional_iterator_tag;

IntervalIterator(T *I, IntervalType &R) : I(I), R(R) {}
bool operator==(const IntervalIterator &Other) const {
assert(&R == &Other.R && "Iterators belong to different regions!");
return Other.I == I;
}
bool operator!=(const IntervalIterator &Other) const {
return !(*this == Other);
}
IntervalIterator &operator++() {
assert(I != nullptr && "already at end()!");
I = I->getNextNode();
return *this;
}
IntervalIterator operator++(int) {
auto ItCopy = *this;
++*this;
return ItCopy;
}
IntervalIterator &operator--() {
// `I` is nullptr for end() when To is the BB terminator.
I = I != nullptr ? I->getPrevNode() : R.To;
return *this;
}
IntervalIterator operator--(int) {
auto ItCopy = *this;
--*this;
return ItCopy;
}
template <typename HT = std::enable_if<std::is_same<T, T *&>::value>>
T &operator*() {
return *I;
}
T &operator*() const { return *I; }
};

template <typename T> class Interval {
T *From;
T *To;

public:
Interval() : From(nullptr), To(nullptr) {}
Interval(T *From, T *To) : From(From), To(To) {
assert((From == To || From->comesBefore(To)) &&
"From should come before From!");
}
Interval(ArrayRef<T *> Elems) {
assert(!Elems.empty() && "Expected non-empty Elems!");
From = Elems[0];
To = Elems[0];
for (auto *I : drop_begin(Elems)) {
if (I->comesBefore(From))
From = I;
else if (To->comesBefore(I))
To = I;
}
}
bool empty() const {
assert(((From == nullptr && To == nullptr) ||
(From != nullptr && To != nullptr)) &&
"Either none or both should be null");
return From == nullptr;
}
bool contains(T *I) const {
if (empty())
return false;
return (From == I || From->comesBefore(I)) &&
(I == To || I->comesBefore(To));
}
T *top() const { return From; }
T *bottom() const { return To; }

using iterator = IntervalIterator<T, Interval>;
using const_iterator = IntervalIterator<const T, const Interval>;
iterator begin() { return iterator(From, *this); }
iterator end() {
return iterator(To != nullptr ? To->getNextNode() : nullptr, *this);
}
const_iterator begin() const { return const_iterator(From, *this); }
const_iterator end() const {
return const_iterator(To != nullptr ? To->getNextNode() : nullptr, *this);
}
};

} // namespace llvm::sandboxir

#endif // LLVM_TRANSFORMS_VECTORIZE_SANDBOXVECTORIZER_INSTRINTERVAL_H
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ void DGNode::dump() const {
}
#endif // NDEBUG

InstrInterval DependencyGraph::extend(ArrayRef<Instruction *> Instrs) {
Interval<Instruction> DependencyGraph::extend(ArrayRef<Instruction *> Instrs) {
if (Instrs.empty())
return {};
// TODO: For now create a chain of dependencies.
InstrInterval Interval(Instrs);
Interval<Instruction> Interval(Instrs);
auto *TopI = Interval.top();
auto *BotI = Interval.bottom();
DGNode *LastN = getOrCreateNode(TopI);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ set(LLVM_LINK_COMPONENTS

add_llvm_unittest(SandboxVectorizerTests
DependencyGraphTest.cpp
InstrIntervalTest.cpp
IntervalTest.cpp
LegalityTest.cpp
RegionTest.cpp
)
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
//===- InstrIntervalTest.cpp ----------------------------------------------===//
//===- IntervalTest.cpp ---------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "llvm/Transforms/Vectorize/SandboxVectorizer/InstrInterval.h"
#include "llvm/Transforms/Vectorize/SandboxVectorizer/Interval.h"
#include "llvm/AsmParser/Parser.h"
#include "llvm/SandboxIR/SandboxIR.h"
#include "llvm/Support/SourceMgr.h"
#include "gmock/gmock-matchers.h"
#include "gtest/gtest.h"

using namespace llvm;

struct InstrIntervalTest : public testing::Test {
struct IntervalTest : public testing::Test {
LLVMContext C;
std::unique_ptr<Module> M;

Expand All @@ -27,7 +26,7 @@ struct InstrIntervalTest : public testing::Test {
}
};

TEST_F(InstrIntervalTest, Basic) {
TEST_F(IntervalTest, Basic) {
parseIR(C, R"IR(
define void @foo(i8 %v0) {
%add0 = add i8 %v0, %v0
Expand All @@ -46,46 +45,47 @@ define void @foo(i8 %v0) {
auto *I2 = &*It++;
auto *Ret = &*It++;

sandboxir::InstrInterval Interval(I0, Ret);
sandboxir::Interval<sandboxir::Instruction> Intvl(I0, Ret);
#ifndef NDEBUG
EXPECT_DEATH(sandboxir::InstrInterval(I1, I0), ".*before.*");
EXPECT_DEATH(sandboxir::Interval<sandboxir::Instruction>(I1, I0),
".*before.*");
#endif // NDEBUG
// Check InstrInterval(ArrayRef), from(), to().
// Check Interval<sandboxir::Instruction>(ArrayRef), from(), to().
{
sandboxir::InstrInterval Interval(
sandboxir::Interval<sandboxir::Instruction> Intvl(
SmallVector<sandboxir::Instruction *>({I0, Ret}));
EXPECT_EQ(Interval.top(), I0);
EXPECT_EQ(Interval.bottom(), Ret);
EXPECT_EQ(Intvl.top(), I0);
EXPECT_EQ(Intvl.bottom(), Ret);
}
{
sandboxir::InstrInterval Interval(
sandboxir::Interval<sandboxir::Instruction> Intvl(
SmallVector<sandboxir::Instruction *>({Ret, I0}));
EXPECT_EQ(Interval.top(), I0);
EXPECT_EQ(Interval.bottom(), Ret);
EXPECT_EQ(Intvl.top(), I0);
EXPECT_EQ(Intvl.bottom(), Ret);
}
{
sandboxir::InstrInterval Interval(
sandboxir::Interval<sandboxir::Instruction> Intvl(
SmallVector<sandboxir::Instruction *>({I0, I0}));
EXPECT_EQ(Interval.top(), I0);
EXPECT_EQ(Interval.bottom(), I0);
EXPECT_EQ(Intvl.top(), I0);
EXPECT_EQ(Intvl.bottom(), I0);
}

// Check empty().
EXPECT_FALSE(Interval.empty());
sandboxir::InstrInterval Empty;
EXPECT_FALSE(Intvl.empty());
sandboxir::Interval<sandboxir::Instruction> Empty;
EXPECT_TRUE(Empty.empty());
sandboxir::InstrInterval One(I0, I0);
sandboxir::Interval<sandboxir::Instruction> One(I0, I0);
EXPECT_FALSE(One.empty());
// Check contains().
for (auto &I : *BB) {
EXPECT_TRUE(Interval.contains(&I));
EXPECT_TRUE(Intvl.contains(&I));
EXPECT_FALSE(Empty.contains(&I));
}
EXPECT_FALSE(One.contains(I1));
EXPECT_FALSE(One.contains(I2));
EXPECT_FALSE(One.contains(Ret));
// Check iterator.
auto BBIt = BB->begin();
for (auto &I : Interval)
for (auto &I : Intvl)
EXPECT_EQ(&I, &*BBIt++);
}
Loading