Skip to content

Commit 30c49a4

Browse files
authored
[mlir][LLVMIR] Handle anonymous TBAA roots during metadata emission (#169167)
This commit enhances MLIR's TBAA export with support for anonymous TBAA roots. The import for this was around for a bit but the export was missing. Fixes: #160721
1 parent 1d64fd5 commit 30c49a4

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

mlir/lib/Target/LLVMIR/ModuleTranslation.cpp

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -677,10 +677,10 @@ llvm::Constant *mlir::LLVM::detail::getLLVMConstant(
677677
}
678678
}
679679
}
680-
// std::vector is used here to accomodate large number of elements that
681-
// exceed SmallVector capacity.
682-
std::vector<llvm::Constant *> constants(numElements, child);
683-
return llvm::ConstantArray::get(arrayType, constants);
680+
// std::vector is used here to accomodate large number of elements that
681+
// exceed SmallVector capacity.
682+
std::vector<llvm::Constant *> constants(numElements, child);
683+
return llvm::ConstantArray::get(arrayType, constants);
684684
}
685685
}
686686

@@ -2131,8 +2131,16 @@ LogicalResult ModuleTranslation::createTBAAMetadata() {
21312131
// LLVM metadata instances.
21322132
AttrTypeWalker walker;
21332133
walker.addWalk([&](TBAARootAttr root) {
2134-
tbaaMetadataMapping.insert(
2135-
{root, llvm::MDNode::get(ctx, llvm::MDString::get(ctx, root.getId()))});
2134+
llvm::MDNode *node;
2135+
if (StringAttr id = root.getId()) {
2136+
node = llvm::MDNode::get(ctx, llvm::MDString::get(ctx, id));
2137+
} else {
2138+
// Anonymous root nodes are self-referencing.
2139+
auto selfRef = llvm::MDNode::getTemporary(ctx, {});
2140+
node = llvm::MDNode::get(ctx, {selfRef.get()});
2141+
node->replaceOperandWith(0, node);
2142+
}
2143+
tbaaMetadataMapping.insert({root, node});
21362144
});
21372145

21382146
walker.addWalk([&](TBAATypeDescriptorAttr descriptor) {
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// RUN: mlir-translate -mlir-to-llvmir -split-input-file %s | FileCheck %s
2+
3+
#tbaa_root_0 = #llvm.tbaa_root<>
4+
#tbaa_type_desc_1 = #llvm.tbaa_type_desc<id = "omnipotent char", members = {<#tbaa_root_0, 0>}>
5+
#tbaa_type_desc_2 = #llvm.tbaa_type_desc<id = "long long", members = {<#tbaa_type_desc_1, 0>}>
6+
#tbaa_tag_3 = #llvm.tbaa_tag<access_type = #tbaa_type_desc_2, base_type = #tbaa_type_desc_2, offset = 0>
7+
8+
// CHECK: define void @tbaa_anonymous_root(ptr %{{.*}}) {
9+
// CHECK: %{{.*}} = load i64, ptr %{{.*}}, align 4, !tbaa ![[TAG:[0-9]+]]
10+
// CHECK: ret void
11+
// CHECK: }
12+
// CHECK: !llvm.module.flags = !{![[FLAGS:[0-9]+]]}
13+
// CHECK: ![[FLAGS]] = !{i32 2, !"Debug Info Version", i32 3}
14+
// CHECK: ![[TAG]] = !{![[TYPE:[0-9]+]], ![[TYPE]], i64 0}
15+
// CHECK: ![[TYPE]] = !{!"long long", ![[BASE:[0-9]+]], i64 0}
16+
// CHECK: ![[BASE]] = !{!"omnipotent char", ![[ROOT:[0-9]+]], i64 0}
17+
// CHECK: ![[ROOT]] = distinct !{![[ROOT]]}
18+
llvm.func @tbaa_anonymous_root(%arg0: !llvm.ptr) {
19+
%0 = llvm.load %arg0 {tbaa = [#tbaa_tag_3]} : !llvm.ptr -> i64
20+
llvm.return
21+
}

0 commit comments

Comments
 (0)