[llvm] [DebugInfo] getMergedLocation: match scopes based on their location (PR #132286)

Vladislav Dzhidzhoev via llvm-commits llvm-commits at lists.llvm.org
Fri Apr 4 10:27:48 PDT 2025


================
@@ -118,6 +118,106 @@ DILocation *DILocation::getMergedLocations(ArrayRef<DILocation *> Locs) {
   return Merged;
 }
 
+static DILexicalBlockBase *cloneAndReplaceParentScope(DILexicalBlockBase *LBB,
+                                                      DIScope *NewParent) {
+  TempMDNode ClonedScope = LBB->clone();
+  cast<DILexicalBlockBase>(*ClonedScope).replaceScope(NewParent);
+  return cast<DILexicalBlockBase>(
+      MDNode::replaceWithUniqued(std::move(ClonedScope)));
+}
+
+using LineColumn = std::pair<unsigned /* Line */, unsigned /* Column */>;
+
+/// Returns the location of DILocalScope, if present, or a default value.
+static LineColumn getLocalScopeLocationOr(DIScope *S, LineColumn Default) {
+  assert(isa<DILocalScope>(S) && "Expected DILocalScope.");
+
+  if (isa<DILexicalBlockFile>(S))
+    return Default;
+  if (auto *LB = dyn_cast<DILexicalBlock>(S))
+    return {LB->getLine(), LB->getColumn()};
+  if (auto *SP = dyn_cast<DISubprogram>(S))
+    return {SP->getLine(), 0u};
+
+  llvm_unreachable("Unhandled type of DILocalScope.");
+}
+
+// Returns the nearest matching scope inside a subprogram.
+template <typename MatcherT>
+static std::pair<DIScope *, LineColumn>
+getNearestMatchingScope(const DILocation *L1, const DILocation *L2) {
+  MatcherT Matcher;
+
+  DIScope *S1 = L1->getScope();
+  DIScope *S2 = L2->getScope();
+
+  // When matching DILexicalBlockFile's, ignore column numbers, so that
+  // DILocation's having different columns within the same
+  // DILexicalBlockFile will match.
----------------
dzhidzhoev wrote:

> Is this a behaviour we definitely want? On the one hand, it seems worth matching `DILexicalBlockFile`s if they have different column numbers, and setting a line 0 column in the result; on the other hand, if we have two `DILexicalBlockFile`s that have identical column numbers, then we would want to preserve that column number.

I added this initially, when the algorithm was meant to be used not in addition to but instead of `getNearestCommonScope()`. It was necessary to make it work the same way as `getNearestCommonScope()` in a simple case of
```
!0 = !DILocation(scope: !2, line: 1, col: 1)
!1 = !DILocation(scope: !2, line: 1, col: 2)
!2 = !DILexicalBlockFile(...)
```

It may also be useful at the current state of the PR, but we can simplify it for now, and probably add this behavior later when needed.

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


More information about the llvm-commits mailing list