[Mlir-commits] [mlir] [mlir][vector] Add verification for incorrect vector.extract (PR #115824)
Kunwar Grover
llvmlistbot at llvm.org
Tue Nov 26 07:29:26 PST 2024
================
@@ -1339,6 +1339,83 @@ bool ExtractOp::isCompatibleReturnTypes(TypeRange l, TypeRange r) {
return l == r;
}
+// Common verification rules for `InsertOp` and `ExtractOp` involving indices
+// and shapes. `indexedType` is the vector type being indexed by the operation,
+// i.e., the destination type in `InsertOp` and the source type in `ExtractOp`.
+// `nonIndexedType` is the inserted or extracted type by an `InsertOp` or and
+// `ExtractOp`, respectively.
+static LogicalResult verifyInsertExtractIndicesAndShapes(Operation *op,
+ VectorType indexedType,
+ int64_t numIndices,
+ Type nonIndexedType) {
+ assert((isa<InsertOp>(op) || isa<ExtractOp>(op)) &&
+ "Expected InsertOp or ExtractOp");
+
+ std::string nonIndexedStr = isa<InsertOp>(op) ? "inserted" : "extracted";
+ std::string indexedStr = isa<InsertOp>(op) ? "destination" : "source";
+ int64_t indexedRank = indexedType.getRank();
+ if (numIndices > indexedRank) {
+ return op->emitOpError()
+ << "expected a number of indices no greater than the " << indexedStr
+ << " vector rank";
+ }
+
+ if (auto nonIndexedVecType = dyn_cast<VectorType>(nonIndexedType)) {
+ // Vector case, including meaningful cases such as:
+ // * 0-D vector:
+ // * vector.extract %src[2]: vector<f32> from vector<8xf32)
+ // * vector.insert %src, %dst[3]: vector<f32> into vector<8xf32>
+ // * One-element vector:
+ // * vector.extract %src[4]: vector<1xf32> from vector<8xf32>
+ // * vector.insert %src, %dst[1]: vector<1xf32> into vector<8xf32>
+ // * vector.extract %src[7]: vector<1xf32> from vector<8x1xf32>
+ // * vector.insert %src, %dst[5]: vector<1xf32> into vector<8x1xf32>
----------------
Groverkss wrote:
Note that Possibility 2 is what the documentation documents:
https://mlir.llvm.org/docs/Dialects/Vector/#vectorextract-vectorextractop
```
Takes an n-D vector and a k-D position and extracts the (n-k)-D vector at the proper position. Degenerates to an element type if n-k is zero.
```
Suprisingly, vector.insert documentation diverges from this:
https://mlir.llvm.org/docs/Dialects/Vector/#vectorinsert-vectorinsertop
```
Takes an n-D source vector, an (n+k)-D destination vector and a k-D position and inserts the n-D source into the (n+k)-D destination at the proper position. Degenerates to a scalar or a 0-d vector source type when n = 0.
```
I'm not sure what degenerates to "0-d source type" means, but we should make it same as vector.extract and it would be easier.
https://github.com/llvm/llvm-project/pull/115824
More information about the Mlir-commits
mailing list