Repository navigation
[mlir][vector] Clarify the semantics of gather/scatter indexing (nfc) - #181357
banach-space wants to merge 6 commits into
Conversation
|
@llvm/pr-subscribers-mlir Author: Andrzej Warzyński (banach-space) ChangesFor context, see: Currently, Supporting strided memrefs in the lowering is left as future work. This PR also adds tests for 4 Files Affected:
diff --git a/mlir/include/mlir/Dialect/Vector/IR/VectorOps.td b/mlir/include/mlir/Dialect/Vector/IR/VectorOps.td
index ddb04b6bbe40d..f49c578c91092 100644
--- a/mlir/include/mlir/Dialect/Vector/IR/VectorOps.td
+++ b/mlir/include/mlir/Dialect/Vector/IR/VectorOps.td
@@ -2087,7 +2087,10 @@ def Vector_GatherOp :
result[i,j] := if mask[i,j] then base[i0, i1, i2 + indices[i,j]]
else pass_thru[i,j]
```
- The index into `base` only varies in the innermost ((k-1)-th) dimension.
+ The index into `base` only varies in the innermost ((k-1)-th) dimension and
+ is treated as a logical rather than a physical index, i.e. it does not
+ encode any potential non-identity strides in the underlying MemRef layout
+ (Tensors do not model strided layouts, so only logical indexing is possible).
If a mask bit is set and the corresponding index is out-of-bounds for the
given base, the behavior is undefined. If a mask bit is not set, the value
@@ -2180,6 +2183,11 @@ def Vector_ScatterOp
is stored regardless of the index, and the index is allowed to be
out-of-bounds.
+ The index into `base` only varies in the innermost ((k-1)-th) dimension and
+ is treated as a logical rather than a physical index, i.e. it does not
+ encode any potential non-identity strides in the underlying MemRef layout
+ (Tensors do not model strided layouts, so only logical indexing is possible).
+
If the index vector contains two or more duplicate indices, the behavior is
undefined. Underlying implementation may enforce strict sequential
semantics.
diff --git a/mlir/lib/Conversion/VectorToLLVM/ConvertVectorToLLVM.cpp b/mlir/lib/Conversion/VectorToLLVM/ConvertVectorToLLVM.cpp
index 05d541fe80356..0a255c853b07e 100644
--- a/mlir/lib/Conversion/VectorToLLVM/ConvertVectorToLLVM.cpp
+++ b/mlir/lib/Conversion/VectorToLLVM/ConvertVectorToLLVM.cpp
@@ -290,6 +290,7 @@ class VectorGatherOpConversion
MemRefType memRefType = dyn_cast<MemRefType>(gather.getBaseType());
assert(memRefType && "The base should be bufferized");
+ // TODO: Add support for strided MemRef.
if (failed(isMemRefTypeSupported(memRefType, *this->getTypeConverter())))
return rewriter.notifyMatchFailure(gather, "memref type not supported");
@@ -348,6 +349,7 @@ class VectorScatterOpConversion
auto memRefType = dyn_cast<MemRefType>(scatter.getBaseType());
assert(memRefType && "The base should be bufferized");
+ // TODO: Add support for strided MemRef.
if (failed(isMemRefTypeSupported(memRefType, *this->getTypeConverter())))
return rewriter.notifyMatchFailure(scatter, "memref type not supported");
diff --git a/mlir/test/Conversion/VectorToLLVM/vector-to-llvm-interface.mlir b/mlir/test/Conversion/VectorToLLVM/vector-to-llvm-interface.mlir
index cb48ca3374e8d..3d075fe3b24ee 100644
--- a/mlir/test/Conversion/VectorToLLVM/vector-to-llvm-interface.mlir
+++ b/mlir/test/Conversion/VectorToLLVM/vector-to-llvm-interface.mlir
@@ -2052,6 +2052,20 @@ func.func @gather_with_alignment(%arg0: memref<?xf32>, %arg1: vector<3xi32>, %ar
// -----
+// TODO: Implement this lowering.
+func.func @negative_gather_on_strided_memref(%arg0: memref<?xf32, strided<[2], offset: ?>>, %arg1: vector<3xi32>, %arg2: vector<3xi1>, %arg3: vector<3xf32>) -> vector<3xf32> {
+ %0 = arith.constant 0: index
+ %1 = vector.gather %arg0[%0][%arg1], %arg2, %arg3
+ : memref<?xf32, strided<[2], offset: ?>>, vector<3xi32>, vector<3xi1>, vector<3xf32> into vector<3xf32>
+ return %1 : vector<3xf32>
+}
+
+// CHECK-LABEL: func @negative_gather_on_strided_memref
+// CHECK-NOT: llvm.intr.masked.gather
+// CHECK: vector.gather
+
+// -----
+
//===----------------------------------------------------------------------===//
// vector.scatter
//===----------------------------------------------------------------------===//
@@ -2138,6 +2152,19 @@ func.func @scatter_with_alignment(%arg0: memref<?xf32>, %arg1: vector<3xi32>, %a
// CHECK-LABEL: func @scatter_with_alignment
// CHECK: llvm.intr.masked.scatter %{{.*}}, %{{.*}}, %{{.*}} {alignment = 8 : i32} : vector<3xf32>, vector<3xi1> into vector<3x!llvm.ptr>
+// -----
+
+// TODO: Implement this lowering.
+func.func @negative_scatter_on_strided_memref(%arg0: memref<?xf32, strided<[2], offset: ?>>, %arg1: vector<3xi32>, %arg2: vector<3xi1>, %arg3: vector<3xf32>) {
+ %0 = arith.constant 0: index
+ vector.scatter %arg0[%0][%arg1], %arg2, %arg3
+ : memref<?xf32, strided<[2], offset: ?>>, vector<3xi32>, vector<3xi1>, vector<3xf32>
+ return
+}
+
+// CHECK-LABEL: func @negative_scatter_on_strided_memref
+// CHECK-NOT: llvm.intr.masked.scatter
+// CHECK: vector.scatter
// -----
diff --git a/mlir/test/Dialect/Vector/invalid.mlir b/mlir/test/Dialect/Vector/invalid.mlir
index 28e1206ff3d0a..94cb0fe70d3fc 100644
--- a/mlir/test/Dialect/Vector/invalid.mlir
+++ b/mlir/test/Dialect/Vector/invalid.mlir
@@ -2038,6 +2038,15 @@ func.func @load_non_pow_of_2_alignment(%memref: memref<4xi32>, %c0: index) {
// -----
+func.func @load_non_unit_stride(%src : memref<?xi8, strided<[2], offset: ?>>) {
+ %c0 = arith.constant 0 : index
+ // expected-error @+1 {{'vector.load' op most minor memref dim must have unit stride}}
+ %0 = vector.load %src[%c0] : memref<?xi8, strided<[2], offset: ?>>, vector<16xi8>
+ return
+}
+
+// -----
+
//===----------------------------------------------------------------------===//
// vector.store
//===----------------------------------------------------------------------===//
@@ -2064,3 +2073,11 @@ func.func @store_non_pow_of_2_alignment(%memref: memref<4xi32>, %val: vector<4xi
vector.store %val, %memref[%c0] { alignment = 3 } : memref<4xi32>, vector<4xi32>
return
}
+
+// -----
+
+func.func @store_non_unit_stride(%src : memref<?xi8, strided<[2], offset:?>>,%val : vector<16xi8>, %c0: index) {
+ // expected-error @below {{'vector.store' op most minor memref dim must have unit stride}}
+ vector.store %val, %src[%c0] : memref<?xi8, strided<[2], offset: ?>>, vector<16xi8>
+ return
+}
|
|
@llvm/pr-subscribers-mlir-vector Author: Andrzej Warzyński (banach-space) ChangesFor context, see: Currently, Supporting strided memrefs in the lowering is left as future work. This PR also adds tests for 4 Files Affected:
diff --git a/mlir/include/mlir/Dialect/Vector/IR/VectorOps.td b/mlir/include/mlir/Dialect/Vector/IR/VectorOps.td
index ddb04b6bbe40d..f49c578c91092 100644
--- a/mlir/include/mlir/Dialect/Vector/IR/VectorOps.td
+++ b/mlir/include/mlir/Dialect/Vector/IR/VectorOps.td
@@ -2087,7 +2087,10 @@ def Vector_GatherOp :
result[i,j] := if mask[i,j] then base[i0, i1, i2 + indices[i,j]]
else pass_thru[i,j]
```
- The index into `base` only varies in the innermost ((k-1)-th) dimension.
+ The index into `base` only varies in the innermost ((k-1)-th) dimension and
+ is treated as a logical rather than a physical index, i.e. it does not
+ encode any potential non-identity strides in the underlying MemRef layout
+ (Tensors do not model strided layouts, so only logical indexing is possible).
If a mask bit is set and the corresponding index is out-of-bounds for the
given base, the behavior is undefined. If a mask bit is not set, the value
@@ -2180,6 +2183,11 @@ def Vector_ScatterOp
is stored regardless of the index, and the index is allowed to be
out-of-bounds.
+ The index into `base` only varies in the innermost ((k-1)-th) dimension and
+ is treated as a logical rather than a physical index, i.e. it does not
+ encode any potential non-identity strides in the underlying MemRef layout
+ (Tensors do not model strided layouts, so only logical indexing is possible).
+
If the index vector contains two or more duplicate indices, the behavior is
undefined. Underlying implementation may enforce strict sequential
semantics.
diff --git a/mlir/lib/Conversion/VectorToLLVM/ConvertVectorToLLVM.cpp b/mlir/lib/Conversion/VectorToLLVM/ConvertVectorToLLVM.cpp
index 05d541fe80356..0a255c853b07e 100644
--- a/mlir/lib/Conversion/VectorToLLVM/ConvertVectorToLLVM.cpp
+++ b/mlir/lib/Conversion/VectorToLLVM/ConvertVectorToLLVM.cpp
@@ -290,6 +290,7 @@ class VectorGatherOpConversion
MemRefType memRefType = dyn_cast<MemRefType>(gather.getBaseType());
assert(memRefType && "The base should be bufferized");
+ // TODO: Add support for strided MemRef.
if (failed(isMemRefTypeSupported(memRefType, *this->getTypeConverter())))
return rewriter.notifyMatchFailure(gather, "memref type not supported");
@@ -348,6 +349,7 @@ class VectorScatterOpConversion
auto memRefType = dyn_cast<MemRefType>(scatter.getBaseType());
assert(memRefType && "The base should be bufferized");
+ // TODO: Add support for strided MemRef.
if (failed(isMemRefTypeSupported(memRefType, *this->getTypeConverter())))
return rewriter.notifyMatchFailure(scatter, "memref type not supported");
diff --git a/mlir/test/Conversion/VectorToLLVM/vector-to-llvm-interface.mlir b/mlir/test/Conversion/VectorToLLVM/vector-to-llvm-interface.mlir
index cb48ca3374e8d..3d075fe3b24ee 100644
--- a/mlir/test/Conversion/VectorToLLVM/vector-to-llvm-interface.mlir
+++ b/mlir/test/Conversion/VectorToLLVM/vector-to-llvm-interface.mlir
@@ -2052,6 +2052,20 @@ func.func @gather_with_alignment(%arg0: memref<?xf32>, %arg1: vector<3xi32>, %ar
// -----
+// TODO: Implement this lowering.
+func.func @negative_gather_on_strided_memref(%arg0: memref<?xf32, strided<[2], offset: ?>>, %arg1: vector<3xi32>, %arg2: vector<3xi1>, %arg3: vector<3xf32>) -> vector<3xf32> {
+ %0 = arith.constant 0: index
+ %1 = vector.gather %arg0[%0][%arg1], %arg2, %arg3
+ : memref<?xf32, strided<[2], offset: ?>>, vector<3xi32>, vector<3xi1>, vector<3xf32> into vector<3xf32>
+ return %1 : vector<3xf32>
+}
+
+// CHECK-LABEL: func @negative_gather_on_strided_memref
+// CHECK-NOT: llvm.intr.masked.gather
+// CHECK: vector.gather
+
+// -----
+
//===----------------------------------------------------------------------===//
// vector.scatter
//===----------------------------------------------------------------------===//
@@ -2138,6 +2152,19 @@ func.func @scatter_with_alignment(%arg0: memref<?xf32>, %arg1: vector<3xi32>, %a
// CHECK-LABEL: func @scatter_with_alignment
// CHECK: llvm.intr.masked.scatter %{{.*}}, %{{.*}}, %{{.*}} {alignment = 8 : i32} : vector<3xf32>, vector<3xi1> into vector<3x!llvm.ptr>
+// -----
+
+// TODO: Implement this lowering.
+func.func @negative_scatter_on_strided_memref(%arg0: memref<?xf32, strided<[2], offset: ?>>, %arg1: vector<3xi32>, %arg2: vector<3xi1>, %arg3: vector<3xf32>) {
+ %0 = arith.constant 0: index
+ vector.scatter %arg0[%0][%arg1], %arg2, %arg3
+ : memref<?xf32, strided<[2], offset: ?>>, vector<3xi32>, vector<3xi1>, vector<3xf32>
+ return
+}
+
+// CHECK-LABEL: func @negative_scatter_on_strided_memref
+// CHECK-NOT: llvm.intr.masked.scatter
+// CHECK: vector.scatter
// -----
diff --git a/mlir/test/Dialect/Vector/invalid.mlir b/mlir/test/Dialect/Vector/invalid.mlir
index 28e1206ff3d0a..94cb0fe70d3fc 100644
--- a/mlir/test/Dialect/Vector/invalid.mlir
+++ b/mlir/test/Dialect/Vector/invalid.mlir
@@ -2038,6 +2038,15 @@ func.func @load_non_pow_of_2_alignment(%memref: memref<4xi32>, %c0: index) {
// -----
+func.func @load_non_unit_stride(%src : memref<?xi8, strided<[2], offset: ?>>) {
+ %c0 = arith.constant 0 : index
+ // expected-error @+1 {{'vector.load' op most minor memref dim must have unit stride}}
+ %0 = vector.load %src[%c0] : memref<?xi8, strided<[2], offset: ?>>, vector<16xi8>
+ return
+}
+
+// -----
+
//===----------------------------------------------------------------------===//
// vector.store
//===----------------------------------------------------------------------===//
@@ -2064,3 +2073,11 @@ func.func @store_non_pow_of_2_alignment(%memref: memref<4xi32>, %val: vector<4xi
vector.store %val, %memref[%c0] { alignment = 3 } : memref<4xi32>, vector<4xi32>
return
}
+
+// -----
+
+func.func @store_non_unit_stride(%src : memref<?xi8, strided<[2], offset:?>>,%val : vector<16xi8>, %c0: index) {
+ // expected-error @below {{'vector.store' op most minor memref dim must have unit stride}}
+ vector.store %val, %src[%c0] : memref<?xi8, strided<[2], offset: ?>>, vector<16xi8>
+ return
+}
|
| The index into `base` only varies in the innermost ((k-1)-th) dimension and | ||
| is treated as a logical rather than a physical index, i.e. it does not | ||
| encode any potential non-identity strides in the underlying MemRef layout |
There was a problem hiding this comment.
I'm not sure the 'physical index' term is well-understood in general (physical addressing is also a thing and mostly orthogonal to this) -- maybe better to say what it is wrt to strides
| The index into `base` only varies in the innermost ((k-1)-th) dimension and | |
| is treated as a logical rather than a physical index, i.e. it does not | |
| encode any potential non-identity strides in the underlying MemRef layout | |
| The access into `base` is contiguous within the innermost ((k-1)-th) dimension and | |
| does not account for any potential non-identity strides in the underlying MemRef layout |
There was a problem hiding this comment.
Thanks!
The access into `base` is contiguous within the innermost ((k-1)-th) dimension a
This wouldn't be accurate - the point of vector.gather is that we can load non-contiguous elements. Let me try rephrasing while incorporating your suggestion.
This might take us a few iterations 😅
|
Re the "it only varies in the innermost dimension" (however we're phrasing that) - I'm not sure that's consistent with existing usage up in lowerings from That being said, I personally think that the high-level fix is to make the vector of offsets take K-tuples of indices (where 1 <= K < rank(base) |
The original wording risked implying a lowering decision; that’s why I removed it and replaced it with the concrete description:
I used "logical indexing" in my original commit (to match my RFC), but Jakub pointed out that we should try something clearer. Hopefully the updated version works! As for "consistency with lowering from Linalg" - that would be referring to the vectorization of
That would be a high-level change along the lines of https://discourse.llvm.org/t/rfc-improving-gather-codegen-for-vector-dialect? I feel that Discourse is more suitable for such a high-level discussion. As per the title of my PR, I merely want to clarify the existing semantics 😅 Thanks for all the discussion so far! |
|
Ping |
|
@banach-space Re your question about Linalg, the code I'm thinking of - which is doing a bunch of multiplies - is . |
| elements (not bytes). The offset is added to the underlying memory address, | ||
| so if the resulting position exceeds the size of the innermost dimension, | ||
| it naturally advances into the next row and/or plane according to the | ||
| identity (row-major) layout of 3D `base` (col = dim 0, row = dim 1, plane |
There was a problem hiding this comment.
Why is this mentioning 3D?
There was a problem hiding this comment.
The running example in the docs is 3D: https://mlir.llvm.org/docs/Dialects/Vector/#vectorgather-vectorgatherop
I can see how this could be confusing though, so let me tweak a bit.
Thanks! I am familiar with that implementation - I wrote it. I don't quite see where the consistency is? Why is the presence of multiplies significant here? To make the discussion a bit more concrete, here is an example: func.func @example(%src:tensor<24x80x16xf32>, %offset: index, %output : tensor<2x1x4xf32>) -> tensor<2x1x4xf32> {
%c13 = arith.constant 13 : index
%1 = linalg.generic {
indexing_maps = [affine_map<(d0, d1, d2) -> (d0, d1, d2)>],
iterator_types = ["parallel", "parallel", "parallel"]
} outs(%output : tensor<2x1x4xf32>) {
^bb0(%out: f32):
%idx_0 = linalg.index 0 : index
%idx_0_p_2 = affine.apply affine_map<(d0) -> (d0 + 2)>(%idx_0)
%idx_2 = linalg.index 2 : index
%idx_2_p_off = affine.apply affine_map<(d0, d1) -> (d0 + d1)>(%idx_2, %offset)
%val = tensor.extract %src[%idx_0_p_2, %idx_2_p_off, %c13] : tensor<24x80x16xf32>
linalg.yield %val : f32
} -> tensor<2x1x4xf32>
return %1 : tensor<2x1x4xf32>
}
module @transforms attributes { transform.with_named_sequence } {
transform.named_sequence @__transform_main(%module: !transform.any_op {transform.readonly}) {
%0 = transform.structured.match ops{["linalg.generic"]} in %module : (!transform.any_op) -> !transform.any_op
%1 = transform.get_parent_op %0 {isolated_from_above} : (!transform.any_op) -> !transform.any_op
%2 = transform.structured.vectorize_children_and_apply_patterns %1 { vectorize_nd_extract } : (!transform.any_op) -> !transform.any_op
transform.yield
}
}It vectorizes as: func.func @vectorize_nd_tensor_extract_with_affine_apply_gather(%arg0: tensor<24x80x16xf32>, %arg1: index, %arg2: tensor<2x1x4xf32>) -> tensor<2x1x4xf32> {
%cst = arith.constant dense<13> : vector<2x1x4xindex>
%cst_0 = arith.constant dense<16> : vector<2x1x4xindex>
%cst_1 = arith.constant dense<[160, 240]> : vector<2xindex>
%c0 = arith.constant 0 : index
%cst_2 = arith.constant dense<0.000000e+00> : vector<2x1x4xf32>
%cst_3 = arith.constant dense<true> : vector<2x1x4xi1>
%cst_4 = arith.constant dense<[0, 1, 2, 3]> : vector<4xindex>
%0 = vector.broadcast %arg1 : index to vector<4xindex>
%1 = arith.addi %0, %cst_4 : vector<4xindex>
%2 = vector.broadcast %cst_1 : vector<2xindex> to vector<4x1x2xindex>
%3 = vector.transpose %2, [2, 1, 0] : vector<4x1x2xindex> to vector<2x1x4xindex>
%4 = vector.broadcast %1 : vector<4xindex> to vector<2x1x4xindex>
%5 = arith.addi %4, %3 : vector<2x1x4xindex>
%6 = arith.muli %5, %cst_0 : vector<2x1x4xindex>
%7 = arith.addi %6, %cst : vector<2x1x4xindex>
%8 = vector.gather %arg0[%c0, %c0, %c0] [%7], %cst_3, %cst_2 : tensor<24x80x16xf32>, vector<2x1x4xindex>, vector<2x1x4xi1>, vector<2x1x4xf32> into vector<2x1x4xf32>
%9 = vector.transfer_write %8, %arg2[%c0, %c0, %c0] {in_bounds = [true, true, true]} : vector<2x1x4xf32>, tensor<2x1x4xf32>
return %9 : tensor<2x1x4xf32>
}This looks OK to me - a vector of indices, one per output element in the output vector, is fed into %7 = arith.addi %6, %cst : vector<2x1x4xindex>
%8 = vector.gather %arg0[%c0, %c0, %c0] [%7], %cst_3, %cst_2 : tensor<24x80x16xf32>, vector<2x1x4xindex>, vector<2x1x4xi1>, vector<2x1x4xf32> into vector<2x1x4xf32>Could you point me at the inconsistency that you are referring to? That would really help. Let me also note, the documentation of |
|
This PR only aims to:
If you think either of these goals is not met, please let me know. Otherwise, if there are no objections, I would plan to land this so we can close the loop on the semantics discussion. Btw, @krzysz00 - I’m happy to continue the discussion about the inconsistency. However, I don’t think this PR changes much in that regard; it simply rephrases the current documentation and adds tests capturing the existing semantics, without modifying the behaviour itself. |
|
Thanks!
I'm not sure what inconsistency you are referring to, so I would appreciate if you could be more specific. As mentioned in my reply in that PR, I believe that this PR and @hanhanW's PR are consistent.
The intent of this PR is to clarify the existing semantics. You are of course welcome to propose a change to the op's design if you think that would be a better long-term solution. In the meantime, I assume that you agree that this PR implements what we (including yourself) agreed on in https://discourse.llvm.org/t/rfc-semantics-of-vector-gather-indices-with-strided-memrefs/89722 - I have not seen any comments stating otherwise. |
|
So, this PR implements the semantics I was pushing for initially with its "going off the end of the last dimension proceeds as if in the identity layout" wording. That definition doesn't match the lowering implemented in #184706 , which attempts to account for the multi-dimensionality of the memref, and has semantics that are meaningfully different from lowering directly to something like LLVM's gather. I also had the sense that your initial RFC was disagreeing with the sort of "ignore subviews" indexing I'd been calling for when I first tried to clarify vector.gather semantics, and now we've ended up back where I started. So, to state the question: If we let as your documentation update and VectorToLLVM suggests, or as the new delinearization-based change suggests? |
|
Also, my recollection of the RFC was that we were explicitly leaving the case I'm asking about as undefined behavior - as in, you're not allowed to do that currently - because of this exact bit of confusion. |
For context, see: * https://discourse.llvm.org/t/rfc-semantics-of-vector-gather-indices-with-strided-memrefs Currently, `ConvertVectorToLLVM` rejects strided memrefs when lowering `vector.gather` and `vector.scatter`. This PR adds tests to document that behavior. Supporting strided memrefs in the lowering is left as future work. However, it is still unclear whether gather/scatter on strided memrefs should be supported at all (see the Discourse discussion above). This PR also adds tests for `vector.load` and `vector.store` in `invalid.mlir` to document that these ops do not support strided memrefs.
b0dff97 to
fd02f73
Compare
|
UPDATE 17/3/26 In order to de-noise this PR, I have moved test changes to #186990. |
Just to clarify: this PR does not implement new semantics - these are NFC documentation changes. (see also my comment below re: trimming this patch)
That PR addresses the lowering of In particular, this accounts for:
Effectively, the ( That said, I’m happy to take a step back and trim the part of this PR where we don’t yet have agreement - see this commit: 9513d44. Thanks for pointing that out. Just to make sure I understand your concern correctly: are you disagreeing with the direction taken in #184706, or with the "logical vs physical" index distinction from the RFC discussion? Are you OK with the current wording in this PR?
Good catch - that should indeed be fixed. Given the confusion and the length of the review (~1 month), I’d like to bring in more eyes. @rengolin - since you participated in the Discourse discussion, would you mind taking a look? (As per Jakub’s suggestion, I’ve avoided using “physical” vs “logical” terminology in the docs.) |
| in units of elements from the starting element, i.e. `%base[%ofs_0, %ofs_1, | ||
| %ofs_2]` for the example above. Importantly, for MemRefs, `indices` are | ||
| %interpreted assuming an identity (contiguous) MemRef | ||
| layout and do not account for non-identity strides. |
There was a problem hiding this comment.
What does do not account mean?
Is it an error (and therefore needs to update the verifier)? Or does it just ignore the stride and take from outside of the domain (and therefore becomes undefined behaviour)?
There was a problem hiding this comment.
By “do not account”, I mean that the indices are interpreted purely in terms of logical element offsets, independent of the MemRef’s physical layout.
In particular:
- this is not an error, and does not require verifier changes, and
- the semantics are defined in terms of the logical view of the MemRef, as if it had an identity (contiguous) layout.
Any non-identity layout (i.e. strides) is handled during lowering, where the logical indexing is mapped to physical addresses.
I’ll update the wording to make this clearer.
There was a problem hiding this comment.
So I'd like to be clear that I don't like the "you have to unfold the index according to the shape of the memref" semantics that are being proposed here - they make the lowering of what's supposed to be a low-level operation extremely non-trivial, and make rewrites (such as folding in a subview) a rather painful pile of index computations.
This is why I'm pushing for "the index just gets added to the last dimension, and if that goes oob, so be it" and the handling of, say, gathering along multiple dimensions involving either k vectors of indices (one per dimension) or a vector of k indices per vector element.
There was a problem hiding this comment.
- the semantics are defined in terms of the logical view of the MemRef, as if it had an identity (contiguous) layout.
So:
%value: memref<10x10xf32, stride=[64,64]>
%indices: vector<2x3xi32>
%mask: vector<2x3xi1>
%pass_thru: vector<2x3xf32>)
%result = vector.gather %base[9, 9][%indices], %mask, %pass_thru : ...will read OOB?
There was a problem hiding this comment.
Depends on the contents of indices.
I'm also going to add the argument against the delinearizaiton semantics that vector.load %m[%x] : memref<?xT>, vector<NxT> is exactly equivalent to
vector.gather %m[%x][seq(0, N)] mask = broadcast(true), passthrough = broadcast(poison) : memref<?xT>, vector<Nxindex>, vector<Nxi1>, vector<NxT> -> vector<NxT>
including OOB semantics
There was a problem hiding this comment.
Test case for the inconsistency #187071 (comment)
There was a problem hiding this comment.
will read OOB?
If your indices are > 0 and mask is "all true", then OOB, yes. At least based on the available context.
I guess that you could consider %value to be a view into a larger source MemRef and in the context of that MemRef it would possibly be OK? I think that we should stick with the "local" view though.
Test case for the inconsistency #187071 (comment)
Thanks! I created #187215 so that we have one central place to discuss that and so that there's a repro that people can use.
For context, see: