Skip to content

[mlir][vector][memref] Add alignment attribute to memory access ops #144344

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions mlir/docs/DefiningDialects/Operations.md
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,8 @@ Right now, the following primitive constraints are supported:
* `IntPositive`: Specifying an integer attribute whose value is positive
* `IntNonNegative`: Specifying an integer attribute whose value is
non-negative
* `IntPowerOf2`: Specifying an integer attribute whose value is a power of
two > 0
* `ArrayMinCount<N>`: Specifying an array attribute to have at least `N`
elements
* `ArrayMaxCount<N>`: Specifying an array attribute to have at most `N`
Expand Down
60 changes: 57 additions & 3 deletions mlir/include/mlir/Dialect/MemRef/IR/MemRefOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -1217,6 +1217,11 @@ def LoadOp : MemRef_Op<"load",
be reused in the cache. For details, refer to the
[https://llvm.org/docs/LangRef.html#load-instruction](LLVM load instruction).

An optional `alignment` attribute allows to specify the byte alignment of the
load operation. It must be a positive power of 2. The operation must access
Copy link
Member

Choose a reason for hiding this comment

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

Let's have a verifier that checks for it being a power of 2.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

How about a new IntPowerOf2 AttrConstraint?

memory at an address aligned to this boundary. Violations may lead to
architecture-specific faults or performance penalties.
A value of 0 indicates no specific alignment requirement.
Example:

```mlir
Expand All @@ -1227,7 +1232,39 @@ def LoadOp : MemRef_Op<"load",
let arguments = (ins Arg<AnyMemRef, "the reference to load from",
[MemRead]>:$memref,
Variadic<Index>:$indices,
DefaultValuedOptionalAttr<BoolAttr, "false">:$nontemporal);
DefaultValuedOptionalAttr<BoolAttr, "false">:$nontemporal,
ConfinedAttr<OptionalAttr<I64Attr>,
[AllAttrOf<[IntPositive, IntPowerOf2]>]>:$alignment);

let builders = [
OpBuilder<(ins "Value":$memref,
"ValueRange":$indices,
CArg<"bool", "false">:$nontemporal,
CArg<"uint64_t", "0">:$alignment), [{
return build($_builder, $_state, memref, indices, nontemporal,
alignment != 0 ? $_builder.getI64IntegerAttr(alignment) :
nullptr);
}]>,
OpBuilder<(ins "Type":$resultType,
"Value":$memref,
"ValueRange":$indices,
CArg<"bool", "false">:$nontemporal,
CArg<"uint64_t", "0">:$alignment), [{
return build($_builder, $_state, resultType, memref, indices, nontemporal,
alignment != 0 ? $_builder.getI64IntegerAttr(alignment) :
nullptr);
}]>,
OpBuilder<(ins "TypeRange":$resultTypes,
"Value":$memref,
"ValueRange":$indices,
CArg<"bool", "false">:$nontemporal,
CArg<"uint64_t", "0">:$alignment), [{
return build($_builder, $_state, resultTypes, memref, indices, nontemporal,
alignment != 0 ? $_builder.getI64IntegerAttr(alignment) :
nullptr);
}]>
];

let results = (outs AnyType:$result);

let extraClassDeclaration = [{
Expand Down Expand Up @@ -1913,6 +1950,11 @@ def MemRef_StoreOp : MemRef_Op<"store",
be reused in the cache. For details, refer to the
[https://llvm.org/docs/LangRef.html#store-instruction](LLVM store instruction).

An optional `alignment` attribute allows to specify the byte alignment of the
store operation. It must be a positive power of 2. The operation must access
memory at an address aligned to this boundary. Violations may lead to
architecture-specific faults or performance penalties.
A value of 0 indicates no specific alignment requirement.
Example:

```mlir
Expand All @@ -1924,13 +1966,25 @@ def MemRef_StoreOp : MemRef_Op<"store",
Arg<AnyMemRef, "the reference to store to",
[MemWrite]>:$memref,
Variadic<Index>:$indices,
DefaultValuedOptionalAttr<BoolAttr, "false">:$nontemporal);
DefaultValuedOptionalAttr<BoolAttr, "false">:$nontemporal,
ConfinedAttr<OptionalAttr<I64Attr>,
[AllAttrOf<[IntPositive, IntPowerOf2]>]>:$alignment);

let builders = [
OpBuilder<(ins "Value":$valueToStore,
"Value":$memref,
"ValueRange":$indices,
CArg<"bool", "false">:$nontemporal,
CArg<"uint64_t", "0">:$alignment), [{
return build($_builder, $_state, valueToStore, memref, indices, nontemporal,
alignment != 0 ? $_builder.getI64IntegerAttr(alignment) :
nullptr);
}]>,
OpBuilder<(ins "Value":$valueToStore, "Value":$memref), [{
$_state.addOperands(valueToStore);
$_state.addOperands(memref);
}]>];
}]>
];

let extraClassDeclaration = [{
Value getValueToStore() { return getOperand(0); }
Expand Down
55 changes: 52 additions & 3 deletions mlir/include/mlir/Dialect/Vector/IR/VectorOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -1734,12 +1734,42 @@ def Vector_LoadOp : Vector_Op<"load"> {
```mlir
%result = vector.load %memref[%c0] : memref<7xf32>, vector<8xf32>
```

An optional `alignment` attribute allows to specify the byte alignment of the
load operation. It must be a positive power of 2. The operation must access
memory at an address aligned to this boundary. Violations may lead to
architecture-specific faults or performance penalties.
A value of 0 indicates no specific alignment requirement.
}];

let arguments = (ins Arg<AnyMemRef, "the reference to load from",
[MemRead]>:$base,
Variadic<Index>:$indices,
DefaultValuedOptionalAttr<BoolAttr, "false">:$nontemporal);
DefaultValuedOptionalAttr<BoolAttr, "false">:$nontemporal,
ConfinedAttr<OptionalAttr<I64Attr>,
[AllAttrOf<[IntPositive, IntPowerOf2]>]>:$alignment);

let builders = [
OpBuilder<(ins "VectorType":$resultType,
"Value":$base,
"ValueRange":$indices,
CArg<"bool", "false">:$nontemporal,
CArg<"uint64_t", "0">:$alignment), [{
return build($_builder, $_state, resultType, base, indices, nontemporal,
alignment != 0 ? $_builder.getI64IntegerAttr(alignment) :
nullptr);
}]>,
OpBuilder<(ins "TypeRange":$resultTypes,
"Value":$base,
"ValueRange":$indices,
CArg<"bool", "false">:$nontemporal,
CArg<"uint64_t", "0">:$alignment), [{
return build($_builder, $_state, resultTypes, base, indices, nontemporal,
alignment != 0 ? $_builder.getI64IntegerAttr(alignment) :
nullptr);
}]>
];

let results = (outs AnyVectorOfAnyRank:$result);

let extraClassDeclaration = [{
Expand Down Expand Up @@ -1818,15 +1848,34 @@ def Vector_StoreOp : Vector_Op<"store"> {
```mlir
vector.store %valueToStore, %memref[%c0] : memref<7xf32>, vector<8xf32>
```

An optional `alignment` attribute allows to specify the byte alignment of the
store operation. It must be a positive power of 2. The operation must access
memory at an address aligned to this boundary. Violations may lead to
architecture-specific faults or performance penalties.
A value of 0 indicates no specific alignment requirement.
}];

let arguments = (ins
AnyVectorOfAnyRank:$valueToStore,
Arg<AnyMemRef, "the reference to store to",
[MemWrite]>:$base,
Variadic<Index>:$indices,
DefaultValuedOptionalAttr<BoolAttr, "false">:$nontemporal
);
DefaultValuedOptionalAttr<BoolAttr, "false">:$nontemporal,
ConfinedAttr<OptionalAttr<I64Attr>,
[AllAttrOf<[IntPositive, IntPowerOf2]>]>:$alignment);

let builders = [
OpBuilder<(ins "Value":$valueToStore,
"Value":$base,
"ValueRange":$indices,
CArg<"bool", "false">:$nontemporal,
CArg<"uint64_t", "0">:$alignment), [{
return build($_builder, $_state, valueToStore, base, indices, nontemporal,
alignment != 0 ? $_builder.getI64IntegerAttr(alignment) :
nullptr);
}]>
];

let extraClassDeclaration = [{
MemRefType getMemRefType() {
Expand Down
4 changes: 4 additions & 0 deletions mlir/include/mlir/IR/CommonAttrConstraints.td
Original file line number Diff line number Diff line change
Expand Up @@ -796,6 +796,10 @@ def IntPositive : AttrConstraint<
CPred<"::llvm::cast<::mlir::IntegerAttr>($_self).getValue().isStrictlyPositive()">,
"whose value is positive">;

def IntPowerOf2 : AttrConstraint<
CPred<"::llvm::cast<::mlir::IntegerAttr>($_self).getValue().isPowerOf2()">,
"whose value is a power of two > 0">;

class ArrayMaxCount<int n> : AttrConstraint<
CPred<"::llvm::cast<::mlir::ArrayAttr>($_self).size() <= " # n>,
"with at most " # n # " elements">;
Expand Down
27 changes: 27 additions & 0 deletions mlir/test/Dialect/MemRef/load-store-alignment.mlir
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// RUN: mlir-opt -split-input-file -verify-diagnostics %s | FileCheck %s

// CHECK-LABEL: func @test_load_store_alignment
// CHECK: memref.load {{.*}} {alignment = 16 : i64}
// CHECK: memref.store {{.*}} {alignment = 16 : i64}
func.func @test_load_store_alignment(%memref: memref<4xi32>) {
%c0 = arith.constant 0 : index
%val = memref.load %memref[%c0] { alignment = 16 } : memref<4xi32>
memref.store %val, %memref[%c0] { alignment = 16 } : memref<4xi32>
return
}

// -----

func.func @test_invalid_negative_load_alignment(%memref: memref<4xi32>) {
// expected-error @+1 {{custom op 'memref.load' 'memref.load' op attribute 'alignment' failed to satisfy constraint: 64-bit signless integer attribute whose value is positive and whose value is a power of two > 0}}
%val = memref.load %memref[%c0] { alignment = -1 } : memref<4xi32>
return
}

// -----

func.func @test_invalid_non_power_of_2_store_alignment(%memref: memref<4xi32>, %val: i32) {
// expected-error @+1 {{custom op 'memref.store' 'memref.store' op attribute 'alignment' failed to satisfy constraint: 64-bit signless integer attribute whose value is positive and whose value is a power of two > 0}}
memref.store %val, %memref[%c0] { alignment = 1 } : memref<4xi32>
return
}
27 changes: 27 additions & 0 deletions mlir/test/Dialect/Vector/load-store-alignment.mlir
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// RUN: mlir-opt -split-input-file -verify-diagnostics %s | FileCheck %s

// CHECK-LABEL: func @test_load_store_alignment
// CHECK: vector.load {{.*}} {alignment = 16 : i64}
// CHECK: vector.store {{.*}} {alignment = 16 : i64}
func.func @test_load_store_alignment(%memref: memref<4xi32>) {
%c0 = arith.constant 0 : index
%val = vector.load %memref[%c0] { alignment = 16 } : memref<4xi32>, vector<4xi32>
vector.store %val, %memref[%c0] { alignment = 16 } : memref<4xi32>, vector<4xi32>
return
}

// -----

func.func @test_invalid_negative_load_alignment(%memref: memref<4xi32>) {
// expected-error @+1 {{custom op 'vector.load' 'vector.load' op attribute 'alignment' failed to satisfy constraint: 64-bit signless integer attribute whose value is positive and whose value is a power of two > 0}}
%val = vector.load %memref[%c0] { alignment = -1 } : memref<4xi32>, vector<4xi32>
return
}

// -----

func.func @test_invalid_non_power_of_2_store_alignment(%memref: memref<4xi32>, %val: vector<4xi32>) {
// expected-error @+1 {{custom op 'vector.store' 'vector.store' op attribute 'alignment' failed to satisfy constraint: 64-bit signless integer attribute whose value is positive and whose value is a power of two > 0}}
vector.store %val, %memref[%c0] { alignment = 1 } : memref<4xi32>, vector<4xi32>
return
}
Loading