Skip to content
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
21 changes: 21 additions & 0 deletions llvm/include/llvm/Transforms/Vectorize/SandboxVectorizer/Debug.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//===- Debug.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
//
//===----------------------------------------------------------------------===//
//
// Defines the DEBUG_TYPE macro for LLVM_DEBUG which is shared across the
// vectorizer components.
//

#ifndef LLVM_TRANSFORMS_VECTORIZE_SANDBOXVECTORIZER_DEBUG_H
#define LLVM_TRANSFORMS_VECTORIZE_SANDBOXVECTORIZER_DEBUG_H

#include "llvm/Support/Debug.h"

#define DEBUG_TYPE "sandbox-vectorizer"
#define DEBUG_PREFIX "SBVec: "

#endif // LLVM_TRANSFORMS_VECTORIZE_SANDBOXVECTORIZER_DEBUG_H
14 changes: 1 addition & 13 deletions llvm/lib/Transforms/Vectorize/SandboxVectorizer/Legality.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@

namespace llvm::sandboxir {

#define DEBUG_TYPE "SBVec:Legality"

#ifndef NDEBUG
void ShuffleMask::dump() const {
print(dbgs());
Expand Down Expand Up @@ -191,13 +189,6 @@ LegalityAnalysis::notVectorizableBasedOnOpcodesAndTypes(
return std::nullopt;
}

#ifndef NDEBUG
static void dumpBndl(ArrayRef<Value *> Bndl) {
for (auto *V : Bndl)
dbgs() << *V << "\n";
}
#endif // NDEBUG

CollectDescr
LegalityAnalysis::getHowToCollectValues(ArrayRef<Value *> Bndl) const {
SmallVector<CollectDescr::ExtractElementDescr, 4> Vec;
Expand All @@ -220,11 +211,8 @@ LegalityAnalysis::getHowToCollectValues(ArrayRef<Value *> Bndl) const {
const LegalityResult &LegalityAnalysis::canVectorize(ArrayRef<Value *> Bndl,
bool SkipScheduling) {
// 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";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems useful

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is no longer needed because I am now dumping all legality outcomes at the call site of of canVectorize() here: BottomUpVec.cpp:289.

dumpBndl(Bndl););
if (any_of(Bndl, [](auto *V) { return !isa<Instruction>(V); }))
return createLegalityResult<Pack>(ResultReason::NotInstructions);
}
// Pack if not in the same BB.
auto *BB = cast<Instruction>(Bndl[0])->getParent();
if (any_of(drop_begin(Bndl),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "llvm/SandboxIR/Module.h"
#include "llvm/SandboxIR/Region.h"
#include "llvm/SandboxIR/Utils.h"
#include "llvm/Transforms/Vectorize/SandboxVectorizer/Debug.h"
#include "llvm/Transforms/Vectorize/SandboxVectorizer/VecUtils.h"

namespace llvm {
Expand Down Expand Up @@ -169,7 +170,9 @@ Value *BottomUpVec::createVectorInstr(ArrayRef<Value *> Bndl,
// TODO: Propagate debug info.
};

return CreateVectorInstr(Bndl, Operands);
auto *NewI = CreateVectorInstr(Bndl, Operands);
LLVM_DEBUG(dbgs() << DEBUG_PREFIX << "New instr: " << *NewI << "\n");
return NewI;
}

void BottomUpVec::tryEraseDeadInstrs() {
Expand All @@ -182,9 +185,11 @@ void BottomUpVec::tryEraseDeadInstrs() {
[](Instruction *I1, Instruction *I2) { return I1->comesBefore(I2); });
for (const auto &Pair : SortedDeadInstrCandidates) {
for (Instruction *I : reverse(Pair.second)) {
if (I->hasNUses(0))
if (I->hasNUses(0)) {
// Erase the dead instructions bottom-to-top.
LLVM_DEBUG(dbgs() << DEBUG_PREFIX << "Erase dead: " << *I << "\n");
I->eraseFromParent();
}
}
}
DeadInstrCandidates.clear();
Expand Down Expand Up @@ -277,8 +282,11 @@ Action *BottomUpVec::vectorizeRec(ArrayRef<Value *> Bndl,
ArrayRef<Value *> UserBndl, unsigned Depth) {
bool StopForDebug =
DebugBndlCnt++ >= StopBundle && StopBundle != StopBundleDisabled;
LLVM_DEBUG(dbgs() << DEBUG_PREFIX << "canVectorize() Bundle:\n";
VecUtils::dump(Bndl));
const auto &LegalityRes = StopForDebug ? Legality->getForcedPackForDebugging()
: Legality->canVectorize(Bndl);
LLVM_DEBUG(dbgs() << DEBUG_PREFIX << "Legality: " << LegalityRes << "\n");
auto ActionPtr =
std::make_unique<Action>(&LegalityRes, Bndl, UserBndl, Depth);
SmallVector<Action *> Operands;
Expand Down Expand Up @@ -479,6 +487,8 @@ bool BottomUpVec::tryVectorize(ArrayRef<Value *> Bndl) {
Actions.clear();
DebugBndlCnt = 0;
vectorizeRec(Bndl, {}, /*Depth=*/0);
LLVM_DEBUG(dbgs() << DEBUG_PREFIX << "BottomUpVec: Vectorization Actions:\n";
Actions.dump());
emitVectors();
tryEraseDeadInstrs();
return Change;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "llvm/Transforms/Vectorize/SandboxVectorizer/Passes/TransactionAcceptOrRevert.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/InstructionCost.h"
#include "llvm/Transforms/Vectorize/SandboxVectorizer/Debug.h"

namespace llvm {

Expand All @@ -20,15 +21,22 @@ namespace sandboxir {

bool TransactionAcceptOrRevert::runOnRegion(Region &Rgn, const Analyses &A) {
const auto &SB = Rgn.getScoreboard();
auto CostBefore = SB.getBeforeCost();
auto CostAfter = SB.getAfterCost();
InstructionCost CostAfterMinusBefore = SB.getAfterCost() - SB.getBeforeCost();
LLVM_DEBUG(dbgs() << DEBUG_PREFIX << "Cost gain: " << CostAfterMinusBefore
<< " (before/after/threshold: " << CostBefore << "/"
<< CostAfter << "/" << CostThreshold << ")\n");
// TODO: Print costs / write to remarks.
auto &Tracker = Rgn.getContext().getTracker();
if (CostAfterMinusBefore < -CostThreshold) {
bool HasChanges = !Tracker.empty();
Tracker.accept();
LLVM_DEBUG(dbgs() << DEBUG_PREFIX << "*** Transaction Accept ***\n");
return HasChanges;
}
// Revert the IR.
LLVM_DEBUG(dbgs() << DEBUG_PREFIX << "*** Transaction Revert ***\n");
Rgn.getContext().getTracker().revert();
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
#include "llvm/Transforms/Vectorize/SandboxVectorizer/Passes/TransactionSave.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/InstructionCost.h"
#include "llvm/Transforms/Vectorize/SandboxVectorizer/Debug.h"

namespace llvm::sandboxir {

bool TransactionSave::runOnRegion(Region &Rgn, const Analyses &A) {
LLVM_DEBUG(dbgs() << DEBUG_PREFIX << "*** Save Transaction ***\n");
Rgn.getContext().save();
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,12 @@
#include "llvm/IR/Module.h"
#include "llvm/SandboxIR/Constant.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Transforms/Vectorize/SandboxVectorizer/Debug.h"
#include "llvm/Transforms/Vectorize/SandboxVectorizer/SandboxVectorizerPassBuilder.h"
#include <regex>

using namespace llvm;

#define SV_NAME "sandbox-vectorizer"
#define DEBUG_TYPE SV_NAME

static cl::opt<bool>
PrintPassPipeline("sbvec-print-pass-pipeline", cl::init(false), cl::Hidden,
cl::desc("Prints the pass pipeline and returns."));
Expand Down Expand Up @@ -119,13 +117,16 @@ bool SandboxVectorizerPass::runImpl(Function &LLVMF) {

// If the target claims to have no vector registers early return.
if (!TTI->getNumberOfRegisters(TTI->getRegisterClassForType(true))) {
LLVM_DEBUG(dbgs() << "SBVec: Target has no vector registers, return.\n");
LLVM_DEBUG(dbgs() << DEBUG_PREFIX
<< "Target has no vector registers, return.\n");
return false;
}
LLVM_DEBUG(dbgs() << "SBVec: Analyzing " << LLVMF.getName() << ".\n");
LLVM_DEBUG(dbgs() << DEBUG_PREFIX << "Analyzing " << LLVMF.getName()
<< ".\n");
// Early return if the attribute NoImplicitFloat is used.
if (LLVMF.hasFnAttribute(Attribute::NoImplicitFloat)) {
LLVM_DEBUG(dbgs() << "SBVec: NoImplicitFloat attribute, return.\n");
LLVM_DEBUG(dbgs() << DEBUG_PREFIX
<< "NoImplicitFloat attribute, return.\n");
return false;
}

Expand Down
Loading