LLVM 22.0.0git
InstrMaps.h
Go to the documentation of this file.
1//===- InstrMaps.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#ifndef LLVM_TRANSFORMS_VECTORIZE_SANDBOXVEC_PASSES_INSTRMAPS_H
10#define LLVM_TRANSFORMS_VECTORIZE_SANDBOXVEC_PASSES_INSTRMAPS_H
11
12#include "llvm/ADT/ArrayRef.h"
13#include "llvm/ADT/DenseMap.h"
14#include "llvm/ADT/SmallSet.h"
22
23namespace llvm::sandboxir {
24
25class LegalityResult;
26
27struct Action {
28 unsigned Idx = 0;
29 const LegalityResult *LegalityRes = nullptr;
32 unsigned Depth;
34 Value *Vec = nullptr;
38#ifndef NDEBUG
39 void print(raw_ostream &OS) const;
40 void dump() const;
41 friend raw_ostream &operator<<(raw_ostream &OS, const Action &A) {
42 A.print(OS);
43 return OS;
44 }
45#endif // NDEBUG
46};
47
48/// Maps the original instructions to the vectorized instrs and the reverse.
49/// For now an original instr can only map to a single vector.
50class InstrMaps {
51 /// A map from the original values that got combined into vectors, to the
52 /// vectorization Action.
53 DenseMap<Value *, Action *> OrigToVectorMap;
54 /// A map from the vec Action to a map of the original value to its lane.
55 /// Please note that for constant vectors, there may multiple original values
56 /// with the same lane, as they may be coming from vectorizing different
57 /// original values.
59 std::optional<Context::CallbackID> EraseInstrCB;
60
61public:
62 InstrMaps() = default;
63 ~InstrMaps() = default;
64 /// \Returns the vector value that we got from vectorizing \p Orig, or
65 /// nullptr if not found.
67 auto It = OrigToVectorMap.find(Orig);
68 return It != OrigToVectorMap.end() ? It->second : nullptr;
69 }
70 /// \Returns the lane of \p Orig before it got vectorized into \p Vec, or
71 /// nullopt if not found.
72 std::optional<unsigned> getOrigLane(Action *Vec, Value *Orig) const {
73 auto It1 = VectorToOrigLaneMap.find(Vec);
74 if (It1 == VectorToOrigLaneMap.end())
75 return std::nullopt;
76 const auto &OrigToLaneMap = It1->second;
77 auto It2 = OrigToLaneMap.find(Orig);
78 if (It2 == OrigToLaneMap.end())
79 return std::nullopt;
80 return It2->second;
81 }
82 /// Update the map to reflect that \p Origs got vectorized into \p Vec.
84 auto &OrigToLaneMap = VectorToOrigLaneMap[Vec];
85 unsigned Lane = 0;
86 for (Value *Orig : Origs) {
87 auto Pair = OrigToVectorMap.try_emplace(Orig, Vec);
88 assert(Pair.second && "Orig already exists in the map!");
89 (void)Pair;
90 OrigToLaneMap[Orig] = Lane;
91 Lane += VecUtils::getNumLanes(Orig);
92 }
93 }
94 void clear() {
95 OrigToVectorMap.clear();
96 VectorToOrigLaneMap.clear();
97 }
98#ifndef NDEBUG
99 void print(raw_ostream &OS) const {
100 OS << "OrigToVectorMap:\n";
101 for (auto [Orig, Vec] : OrigToVectorMap)
102 OS << *Orig << " : " << *Vec << "\n";
103 }
104 LLVM_DUMP_METHOD void dump() const;
105#endif
106};
107} // namespace llvm::sandboxir
108
109#endif // LLVM_TRANSFORMS_VECTORIZE_SANDBOXVEC_PASSES_INSTRMAPS_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition Compiler.h:638
This file defines the DenseMap class.
This file defines the SmallSet class.
This file defines the SmallVector class.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
std::optional< unsigned > getOrigLane(Action *Vec, Value *Orig) const
\Returns the lane of Orig before it got vectorized into Vec, or nullopt if not found.
Definition InstrMaps.h:72
void print(raw_ostream &OS) const
Definition InstrMaps.h:99
void registerVector(ArrayRef< Value * > Origs, Action *Vec)
Update the map to reflect that Origs got vectorized into Vec.
Definition InstrMaps.h:83
Action * getVectorForOrig(Value *Orig) const
\Returns the vector value that we got from vectorizing Orig, or nullptr if not found.
Definition InstrMaps.h:66
LLVM_DUMP_METHOD void dump() const
Definition InstrMaps.cpp:28
The legality outcome is represented by a class rather than an enum class because in some cases the le...
Definition Legality.h:158
A SandboxIR Value has users. This is the base class.
Definition Value.h:66
static unsigned getNumLanes(Type *Ty)
\Returns the number of vector lanes of Ty or 1 if not a vector.
Definition VecUtils.h:91
Action(const LegalityResult *LR, ArrayRef< Value * > B, ArrayRef< Value * > UB, unsigned Depth)
Definition InstrMaps.h:35
SmallVector< Value * > UserBndl
Definition InstrMaps.h:31
SmallVector< Value *, 4 > Bndl
Definition InstrMaps.h:30
SmallVector< Action * > Operands
Definition InstrMaps.h:33
void print(raw_ostream &OS) const
Definition InstrMaps.cpp:16
friend raw_ostream & operator<<(raw_ostream &OS, const Action &A)
Definition InstrMaps.h:41
const LegalityResult * LegalityRes
Definition InstrMaps.h:29