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#include <algorithm>
23
24namespace llvm::sandboxir {
25
26class LegalityResult;
27
28struct Action {
29 unsigned Idx = 0;
30 const LegalityResult *LegalityRes = nullptr;
33 unsigned Depth;
35 Value *Vec = nullptr;
37 unsigned Depth)
38 : LegalityRes(LR), Bndl(B), UserBndl(UB), Depth(Depth) {}
39#ifndef NDEBUG
40 void print(raw_ostream &OS) const;
41 void dump() const;
43 A.print(OS);
44 return OS;
45 }
46#endif // NDEBUG
47};
48
49/// Maps the original instructions to the vectorized instrs and the reverse.
50/// For now an original instr can only map to a single vector.
51class InstrMaps {
52 /// A map from the original values that got combined into vectors, to the
53 /// vectorization Action.
54 DenseMap<Value *, Action *> OrigToVectorMap;
55 /// A map from the vec Action to a map of the original value to its lane.
56 /// Please note that for constant vectors, there may multiple original values
57 /// with the same lane, as they may be coming from vectorizing different
58 /// original values.
60 std::optional<Context::CallbackID> EraseInstrCB;
61
62public:
63 InstrMaps() = default;
64 ~InstrMaps() = default;
65 /// \Returns the vector value that we got from vectorizing \p Orig, or
66 /// nullptr if not found.
68 auto It = OrigToVectorMap.find(Orig);
69 return It != OrigToVectorMap.end() ? It->second : nullptr;
70 }
71 /// \Returns the lane of \p Orig before it got vectorized into \p Vec, or
72 /// nullopt if not found.
73 std::optional<unsigned> getOrigLane(Action *Vec, Value *Orig) const {
74 auto It1 = VectorToOrigLaneMap.find(Vec);
75 if (It1 == VectorToOrigLaneMap.end())
76 return std::nullopt;
77 const auto &OrigToLaneMap = It1->second;
78 auto It2 = OrigToLaneMap.find(Orig);
79 if (It2 == OrigToLaneMap.end())
80 return std::nullopt;
81 return It2->second;
82 }
83 /// Update the map to reflect that \p Origs got vectorized into \p Vec.
85 auto &OrigToLaneMap = VectorToOrigLaneMap[Vec];
86 unsigned Lane = 0;
87 for (Value *Orig : Origs) {
88 auto Pair = OrigToVectorMap.try_emplace(Orig, Vec);
89 assert(Pair.second && "Orig already exists in the map!");
90 (void)Pair;
91 OrigToLaneMap[Orig] = Lane;
92 Lane += VecUtils::getNumLanes(Orig);
93 }
94 }
95 void clear() {
96 OrigToVectorMap.clear();
97 VectorToOrigLaneMap.clear();
98 }
99#ifndef NDEBUG
100 void print(raw_ostream &OS) const {
101 OS << "OrigToVectorMap:\n";
102 for (auto [Orig, Vec] : OrigToVectorMap)
103 OS << *Orig << " : " << *Vec << "\n";
104 }
105 LLVM_DUMP_METHOD void dump() const;
106#endif
107};
108} // namespace llvm::sandboxir
109
110#endif // LLVM_TRANSFORMS_VECTORIZE_SANDBOXVEC_PASSES_INSTRMAPS_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
#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.
raw_pwrite_stream & OS
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:41
iterator find(const_arg_type_t< KeyT > Val)
Definition: DenseMap.h:177
std::pair< iterator, bool > try_emplace(KeyT &&Key, Ts &&...Args)
Definition: DenseMap.h:245
iterator end()
Definition: DenseMap.h:87
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1197
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
Maps the original instructions to the vectorized instrs and the reverse.
Definition: InstrMaps.h:51
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:73
void print(raw_ostream &OS) const
Definition: InstrMaps.h:100
void registerVector(ArrayRef< Value * > Origs, Action *Vec)
Update the map to reflect that Origs got vectorized into Vec.
Definition: InstrMaps.h:84
Action * getVectorForOrig(Value *Orig) const
\Returns the vector value that we got from vectorizing Orig, or nullptr if not found.
Definition: InstrMaps.h:67
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:36
SmallVector< Value * > UserBndl
Definition: InstrMaps.h:32
SmallVector< Value *, 4 > Bndl
Definition: InstrMaps.h:31
SmallVector< Action * > Operands
Definition: InstrMaps.h:34
void print(raw_ostream &OS) const
Definition: InstrMaps.cpp:16
friend raw_ostream & operator<<(raw_ostream &OS, const Action &A)
Definition: InstrMaps.h:42
const LegalityResult * LegalityRes
Definition: InstrMaps.h:30