-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[mlir][vector] Add alignment attribute to vector operations. #152507
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
Changes from all commits
a97cbc6
158b91c
36949d1
a4d820f
e2ad0f9
92b3886
482ad75
9b451db
3180cd0
47db5b1
a714521
0f81b89
be906aa
46d9010
87958eb
c80adf0
dcf38e2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1714,7 +1714,6 @@ def Vector_LoadOp : Vector_Op<"load", [ | |
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", | ||
|
@@ -1830,7 +1829,6 @@ def Vector_StoreOp : Vector_Op<"store", [ | |
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 | ||
|
@@ -1919,7 +1917,6 @@ def Vector_MaskedLoadOp : | |
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 extraClassDeclaration = [{ | ||
MemRefType getMemRefType() { | ||
|
@@ -2012,7 +2009,6 @@ def Vector_MaskedStoreOp : | |
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 extraClassDeclaration = [{ | ||
MemRefType getMemRefType() { | ||
|
@@ -2054,7 +2050,9 @@ def Vector_GatherOp : | |
Variadic<Index>:$indices, | ||
VectorOfNonZeroRankOf<[AnyInteger, Index]>:$index_vec, | ||
VectorOfNonZeroRankOf<[I1]>:$mask, | ||
AnyVectorOfNonZeroRank:$pass_thru)>, | ||
AnyVectorOfNonZeroRank:$pass_thru, | ||
ConfinedAttr<OptionalAttr<I64Attr>, | ||
[AllAttrOf<[IntPositive, IntPowerOf2]>]>:$alignment)>, | ||
Results<(outs AnyVectorOfNonZeroRank:$result)> { | ||
|
||
let summary = [{ | ||
|
@@ -2096,6 +2094,15 @@ def Vector_GatherOp : | |
comes from the pass-through vector regardless of the index, and the index is | ||
allowed to be out-of-bounds. | ||
|
||
The gather operation can be used directly where applicable, or can be used | ||
during progressively lowering to bring other memory operations closer to | ||
hardware ISA support for a gather. | ||
|
||
An optional `alignment` attribute allows to specify the byte alignment of the | ||
gather 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. | ||
|
||
Examples: | ||
|
||
```mlir | ||
|
@@ -2124,6 +2131,20 @@ def Vector_GatherOp : | |
"`into` type($result)"; | ||
let hasCanonicalizer = 1; | ||
let hasVerifier = 1; | ||
|
||
let builders = [ | ||
OpBuilder<(ins "VectorType":$resultType, | ||
"Value":$base, | ||
"ValueRange":$indices, | ||
"Value":$index_vec, | ||
"Value":$mask, | ||
"Value":$passthrough, | ||
CArg<"llvm::MaybeAlign", "llvm::MaybeAlign()">:$alignment), [{ | ||
return build($_builder, $_state, resultType, base, indices, index_vec, mask, passthrough, | ||
alignment.has_value() ? $_builder.getI64IntegerAttr(alignment->value()) : | ||
nullptr); | ||
}]> | ||
]; | ||
} | ||
|
||
def Vector_ScatterOp : | ||
|
@@ -2132,7 +2153,9 @@ def Vector_ScatterOp : | |
Variadic<Index>:$indices, | ||
VectorOfNonZeroRankOf<[AnyInteger, Index]>:$index_vec, | ||
VectorOfNonZeroRankOf<[I1]>:$mask, | ||
AnyVectorOfNonZeroRank:$valueToStore)> { | ||
AnyVectorOfNonZeroRank:$valueToStore, | ||
ConfinedAttr<OptionalAttr<I64Attr>, | ||
[AllAttrOf<[IntPositive, IntPowerOf2]>]>:$alignment)> { | ||
|
||
let summary = [{ | ||
scatters elements from a vector into memory as defined by an index vector | ||
|
@@ -2166,6 +2189,11 @@ def Vector_ScatterOp : | |
correspond to those of the `llvm.masked.scatter` | ||
[intrinsic](https://llvm.org/docs/LangRef.html#llvm-masked-scatter-intrinsics). | ||
|
||
An optional `alignment` attribute allows to specify the byte alignment of the | ||
scatter 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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The violation is strangely defined here, why isn't this specified as UB? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In this particular case, there we some operations defined in this other PR #144344 which used this wording as documentation. I think changing the wording to be undefined behaviour is reasonable. Would something like the following be preferrable?
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think
For the loads maybe we should instead use:
or something like that. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think immediate UB is more suitable since using aligned loads on unaligned pointers is known to crash on some architectures There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point, but that means we can't speculate an aligned load anymore right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can open a PR to change the wording. Thanks @joker-eph ! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I checked LLVM and that seems like what we expect:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I personally prefer to avoid phrases like this unless we can provide a specific example. More generic terms like "UB" or "poison" tend to be more universal. |
||
|
||
Examples: | ||
|
||
```mlir | ||
|
@@ -2190,14 +2218,29 @@ def Vector_ScatterOp : | |
"type($index_vec) `,` type($mask) `,` type($valueToStore)"; | ||
let hasCanonicalizer = 1; | ||
let hasVerifier = 1; | ||
|
||
let builders = [ | ||
OpBuilder<(ins "Value":$base, | ||
"ValueRange":$indices, | ||
"Value":$index_vec, | ||
"Value":$mask, | ||
"Value":$valueToStore, | ||
CArg<"llvm::MaybeAlign", "llvm::MaybeAlign()">: $alignment), [{ | ||
return build($_builder, $_state, base, indices, index_vec, mask, valueToStore, | ||
alignment.has_value() ? $_builder.getI64IntegerAttr(alignment->value()) : | ||
nullptr); | ||
}]> | ||
]; | ||
} | ||
|
||
def Vector_ExpandLoadOp : | ||
Vector_Op<"expandload">, | ||
Arguments<(ins Arg<AnyMemRef, "", [MemRead]>:$base, | ||
Variadic<Index>:$indices, | ||
FixedVectorOfNonZeroRankOf<[I1]>:$mask, | ||
AnyVectorOfNonZeroRank:$pass_thru)>, | ||
AnyVectorOfNonZeroRank:$pass_thru, | ||
ConfinedAttr<OptionalAttr<I64Attr>, | ||
[AllAttrOf<[IntPositive, IntPowerOf2]>]>:$alignment)>, | ||
Results<(outs AnyVectorOfNonZeroRank:$result)> { | ||
|
||
let summary = "reads elements from memory and spreads them into a vector as defined by a mask"; | ||
|
@@ -2229,6 +2272,11 @@ def Vector_ExpandLoadOp : | |
correspond to those of the `llvm.masked.expandload` | ||
[intrinsic](https://llvm.org/docs/LangRef.html#llvm-masked-expandload-intrinsics). | ||
|
||
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. | ||
|
||
Note, at the moment this Op is only available for fixed-width vectors. | ||
|
||
Examples: | ||
|
@@ -2259,14 +2307,29 @@ def Vector_ExpandLoadOp : | |
"type($base) `,` type($mask) `,` type($pass_thru) `into` type($result)"; | ||
let hasCanonicalizer = 1; | ||
let hasVerifier = 1; | ||
|
||
let builders = [ | ||
OpBuilder<(ins "VectorType":$resultType, | ||
"Value":$base, | ||
"ValueRange":$indices, | ||
"Value":$mask, | ||
"Value":$passthrough, | ||
CArg<"llvm::MaybeAlign", "llvm::MaybeAlign()">:$alignment), [{ | ||
return build($_builder, $_state, resultType, base, indices, mask, passthrough, | ||
alignment.has_value() ? $_builder.getI64IntegerAttr(alignment->value()) : | ||
nullptr); | ||
}]> | ||
]; | ||
} | ||
|
||
def Vector_CompressStoreOp : | ||
Vector_Op<"compressstore">, | ||
Arguments<(ins Arg<AnyMemRef, "", [MemWrite]>:$base, | ||
Variadic<Index>:$indices, | ||
FixedVectorOfNonZeroRankOf<[I1]>:$mask, | ||
AnyVectorOfNonZeroRank:$valueToStore)> { | ||
AnyVectorOfNonZeroRank:$valueToStore, | ||
ConfinedAttr<OptionalAttr<I64Attr>, | ||
[AllAttrOf<[IntPositive, IntPowerOf2]>]>:$alignment)> { | ||
|
||
let summary = "writes elements selectively from a vector as defined by a mask"; | ||
|
||
|
@@ -2297,6 +2360,11 @@ def Vector_CompressStoreOp : | |
correspond to those of the `llvm.masked.compressstore` | ||
[intrinsic](https://llvm.org/docs/LangRef.html#llvm-masked-compressstore-intrinsics). | ||
|
||
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. | ||
|
||
Note, at the moment this Op is only available for fixed-width vectors. | ||
|
||
Examples: | ||
|
@@ -2325,6 +2393,17 @@ def Vector_CompressStoreOp : | |
"type($base) `,` type($mask) `,` type($valueToStore)"; | ||
let hasCanonicalizer = 1; | ||
let hasVerifier = 1; | ||
let builders = [ | ||
OpBuilder<(ins "Value":$base, | ||
"ValueRange":$indices, | ||
"Value":$mask, | ||
"Value":$valueToStore, | ||
CArg<"llvm::MaybeAlign", "llvm::MaybeAlign()">:$alignment), [{ | ||
return build($_builder, $_state, base, indices, valueToStore, mask, | ||
alignment.has_value() ? $_builder.getI64IntegerAttr(alignment->value()) : | ||
nullptr); | ||
}]> | ||
]; | ||
} | ||
|
||
def Vector_ShapeCastOp : | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you remind me what happens when alignment is not specified?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I originally wanted thought about removing this line since I imagined that the constructors using the
llvm::Maybe
align will be preferred, but I now believe that adding this line back makes more sense since there are other constructors as well and the actual value stored is an integer attribute. Thanks for pointing it out! 47db5b1There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And just to double check - is
0
the default value?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
0
is not the default value of thealignment
attribute in the operation. I think there's some ambiguity here regarding what "default" means. It could mean two things in my opinion:In the original PR (#144344) the default parameter to one of the constructors is indeed zero, but the attribute is optional and the attribute linked to the operation is actually a
nullptr
.In PR #151690 the default parameter for these constructors was changed from a
uint64_t
type to anllvm::MaybeAlign
, but keeps the attribute linked to the operation to benullptr
when there is no alignment requirement. I.e., PR #15169llvm::MaybeAlign()
llvm::MaybeAlign()
the integer attribute pointer isnullptr
.Just to be complete, I believe in both cases the operation could have the field could be
nullptr
or point toI64IntegerAttr(0)
to indicate no alignment requirement. For example, if the user used a different constructor passing all attributes in order.I think having the documentation indicate that a value of zero indicates no specific alignment requirements is still correct as the Operation's alignment field is still an integer (when present) and it being zero would still signifies no specific alignment requirements.
I think we could also make the alignment attribute required by removing the OptionalAttr and then setting the alignment field point to
I64IntegerAttr(0)
to remove thenullptr
and have solelyI64IntegerAttr(0)
mean no specific alignment requirement. Happy to add changes if you think it is required :-).I could also change the line to say that a value of
llvm::MaybeAlign()
indicates no specific alignment requirements and values ofllvm::Align(n)
for n bigger than zero to be alignment requirements.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the detailed explanation! Looks like things have become a bit complex recently 😅
I've just realised that we no longer support
{ alignment = 0 }
. Take this example:Now:
So, zero-alignment is no longer valid :) This makes sense to me - otherwise
0
was some magic value with some magic meaning.To me, all of this calls for a few updates:
alignemt = 0
).WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, you are right.
alignment = 0
was not supported in the Attribute due to the constraint.Regarding next steps:
This is fine, I'll remove this line.
I think just updating the ones that are
-1
to0
would be enough.Sure.
Sounds good! Thanks @banach-space
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@banach-space I am not sure about predicates yet. I think as long as alignment is modeled as an integer attribute,
I64Attr
should be written in the tblgen file, and also if it is optional,OptionalAttr
should also be written in the tblgen file.If the only requirements are positive and power of two, then they are already self-descriptive. One could also have something like:
What exactly do you propose as a predicate here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Basically, something like:
You are right that
IntPositive
andIntPowerOf2
are self descriptive on their own, but IMHO usingIntValidAlignment
would create a global definition of what constitutes a valid alignment. This is a nice-to-have.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was having some doubts because
IntValidAlignment
would be a little bit of an indirection and now in order to know what a valid alignment is, one would need to look at this definition itself as opposed to having it directly on tablegen. I'll open the new PR with this predicate.