@@ -13,6 +13,7 @@ include "mlir/Dialect/Ptr/IR/PtrDialect.td"
1313include "mlir/Dialect/Ptr/IR/PtrAttrDefs.td"
1414include "mlir/Dialect/Ptr/IR/PtrEnums.td"
1515include "mlir/Dialect/Ptr/IR/MemorySpaceInterfaces.td"
16+ include "mlir/Interfaces/InferTypeOpInterface.td"
1617include "mlir/Interfaces/SideEffectInterfaces.td"
1718include "mlir/Interfaces/ViewLikeInterface.td"
1819include "mlir/IR/OpAsmInterface.td"
@@ -34,8 +35,15 @@ class Ptr_ShapedValueType<list<Type> allowedTypes, list<Pred> preds = []> :
3435 /*descr=*/[{A shaped type with value semantics and rank.}],
3536 /*cppType=*/"::mlir::ShapedType">;
3637
37- // A shaped pointer type with value semantics and rank.
38- class Ptr_ShapedPtrType : Ptr_ShapedValueType<[Ptr_PtrType], [HasRankPred]>;
38+ // A ptr-like type, either scalar or shaped type with value semantics.
39+ def Ptr_PtrLikeType :
40+ AnyTypeOf<[Ptr_ShapedValueType<[Ptr_PtrType], [HasRankPred]>, Ptr_PtrType]>;
41+
42+ // An int-like type, either scalar or shaped type with value semantics.
43+ def Ptr_IntLikeType :AnyTypeOf<[
44+ Ptr_ShapedValueType<[AnySignlessIntegerOrIndex], [HasRankPred]>,
45+ AnySignlessIntegerOrIndex
46+ ]>;
3947
4048// A shaped value type of rank 1 of any element type.
4149def Ptr_Any1DType :
@@ -175,41 +183,6 @@ def Ptr_GetMetadataOp : Pointer_Op<"get_metadata", [
175183 }];
176184}
177185
178- //===----------------------------------------------------------------------===//
179- // PtrAddOp
180- //===----------------------------------------------------------------------===//
181-
182- def Ptr_PtrAddOp : Pointer_Op<"ptr_add", [
183- Pure, AllTypesMatch<["base", "result"]>, ViewLikeOpInterface
184- ]> {
185- let summary = "Pointer add operation";
186- let description = [{
187- The `ptr_add` operation adds an integer offset to a pointer to produce a new
188- pointer. The input and output pointer types are always the same.
189-
190- Example:
191-
192- ```mlir
193- %x_off = ptr.ptr_add %x, %off : !ptr.ptr<#ptr.generic_space>, i32
194- %x_off0 = ptr.ptr_add nusw %x, %off : !ptr.ptr<#ptr.generic_space>, i32
195- ```
196- }];
197-
198- let arguments = (ins
199- Ptr_PtrType:$base,
200- AnySignlessIntegerOrIndex:$offset,
201- DefaultValuedProp<EnumProp<Ptr_PtrAddFlags>, "PtrAddFlags::none">:$flags);
202- let results = (outs Ptr_PtrType:$result);
203- let assemblyFormat = [{
204- ($flags^)? $base `,` $offset attr-dict `:` type($base) `,` type($offset)
205- }];
206- let hasFolder = 1;
207- let extraClassDeclaration = [{
208- /// `ViewLikeOp::getViewSource` method.
209- Value getViewSource() { return getBase(); }
210- }];
211- }
212-
213186//===----------------------------------------------------------------------===//
214187// LoadOp
215188//===----------------------------------------------------------------------===//
@@ -369,6 +342,62 @@ def Ptr_MaskedStoreOp : Pointer_Op<"masked_store", [
369342 let hasVerifier = 1;
370343}
371344
345+ //===----------------------------------------------------------------------===//
346+ // PtrAddOp
347+ //===----------------------------------------------------------------------===//
348+
349+ def Ptr_PtrAddOp : Pointer_Op<"ptr_add", [
350+ Pure, ViewLikeOpInterface,
351+ DeclareOpInterfaceMethods<InferTypeOpInterface>
352+ ]> {
353+ let summary = "Pointer add operation";
354+ let description = [{
355+ The `ptr_add` operation adds an int-like offset to one or more pointers to produce one or more new pointers.
356+
357+ The operation supports both scalar and shaped types with value semantics:
358+ - When both base and offset are scalar: produces a single new pointer
359+ - When base is shaped and offset is scalar: adds the same offset to each
360+ pointer in the base
361+ - When base is scalar and offset is shaped: adds the single pointer to each
362+ offset in the shaped value
363+ - When both are shaped: performs element-wise addition (shapes must be
364+ compatible)
365+
366+ Example:
367+
368+ ```mlir
369+ // Scalar base and offset
370+ %x_off = ptr.ptr_add %x, %off : !ptr.ptr<#ptr.generic_space>, i32
371+ %x_off0 = ptr.ptr_add nusw %x, %off : !ptr.ptr<#ptr.generic_space>, i32
372+
373+ // Shaped base with scalar offset
374+ %ptrs_off = ptr.ptr_add %ptrs, %off : vector<4x!ptr.ptr<#ptr.generic_space>>, i32
375+
376+ // Scalar base with shaped offset
377+ %x_offs = ptr.ptr_add %x, %offs : !ptr.ptr<#ptr.generic_space>, vector<4xi32>
378+
379+ // Both base and offset are shaped
380+ %ptrs_offs = ptr.ptr_add %ptrs, %offs : vector<4x!ptr.ptr<#ptr.generic_space>>, vector<4xi32>
381+ ```
382+ }];
383+ let arguments = (ins
384+ Ptr_PtrLikeType:$base,
385+ Ptr_IntLikeType:$offset,
386+ DefaultValuedProp<EnumProp<Ptr_PtrAddFlags>, "PtrAddFlags::none">:$flags);
387+ let results = (outs Ptr_PtrLikeType:$result);
388+ let assemblyFormat = [{
389+ ($flags^)? $base `,` $offset attr-dict `:` type($base) `,` type($offset)
390+ }];
391+ let hasFolder = 1;
392+ let extraClassDeclaration = [{
393+ /// `ViewLikeOp::getViewSource` method.
394+ Value getViewSource() { return getBase(); }
395+
396+ /// Returns the ptr type of the operation.
397+ ptr::PtrType getPtrType();
398+ }];
399+ }
400+
372401//===----------------------------------------------------------------------===//
373402// ScatterOp
374403//===----------------------------------------------------------------------===//
0 commit comments