Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 57 additions & 3 deletions mlir/include/mlir/Dialect/Ptr/IR/PtrAttrDefs.td
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,34 @@ class Ptr_Attr<string name, string attrMnemonic,
let mnemonic = attrMnemonic;
}

//===----------------------------------------------------------------------===//
// AddressAttr
//===----------------------------------------------------------------------===//

def Ptr_AddressAttr : Ptr_Attr<"Address", "address", [
DeclareAttrInterfaceMethods<TypedAttrInterface>
]> {
let summary = "Address attribute";
let description = [{
The `address` attribute represents a raw memory address, expressed in bytes.

Example:

```mlir
#ptr.address<0x1000> : !ptr.ptr<#ptr.generic_space>
```
}];
let parameters = (ins AttributeSelfTypeParameter<"", "PtrType">:$type,
APIntParameter<"">:$value);
let builders = [
AttrBuilderWithInferredContext<(ins "PtrType":$type,
"const llvm::APInt &":$value), [{
return $_get(type.getContext(), type, value);
}]>
];
let assemblyFormat = "`<` $value `>`";
}

//===----------------------------------------------------------------------===//
// GenericSpaceAttr
//===----------------------------------------------------------------------===//
Expand All @@ -37,16 +65,42 @@ def Ptr_GenericSpaceAttr :
- Load and store operations are always valid, regardless of the type.
- Atomic operations are always valid, regardless of the type.
- Cast operations to `generic_space` are always valid.

Example:

```mlir
#ptr.generic_space
#ptr.generic_space : !ptr.ptr<#ptr.generic_space>
```
}];
let assemblyFormat = "";
}

//===----------------------------------------------------------------------===//
// NullAttr
//===----------------------------------------------------------------------===//

def Ptr_NullAttr : Ptr_Attr<"Null", "null", [
DeclareAttrInterfaceMethods<TypedAttrInterface>
]> {
let summary = "Null pointer attribute";
let description = [{
The `null` attribute represents a null pointer.

Example:

```mlir
#ptr.null
```
}];
let parameters = (ins AttributeSelfTypeParameter<"", "PtrType">:$type);
let builders = [
AttrBuilderWithInferredContext<(ins "PtrType":$type), [{
return $_get(type.getContext(), type);
}]>
];
let assemblyFormat = "";
}

//===----------------------------------------------------------------------===//
// SpecAttr
//===----------------------------------------------------------------------===//
Expand All @@ -62,7 +116,7 @@ def Ptr_SpecAttr : Ptr_Attr<"Spec", "spec"> {
- [Optional] index: bitwidth that should be used when performing index
computations for the type. Setting the field to `kOptionalSpecValue`, means
the field is optional.

Furthermore, the attribute will verify that all present values are divisible
by 8 (number of bits in a byte), and that `preferred` > `abi`.

Expand Down
6 changes: 6 additions & 0 deletions mlir/include/mlir/Dialect/Ptr/IR/PtrAttrs.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@
#include "mlir/Dialect/Ptr/IR/MemorySpaceInterfaces.h"
#include "mlir/Dialect/Ptr/IR/PtrEnums.h"

namespace mlir {
namespace ptr {
class PtrType;
} // namespace ptr
} // namespace mlir

#define GET_ATTRDEF_CLASSES
#include "mlir/Dialect/Ptr/IR/PtrOpsAttrs.h.inc"

Expand Down
39 changes: 32 additions & 7 deletions mlir/include/mlir/Dialect/Ptr/IR/PtrOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Ptr_ShapedValueType<list<Type> allowedTypes, list<Pred> preds = []> :
/*cppType=*/"::mlir::ShapedType">;

// A ptr-like type, either scalar or shaped type with value semantics.
def Ptr_PtrLikeType :
def Ptr_PtrLikeType :
AnyTypeOf<[Ptr_ShapedValueType<[Ptr_PtrType], [HasRankPred]>, Ptr_PtrType]>;

// An int-like type, either scalar or shaped type with value semantics.
Expand All @@ -57,6 +57,31 @@ def Ptr_Mask1DType :
def Ptr_Ptr1DType :
Ptr_ShapedValueType<[Ptr_PtrType], [HasAnyRankOfPred<[1]>]>;

//===----------------------------------------------------------------------===//
// ConstantOp
//===----------------------------------------------------------------------===//

def Ptr_ConstantOp : Pointer_Op<"constant", [
ConstantLike, Pure, AllTypesMatch<["value", "result"]>
]> {
let summary = "Pointer constant operation";
let description = [{
The `constant` operation produces a pointer constant. The attribute must be
a typed attribute of pointer type.

Example:

```mlir
// Create a null pointer
%null = ptr.constant #ptr.null : !ptr.ptr<#ptr.generic_space>
```
}];
let arguments = (ins TypedAttrInterface:$value);
let results = (outs Ptr_PtrType:$result);
let assemblyFormat = "attr-dict $value";
let hasFolder = 1;
}

//===----------------------------------------------------------------------===//
// FromPtrOp
//===----------------------------------------------------------------------===//
Expand All @@ -81,7 +106,7 @@ def Ptr_FromPtrOp : Pointer_Op<"from_ptr", [
```mlir
%typed_ptr = ptr.from_ptr %ptr : !ptr.ptr<#ptr.generic_space> -> !my.ptr<f32, #ptr.generic_space>
%memref = ptr.from_ptr %ptr metadata %md : !ptr.ptr<#ptr.generic_space> -> memref<f32, #ptr.generic_space>

// Cast the `%ptr` to a memref without utilizing metadata.
%memref = ptr.from_ptr %ptr : !ptr.ptr<#ptr.generic_space> -> memref<f32, #ptr.generic_space>
```
Expand Down Expand Up @@ -361,13 +386,13 @@ def Ptr_PtrAddOp : Pointer_Op<"ptr_add", [
// Scalar base and offset
%x_off = ptr.ptr_add %x, %off : !ptr.ptr<#ptr.generic_space>, i32
%x_off0 = ptr.ptr_add nusw %x, %off : !ptr.ptr<#ptr.generic_space>, i32

// Shaped base with scalar offset
%ptrs_off = ptr.ptr_add %ptrs, %off : vector<4x!ptr.ptr<#ptr.generic_space>>, i32

// Scalar base with shaped offset
%x_offs = ptr.ptr_add %x, %offs : !ptr.ptr<#ptr.generic_space>, vector<4xi32>

// Both base and offset are shaped
%ptrs_offs = ptr.ptr_add %ptrs, %offs : vector<4x!ptr.ptr<#ptr.generic_space>>, vector<4xi32>
```
Expand All @@ -382,7 +407,7 @@ def Ptr_PtrAddOp : Pointer_Op<"ptr_add", [
}];
let hasFolder = 1;
let extraClassDeclaration = [{
/// `ViewLikeOp::getViewSource` method.
/// `ViewLikeOp::getViewSource` method.
Value getViewSource() { return getBase(); }

/// Returns the ptr type of the operation.
Expand Down Expand Up @@ -418,7 +443,7 @@ def Ptr_ScatterOp : Pointer_Op<"scatter", [
// Scatter values to multiple memory locations
ptr.scatter %value, %ptrs, %mask :
vector<4xf32>, vector<4x!ptr.ptr<#ptr.generic_space>>

// Scatter with alignment
ptr.scatter %value, %ptrs, %mask alignment = 8 :
vector<4xf32>, vector<4x!ptr.ptr<#ptr.generic_space>>
Expand Down
7 changes: 4 additions & 3 deletions mlir/include/mlir/IR/DialectImplementation.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,11 @@ struct FieldParser<

/// Parse any integer.
template <typename IntT>
struct FieldParser<IntT,
std::enable_if_t<std::is_integral<IntT>::value, IntT>> {
struct FieldParser<IntT, std::enable_if_t<(std::is_integral<IntT>::value ||
std::is_same_v<IntT, llvm::APInt>),
IntT>> {
static FailureOr<IntT> parse(AsmParser &parser) {
IntT value = 0;
IntT value{};
Copy link
Preview

Copilot AI Sep 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default initialization IntT value{} will zero-initialize llvm::APInt, but APInt requires explicit bit width specification in its constructor. This will cause compilation errors when IntT is llvm::APInt.

Copilot uses AI. Check for mistakes.

if (parser.parseInteger(value))
return failure();
return value;
Expand Down
6 changes: 6 additions & 0 deletions mlir/lib/Dialect/Ptr/IR/PtrDialect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ verifyAlignment(std::optional<int64_t> alignment,
return success();
}

//===----------------------------------------------------------------------===//
// ConstantOp
//===----------------------------------------------------------------------===//

OpFoldResult ConstantOp::fold(FoldAdaptor adaptor) { return getValue(); }

//===----------------------------------------------------------------------===//
// FromPtrOp
//===----------------------------------------------------------------------===//
Expand Down
Loading
Loading