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
10
11namespace llvm::sandboxir {
12
14 LLVMContext &LLVMCtx = Ctx.LLVMCtx;
15 auto *RegionStrMD = MDString::get(LLVMCtx, RegionStr);
16 RegionMDN = MDNode::getDistinct(LLVMCtx, {RegionStrMD});
17
18 // Note: We may have multiple Region callbacks with the same name, thus the
19 // *Region name with the '*' prefix.
20 CreateInstCB = Ctx.registerCreateInstrCallback(
21 [this](Instruction *NewInst) { addRaw(NewInst); }, "Region",
22 /*AllowDuplicateNames=*/true);
23 EraseInstCB = Ctx.registerEraseInstrCallback(
24 [this](Instruction *ErasedInst) {
25 remove(ErasedInst);
26 removeFromAux(ErasedInst);
27 },
28 "Region", /*AllowDuplicateNames=*/true);
29}
30
32 Ctx.unregisterCreateInstrCallback(CreateInstCB);
33 Ctx.unregisterEraseInstrCallback(EraseInstCB);
34}
35
38 auto &LLVMCtx = Ctx.LLVMCtx;
39 for (auto [Idx, I] : enumerate(Aux)) {
40 llvm::ConstantInt *IdxC =
41 llvm::ConstantInt::get(llvm::Type::getInt32Ty(LLVMCtx), Idx, false);
42 assert(cast<llvm::Instruction>(I->Val)->getMetadata(AuxMDKind) == nullptr &&
43 "Instruction already in Aux!");
44 cast<llvm::Instruction>(I->Val)->setMetadata(
46 // Aux instrs should always be in a region.
47 addRaw(I);
48 }
49}
50
51void Region::setAux(unsigned Idx, Instruction *I) {
52 assert((Idx >= Aux.size() || Aux[Idx] == nullptr) &&
53 "There is already an Instruction at Idx in Aux!");
54 unsigned ExpectedSz = Idx + 1;
55 if (Aux.size() < ExpectedSz) {
56 auto SzBefore = Aux.size();
57 Aux.resize(ExpectedSz);
58 // Initialize the gap with nullptr.
59 for (unsigned Idx = SzBefore; Idx + 1 < ExpectedSz; ++Idx)
60 Aux[Idx] = nullptr;
61 }
62 Aux[Idx] = I;
63 // Aux instrs should always be in a region.
64 addRaw(I);
65}
66
68 auto *LLVMI = cast<llvm::Instruction>(I->Val);
69 LLVMI->setMetadata(AuxMDKind, nullptr);
70}
71
73 auto It = find(Aux, I);
74 if (It == Aux.end())
75 return;
77 Aux.erase(It);
78}
79
81 for (unsigned Idx : seq<unsigned>(0, Aux.size()))
82 dropAuxMetadata(Aux[Idx]);
83 Aux.clear();
84}
85
87 Insts.remove(I);
88 cast<llvm::Instruction>(I->Val)->setMetadata(MDKind, nullptr);
89}
90
91#ifndef NDEBUG
92bool Region::operator==(const Region &Other) const {
93 if (Insts.size() != Other.Insts.size())
94 return false;
95 if (!std::is_permutation(Insts.begin(), Insts.end(), Other.Insts.begin()))
96 return false;
97 return true;
98}
99
100void Region::dump(raw_ostream &OS) const {
101 for (auto *I : Insts)
102 OS << *I << "\n";
103 if (!Aux.empty()) {
104 OS << "\nAux:\n";
105 for (auto *I : Aux) {
106 if (I == nullptr)
107 OS << "NULL\n";
108 else
109 OS << *I << "\n";
110 }
111 }
112}
113
114void Region::dump() const {
115 dump(dbgs());
116 dbgs() << "\n";
117}
118#endif // NDEBUG
119
122 F, [&F]() { return std::make_unique<Region>(F.getContext()); });
123}
124
125} // 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:72
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:92
virtual void remove(Instruction *I)
Removes I from the set.
Definition Region.cpp:86
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:67
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:13
void clearAux()
Clears all auxiliary data.
Definition Region.cpp:80
void setAux(unsigned Idx, Instruction *I)
Set I as the Idx'th element in the auxiliary vector.
Definition Region.cpp:51
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
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