[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 07:57:01 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:

Right now, I'm folding it with `TypeOffsetAttr getOffset()`, a int/index typed attribute representing the unrealized constant value. Also, we cannot fold it to an `IntegerAttr` if there's no DL.

Now, the op is `ConstantLike`, because it represents a constant with respect to a potential DL. Also, if it's not constant then:
```mlir
%0 = ptr.type_offset <f32> : i32
%1 = ptr.type_offset <f32> : i32
```
Never gets simplified to `%0 = %1`.

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


More information about the Mlir-commits mailing list