Skip to content

Commit 69b8cf4

Browse files
authored
[SandboxVec][BottomUpVec] Add cost estimation and tr-accept-or-revert pass (llvm#126325)
The TransactionAcceptOrRevert pass is the final pass in the Sandbox Vectorizer's default pass pipeline. It's job is to check the cost before/after vectorization and accept or revert the IR to its original state. Since we are now starting the transaction in BottomUpVec, tests that run a custom pipeline need to accept the transaction. This is done with the help of the TransactionAlwaysAccept pass (tr-accept).
1 parent 2feced1 commit 69b8cf4

File tree

22 files changed

+249
-18
lines changed

22 files changed

+249
-18
lines changed

llvm/include/llvm/SandboxIR/Tracker.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -440,8 +440,9 @@ class ShuffleVectorSetMask final : public IRChangeBase {
440440
class Tracker {
441441
public:
442442
enum class TrackerState {
443-
Disabled, ///> Tracking is disabled
444-
Record, ///> Tracking changes
443+
Disabled, ///> Tracking is disabled
444+
Record, ///> Tracking changes
445+
Reverting, ///> Reverting changes
445446
};
446447

447448
private:
@@ -473,6 +474,8 @@ class Tracker {
473474

474475
~Tracker();
475476
Context &getContext() const { return Ctx; }
477+
/// \Returns true if there are no changes tracked.
478+
bool empty() const { return Changes.empty(); }
476479
/// Record \p Change and take ownership. This is the main function used to
477480
/// track Sandbox IR changes.
478481
void track(std::unique_ptr<IRChangeBase> &&Change) {
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//===- TransactionAcceptOrRevert.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+
// This is a region pass that checks the region cost before/after vectorization
10+
// and accepts the state of Sandbox IR if the cost is better, or otherwise
11+
// reverts it.
12+
//
13+
14+
#ifndef LLVM_TRANSFORMS_VECTORIZE_SANDBOXVECTORIZER_PASSES_TRANSACTIONACCEPTORREVERT_H
15+
#define LLVM_TRANSFORMS_VECTORIZE_SANDBOXVECTORIZER_PASSES_TRANSACTIONACCEPTORREVERT_H
16+
17+
#include "llvm/SandboxIR/Pass.h"
18+
#include "llvm/SandboxIR/Region.h"
19+
20+
namespace llvm::sandboxir {
21+
22+
class TransactionAcceptOrRevert : public RegionPass {
23+
public:
24+
TransactionAcceptOrRevert() : RegionPass("tr-accept-or-revert") {}
25+
bool runOnRegion(Region &Rgn, const Analyses &A) final;
26+
};
27+
28+
} // namespace llvm::sandboxir
29+
30+
#endif // LLVM_TRANSFORMS_VECTORIZE_SANDBOXVECTORIZER_PASSES_TRANSACTIONACCEPTORREVERT_H
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//===- TransactionAlwaysAccept.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+
// This is a region pass that always accepts the transaction without checking
10+
// its cost. This is mainly used as a final pass in lit tests.
11+
//
12+
13+
#ifndef LLVM_TRANSFORMS_VECTORIZE_SANDBOXVECTORIZER_PASSES_TRANSACTIONALWAYSACCEPT_H
14+
#define LLVM_TRANSFORMS_VECTORIZE_SANDBOXVECTORIZER_PASSES_TRANSACTIONALWAYSACCEPT_H
15+
16+
#include "llvm/SandboxIR/Pass.h"
17+
#include "llvm/SandboxIR/Region.h"
18+
19+
namespace llvm::sandboxir {
20+
21+
class TransactionAlwaysAccept : public RegionPass {
22+
public:
23+
TransactionAlwaysAccept() : RegionPass("tr-accept") {}
24+
bool runOnRegion(Region &Rgn, const Analyses &A) final {
25+
auto &Tracker = Rgn.getContext().getTracker();
26+
bool HasChanges = !Tracker.empty();
27+
Tracker.accept();
28+
return HasChanges;
29+
}
30+
};
31+
32+
} // namespace llvm::sandboxir
33+
34+
#endif // LLVM_TRANSFORMS_VECTORIZE_SANDBOXVECTORIZER_PASSES_TRANSACTIONALWAYSACCEPT_H

llvm/lib/SandboxIR/Tracker.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,13 +347,14 @@ void Tracker::save() {
347347

348348
void Tracker::revert() {
349349
assert(State == TrackerState::Record && "Forgot to save()!");
350-
State = TrackerState::Disabled;
350+
State = TrackerState::Reverting;
351351
for (auto &Change : reverse(Changes))
352352
Change->revert(*this);
353353
Changes.clear();
354354
#if !defined(NDEBUG) && defined(EXPENSIVE_CHECKS)
355355
SnapshotChecker.expectNoDiff();
356356
#endif
357+
State = TrackerState::Disabled;
357358
}
358359

359360
void Tracker::accept() {

llvm/lib/Transforms/Vectorize/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ add_llvm_component_library(LLVMVectorize
99
SandboxVectorizer/Legality.cpp
1010
SandboxVectorizer/Passes/BottomUpVec.cpp
1111
SandboxVectorizer/Passes/RegionsFromMetadata.cpp
12+
SandboxVectorizer/Passes/TransactionAcceptOrRevert.cpp
1213
SandboxVectorizer/SandboxVectorizer.cpp
1314
SandboxVectorizer/SandboxVectorizerPassBuilder.cpp
1415
SandboxVectorizer/Scheduler.cpp

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,9 @@ MemDGNode *DependencyGraph::getMemDGNodeAfter(DGNode *N, bool IncludingN,
368368
}
369369

370370
void DependencyGraph::notifyCreateInstr(Instruction *I) {
371+
if (Ctx->getTracker().getState() == Tracker::TrackerState::Reverting)
372+
// We don't maintain the DAG while reverting.
373+
return;
371374
// Nothing to do if the node is not in the focus range of the DAG.
372375
if (!(DAGInterval.contains(I) || DAGInterval.touches(I)))
373376
return;
@@ -405,6 +408,9 @@ void DependencyGraph::notifyCreateInstr(Instruction *I) {
405408
}
406409

407410
void DependencyGraph::notifyMoveInstr(Instruction *I, const BBIterator &To) {
411+
if (Ctx->getTracker().getState() == Tracker::TrackerState::Reverting)
412+
// We don't maintain the DAG while reverting.
413+
return;
408414
// NOTE: This function runs before `I` moves to its new destination.
409415
BasicBlock *BB = To.getNodeParent();
410416
assert(!(To != BB->end() && &*To == I->getNextNode()) &&
@@ -472,6 +478,9 @@ void DependencyGraph::notifyMoveInstr(Instruction *I, const BBIterator &To) {
472478
}
473479

474480
void DependencyGraph::notifyEraseInstr(Instruction *I) {
481+
if (Ctx->getTracker().getState() == Tracker::TrackerState::Reverting)
482+
// We don't maintain the DAG while reverting.
483+
return;
475484
// Update the MemDGNode chain if this is a memory node.
476485
if (auto *MemN = dyn_cast_or_null<MemDGNode>(getNodeOrNull(I))) {
477486
auto *PrevMemN = getMemDGNodeBefore(MemN, /*IncludingN=*/false);

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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "llvm/SandboxIR/Function.h"
1313
#include "llvm/SandboxIR/Instruction.h"
1414
#include "llvm/SandboxIR/Module.h"
15+
#include "llvm/SandboxIR/Region.h"
1516
#include "llvm/SandboxIR/Utils.h"
1617
#include "llvm/Transforms/Vectorize/SandboxVectorizer/SandboxVectorizerPassBuilder.h"
1718
#include "llvm/Transforms/Vectorize/SandboxVectorizer/SeedCollector.h"
@@ -448,13 +449,24 @@ bool BottomUpVec::runOnFunction(Function &F, const Analyses &A) {
448449

449450
assert(SeedSlice.size() >= 2 && "Should have been rejected!");
450451

451-
// TODO: If vectorization succeeds, run the RegionPassManager on the
452-
// resulting region.
453-
454452
// TODO: Refactor to remove the unnecessary copy to SeedSliceVals.
455453
SmallVector<Value *> SeedSliceVals(SeedSlice.begin(),
456454
SeedSlice.end());
457-
Change |= tryVectorize(SeedSliceVals);
455+
// Create an empty region. Instructions get added to the region
456+
// automatically by the callbacks.
457+
auto &Ctx = F.getContext();
458+
Region Rgn(Ctx, A.getTTI());
459+
// Save the state of the IR before we make any changes. The
460+
// transaction gets accepted/reverted by the tr-accept-or-revert pass.
461+
Ctx.save();
462+
// Try to vectorize starting from the seed slice. The returned value
463+
// is true if we found vectorizable code and generated some vector
464+
// code for it. It does not mean that the code is profitable.
465+
bool VecSuccess = tryVectorize(SeedSliceVals);
466+
if (VecSuccess)
467+
// WARNING: All passes should return false, except those that
468+
// accept/revert the state.
469+
Change |= RPM.runOnRegion(Rgn, A);
458470
}
459471
}
460472
}

llvm/lib/Transforms/Vectorize/SandboxVectorizer/Passes/PassRegistry.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
REGION_PASS("null", ::llvm::sandboxir::NullPass)
2121
REGION_PASS("print-instruction-count", ::llvm::sandboxir::PrintInstructionCount)
22+
REGION_PASS("tr-accept", ::llvm::sandboxir::TransactionAlwaysAccept)
23+
REGION_PASS("tr-accept-or-revert", ::llvm::sandboxir::TransactionAcceptOrRevert)
2224

2325
#undef REGION_PASS
2426

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//===- TransactionAcceptOrRevert.cpp - Check cost and accept/revert region ===//
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+
#include "llvm/Transforms/Vectorize/SandboxVectorizer/Passes/TransactionAcceptOrRevert.h"
10+
#include "llvm/Support/CommandLine.h"
11+
#include "llvm/Support/InstructionCost.h"
12+
13+
namespace llvm {
14+
15+
static cl::opt<int> CostThreshold("sbvec-cost-threshold", cl::init(0),
16+
cl::Hidden,
17+
cl::desc("Vectorization cost threshold."));
18+
19+
namespace sandboxir {
20+
21+
bool TransactionAcceptOrRevert::runOnRegion(Region &Rgn, const Analyses &A) {
22+
const auto &SB = Rgn.getScoreboard();
23+
InstructionCost CostAfterMinusBefore = SB.getAfterCost() - SB.getBeforeCost();
24+
// TODO: Print costs / write to remarks.
25+
auto &Tracker = Rgn.getContext().getTracker();
26+
if (CostAfterMinusBefore < -CostThreshold) {
27+
bool HasChanges = !Tracker.empty();
28+
Tracker.accept();
29+
return HasChanges;
30+
}
31+
// Revert the IR.
32+
Rgn.getContext().getTracker().revert();
33+
return false;
34+
}
35+
36+
} // namespace sandboxir
37+
} // namespace llvm

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,10 @@ static cl::opt<std::string> UserDefinedPassPipeline(
3131

3232
SandboxVectorizerPass::SandboxVectorizerPass() : FPM("fpm") {
3333
if (UserDefinedPassPipeline == DefaultPipelineMagicStr) {
34-
// TODO: Add region passes to the default pipeline.
34+
// TODO: Add passes to the default pipeline. It currently contains:
35+
// - the bottom-up-vectorizer pass
3536
FPM.setPassPipeline(
36-
"bottom-up-vec<>",
37+
"bottom-up-vec<tr-accept-or-revert>",
3738
sandboxir::SandboxVectorizerPassBuilder::createFunctionPass);
3839
} else {
3940
// Create the user-defined pipeline.

0 commit comments

Comments
 (0)