aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Raoux <thomasraoux@google.com>2020-08-26 14:13:52 -0700
committerThomas Raoux <thomasraoux@google.com>2020-08-26 14:13:52 -0700
commit5fbfe2ec4f8baf6a4729f9dc2e4fe16f269921eb (patch)
tree4ebb29595c6466df80cf5bcf971c47a8e5c83a03
parent5d989fb37d7cfb4f7766a45d4efc82b5add3811f (diff)
downloadllvm-5fbfe2ec4f8baf6a4729f9dc2e4fe16f269921eb.zip
llvm-5fbfe2ec4f8baf6a4729f9dc2e4fe16f269921eb.tar.gz
llvm-5fbfe2ec4f8baf6a4729f9dc2e4fe16f269921eb.tar.bz2
[mlir][vector] Add vector.bitcast operation
Based on the RFC discussed here: https://llvm.discourse.group/t/rfc-vector-standard-add-bitcast-operation/1628/ Adding a vector.bitcast operation that allows casting to a vector of different element type. The most minor dimension bitwidth must stay unchanged. Differential Revision: https://reviews.llvm.org/D86580
-rw-r--r--mlir/include/mlir/Dialect/Vector/VectorOps.td35
-rw-r--r--mlir/lib/Dialect/Vector/VectorOps.cpp36
-rw-r--r--mlir/test/Dialect/Vector/canonicalize.mlir13
-rw-r--r--mlir/test/Dialect/Vector/invalid.mlir28
-rw-r--r--mlir/test/Dialect/Vector/ops.mlir27
5 files changed, 139 insertions, 0 deletions
diff --git a/mlir/include/mlir/Dialect/Vector/VectorOps.td b/mlir/include/mlir/Dialect/Vector/VectorOps.td
index 3dc01b3..22fd036 100644
--- a/mlir/include/mlir/Dialect/Vector/VectorOps.td
+++ b/mlir/include/mlir/Dialect/Vector/VectorOps.td
@@ -1525,6 +1525,41 @@ def Vector_ShapeCastOp :
let hasFolder = 1;
}
+def Vector_BitCastOp :
+ Vector_Op<"bitcast", [NoSideEffect, AllRanksMatch<["source", "result"]>]>,
+ Arguments<(ins AnyVector:$source)>,
+ Results<(outs AnyVector:$result)>{
+ let summary = "bitcast casts between vectors";
+ let description = [{
+ The bitcast operation casts between vectors of the same rank, the minor 1-D
+ vector size is casted to a vector with a different element type but same
+ bitwidth.
+
+ Example:
+
+ ```mlir
+ // Example casting to a smaller element type.
+ %1 = vector.bitcast %0 : vector<5x1x4x3xf32> to vector<5x1x4x6xi16>
+
+ // Example casting to a bigger element type.
+ %3 = vector.bitcast %2 : vector<10x12x8xi8> to vector<10x12x2xi32>
+
+ // Example casting to an element type of the same size.
+ %5 = vector.bitcast %4 : vector<5x1x4x3xf32> to vector<5x1x4x3xi32>
+ ```
+ }];
+ let extraClassDeclaration = [{
+ VectorType getSourceVectorType() {
+ return source().getType().cast<VectorType>();
+ }
+ VectorType getResultVectorType() {
+ return getResult().getType().cast<VectorType>();
+ }
+ }];
+ let assemblyFormat = "$source attr-dict `:` type($source) `to` type($result)";
+ let hasFolder = 1;
+}
+
def Vector_TypeCastOp :
Vector_Op<"type_cast", [NoSideEffect]>,
Arguments<(ins StaticShapeMemRefOf<[AnyType]>:$memref)>,
diff --git a/mlir/lib/Dialect/Vector/VectorOps.cpp b/mlir/lib/Dialect/Vector/VectorOps.cpp
index d3eebc3..7fa62ea 100644
--- a/mlir/lib/Dialect/Vector/VectorOps.cpp
+++ b/mlir/lib/Dialect/Vector/VectorOps.cpp
@@ -2301,6 +2301,42 @@ OpFoldResult ShapeCastOp::fold(ArrayRef<Attribute> operands) {
}
//===----------------------------------------------------------------------===//
+// VectorBitCastOp
+//===----------------------------------------------------------------------===//
+
+static LogicalResult verify(BitCastOp op) {
+ auto sourceVectorType = op.getSourceVectorType();
+ auto resultVectorType = op.getResultVectorType();
+
+ for (int64_t i = 0, e = sourceVectorType.getRank() - 1; i < e; i++) {
+ if (sourceVectorType.getDimSize(i) != resultVectorType.getDimSize(i))
+ return op.emitOpError("dimension size mismatch at: ") << i;
+ }
+
+ if (sourceVectorType.getElementTypeBitWidth() *
+ sourceVectorType.getShape().back() !=
+ resultVectorType.getElementTypeBitWidth() *
+ resultVectorType.getShape().back())
+ return op.emitOpError(
+ "source/result bitwidth of the minor 1-D vectors must be equal");
+
+ return success();
+}
+
+OpFoldResult BitCastOp::fold(ArrayRef<Attribute> operands) {
+ // Nop cast.
+ if (source().getType() == result().getType())
+ return source();
+
+ // Canceling bitcasts.
+ if (auto otherOp = source().getDefiningOp<BitCastOp>())
+ if (result().getType() == otherOp.source().getType())
+ return otherOp.source();
+
+ return {};
+}
+
+//===----------------------------------------------------------------------===//
// TypeCastOp
//===----------------------------------------------------------------------===//
diff --git a/mlir/test/Dialect/Vector/canonicalize.mlir b/mlir/test/Dialect/Vector/canonicalize.mlir
index bd6ef41..1b1362f 100644
--- a/mlir/test/Dialect/Vector/canonicalize.mlir
+++ b/mlir/test/Dialect/Vector/canonicalize.mlir
@@ -372,3 +372,16 @@ func @fold_vector_transfers(%A: memref<?x8xf32>) -> (vector<4x8xf32>, vector<4x9
// CHECK: return
return %1, %2 : vector<4x8xf32>, vector<4x9xf32>
}
+
+// -----
+
+// CHECK-LABEL: bitcast_folding
+// CHECK-SAME: %[[A:.*]]: vector<4x8xf32>
+// CHECK-SAME: %[[B:.*]]: vector<2xi32>
+// CHECK: return %[[A]], %[[B]] : vector<4x8xf32>, vector<2xi32>
+func @bitcast_folding(%I1: vector<4x8xf32>, %I2: vector<2xi32>) -> (vector<4x8xf32>, vector<2xi32>) {
+ %0 = vector.bitcast %I1 : vector<4x8xf32> to vector<4x8xf32>
+ %1 = vector.bitcast %I2 : vector<2xi32> to vector<4xi16>
+ %2 = vector.bitcast %1 : vector<4xi16> to vector<2xi32>
+ return %0, %2 : vector<4x8xf32>, vector<2xi32>
+}
diff --git a/mlir/test/Dialect/Vector/invalid.mlir b/mlir/test/Dialect/Vector/invalid.mlir
index 71d2989..3a08123 100644
--- a/mlir/test/Dialect/Vector/invalid.mlir
+++ b/mlir/test/Dialect/Vector/invalid.mlir
@@ -1065,6 +1065,34 @@ func @shape_cast_different_tuple_sizes(
// -----
+func @bitcast_not_vector(%arg0 : vector<5x1x3x2xf32>) {
+ // expected-error@+1 {{must be vector of any type values}}
+ %0 = vector.bitcast %arg0 : vector<5x1x3x2xf32> to f32
+}
+
+// -----
+
+func @bitcast_rank_mismatch(%arg0 : vector<5x1x3x2xf32>) {
+ // expected-error@+1 {{op failed to verify that all of {source, result} have same rank}}
+ %0 = vector.bitcast %arg0 : vector<5x1x3x2xf32> to vector<5x3x2xf32>
+}
+
+// -----
+
+func @bitcast_shape_mismatch(%arg0 : vector<5x1x3x2xf32>) {
+ // expected-error@+1 {{op dimension size mismatch}}
+ %0 = vector.bitcast %arg0 : vector<5x1x3x2xf32> to vector<5x2x3x2xf32>
+}
+
+// -----
+
+func @bitcast_sizemismatch(%arg0 : vector<5x1x3x2xf32>) {
+ // expected-error@+1 {{op source/result bitwidth of the minor 1-D vectors must be equal}}
+ %0 = vector.bitcast %arg0 : vector<5x1x3x2xf32> to vector<5x1x3x3xf16>
+}
+
+// -----
+
func @reduce_unknown_kind(%arg0: vector<16xf32>) -> f32 {
// expected-error@+1 {{'vector.reduction' op unknown reduction kind: joho}}
%0 = vector.reduction "joho", %arg0 : vector<16xf32> into f32
diff --git a/mlir/test/Dialect/Vector/ops.mlir b/mlir/test/Dialect/Vector/ops.mlir
index 6f9990d..2a62be9 100644
--- a/mlir/test/Dialect/Vector/ops.mlir
+++ b/mlir/test/Dialect/Vector/ops.mlir
@@ -298,6 +298,33 @@ func @shape_cast(%arg0 : vector<5x1x3x2xf32>,
return %0, %1, %2, %3, %4 : vector<15x2xf32>, tuple<vector<20x2xf32>, vector<12x2xf32>>, vector<8xf32>, vector<16xf32>, vector<16x1xf32>
}
+// CHECK-LABEL: @bitcast
+func @bitcast(%arg0 : vector<5x1x3x2xf32>,
+ %arg1 : vector<8x1xi32>,
+ %arg2 : vector<16x1x8xi8>)
+ -> (vector<5x1x3x4xf16>, vector<5x1x3x8xi8>, vector<8x4xi8>, vector<8x1xf32>, vector<16x1x2xi32>, vector<16x1x4xi16>) {
+
+ // CHECK: vector.bitcast %{{.*}} : vector<5x1x3x2xf32> to vector<5x1x3x4xf16>
+ %0 = vector.bitcast %arg0 : vector<5x1x3x2xf32> to vector<5x1x3x4xf16>
+
+ // CHECK-NEXT: vector.bitcast %{{.*}} : vector<5x1x3x2xf32> to vector<5x1x3x8xi8>
+ %1 = vector.bitcast %arg0 : vector<5x1x3x2xf32> to vector<5x1x3x8xi8>
+
+ // CHECK-NEXT: vector.bitcast %{{.*}} : vector<8x1xi32> to vector<8x4xi8>
+ %2 = vector.bitcast %arg1 : vector<8x1xi32> to vector<8x4xi8>
+
+ // CHECK-NEXT: vector.bitcast %{{.*}} : vector<8x1xi32> to vector<8x1xf32>
+ %3 = vector.bitcast %arg1 : vector<8x1xi32> to vector<8x1xf32>
+
+ // CHECK-NEXT: vector.bitcast %{{.*}} : vector<16x1x8xi8> to vector<16x1x2xi32>
+ %4 = vector.bitcast %arg2 : vector<16x1x8xi8> to vector<16x1x2xi32>
+
+ // CHECK-NEXT: vector.bitcast %{{.*}} : vector<16x1x8xi8> to vector<16x1x4xi16>
+ %5 = vector.bitcast %arg2 : vector<16x1x8xi8> to vector<16x1x4xi16>
+
+ return %0, %1, %2, %3, %4, %5 : vector<5x1x3x4xf16>, vector<5x1x3x8xi8>, vector<8x4xi8>, vector<8x1xf32>, vector<16x1x2xi32>, vector<16x1x4xi16>
+}
+
// CHECK-LABEL: @vector_fma
func @vector_fma(%a: vector<8xf32>, %b: vector<8x4xf32>) {
// CHECK: vector.fma %{{.*}} : vector<8xf32>