Jorge Gorbe Moya 3bb92b530f
[SandboxVec] Tag insts in a Region with metadata. (#109353)
For each region, we create a metadata node. Then when an instruction is
added to the Region, it gets tagged with the metadata node for that
region. In the following example, we have a Region that contains only
the `%t0` instruction.

```
define i8 @foo(i8 %v0, i8 %v1) {
  %t0 = add i8 %v0, 1, !sbvec !0
  %t1 = add i8 %t0, %v1
  ret i8 %t1
}

!0 = distinct !{!"region"}
```

This commit also adds a function to create regions from metadata already
present in a Function.

This metadata can be used for debugging: if we dump IR before a Region
pass, the IR will contain enough info to re-create the Region and run
the pass by itself in a later invocation.

---------

Co-authored-by: Alina Sbirlea <alina.g.simion@gmail.com>
2024-09-23 16:59:02 -07:00

76 lines
2.1 KiB
C++

//===- Region.cpp ---------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "llvm/Transforms/Vectorize/SandboxVectorizer/Region.h"
namespace llvm::sandboxir {
Region::Region(Context &Ctx) : Ctx(Ctx) {
LLVMContext &LLVMCtx = Ctx.LLVMCtx;
auto *RegionStrMD = MDString::get(LLVMCtx, RegionStr);
RegionMDN = MDNode::getDistinct(LLVMCtx, {RegionStrMD});
}
Region::~Region() {}
void Region::add(Instruction *I) {
Insts.insert(I);
// TODO: Consider tagging instructions lazily.
cast<llvm::Instruction>(I->Val)->setMetadata(MDKind, RegionMDN);
}
void Region::remove(Instruction *I) {
Insts.remove(I);
cast<llvm::Instruction>(I->Val)->setMetadata(MDKind, nullptr);
}
#ifndef NDEBUG
bool Region::operator==(const Region &Other) const {
if (Insts.size() != Other.Insts.size())
return false;
if (!std::is_permutation(Insts.begin(), Insts.end(), Other.Insts.begin()))
return false;
return true;
}
void Region::dump(raw_ostream &OS) const {
for (auto *I : Insts)
OS << *I << "\n";
}
void Region::dump() const {
dump(dbgs());
dbgs() << "\n";
}
#endif // NDEBUG
SmallVector<std::unique_ptr<Region>> Region::createRegionsFromMD(Function &F) {
SmallVector<std::unique_ptr<Region>> Regions;
DenseMap<MDNode *, Region *> MDNToRegion;
auto &Ctx = F.getContext();
for (BasicBlock &BB : F) {
for (Instruction &Inst : BB) {
if (auto *MDN = cast<llvm::Instruction>(Inst.Val)->getMetadata(MDKind)) {
Region *R = nullptr;
auto It = MDNToRegion.find(MDN);
if (It == MDNToRegion.end()) {
Regions.push_back(std::make_unique<Region>(Ctx));
R = Regions.back().get();
MDNToRegion[MDN] = R;
} else {
R = It->second;
}
R->add(&Inst);
}
}
}
return Regions;
}
} // namespace llvm::sandboxir