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"
16
17namespace llvm::sandboxir {
18
20 "sbvec-max-users-to-consider", cl::init(16), cl::Hidden,
21 cl::desc("Limit the number of a seed's users that getNextUserBundles() "
22 "will examine as candidates for a matching bundle, to cap "
23 "compilation time."));
24
27 for (unsigned Idx : seq<unsigned>(U->getNumOperands()))
28 if (U->getOperand(Idx) == Op)
29 OpIdxVec.push_back(Idx);
30 return OpIdxVec;
31}
32
33static std::optional<BundleTy>
35 Instruction *SeedUserInst,
37 SmallVector<unsigned, 2> OpIdxVec0 =
38 getOperandIndicesInUser(SeedUserInst, Seed);
39 assert(!OpIdxVec0.empty() && "U0 does not use Seed!");
40 BundleTy NextUserBndl;
41 NextUserBndl.push_back(SeedUserInst);
42 Claimed.insert(SeedUserInst);
43 for (Value *V : drop_begin(Bndl)) {
44 Instruction *Match = nullptr;
45 for (User *U : V->users()) {
46 auto *UI = dyn_cast<Instruction>(U);
47 if (!UI || IMaps.isVectorized(UI) || Claimed.contains(UI) ||
48 UI->getOpcode() != SeedUserInst->getOpcode() ||
49 UI->getType() != SeedUserInst->getType() ||
50 UI->getParent() != SeedUserInst->getParent() ||
51 getOperandIndicesInUser(UI, V) != OpIdxVec0)
52 continue;
53
54 Match = UI;
55 break;
56 }
57 if (!Match)
58 return std::nullopt;
59 NextUserBndl.push_back(Match);
60 }
61
62 for (auto *I : NextUserBndl)
63 Claimed.insert(cast<Instruction>(I));
64 return NextUserBndl;
65}
66
71 if (Bndl.empty())
72 return Bundles;
73
74 Value *V0 = Bndl[0];
75 DenseSet<User *> SeenUsers;
76 // For each user U0 of lane 0, try to form a bundle of matching users across
77 // all lanes. Cap the number of users considered to bound compilation time,
78 // since each one may trigger an O(Bndl.size()) search across the other
79 // lanes' users.
80 for (User *U0 : V0->users()) {
81 if (SeenUsers.size() >= MaxUsersToConsider)
82 break;
83 if (!SeenUsers.insert(U0).second)
84 continue;
85 auto *UI0 = dyn_cast<Instruction>(U0);
86 if (!UI0 || IMaps.isVectorized(UI0) || Claimed.contains(UI0))
87 continue;
88 std::optional<BundleTy> NextUserBndl =
89 getMatchingBundle(Bndl, IMaps, V0, UI0, Claimed);
90 if (NextUserBndl)
91 Bundles.emplace_back(std::move(*NextUserBndl));
92 }
93 return Bundles;
94}
95
96unsigned VecUtils::getFloorPowerOf2(unsigned Num) {
97 if (Num == 0)
98 return Num;
99 unsigned Mask = Num;
100 Mask >>= 1;
101 for (unsigned ShiftBy = 1; ShiftBy < sizeof(Num) * 8; ShiftBy <<= 1)
102 Mask |= Mask >> ShiftBy;
103 return Num & ~Mask;
104}
105
106#ifndef NDEBUG
107template <typename T> static void dumpImpl(ArrayRef<T *> Bndl) {
108 for (auto [Idx, V] : enumerate(Bndl))
109 dbgs() << Idx << "." << *V << "\n";
110}
113#endif // NDEBUG
114
115} // 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.
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
static LLVM_DUMP_METHOD void dump(ArrayRef< Value * > Bndl)
Helper dump function for debugging.
Definition VecUtils.cpp:111
static LLVM_ABI unsigned getFloorPowerOf2(unsigned Num)
\Returns the first integer power of 2 that is <= Num.
Definition VecUtils.cpp:96
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:68
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:25
static void dumpImpl(ArrayRef< T * > Bndl)
Definition VecUtils.cpp:107
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:34
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
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