LLVM 23.0.0git
RegionWithScore.h
Go to the documentation of this file.
1//===- RegionWithScore.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// A region with score tracking for added/removed instructions.
10//
11
12#ifndef LLVM_SANDBOXIR_REGIONWITHSCORE_H
13#define LLVM_SANDBOXIR_REGIONWITHSCORE_H
14
17
18namespace llvm::sandboxir {
19
20/// Vectorization Score (cost) tracking class.
21class ScoreBoard {
22 const Region &Rgn;
23 const TargetTransformInfo &TTI;
24 constexpr static TTI::TargetCostKind CostKind = TTI::TCK_RecipThroughput;
25 /// The cost of all instructions added to the region.
26 InstructionCost AfterCost = 0;
27 /// The cost of all instructions that got removed and replaced by new ones.
28 InstructionCost BeforeCost = 0;
29 /// Helper for both add() and remove(). \Returns the TTI cost of \p I.
30 LLVM_ABI InstructionCost getCost(Instruction *I) const;
31 /// No need to allow copies.
32 ScoreBoard(const ScoreBoard &) = delete;
33 const ScoreBoard &operator=(const ScoreBoard &) = delete;
34
35public:
37 : Rgn(Rgn), TTI(TTI) {}
38 /// Mark \p I as a newly added instruction to the region.
39 void add(Instruction *I) { AfterCost += getCost(I); }
40 /// Mark \p I as a deleted instruction from the region.
42 /// \Returns the cost of the newly added instructions.
43 InstructionCost getAfterCost() const { return AfterCost; }
44 /// \Returns the cost of the Removed instructions.
45 InstructionCost getBeforeCost() const { return BeforeCost; }
46
47#ifndef NDEBUG
48 void dump(raw_ostream &OS) const {
49 OS << "BeforeCost: " << BeforeCost << "\n";
50 OS << "AfterCost: " << AfterCost << "\n";
51 }
52 LLVM_DUMP_METHOD void dump() const;
53#endif // NDEBUG
54};
55
56/// A Region class that tracks its instructions score.
57class RegionWithScore final : public Region {
58 /// Keeps track of cost of instructions added and removed.
59 ScoreBoard Scoreboard;
60
61 void add(Instruction *I) override {
62 addRaw(I);
63 // Keep track of the instruction cost.
64 Scoreboard.add(I);
65 }
66 friend class RegionsFromBBs; // For add().
67
68 void remove(Instruction *I) override {
69 // Keep track of the instruction cost. This need to be done *before* we
70 // remove `I` from the region.
71 Scoreboard.remove(I);
73 }
74
75public:
79 : Region(std::move(Rgn)), Scoreboard(*this, TTI) {}
80 // For isa<> cast<> etc.
81 static bool classof(const Region *From) {
83 }
84
85 /// \Returns the ScoreBoard data structure that keeps track of instr costs.
86 const ScoreBoard &getScoreboard() const { return Scoreboard; }
87
90};
91
92} // namespace llvm::sandboxir
93
94#endif // LLVM_SANDBOXIR_REGIONWITHSCORE_H
#define LLVM_ABI
Definition Compiler.h:213
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition Compiler.h:661
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
This pass exposes codegen information to IR-level passes.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
TargetCostKind
The kind of cost model.
@ TCK_RecipThroughput
Reciprocal throughput.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
A sandboxir::User with operands, opcode and linked with previous/next instructions in an instruction ...
Definition Instruction.h:43
static bool classof(const Region *From)
RegionWithScore(Context &Ctx, const TargetTransformInfo &TTI)
const ScoreBoard & getScoreboard() const
\Returns the ScoreBoard data structure that keeps track of instr costs.
static SmallVector< std::unique_ptr< RegionWithScore > > createRegionsFromMD(Function &F, const TargetTransformInfo &TTI)
RegionWithScore(Region &&Rgn, const TargetTransformInfo &TTI)
RegionClassID getSubclassID() const
Definition Region.h:189
void addRaw(Instruction *I)
Adds I to the set.
Definition Region.h:97
virtual LLVM_ABI void remove(Instruction *I)
Removes I from the set.
Definition Region.cpp:81
friend class Context
Definition Region.h:107
LLVM_ABI Region(Context &Ctx, RegionClassID ID)
Definition Region.cpp:13
Vectorization Score (cost) tracking class.
InstructionCost getBeforeCost() const
\Returns the cost of the Removed instructions.
LLVM_ABI void remove(Instruction *I)
Mark I as a deleted instruction from the region.
InstructionCost getAfterCost() const
\Returns the cost of the newly added instructions.
LLVM_DUMP_METHOD void dump() const
void dump(raw_ostream &OS) const
ScoreBoard(Region &Rgn, const TargetTransformInfo &TTI)
void add(Instruction *I)
Mark I as a newly added instruction to the region.
RegionClassID
The main job of the Region is to point to new instructions generated by vectorization passes.
Definition Region.h:61
@ RegionWithScoreID
Definition Region.h:63
TargetTransformInfo TTI
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1916
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:874