|
| 1 | +// RUN: enzymexlamlir-opt --enzyme-hlo-opt %s | FileCheck %s |
| 2 | + |
| 3 | +// ============================================================================ |
| 4 | +// Tests for gather with iota-like indexing that converts to slice |
| 5 | +// ============================================================================ |
| 6 | + |
| 7 | +// Simple iota indexing: gather with iota indices should become a slice |
| 8 | +func.func @gather_iota_to_slice(%arg0: tensor<10xi64>) -> tensor<5xi64> { |
| 9 | + %indices = stablehlo.iota dim = 0 : tensor<5x1xi64> |
| 10 | + %0 = "stablehlo.gather"(%arg0, %indices) { |
| 11 | + dimension_numbers = #stablehlo.gather< |
| 12 | + collapsed_slice_dims = [0], |
| 13 | + start_index_map = [0], |
| 14 | + index_vector_dim = 1 |
| 15 | + >, |
| 16 | + slice_sizes = array<i64: 1> |
| 17 | + } : (tensor<10xi64>, tensor<5x1xi64>) -> tensor<5xi64> |
| 18 | + return %0 : tensor<5xi64> |
| 19 | +} |
| 20 | +// CHECK-LABEL: func.func @gather_iota_to_slice |
| 21 | +// CHECK-NEXT: %[[SLICE:.+]] = stablehlo.slice %arg0 [0:5] |
| 22 | +// CHECK-NEXT: return %[[SLICE]] |
| 23 | + |
| 24 | +// Iota with offset: gather with indices [2, 3, 4, 5] should become slice [2:6:1] |
| 25 | +func.func @gather_iota_offset_to_slice(%arg0: tensor<10xi64>) -> tensor<4xi64> { |
| 26 | + %c = stablehlo.constant dense<2> : tensor<4x1xi64> |
| 27 | + %iota = stablehlo.iota dim = 0 : tensor<4x1xi64> |
| 28 | + %indices = stablehlo.add %iota, %c : tensor<4x1xi64> |
| 29 | + %0 = "stablehlo.gather"(%arg0, %indices) { |
| 30 | + dimension_numbers = #stablehlo.gather< |
| 31 | + collapsed_slice_dims = [0], |
| 32 | + start_index_map = [0], |
| 33 | + index_vector_dim = 1 |
| 34 | + >, |
| 35 | + slice_sizes = array<i64: 1> |
| 36 | + } : (tensor<10xi64>, tensor<4x1xi64>) -> tensor<4xi64> |
| 37 | + return %0 : tensor<4xi64> |
| 38 | +} |
| 39 | +// CHECK-LABEL: func.func @gather_iota_offset_to_slice |
| 40 | +// CHECK-NEXT: %[[SLICE:.+]] = stablehlo.slice %arg0 [2:6] |
| 41 | +// CHECK-NEXT: return %[[SLICE]] |
| 42 | + |
| 43 | +// Iota with stride: gather with indices [0, 2, 4, 6] should become slice [0:8:2] |
| 44 | +func.func @gather_iota_stride_to_slice(%arg0: tensor<10xi64>) -> tensor<4xi64> { |
| 45 | + %c = stablehlo.constant dense<2> : tensor<4x1xi64> |
| 46 | + %iota = stablehlo.iota dim = 0 : tensor<4x1xi64> |
| 47 | + %indices = stablehlo.multiply %iota, %c : tensor<4x1xi64> |
| 48 | + %0 = "stablehlo.gather"(%arg0, %indices) { |
| 49 | + dimension_numbers = #stablehlo.gather< |
| 50 | + collapsed_slice_dims = [0], |
| 51 | + start_index_map = [0], |
| 52 | + index_vector_dim = 1 |
| 53 | + >, |
| 54 | + slice_sizes = array<i64: 1> |
| 55 | + } : (tensor<10xi64>, tensor<4x1xi64>) -> tensor<4xi64> |
| 56 | + return %0 : tensor<4xi64> |
| 57 | +} |
| 58 | +// CHECK-LABEL: func.func @gather_iota_stride_to_slice |
| 59 | +// CHECK-NEXT: %[[SLICE:.+]] = stablehlo.slice %arg0 [0:8:2] |
| 60 | +// CHECK-NEXT: return %[[SLICE]] |
| 61 | + |
| 62 | +// Iota with offset and stride: gather with indices [1, 3, 5, 7] should become slice [1:9:2] |
| 63 | +func.func @gather_iota_offset_stride_to_slice(%arg0: tensor<10xi64>) -> tensor<4xi64> { |
| 64 | + %c_offset = stablehlo.constant dense<1> : tensor<4x1xi64> |
| 65 | + %c_scale = stablehlo.constant dense<2> : tensor<4x1xi64> |
| 66 | + %iota = stablehlo.iota dim = 0 : tensor<4x1xi64> |
| 67 | + %scaled = stablehlo.multiply %iota, %c_scale : tensor<4x1xi64> |
| 68 | + %indices = stablehlo.add %scaled, %c_offset : tensor<4x1xi64> |
| 69 | + %0 = "stablehlo.gather"(%arg0, %indices) { |
| 70 | + dimension_numbers = #stablehlo.gather< |
| 71 | + collapsed_slice_dims = [0], |
| 72 | + start_index_map = [0], |
| 73 | + index_vector_dim = 1 |
| 74 | + >, |
| 75 | + slice_sizes = array<i64: 1> |
| 76 | + } : (tensor<10xi64>, tensor<4x1xi64>) -> tensor<4xi64> |
| 77 | + return %0 : tensor<4xi64> |
| 78 | +} |
| 79 | +// CHECK-LABEL: func.func @gather_iota_offset_stride_to_slice |
| 80 | +// CHECK-NEXT: %[[SLICE:.+]] = stablehlo.slice %arg0 [1:9:2] |
| 81 | +// CHECK-NEXT: return %[[SLICE]] |
| 82 | + |
| 83 | +// Constant iota-like indices: dense constant that forms an iota pattern |
| 84 | +func.func @gather_const_iota_to_slice(%arg0: tensor<10xi64>) -> tensor<4xi64> { |
| 85 | + %indices = stablehlo.constant dense<[[0], [1], [2], [3]]> : tensor<4x1xi64> |
| 86 | + %0 = "stablehlo.gather"(%arg0, %indices) { |
| 87 | + dimension_numbers = #stablehlo.gather< |
| 88 | + collapsed_slice_dims = [0], |
| 89 | + start_index_map = [0], |
| 90 | + index_vector_dim = 1 |
| 91 | + >, |
| 92 | + slice_sizes = array<i64: 1> |
| 93 | + } : (tensor<10xi64>, tensor<4x1xi64>) -> tensor<4xi64> |
| 94 | + return %0 : tensor<4xi64> |
| 95 | +} |
| 96 | +// CHECK-LABEL: func.func @gather_const_iota_to_slice |
| 97 | +// CHECK-NEXT: %[[SLICE:.+]] = stablehlo.slice %arg0 [0:4] |
| 98 | +// CHECK-NEXT: return %[[SLICE]] |
| 99 | + |
| 100 | +// ============================================================================ |
| 101 | +// Tests for gather with size-1 index -> dynamic_slice |
| 102 | +// ============================================================================ |
| 103 | + |
| 104 | +// Size-1 index: scalar-like index should become dynamic_slice |
| 105 | +func.func @gather_scalar_index_to_dynamic_slice(%arg0: tensor<10xi64>, %idx: tensor<1xi64>) -> tensor<1xi64> { |
| 106 | + %0 = "stablehlo.gather"(%arg0, %idx) { |
| 107 | + dimension_numbers = #stablehlo.gather< |
| 108 | + collapsed_slice_dims = [0], |
| 109 | + start_index_map = [0], |
| 110 | + index_vector_dim = 1 |
| 111 | + >, |
| 112 | + slice_sizes = array<i64: 1> |
| 113 | + } : (tensor<10xi64>, tensor<1xi64>) -> tensor<1xi64> |
| 114 | + return %0 : tensor<1xi64> |
| 115 | +} |
| 116 | +// CHECK-LABEL: func.func @gather_scalar_index_to_dynamic_slice |
| 117 | +// CHECK: stablehlo.reshape |
| 118 | +// CHECK: stablehlo.dynamic_slice |
| 119 | + |
| 120 | +// Floating point elements in gather |
| 121 | +func.func @gather_iota_float(%arg0: tensor<10xf64>) -> tensor<5xf64> { |
| 122 | + %indices = stablehlo.iota dim = 0 : tensor<5x1xi64> |
| 123 | + %0 = "stablehlo.gather"(%arg0, %indices) { |
| 124 | + dimension_numbers = #stablehlo.gather< |
| 125 | + collapsed_slice_dims = [0], |
| 126 | + start_index_map = [0], |
| 127 | + index_vector_dim = 1 |
| 128 | + >, |
| 129 | + slice_sizes = array<i64: 1> |
| 130 | + } : (tensor<10xf64>, tensor<5x1xi64>) -> tensor<5xf64> |
| 131 | + return %0 : tensor<5xf64> |
| 132 | +} |
| 133 | +// CHECK-LABEL: func.func @gather_iota_float |
| 134 | +// CHECK-NEXT: %[[SLICE:.+]] = stablehlo.slice %arg0 [0:5] |
| 135 | +// CHECK-NEXT: return %[[SLICE]] |
| 136 | + |
| 137 | +// ============================================================================ |
| 138 | +// Negative tests: should NOT be simplified |
| 139 | +// ============================================================================ |
| 140 | + |
| 141 | +// Non-1D operand: should not simplify (currently only supports 1D operands) |
| 142 | +func.func @gather_non_1d_operand(%arg0: tensor<4x4xi64>) -> tensor<2xi64> { |
| 143 | + %indices = stablehlo.constant dense<[[0, 0], [1, 1]]> : tensor<2x2xi64> |
| 144 | + %0 = "stablehlo.gather"(%arg0, %indices) { |
| 145 | + dimension_numbers = #stablehlo.gather< |
| 146 | + collapsed_slice_dims = [0, 1], |
| 147 | + start_index_map = [0, 1], |
| 148 | + index_vector_dim = 1 |
| 149 | + >, |
| 150 | + slice_sizes = array<i64: 1, 1> |
| 151 | + } : (tensor<4x4xi64>, tensor<2x2xi64>) -> tensor<2xi64> |
| 152 | + return %0 : tensor<2xi64> |
| 153 | +} |
| 154 | +// CHECK-LABEL: func.func @gather_non_1d_operand |
| 155 | +// CHECK: stablehlo.gather |
| 156 | + |
| 157 | +// Slice sizes not all 1: should not simplify |
| 158 | +func.func @gather_slice_size_not_1(%arg0: tensor<10xi64>) -> tensor<4x2xi64> { |
| 159 | + %indices = stablehlo.iota dim = 0 : tensor<4x1xi64> |
| 160 | + %0 = "stablehlo.gather"(%arg0, %indices) { |
| 161 | + dimension_numbers = #stablehlo.gather< |
| 162 | + offset_dims = [1], |
| 163 | + start_index_map = [0], |
| 164 | + index_vector_dim = 1 |
| 165 | + >, |
| 166 | + slice_sizes = array<i64: 2> |
| 167 | + } : (tensor<10xi64>, tensor<4x1xi64>) -> tensor<4x2xi64> |
| 168 | + return %0 : tensor<4x2xi64> |
| 169 | +} |
| 170 | +// CHECK-LABEL: func.func @gather_slice_size_not_1 |
| 171 | +// CHECK: stablehlo.gather |
| 172 | + |
| 173 | +// Non-iota indices: random indices should not simplify |
| 174 | +func.func @gather_non_iota_indices(%arg0: tensor<10xi64>) -> tensor<4xi64> { |
| 175 | + %indices = stablehlo.constant dense<[[3], [1], [4], [2]]> : tensor<4x1xi64> |
| 176 | + %0 = "stablehlo.gather"(%arg0, %indices) { |
| 177 | + dimension_numbers = #stablehlo.gather< |
| 178 | + collapsed_slice_dims = [0], |
| 179 | + start_index_map = [0], |
| 180 | + index_vector_dim = 1 |
| 181 | + >, |
| 182 | + slice_sizes = array<i64: 1> |
| 183 | + } : (tensor<10xi64>, tensor<4x1xi64>) -> tensor<4xi64> |
| 184 | + return %0 : tensor<4xi64> |
| 185 | +} |
| 186 | +// CHECK-LABEL: func.func @gather_non_iota_indices |
| 187 | +// CHECK: stablehlo.gather |
0 commit comments