Skip to content

Commit 71e434d

Browse files
authored
[SandboxVec] Reapply "Add barebones Region class. (#108899)" (#109059)
A `#ifndef NDEBUG` in the wrong place caused an error in release builds.
1 parent 84d7f29 commit 71e434d

File tree

5 files changed

+232
-0
lines changed

5 files changed

+232
-0
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
//===- Region.h -------------------------------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_TRANSFORMS_VECTORIZE_SANDBOXVECTORIZER_REGION_H
10+
#define LLVM_TRANSFORMS_VECTORIZE_SANDBOXVECTORIZER_REGION_H
11+
12+
#include "llvm/ADT/SetVector.h"
13+
#include "llvm/ADT/iterator_range.h"
14+
#include "llvm/SandboxIR/SandboxIR.h"
15+
#include "llvm/Support/InstructionCost.h"
16+
#include "llvm/Support/raw_ostream.h"
17+
18+
namespace llvm::sandboxir {
19+
20+
/// The main job of the Region is to point to new instructions generated by
21+
/// vectorization passes. It is the unit that RegionPasses operate on with their
22+
/// runOnRegion() function.
23+
///
24+
/// The region allows us to stack transformations horizontally, meaning that
25+
/// each transformation operates on a single region and the resulting region is
26+
/// the input to the next transformation, as opposed to vertically, which is the
27+
/// common way of applying a transformation across the whole function. This
28+
/// enables us to check for profitability and decide whether we accept or
29+
/// rollback at a region granularity, which is much better than doing this at
30+
/// the function level.
31+
///
32+
// Traditional approach: transformations applied vertically for the whole
33+
// function
34+
// F
35+
// +----+
36+
// | |
37+
// | |
38+
// | | -> Transform1 -> ... -> TransformN -> Check Cost
39+
// | |
40+
// | |
41+
// +----+
42+
//
43+
// Region-based approach: transformations applied horizontally, for each Region
44+
// F
45+
// +----+
46+
// |Rgn1| -> Transform1 -> ... -> TransformN -> Check Cost
47+
// | |
48+
// |Rgn2| -> Transform1 -> ... -> TransformN -> Check Cost
49+
// | |
50+
// |Rgn3| -> Transform1 -> ... -> TransformN -> Check Cost
51+
// +----+
52+
53+
class Region {
54+
/// All the instructions in the Region. Only new instructions generated during
55+
/// vectorization are part of the Region.
56+
SetVector<Instruction *> Insts;
57+
58+
/// A unique ID, used for debugging.
59+
unsigned RegionID = 0;
60+
61+
Context &Ctx;
62+
63+
// TODO: Add cost modeling.
64+
// TODO: Add a way to encode/decode region info to/from metadata.
65+
66+
public:
67+
Region(Context &Ctx);
68+
~Region();
69+
70+
Context &getContext() const { return Ctx; }
71+
/// Returns the region's unique ID.
72+
unsigned getID() const { return RegionID; }
73+
74+
/// Adds I to the set.
75+
void add(Instruction *I);
76+
/// Removes I from the set.
77+
void remove(Instruction *I);
78+
/// Returns true if I is in the Region.
79+
bool contains(Instruction *I) const { return Insts.contains(I); }
80+
/// Returns true if the Region has no instructions.
81+
bool empty() const { return Insts.empty(); }
82+
83+
using iterator = decltype(Insts.begin());
84+
iterator begin() { return Insts.begin(); }
85+
iterator end() { return Insts.end(); }
86+
iterator_range<iterator> insts() { return make_range(begin(), end()); }
87+
88+
#ifndef NDEBUG
89+
/// This is an expensive check, meant for testing.
90+
bool operator==(const Region &Other) const;
91+
bool operator!=(const Region &other) const { return !(*this == other); }
92+
93+
void dump(raw_ostream &OS) const;
94+
void dump() const;
95+
friend raw_ostream &operator<<(raw_ostream &OS, const Region &Rgn) {
96+
Rgn.dump(OS);
97+
return OS;
98+
}
99+
#endif
100+
};
101+
102+
} // namespace llvm::sandboxir
103+
104+
#endif // LLVM_TRANSFORMS_VECTORIZE_SANDBOXVECTORIZER_REGION_H

llvm/lib/Transforms/Vectorize/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ add_llvm_component_library(LLVMVectorize
55
LoopVectorize.cpp
66
SandboxVectorizer/DependencyGraph.cpp
77
SandboxVectorizer/Passes/BottomUpVec.cpp
8+
SandboxVectorizer/Region.cpp
89
SandboxVectorizer/SandboxVectorizer.cpp
910
SLPVectorizer.cpp
1011
Vectorize.cpp
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//===- Region.cpp ---------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "llvm/Transforms/Vectorize/SandboxVectorizer/Region.h"
10+
11+
namespace llvm::sandboxir {
12+
13+
Region::Region(Context &Ctx) : Ctx(Ctx) {
14+
static unsigned StaticRegionID;
15+
RegionID = StaticRegionID++;
16+
}
17+
18+
Region::~Region() {}
19+
20+
void Region::add(Instruction *I) { Insts.insert(I); }
21+
22+
void Region::remove(Instruction *I) { Insts.remove(I); }
23+
24+
#ifndef NDEBUG
25+
bool Region::operator==(const Region &Other) const {
26+
if (Insts.size() != Other.Insts.size())
27+
return false;
28+
if (!std::is_permutation(Insts.begin(), Insts.end(), Other.Insts.begin()))
29+
return false;
30+
return true;
31+
}
32+
33+
void Region::dump(raw_ostream &OS) const {
34+
OS << "RegionID: " << getID() << "\n";
35+
for (auto *I : Insts)
36+
OS << *I << "\n";
37+
}
38+
39+
void Region::dump() const {
40+
dump(dbgs());
41+
dbgs() << "\n";
42+
}
43+
#endif // NDEBUG
44+
45+
} // namespace llvm::sandboxir

llvm/unittests/Transforms/Vectorize/SandboxVectorizer/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ set(LLVM_LINK_COMPONENTS
1010
add_llvm_unittest(SandboxVectorizerTests
1111
DependencyGraphTest.cpp
1212
LegalityTest.cpp
13+
RegionTest.cpp
1314
)
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
//===- RegionTest.cpp -----------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "llvm/Transforms/Vectorize/SandboxVectorizer/Region.h"
10+
#include "llvm/AsmParser/Parser.h"
11+
#include "llvm/SandboxIR/SandboxIR.h"
12+
#include "llvm/Support/SourceMgr.h"
13+
#include "gmock/gmock-matchers.h"
14+
#include "gtest/gtest.h"
15+
16+
using namespace llvm;
17+
18+
struct RegionTest : public testing::Test {
19+
LLVMContext C;
20+
std::unique_ptr<Module> M;
21+
22+
void parseIR(LLVMContext &C, const char *IR) {
23+
SMDiagnostic Err;
24+
M = parseAssemblyString(IR, Err, C);
25+
if (!M)
26+
Err.print("RegionTest", errs());
27+
}
28+
};
29+
30+
TEST_F(RegionTest, Basic) {
31+
parseIR(C, R"IR(
32+
define i8 @foo(i8 %v0, i8 %v1) {
33+
%t0 = add i8 %v0, 1
34+
%t1 = add i8 %t0, %v1
35+
ret i8 %t1
36+
}
37+
)IR");
38+
llvm::Function *LLVMF = &*M->getFunction("foo");
39+
sandboxir::Context Ctx(C);
40+
auto *F = Ctx.createFunction(LLVMF);
41+
auto *BB = &*F->begin();
42+
auto It = BB->begin();
43+
auto *T0 = cast<sandboxir::Instruction>(&*It++);
44+
auto *T1 = cast<sandboxir::Instruction>(&*It++);
45+
auto *Ret = cast<sandboxir::Instruction>(&*It++);
46+
sandboxir::Region Rgn(Ctx);
47+
48+
// Check getters
49+
EXPECT_EQ(&Ctx, &Rgn.getContext());
50+
EXPECT_EQ(0U, Rgn.getID());
51+
52+
// Check add / remove / empty.
53+
EXPECT_TRUE(Rgn.empty());
54+
Rgn.add(T0);
55+
EXPECT_FALSE(Rgn.empty());
56+
Rgn.remove(T0);
57+
EXPECT_TRUE(Rgn.empty());
58+
59+
// Check iteration.
60+
Rgn.add(T0);
61+
Rgn.add(T1);
62+
Rgn.add(Ret);
63+
// Use an ordered matcher because we're supposed to preserve the insertion
64+
// order for determinism.
65+
EXPECT_THAT(Rgn.insts(), testing::ElementsAre(T0, T1, Ret));
66+
67+
// Check contains
68+
EXPECT_TRUE(Rgn.contains(T0));
69+
Rgn.remove(T0);
70+
EXPECT_FALSE(Rgn.contains(T0));
71+
72+
#ifndef NDEBUG
73+
// Check equality comparison. Insert in reverse order into `Other` to check
74+
// that comparison is order-independent.
75+
sandboxir::Region Other(Ctx);
76+
Other.add(Ret);
77+
EXPECT_NE(Rgn, Other);
78+
Other.add(T1);
79+
EXPECT_EQ(Rgn, Other);
80+
#endif
81+
}

0 commit comments

Comments
 (0)