LLVM 20.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"
21#include <algorithm>
22
23namespace llvm::sandboxir {
24
25/// Maps the original instructions to the vectorized instrs and the reverse.
26/// For now an original instr can only map to a single vector.
27class InstrMaps {
28 /// A map from the original values that got combined into vectors, to the
29 /// vector value(s).
30 DenseMap<Value *, Value *> OrigToVectorMap;
31 /// A map from the vector value to a map of the original value to its lane.
32 /// Please note that for constant vectors, there may multiple original values
33 /// with the same lane, as they may be coming from vectorizing different
34 /// original values.
36 Context &Ctx;
37 std::optional<Context::CallbackID> EraseInstrCB;
38
39private:
40 void notifyEraseInstr(Value *V) {
41 // We don't know if V is an original or a vector value.
42 auto It = OrigToVectorMap.find(V);
43 if (It != OrigToVectorMap.end()) {
44 // V is an original value.
45 // Remove it from VectorToOrigLaneMap.
46 Value *Vec = It->second;
47 VectorToOrigLaneMap[Vec].erase(V);
48 // Now erase V from OrigToVectorMap.
49 OrigToVectorMap.erase(It);
50 } else {
51 // V is a vector value.
52 // Go over the original values it came from and remove them from
53 // OrigToVectorMap.
54 for (auto [Orig, Lane] : VectorToOrigLaneMap[V])
55 OrigToVectorMap.erase(Orig);
56 // Now erase V from VectorToOrigLaneMap.
57 VectorToOrigLaneMap.erase(V);
58 }
59 }
60
61public:
62 InstrMaps(Context &Ctx) : Ctx(Ctx) {
63 EraseInstrCB = Ctx.registerEraseInstrCallback(
64 [this](Instruction *I) { notifyEraseInstr(I); });
65 }
66 ~InstrMaps() { Ctx.unregisterEraseInstrCallback(*EraseInstrCB); }
67 /// \Returns the vector value that we got from vectorizing \p Orig, or
68 /// nullptr if not found.
70 auto It = OrigToVectorMap.find(Orig);
71 return It != OrigToVectorMap.end() ? It->second : nullptr;
72 }
73 /// \Returns the lane of \p Orig before it got vectorized into \p Vec, or
74 /// nullopt if not found.
75 std::optional<unsigned> getOrigLane(Value *Vec, Value *Orig) const {
76 auto It1 = VectorToOrigLaneMap.find(Vec);
77 if (It1 == VectorToOrigLaneMap.end())
78 return std::nullopt;
79 const auto &OrigToLaneMap = It1->second;
80 auto It2 = OrigToLaneMap.find(Orig);
81 if (It2 == OrigToLaneMap.end())
82 return std::nullopt;
83 return It2->second;
84 }
85 /// Update the map to reflect that \p Origs got vectorized into \p Vec.
87 auto &OrigToLaneMap = VectorToOrigLaneMap[Vec];
88 for (auto [Lane, Orig] : enumerate(Origs)) {
89 auto Pair = OrigToVectorMap.try_emplace(Orig, Vec);
90 assert(Pair.second && "Orig already exists in the map!");
91 (void)Pair;
92 OrigToLaneMap[Orig] = Lane;
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
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition: Compiler.h:622
This file defines the DenseMap class.
#define I(x, y, z)
Definition: MD5.cpp:58
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
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:156
std::pair< iterator, bool > try_emplace(KeyT &&Key, Ts &&...Args)
Definition: DenseMap.h:226
bool erase(const KeyT &Val)
Definition: DenseMap.h:321
iterator end()
Definition: DenseMap.h:84
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
void unregisterEraseInstrCallback(CallbackID ID)
Definition: Context.cpp:693
CallbackID registerEraseInstrCallback(EraseInstrCallback CB)
Register a callback that gets called when a SandboxIR instruction is about to be removed from its par...
Definition: Context.cpp:686
Maps the original instructions to the vectorized instrs and the reverse.
Definition: InstrMaps.h:27
Value * getVectorForOrig(Value *Orig) const
\Returns the vector value that we got from vectorizing Orig, or nullptr if not found.
Definition: InstrMaps.h:69
void print(raw_ostream &OS) const
Definition: InstrMaps.h:100
void registerVector(ArrayRef< Value * > Origs, Value *Vec)
Update the map to reflect that Origs got vectorized into Vec.
Definition: InstrMaps.h:86
InstrMaps(Context &Ctx)
Definition: InstrMaps.h:62
LLVM_DUMP_METHOD void dump() const
Definition: InstrMaps.cpp:15
std::optional< unsigned > getOrigLane(Value *Vec, Value *Orig) const
\Returns the lane of Orig before it got vectorized into Vec, or nullopt if not found.
Definition: InstrMaps.h:75
A sandboxir::User with operands, opcode and linked with previous/next instructions in an instruction ...
Definition: Instruction.h:42
A SandboxIR Value has users. This is the base class.
Definition: Value.h:63
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:2448