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#include "llvm/Transforms/Vectorize/SandboxVectorizer/Debug.h"
13
14namespace llvm {
15
16static cl::opt<int> CostThreshold("sbvec-cost-threshold", cl::init(Val: 0),
17 cl::Hidden,
18 cl::desc("Vectorization cost threshold."));
19
20namespace sandboxir {
21
22bool TransactionAcceptOrRevert::runOnRegion(Region &Rgn, const Analyses &A) {
23 const auto &SB = Rgn.getScoreboard();
24 [[maybe_unused]] auto CostBefore = SB.getBeforeCost();
25 [[maybe_unused]] auto CostAfter = SB.getAfterCost();
26 InstructionCost CostAfterMinusBefore = SB.getAfterCost() - SB.getBeforeCost();
27 LLVM_DEBUG(dbgs() << DEBUG_PREFIX << "Cost gain: " << CostAfterMinusBefore
28 << " (before/after/threshold: " << CostBefore << "/"
29 << CostAfter << "/" << CostThreshold << ")\n");
30 // TODO: Print costs / write to remarks.
31 auto &Tracker = Rgn.getContext().getTracker();
32 if (CostAfterMinusBefore < -CostThreshold) {
33 bool HasChanges = !Tracker.empty();
34 Tracker.accept();
35 LLVM_DEBUG(dbgs() << DEBUG_PREFIX << "*** Transaction Accept ***\n");
36 return HasChanges;
37 }
38 // Revert the IR.
39 LLVM_DEBUG(dbgs() << DEBUG_PREFIX << "*** Transaction Revert ***\n");
40 Rgn.getContext().getTracker().revert();
41 return false;
42}
43
44} // namespace sandboxir
45} // namespace llvm
46