[VectorOps] Fix off-by-one error in insert/extract validation
authorAart Bik <ajcbik@google.com>
Mon, 9 Dec 2019 23:53:50 +0000 (15:53 -0800)
committerA. Unique TensorFlower <gardener@tensorflow.org>
Mon, 9 Dec 2019 23:54:23 +0000 (15:54 -0800)
PiperOrigin-RevId: 284652653

mlir/lib/Dialect/VectorOps/VectorOps.cpp
mlir/test/Dialect/VectorOps/invalid.mlir

index 19581df..08bb8f4 100644 (file)
@@ -403,7 +403,7 @@ static LogicalResult verify(vector::ExtractOp op) {
   for (auto en : llvm::enumerate(positionAttr)) {
     auto attr = en.value().dyn_cast<IntegerAttr>();
     if (!attr || attr.getInt() < 0 ||
-        attr.getInt() > op.getVectorType().getDimSize(en.index()))
+        attr.getInt() >= op.getVectorType().getDimSize(en.index()))
       return op.emitOpError("expected position attribute #")
              << (en.index() + 1)
              << " to be a non-negative integer smaller than the corresponding "
@@ -517,7 +517,7 @@ static LogicalResult verify(InsertOp op) {
   for (auto en : llvm::enumerate(positionAttr)) {
     auto attr = en.value().dyn_cast<IntegerAttr>();
     if (!attr || attr.getInt() < 0 ||
-        attr.getInt() > destVectorType.getDimSize(en.index()))
+        attr.getInt() >= destVectorType.getDimSize(en.index()))
       return op.emitOpError("expected position attribute #")
              << (en.index() + 1)
              << " to be a non-negative integer smaller than the corresponding "
index a0faa37..d6aa291 100644 (file)
@@ -66,6 +66,13 @@ func @extract_position_overflow(%arg0: vector<4x8x16xf32>) {
 
 // -----
 
+func @extract_precise_position_overflow(%arg0: vector<4x8x16xf32>) {
+  // expected-error@+1 {{expected position attribute #3 to be a non-negative integer smaller than the corresponding vector dimension}}
+  %1 = vector.extract %arg0[3 : i32, 7 : i32, 16 : i32] : vector<4x8x16xf32>
+}
+
+// -----
+
 func @extract_position_overflow(%arg0: vector<4x8x16xf32>) {
   // expected-error@+1 {{expected position attribute #3 to be a non-negative integer smaller than the corresponding vector dimension}}
   %1 = vector.extract %arg0[0 : i32, 0 : i32, -1 : i32] : vector<4x8x16xf32>
@@ -108,6 +115,13 @@ func @insert_position_overflow(%a: f32, %b: vector<4x8x16xf32>) {
 
 // -----
 
+func @insert_precise_position_overflow(%a: f32, %b: vector<4x8x16xf32>) {
+  // expected-error@+1 {{expected position attribute #1 to be a non-negative integer smaller than the corresponding dest vector dimension}}
+  %1 = vector.insert %a, %b[4 : i32, 7 : i32, 15 : i32] : f32 into vector<4x8x16xf32>
+}
+
+// -----
+
 func @outerproduct_num_operands(%arg0: f32) {
   // expected-error@+1 {{expected at least 2 operands}}
   %1 = vector.outerproduct %arg0 : f32, f32