Skip to content

Commit

Permalink
[SandboxVec][Legality] Reject non-instructions (llvm#113190)
Browse files Browse the repository at this point in the history
  • Loading branch information
vporpo authored Oct 25, 2024
1 parent 75252e2 commit 6c9bbbc
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ enum class LegalityResultID {

/// The reason for vectorizing or not vectorizing.
enum class ResultReason {
NotInstructions,
DiffOpcodes,
DiffTypes,
};
Expand All @@ -46,6 +47,8 @@ struct ToStr {

static const char *getVecReason(ResultReason Reason) {
switch (Reason) {
case ResultReason::NotInstructions:
return "NotInstructions";
case ResultReason::DiffOpcodes:
return "DiffOpcodes";
case ResultReason::DiffTypes:
Expand All @@ -67,6 +70,10 @@ class LegalityResult {
LegalityResult(LegalityResultID ID) : ID(ID) {}
friend class LegalityAnalysis;

/// We shouldn't need copies.
LegalityResult(const LegalityResult &) = delete;
LegalityResult &operator=(const LegalityResult &) = delete;

public:
virtual ~LegalityResult() {}
LegalityResultID getSubclassID() const { return ID; }
Expand All @@ -90,6 +97,7 @@ class LegalityResultWithReason : public LegalityResult {
friend class Pack; // For constructor.

public:
ResultReason getReason() const { return Reason; }
#ifndef NDEBUG
void print(raw_ostream &OS) const override {
LegalityResult::print(OS);
Expand Down Expand Up @@ -138,7 +146,7 @@ class LegalityAnalysis {
}
/// Checks if it's legal to vectorize the instructions in \p Bndl.
/// \Returns a LegalityResult object owned by LegalityAnalysis.
LegalityResult &canVectorize(ArrayRef<Value *> Bndl);
const LegalityResult &canVectorize(ArrayRef<Value *> Bndl);
};

} // namespace llvm::sandboxir
Expand Down
18 changes: 17 additions & 1 deletion llvm/lib/Transforms/Vectorize/SandboxVectorizer/Legality.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@
//===----------------------------------------------------------------------===//

#include "llvm/Transforms/Vectorize/SandboxVectorizer/Legality.h"
#include "llvm/SandboxIR/Instruction.h"
#include "llvm/SandboxIR/Utils.h"
#include "llvm/SandboxIR/Value.h"
#include "llvm/Support/Debug.h"

namespace llvm::sandboxir {

#define DEBUG_TYPE "SBVec:Legality"

#ifndef NDEBUG
void LegalityResult::dump() const {
print(dbgs());
Expand All @@ -26,7 +30,19 @@ LegalityAnalysis::notVectorizableBasedOnOpcodesAndTypes(
return std::nullopt;
}

LegalityResult &LegalityAnalysis::canVectorize(ArrayRef<Value *> Bndl) {
static void dumpBndl(ArrayRef<Value *> Bndl) {
for (auto *V : Bndl)
dbgs() << *V << "\n";
}

const LegalityResult &LegalityAnalysis::canVectorize(ArrayRef<Value *> Bndl) {
// If Bndl contains values other than instructions, we need to Pack.
if (any_of(Bndl, [](auto *V) { return !isa<Instruction>(V); })) {
LLVM_DEBUG(dbgs() << "Not vectorizing: Not Instructions:\n";
dumpBndl(Bndl););
return createLegalityResult<Pack>(ResultReason::NotInstructions);
}

if (auto ReasonOpt = notVectorizableBasedOnOpcodesAndTypes(Bndl))
return createLegalityResult<Pack>(*ReasonOpt);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ static SmallVector<Value *, 4> getOperand(ArrayRef<Value *> Bndl,
}

void BottomUpVec::vectorizeRec(ArrayRef<Value *> Bndl) {
auto LegalityRes = Legality.canVectorize(Bndl);
const auto &LegalityRes = Legality.canVectorize(Bndl);
switch (LegalityRes.getSubclassID()) {
case LegalityResultID::Widen: {
auto *I = cast<Instruction>(Bndl[0]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,16 @@ define void @foo(ptr %ptr) {
auto *St1 = cast<sandboxir::StoreInst>(&*It++);

sandboxir::LegalityAnalysis Legality;
auto Result = Legality.canVectorize({St0, St1});
const auto &Result = Legality.canVectorize({St0, St1});
EXPECT_TRUE(isa<sandboxir::Widen>(Result));

{
// Check NotInstructions
auto &Result = Legality.canVectorize({F, St0});
EXPECT_TRUE(isa<sandboxir::Pack>(Result));
EXPECT_EQ(cast<sandboxir::Pack>(Result).getReason(),
sandboxir::ResultReason::NotInstructions);
}
}

#ifndef NDEBUG
Expand All @@ -68,6 +76,9 @@ TEST_F(LegalityTest, LegalityResultDump) {
sandboxir::LegalityAnalysis Legality;
EXPECT_TRUE(
Matches(Legality.createLegalityResult<sandboxir::Widen>(), "Widen"));
EXPECT_TRUE(Matches(Legality.createLegalityResult<sandboxir::Pack>(
sandboxir::ResultReason::NotInstructions),
"Pack Reason: NotInstructions"));
EXPECT_TRUE(Matches(Legality.createLegalityResult<sandboxir::Pack>(
sandboxir::ResultReason::DiffOpcodes),
"Pack Reason: DiffOpcodes"));
Expand Down

0 comments on commit 6c9bbbc

Please sign in to comment.