Skip to content

Commit 76812fc

Browse files
authored
[mlir][vector] Improve vector dialect documentation and constraint predicates (#155259)
This PR changes the documentation of the vector dialect to better reflect reality. When vector operations with alignment requirements are violated, undefined behavior will occur. This PR also adds the predicate `IntValidAlignment` and adds it as constraint predicates for alignment attribtues.
1 parent 0ad35d7 commit 76812fc

File tree

2 files changed

+28
-32
lines changed

2 files changed

+28
-32
lines changed

mlir/include/mlir/Dialect/Vector/IR/VectorOps.td

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1712,16 +1712,15 @@ def Vector_LoadOp : Vector_Op<"load", [
17121712

17131713
An optional `alignment` attribute allows to specify the byte alignment of the
17141714
load operation. It must be a positive power of 2. The operation must access
1715-
memory at an address aligned to this boundary. Violations may lead to
1716-
architecture-specific faults or performance penalties.
1715+
memory at an address aligned to this boundary. Violating this requirement
1716+
triggers immediate undefined behavior.
17171717
}];
17181718

17191719
let arguments = (ins Arg<AnyMemRef, "the reference to load from",
17201720
[MemRead]>:$base,
17211721
Variadic<Index>:$indices,
17221722
DefaultValuedOptionalAttr<BoolAttr, "false">:$nontemporal,
1723-
ConfinedAttr<OptionalAttr<I64Attr>,
1724-
[AllAttrOf<[IntPositive, IntPowerOf2]>]>:$alignment);
1723+
OptionalAttr<IntValidAlignment<I64Attr>>: $alignment);
17251724

17261725
let builders = [
17271726
OpBuilder<(ins "VectorType":$resultType,
@@ -1827,8 +1826,8 @@ def Vector_StoreOp : Vector_Op<"store", [
18271826

18281827
An optional `alignment` attribute allows to specify the byte alignment of the
18291828
store operation. It must be a positive power of 2. The operation must access
1830-
memory at an address aligned to this boundary. Violations may lead to
1831-
architecture-specific faults or performance penalties.
1829+
memory at an address aligned to this boundary. Violating this requirement
1830+
triggers immediate undefined behavior.
18321831
}];
18331832

18341833
let arguments = (ins
@@ -1837,8 +1836,7 @@ def Vector_StoreOp : Vector_Op<"store", [
18371836
[MemWrite]>:$base,
18381837
Variadic<Index>:$indices,
18391838
DefaultValuedOptionalAttr<BoolAttr, "false">:$nontemporal,
1840-
ConfinedAttr<OptionalAttr<I64Attr>,
1841-
[AllAttrOf<[IntPositive, IntPowerOf2]>]>:$alignment);
1839+
OptionalAttr<IntValidAlignment<I64Attr>>: $alignment);
18421840

18431841
let builders = [
18441842
OpBuilder<(ins "Value":$valueToStore,
@@ -1875,8 +1873,7 @@ def Vector_MaskedLoadOp :
18751873
Variadic<Index>:$indices,
18761874
VectorOfNonZeroRankOf<[I1]>:$mask,
18771875
AnyVectorOfNonZeroRank:$pass_thru,
1878-
ConfinedAttr<OptionalAttr<I64Attr>,
1879-
[AllAttrOf<[IntPositive, IntPowerOf2]>]>:$alignment)>,
1876+
OptionalAttr<IntValidAlignment<I64Attr>>: $alignment)>,
18801877
Results<(outs AnyVectorOfNonZeroRank:$result)> {
18811878

18821879
let summary = "loads elements from memory into a vector as defined by a mask vector";
@@ -1915,8 +1912,8 @@ def Vector_MaskedLoadOp :
19151912

19161913
An optional `alignment` attribute allows to specify the byte alignment of the
19171914
load operation. It must be a positive power of 2. The operation must access
1918-
memory at an address aligned to this boundary. Violations may lead to
1919-
architecture-specific faults or performance penalties.
1915+
memory at an address aligned to this boundary. Violating this requirement
1916+
triggers immediate undefined behavior.
19201917
}];
19211918
let extraClassDeclaration = [{
19221919
MemRefType getMemRefType() {
@@ -1968,8 +1965,7 @@ def Vector_MaskedStoreOp :
19681965
Variadic<Index>:$indices,
19691966
VectorOfNonZeroRankOf<[I1]>:$mask,
19701967
AnyVectorOfNonZeroRank:$valueToStore,
1971-
ConfinedAttr<OptionalAttr<I64Attr>,
1972-
[AllAttrOf<[IntPositive, IntPowerOf2]>]>:$alignment)> {
1968+
OptionalAttr<IntValidAlignment<I64Attr>>: $alignment)> {
19731969

19741970
let summary = "stores elements from a vector into memory as defined by a mask vector";
19751971

@@ -2007,8 +2003,8 @@ def Vector_MaskedStoreOp :
20072003

20082004
An optional `alignment` attribute allows to specify the byte alignment of the
20092005
store operation. It must be a positive power of 2. The operation must access
2010-
memory at an address aligned to this boundary. Violations may lead to
2011-
architecture-specific faults or performance penalties.
2006+
memory at an address aligned to this boundary. Violating this requirement
2007+
triggers immediate undefined behavior.
20122008
}];
20132009
let extraClassDeclaration = [{
20142010
MemRefType getMemRefType() {
@@ -2051,8 +2047,7 @@ def Vector_GatherOp :
20512047
VectorOfNonZeroRankOf<[AnyInteger, Index]>:$indices,
20522048
VectorOfNonZeroRankOf<[I1]>:$mask,
20532049
AnyVectorOfNonZeroRank:$pass_thru,
2054-
ConfinedAttr<OptionalAttr<I64Attr>,
2055-
[AllAttrOf<[IntPositive, IntPowerOf2]>]>:$alignment)>,
2050+
OptionalAttr<IntValidAlignment<I64Attr>>: $alignment)>,
20562051
Results<(outs AnyVectorOfNonZeroRank:$result)> {
20572052

20582053
let summary = [{
@@ -2100,8 +2095,8 @@ def Vector_GatherOp :
21002095

21012096
An optional `alignment` attribute allows to specify the byte alignment of the
21022097
gather operation. It must be a positive power of 2. The operation must access
2103-
memory at an address aligned to this boundary. Violations may lead to
2104-
architecture-specific faults or performance penalties.
2098+
memory at an address aligned to this boundary. Violating this requirement
2099+
triggers immediate undefined behavior.
21052100

21062101
Examples:
21072102

@@ -2154,8 +2149,7 @@ def Vector_ScatterOp :
21542149
VectorOfNonZeroRankOf<[AnyInteger, Index]>:$indices,
21552150
VectorOfNonZeroRankOf<[I1]>:$mask,
21562151
AnyVectorOfNonZeroRank:$valueToStore,
2157-
ConfinedAttr<OptionalAttr<I64Attr>,
2158-
[AllAttrOf<[IntPositive, IntPowerOf2]>]>:$alignment)> {
2152+
OptionalAttr<IntValidAlignment<I64Attr>>: $alignment)> {
21592153

21602154
let summary = [{
21612155
scatters elements from a vector into memory as defined by an index vector
@@ -2191,8 +2185,8 @@ def Vector_ScatterOp :
21912185

21922186
An optional `alignment` attribute allows to specify the byte alignment of the
21932187
scatter operation. It must be a positive power of 2. The operation must access
2194-
memory at an address aligned to this boundary. Violations may lead to
2195-
architecture-specific faults or performance penalties.
2188+
memory at an address aligned to this boundary. Violating this requirement
2189+
triggers immediate undefined behavior.
21962190

21972191
Examples:
21982192

@@ -2239,8 +2233,7 @@ def Vector_ExpandLoadOp :
22392233
Variadic<Index>:$indices,
22402234
FixedVectorOfNonZeroRankOf<[I1]>:$mask,
22412235
AnyVectorOfNonZeroRank:$pass_thru,
2242-
ConfinedAttr<OptionalAttr<I64Attr>,
2243-
[AllAttrOf<[IntPositive, IntPowerOf2]>]>:$alignment)>,
2236+
OptionalAttr<IntValidAlignment<I64Attr>>: $alignment)>,
22442237
Results<(outs AnyVectorOfNonZeroRank:$result)> {
22452238

22462239
let summary = "reads elements from memory and spreads them into a vector as defined by a mask";
@@ -2274,8 +2267,8 @@ def Vector_ExpandLoadOp :
22742267

22752268
An optional `alignment` attribute allows to specify the byte alignment of the
22762269
load operation. It must be a positive power of 2. The operation must access
2277-
memory at an address aligned to this boundary. Violations may lead to
2278-
architecture-specific faults or performance penalties.
2270+
memory at an address aligned to this boundary. Violating this requirement
2271+
triggers immediate undefined behavior.
22792272

22802273
Note, at the moment this Op is only available for fixed-width vectors.
22812274

@@ -2328,8 +2321,7 @@ def Vector_CompressStoreOp :
23282321
Variadic<Index>:$indices,
23292322
FixedVectorOfNonZeroRankOf<[I1]>:$mask,
23302323
AnyVectorOfNonZeroRank:$valueToStore,
2331-
ConfinedAttr<OptionalAttr<I64Attr>,
2332-
[AllAttrOf<[IntPositive, IntPowerOf2]>]>:$alignment)> {
2324+
OptionalAttr<IntValidAlignment<I64Attr>>: $alignment)> {
23332325

23342326
let summary = "writes elements selectively from a vector as defined by a mask";
23352327

@@ -2362,8 +2354,8 @@ def Vector_CompressStoreOp :
23622354

23632355
An optional `alignment` attribute allows to specify the byte alignment of the
23642356
store operation. It must be a positive power of 2. The operation must access
2365-
memory at an address aligned to this boundary. Violations may lead to
2366-
architecture-specific faults or performance penalties.
2357+
memory at an address aligned to this boundary. Violating this requirement
2358+
triggers immediate undefined behavior.
23672359

23682360
Note, at the moment this Op is only available for fixed-width vectors.
23692361

mlir/include/mlir/IR/CommonAttrConstraints.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,10 @@ def IntPowerOf2 : AttrConstraint<
800800
CPred<"::llvm::cast<::mlir::IntegerAttr>($_self).getValue().isPowerOf2()">,
801801
"whose value is a power of two > 0">;
802802

803+
def IntPositivePowerOf2 : AllAttrOf<[IntPositive, IntPowerOf2]>;
804+
805+
class IntValidAlignment<Attr attr>: ConfinedAttr<attr, [IntPositivePowerOf2]>;
806+
803807
class ArrayMaxCount<int n> : AttrConstraint<
804808
CPred<"::llvm::cast<::mlir::ArrayAttr>($_self).size() <= " # n>,
805809
"with at most " # n # " elements">;

0 commit comments

Comments
 (0)