Skip to content

Commit 1540f77

Browse files
committed
Reapply "[SandboxVec][Legality] Reject non-instructions (#113190)"
This reverts commit eb9f475.
1 parent eb9f475 commit 1540f77

File tree

4 files changed

+41
-4
lines changed

4 files changed

+41
-4
lines changed

llvm/include/llvm/Transforms/Vectorize/SandboxVectorizer/Legality.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ enum class LegalityResultID {
2828

2929
/// The reason for vectorizing or not vectorizing.
3030
enum class ResultReason {
31+
NotInstructions,
3132
DiffOpcodes,
3233
DiffTypes,
3334
};
@@ -46,6 +47,8 @@ struct ToStr {
4647

4748
static const char *getVecReason(ResultReason Reason) {
4849
switch (Reason) {
50+
case ResultReason::NotInstructions:
51+
return "NotInstructions";
4952
case ResultReason::DiffOpcodes:
5053
return "DiffOpcodes";
5154
case ResultReason::DiffTypes:
@@ -67,6 +70,10 @@ class LegalityResult {
6770
LegalityResult(LegalityResultID ID) : ID(ID) {}
6871
friend class LegalityAnalysis;
6972

73+
/// We shouldn't need copies.
74+
LegalityResult(const LegalityResult &) = delete;
75+
LegalityResult &operator=(const LegalityResult &) = delete;
76+
7077
public:
7178
virtual ~LegalityResult() {}
7279
LegalityResultID getSubclassID() const { return ID; }
@@ -90,6 +97,7 @@ class LegalityResultWithReason : public LegalityResult {
9097
friend class Pack; // For constructor.
9198

9299
public:
100+
ResultReason getReason() const { return Reason; }
93101
#ifndef NDEBUG
94102
void print(raw_ostream &OS) const override {
95103
LegalityResult::print(OS);
@@ -138,7 +146,7 @@ class LegalityAnalysis {
138146
}
139147
/// Checks if it's legal to vectorize the instructions in \p Bndl.
140148
/// \Returns a LegalityResult object owned by LegalityAnalysis.
141-
LegalityResult &canVectorize(ArrayRef<Value *> Bndl);
149+
const LegalityResult &canVectorize(ArrayRef<Value *> Bndl);
142150
};
143151

144152
} // namespace llvm::sandboxir

llvm/lib/Transforms/Vectorize/SandboxVectorizer/Legality.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,15 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "llvm/Transforms/Vectorize/SandboxVectorizer/Legality.h"
10+
#include "llvm/SandboxIR/Instruction.h"
11+
#include "llvm/SandboxIR/Utils.h"
1012
#include "llvm/SandboxIR/Value.h"
1113
#include "llvm/Support/Debug.h"
1214

1315
namespace llvm::sandboxir {
1416

17+
#define DEBUG_TYPE "SBVec:Legality"
18+
1519
#ifndef NDEBUG
1620
void LegalityResult::dump() const {
1721
print(dbgs());
@@ -26,7 +30,21 @@ LegalityAnalysis::notVectorizableBasedOnOpcodesAndTypes(
2630
return std::nullopt;
2731
}
2832

29-
LegalityResult &LegalityAnalysis::canVectorize(ArrayRef<Value *> Bndl) {
33+
#ifndef NDEBUG
34+
static void dumpBndl(ArrayRef<Value *> Bndl) {
35+
for (auto *V : Bndl)
36+
dbgs() << *V << "\n";
37+
}
38+
#endif // NDEBUG
39+
40+
const LegalityResult &LegalityAnalysis::canVectorize(ArrayRef<Value *> Bndl) {
41+
// If Bndl contains values other than instructions, we need to Pack.
42+
if (any_of(Bndl, [](auto *V) { return !isa<Instruction>(V); })) {
43+
LLVM_DEBUG(dbgs() << "Not vectorizing: Not Instructions:\n";
44+
dumpBndl(Bndl););
45+
return createLegalityResult<Pack>(ResultReason::NotInstructions);
46+
}
47+
3048
if (auto ReasonOpt = notVectorizableBasedOnOpcodesAndTypes(Bndl))
3149
return createLegalityResult<Pack>(*ReasonOpt);
3250

llvm/lib/Transforms/Vectorize/SandboxVectorizer/Passes/BottomUpVec.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ static SmallVector<Value *, 4> getOperand(ArrayRef<Value *> Bndl,
4040
}
4141

4242
void BottomUpVec::vectorizeRec(ArrayRef<Value *> Bndl) {
43-
auto LegalityRes = Legality.canVectorize(Bndl);
43+
const auto &LegalityRes = Legality.canVectorize(Bndl);
4444
switch (LegalityRes.getSubclassID()) {
4545
case LegalityResultID::Widen: {
4646
auto *I = cast<Instruction>(Bndl[0]);

llvm/unittests/Transforms/Vectorize/SandboxVectorizer/LegalityTest.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,16 @@ define void @foo(ptr %ptr) {
5252
auto *St1 = cast<sandboxir::StoreInst>(&*It++);
5353

5454
sandboxir::LegalityAnalysis Legality;
55-
auto Result = Legality.canVectorize({St0, St1});
55+
const auto &Result = Legality.canVectorize({St0, St1});
5656
EXPECT_TRUE(isa<sandboxir::Widen>(Result));
57+
58+
{
59+
// Check NotInstructions
60+
auto &Result = Legality.canVectorize({F, St0});
61+
EXPECT_TRUE(isa<sandboxir::Pack>(Result));
62+
EXPECT_EQ(cast<sandboxir::Pack>(Result).getReason(),
63+
sandboxir::ResultReason::NotInstructions);
64+
}
5765
}
5866

5967
#ifndef NDEBUG
@@ -68,6 +76,9 @@ TEST_F(LegalityTest, LegalityResultDump) {
6876
sandboxir::LegalityAnalysis Legality;
6977
EXPECT_TRUE(
7078
Matches(Legality.createLegalityResult<sandboxir::Widen>(), "Widen"));
79+
EXPECT_TRUE(Matches(Legality.createLegalityResult<sandboxir::Pack>(
80+
sandboxir::ResultReason::NotInstructions),
81+
"Pack Reason: NotInstructions"));
7182
EXPECT_TRUE(Matches(Legality.createLegalityResult<sandboxir::Pack>(
7283
sandboxir::ResultReason::DiffOpcodes),
7384
"Pack Reason: DiffOpcodes"));

0 commit comments

Comments
 (0)