Skip to content

Commit 45d0180

Browse files
authored
[SandboxVec][NFC] Add LLVM_DEBUG dumps (#129335)
This patch updates/adds LLVM_DEBUG dumps. It moves the DEBUG_TYPE into SandboxVectorizer/Debug.h such that it can be shared across all components of the vectorizer.
1 parent a085da6 commit 45d0180

File tree

6 files changed

+51
-21
lines changed

6 files changed

+51
-21
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===- Debug.h --------------------------------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// Defines the DEBUG_TYPE macro for LLVM_DEBUG which is shared across the
10+
// vectorizer components.
11+
//
12+
13+
#ifndef LLVM_TRANSFORMS_VECTORIZE_SANDBOXVECTORIZER_DEBUG_H
14+
#define LLVM_TRANSFORMS_VECTORIZE_SANDBOXVECTORIZER_DEBUG_H
15+
16+
#include "llvm/Support/Debug.h"
17+
18+
#define DEBUG_TYPE "sandbox-vectorizer"
19+
#define DEBUG_PREFIX "SBVec: "
20+
21+
#endif // LLVM_TRANSFORMS_VECTORIZE_SANDBOXVECTORIZER_DEBUG_H

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

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717

1818
namespace llvm::sandboxir {
1919

20-
#define DEBUG_TYPE "SBVec:Legality"
21-
2220
#ifndef NDEBUG
2321
void ShuffleMask::dump() const {
2422
print(dbgs());
@@ -191,13 +189,6 @@ LegalityAnalysis::notVectorizableBasedOnOpcodesAndTypes(
191189
return std::nullopt;
192190
}
193191

194-
#ifndef NDEBUG
195-
static void dumpBndl(ArrayRef<Value *> Bndl) {
196-
for (auto *V : Bndl)
197-
dbgs() << *V << "\n";
198-
}
199-
#endif // NDEBUG
200-
201192
CollectDescr
202193
LegalityAnalysis::getHowToCollectValues(ArrayRef<Value *> Bndl) const {
203194
SmallVector<CollectDescr::ExtractElementDescr, 4> Vec;
@@ -220,11 +211,8 @@ LegalityAnalysis::getHowToCollectValues(ArrayRef<Value *> Bndl) const {
220211
const LegalityResult &LegalityAnalysis::canVectorize(ArrayRef<Value *> Bndl,
221212
bool SkipScheduling) {
222213
// If Bndl contains values other than instructions, we need to Pack.
223-
if (any_of(Bndl, [](auto *V) { return !isa<Instruction>(V); })) {
224-
LLVM_DEBUG(dbgs() << "Not vectorizing: Not Instructions:\n";
225-
dumpBndl(Bndl););
214+
if (any_of(Bndl, [](auto *V) { return !isa<Instruction>(V); }))
226215
return createLegalityResult<Pack>(ResultReason::NotInstructions);
227-
}
228216
// Pack if not in the same BB.
229217
auto *BB = cast<Instruction>(Bndl[0])->getParent();
230218
if (any_of(drop_begin(Bndl),

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "llvm/SandboxIR/Module.h"
1515
#include "llvm/SandboxIR/Region.h"
1616
#include "llvm/SandboxIR/Utils.h"
17+
#include "llvm/Transforms/Vectorize/SandboxVectorizer/Debug.h"
1718
#include "llvm/Transforms/Vectorize/SandboxVectorizer/VecUtils.h"
1819

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

172-
return CreateVectorInstr(Bndl, Operands);
173+
auto *NewI = CreateVectorInstr(Bndl, Operands);
174+
LLVM_DEBUG(dbgs() << DEBUG_PREFIX << "New instr: " << *NewI << "\n");
175+
return NewI;
173176
}
174177

175178
void BottomUpVec::tryEraseDeadInstrs() {
@@ -182,9 +185,11 @@ void BottomUpVec::tryEraseDeadInstrs() {
182185
[](Instruction *I1, Instruction *I2) { return I1->comesBefore(I2); });
183186
for (const auto &Pair : SortedDeadInstrCandidates) {
184187
for (Instruction *I : reverse(Pair.second)) {
185-
if (I->hasNUses(0))
188+
if (I->hasNUses(0)) {
186189
// Erase the dead instructions bottom-to-top.
190+
LLVM_DEBUG(dbgs() << DEBUG_PREFIX << "Erase dead: " << *I << "\n");
187191
I->eraseFromParent();
192+
}
188193
}
189194
}
190195
DeadInstrCandidates.clear();
@@ -277,8 +282,11 @@ Action *BottomUpVec::vectorizeRec(ArrayRef<Value *> Bndl,
277282
ArrayRef<Value *> UserBndl, unsigned Depth) {
278283
bool StopForDebug =
279284
DebugBndlCnt++ >= StopBundle && StopBundle != StopBundleDisabled;
285+
LLVM_DEBUG(dbgs() << DEBUG_PREFIX << "canVectorize() Bundle:\n";
286+
VecUtils::dump(Bndl));
280287
const auto &LegalityRes = StopForDebug ? Legality->getForcedPackForDebugging()
281288
: Legality->canVectorize(Bndl);
289+
LLVM_DEBUG(dbgs() << DEBUG_PREFIX << "Legality: " << LegalityRes << "\n");
282290
auto ActionPtr =
283291
std::make_unique<Action>(&LegalityRes, Bndl, UserBndl, Depth);
284292
SmallVector<Action *> Operands;
@@ -479,6 +487,8 @@ bool BottomUpVec::tryVectorize(ArrayRef<Value *> Bndl) {
479487
Actions.clear();
480488
DebugBndlCnt = 0;
481489
vectorizeRec(Bndl, {}, /*Depth=*/0);
490+
LLVM_DEBUG(dbgs() << DEBUG_PREFIX << "BottomUpVec: Vectorization Actions:\n";
491+
Actions.dump());
482492
emitVectors();
483493
tryEraseDeadInstrs();
484494
return Change;

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "llvm/Transforms/Vectorize/SandboxVectorizer/Passes/TransactionAcceptOrRevert.h"
1010
#include "llvm/Support/CommandLine.h"
1111
#include "llvm/Support/InstructionCost.h"
12+
#include "llvm/Transforms/Vectorize/SandboxVectorizer/Debug.h"
1213

1314
namespace llvm {
1415

@@ -20,15 +21,22 @@ namespace sandboxir {
2021

2122
bool TransactionAcceptOrRevert::runOnRegion(Region &Rgn, const Analyses &A) {
2223
const auto &SB = Rgn.getScoreboard();
24+
auto CostBefore = SB.getBeforeCost();
25+
auto CostAfter = SB.getAfterCost();
2326
InstructionCost CostAfterMinusBefore = SB.getAfterCost() - SB.getBeforeCost();
27+
LLVM_DEBUG(dbgs() << DEBUG_PREFIX << "Cost gain: " << CostAfterMinusBefore
28+
<< " (before/after/threshold: " << CostBefore << "/"
29+
<< CostAfter << "/" << CostThreshold << ")\n");
2430
// TODO: Print costs / write to remarks.
2531
auto &Tracker = Rgn.getContext().getTracker();
2632
if (CostAfterMinusBefore < -CostThreshold) {
2733
bool HasChanges = !Tracker.empty();
2834
Tracker.accept();
35+
LLVM_DEBUG(dbgs() << DEBUG_PREFIX << "*** Transaction Accept ***\n");
2936
return HasChanges;
3037
}
3138
// Revert the IR.
39+
LLVM_DEBUG(dbgs() << DEBUG_PREFIX << "*** Transaction Revert ***\n");
3240
Rgn.getContext().getTracker().revert();
3341
return false;
3442
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@
99
#include "llvm/Transforms/Vectorize/SandboxVectorizer/Passes/TransactionSave.h"
1010
#include "llvm/Support/CommandLine.h"
1111
#include "llvm/Support/InstructionCost.h"
12+
#include "llvm/Transforms/Vectorize/SandboxVectorizer/Debug.h"
1213

1314
namespace llvm::sandboxir {
1415

1516
bool TransactionSave::runOnRegion(Region &Rgn, const Analyses &A) {
17+
LLVM_DEBUG(dbgs() << DEBUG_PREFIX << "*** Save Transaction ***\n");
1618
Rgn.getContext().save();
1719
return false;
1820
}

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,12 @@
1111
#include "llvm/IR/Module.h"
1212
#include "llvm/SandboxIR/Constant.h"
1313
#include "llvm/Support/CommandLine.h"
14+
#include "llvm/Transforms/Vectorize/SandboxVectorizer/Debug.h"
1415
#include "llvm/Transforms/Vectorize/SandboxVectorizer/SandboxVectorizerPassBuilder.h"
1516
#include <regex>
1617

1718
using namespace llvm;
1819

19-
#define SV_NAME "sandbox-vectorizer"
20-
#define DEBUG_TYPE SV_NAME
21-
2220
static cl::opt<bool>
2321
PrintPassPipeline("sbvec-print-pass-pipeline", cl::init(false), cl::Hidden,
2422
cl::desc("Prints the pass pipeline and returns."));
@@ -119,13 +117,16 @@ bool SandboxVectorizerPass::runImpl(Function &LLVMF) {
119117

120118
// If the target claims to have no vector registers early return.
121119
if (!TTI->getNumberOfRegisters(TTI->getRegisterClassForType(true))) {
122-
LLVM_DEBUG(dbgs() << "SBVec: Target has no vector registers, return.\n");
120+
LLVM_DEBUG(dbgs() << DEBUG_PREFIX
121+
<< "Target has no vector registers, return.\n");
123122
return false;
124123
}
125-
LLVM_DEBUG(dbgs() << "SBVec: Analyzing " << LLVMF.getName() << ".\n");
124+
LLVM_DEBUG(dbgs() << DEBUG_PREFIX << "Analyzing " << LLVMF.getName()
125+
<< ".\n");
126126
// Early return if the attribute NoImplicitFloat is used.
127127
if (LLVMF.hasFnAttribute(Attribute::NoImplicitFloat)) {
128-
LLVM_DEBUG(dbgs() << "SBVec: NoImplicitFloat attribute, return.\n");
128+
LLVM_DEBUG(dbgs() << DEBUG_PREFIX
129+
<< "NoImplicitFloat attribute, return.\n");
129130
return false;
130131
}
131132

0 commit comments

Comments
 (0)