Skip to content

Commit d2cbd5f

Browse files
authored
[SandboxIR][Region][NFC] Change visibility of Region::add()/remove() (#129277)
The vectorizer's passes should not be allowed to manually add/remove elements. This should only be done automatically by the callbacks.
1 parent ddbce2f commit d2cbd5f

File tree

2 files changed

+33
-27
lines changed

2 files changed

+33
-27
lines changed

llvm/include/llvm/SandboxIR/Region.h

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,12 @@ class Region {
114114
/// ID (for later deregistration) of the "erase instruction" callback.
115115
Context::CallbackID EraseInstCB;
116116

117-
// TODO: Add cost modeling.
118-
// TODO: Add a way to encode/decode region info to/from metadata.
117+
/// Adds I to the set.
118+
void add(Instruction *I);
119+
/// Removes I from the set.
120+
void remove(Instruction *I);
121+
friend class Context; // The callbacks need to call add() and remove().
122+
friend class RegionInternalsAttorney; // For unit tests.
119123

120124
/// Set \p I as the \p Idx'th element in the auxiliary vector.
121125
/// NOTE: This is for internal use, it does not set the metadata.
@@ -130,11 +134,6 @@ class Region {
130134
~Region();
131135

132136
Context &getContext() const { return Ctx; }
133-
134-
/// Adds I to the set.
135-
void add(Instruction *I);
136-
/// Removes I from the set.
137-
void remove(Instruction *I);
138137
/// Returns true if I is in the Region.
139138
bool contains(Instruction *I) const { return Insts.contains(I); }
140139
/// Returns true if the Region has no instructions.
@@ -170,6 +169,13 @@ class Region {
170169
#endif
171170
};
172171

172+
/// A helper client-attorney class for unit tests.
173+
class RegionInternalsAttorney {
174+
public:
175+
static void add(Region &Rgn, Instruction *I) { Rgn.add(I); }
176+
static void remove(Region &Rgn, Instruction *I) { Rgn.remove(I); }
177+
};
178+
173179
} // namespace llvm::sandboxir
174180

175181
#endif // LLVM_SANDBOXIR_REGION_H

llvm/unittests/SandboxIR/RegionTest.cpp

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -55,31 +55,31 @@ define i8 @foo(i8 %v0, i8 %v1) {
5555

5656
// Check add / remove / empty.
5757
EXPECT_TRUE(Rgn.empty());
58-
Rgn.add(T0);
58+
sandboxir::RegionInternalsAttorney::add(Rgn, T0);
5959
EXPECT_FALSE(Rgn.empty());
60-
Rgn.remove(T0);
60+
sandboxir::RegionInternalsAttorney::remove(Rgn, T0);
6161
EXPECT_TRUE(Rgn.empty());
6262

6363
// Check iteration.
64-
Rgn.add(T0);
65-
Rgn.add(T1);
66-
Rgn.add(Ret);
64+
sandboxir::RegionInternalsAttorney::add(Rgn, T0);
65+
sandboxir::RegionInternalsAttorney::add(Rgn, T1);
66+
sandboxir::RegionInternalsAttorney::add(Rgn, Ret);
6767
// Use an ordered matcher because we're supposed to preserve the insertion
6868
// order for determinism.
6969
EXPECT_THAT(Rgn.insts(), testing::ElementsAre(T0, T1, Ret));
7070

7171
// Check contains
7272
EXPECT_TRUE(Rgn.contains(T0));
73-
Rgn.remove(T0);
73+
sandboxir::RegionInternalsAttorney::remove(Rgn, T0);
7474
EXPECT_FALSE(Rgn.contains(T0));
7575

7676
#ifndef NDEBUG
7777
// Check equality comparison. Insert in reverse order into `Other` to check
7878
// that comparison is order-independent.
7979
sandboxir::Region Other(Ctx, *TTI);
80-
Other.add(Ret);
80+
sandboxir::RegionInternalsAttorney::add(Other, Ret);
8181
EXPECT_NE(Rgn, Other);
82-
Other.add(T1);
82+
sandboxir::RegionInternalsAttorney::add(Other, T1);
8383
EXPECT_EQ(Rgn, Other);
8484
#endif
8585
}
@@ -102,8 +102,8 @@ define i8 @foo(i8 %v0, i8 %v1, ptr %ptr) {
102102
auto *T1 = cast<sandboxir::Instruction>(&*It++);
103103
auto *Ret = cast<sandboxir::Instruction>(&*It++);
104104
sandboxir::Region Rgn(Ctx, *TTI);
105-
Rgn.add(T0);
106-
Rgn.add(T1);
105+
sandboxir::RegionInternalsAttorney::add(Rgn, T0);
106+
sandboxir::RegionInternalsAttorney::add(Rgn, T1);
107107

108108
// Test creation.
109109
auto *NewI = sandboxir::StoreInst::create(T0, Ptr, /*Align=*/std::nullopt,
@@ -186,9 +186,9 @@ define i8 @foo(i8 %v0, i8 %v1) {
186186
auto *T2 = cast<sandboxir::Instruction>(&*It++);
187187
[[maybe_unused]] auto *Ret = cast<sandboxir::Instruction>(&*It++);
188188
sandboxir::Region Rgn(Ctx, *TTI);
189-
Rgn.add(T0);
189+
sandboxir::RegionInternalsAttorney::add(Rgn, T0);
190190
sandboxir::Region Rgn2(Ctx, *TTI);
191-
Rgn2.add(T2);
191+
sandboxir::RegionInternalsAttorney::add(Rgn2, T2);
192192

193193
std::string output;
194194
llvm::raw_string_ostream RSO(output);
@@ -230,8 +230,8 @@ define i8 @foo(i8 %v0, i8 %v1) {
230230
auto *T1 = cast<sandboxir::Instruction>(&*It++);
231231

232232
sandboxir::Region Rgn(Ctx, *TTI);
233-
Rgn.add(T0);
234-
Rgn.add(T1);
233+
sandboxir::RegionInternalsAttorney::add(Rgn, T0);
234+
sandboxir::RegionInternalsAttorney::add(Rgn, T1);
235235

236236
SmallVector<std::unique_ptr<sandboxir::Region>> Regions =
237237
sandboxir::Region::createRegionsFromMD(*F, *TTI);
@@ -276,19 +276,19 @@ define void @foo(i8 %v0, i8 %v1, i8 %v2) {
276276
return TTI->getInstructionCost(LLVMI, Operands, CostKind);
277277
};
278278
// Add `Add0` to the region, should be counted in "After".
279-
Rgn.add(Add0);
279+
sandboxir::RegionInternalsAttorney::add(Rgn, Add0);
280280
EXPECT_EQ(SB.getBeforeCost(), 0);
281281
EXPECT_EQ(SB.getAfterCost(), GetCost(LLVMAdd0));
282282
// Same for `Add1`.
283-
Rgn.add(Add1);
283+
sandboxir::RegionInternalsAttorney::add(Rgn, Add1);
284284
EXPECT_EQ(SB.getBeforeCost(), 0);
285285
EXPECT_EQ(SB.getAfterCost(), GetCost(LLVMAdd0) + GetCost(LLVMAdd1));
286286
// Remove `Add0`, should be subtracted from "After".
287-
Rgn.remove(Add0);
287+
sandboxir::RegionInternalsAttorney::remove(Rgn, Add0);
288288
EXPECT_EQ(SB.getBeforeCost(), 0);
289289
EXPECT_EQ(SB.getAfterCost(), GetCost(LLVMAdd1));
290290
// Remove `Add2` which was never in the region, should counted in "Before".
291-
Rgn.remove(Add2);
291+
sandboxir::RegionInternalsAttorney::remove(Rgn, Add2);
292292
EXPECT_EQ(SB.getBeforeCost(), GetCost(LLVMAdd2));
293293
EXPECT_EQ(SB.getAfterCost(), GetCost(LLVMAdd1));
294294
}
@@ -446,11 +446,11 @@ define i8 @foo(i8 %v0, i8 %v1) {
446446
auto *T1 = cast<sandboxir::Instruction>(&*It++);
447447

448448
sandboxir::Region Rgn(Ctx, *TTI);
449-
Rgn.add(T0);
450-
Rgn.add(T1);
451449
#ifndef NDEBUG
452450
EXPECT_DEATH(Rgn.setAux({T0, T0}), ".*already.*");
453451
#endif
452+
sandboxir::RegionInternalsAttorney::add(Rgn, T0);
453+
sandboxir::RegionInternalsAttorney::add(Rgn, T1);
454454
Rgn.setAux({T1, T0});
455455

456456
SmallVector<std::unique_ptr<sandboxir::Region>> Regions =

0 commit comments

Comments
 (0)