[Mlir-commits] [mlir] [mlir][ptr] Add the `ptradd` and `type_offset` ops, and `int_space` attr (PR #136434)

Fabian Mora llvmlistbot at llvm.org
Sun Apr 20 08:18:14 PDT 2025


================
@@ -39,6 +41,37 @@ void PtrDialect::initialize() {
       >();
 }
 
+//===----------------------------------------------------------------------===//
+// PtrAddOp
+//===----------------------------------------------------------------------===//
+
+/// Fold the op to the base ptr when the offset is 0.
+OpFoldResult PtrAddOp::fold(FoldAdaptor adaptor) {
+  Attribute attr = adaptor.getOffset();
+  if (!attr)
+    return nullptr;
+  if (llvm::APInt value; m_ConstantInt(&value).match(attr) && value.isZero())
+    return getBase();
+  return nullptr;
+}
+
+Value PtrAddOp::getViewSource() { return getBase(); }
+
+//===----------------------------------------------------------------------===//
+// TypeOffsetOp
+//===----------------------------------------------------------------------===//
+
+OpFoldResult TypeOffsetOp::fold(FoldAdaptor adaptor) {
+  return TypeAttr::get(getElementType());
----------------
fabianmcg wrote:

Yes, CSE does the trick. I was thinking of that op as a constant, thus we get equivalence during canonicalization without having to invoke another transformation.

I'll change it.

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


More information about the Mlir-commits mailing list