[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
Wed Nov 4 19:54:24 PST 2026


https://github.com/bcardosolopes updated https://github.com/llvm/llvm-project/pull/238991

>From 38cfcd7d224b9d07deb8aad2eb18d6cefcfddfc2 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/4] [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    | 36 +++++++++++++
 .../LLVMIR/Import/metadata-tbaa-struct.ll     | 40 ++++++++++++++
 .../Target/LLVMIR/tbaa-struct-metadata.mlir   | 34 ++++++++++++
 11 files changed, 276 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 4e890aa4ae4cb5..39f6a8811dc1b1 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 f0b31a1c661b4c..7dcd546a8b4d39 100644
--- a/mlir/lib/Target/LLVMIR/Dialect/LLVMIR/LLVMIRToLLVMTranslation.cpp
+++ b/mlir/lib/Target/LLVMIR/Dialect/LLVMIR/LLVMIRToLLVMTranslation.cpp
@@ -95,6 +95,7 @@ getSupportedMetadataImpl(llvm::LLVMContext &llvmContext) {
       llvm::LLVMContext::MD_dereferenceable_or_null,
       llvm::LLVMContext::MD_range,
       llvm::LLVMContext::MD_mmra,
+      llvm::LLVMContext::MD_tbaa_struct,
       llvmContext.getMDKindID(vecTypeHintMDName),
       llvmContext.getMDKindID(workGroupSizeHintMDName),
       llvmContext.getMDKindID(reqdWorkGroupSizeMDName),
@@ -212,6 +213,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.
@@ -520,6 +570,8 @@ class LLVMDialectLLVMIRImportInterface : public LLVMImportDialectInterface {
       return setRangeAttr(node, op);
     if (kind == llvm::LLVMContext::MD_mmra)
       return setMmraAttr(node, op, moduleImport);
+    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 ef16a0c9bb6c04..3cd23c15f44ada 100644
--- a/mlir/test/Target/LLVMIR/Import/import-failure.ll
+++ b/mlir/test/Target/LLVMIR/Import/import-failure.ll
@@ -646,3 +646,39 @@ define void @reuse_b(ptr %q) {
 
 !0 = distinct !{!0, i32 42}
 !1 = distinct !{!1, i32 43}
+
+; // -----
+
+; 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 4e2d02d849351f6a8bf6074c641f4868fece2810 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/4] 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 7dcd546a8b4d39..c6b288a40efd9b 100644
--- a/mlir/lib/Target/LLVMIR/Dialect/LLVMIR/LLVMIRToLLVMTranslation.cpp
+++ b/mlir/lib/Target/LLVMIR/Dialect/LLVMIR/LLVMIRToLLVMTranslation.cpp
@@ -236,12 +236,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();
@@ -250,7 +257,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 25cbac837323b878add884b59e8f6558ca4f2c3f 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/4] 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 11616ae6037a999a34521fc9a8d8ead33b0e491c 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/4] 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;
 }



More information about the cfe-commits mailing list