1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
|
// RUN: mlir-opt --arith-int-narrowing="int-bitwidths-supported=1,8,16,32" \
// RUN: --verify-diagnostics %s | FileCheck %s
// Check that we can calculate `linalg.index` value bounds and use them to
// optimize index casts.
//===----------------------------------------------------------------------===//
// arith.index_cast
//===----------------------------------------------------------------------===//
// CHECK-LABEL: func @linalg_indexcast_dim_0_i8
// CHECK: %[[IDX:.+]] = linalg.index 0 : index
// CHECK-NEXT: %[[INT:.+]] = arith.index_cast %[[IDX]] : index to i8
// CHECK-NEXT: %[[FP:.+]] = arith.sitofp %[[INT]] : i8 to f16
// CHECK-NEXT: linalg.yield %[[FP]] : f16
func.func @linalg_indexcast_dim_0_i8(%arg0: tensor<f16>) -> tensor<128xf16> {
%init = tensor.empty() : tensor<128xf16>
%res = linalg.generic {
indexing_maps = [affine_map<(d0) -> ()>, affine_map<(d0) -> (d0)>],
iterator_types = ["parallel"]
}
ins(%arg0 : tensor<f16>)
outs(%init : tensor<128xf16>) {
^bb0(%in: f16, %out: f16):
%idx = linalg.index 0 : index
%int = arith.index_cast %idx : index to i64
%fp = arith.sitofp %int : i64 to f16
linalg.yield %fp : f16
} -> tensor<128xf16>
return %res : tensor<128xf16>
}
// CHECK-LABEL: func @linalg_indexcast_dim_1_i16
// CHECK: %[[IDX:.+]] = linalg.index 1 : index
// CHECK-NEXT: %[[INT:.+]] = arith.index_cast %[[IDX]] : index to i16
// CHECK-NEXT: %[[FP:.+]] = arith.sitofp %[[INT]] : i16 to f16
// CHECK-NEXT: linalg.yield %[[FP]] : f16
func.func @linalg_indexcast_dim_1_i16(%arg0: tensor<f16>, %arg1: tensor<?x129xf16>) -> tensor<?x129xf16> {
%res = linalg.generic {
indexing_maps = [affine_map<(d0, d1) -> ()>, affine_map<(d0, d1) -> (d0, d1)>],
iterator_types = ["parallel", "parallel"]
}
ins(%arg0 : tensor<f16>)
outs(%arg1 : tensor<?x129xf16>) {
^bb0(%in: f16, %out: f16):
%idx = linalg.index 1 : index
%int = arith.index_cast %idx : index to i64
%fp = arith.sitofp %int : i64 to f16
linalg.yield %fp : f16
} -> tensor<?x129xf16>
return %res : tensor<?x129xf16>
}
// CHECK-LABEL: func @linalg_indexcast_dynamic_dim_i64
// CHECK: %[[IDX:.+]] = linalg.index 0 : index
// CHECK-NEXT: %[[INT:.+]] = arith.index_cast %[[IDX]] : index to i64
// CHECK-NEXT: %[[FP:.+]] = arith.sitofp %[[INT]] : i64 to f16
// CHECK-NEXT: linalg.yield %[[FP]] : f16
func.func @linalg_indexcast_dynamic_dim_i64(%arg0: tensor<f16>, %arg1: tensor<?xf16>) -> tensor<?xf16> {
%res = linalg.generic {
indexing_maps = [affine_map<(d0) -> ()>, affine_map<(d0) -> (d0)>],
iterator_types = ["parallel"]
}
ins(%arg0 : tensor<f16>)
outs(%arg1 : tensor<?xf16>) {
^bb0(%in: f16, %out: f16):
%idx = linalg.index 0 : index
%int = arith.index_cast %idx : index to i64
%fp = arith.sitofp %int : i64 to f16
linalg.yield %fp : f16
} -> tensor<?xf16>
return %res : tensor<?xf16>
}
//===----------------------------------------------------------------------===//
// arith.index_castui
//===----------------------------------------------------------------------===//
// CHECK-LABEL: func @linalg_indexcastui_dim_0_i8
// CHECK: %[[IDX:.+]] = linalg.index 0 : index
// CHECK-NEXT: %[[INT:.+]] = arith.index_castui %[[IDX]] : index to i8
// CHECK-NEXT: %[[FP:.+]] = arith.uitofp %[[INT]] : i8 to f16
// CHECK-NEXT: linalg.yield %[[FP]] : f16
func.func @linalg_indexcastui_dim_0_i8(%arg0: tensor<f16>) -> tensor<256xf16> {
%init = tensor.empty() : tensor<256xf16>
%res = linalg.generic {
indexing_maps = [affine_map<(d0) -> ()>, affine_map<(d0) -> (d0)>],
iterator_types = ["parallel"]
}
ins(%arg0 : tensor<f16>)
outs(%init : tensor<256xf16>) {
^bb0(%in: f16, %out: f16):
%idx = linalg.index 0 : index
%int = arith.index_castui %idx : index to i64
%fp = arith.uitofp %int : i64 to f16
linalg.yield %fp : f16
} -> tensor<256xf16>
return %res : tensor<256xf16>
}
// CHECK-LABEL: func @linalg_indexcastui_dim_1_i16
// CHECK: %[[IDX:.+]] = linalg.index 1 : index
// CHECK-NEXT: %[[INT:.+]] = arith.index_castui %[[IDX]] : index to i16
// CHECK-NEXT: %[[FP:.+]] = arith.uitofp %[[INT]] : i16 to f16
// CHECK-NEXT: linalg.yield %[[FP]] : f16
func.func @linalg_indexcastui_dim_1_i16(%arg0: tensor<f16>, %arg1: tensor<?x257xf16>) -> tensor<?x257xf16> {
%res = linalg.generic {
indexing_maps = [affine_map<(d0, d1) -> ()>, affine_map<(d0, d1) -> (d0, d1)>],
iterator_types = ["parallel", "parallel"]
}
ins(%arg0 : tensor<f16>)
outs(%arg1 : tensor<?x257xf16>) {
^bb0(%in: f16, %out: f16):
%idx = linalg.index 1 : index
%int = arith.index_castui %idx : index to i64
%fp = arith.uitofp %int : i64 to f16
linalg.yield %fp : f16
} -> tensor<?x257xf16>
return %res : tensor<?x257xf16>
}
// CHECK-LABEL: func @linalg_indexcastui_dynamic_dim_i64
// CHECK: %[[IDX:.+]] = linalg.index 0 : index
// CHECK-NEXT: %[[INT:.+]] = arith.index_castui %[[IDX]] : index to i64
// CHECK-NEXT: %[[FP:.+]] = arith.uitofp %[[INT]] : i64 to f16
// CHECK-NEXT: linalg.yield %[[FP]] : f16
func.func @linalg_indexcastui_dynamic_dim_i64(%arg0: tensor<f16>, %arg1: tensor<?xf16>) -> tensor<?xf16> {
%res = linalg.generic {
indexing_maps = [affine_map<(d0) -> ()>, affine_map<(d0) -> (d0)>],
iterator_types = ["parallel"]
}
ins(%arg0 : tensor<f16>)
outs(%arg1 : tensor<?xf16>) {
^bb0(%in: f16, %out: f16):
%idx = linalg.index 0 : index
%int = arith.index_castui %idx : index to i64
%fp = arith.uitofp %int : i64 to f16
linalg.yield %fp : f16
} -> tensor<?xf16>
return %res : tensor<?xf16>
}
|