[Mlir-commits] [mlir] [mlir][LLVMIR] Handle anonymous TBAA roots during metadata emission (PR #169167)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Sat Nov 22 07:50:43 PST 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-mlir

Author: Men-cotton (Men-cotton)

<details>
<summary>Changes</summary>

Fixes: #<!-- -->160721 

Now `build/bin/mlir-translate -mlir-to-llvmir mlir/test/Dialect/LLVMIR/tbaa-roundtrip.mlir` passes.

---
Full diff: https://github.com/llvm/llvm-project/pull/169167.diff


2 Files Affected:

- (modified) mlir/lib/Target/LLVMIR/ModuleTranslation.cpp (+14-6) 
- (added) mlir/test/Target/LLVMIR/anonymous-tbaa.mlir (+21) 


``````````diff
diff --git a/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp b/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
index caecc0f774155..412a5f76d5753 100644
--- a/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
+++ b/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
@@ -677,10 +677,10 @@ llvm::Constant *mlir::LLVM::detail::getLLVMConstant(
           }
         }
       }
-        // std::vector is used here to accomodate large number of elements that
-        // exceed SmallVector capacity.
-        std::vector<llvm::Constant *> constants(numElements, child);
-        return llvm::ConstantArray::get(arrayType, constants);
+      // std::vector is used here to accomodate large number of elements that
+      // exceed SmallVector capacity.
+      std::vector<llvm::Constant *> constants(numElements, child);
+      return llvm::ConstantArray::get(arrayType, constants);
     }
   }
 
@@ -2131,8 +2131,16 @@ LogicalResult ModuleTranslation::createTBAAMetadata() {
   //    LLVM metadata instances.
   AttrTypeWalker walker;
   walker.addWalk([&](TBAARootAttr root) {
-    tbaaMetadataMapping.insert(
-        {root, llvm::MDNode::get(ctx, llvm::MDString::get(ctx, root.getId()))});
+    llvm::MDNode *node;
+    if (StringAttr id = root.getId()) {
+      node = llvm::MDNode::get(ctx, llvm::MDString::get(ctx, id));
+    } else {
+      // Anonymous root nodes are self-referencing.
+      auto selfRef = llvm::MDNode::getTemporary(ctx, {});
+      node = llvm::MDNode::get(ctx, {selfRef.get()});
+      node->replaceOperandWith(0, node);
+    }
+    tbaaMetadataMapping.insert({root, node});
   });
 
   walker.addWalk([&](TBAATypeDescriptorAttr descriptor) {
diff --git a/mlir/test/Target/LLVMIR/anonymous-tbaa.mlir b/mlir/test/Target/LLVMIR/anonymous-tbaa.mlir
new file mode 100644
index 0000000000000..b54bfe47e0807
--- /dev/null
+++ b/mlir/test/Target/LLVMIR/anonymous-tbaa.mlir
@@ -0,0 +1,21 @@
+// RUN: mlir-translate -mlir-to-llvmir -split-input-file %s | FileCheck %s
+
+#tbaa_root_0 = #llvm.tbaa_root<>
+#tbaa_type_desc_1 = #llvm.tbaa_type_desc<id = "omnipotent char", members = {<#tbaa_root_0, 0>}>
+#tbaa_type_desc_2 = #llvm.tbaa_type_desc<id = "long long", members = {<#tbaa_type_desc_1, 0>}>
+#tbaa_tag_3 = #llvm.tbaa_tag<access_type = #tbaa_type_desc_2, base_type = #tbaa_type_desc_2, offset = 0>
+
+// CHECK: define void @tbaa_anonymous_root(ptr %{{.*}}) {
+// CHECK:   %{{.*}} = load i64, ptr %{{.*}}, align 4, !tbaa ![[TAG:[0-9]+]]
+// CHECK:   ret void
+// CHECK: }
+// CHECK: !llvm.module.flags = !{![[FLAGS:[0-9]+]]}
+// CHECK: ![[FLAGS]] = !{i32 2, !"Debug Info Version", i32 3}
+// CHECK: ![[TAG]] = !{![[TYPE:[0-9]+]], ![[TYPE]], i64 0}
+// CHECK: ![[TYPE]] = !{!"long long", ![[BASE:[0-9]+]], i64 0}
+// CHECK: ![[BASE]] = !{!"omnipotent char", ![[ROOT:[0-9]+]], i64 0}
+// CHECK: ![[ROOT]] = distinct !{![[ROOT]]}
+llvm.func @tbaa_anonymous_root(%arg0: !llvm.ptr) {
+  %0 = llvm.load %arg0 {tbaa = [#tbaa_tag_3]} : !llvm.ptr -> i64
+  llvm.return
+}

``````````

</details>


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


More information about the Mlir-commits mailing list