[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 20:03:25 PST 2026


================
@@ -212,6 +213,62 @@ 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; 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();
----------------
bcardosolopes wrote:

Kept the uint64_t form: tryZExtValue already carries the APInt semantics (declines values that don't fit in 64 bits, preserves the unsigned value), and the cast keeps the bits for the signed attribute, so the APInt version ends up the same checks plus a temporary. The narrow, high-bit i64, and wide cases are pinned by tests in metadata-tbaa-struct.ll. Happy to switch to the APInt form if you'd prefer that shape though.

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


More information about the cfe-commits mailing list