diff --git a/llvm/include/llvm/Transforms/Vectorize/SandboxVectorizer/Debug.h b/llvm/include/llvm/Transforms/Vectorize/SandboxVectorizer/Debug.h new file mode 100644 index 0000000000000..85b25c0249e7d --- /dev/null +++ b/llvm/include/llvm/Transforms/Vectorize/SandboxVectorizer/Debug.h @@ -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 diff --git a/llvm/lib/Transforms/Vectorize/SandboxVectorizer/Legality.cpp b/llvm/lib/Transforms/Vectorize/SandboxVectorizer/Legality.cpp index 74634372156aa..a80ae2aefff0a 100644 --- a/llvm/lib/Transforms/Vectorize/SandboxVectorizer/Legality.cpp +++ b/llvm/lib/Transforms/Vectorize/SandboxVectorizer/Legality.cpp @@ -17,8 +17,6 @@ namespace llvm::sandboxir { -#define DEBUG_TYPE "SBVec:Legality" - #ifndef NDEBUG void ShuffleMask::dump() const { print(dbgs()); @@ -191,13 +189,6 @@ LegalityAnalysis::notVectorizableBasedOnOpcodesAndTypes( return std::nullopt; } -#ifndef NDEBUG -static void dumpBndl(ArrayRef Bndl) { - for (auto *V : Bndl) - dbgs() << *V << "\n"; -} -#endif // NDEBUG - CollectDescr LegalityAnalysis::getHowToCollectValues(ArrayRef Bndl) const { SmallVector Vec; @@ -220,11 +211,8 @@ LegalityAnalysis::getHowToCollectValues(ArrayRef Bndl) const { const LegalityResult &LegalityAnalysis::canVectorize(ArrayRef Bndl, bool SkipScheduling) { // If Bndl contains values other than instructions, we need to Pack. - if (any_of(Bndl, [](auto *V) { return !isa(V); })) { - LLVM_DEBUG(dbgs() << "Not vectorizing: Not Instructions:\n"; - dumpBndl(Bndl);); + if (any_of(Bndl, [](auto *V) { return !isa(V); })) return createLegalityResult(ResultReason::NotInstructions); - } // Pack if not in the same BB. auto *BB = cast(Bndl[0])->getParent(); if (any_of(drop_begin(Bndl), diff --git a/llvm/lib/Transforms/Vectorize/SandboxVectorizer/Passes/BottomUpVec.cpp b/llvm/lib/Transforms/Vectorize/SandboxVectorizer/Passes/BottomUpVec.cpp index fe4f73e50687d..9a65f6cec42fb 100644 --- a/llvm/lib/Transforms/Vectorize/SandboxVectorizer/Passes/BottomUpVec.cpp +++ b/llvm/lib/Transforms/Vectorize/SandboxVectorizer/Passes/BottomUpVec.cpp @@ -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 { @@ -169,7 +170,9 @@ Value *BottomUpVec::createVectorInstr(ArrayRef 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() { @@ -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(); @@ -277,8 +282,11 @@ Action *BottomUpVec::vectorizeRec(ArrayRef Bndl, ArrayRef 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(&LegalityRes, Bndl, UserBndl, Depth); SmallVector Operands; @@ -479,6 +487,8 @@ bool BottomUpVec::tryVectorize(ArrayRef Bndl) { Actions.clear(); DebugBndlCnt = 0; vectorizeRec(Bndl, {}, /*Depth=*/0); + LLVM_DEBUG(dbgs() << DEBUG_PREFIX << "BottomUpVec: Vectorization Actions:\n"; + Actions.dump()); emitVectors(); tryEraseDeadInstrs(); return Change; diff --git a/llvm/lib/Transforms/Vectorize/SandboxVectorizer/Passes/TransactionAcceptOrRevert.cpp b/llvm/lib/Transforms/Vectorize/SandboxVectorizer/Passes/TransactionAcceptOrRevert.cpp index 874390ba2daae..ec929fb3c71e4 100644 --- a/llvm/lib/Transforms/Vectorize/SandboxVectorizer/Passes/TransactionAcceptOrRevert.cpp +++ b/llvm/lib/Transforms/Vectorize/SandboxVectorizer/Passes/TransactionAcceptOrRevert.cpp @@ -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 { @@ -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; } diff --git a/llvm/lib/Transforms/Vectorize/SandboxVectorizer/Passes/TransactionSave.cpp b/llvm/lib/Transforms/Vectorize/SandboxVectorizer/Passes/TransactionSave.cpp index 8d39d971273b4..e34d0fd817d0a 100644 --- a/llvm/lib/Transforms/Vectorize/SandboxVectorizer/Passes/TransactionSave.cpp +++ b/llvm/lib/Transforms/Vectorize/SandboxVectorizer/Passes/TransactionSave.cpp @@ -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; } diff --git a/llvm/lib/Transforms/Vectorize/SandboxVectorizer/SandboxVectorizer.cpp b/llvm/lib/Transforms/Vectorize/SandboxVectorizer/SandboxVectorizer.cpp index bffb9f187e882..20186426a5259 100644 --- a/llvm/lib/Transforms/Vectorize/SandboxVectorizer/SandboxVectorizer.cpp +++ b/llvm/lib/Transforms/Vectorize/SandboxVectorizer/SandboxVectorizer.cpp @@ -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 using namespace llvm; -#define SV_NAME "sandbox-vectorizer" -#define DEBUG_TYPE SV_NAME - static cl::opt PrintPassPipeline("sbvec-print-pass-pipeline", cl::init(false), cl::Hidden, cl::desc("Prints the pass pipeline and returns.")); @@ -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; }