[Mlir-commits] [mlir] [MLIR][LLVM] Support Recursive DITypes (PR #80251)

Billy Zhu llvmlistbot at llvm.org
Thu Feb 29 23:32:39 PST 2024


================
@@ -247,12 +242,47 @@ DINodeAttr DebugImporter::translate(llvm::DINode *node) {
   if (DINodeAttr attr = nodeToAttr.lookup(node))
     return attr;
 
-  // Return nullptr if a cyclic dependency is detected since the same node is
-  // being traversed twice. This check avoids infinite recursion if the debug
-  // metadata contains cycles.
-  if (!translationStack.insert(node))
-    return nullptr;
-  auto guard = llvm::make_scope_exit([&]() { translationStack.pop_back(); });
+  // If a cyclic dependency is detected since the same node is being traversed
+  // twice, emit a recursive self type, and mark the duplicate node on the
+  // translationStack so it can emit a recursive decl type.
+  auto *typeNode = dyn_cast<llvm::DIType>(node);
+  if (typeNode) {
+    auto [iter, inserted] = typeTranslationStack.try_emplace(typeNode, nullptr);
+    if (!inserted) {
+      // The original node may have already been assigned a recursive ID from
+      // a different self-reference. Use that if possible.
+      DistinctAttr recId = iter->second;
+      if (!recId) {
+        recId = DistinctAttr::create(UnitAttr::get(context));
+        iter->second = recId;
+      }
+      unboundRecursiveSelfRefs.back().insert(recId);
+      return DIRecursiveTypeAttr::get(recId);
+    }
+  } else {
+    bool inserted =
+        nonTypeTranslationStack.insert({node, typeTranslationStack.size()});
----------------
zyx-billy wrote:

Yeah when I was originally thinking about it, I didn't realize most DITypes don't support recursion: Even though any DIType can theoretically participate in a cycle, not all of them can practically "start" a cycle, because not all of them can mutate (using the API that is... asm format is more permissive).

So realistically speaking, it's only the ones that support mutation that we need to support representing & importing (because those are the only ones that can be exported).

I just went through DebugInfoMetadata.h, and IIUC, the only DIType that has helper methods for replacing operands is DICompositeType 🤦. There are a few non-type DINodes that also support replacing, but we can ignore those for now as they're not types.

Please let me know if I'm missing something with this... otherwise if there's only one type to support, we can simplify away the generic support 😂.

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


More information about the Mlir-commits mailing list