LLVM 24.0.0git
BundleVec.h
Go to the documentation of this file.
1//===- BundleVec.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 vectorizer pass that forms bundles by walking the def-use chain bottom-up
10// or top-down, depending on the auxiliary pass argument.
11//
12
13#ifndef LLVM_TRANSFORMS_VECTORIZE_SANDBOXVECTORIZER_PASSES_BUNDLEVEC_H
14#define LLVM_TRANSFORMS_VECTORIZE_SANDBOXVECTORIZER_PASSES_BUNDLEVEC_H
15
16#include "llvm/ADT/ArrayRef.h"
17#include "llvm/ADT/StringRef.h"
19#include "llvm/SandboxIR/Pass.h"
25
26namespace llvm {
27struct VectorizeOptions;
28} // namespace llvm
29
30namespace llvm::sandboxir {
31
32/// This is a simple bottom-up (top-down) vectorizer Region pass.
33/// It expects a "seed slice" as an input in the Region's Aux vector.
34/// The "seed slice" is a vector of instructions that can be used as a starting
35/// point for vectorization, like stores (loads) to consecutive memory
36/// addresses. Starting from the seed instructions, it recursively walks up
37/// (down) the use-def (def-use) chains, forming a bundle of instructions per
38/// operand (user) position, for as long as these bundles can be vectorized.
39/// This pass will generate vector code if it can legally vectorize the code,
40/// regardless of whether it is profitable or not. For now profitability is
41/// checked at the end of the region pass pipeline by a dedicated pass that
42/// accepts or rejects the IR transaction, depending on the cost.
43class LLVM_ABI BundleVec final : public RegionPass {
44private:
45 const VectorizeOptions *Opts = nullptr;
46 /// Set to true whenever the pass modifies the IR.
47 bool Change = false;
48 AuxPassArgsRegistry ArgsRegistry;
49 AuxPassArg TopDownArg = ArgsRegistry.createArg("top-down");
50 AuxPassArg BottomUpArg = ArgsRegistry.createArg("bottom-up");
51 /// Direction for vectorization, set from the mandatory aux argument.
53 /// Maps scalars to vectors.
54 std::unique_ptr<InstrMaps> IMaps;
55 /// Counter used for force-stopping the vectorizer after this many
56 /// invocations. Used for debugging miscompiles.
57 unsigned long InvocationCnt = 0;
58
59 VecUtils::DeadInstructionMorgue DeadInstrMorgue;
60
61 /// Creates and returns a vector instruction that replaces the instructions in
62 /// \p Bndl. \p Operands are the already vectorized operands.
63 Value *createVectorInstr(BndlRef<Value *> Bndl, BndlRef<Value *> Operands);
64
65 /// Creates a shuffle instruction that shuffles \p VecOp according to \p Mask.
66 /// \p UserBB is the block of the user bundle.
67 Value *createShuffle(Value *VecOp, const ShuffleMask &Mask,
68 BasicBlock *UserBB);
69 /// Packs all elements of \p ToPack into a vector and returns that vector. \p
70 /// UserBB is the block of the user bundle.
71 Value *createPack(BndlRef<Value *> ToPack, BasicBlock *UserBB);
72
73 /// Helper class describing how(if) to vectorize the code.
74 class ActionsVector {
75 private:
77
78 public:
79 auto begin() const { return Actions.begin(); }
80 auto end() const { return Actions.end(); }
81 void push_back(std::unique_ptr<Action> &&ActPtr) {
82 ActPtr->Idx = Actions.size();
83 Actions.push_back(std::move(ActPtr));
84 }
85 void clear() { Actions.clear(); }
86#ifndef NDEBUG
87 void print(raw_ostream &OS) const;
88 void dump() const;
89#endif // NDEBUG
90 };
91 ActionsVector Actions;
92 /// Helper counter for debugging. It counts the bundles that we attempt to
93 /// vectorize in vectorizeRec().
94 unsigned DebugBndlCnt = 0;
95
96 /// Recursively try to vectorize \p Bndl. \p UserBndl identifies the
97 /// users that this recursive call originates from.
98 Action *vectorizeRec(BndlRef<Value *> Bndl, BndlRef<Value *> UserBndl,
99 unsigned Depth, LegalityAnalysis &Legality);
100 /// If the values in \p Bndl have external users, then emit unpacks and
101 /// connect them to the users. \p Vec is the vectorized form of \p Bndl.
102 void emitUnpacksForExternalUses(BndlRef<Value *> Bndl, Value *Vec);
103 /// Generate vector instructions based on `Actions` and return the last vector
104 /// created.
105 Value *emitVectors();
106 /// Entry point for vectorization starting from \p Seeds.
107 bool tryVectorize(BndlRef<Value *> Seeds, LegalityAnalysis &Legality);
108
109public:
110 BundleVec(StringRef AuxArg) : RegionPass("bundle-vec") {
111 ArgsRegistry.parse(AuxArg);
112 if (BottomUpArg) {
114 } else if (TopDownArg) {
116 } else {
117 std::string ErrStr;
118 raw_string_ostream ErrSS(ErrStr);
119 ErrSS << "bundle-vec requires either '" << BottomUpArg.getFlagStr()
120 << "' or '" << TopDownArg.getFlagStr()
121 << "' as its aux argument!\n";
122 reportFatalUsageError(ErrStr.c_str());
123 }
124 }
125
126 bool runOnRegion(Region &Rgn, const Analyses &A) final;
127};
128
129} // namespace llvm::sandboxir
130
131#endif // LLVM_TRANSFORMS_VECTORIZE_SANDBOXVECTORIZER_PASSES_BUNDLEVEC_H
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
#define LLVM_ABI
Definition Compiler.h:211
SI Fold Operands
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
A raw_ostream that writes to an std::string.
This represents an auxiliary pass argument.
Definition Pass.h:96
A registry for the auxiliary pass argument arguments.
Definition Pass.h:121
An ArrayRef of Values or Instructions that we can print/dump for debugging.
Definition VecUtils.h:39
BundleVec(StringRef AuxArg)
Definition BundleVec.h:110
Performs the legality analysis and returns a LegalityResult object.
Definition Legality.h:322
virtual LLVM_DUMP_METHOD void dump() const
RegionPass(StringRef Name)
Name can't contain any spaces or start with '-'.
Definition Pass.h:88
A SandboxIR Value has users. This is the base class.
Definition Value.h:72
Utility class to collect and erase dead instructions.
Definition VecUtils.h:502
BasicBlock(llvm::BasicBlock *BB, Context &SBCtx)
Definition BasicBlock.h:75
iterator end() const
Definition BasicBlock.h:89
LLVM_ABI iterator begin() const
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI void reportFatalUsageError(Error Err)
Report a fatal error that does not indicate a bug in LLVM.
Definition Error.cpp:177