[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 18:50:54 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();
+ 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);
----------------
bcardosolopes wrote:
I ended up going with krzysz00 suggestion here, so not a problem anymore.
https://github.com/llvm/llvm-project/pull/238991
More information about the cfe-commits
mailing list