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::sandboxir {
27
28/// This is a simple bottom-up (top-down) vectorizer Region pass.
29/// It expects a "seed slice" as an input in the Region's Aux vector.
30/// The "seed slice" is a vector of instructions that can be used as a starting
31/// point for vectorization, like stores (loads) to consecutive memory
32/// addresses. Starting from the seed instructions, it recursively walks up
33/// (down) the use-def (def-use) chains, forming a bundle of instructions per
34/// operand (user) position, for as long as these bundles can be vectorized.
35/// This pass will generate vector code if it can legally vectorize the code,
36/// regardless of whether it is profitable or not. For now profitability is
37/// checked at the end of the region pass pipeline by a dedicated pass that
38/// accepts or rejects the IR transaction, depending on the cost.
39class LLVM_ABI BundleVec final : public RegionPass {
40private:
41 /// Set to true whenever the pass modifies the IR.
42 bool Change = false;
43 AuxPassArgsRegistry ArgsRegistry;
44 AuxPassArg TopDownArg = ArgsRegistry.createArg("top-down");
45 AuxPassArg BottomUpArg = ArgsRegistry.createArg("bottom-up");
46 /// Direction for vectorization, set from the mandatory aux argument.
48 /// Maps scalars to vectors.
49 std::unique_ptr<InstrMaps> IMaps;
50 /// Counter used for force-stopping the vectorizer after this many
51 /// invocations. Used for debugging miscompiles.
52 unsigned long InvocationCnt = 0;
53
54 VecUtils::DeadInstructionMorgue DeadInstrMorgue;
55
56 /// Creates and returns a vector instruction that replaces the instructions in
57 /// \p Bndl. \p Operands are the already vectorized operands.
58 Value *createVectorInstr(BndlRef<Value *> Bndl, BndlRef<Value *> Operands);
59
60 /// Creates a shuffle instruction that shuffles \p VecOp according to \p Mask.
61 /// \p UserBB is the block of the user bundle.
62 Value *createShuffle(Value *VecOp, const ShuffleMask &Mask,
63 BasicBlock *UserBB);
64 /// Packs all elements of \p ToPack into a vector and returns that vector. \p
65 /// UserBB is the block of the user bundle.
66 Value *createPack(BndlRef<Value *> ToPack, BasicBlock *UserBB);
67
68 /// Helper class describing how(if) to vectorize the code.
69 class ActionsVector {
70 private:
72
73 public:
74 auto begin() const { return Actions.begin(); }
75 auto end() const { return Actions.end(); }
76 void push_back(std::unique_ptr<Action> &&ActPtr) {
77 ActPtr->Idx = Actions.size();
78 Actions.push_back(std::move(ActPtr));
79 }
80 void clear() { Actions.clear(); }
81#ifndef NDEBUG
82 void print(raw_ostream &OS) const;
83 void dump() const;
84#endif // NDEBUG
85 };
86 ActionsVector Actions;
87 /// Helper counter for debugging. It counts the bundles that we attempt to
88 /// vectorize in vectorizeRec().
89 unsigned DebugBndlCnt = 0;
90
91 /// Recursively try to vectorize \p Bndl. \p UserBndl identifies the
92 /// users that this recursive call originates from.
93 Action *vectorizeRec(BndlRef<Value *> Bndl, BndlRef<Value *> UserBndl,
94 unsigned Depth, LegalityAnalysis &Legality);
95 /// If the values in \p Bndl have external users, then emit unpacks and
96 /// connect them to the users. \p Vec is the vectorized form of \p Bndl.
97 void emitUnpacksForExternalUses(BndlRef<Value *> Bndl, Value *Vec);
98 /// Generate vector instructions based on `Actions` and return the last vector
99 /// created.
100 Value *emitVectors();
101 /// Entry point for vectorization starting from \p Seeds.
102 bool tryVectorize(BndlRef<Value *> Seeds, LegalityAnalysis &Legality);
103
104public:
105 BundleVec(StringRef AuxArg) : RegionPass("bundle-vec") {
106 ArgsRegistry.parse(AuxArg);
107 if (BottomUpArg) {
109 } else if (TopDownArg) {
111 } else {
112 std::string ErrStr;
113 raw_string_ostream ErrSS(ErrStr);
114 ErrSS << "bundle-vec requires either '" << BottomUpArg.getFlagStr()
115 << "' or '" << TopDownArg.getFlagStr()
116 << "' as its aux argument!\n";
117 reportFatalUsageError(ErrStr.c_str());
118 }
119 }
120
121 bool runOnRegion(Region &Rgn, const Analyses &A) final;
122};
123
124} // namespace llvm::sandboxir
125
126#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:215
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:105
Performs the legality analysis and returns a LegalityResult object.
Definition Legality.h:322
LLVM_ABI 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:457
BasicBlock(llvm::BasicBlock *BB, Context &SBCtx)
Definition BasicBlock.h:75
iterator end() const
Definition BasicBlock.h:89
LLVM_ABI iterator begin() const
LLVM_ABI void reportFatalUsageError(Error Err)
Report a fatal error that does not indicate a bug in LLVM.
Definition Error.cpp:177