[mlir][affine] SuperVectorizer only widen ops with valid types
authorJoshua Cao <cao.joshua@yahoo.com>
Thu, 6 Apr 2023 04:47:59 +0000 (21:47 -0700)
committerJoshua Cao <cao.joshua@yahoo.com>
Thu, 13 Apr 2023 04:59:15 +0000 (21:59 -0700)
fixes https://github.com/llvm/llvm-project/issues/61309

Differential Revision: https://reviews.llvm.org/D147679

mlir/lib/Dialect/Affine/Analysis/LoopAnalysis.cpp
mlir/test/Dialect/Affine/SuperVectorize/invalid_type.mlir [new file with mode: 0644]

index 81d0655..6b028b7 100644 (file)
@@ -279,6 +279,25 @@ isVectorizableLoopBodyWithOpCond(AffineForOp loop,
     return false;
   }
 
+  // No vectorization for ops with operand or result types that are not
+  // vectorizable.
+  auto types = matcher::Op([](Operation &op) -> bool {
+    if (llvm::any_of(op.getOperandTypes(), [](Type type) {
+          if (MemRefType t = dyn_cast<MemRefType>(type))
+            return !VectorType::isValidElementType(t.getElementType());
+          return !VectorType::isValidElementType(type);
+        }))
+      return true;
+    return llvm::any_of(op.getResultTypes(), [](Type type) {
+      return !VectorType::isValidElementType(type);
+    });
+  });
+  SmallVector<NestedMatch, 8> opsMatched;
+  types.match(forOp, &opsMatched);
+  if (!opsMatched.empty()) {
+    return false;
+  }
+
   // No vectorization across unknown regions.
   auto regions = matcher::Op([](Operation &op) -> bool {
     return op.getNumRegions() != 0 && !isa<AffineIfOp, AffineForOp>(op);
diff --git a/mlir/test/Dialect/Affine/SuperVectorize/invalid_type.mlir b/mlir/test/Dialect/Affine/SuperVectorize/invalid_type.mlir
new file mode 100644 (file)
index 0000000..65df957
--- /dev/null
@@ -0,0 +1,31 @@
+// RUN: mlir-opt %s -affine-super-vectorize="virtual-vector-size=128" -split-input-file | FileCheck %s
+
+// CHECK-LABEL: func @invalid_operand
+func.func @invalid_operand(%a : vector<4xf32>, %b : vector<4xf32>) {
+// CHECK: affine.for %{{.*}} = 0 to 10
+// CHECK:   %{{.*}} = vector.reduction <add>, %{{.*}} : vector<4xf32> into f32
+// CHECK: }
+// CHECK: return
+  affine.for %j = 0 to 10 {
+    %1 = vector.reduction <add>, %a : vector<4xf32> into f32
+  }
+  return
+}
+
+// CHECK-LABEL: func @invalid_result
+func.func @invalid_result(%a : memref<10x20xf32>, %b : memref<10x20xf32>) {
+// CHECK: affine.for %{{.*}} = 0 to 10
+// CHECK:   affine.for %{{.*}} = 0 to 5
+// CHECK:     %{{.*}} = affine.vector_load %{{.*}}[%{{.*}}, %{{.*}}] : memref<10x20xf32>, vector<4xf32>
+// CHECK:     affine.vector_store %{{.*}}, %{{.*}}[%{{.*}}, %{{.*}}] : memref<10x20xf32>, vector<4xf32>
+// CHECK:   }
+// CHECK: }
+// CHECK: return
+  affine.for %j = 0 to 10 {
+    affine.for %i = 0 to 5 {
+      %ld0 = affine.vector_load %a[%j, %i] : memref<10x20xf32>, vector<4xf32>
+      affine.vector_store %ld0, %b[%j, %i] : memref<10x20xf32>, vector<4xf32>
+    }
+  }
+  return
+}