LLVM 24.0.0git
VecUtils.cpp
Go to the documentation of this file.
1//===- VecUtils.cpp -------------------------------------------------------===//
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
10
11#include "llvm/ADT/Sequence.h"
17
18namespace llvm::sandboxir {
19
21 "sbvec-max-users-to-consider", cl::init(16), cl::Hidden,
22 cl::desc("Limit the number of a seed's users that getNextUserBundles() "
23 "will examine as candidates for a matching bundle, to cap "
24 "compilation time."));
25
28 for (unsigned Idx : seq<unsigned>(U->getNumOperands()))
29 if (U->getOperand(Idx) == Op)
30 OpIdxVec.push_back(Idx);
31 return OpIdxVec;
32}
33
34static std::optional<BundleTy>
36 Instruction *SeedUserInst,
38 SmallVector<unsigned, 2> OpIdxVec0 =
39 getOperandIndicesInUser(SeedUserInst, Seed);
40 assert(!OpIdxVec0.empty() && "U0 does not use Seed!");
41 BundleTy NextUserBndl;
42 NextUserBndl.push_back(SeedUserInst);
43 Claimed.insert(SeedUserInst);
44 for (Value *V : drop_begin(Bndl)) {
45 Instruction *Match = nullptr;
46 for (User *U : V->users()) {
47 auto *UI = dyn_cast<Instruction>(U);
48 if (!UI || IMaps.isVectorized(UI) || Claimed.contains(UI) ||
49 UI->getOpcode() != SeedUserInst->getOpcode() ||
50 UI->getType() != SeedUserInst->getType() ||
51 UI->getParent() != SeedUserInst->getParent() ||
52 getOperandIndicesInUser(UI, V) != OpIdxVec0)
53 continue;
54
55 Match = UI;
56 break;
57 }
58 if (!Match)
59 return std::nullopt;
60 NextUserBndl.push_back(Match);
61 }
62
63 for (auto *I : NextUserBndl)
64 Claimed.insert(cast<Instruction>(I));
65 return NextUserBndl;
66}
67
72 if (Bndl.empty())
73 return Bundles;
74
75 Value *V0 = Bndl[0];
76 DenseSet<User *> SeenUsers;
77 // For each user U0 of lane 0, try to form a bundle of matching users across
78 // all lanes. Cap the number of users considered to bound compilation time,
79 // since each one may trigger an O(Bndl.size()) search across the other
80 // lanes' users.
81 for (User *U0 : V0->users()) {
82 if (SeenUsers.size() >= MaxUsersToConsider)
83 break;
84 if (!SeenUsers.insert(U0).second)
85 continue;
86 auto *UI0 = dyn_cast<Instruction>(U0);
87 if (!UI0 || IMaps.isVectorized(UI0) || Claimed.contains(UI0))
88 continue;
89 std::optional<BundleTy> NextUserBndl =
90 getMatchingBundle(Bndl, IMaps, V0, UI0, Claimed);
91 if (NextUserBndl)
92 Bundles.emplace_back(std::move(*NextUserBndl));
93 }
94 return Bundles;
95}
96
97unsigned VecUtils::getFloorPowerOf2(unsigned Num) {
98 if (Num == 0)
99 return Num;
100 unsigned Mask = Num;
101 Mask >>= 1;
102 for (unsigned ShiftBy = 1; ShiftBy < sizeof(Num) * 8; ShiftBy <<= 1)
103 Mask |= Mask >> ShiftBy;
104 return Num & ~Mask;
105}
106
108 ArrayRef<Value *> Bndl) {
109 for (Value *V : Bndl)
110 DeadInstrCandidates.insert(cast<Instruction>(V));
111 // Also collect the GEPs of vectorized loads and stores.
112 auto Opcode = cast<Instruction>(Bndl[0])->getOpcode();
113 switch (Opcode) {
114 case Instruction::Opcode::Load: {
115 for (Value *V : drop_begin(Bndl))
116 if (auto *Ptr =
118 DeadInstrCandidates.insert(Ptr);
119 break;
120 }
121 case Instruction::Opcode::Store: {
122 for (Value *V : drop_begin(Bndl))
123 if (auto *Ptr =
125 DeadInstrCandidates.insert(Ptr);
126 break;
127 }
128 default:
129 break;
130 }
131}
132
134 DenseMap<BasicBlock *, SmallVector<Instruction *>> SortedDeadInstrCandidates;
135 // The dead instrs could span BBs, so we need to collect and sort them per BB.
136 for (auto *V : DeadInstrCandidates) {
137 auto *DeadI = cast<Instruction>(V);
138 SortedDeadInstrCandidates[DeadI->getParent()].push_back(DeadI);
139 }
140 for (auto &Pair : SortedDeadInstrCandidates)
141 sort(Pair.second,
142 [](Instruction *I1, Instruction *I2) { return I1->comesBefore(I2); });
143 for (const auto &Pair : SortedDeadInstrCandidates) {
144 for (Instruction *I : reverse(Pair.second)) {
145 if (I->hasNUses(0)) {
146 // Erase the dead instructions bottom-to-top.
147 LLVM_DEBUG(dbgs() << DEBUG_PREFIX << "Erase dead: " << *I << "\n");
148 I->eraseFromParent();
149 }
150 }
151 }
152 DeadInstrCandidates.clear();
153}
154
155#ifndef NDEBUG
156template <typename T> static void dumpImpl(ArrayRef<T *> Bndl) {
157 for (auto [Idx, V] : enumerate(Bndl))
158 dbgs() << Idx << "." << *V << "\n";
159}
162#endif // NDEBUG
163
164} // namespace llvm::sandboxir
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define I(x, y, z)
Definition MD5.cpp:57
static ManagedStatic< cl::opt< uint64_t >, CreateSeed > Seed
Provides some synthesis utilities to produce sequences of values.
This file defines the SmallPtrSet class.
#define LLVM_DEBUG(...)
Definition Debug.h:119
#define DEBUG_PREFIX
Definition Debug.h:19
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
bool empty() const
Check if the array is empty.
Definition ArrayRef.h:136
Implements a dense probed hash-table based set.
Definition DenseSet.h:281
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
bool contains(ConstPtrType Ptr) const
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
reference emplace_back(ArgTypes &&... Args)
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
std::pair< iterator, bool > insert(const ValueT &V)
Definition DenseSet.h:209
size_type size() const
Definition DenseSet.h:84
Maps the original instructions to the vectorized instrs and the reverse.
Definition InstrMaps.h:50
bool isVectorized(Value *Orig) const
\Returns true if Orig was vectorized
Definition InstrMaps.h:65
A sandboxir::User with operands, opcode and linked with previous/next instructions in an instruction ...
Definition Instruction.h:43
Opcode getOpcode() const
\Returns this Instruction's opcode.
LLVM_ABI BasicBlock * getParent() const
\Returns the BasicBlock containing this Instruction, or null if it is detached.
A sandboxir::User has operands.
Definition User.h:59
A SandboxIR Value has users. This is the base class.
Definition Value.h:72
LLVM_ABI Type * getType() const
Definition Value.cpp:46
iterator_range< user_iterator > users()
Definition Value.h:253
void tryEraseDeadInstrs()
Erase candidates recorded by collectPotentiallyDeadInstrs() that now have no uses,...
Definition VecUtils.cpp:133
void collectPotentiallyDeadInstrs(ArrayRef< Value * > Bndl)
Record instructions in Bndl that may be dead after vectorization.
Definition VecUtils.cpp:107
static LLVM_DUMP_METHOD void dump(ArrayRef< Value * > Bndl)
Helper dump function for debugging.
Definition VecUtils.cpp:160
static LLVM_ABI unsigned getFloorPowerOf2(unsigned Num)
\Returns the first integer power of 2 that is <= Num.
Definition VecUtils.cpp:97
static LLVM_ABI SmallVector< BundleTy > getNextUserBundles(ArrayRef< Value * > Bndl, const InstrMaps &IMaps, SmallPtrSet< Instruction *, 4 > &Claimed)
For each user of lane 0 in Bndl, try to form a bundle of matching users for all lanes.
Definition VecUtils.cpp:69
initializer< Ty > init(const Ty &Val)
static cl::opt< unsigned > MaxUsersToConsider("sbvec-max-users-to-consider", cl::init(16), cl::Hidden, cl::desc("Limit the number of a seed's users that getNextUserBundles() " "will examine as candidates for a matching bundle, to cap " "compilation time."))
static SmallVector< unsigned, 2 > getOperandIndicesInUser(User *U, Value *Op)
Definition VecUtils.cpp:26
static void dumpImpl(ArrayRef< T * > Bndl)
Definition VecUtils.cpp:156
SmallVector< Value *, 4 > BundleTy
Definition VecUtils.h:38
static std::optional< BundleTy > getMatchingBundle(ArrayRef< Value * > Bndl, const InstrMaps &IMaps, Value *Seed, Instruction *SeedUserInst, SmallPtrSet< Instruction *, 4 > &Claimed)
Definition VecUtils.cpp:35
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
Definition STLExtras.h:315
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
Definition STLExtras.h:2554
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
const Value * getPointerOperand(const Value *V)
A helper function that returns the pointer operand of a load, store or GEP instruction.
auto reverse(ContainerTy &&C)
Definition STLExtras.h:407
void sort(IteratorTy Start, IteratorTy End)
Definition STLExtras.h:1636
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
DWARFExpression::Operation Op
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
constexpr auto seq(T Begin, T End)
Iterate over an integral type from Begin up to - but not including - End.
Definition Sequence.h:341