LLVM 24.0.0git
Region.cpp
Go to the documentation of this file.
1//===- Region.cpp ---------------------------------------------------------===//
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
12
13namespace llvm::sandboxir {
14
16 LLVMContext &LLVMCtx = Ctx.LLVMCtx;
17 auto *RegionStrMD = MDString::get(LLVMCtx, RegionStr);
18 RegionMDN = MDNode::getDistinct(LLVMCtx, {RegionStrMD});
19
20 // Note: We may have multiple Region callbacks with the same name, thus the
21 // *Region name with the '*' prefix.
22 CreateInstCB = Ctx.registerCreateInstrCallback(
23 [this](Instruction *NewInst) { addRaw(NewInst); }, "Region",
24 /*AllowDuplicateNames=*/true);
25 EraseInstCB = Ctx.registerEraseInstrCallback(
26 [this](Instruction *ErasedInst) {
27 remove(ErasedInst);
28 removeFromAux(ErasedInst);
29 },
30 "Region", /*AllowDuplicateNames=*/true);
31}
32
34 Ctx.unregisterCreateInstrCallback(CreateInstCB);
35 Ctx.unregisterEraseInstrCallback(EraseInstCB);
36}
37
40 auto &LLVMCtx = Ctx.LLVMCtx;
41 for (auto [Idx, I] : enumerate(Aux)) {
42 llvm::ConstantInt *IdxC =
43 llvm::ConstantInt::get(llvm::Type::getInt32Ty(LLVMCtx), Idx, false);
44 assert(cast<llvm::Instruction>(I->Val)->getMetadata(AuxMDKind) == nullptr &&
45 "Instruction already in Aux!");
46 cast<llvm::Instruction>(I->Val)->setMetadata(
48 // Aux instrs should always be in a region.
49 addRaw(I);
50 }
51}
52
53void Region::setAux(unsigned Idx, Instruction *I) {
54 assert((Idx >= Aux.size() || Aux[Idx] == nullptr) &&
55 "There is already an Instruction at Idx in Aux!");
56 unsigned ExpectedSz = Idx + 1;
57 if (Aux.size() < ExpectedSz) {
58 auto SzBefore = Aux.size();
59 Aux.resize(ExpectedSz);
60 // Initialize the gap with nullptr.
61 for (unsigned Idx = SzBefore; Idx + 1 < ExpectedSz; ++Idx)
62 Aux[Idx] = nullptr;
63 }
64 Aux[Idx] = I;
65 // Aux instrs should always be in a region.
66 addRaw(I);
67}
68
70 auto *LLVMI = cast<llvm::Instruction>(I->Val);
71 LLVMI->setMetadata(AuxMDKind, nullptr);
72}
73
75 auto It = find(Aux, I);
76 if (It == Aux.end())
77 return;
79 Aux.erase(It);
80}
81
83 for (unsigned Idx : seq<unsigned>(0, Aux.size()))
84 dropAuxMetadata(Aux[Idx]);
85 Aux.clear();
86}
87
89 Insts.remove(I);
90 cast<llvm::Instruction>(I->Val)->setMetadata(MDKind, nullptr);
91}
92
93#ifndef NDEBUG
94bool Region::operator==(const Region &Other) const {
95 if (Insts.size() != Other.Insts.size())
96 return false;
97 if (!std::is_permutation(Insts.begin(), Insts.end(), Other.Insts.begin()))
98 return false;
99 return true;
100}
101
102void Region::dump(raw_ostream &OS) const {
103 for (auto *I : Insts)
104 OS << *I << "\n";
105 if (!Aux.empty()) {
106 OS << "\nAux:\n";
107 for (auto *I : Aux) {
108 if (I == nullptr)
109 OS << "NULL\n";
110 else
111 OS << *I << "\n";
112 }
113 }
114}
115
116void Region::dump() const {
117 dump(dbgs());
118 dbgs() << "\n";
119}
120#endif // NDEBUG
121
124 F, [&F]() { return std::make_unique<Region>(F.getContext()); });
125}
126
128 if (!Insts.empty())
129 return *Insts.front()->getParent()->getParent();
130 // Use the aux.
131 if (!Aux.empty())
132 return *Aux.front()->getParent()->getParent();
133 llvm_unreachable("Both instrs and aux are empty!");
134}
135
136} // namespace llvm::sandboxir
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
static ConstantAsMetadata * get(Constant *C)
Definition Metadata.h:549
This is the shared class of boolean and integer constants.
Definition Constants.h:87
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:76
static MDTuple * getDistinct(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition Metadata.h:1596
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition Metadata.h:1588
static LLVM_ABI MDString * get(LLVMContext &Context, StringRef Str)
Definition Metadata.cpp:595
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
static LLVM_ABI IntegerType * getInt32Ty(LLVMContext &C)
Definition Type.cpp:294
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
A sandboxir::User with operands, opcode and linked with previous/next instructions in an instruction ...
Definition Instruction.h:43
static constexpr const char * RegionStr
Definition Region.h:77
void addRaw(Instruction *I)
Adds I to the set.
Definition Region.h:97
void removeFromAux(Instruction *I)
Remove instruction I from Aux and drop metadata.
Definition Region.cpp:74
static constexpr const char * MDKind
Definition Region.h:76
RegionClassID ID
Definition Region.h:82
Context::CallbackID EraseInstCB
ID (for later deregistration) of the "erase instruction" callback.
Definition Region.h:87
bool operator==(const Region &Other) const
This is an expensive check, meant for testing.
Definition Region.cpp:94
virtual void remove(Instruction *I)
Removes I from the set.
Definition Region.cpp:88
Function & getParentFunction() const
Returns the function in which the region is defined.
Definition Region.cpp:127
static SmallVector< std::unique_ptr< RegionT > > createRegionsFromMD(Function &F, RegionFactoryT Factory)
Definition Region.h:122
Context::CallbackID CreateInstCB
ID (for later deregistration) of the "create instruction" callback.
Definition Region.h:85
void dropAuxMetadata(Instruction *I)
Helper for dropping Aux metadata for I.
Definition Region.cpp:69
SetVector< Instruction * > Insts
All the instructions in the Region.
Definition Region.h:70
static constexpr const char * AuxMDKind
Definition Region.h:78
friend class Context
Definition Region.h:107
Region(Context &Ctx, RegionClassID ID)
Definition Region.cpp:15
void clearAux()
Clears all auxiliary data.
Definition Region.cpp:82
void setAux(unsigned Idx, Instruction *I)
Set I as the Idx'th element in the auxiliary vector.
Definition Region.cpp:53
MDNode * RegionMDN
MDNode that we'll use to mark instructions as being part of the region.
Definition Region.h:75
SmallVector< Instruction * > Aux
An auxiliary sequence of Instruction-Index pairs.
Definition Region.h:72
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
RegionClassID
The main job of the Region is to point to new instructions generated by vectorization passes.
Definition Region.h:61
auto find(R &&Range, const T &Val)
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1781
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:2575
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
@ Other
Any other memory.
Definition ModRef.h:68
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
constexpr auto seq(T Begin, T End)
Iterate over an integral type from Begin up to - but not including - End.
Definition Sequence.h:341