LLVM 20.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
11
12namespace llvm::sandboxir {
13
14Region::Region(Context &Ctx) : Ctx(Ctx) {
15 LLVMContext &LLVMCtx = Ctx.LLVMCtx;
16 auto *RegionStrMD = MDString::get(LLVMCtx, RegionStr);
17 RegionMDN = MDNode::getDistinct(LLVMCtx, {RegionStrMD});
18
19 CreateInstCB = Ctx.registerCreateInstrCallback(
20 [this](Instruction *NewInst) { add(NewInst); });
21 EraseInstCB = Ctx.registerEraseInstrCallback(
22 [this](Instruction *ErasedInst) { remove(ErasedInst); });
23}
24
26 Ctx.unregisterCreateInstrCallback(CreateInstCB);
27 Ctx.unregisterEraseInstrCallback(EraseInstCB);
28}
29
31 Insts.insert(I);
32 // TODO: Consider tagging instructions lazily.
33 cast<llvm::Instruction>(I->Val)->setMetadata(MDKind, RegionMDN);
34}
35
37 Insts.remove(I);
38 cast<llvm::Instruction>(I->Val)->setMetadata(MDKind, nullptr);
39}
40
41#ifndef NDEBUG
42bool Region::operator==(const Region &Other) const {
43 if (Insts.size() != Other.Insts.size())
44 return false;
45 if (!std::is_permutation(Insts.begin(), Insts.end(), Other.Insts.begin()))
46 return false;
47 return true;
48}
49
51 for (auto *I : Insts)
52 OS << *I << "\n";
53}
54
55void Region::dump() const {
56 dump(dbgs());
57 dbgs() << "\n";
58}
59#endif // NDEBUG
60
64 auto &Ctx = F.getContext();
65 for (BasicBlock &BB : F) {
66 for (Instruction &Inst : BB) {
67 if (auto *MDN = cast<llvm::Instruction>(Inst.Val)->getMetadata(MDKind)) {
68 Region *R = nullptr;
69 auto It = MDNToRegion.find(MDN);
70 if (It == MDNToRegion.end()) {
71 Regions.push_back(std::make_unique<Region>(Ctx));
72 R = Regions.back().get();
73 MDNToRegion[MDN] = R;
74 } else {
75 R = It->second;
76 }
77 R->add(&Inst);
78 }
79 }
80 }
81 return Regions;
82}
83
84} // namespace llvm::sandboxir
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
raw_pwrite_stream & OS
iterator find(const_arg_type_t< KeyT > Val)
Definition: DenseMap.h:156
iterator end()
Definition: DenseMap.h:84
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
static MDTuple * getDistinct(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition: Metadata.h:1551
static MDString * get(LLVMContext &Context, StringRef Str)
Definition: Metadata.cpp:606
void push_back(const T &Elt)
Definition: SmallVector.h:413
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1196
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
Contains a list of sandboxir::Instruction's.
Definition: BasicBlock.h:67
CallbackID registerCreateInstrCallback(CreateInstrCallback CB)
Register a callback that gets called right after a SandboxIR instruction is created.
Definition: Context.cpp:700
void unregisterCreateInstrCallback(CallbackID ID)
Definition: Context.cpp:707
void unregisterEraseInstrCallback(CallbackID ID)
Definition: Context.cpp:693
LLVMContext & LLVMCtx
Definition: Context.h:46
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
A sandboxir::User with operands, opcode and linked with previous/next instructions in an instruction ...
Definition: Instruction.h:42
The main job of the Region is to point to new instructions generated by vectorization passes.
Definition: Region.h:54
void dump() const
Definition: Region.cpp:55
void add(Instruction *I)
Adds I to the set.
Definition: Region.cpp:30
Region(Context &Ctx)
Definition: Region.cpp:14
bool operator==(const Region &Other) const
This is an expensive check, meant for testing.
Definition: Region.cpp:42
void remove(Instruction *I)
Removes I from the set.
Definition: Region.cpp:36
static SmallVector< std::unique_ptr< Region > > createRegionsFromMD(Function &F)
Definition: Region.cpp:61
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
@ Other
Any other memory.