[clang] [mlir] [mlir][llvm] Add tbaa_struct field attrs to the LLVM dialect (PR #238991)
Bruno Cardoso Lopes via cfe-commits
cfe-commits at lists.llvm.org
Thu Nov 5 17:55:25 PST 2026
https://github.com/bcardosolopes updated https://github.com/llvm/llvm-project/pull/238991
>From 9736504ed96a6656cef93bb92703648fa8fa5f41 Mon Sep 17 00:00:00 2001
From: Bruno Cardoso Lopes <bruno.cardoso at gmail.com>
Date: Fri, 16 Oct 2026 15:55:13 -0700
Subject: [PATCH 1/5] [mlir][llvm] Add tbaa_struct field attrs to the LLVM
dialect
The LLVM dialect models `!tbaa` access tags but has no representation for
`!tbaa.struct`, so aggregate copies with struct TBAA lose that metadata in
both directions: export drops it, import warns unhandled metadata.
This adds the missing piece:
- LLVMAttrDefs.td: new TBAAStructField attr (offset, size, tag triple)
plus the TBAAStructFieldArray array, following the existing TBAA attrs.
- LLVMIntrinsicOps.td: optional tbaa_struct on memcpy, memmove, and
memcpy.inline, with the export hook factored through a shared
setTBAAStructMetadataCode fragment in LLVMOpBase.td.
- ModuleTranslation: emit the flat (offset, size, tag) node, and walk
tbaa_struct tags in createTBAAMetadata since they are reachable from
nowhere else (without the walk the exported node has null tags).
- Import: convert `!tbaa.struct` back to the attribute, with the tags
pre-walked in convertMetadata; nodes that cannot be modeled (wide
offsets, non-copy carriers) are declined with a warning.
- InlinerInterfaceImpl: pass the new optional argument at the one
full-argument MemcpyOp::create call site.
Tests: tbaa-struct-metadata.mlir (export, mirror of the import test) and
Import/metadata-tbaa-struct.ll (import, tags pinned to their type
descriptors), plus two decline-path cases in Import/import-failure.ll.
The importer declines nodes it cannot model (null tags, empty nodes)
rather than representing them; MLIR-side verification of LLVM's
non-decreasing-offset rule is left for later.
Assisted-by: Muse Code (Muse).
---
.../mlir/Dialect/LLVMIR/LLVMAttrDefs.td | 37 +++++++++++++
.../mlir/Dialect/LLVMIR/LLVMIntrinsicOps.td | 22 +++++++-
.../include/mlir/Dialect/LLVMIR/LLVMOpBase.td | 3 ++
.../mlir/Target/LLVMIR/ModuleTranslation.h | 4 ++
.../Transforms/InlinerInterfaceImpl.cpp | 1 +
.../LLVMIR/LLVMIRToLLVMTranslation.cpp | 52 +++++++++++++++++++
mlir/lib/Target/LLVMIR/ModuleImport.cpp | 11 ++++
mlir/lib/Target/LLVMIR/ModuleTranslation.cpp | 38 ++++++++++++++
.../Target/LLVMIR/Import/import-failure.ll | 35 +++++++++++++
.../LLVMIR/Import/metadata-tbaa-struct.ll | 40 ++++++++++++++
.../Target/LLVMIR/tbaa-struct-metadata.mlir | 34 ++++++++++++
11 files changed, 275 insertions(+), 2 deletions(-)
create mode 100644 mlir/test/Target/LLVMIR/Import/metadata-tbaa-struct.ll
create mode 100644 mlir/test/Target/LLVMIR/tbaa-struct-metadata.mlir
diff --git a/mlir/include/mlir/Dialect/LLVMIR/LLVMAttrDefs.td b/mlir/include/mlir/Dialect/LLVMIR/LLVMAttrDefs.td
index 2dcc73dc38bfa1..2a4337ed107fc0 100644
--- a/mlir/include/mlir/Dialect/LLVMIR/LLVMAttrDefs.td
+++ b/mlir/include/mlir/Dialect/LLVMIR/LLVMAttrDefs.td
@@ -1385,6 +1385,43 @@ def LLVM_TBAATagArrayAttr
let constBuilderCall = ?;
}
+//===----------------------------------------------------------------------===//
+// TBAAStructFieldAttr
+//===----------------------------------------------------------------------===//
+
+def LLVM_TBAAStructFieldAttr
+ : LLVM_Attr<"TBAAStructField", "tbaa_struct_field"> {
+ let parameters = (ins
+ "int64_t":$offset,
+ "int64_t":$size,
+ "TBAATagAttr":$tag
+ );
+
+ let summary = "One field of an LLVM dialect tbaa.struct description";
+
+ let description = [{
+ One entry of a `!tbaa.struct` node, which describes the fields an aggregate
+ copy touches so that the copy is not treated as one opaque access. The
+ fields need not be contiguous: a gap is padding, which carries no data.
+
+ Example:
+ ```mlir
+ #field = #llvm.tbaa_struct_field<offset = 0, size = 4, tag = #tbaa_tag>
+ ```
+ }];
+
+ let assemblyFormat = "`<` struct(params) `>`";
+
+ // Generate mnemonic alias for the attribute.
+ let genMnemonicAlias = 1;
+}
+
+def LLVM_TBAAStructFieldArrayAttr
+ : TypedArrayAttrBase<LLVM_TBAAStructFieldAttr,
+ LLVM_TBAAStructFieldAttr.summary # " array"> {
+ let constBuilderCall = ?;
+}
+
//===----------------------------------------------------------------------===//
// MMRATagAttr
//===----------------------------------------------------------------------===//
diff --git a/mlir/include/mlir/Dialect/LLVMIR/LLVMIntrinsicOps.td b/mlir/include/mlir/Dialect/LLVMIR/LLVMIntrinsicOps.td
index 3695fcf52ebe14..5b418d2529e953 100644
--- a/mlir/include/mlir/Dialect/LLVMIR/LLVMIntrinsicOps.td
+++ b/mlir/include/mlir/Dialect/LLVMIR/LLVMIntrinsicOps.td
@@ -265,7 +265,10 @@ class LLVM_MemcpyIntrOpBase<string name> :
/*immArgPositions=*/[3], /*immArgAttrNames=*/["isVolatile"]> {
dag args = (ins Arg<LLVM_AnyPointer,"",[MemWrite]>:$dst,
Arg<LLVM_AnyPointer,"",[MemRead]>:$src,
- AnySignlessInteger:$len, I1Attr:$isVolatile);
+ AnySignlessInteger:$len, I1Attr:$isVolatile,
+ // `!tbaa.struct`: which fields of the aggregate the copy
+ // touches, so it is not treated as one opaque access.
+ OptionalAttr<LLVM_TBAAStructFieldArrayAttr>:$tbaa_struct);
// Append the arguments defined by LLVM_IntrOpBase.
let arguments = !con(args, baseArgs);
let builders = [
@@ -277,11 +280,19 @@ class LLVM_MemcpyIntrOpBase<string name> :
OpBuilder<(ins "Value":$dst, "Value":$src, "Value":$len,
"IntegerAttr":$isVolatile), [{
build($_builder, $_state, dst, src, len, isVolatile,
+ /*tbaa_struct=*/nullptr,
/*access_groups=*/nullptr, /*alias_scopes=*/nullptr,
/*noalias_scopes=*/nullptr, /*tbaa=*/nullptr,
/*arg_attrs=*/nullptr, /*res_attrs=*/nullptr);
}]>
];
+
+ // The generated builder writes the alias metadata; tbaa.struct is specific to
+ // the aggregate-copy intrinsics, so it is appended here rather than in the
+ // shared alias code, which also covers loads and stores.
+ let llvmBuilder = baseLlvmBuilder # setAccessGroupsMetadataCode
+ # setAliasAnalysisMetadataCode # setTBAAStructMetadataCode
+ # baseLlvmBuilderArgAndResultAttrs # baseLlvmBuilderCoda;
}
def LLVM_MemcpyOp : LLVM_MemcpyIntrOpBase<"memcpy">;
@@ -297,7 +308,8 @@ def LLVM_MemcpyInlineOp :
/*immArgPositions=*/[2, 3], /*immArgAttrNames=*/["len", "isVolatile"]> {
dag args = (ins Arg<LLVM_AnyPointer,"",[MemWrite]>:$dst,
Arg<LLVM_AnyPointer,"",[MemRead]>:$src,
- APIntAttr:$len, I1Attr:$isVolatile);
+ APIntAttr:$len, I1Attr:$isVolatile,
+ OptionalAttr<LLVM_TBAAStructFieldArrayAttr>:$tbaa_struct);
// Append the arguments defined by LLVM_IntrOpBase.
let arguments = !con(args, baseArgs);
let builders = [
@@ -309,11 +321,17 @@ def LLVM_MemcpyInlineOp :
OpBuilder<(ins "Value":$dst, "Value":$src, "IntegerAttr":$len,
"IntegerAttr":$isVolatile), [{
build($_builder, $_state, dst, src, len, isVolatile,
+ /*tbaa_struct=*/nullptr,
/*access_groups=*/nullptr, /*alias_scopes=*/nullptr,
/*noalias_scopes=*/nullptr, /*tbaa=*/nullptr,
/*arg_attrs=*/nullptr, /*res_attrs=*/nullptr);
}]>
];
+
+ // Same as the memcpy base: tbaa.struct is specific to the aggregate copies.
+ let llvmBuilder = baseLlvmBuilder # setAccessGroupsMetadataCode
+ # setAliasAnalysisMetadataCode # setTBAAStructMetadataCode
+ # baseLlvmBuilderArgAndResultAttrs # baseLlvmBuilderCoda;
}
def LLVM_MemsetOp : LLVM_ZeroResultIntrOp<"memset", [0, 2],
diff --git a/mlir/include/mlir/Dialect/LLVMIR/LLVMOpBase.td b/mlir/include/mlir/Dialect/LLVMIR/LLVMOpBase.td
index d0506f971b2c2a..66583a592b3f64 100644
--- a/mlir/include/mlir/Dialect/LLVMIR/LLVMOpBase.td
+++ b/mlir/include/mlir/Dialect/LLVMIR/LLVMOpBase.td
@@ -260,6 +260,9 @@ class LLVM_MemOpPatterns {
moduleTranslation.setAliasScopeMetadata(op, inst);
moduleTranslation.setTBAAMetadata(op, inst);
}];
+ code setTBAAStructMetadataCode = [{
+ moduleTranslation.setTBAAStructMetadata(op.getTbaaStructAttr(), inst);
+ }];
}
//===----------------------------------------------------------------------===//
diff --git a/mlir/include/mlir/Target/LLVMIR/ModuleTranslation.h b/mlir/include/mlir/Target/LLVMIR/ModuleTranslation.h
index 323b44f7ddfa12..528775d8edc1cf 100644
--- a/mlir/include/mlir/Target/LLVMIR/ModuleTranslation.h
+++ b/mlir/include/mlir/Target/LLVMIR/ModuleTranslation.h
@@ -198,6 +198,10 @@ class ModuleTranslation {
/// Sets LLVM TBAA metadata for memory operations that have TBAA attributes.
void setTBAAMetadata(AliasAnalysisOpInterface op, llvm::Instruction *inst);
+ /// Sets LLVM tbaa.struct metadata on an aggregate copy, describing which
+ /// fields of the aggregate the copy touches.
+ void setTBAAStructMetadata(ArrayAttr fields, llvm::Instruction *inst);
+
/// Sets LLVM dereferenceable metadata for operations that have
/// dereferenceable attributes.
void setDereferenceableMetadata(DereferenceableOpInterface op,
diff --git a/mlir/lib/Dialect/LLVMIR/Transforms/InlinerInterfaceImpl.cpp b/mlir/lib/Dialect/LLVMIR/Transforms/InlinerInterfaceImpl.cpp
index 29b8321e3b3640..c824cf6ed41d40 100644
--- a/mlir/lib/Dialect/LLVMIR/Transforms/InlinerInterfaceImpl.cpp
+++ b/mlir/lib/Dialect/LLVMIR/Transforms/InlinerInterfaceImpl.cpp
@@ -630,6 +630,7 @@ static Value handleByValArgumentInit(OpBuilder &builder, Location loc,
builder.getArrayAttr({builder.getDictionaryAttr({dstAlignAttr})});
LLVM::MemcpyOp::create(builder, loc, allocaOp, argument, copySize,
/*isVolatile=*/false,
+ /*tbaa_struct=*/nullptr,
/*access_groups=*/nullptr, /*alias_scopes=*/nullptr,
/*noalias_scopes=*/nullptr, /*tbaa=*/nullptr, argAttrs,
/*res_attrs=*/nullptr);
diff --git a/mlir/lib/Target/LLVMIR/Dialect/LLVMIR/LLVMIRToLLVMTranslation.cpp b/mlir/lib/Target/LLVMIR/Dialect/LLVMIR/LLVMIRToLLVMTranslation.cpp
index ae75a7690163a8..63d32e7a04bd1d 100644
--- a/mlir/lib/Target/LLVMIR/Dialect/LLVMIR/LLVMIRToLLVMTranslation.cpp
+++ b/mlir/lib/Target/LLVMIR/Dialect/LLVMIR/LLVMIRToLLVMTranslation.cpp
@@ -97,6 +97,7 @@ getSupportedMetadataImpl(llvm::LLVMContext &llvmContext) {
llvm::LLVMContext::MD_mmra,
llvm::LLVMContext::MD_memprof,
llvm::LLVMContext::MD_callsite,
+ llvm::LLVMContext::MD_tbaa_struct,
llvmContext.getMDKindID(vecTypeHintMDName),
llvmContext.getMDKindID(workGroupSizeHintMDName),
llvmContext.getMDKindID(reqdWorkGroupSizeMDName),
@@ -214,6 +215,55 @@ static LogicalResult setTBAAAttr(const llvm::MDNode *node, Operation *op,
return success();
}
+/// Converts `!tbaa.struct` to the `tbaa_struct` attribute on the imported
+/// aggregate copy. The node is a flat list of (offset, size, tag) triples: it
+/// says which fields of the aggregate the copy actually touches, so the copy is
+/// not treated as one opaque access and the gaps are known to be padding.
+static LogicalResult setTBAAStructAttr(OpBuilder &builder,
+ const llvm::MDNode *node, Operation *op,
+ LLVM::ModuleImport &moduleImport) {
+ if (!isa<LLVM::MemcpyOp, LLVM::MemcpyInlineOp, LLVM::MemmoveOp>(op))
+ return failure();
+
+ // Operands come in conceptual groups of three; anything else is malformed.
+ if (node->getNumOperands() == 0 || node->getNumOperands() % 3 != 0)
+ return failure();
+
+ SmallVector<Attribute> fields;
+ fields.reserve(node->getNumOperands() / 3);
+ for (unsigned i = 0, e = node->getNumOperands(); i != e; i += 3) {
+ auto *offset = llvm::mdconst::dyn_extract_or_null<llvm::ConstantInt>(
+ node->getOperand(i));
+ auto *size = llvm::mdconst::dyn_extract_or_null<llvm::ConstantInt>(
+ node->getOperand(i + 1));
+ if (!offset || !size)
+ return failure();
+ // Offsets and sizes are int64 in the attribute; wider constants cannot be
+ // represented, and extracting them would assert.
+ std::optional<int64_t> offsetValue = offset->getValue().trySExtValue();
+ std::optional<int64_t> sizeValue = size->getValue().trySExtValue();
+ if (!offsetValue || !sizeValue)
+ return failure();
+ auto *tagNode = dyn_cast_if_present<llvm::MDNode>(node->getOperand(i + 2));
+ if (!tagNode)
+ return failure();
+ auto tag =
+ dyn_cast_if_present<TBAATagAttr>(moduleImport.lookupTBAAAttr(tagNode));
+ if (!tag)
+ return failure();
+ fields.push_back(
+ builder.getAttr<TBAAStructFieldAttr>(*offsetValue, *sizeValue, tag));
+ }
+
+ return TypeSwitch<Operation *, LogicalResult>(op)
+ .Case<LLVM::MemcpyOp, LLVM::MemcpyInlineOp, LLVM::MemmoveOp>(
+ [&](auto copyOp) {
+ copyOp.setTbaaStructAttr(builder.getArrayAttr(fields));
+ return success();
+ })
+ .Default([](auto) { return failure(); });
+}
+
/// Looks up all the access group attributes that map to the access group nodes
/// starting from the access group metadata `node`, and attaches all of them to
/// the imported operation if the lookups succeed. Returns failure otherwise.
@@ -604,6 +654,8 @@ class LLVMDialectLLVMIRImportInterface : public LLVMImportDialectInterface {
return setMemProfAttr(builder, node, op);
if (kind == llvm::LLVMContext::MD_callsite)
return setCallsiteAttr(builder, node, op);
+ if (kind == llvm::LLVMContext::MD_tbaa_struct)
+ return setTBAAStructAttr(builder, node, op, moduleImport);
llvm::LLVMContext &context = node->getContext();
if (kind == context.getMDKindID(vecTypeHintMDName))
return setVecTypeHintAttr(builder, node, op, moduleImport);
diff --git a/mlir/lib/Target/LLVMIR/ModuleImport.cpp b/mlir/lib/Target/LLVMIR/ModuleImport.cpp
index 0223c1e3343c7f..051b67cbf3f797 100644
--- a/mlir/lib/Target/LLVMIR/ModuleImport.cpp
+++ b/mlir/lib/Target/LLVMIR/ModuleImport.cpp
@@ -1170,6 +1170,17 @@ LogicalResult ModuleImport::convertMetadata() {
if (aliasAnalysisNodes.TBAA)
if (failed(processTBAAMetadata(aliasAnalysisNodes.TBAA)))
return failure();
+ // A `!tbaa.struct` node reaches its own tags, and those tags may appear
+ // nowhere else in the module. Without walking them here the lookup in
+ // setTBAAStructAttr finds nothing and the node is declined.
+ if (aliasAnalysisNodes.TBAAStruct) {
+ const llvm::MDNode *structNode = aliasAnalysisNodes.TBAAStruct;
+ for (unsigned i = 2, e = structNode->getNumOperands(); i < e; i += 3)
+ if (auto *tag =
+ dyn_cast_if_present<llvm::MDNode>(structNode->getOperand(i)))
+ if (failed(processTBAAMetadata(tag)))
+ return failure();
+ }
if (aliasAnalysisNodes.Scope)
if (failed(processAliasScopeMetadata(aliasAnalysisNodes.Scope)))
return failure();
diff --git a/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp b/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
index 41ead15f4ecb0d..1428ce906860de 100644
--- a/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
+++ b/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
@@ -2418,6 +2418,29 @@ void ModuleTranslation::setTBAAMetadata(AliasAnalysisOpInterface op,
inst->setMetadata(llvm::LLVMContext::MD_tbaa, node);
}
+void ModuleTranslation::setTBAAStructMetadata(ArrayAttr fields,
+ llvm::Instruction *inst) {
+ if (!fields || fields.empty())
+ return;
+
+ // The node is a flat list of (offset, size, tag) triples, not a list of
+ // per-field nodes.
+ llvm::LLVMContext &ctx = getLLVMContext();
+ llvm::Type *i64 = llvm::Type::getInt64Ty(ctx);
+ SmallVector<llvm::Metadata *> operands;
+ operands.reserve(fields.size() * 3);
+ for (Attribute field : fields) {
+ auto fieldAttr = cast<TBAAStructFieldAttr>(field);
+ operands.push_back(llvm::ConstantAsMetadata::get(
+ llvm::ConstantInt::getSigned(i64, fieldAttr.getOffset())));
+ operands.push_back(llvm::ConstantAsMetadata::get(
+ llvm::ConstantInt::getSigned(i64, fieldAttr.getSize())));
+ operands.push_back(getTBAANode(fieldAttr.getTag()));
+ }
+ inst->setMetadata(llvm::LLVMContext::MD_tbaa_struct,
+ llvm::MDNode::get(ctx, operands));
+}
+
void ModuleTranslation::setDereferenceableMetadata(
DereferenceableOpInterface op, llvm::Instruction *inst) {
DereferenceableAttr derefAttr = op.getDereferenceableOrNull();
@@ -2506,6 +2529,21 @@ LogicalResult ModuleTranslation::createTBAAMetadata() {
walker.walk(attr);
});
+ // The tags inside a `tbaa_struct` are reachable from nowhere else, so without
+ // this walk they never get an MDNode and the exported node has null tags.
+ auto walkStructFields = [&](ArrayAttr fields) {
+ if (fields)
+ walker.walk(fields);
+ };
+ mlirModule->walk([&](Operation *op) {
+ if (auto copyOp = dyn_cast<LLVM::MemcpyOp>(op))
+ walkStructFields(copyOp.getTbaaStructAttr());
+ else if (auto copyOp = dyn_cast<LLVM::MemcpyInlineOp>(op))
+ walkStructFields(copyOp.getTbaaStructAttr());
+ else if (auto copyOp = dyn_cast<LLVM::MemmoveOp>(op))
+ walkStructFields(copyOp.getTbaaStructAttr());
+ });
+
return success();
}
diff --git a/mlir/test/Target/LLVMIR/Import/import-failure.ll b/mlir/test/Target/LLVMIR/Import/import-failure.ll
index 083ea8b6ce81bf..a7f187b0a8c3d3 100644
--- a/mlir/test/Target/LLVMIR/Import/import-failure.ll
+++ b/mlir/test/Target/LLVMIR/Import/import-failure.ll
@@ -660,3 +660,38 @@ define ptr @wide_callsite_id() {
declare ptr @alloc(i64)
!0 = !{i128 1}
+; // -----
+
+; Only aggregate copies carry tbaa_struct; on any other instruction the node is
+; declined, and the import keeps going.
+; CHECK: <unknown>
+; CHECK-SAME: warning: unhandled metadata: ![[ST:[0-9]+]] = !{i64 0, i64 4, ![[TAG:[0-9]+]]}
+define i32 @tbaa_struct_on_load(ptr %p) {
+ %v = load i32, ptr %p, !tbaa.struct !7
+ ret i32 %v
+}
+
+!5 = !{!"Simple C++ TBAA"}
+!0 = !{!"omnipotent char", !5, i64 0}
+!1 = !{!"int", !0, i64 0}
+!2 = !{!1, !1, i64 0}
+!7 = !{i64 0, i64 4, !2}
+
+; // -----
+
+; Offsets and sizes are int64 in the attribute; a wider constant cannot be
+; represented, so the node is declined instead of crashing.
+; CHECK: <unknown>
+; CHECK-SAME: warning: unhandled metadata: ![[ST:[0-9]+]] = !{i128 18446744073709551616, i64 4, ![[TAG:[0-9]+]]}
+declare void @llvm.memcpy.p0.p0.i64(ptr, ptr, i64, i1)
+
+define void @tbaa_struct_wide_offset(ptr %d, ptr %s) {
+ call void @llvm.memcpy.p0.p0.i64(ptr %d, ptr %s, i64 12, i1 false), !tbaa.struct !8
+ ret void
+}
+
+!5 = !{!"Simple C++ TBAA"}
+!0 = !{!"omnipotent char", !5, i64 0}
+!1 = !{!"int", !0, i64 0}
+!2 = !{!1, !1, i64 0}
+!8 = !{i128 18446744073709551616, i64 4, !2}
diff --git a/mlir/test/Target/LLVMIR/Import/metadata-tbaa-struct.ll b/mlir/test/Target/LLVMIR/Import/metadata-tbaa-struct.ll
new file mode 100644
index 00000000000000..728168dbcc128f
--- /dev/null
+++ b/mlir/test/Target/LLVMIR/Import/metadata-tbaa-struct.ll
@@ -0,0 +1,40 @@
+; RUN: mlir-translate -import-llvm -emit-expensive-warnings %s 2>&1 | FileCheck %s
+
+; !tbaa.struct describes which fields of an aggregate a copy touches, so the
+; copy is not treated as one opaque access and the gaps are known to be padding.
+; The node is a flat list of (offset, size, tag) triples.
+
+; CHECK-NOT: warning
+
+declare void @llvm.memcpy.p0.p0.i64(ptr, ptr, i64, i1)
+declare void @llvm.memmove.p0.p0.i64(ptr, ptr, i64, i1)
+declare void @llvm.memcpy.inline.p0.p0.i64(ptr, ptr, i64, i1)
+
+; The two fields start eight bytes apart with a four byte hole between them;
+; that hole is padding the copy need not preserve.
+; CHECK-DAG: #[[INT:.*]] = #llvm.tbaa_type_desc<id = "int"
+; CHECK-DAG: #[[FLT:.*]] = #llvm.tbaa_type_desc<id = "float"
+; CHECK-DAG: #[[TI:.*]] = #llvm.tbaa_tag<base_type = #[[INT]], access_type = #[[INT]], offset = 0>
+; CHECK-DAG: #[[TF:.*]] = #llvm.tbaa_tag<base_type = #[[FLT]], access_type = #[[FLT]], offset = 0>
+; CHECK-DAG: #[[F0:.*]] = #llvm.tbaa_struct_field<offset = 0, size = 4, tag = #[[TI]]>
+; CHECK-DAG: #[[F1:.*]] = #llvm.tbaa_struct_field<offset = 8, size = 4, tag = #[[TF]]>
+; CHECK: llvm.intr.memcpy
+; CHECK-SAME: tbaa_struct = [#[[F0]], #[[F1]]]
+; CHECK: llvm.intr.memmove
+; CHECK-SAME: tbaa_struct = [#[[F0]], #[[F1]]]
+; CHECK: llvm.intr.memcpy.inline
+; CHECK-SAME: tbaa_struct = [#[[F0]], #[[F1]]]
+define void @cp(ptr %d, ptr %s) {
+ call void @llvm.memcpy.p0.p0.i64(ptr %d, ptr %s, i64 12, i1 false), !tbaa.struct !4
+ call void @llvm.memmove.p0.p0.i64(ptr %d, ptr %s, i64 12, i1 false), !tbaa.struct !4
+ call void @llvm.memcpy.inline.p0.p0.i64(ptr %d, ptr %s, i64 12, i1 false), !tbaa.struct !4
+ ret void
+}
+
+!0 = !{!"omnipotent char", !5, i64 0}
+!5 = !{!"Simple C++ TBAA"}
+!1 = !{!"int", !0, i64 0}
+!2 = !{!"float", !0, i64 0}
+!6 = !{!1, !1, i64 0}
+!7 = !{!2, !2, i64 0}
+!4 = !{ i64 0, i64 4, !6, i64 8, i64 4, !7 }
diff --git a/mlir/test/Target/LLVMIR/tbaa-struct-metadata.mlir b/mlir/test/Target/LLVMIR/tbaa-struct-metadata.mlir
new file mode 100644
index 00000000000000..cd1c87c069529d
--- /dev/null
+++ b/mlir/test/Target/LLVMIR/tbaa-struct-metadata.mlir
@@ -0,0 +1,34 @@
+// RUN: mlir-translate -mlir-to-llvmir %s | FileCheck %s
+// RUN: mlir-opt %s --verify-roundtrip -o /dev/null
+
+// The export mirror of Import/metadata-tbaa-struct.ll. The body is exactly what
+// the importer produces, so the two pin the whole trip.
+//
+// The tags inside a tbaa_struct are reachable from nowhere else in the module,
+// so the exporter has to walk them explicitly; without that the node comes out
+// with null tags.
+
+// CHECK: call void @llvm.memcpy.p0.p0.i64(ptr %{{.*}}, ptr %{{.*}}, i64 12, i1 false), !tbaa.struct ![[S:[0-9]+]]
+// CHECK: call void @llvm.memmove.p0.p0.i64(ptr %{{.*}}, ptr %{{.*}}, i64 12, i1 false), !tbaa.struct ![[S]]
+// CHECK: call void @llvm.memcpy.inline.p0.p0.i64(ptr %{{.*}}, ptr %{{.*}}, i64 12, i1 false), !tbaa.struct ![[S]]
+// CHECK: ![[S]] = !{i64 0, i64 4, ![[T0:[0-9]+]], i64 8, i64 4, ![[T1:[0-9]+]]}
+// CHECK-DAG: ![[T0]] = !{![[INT:[0-9]+]], ![[INT]], i64 0}
+// CHECK-DAG: ![[INT]] = !{!"int", !{{[0-9]+}}, i64 0}
+// CHECK-DAG: ![[T1]] = !{![[FLT:[0-9]+]], ![[FLT]], i64 0}
+// CHECK-DAG: ![[FLT]] = !{!"float", !{{[0-9]+}}, i64 0}
+
+#tbaa_root = #llvm.tbaa_root<id = "Simple C++ TBAA">
+#tbaa_type_desc = #llvm.tbaa_type_desc<id = "omnipotent char", members = {<#tbaa_root, 0>}>
+#tbaa_type_desc1 = #llvm.tbaa_type_desc<id = "int", members = {<#tbaa_type_desc, 0>}>
+#tbaa_type_desc2 = #llvm.tbaa_type_desc<id = "float", members = {<#tbaa_type_desc, 0>}>
+#tbaa_tag = #llvm.tbaa_tag<base_type = #tbaa_type_desc1, access_type = #tbaa_type_desc1, offset = 0>
+#tbaa_tag1 = #llvm.tbaa_tag<base_type = #tbaa_type_desc2, access_type = #tbaa_type_desc2, offset = 0>
+module {
+ llvm.func @cp(%arg0: !llvm.ptr, %arg1: !llvm.ptr) {
+ %0 = llvm.mlir.constant(12 : i64) : i64
+ "llvm.intr.memcpy"(%arg0, %arg1, %0) <{isVolatile = false, tbaa_struct = [#llvm.tbaa_struct_field<offset = 0, size = 4, tag = #tbaa_tag>, #llvm.tbaa_struct_field<offset = 8, size = 4, tag = #tbaa_tag1>]}> : (!llvm.ptr, !llvm.ptr, i64) -> ()
+ "llvm.intr.memmove"(%arg0, %arg1, %0) <{isVolatile = false, tbaa_struct = [#llvm.tbaa_struct_field<offset = 0, size = 4, tag = #tbaa_tag>, #llvm.tbaa_struct_field<offset = 8, size = 4, tag = #tbaa_tag1>]}> : (!llvm.ptr, !llvm.ptr, i64) -> ()
+ "llvm.intr.memcpy.inline"(%arg0, %arg1) <{isVolatile = false, len = 12 : i64, tbaa_struct = [#llvm.tbaa_struct_field<offset = 0, size = 4, tag = #tbaa_tag>, #llvm.tbaa_struct_field<offset = 8, size = 4, tag = #tbaa_tag1>]}> : (!llvm.ptr, !llvm.ptr) -> ()
+ llvm.return
+ }
+}
>From 20c01f9fe8413ae1321a97154a34e62d5d2d465b Mon Sep 17 00:00:00 2001
From: Bruno Cardoso Lopes <bruno.cardoso at gmail.com>
Date: Wed, 4 Nov 2026 16:22:39 -0800
Subject: [PATCH 2/5] Address review: verify tbaa.struct field offsets are
non-decreasing
Add verifiers to the memcpy, memmove, and memcpy.inline ops checking
that tbaa.struct field offsets are non-decreasing, matching LLVM's
verifier rule (unsigned comparison, equal offsets allowed). Read
imported offsets and sizes unsigned so narrow constants keep their
value and high-bit i64 offsets round-trip through the signed
attribute unchanged.
---
.../mlir/Dialect/LLVMIR/LLVMIntrinsicOps.td | 10 +++-
mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp | 30 +++++++++++
.../LLVMIR/LLVMIRToLLVMTranslation.cpp | 19 ++++---
mlir/test/Dialect/LLVMIR/invalid.mlir | 51 +++++++++++++++++++
.../LLVMIR/Import/metadata-tbaa-struct.ll | 36 +++++++++++++
5 files changed, 138 insertions(+), 8 deletions(-)
diff --git a/mlir/include/mlir/Dialect/LLVMIR/LLVMIntrinsicOps.td b/mlir/include/mlir/Dialect/LLVMIR/LLVMIntrinsicOps.td
index 5b418d2529e953..35d8e6ae68ff51 100644
--- a/mlir/include/mlir/Dialect/LLVMIR/LLVMIntrinsicOps.td
+++ b/mlir/include/mlir/Dialect/LLVMIR/LLVMIntrinsicOps.td
@@ -295,8 +295,12 @@ class LLVM_MemcpyIntrOpBase<string name> :
# baseLlvmBuilderArgAndResultAttrs # baseLlvmBuilderCoda;
}
-def LLVM_MemcpyOp : LLVM_MemcpyIntrOpBase<"memcpy">;
-def LLVM_MemmoveOp : LLVM_MemcpyIntrOpBase<"memmove">;
+def LLVM_MemcpyOp : LLVM_MemcpyIntrOpBase<"memcpy"> {
+ let hasVerifier = 1;
+}
+def LLVM_MemmoveOp : LLVM_MemcpyIntrOpBase<"memmove"> {
+ let hasVerifier = 1;
+}
def LLVM_MemcpyInlineOp :
LLVM_ZeroResultIntrOp<"memcpy.inline", [0, 1, 2],
@@ -332,6 +336,8 @@ def LLVM_MemcpyInlineOp :
let llvmBuilder = baseLlvmBuilder # setAccessGroupsMetadataCode
# setAliasAnalysisMetadataCode # setTBAAStructMetadataCode
# baseLlvmBuilderArgAndResultAttrs # baseLlvmBuilderCoda;
+
+ let hasVerifier = 1;
}
def LLVM_MemsetOp : LLVM_ZeroResultIntrOp<"memset", [0, 2],
diff --git a/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp b/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
index ce020fd50a1aeb..414a5916eeff36 100644
--- a/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
+++ b/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
@@ -451,6 +451,36 @@ LogicalResult AllocaOp::verify() {
return success();
}
+// tbaa.struct field offsets must be non-decreasing, matching LLVM's verifier
+// rule. Compare unsigned: LLVM zero-extends the offsets before comparing.
+static LogicalResult verifyTBAAStructFieldOffsets(Operation *op,
+ ArrayAttr fields) {
+ if (!fields)
+ return success();
+ std::optional<uint64_t> prevOffset;
+ for (Attribute field : fields) {
+ uint64_t offset =
+ static_cast<uint64_t>(cast<TBAAStructFieldAttr>(field).getOffset());
+ if (prevOffset && offset < *prevOffset)
+ return op->emitOpError(
+ "tbaa.struct field offsets must be non-decreasing");
+ prevOffset = offset;
+ }
+ return success();
+}
+
+LogicalResult MemcpyOp::verify() {
+ return verifyTBAAStructFieldOffsets(getOperation(), getTbaaStructAttr());
+}
+
+LogicalResult MemmoveOp::verify() {
+ return verifyTBAAStructFieldOffsets(getOperation(), getTbaaStructAttr());
+}
+
+LogicalResult MemcpyInlineOp::verify() {
+ return verifyTBAAStructFieldOffsets(getOperation(), getTbaaStructAttr());
+}
+
LogicalResult AllocaOp::canonicalize(AllocaOp op, PatternRewriter &rewriter) {
// Convert `alloca Ty, C` to the canonical `alloca [C x Ty], 1` form.
APInt numElements;
diff --git a/mlir/lib/Target/LLVMIR/Dialect/LLVMIR/LLVMIRToLLVMTranslation.cpp b/mlir/lib/Target/LLVMIR/Dialect/LLVMIR/LLVMIRToLLVMTranslation.cpp
index 63d32e7a04bd1d..dfc93a544c0a27 100644
--- a/mlir/lib/Target/LLVMIR/Dialect/LLVMIR/LLVMIRToLLVMTranslation.cpp
+++ b/mlir/lib/Target/LLVMIR/Dialect/LLVMIR/LLVMIRToLLVMTranslation.cpp
@@ -238,12 +238,19 @@ static LogicalResult setTBAAStructAttr(OpBuilder &builder,
node->getOperand(i + 1));
if (!offset || !size)
return failure();
- // Offsets and sizes are int64 in the attribute; wider constants cannot be
- // represented, and extracting them would assert.
- std::optional<int64_t> offsetValue = offset->getValue().trySExtValue();
- std::optional<int64_t> sizeValue = size->getValue().trySExtValue();
- if (!offsetValue || !sizeValue)
+ // Offsets and sizes are int64 in the attribute; values that do not fit
+ // in 64 bits cannot be represented, and extracting them would assert.
+ // Read them unsigned and keep the bits: LLVM compares offsets
+ // zero-extended, so a narrow constant with its high bit set (e.g. i32
+ // 0x80000000) must keep its unsigned value rather than sign-extending,
+ // and a high-bit i64 offset must round-trip through the signed
+ // attribute unchanged.
+ std::optional<uint64_t> offsetZExt = offset->getValue().tryZExtValue();
+ std::optional<uint64_t> sizeZExt = size->getValue().tryZExtValue();
+ if (!offsetZExt || !sizeZExt)
return failure();
+ int64_t offsetValue = static_cast<int64_t>(*offsetZExt);
+ int64_t sizeValue = static_cast<int64_t>(*sizeZExt);
auto *tagNode = dyn_cast_if_present<llvm::MDNode>(node->getOperand(i + 2));
if (!tagNode)
return failure();
@@ -252,7 +259,7 @@ static LogicalResult setTBAAStructAttr(OpBuilder &builder,
if (!tag)
return failure();
fields.push_back(
- builder.getAttr<TBAAStructFieldAttr>(*offsetValue, *sizeValue, tag));
+ builder.getAttr<TBAAStructFieldAttr>(offsetValue, sizeValue, tag));
}
return TypeSwitch<Operation *, LogicalResult>(op)
diff --git a/mlir/test/Dialect/LLVMIR/invalid.mlir b/mlir/test/Dialect/LLVMIR/invalid.mlir
index 79c09dea8d301d..ea3c039899bcd9 100644
--- a/mlir/test/Dialect/LLVMIR/invalid.mlir
+++ b/mlir/test/Dialect/LLVMIR/invalid.mlir
@@ -2632,3 +2632,54 @@ llvm.func @nvvm_duplicate_fp_modifier(%a: f32, %b: f32) {
// expected-error at +1 {{failed to parse LoopAnnotationAttr parameter 'align' which is to be a `uint32_t`}}
#loop_align_too_large = #llvm.loop_annotation<align = 4294967296>
+// -----
+
+#tbaa_root = #llvm.tbaa_root<id = "Simple C++ TBAA">
+#tbaa_type_desc = #llvm.tbaa_type_desc<id = "omnipotent char", members = {<#tbaa_root, 0>}>
+#tbaa_type_desc1 = #llvm.tbaa_type_desc<id = "int", members = {<#tbaa_type_desc, 0>}>
+#tbaa_tag = #llvm.tbaa_tag<base_type = #tbaa_type_desc1, access_type = #tbaa_type_desc1, offset = 0>
+llvm.func @tbaa_struct_equal_offsets(%arg0: !llvm.ptr, %arg1: !llvm.ptr) {
+ %0 = llvm.mlir.constant(12 : i64) : i64
+ // Equal offsets are allowed: the rule is non-decreasing, not increasing.
+ "llvm.intr.memcpy"(%arg0, %arg1, %0) <{isVolatile = false, tbaa_struct = [#llvm.tbaa_struct_field<offset = 8, size = 4, tag = #tbaa_tag>, #llvm.tbaa_struct_field<offset = 8, size = 4, tag = #tbaa_tag>]}> : (!llvm.ptr, !llvm.ptr, i64) -> ()
+ llvm.return
+}
+
+// -----
+
+#tbaa_root = #llvm.tbaa_root<id = "Simple C++ TBAA">
+#tbaa_type_desc = #llvm.tbaa_type_desc<id = "omnipotent char", members = {<#tbaa_root, 0>}>
+#tbaa_type_desc1 = #llvm.tbaa_type_desc<id = "int", members = {<#tbaa_type_desc, 0>}>
+#tbaa_tag = #llvm.tbaa_tag<base_type = #tbaa_type_desc1, access_type = #tbaa_type_desc1, offset = 0>
+llvm.func @tbaa_struct_decreasing_memcpy(%arg0: !llvm.ptr, %arg1: !llvm.ptr) {
+ %0 = llvm.mlir.constant(12 : i64) : i64
+ // expected-error at +1 {{tbaa.struct field offsets must be non-decreasing}}
+ "llvm.intr.memcpy"(%arg0, %arg1, %0) <{isVolatile = false, tbaa_struct = [#llvm.tbaa_struct_field<offset = 8, size = 4, tag = #tbaa_tag>, #llvm.tbaa_struct_field<offset = 0, size = 4, tag = #tbaa_tag>]}> : (!llvm.ptr, !llvm.ptr, i64) -> ()
+ llvm.return
+}
+
+// -----
+
+#tbaa_root = #llvm.tbaa_root<id = "Simple C++ TBAA">
+#tbaa_type_desc = #llvm.tbaa_type_desc<id = "omnipotent char", members = {<#tbaa_root, 0>}>
+#tbaa_type_desc1 = #llvm.tbaa_type_desc<id = "int", members = {<#tbaa_type_desc, 0>}>
+#tbaa_tag = #llvm.tbaa_tag<base_type = #tbaa_type_desc1, access_type = #tbaa_type_desc1, offset = 0>
+llvm.func @tbaa_struct_decreasing_memmove(%arg0: !llvm.ptr, %arg1: !llvm.ptr) {
+ %0 = llvm.mlir.constant(12 : i64) : i64
+ // expected-error at +1 {{tbaa.struct field offsets must be non-decreasing}}
+ "llvm.intr.memmove"(%arg0, %arg1, %0) <{isVolatile = false, tbaa_struct = [#llvm.tbaa_struct_field<offset = 8, size = 4, tag = #tbaa_tag>, #llvm.tbaa_struct_field<offset = 0, size = 4, tag = #tbaa_tag>]}> : (!llvm.ptr, !llvm.ptr, i64) -> ()
+ llvm.return
+}
+
+// -----
+
+#tbaa_root = #llvm.tbaa_root<id = "Simple C++ TBAA">
+#tbaa_type_desc = #llvm.tbaa_type_desc<id = "omnipotent char", members = {<#tbaa_root, 0>}>
+#tbaa_type_desc1 = #llvm.tbaa_type_desc<id = "int", members = {<#tbaa_type_desc, 0>}>
+#tbaa_tag = #llvm.tbaa_tag<base_type = #tbaa_type_desc1, access_type = #tbaa_type_desc1, offset = 0>
+llvm.func @tbaa_struct_decreasing_memcpy_inline(%arg0: !llvm.ptr, %arg1: !llvm.ptr) {
+ // expected-error at +1 {{tbaa.struct field offsets must be non-decreasing}}
+ "llvm.intr.memcpy.inline"(%arg0, %arg1) <{isVolatile = false, len = 12 : i64, tbaa_struct = [#llvm.tbaa_struct_field<offset = 8, size = 4, tag = #tbaa_tag>, #llvm.tbaa_struct_field<offset = 0, size = 4, tag = #tbaa_tag>]}> : (!llvm.ptr, !llvm.ptr) -> ()
+ llvm.return
+}
+
diff --git a/mlir/test/Target/LLVMIR/Import/metadata-tbaa-struct.ll b/mlir/test/Target/LLVMIR/Import/metadata-tbaa-struct.ll
index 728168dbcc128f..89a753009d1731 100644
--- a/mlir/test/Target/LLVMIR/Import/metadata-tbaa-struct.ll
+++ b/mlir/test/Target/LLVMIR/Import/metadata-tbaa-struct.ll
@@ -18,6 +18,9 @@ declare void @llvm.memcpy.inline.p0.p0.i64(ptr, ptr, i64, i1)
; CHECK-DAG: #[[TF:.*]] = #llvm.tbaa_tag<base_type = #[[FLT]], access_type = #[[FLT]], offset = 0>
; CHECK-DAG: #[[F0:.*]] = #llvm.tbaa_struct_field<offset = 0, size = 4, tag = #[[TI]]>
; CHECK-DAG: #[[F1:.*]] = #llvm.tbaa_struct_field<offset = 8, size = 4, tag = #[[TF]]>
+; CHECK-DAG: #[[F2:.*]] = #llvm.tbaa_struct_field<offset = 2147483648, size = 4, tag = #[[TI]]>
+; CHECK-DAG: #[[F3:.*]] = #llvm.tbaa_struct_field<offset = -1, size = 4, tag = #[[TI]]>
+; CHECK-DAG: #[[F4:.*]] = #llvm.tbaa_struct_field<offset = 0, size = 2147483648, tag = #[[TI]]>
; CHECK: llvm.intr.memcpy
; CHECK-SAME: tbaa_struct = [#[[F0]], #[[F1]]]
; CHECK: llvm.intr.memmove
@@ -38,3 +41,36 @@ define void @cp(ptr %d, ptr %s) {
!6 = !{!1, !1, i64 0}
!7 = !{!2, !2, i64 0}
!4 = !{ i64 0, i64 4, !6, i64 8, i64 4, !7 }
+
+; A narrow offset constant with its high bit set keeps its unsigned value:
+; LLVM compares offsets zero-extended, so i32 0x80000000 is 2147483648, not
+; a negative offset.
+; CHECK: llvm.intr.memcpy
+; CHECK-SAME: tbaa_struct = [#[[F2]]]
+define void @narrow_offset(ptr %d, ptr %s) {
+ call void @llvm.memcpy.p0.p0.i64(ptr %d, ptr %s, i64 12, i1 false), !tbaa.struct !8
+ ret void
+}
+
+!8 = !{ i32 2147483648, i64 4, !6 }
+
+; A high-bit i64 offset keeps its bits through the signed attribute; it is
+; not dropped as unrepresentable.
+; CHECK: llvm.intr.memcpy
+; CHECK-SAME: tbaa_struct = [#[[F3]]]
+define void @high_bit_offset(ptr %d, ptr %s) {
+ call void @llvm.memcpy.p0.p0.i64(ptr %d, ptr %s, i64 12, i1 false), !tbaa.struct !9
+ ret void
+}
+
+!9 = !{ i64 -1, i64 4, !6 }
+
+; A narrow size constant with its high bit set keeps its unsigned value too.
+; CHECK: llvm.intr.memcpy
+; CHECK-SAME: tbaa_struct = [#[[F4]]]
+define void @narrow_size(ptr %d, ptr %s) {
+ call void @llvm.memcpy.p0.p0.i64(ptr %d, ptr %s, i64 12, i1 false), !tbaa.struct !10
+ ret void
+}
+
+!10 = !{ i64 0, i32 2147483648, !6 }
>From fd8cddff38f21b55050340e0b0b3128922063857 Mon Sep 17 00:00:00 2001
From: Bruno Cardoso Lopes <bruno.cardoso at gmail.com>
Date: Wed, 4 Nov 2026 16:22:40 -0800
Subject: [PATCH 3/5] Fix CI: pass tbaa_struct to CIR memcpy builders
The tbaa_struct argument added to the copy ops shifted the
positional builder arguments. Pass nullptr for it at the two
full-argument call sites so the build matches the new signatures.
---
clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
index a48dbd04de84ac..e681890a05e249 100644
--- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
+++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
@@ -401,9 +401,9 @@ mlir::LogicalResult CIRToLLVMCopyOpLowering::matchAndRewrite(
rewriter.replaceOpWithNewOp<mlir::LLVM::MemcpyOp>(
op, adaptor.getDst(), adaptor.getSrc(), length, op.getIsVolatile(),
- /*access_groups=*/nullptr, /*alias_scopes=*/nullptr,
- /*noalias_scopes=*/nullptr, /*tbaa=*/nullptr, /*arg_attrs=*/argAttrs,
- /*res_attrs=*/nullptr);
+ /*tbaa_struct=*/nullptr, /*access_groups=*/nullptr,
+ /*alias_scopes=*/nullptr, /*noalias_scopes=*/nullptr, /*tbaa=*/nullptr,
+ /*arg_attrs=*/argAttrs, /*res_attrs=*/nullptr);
return mlir::success();
}
@@ -426,7 +426,7 @@ lowerMemTransferOp(CIROp op, typename CIROp::Adaptor adaptor,
}
rewriter.replaceOpWithNewOp<LLVMOp>(
op, adaptor.getDst(), adaptor.getSrc(), adaptor.getLen(),
- /*isVolatile=*/false,
+ /*isVolatile=*/false, /*tbaa_struct=*/nullptr,
/*access_groups=*/nullptr, /*alias_scopes=*/nullptr,
/*noalias_scopes=*/nullptr, /*tbaa=*/nullptr, /*arg_attrs=*/argAttrs,
/*res_attrs=*/nullptr);
>From 2d9cfabc1c3e27cde5f7fd1d546fd16089804682 Mon Sep 17 00:00:00 2001
From: Bruno Cardoso Lopes <bruno.cardoso at gmail.com>
Date: Wed, 4 Nov 2026 19:54:01 -0800
Subject: [PATCH 4/5] Address review: tbaa_struct via the baseArgs flag
Move the tbaa_struct argument from the per-op argument lists into a
new flag-gated baseArgs group (requiresTBAAStruct) in
LLVM_IntrOpBase, threaded through LLVM_IntrOp and
LLVM_ZeroResultIntrOp and enabled only for the memcpy, memmove, and
memcpy.inline ops. The argument now sits next to tbaa in the builder
signatures instead of hand-rolled per op; full-argument call sites
in TableGen builders, the inliner, and CIR lowering follow the new
order.
---
.../CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp | 11 ++++-----
.../mlir/Dialect/LLVMIR/LLVMIntrinsicOps.td | 18 +++++++-------
.../include/mlir/Dialect/LLVMIR/LLVMOpBase.td | 24 ++++++++++++++-----
.../Transforms/InlinerInterfaceImpl.cpp | 4 ++--
4 files changed, 33 insertions(+), 24 deletions(-)
diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
index e681890a05e249..0ce08711116f1b 100644
--- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
+++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
@@ -401,8 +401,8 @@ mlir::LogicalResult CIRToLLVMCopyOpLowering::matchAndRewrite(
rewriter.replaceOpWithNewOp<mlir::LLVM::MemcpyOp>(
op, adaptor.getDst(), adaptor.getSrc(), length, op.getIsVolatile(),
- /*tbaa_struct=*/nullptr, /*access_groups=*/nullptr,
- /*alias_scopes=*/nullptr, /*noalias_scopes=*/nullptr, /*tbaa=*/nullptr,
+ /*access_groups=*/nullptr, /*alias_scopes=*/nullptr,
+ /*noalias_scopes=*/nullptr, /*tbaa=*/nullptr, /*tbaa_struct=*/nullptr,
/*arg_attrs=*/argAttrs, /*res_attrs=*/nullptr);
return mlir::success();
}
@@ -426,10 +426,9 @@ lowerMemTransferOp(CIROp op, typename CIROp::Adaptor adaptor,
}
rewriter.replaceOpWithNewOp<LLVMOp>(
op, adaptor.getDst(), adaptor.getSrc(), adaptor.getLen(),
- /*isVolatile=*/false, /*tbaa_struct=*/nullptr,
- /*access_groups=*/nullptr, /*alias_scopes=*/nullptr,
- /*noalias_scopes=*/nullptr, /*tbaa=*/nullptr, /*arg_attrs=*/argAttrs,
- /*res_attrs=*/nullptr);
+ /*isVolatile=*/false, /*access_groups=*/nullptr,
+ /*alias_scopes=*/nullptr, /*noalias_scopes=*/nullptr, /*tbaa=*/nullptr,
+ /*tbaa_struct=*/nullptr, /*arg_attrs=*/argAttrs, /*res_attrs=*/nullptr);
return mlir::success();
}
diff --git a/mlir/include/mlir/Dialect/LLVMIR/LLVMIntrinsicOps.td b/mlir/include/mlir/Dialect/LLVMIR/LLVMIntrinsicOps.td
index 35d8e6ae68ff51..ea67e1ef258142 100644
--- a/mlir/include/mlir/Dialect/LLVMIR/LLVMIntrinsicOps.td
+++ b/mlir/include/mlir/Dialect/LLVMIR/LLVMIntrinsicOps.td
@@ -262,13 +262,11 @@ class LLVM_MemcpyIntrOpBase<string name> :
DeclareOpInterfaceMethods<SafeMemorySlotAccessOpInterface>],
/*requiresAccessGroup=*/1, /*requiresAliasAnalysis=*/1,
/*requiresArgAndResultAttrs=*/1, /*requiresOpBundles=*/0,
- /*immArgPositions=*/[3], /*immArgAttrNames=*/["isVolatile"]> {
+ /*immArgPositions=*/[3], /*immArgAttrNames=*/["isVolatile"],
+ /*requiresTBAAStruct=*/1> {
dag args = (ins Arg<LLVM_AnyPointer,"",[MemWrite]>:$dst,
Arg<LLVM_AnyPointer,"",[MemRead]>:$src,
- AnySignlessInteger:$len, I1Attr:$isVolatile,
- // `!tbaa.struct`: which fields of the aggregate the copy
- // touches, so it is not treated as one opaque access.
- OptionalAttr<LLVM_TBAAStructFieldArrayAttr>:$tbaa_struct);
+ AnySignlessInteger:$len, I1Attr:$isVolatile);
// Append the arguments defined by LLVM_IntrOpBase.
let arguments = !con(args, baseArgs);
let builders = [
@@ -280,9 +278,9 @@ class LLVM_MemcpyIntrOpBase<string name> :
OpBuilder<(ins "Value":$dst, "Value":$src, "Value":$len,
"IntegerAttr":$isVolatile), [{
build($_builder, $_state, dst, src, len, isVolatile,
- /*tbaa_struct=*/nullptr,
/*access_groups=*/nullptr, /*alias_scopes=*/nullptr,
/*noalias_scopes=*/nullptr, /*tbaa=*/nullptr,
+ /*tbaa_struct=*/nullptr,
/*arg_attrs=*/nullptr, /*res_attrs=*/nullptr);
}]>
];
@@ -309,11 +307,11 @@ def LLVM_MemcpyInlineOp :
DeclareOpInterfaceMethods<SafeMemorySlotAccessOpInterface>],
/*requiresAccessGroup=*/1, /*requiresAliasAnalysis=*/1,
/*requiresArgAndResultAttrs=*/1, /*requiresOpBundles=*/0,
- /*immArgPositions=*/[2, 3], /*immArgAttrNames=*/["len", "isVolatile"]> {
+ /*immArgPositions=*/[2, 3], /*immArgAttrNames=*/["len", "isVolatile"],
+ /*requiresTBAAStruct=*/1> {
dag args = (ins Arg<LLVM_AnyPointer,"",[MemWrite]>:$dst,
Arg<LLVM_AnyPointer,"",[MemRead]>:$src,
- APIntAttr:$len, I1Attr:$isVolatile,
- OptionalAttr<LLVM_TBAAStructFieldArrayAttr>:$tbaa_struct);
+ APIntAttr:$len, I1Attr:$isVolatile);
// Append the arguments defined by LLVM_IntrOpBase.
let arguments = !con(args, baseArgs);
let builders = [
@@ -325,9 +323,9 @@ def LLVM_MemcpyInlineOp :
OpBuilder<(ins "Value":$dst, "Value":$src, "IntegerAttr":$len,
"IntegerAttr":$isVolatile), [{
build($_builder, $_state, dst, src, len, isVolatile,
- /*tbaa_struct=*/nullptr,
/*access_groups=*/nullptr, /*alias_scopes=*/nullptr,
/*noalias_scopes=*/nullptr, /*tbaa=*/nullptr,
+ /*tbaa_struct=*/nullptr,
/*arg_attrs=*/nullptr, /*res_attrs=*/nullptr);
}]>
];
diff --git a/mlir/include/mlir/Dialect/LLVMIR/LLVMOpBase.td b/mlir/include/mlir/Dialect/LLVMIR/LLVMOpBase.td
index 66583a592b3f64..138a840fce7628 100644
--- a/mlir/include/mlir/Dialect/LLVMIR/LLVMOpBase.td
+++ b/mlir/include/mlir/Dialect/LLVMIR/LLVMOpBase.td
@@ -310,7 +310,11 @@ class LLVM_MemAccessOpBase<string mnemonic, list<Trait> traits = []> :
// interfaces the intrinsic implements. When a flag is set, the "baseArgs"
// list includes the arguments required by the corresponding interface.
// Derived intrinsics must append "baseArgs" to their argument list if they
-// enable any of these flags.
+// enable any of these flags. "requiresTBAAStruct" is not tied to an
+// interface: it adds the tbaa_struct argument after tbaa (when present),
+// for the aggregate-copy intrinsics only. It only wires the argument;
+// the export, import, and verifier sites name those three ops directly,
+// so a future op setting the flag also needs handling there.
//
// LLVM `immargs` can be represented as MLIR attributes by providing both
// the `immArgPositions` and `immArgAttrNames` lists. These two lists should
@@ -324,7 +328,8 @@ class LLVM_IntrOpBase<Dialect dialect, string opName, string enumName,
bit requiresFastmath = 0, bit requiresArgAndResultAttrs = 0,
bit requiresOpBundles = 0,
list<int> immArgPositions = [],
- list<string> immArgAttrNames = []>
+ list<string> immArgAttrNames = [],
+ bit requiresTBAAStruct = 0>
: LLVM_OpBase<dialect, opName, !listconcat(
!if(!gt(requiresAccessGroup, 0),
[DeclareOpInterfaceMethods<AccessGroupOpInterface>], []),
@@ -346,6 +351,9 @@ class LLVM_IntrOpBase<Dialect dialect, string opName, string enumName,
OptionalAttr<LLVM_AliasScopeArrayAttr>:$noalias_scopes,
OptionalAttr<LLVM_TBAATagArrayAttr>:$tbaa),
(ins )),
+ !if(!gt(requiresTBAAStruct, 0),
+ (ins OptionalAttr<LLVM_TBAAStructFieldArrayAttr>:$tbaa_struct),
+ (ins )),
!if(!gt(requiresArgAndResultAttrs, 0),
(ins OptionalAttr<DictArrayAttr>:$arg_attrs,
OptionalAttr<DictArrayAttr>:$res_attrs),
@@ -456,12 +464,14 @@ class LLVM_IntrOp<string mnem, list<int> overloadedResults,
bit requiresAliasAnalysis = 0, bit requiresFastmath = 0,
bit requiresArgAndResultAttrs = 0, bit requiresOpBundles = 0,
list<int> immArgPositions = [],
- list<string> immArgAttrNames = []>
+ list<string> immArgAttrNames = [],
+ bit requiresTBAAStruct = 0>
: LLVM_IntrOpBase<LLVM_Dialect, "intr." # mnem, !subst(".", "_", mnem),
overloadedResults, overloadedOperands, traits,
numResults, requiresAccessGroup, requiresAliasAnalysis,
requiresFastmath, requiresArgAndResultAttrs,
- requiresOpBundles, immArgPositions, immArgAttrNames>;
+ requiresOpBundles, immArgPositions, immArgAttrNames,
+ requiresTBAAStruct>;
// Base class for LLVM intrinsic operations returning no results. Places the
// intrinsic into the LLVM dialect and prefixes its name with "intr.".
@@ -484,11 +494,13 @@ class LLVM_ZeroResultIntrOp<string mnem, list<int> overloadedOperands = [],
bit requiresArgAndResultAttrs = 0,
bit requiresOpBundles = 0,
list<int> immArgPositions = [],
- list<string> immArgAttrNames = []>
+ list<string> immArgAttrNames = [],
+ bit requiresTBAAStruct = 0>
: LLVM_IntrOp<mnem, [], overloadedOperands, traits, /*numResults=*/0,
requiresAccessGroup, requiresAliasAnalysis,
/*requiresFastMath=*/0, requiresArgAndResultAttrs,
- requiresOpBundles, immArgPositions, immArgAttrNames>;
+ requiresOpBundles, immArgPositions, immArgAttrNames,
+ requiresTBAAStruct>;
// Base class for LLVM intrinsic operations returning one result. Places the
// intrinsic into the LLVM dialect and prefixes its name with "intr.". This is
diff --git a/mlir/lib/Dialect/LLVMIR/Transforms/InlinerInterfaceImpl.cpp b/mlir/lib/Dialect/LLVMIR/Transforms/InlinerInterfaceImpl.cpp
index c824cf6ed41d40..5af3706c3b2660 100644
--- a/mlir/lib/Dialect/LLVMIR/Transforms/InlinerInterfaceImpl.cpp
+++ b/mlir/lib/Dialect/LLVMIR/Transforms/InlinerInterfaceImpl.cpp
@@ -630,9 +630,9 @@ static Value handleByValArgumentInit(OpBuilder &builder, Location loc,
builder.getArrayAttr({builder.getDictionaryAttr({dstAlignAttr})});
LLVM::MemcpyOp::create(builder, loc, allocaOp, argument, copySize,
/*isVolatile=*/false,
- /*tbaa_struct=*/nullptr,
/*access_groups=*/nullptr, /*alias_scopes=*/nullptr,
- /*noalias_scopes=*/nullptr, /*tbaa=*/nullptr, argAttrs,
+ /*noalias_scopes=*/nullptr, /*tbaa=*/nullptr,
+ /*tbaa_struct=*/nullptr, argAttrs,
/*res_attrs=*/nullptr);
return allocaOp;
}
>From fe639fffea4c2720f3c778d03b1d0f7a05d1c847 Mon Sep 17 00:00:00 2001
From: Bruno Cardoso Lopes <bruno.cardoso at gmail.com>
Date: Thu, 5 Nov 2026 17:55:04 -0800
Subject: [PATCH 5/5] Address review: hoist tbaa.struct conversion, use LLVM
helper
Convert !tbaa.struct nodes once in convertMetadata (after the tag
pre-walk) and cache the result in a separate tbaaStructMapping;
setTBAAStructAttr is now mostly a lookup, like setTBAAAttr for
access tags. A null value records nodes that could not be modeled
so they are declined at attach time without reconverting. The
separate map keeps struct nodes from being mistaken for TBAA tag
or descriptor nodes. Export builds the node with
MDBuilder::createTBAAStructNode.
---
.../include/mlir/Target/LLVMIR/ModuleImport.h | 18 ++++++
.../LLVMIR/LLVMIRToLLVMTranslation.cpp | 56 ++++---------------
mlir/lib/Target/LLVMIR/ModuleImport.cpp | 51 ++++++++++++++++-
mlir/lib/Target/LLVMIR/ModuleTranslation.cpp | 19 +++----
4 files changed, 85 insertions(+), 59 deletions(-)
diff --git a/mlir/include/mlir/Target/LLVMIR/ModuleImport.h b/mlir/include/mlir/Target/LLVMIR/ModuleImport.h
index ab46245b040d09..e9eaf2e78e737a 100644
--- a/mlir/include/mlir/Target/LLVMIR/ModuleImport.h
+++ b/mlir/include/mlir/Target/LLVMIR/ModuleImport.h
@@ -257,6 +257,12 @@ class ModuleImport {
return tbaaMapping.lookup(node);
}
+ /// Looks up a converted `!tbaa.struct` node. Returns null when the
+ /// node was never converted or could not be modeled.
+ Attribute lookupTBAAStructAttr(const llvm::MDNode *node) const {
+ return tbaaStructMapping.lookup(node);
+ }
+
/// Returns the access group attributes that map to the access group nodes
/// starting from the access group metadata `node`. Returns failure, if any of
/// the attributes cannot be found.
@@ -471,6 +477,12 @@ class ModuleImport {
/// (unless all dependencies have been converted by a previous
/// invocation of this function).
LogicalResult processTBAAMetadata(const llvm::MDNode *node);
+ /// Converts a `!tbaa.struct` node to the `tbaa_struct` attribute. The
+ /// node's tags must already be converted (see the pre-walk in
+ /// convertMetadata). Returns null for nodes that cannot be modeled;
+ /// callers record the null in tbaaStructMapping so the node is
+ /// declined at attach time.
+ Attribute convertTBAAStructMetadata(const llvm::MDNode *node);
/// Converts all LLVM access groups starting from `node` to MLIR access group
/// operations and stores a mapping from every nested access group node to the
/// translated attribute. Returns success if all conversions succeed and
@@ -539,6 +551,12 @@ class ModuleImport {
/// Mapping between LLVM TBAA metadata nodes and LLVM dialect TBAA attributes
/// corresponding to these nodes.
DenseMap<const llvm::MDNode *, Attribute> tbaaMapping;
+ /// Mapping between LLVM `!tbaa.struct` nodes and the converted
+ /// `tbaa_struct` attributes. Kept separate from tbaaMapping so a struct
+ /// node can never be mistaken for a TBAA tag or type descriptor node,
+ /// which processTBAAMetadata casts to those attribute kinds. A null
+ /// value records that the node was converted and could not be modeled.
+ DenseMap<const llvm::MDNode *, Attribute> tbaaStructMapping;
/// Mapping between LLVM comdat structs and symbol references to LLVM dialect
/// comdat selector operations corresponding to these structs.
DenseMap<const llvm::Comdat *, SymbolRefAttr> comdatMapping;
diff --git a/mlir/lib/Target/LLVMIR/Dialect/LLVMIR/LLVMIRToLLVMTranslation.cpp b/mlir/lib/Target/LLVMIR/Dialect/LLVMIR/LLVMIRToLLVMTranslation.cpp
index dfc93a544c0a27..f0947a69f6012b 100644
--- a/mlir/lib/Target/LLVMIR/Dialect/LLVMIR/LLVMIRToLLVMTranslation.cpp
+++ b/mlir/lib/Target/LLVMIR/Dialect/LLVMIR/LLVMIRToLLVMTranslation.cpp
@@ -215,57 +215,21 @@ static LogicalResult setTBAAAttr(const llvm::MDNode *node, Operation *op,
return success();
}
-/// Converts `!tbaa.struct` to the `tbaa_struct` attribute on the imported
-/// aggregate copy. The node is a flat list of (offset, size, tag) triples: it
-/// says which fields of the aggregate the copy actually touches, so the copy is
-/// not treated as one opaque access and the gaps are known to be padding.
-static LogicalResult setTBAAStructAttr(OpBuilder &builder,
- const llvm::MDNode *node, Operation *op,
+/// Attaches the `tbaa_struct` attribute converted from `!tbaa.struct` by
+/// ModuleImport::convertTBAAStructMetadata to the imported aggregate copy,
+/// like setTBAAAttr does for access tags. Nodes that could not be modeled
+/// are unmapped and declined here.
+static LogicalResult setTBAAStructAttr(const llvm::MDNode *node, Operation *op,
LLVM::ModuleImport &moduleImport) {
- if (!isa<LLVM::MemcpyOp, LLVM::MemcpyInlineOp, LLVM::MemmoveOp>(op))
+ auto fields =
+ dyn_cast_if_present<ArrayAttr>(moduleImport.lookupTBAAStructAttr(node));
+ if (!fields)
return failure();
- // Operands come in conceptual groups of three; anything else is malformed.
- if (node->getNumOperands() == 0 || node->getNumOperands() % 3 != 0)
- return failure();
-
- SmallVector<Attribute> fields;
- fields.reserve(node->getNumOperands() / 3);
- for (unsigned i = 0, e = node->getNumOperands(); i != e; i += 3) {
- auto *offset = llvm::mdconst::dyn_extract_or_null<llvm::ConstantInt>(
- node->getOperand(i));
- auto *size = llvm::mdconst::dyn_extract_or_null<llvm::ConstantInt>(
- node->getOperand(i + 1));
- if (!offset || !size)
- return failure();
- // Offsets and sizes are int64 in the attribute; values that do not fit
- // in 64 bits cannot be represented, and extracting them would assert.
- // Read them unsigned and keep the bits: LLVM compares offsets
- // zero-extended, so a narrow constant with its high bit set (e.g. i32
- // 0x80000000) must keep its unsigned value rather than sign-extending,
- // and a high-bit i64 offset must round-trip through the signed
- // attribute unchanged.
- std::optional<uint64_t> offsetZExt = offset->getValue().tryZExtValue();
- std::optional<uint64_t> sizeZExt = size->getValue().tryZExtValue();
- if (!offsetZExt || !sizeZExt)
- return failure();
- int64_t offsetValue = static_cast<int64_t>(*offsetZExt);
- int64_t sizeValue = static_cast<int64_t>(*sizeZExt);
- auto *tagNode = dyn_cast_if_present<llvm::MDNode>(node->getOperand(i + 2));
- if (!tagNode)
- return failure();
- auto tag =
- dyn_cast_if_present<TBAATagAttr>(moduleImport.lookupTBAAAttr(tagNode));
- if (!tag)
- return failure();
- fields.push_back(
- builder.getAttr<TBAAStructFieldAttr>(offsetValue, sizeValue, tag));
- }
-
return TypeSwitch<Operation *, LogicalResult>(op)
.Case<LLVM::MemcpyOp, LLVM::MemcpyInlineOp, LLVM::MemmoveOp>(
[&](auto copyOp) {
- copyOp.setTbaaStructAttr(builder.getArrayAttr(fields));
+ copyOp.setTbaaStructAttr(fields);
return success();
})
.Default([](auto) { return failure(); });
@@ -662,7 +626,7 @@ class LLVMDialectLLVMIRImportInterface : public LLVMImportDialectInterface {
if (kind == llvm::LLVMContext::MD_callsite)
return setCallsiteAttr(builder, node, op);
if (kind == llvm::LLVMContext::MD_tbaa_struct)
- return setTBAAStructAttr(builder, node, op, moduleImport);
+ return setTBAAStructAttr(node, op, moduleImport);
llvm::LLVMContext &context = node->getContext();
if (kind == context.getMDKindID(vecTypeHintMDName))
return setVecTypeHintAttr(builder, node, op, moduleImport);
diff --git a/mlir/lib/Target/LLVMIR/ModuleImport.cpp b/mlir/lib/Target/LLVMIR/ModuleImport.cpp
index 051b67cbf3f797..69499e83f34591 100644
--- a/mlir/lib/Target/LLVMIR/ModuleImport.cpp
+++ b/mlir/lib/Target/LLVMIR/ModuleImport.cpp
@@ -575,6 +575,46 @@ LogicalResult ModuleImport::processTBAAMetadata(const llvm::MDNode *node) {
return success();
}
+Attribute ModuleImport::convertTBAAStructMetadata(const llvm::MDNode *node) {
+ // Operands come in conceptual groups of three; anything else is malformed.
+ if (node->getNumOperands() == 0 || node->getNumOperands() % 3 != 0)
+ return {};
+
+ SmallVector<Attribute> fields;
+ fields.reserve(node->getNumOperands() / 3);
+ for (unsigned i = 0, e = node->getNumOperands(); i != e; i += 3) {
+ auto *offset = llvm::mdconst::dyn_extract_or_null<llvm::ConstantInt>(
+ node->getOperand(i));
+ auto *size = llvm::mdconst::dyn_extract_or_null<llvm::ConstantInt>(
+ node->getOperand(i + 1));
+ if (!offset || !size)
+ return {};
+ // Offsets and sizes are int64 in the attribute; values that do not fit
+ // in 64 bits cannot be represented, and extracting them would assert.
+ // Read them unsigned and keep the bits: LLVM compares offsets
+ // zero-extended, so a narrow constant with its high bit set (e.g. i32
+ // 0x80000000) must keep its unsigned value rather than sign-extending,
+ // and a high-bit i64 offset must round-trip through the signed
+ // attribute unchanged.
+ std::optional<uint64_t> offsetZExt = offset->getValue().tryZExtValue();
+ std::optional<uint64_t> sizeZExt = size->getValue().tryZExtValue();
+ if (!offsetZExt || !sizeZExt)
+ return {};
+ int64_t offsetValue = static_cast<int64_t>(*offsetZExt);
+ int64_t sizeValue = static_cast<int64_t>(*sizeZExt);
+ auto *tagNode = dyn_cast_if_present<llvm::MDNode>(node->getOperand(i + 2));
+ if (!tagNode)
+ return {};
+ auto tag = dyn_cast_if_present<TBAATagAttr>(lookupTBAAAttr(tagNode));
+ if (!tag)
+ return {};
+ fields.push_back(
+ builder.getAttr<TBAAStructFieldAttr>(offsetValue, sizeValue, tag));
+ }
+
+ return builder.getArrayAttr(fields);
+}
+
LogicalResult
ModuleImport::processAccessGroupMetadata(const llvm::MDNode *node) {
Location loc = mlirModule.getLoc();
@@ -1171,8 +1211,8 @@ LogicalResult ModuleImport::convertMetadata() {
if (failed(processTBAAMetadata(aliasAnalysisNodes.TBAA)))
return failure();
// A `!tbaa.struct` node reaches its own tags, and those tags may appear
- // nowhere else in the module. Without walking them here the lookup in
- // setTBAAStructAttr finds nothing and the node is declined.
+ // nowhere else in the module. Without walking them here the struct
+ // conversion below finds nothing and the node is declined.
if (aliasAnalysisNodes.TBAAStruct) {
const llvm::MDNode *structNode = aliasAnalysisNodes.TBAAStruct;
for (unsigned i = 2, e = structNode->getNumOperands(); i < e; i += 3)
@@ -1180,6 +1220,13 @@ LogicalResult ModuleImport::convertMetadata() {
dyn_cast_if_present<llvm::MDNode>(structNode->getOperand(i)))
if (failed(processTBAAMetadata(tag)))
return failure();
+ // Convert the struct node itself once its tags are in the map. A
+ // node that cannot be modeled is recorded with a null value so it
+ // is not converted again for every instruction carrying it, and is
+ // declined at attach time.
+ if (!tbaaStructMapping.contains(structNode))
+ tbaaStructMapping.insert(
+ {structNode, convertTBAAStructMetadata(structNode)});
}
if (aliasAnalysisNodes.Scope)
if (failed(processAliasScopeMetadata(aliasAnalysisNodes.Scope)))
diff --git a/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp b/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
index 1428ce906860de..130f2cad5719b3 100644
--- a/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
+++ b/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
@@ -2425,20 +2425,17 @@ void ModuleTranslation::setTBAAStructMetadata(ArrayAttr fields,
// The node is a flat list of (offset, size, tag) triples, not a list of
// per-field nodes.
- llvm::LLVMContext &ctx = getLLVMContext();
- llvm::Type *i64 = llvm::Type::getInt64Ty(ctx);
- SmallVector<llvm::Metadata *> operands;
- operands.reserve(fields.size() * 3);
+ SmallVector<llvm::MDBuilder::TBAAStructField> structFields;
+ structFields.reserve(fields.size());
for (Attribute field : fields) {
auto fieldAttr = cast<TBAAStructFieldAttr>(field);
- operands.push_back(llvm::ConstantAsMetadata::get(
- llvm::ConstantInt::getSigned(i64, fieldAttr.getOffset())));
- operands.push_back(llvm::ConstantAsMetadata::get(
- llvm::ConstantInt::getSigned(i64, fieldAttr.getSize())));
- operands.push_back(getTBAANode(fieldAttr.getTag()));
+ structFields.emplace_back(static_cast<uint64_t>(fieldAttr.getOffset()),
+ static_cast<uint64_t>(fieldAttr.getSize()),
+ getTBAANode(fieldAttr.getTag()));
}
- inst->setMetadata(llvm::LLVMContext::MD_tbaa_struct,
- llvm::MDNode::get(ctx, operands));
+ inst->setMetadata(
+ llvm::LLVMContext::MD_tbaa_struct,
+ llvm::MDBuilder(getLLVMContext()).createTBAAStructNode(structFields));
}
void ModuleTranslation::setDereferenceableMetadata(
More information about the cfe-commits
mailing list