[llvm] [SandboxVec] Tag insts in a Region with metadata. (PR #109353)

via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 19 17:49:22 PDT 2024


================
@@ -42,4 +54,27 @@ void Region::dump() const {
 }
 #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);
----------------
vporpo wrote:

Perhaps move this code into a lambda?
```
auto GetOrCreateNode = [&MDNToRegion](MDNode *MDN, std::unique_ptr<Region> &Regions) {
  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;
  }
  return R;
};
Region *R = GetOrCreateNode(MDN, Regions);
R->add(&Inst);
```

https://github.com/llvm/llvm-project/pull/109353


More information about the llvm-commits mailing list