// RUN: mlir-opt %s -transform-interpreter -split-input-file -canonicalize -cse -verify-diagnostics | FileCheck %s // REQUIRES: strange-fix-for-ubuntu func.func @reduction_tile(%arg0: tensor, %out: tensor) -> tensor { %red = linalg.generic {indexing_maps = [affine_map<(d0, d1) -> (d0, d1)>, affine_map<(d0, d1) -> (d0)>], iterator_types = ["parallel", "reduction"]} ins(%arg0 : tensor) outs(%out : tensor) { ^bb0(%arg7: f32, %arg9: f32): %1 = arith.mulf %arg7, %arg7 : f32 %2 = arith.addf %1, %arg9 : f32 linalg.yield %2 : f32 } -> tensor return %red : tensor } module attributes {transform.with_named_sequence} { transform.named_sequence @__transform_main(%arg1: !transform.any_op {transform.readonly}) { %0 = transform.structured.match ops{["linalg.generic"]} in %arg1 : (!transform.any_op) -> !transform.any_op %1, %2, %3, %loop = transform.structured.tile_reduction_using_for %0 by tile_sizes = [0, 5] : (!transform.any_op) -> (!transform.any_op, !transform.any_op, !transform.any_op, !transform.any_op) transform.yield } } // CHECK-DAG: #[[MAP0:.*]] = affine_map<(d0)[s0] -> (-d0 + s0, 5)> // CHECK-DAG: #[[MAP1:.*]] = affine_map<(d0, d1) -> (d0, d1)> // CHECK: func @reduction_tile(%[[ARG0:.+]]: tensor, %[[ARG1:.+]]: tensor // CHECK-DAG: %[[I:.*]] = arith.constant 0.000000e+00 : f32 // CHECK-DAG: %[[C5:.*]] = arith.constant 5 : index // CHECK-DAG: %[[C1:.*]] = arith.constant 1 : index // CHECK-DAG: %[[C0:.*]] = arith.constant 0 : index // CHECK-DAG: %[[D0:.*]] = tensor.dim %[[ARG0]], %[[C0]] : tensor // CHECK-DAG: %[[D1:.*]] = tensor.dim %[[ARG0]], %[[C1]] : tensor // CHECK: %[[E:.*]] = tensor.empty(%[[D0]]) : tensor // CHECK: %[[F:.*]] = linalg.fill ins(%[[I]] : f32) outs(%[[E]] : tensor) -> tensor // CHECK: %[[L:.*]] = scf.for %[[K:.*]] = %[[C0]] to %[[D1]] step %[[C5]] iter_args(%[[ARG3:.*]] = %[[F]]) -> (tensor) { // CHECK: %[[PS:.*]] = affine.min #[[MAP0]](%[[K]])[%[[D1]]] // CHECK: %[[EXT2:.*]] = tensor.extract_slice %[[ARG0]][0, %[[K:.*]]] [%[[D0]], %[[PS]]] [1, 1] : tensor to tensor // CHECK: %[[EXT:.*]] = tensor.extract_slice %[[ARG3]][0, 0] [%[[D0]], %[[PS]]] [1, 1] : tensor to tensor // CHECK: %[[PR:.*]] = linalg.generic {indexing_maps = [#[[MAP1]], #[[MAP1]]], iterator_types = ["parallel", "parallel"]} ins(%[[EXT2]] : tensor) outs(%[[EXT]] : tensor) { // CHECK: arith.mulf // CHECK: arith.addf // CHECK: linalg.yield // CHECK: } -> tensor // CHECK: %[[INS:.*]] = tensor.insert_slice %[[PR]] into %[[ARG3]][0, 0] [%[[D0]], %[[PS]]] [1, 1] : tensor into tensor // CHECK: scf.yield %[[INS]] : tensor // CHECK: } // CHECK: %[[R:.*]] = linalg.reduce ins(%[[L]] : tensor) outs(%[[ARG1]] : tensor) dimensions = [1] // CHECK: arith.addf // CHECK: linalg.yield // CHECK: } // CHECK: return %[[R]] : tensor // ----- func.func @reduction_tile_transpose(%arg0: tensor, %out: tensor) -> tensor { %red = linalg.generic {indexing_maps = [affine_map<(d0, d1) -> (d0, d1)>, affine_map<(d0, d1) -> (d1)>], iterator_types = ["reduction", "parallel"]} ins(%arg0 : tensor) outs(%out : tensor) { ^bb0(%arg7: f32, %arg9: f32): %42 = arith.addf %arg7, %arg9 : f32 linalg.yield %42 : f32 } -> tensor return %red : tensor } module attributes {transform.with_named_sequence} { transform.named_sequence @__transform_main(%arg1: !transform.any_op {transform.readonly}) { %0 = transform.structured.match ops{["linalg.generic"]} in %arg1 : (!transform.any_op) -> !transform.any_op %1, %2, %3, %loop = transform.structured.tile_reduction_using_for %0 by tile_sizes = [5, 0] : (!transform.any_op) -> (!transform.any_op, !transform.any_op, !transform.any_op, !transform.any_op) transform.yield } } // CHECK-DAG: #[[MAP0:.*]] = affine_map<(d0)[s0] -> (-d0 + s0, 5)> // CHECK-DAG: #[[MAP1:.*]] = affine_map<(d0, d1) -> (d0, d1)> // CHECK-DAG: #[[MAP2:.*]] = affine_map<(d0, d1) -> (d1, d0)> // CHECK: func @reduction_tile_transpose // CHECK: tensor.empty(%{{.*}}) : tensor // CHECK: linalg.fill {{.*}} : tensor) -> tensor // CHECK: scf.for // CHECK: %[[EXT:.*]] = tensor.extract_slice %[[ARG3:.*]][0, 0] [%[[D0:.*]], %[[D1:.*]]] [1, 1] : tensor to tensor // CHECK: %[[R:.*]] = linalg.generic {indexing_maps = [#[[MAP1]], #[[MAP2]]], iterator_types = ["parallel", "parallel"]} ins(%[[L:.*]] : tensor) outs(%[[EXT]] : tensor) // CHECK: %[[INS:.*]] = tensor.insert_slice %[[R]] into %[[ARG3]][0, 0] [%[[D0]], %[[D1]]] [1, 1] : tensor into tensor // CHECK: scf.yield {{.*}} : tensor // CHECK: } // CHECK: linalg.reduce // CHECK: return // ----- func.func @reduction_tile_parallel( %arg0: tensor, %out: tensor) -> tensor { %red = linalg.generic {indexing_maps = [affine_map<(d0, d1) -> (d0, d1)>, affine_map<(d0, d1) -> (d0)>], iterator_types = ["parallel", "reduction"]} ins(%arg0 : tensor) outs(%out : tensor) { ^bb0(%arg7: f32, %arg9: f32): %1 = arith.mulf %arg7, %arg7 : f32 %2 = arith.addf %1, %arg9 : f32 linalg.yield %2 : f32 } -> tensor return %red : tensor } module attributes {transform.with_named_sequence} { transform.named_sequence @__transform_main(%arg1: !transform.any_op {transform.readonly}) { %0 = transform.structured.match ops{["linalg.generic"]} in %arg1 : (!transform.any_op) -> !transform.any_op %1, %2, %3, %loop = transform.structured.tile_reduction_using_forall %0 by num_threads = [0, 5] tile_sizes = [] : (!transform.any_op) -> (!transform.any_op, !transform.any_op, !transform.any_op, !transform.any_op) transform.yield } } // CHECK-DAG: #[[MAP0:.*]] = affine_map<(d0)[s0] -> (-(d0 * (s0 ceildiv 5)) + s0, s0 ceildiv 5)> // CHECK-DAG: #[[MAP1:.*]] = affine_map<(d0) -> (0, d0)> // CHECK-DAG: #[[MAP2:.*]] = affine_map<(d0)[s0] -> (d0 * (s0 ceildiv 5))> // CHECK-DAG: #[[MAP3:.*]] = affine_map<(d0, d1) -> (d0, d1)> // CHECK-DAG: #[[MAP4:.*]] = affine_map<(d0, d1) -> (d0)> // CHECK: func @reduction_tile_parallel(%[[ARG0:.+]]: tensor, %[[ARG1:.+]]: tensor // CHECK-DAG: %[[I:.*]] = arith.constant 0.000000e+00 : f32 // CHECK-DAG: %[[C0:.*]] = arith.constant 0 : index // CHECK-DAG: %[[C1:.*]] = arith.constant 1 : index // CHECK-DAG: %[[D0:.*]] = tensor.dim %[[ARG0]], %[[C0]] : tensor // CHECK-DAG: %[[D1:.*]] = tensor.dim %[[ARG0]], %[[C1]] : tensor // CHECK: %[[E:.*]] = tensor.empty(%[[D0]]) : tensor // CHECK: %[[F:.*]] = linalg.fill ins(%[[I]] : f32) outs(%[[E]] : tensor) -> tensor // CHECK: %[[L:.*]] = scf.forall (%[[IV:.+]]) in (5) shared_outs(%[[ARG3:.+]] = %[[F]]) -> (tensor) { // CHECK-DAG: %[[TS0:.+]] = affine.min #[[MAP0]](%[[IV]])[%[[D1]]] // CHECK-DAG: %[[TS1:.+]] = affine.max #[[MAP1]](%[[TS0]]) // CHECK-DAG: %[[ET:.+]] = tensor.extract_slice %[[ARG3:.+]][0, %[[IV]]] [%[[D0]], 1] [1, 1] : tensor to tensor // CHECK-DAG: %[[TINDEX:.+]] = affine.apply #[[MAP2]](%[[IV]])[%[[D1]]] // CHECK-DAG: %[[INCHUNK:.+]] = tensor.extract_slice %[[ARG0]][0, %[[TINDEX]]] [%[[D0]], %[[TS1]]] [1, 1] : tensor to tensor // CHECK: %[[PARTIAL:.+]] = linalg.generic {indexing_maps = [#[[MAP3]], #[[MAP4]]], iterator_types = ["parallel", "reduction"]} ins(%[[INCHUNK]] : tensor) outs(%[[ET]] : tensor) { // CHECK: arith.mulf // CHECK: arith.addf // CHECK: linalg.yield // CHECK: } -> tensor // CHECK: scf.forall.in_parallel { // CHECK: tensor.parallel_insert_slice %[[PARTIAL]] into %[[ARG3]][0, %[[IV]]] [%[[D0]], 1] [1, 1] : tensor into tensor // CHECK: } // CHECK: } // CHECK: %[[R:.*]] = linalg.reduce ins(%[[L]] : tensor) outs(%[[ARG1]] : tensor) dimensions = [1] // CHECK: { // CHECK: arith.addf // CHECK: linalg.yield // CHECK: } // CHECK: return %[[R]] : tensor // ----- func.func @matmul_tile_parallel( %A: tensor, %B: tensor, %out: tensor) -> tensor { %matmul = linalg.matmul ins(%A, %B: tensor, tensor) outs(%out: tensor) -> tensor return %matmul : tensor } module attributes {transform.with_named_sequence} { transform.named_sequence @__transform_main(%arg1: !transform.any_op {transform.readonly}) { %0 = transform.structured.match ops{["linalg.matmul"]} in %arg1 : (!transform.any_op) -> !transform.any_op %1, %2, %3, %loop = transform.structured.tile_reduction_using_forall %0 by num_threads = [0, 0, 5] : (!transform.any_op) -> (!transform.any_op, !transform.any_op, !transform.any_op, !transform.any_op) transform.yield } } // CHECK-DAG: #[[MAP0:.*]] = affine_map<(d0)[s0] -> (-(d0 * (s0 ceildiv 5)) + s0, s0 ceildiv 5)> // CHECK-DAG: #[[MAP1:.*]] = affine_map<(d0) -> (0, d0)> // CHECK-DAG: #[[MAP2:.*]] = affine_map<(d0)[s0] -> (d0 * (s0 ceildiv 5))> // CHECK: func @matmul_tile_parallel(%[[ARG0:.+]]: tensor, %[[ARG1:.+]]: tensor, %[[ARG2:.+]]: tensor // CHECK-DAG: %[[I:.*]] = arith.constant 0.000000e+00 : f32 // CHECK-DAG: %[[C0:.*]] = arith.constant 0 : index // CHECK-DAG: %[[C1:.*]] = arith.constant 1 : index // CHECK-DAG: %[[D0:.*]] = tensor.dim %[[ARG0]], %[[C0]] : tensor // CHECK-DAG: %[[D1:.*]] = tensor.dim %[[ARG0]], %[[C1]] : tensor // CHECK-DAG: %[[D2:.*]] = tensor.dim %[[ARG1]], %[[C1]] : tensor // CHECK: %[[E:.*]] = tensor.empty(%[[D0]], %[[D2]]) : tensor // CHECK: %[[F:.*]] = linalg.fill ins(%[[I]] : f32) outs(%[[E]] : tensor) -> tensor // CHECK: %[[L:.*]] = scf.forall (%[[IV:.+]]) in (5) shared_outs(%[[ARG3:.+]] = %[[F]]) -> (tensor) { // CHECK-DAG: %[[TS0:.+]] = affine.min #[[MAP0]](%[[IV]])[%[[D1]]] // CHECK-DAG: %[[TS1:.+]] = affine.max #[[MAP1]](%[[TS0]]) // CHECK-DAG: %[[ET:.+]] = tensor.extract_slice %[[ARG3:.+]][0, 0, %[[IV]]] [%[[D0]], %[[D2]], 1] [1, 1, 1] : tensor to tensor // CHECK-DAG: %[[TINDEX:.+]] = affine.apply #[[MAP2]](%[[IV]])[%[[D1]]] // CHECK-DAG: %[[INCHUNKA:.+]] = tensor.extract_slice %[[ARG0]][0, %[[TINDEX]]] [%[[D0]], %[[TS1]]] [1, 1] : tensor to tensor // CHECK-DAG: %[[INCHUNKB:.+]] = tensor.extract_slice %[[ARG1]][%[[TINDEX]], 0] [%[[TS1]], %[[D2]]] [1, 1] : tensor to tensor // CHECK: %[[PARTIAL:.+]] = linalg.matmul ins(%[[INCHUNKA]], %[[INCHUNKB]] : tensor, tensor) outs(%[[ET]] : tensor) -> tensor // CHECK: scf.forall.in_parallel { // CHECK: tensor.parallel_insert_slice %[[PARTIAL]] into %[[ARG3]][0, 0, %[[IV]]] [%[[D0]], %[[D2]], 1] [1, 1, 1] : tensor into tensor // CHECK: } // CHECK: } // CHECK: %[[R:.*]] = linalg.reduce ins(%[[L]] : tensor) outs(%[[ARG2]] : tensor) dimensions = [2] // CHECK: arith.addf // CHECK: linalg.yield // CHECK: } // CHECK: return %[[R]] : tensor // ----- func.func @reduction_untiled_forall( %arg0: tensor, %out: tensor) -> tensor { // expected-error @below {{tiling parallel dimensions is not supported with partial reduction tiling strategies}} %red = linalg.generic {indexing_maps = [affine_map<(d0, d1) -> (d0, d1)>, affine_map<(d0, d1) -> (d0)>], iterator_types = ["parallel", "reduction"]} ins(%arg0 : tensor) outs(%out : tensor) { ^bb0(%arg7: f32, %arg9: f32): %1 = arith.mulf %arg7, %arg7 : f32 %2 = arith.addf %1, %arg9 : f32 linalg.yield %2 : f32 } -> tensor return %red : tensor } module attributes {transform.with_named_sequence} { transform.named_sequence @__transform_main(%arg1: !transform.any_op {transform.readonly}) { %0 = transform.structured.match ops{["linalg.generic"]} in %arg1 : (!transform.any_op) -> !transform.any_op // expected-error @below {{could not tile reduction}} %1, %2, %3, %loop = transform.structured.tile_reduction_using_forall %0 by num_threads = [5] tile_sizes = [3] mapping = [#gpu.thread] : (!transform.any_op) -> (!transform.any_op, !transform.any_op, !transform.any_op, !transform.any_op) transform.yield } } // ----- #map = affine_map<(d0, d1) -> (d0, d1)> #map1 = affine_map<(d0, d1) -> (d0)> module { func.func @fail_for_float_neutral(%arg0: tensor, %arg1: tensor) -> tensor { // expected-error @below {{'linalg.generic' op Failed to get an identity value for the reduction operation.}} %0 = linalg.generic {indexing_maps = [#map, #map1], iterator_types = ["parallel", "reduction"]} ins(%arg0 : tensor) outs(%arg1 : tensor) { ^bb0(%in: f32, %out: f32): %1 = llvm.fmul %in, %in : f32 %2 = llvm.fadd %1, %out : f32 linalg.yield %2 : f32 } -> tensor return %0 : tensor } module attributes {transform.with_named_sequence} { transform.named_sequence @__transform_main(%arg0: !transform.any_op {transform.readonly}) { %0 = transform.structured.match ops{["linalg.generic"]} in %arg0 : (!transform.any_op) -> !transform.any_op // expected-error @below {{failed to tile using partial reduction}} %fill_op, %split_linalg_op, %combining_linalg_op, %for_op = transform.structured.tile_reduction_using_for %0 by tile_sizes = [0, 5] : (!transform.any_op) -> (!transform.any_op, !transform.any_op, !transform.any_op, !transform.any_op) transform.yield } } } // ----- #map = affine_map<(d0, d1, d2) -> (d1, d2)> #map1 = affine_map<(d0, d1, d2) -> (d0, d1, d2)> #map2 = affine_map<(d0, d1, d2) -> (d0)> module { func.func @reduction_tile_multiple_reduction(%arg0: tensor<86x128xf32>, %arg1: tensor<4096x86x128xf32>, %arg2: tensor<4096xf32>) -> tensor<4096xf32> { %0 = linalg.generic {indexing_maps = [#map, #map1, #map2], iterator_types = ["parallel", "reduction", "reduction"]} ins(%arg0, %arg1 : tensor<86x128xf32>, tensor<4096x86x128xf32>) outs(%arg2 : tensor<4096xf32>) { ^bb0(%in: f32, %in_0: f32, %out: f32): %1 = arith.mulf %in, %in_0 : f32 %2 = arith.addf %1, %out : f32 linalg.yield %2 : f32 } -> tensor<4096xf32> return %0 : tensor<4096xf32> } module attributes {transform.with_named_sequence} { transform.named_sequence @__transform_main(%arg0: !transform.any_op {transform.readonly}) { %0 = transform.structured.match ops{["linalg.generic"]} in %arg0 : (!transform.any_op) -> !transform.any_op %fill_op, %split_linalg_op, %combining_linalg_op, %for_op = transform.structured.tile_reduction_using_for %0 by tile_sizes = [0, 2, 64] : (!transform.any_op) -> (!transform.any_op, !transform.any_op, !transform.any_op, !transform.any_op) transform.yield } } } // CHECK: func @reduction_tile_multiple_reduction(%[[ARG0:.+]]: tensor<86x128xf32>, %[[ARG1:.+]]: tensor<4096x86x128xf32>, %[[ARG2:.+]]: tensor<4096xf32> // CHECK: %[[F:.*]] = linalg.fill ins(%{{.*}} : f32) outs(%{{.*}} : tensor<4096x2x64xf32>) -> tensor<4096x2x64xf32> // CHECK: %[[L0:.*]] = scf.for %{{.*}} = %{{.*}} to %{{.*}} step %{{.*}} iter_args(%[[ARG3:.*]] = %[[F]]) -> (tensor<4096x2x64xf32>) // CHECK: %[[L1:.*]] = scf.for %{{.*}} = %{{.*}} to %{{.*}} step %{{.*}} iter_args(%[[ARG4:.*]] = %[[ARG3]]) -> (tensor<4096x2x64xf32>) // CHECK: %[[OUT:.*]] = linalg.generic {indexing_maps = [{{.*}}, {{.*}}, {{.*}}], iterator_types = ["parallel", "parallel", "parallel"]} ins(%{{.*}}, %{{.*}}: tensor<2x64xf32>, tensor<4096x2x64xf32>) outs(%{{.*}}: tensor<4096x2x64xf32>) // CHECK: scf.yield %[[OUT]] : tensor<4096x2x64xf32> // CHECK: scf.yield %[[L1]] : tensor<4096x2x64xf32> // CHECK: %[[OUT2:.*]] = linalg.reduce ins(%{{.*}} : tensor<4096x2x64xf32>) outs(%{{.*}} : tensor<4096xf32>) // CHECK: return %[[OUT2]] : tensor<4096xf32> // ----- func.func @reduction_tile_multiple_results(%arg0: tensor, %out: tensor, %out2: tensor) -> (tensor, tensor) { %red:2 = linalg.generic {indexing_maps = [affine_map<(d0, d1) -> (d0, d1)>, affine_map<(d0, d1) -> (d0)>, affine_map<(d0, d1) -> (d0)>], iterator_types = ["parallel", "reduction"]} ins(%arg0 : tensor) outs(%out, %out2 : tensor, tensor) { ^bb0(%arg7: f32, %arg9: f32, %arg9_1: f32): %1 = arith.mulf %arg7, %arg7 : f32 %2 = arith.addf %1, %arg9 : f32 %3 = arith.maximumf %1, %arg9_1 : f32 linalg.yield %2, %3 : f32, f32 } -> (tensor, tensor) return %red#0, %red#1 : tensor, tensor } module attributes {transform.with_named_sequence} { transform.named_sequence @__transform_main(%arg1: !transform.any_op {transform.readonly}) { %0 = transform.structured.match ops{["linalg.generic"]} in %arg1 : (!transform.any_op) -> !transform.any_op %1, %12, %2, %3, %4, %loop = transform.structured.tile_reduction_using_for %0 by tile_sizes = [0, 5] : (!transform.any_op) -> (!transform.any_op, !transform.any_op, !transform.any_op, !transform.any_op, !transform.any_op, !transform.any_op) transform.yield } } // CHECK: func @reduction_tile_multiple_results // CHECK-DAG: %[[SUM_ID:.+]] = arith.constant 0.000000e+00 : f32 // CHECK-DAG: %[[MAX_ID:.+]] = arith.constant 0xFF800000 : f32 // CHECK-DAG: %[[SUM_INIT:.+]] = linalg.fill ins(%[[SUM_ID]] : f32) outs(%{{.*}} : tensor) -> tensor // CHECK-DAG: %[[MAX_INIT:.+]] = linalg.fill ins(%[[MAX_ID]] : f32) outs(%{{.*}} : tensor) -> tensor // CHECK: %[[OUT:.+]]:2 = scf.for // CHECK-SAME: iter_args(%[[SUM:.+]] = %[[SUM_INIT]], %[[MAX:.+]] = %[[MAX_INIT]]) // CHECK: %[[UPDATED:.*]]:2 = linalg.generic // CHECK: arith.mulf // CHECK: arith.addf // CHECK: arith.maximumf // CHECK: %[[INSERT1:.+]] = tensor.insert_slice %[[UPDATED]]#0 into %[[SUM]] // CHECK: %[[INSERT2:.+]] = tensor.insert_slice %[[UPDATED]]#1 into %[[MAX]] // CHECK: scf.yield %[[INSERT1]], %[[INSERT1]] // CHECK: linalg.reduce // CHECK: arith.addf // CHECK: linalg.reduce // CHECK: arith.maximumf // ----- func.func @reduction_tile_multi_dim_transpose(%arg0: tensor, %out: tensor) -> tensor { %red = linalg.generic {indexing_maps = [affine_map<(d0, d1, d2) -> (d0, d1, d2)>, affine_map<(d0, d1, d2) -> (d2, d0)>], iterator_types = ["parallel", "reduction", "parallel"]} ins(%arg0 : tensor) outs(%out : tensor) { ^bb0(%arg7: f32, %arg9: f32): %42 = arith.addf %arg7, %arg9 : f32 linalg.yield %42 : f32 } -> tensor return %red : tensor } module attributes {transform.with_named_sequence} { transform.named_sequence @__transform_main(%arg1: !transform.any_op {transform.readonly}) { %0 = transform.structured.match ops{["linalg.generic"]} in %arg1 : (!transform.any_op) -> !transform.any_op %1, %2, %3, %loop = transform.structured.tile_reduction_using_for %0 by tile_sizes = [0, 5, 0] : (!transform.any_op) -> (!transform.any_op, !transform.any_op, !transform.any_op, !transform.any_op) transform.yield } } // CHECK-DAG: #[[MAP1:.*]] = affine_map<(d0, d1, d2) -> (d0, d1, d2)> // CHECK-DAG: #[[MAP2:.*]] = affine_map<(d0, d1, d2) -> (d2, d0, d1)> // CHECK: func @reduction_tile_multi_dim_transpose // CHECK: tensor.empty(%{{.*}}) : tensor // CHECK: linalg.fill {{.*}} : tensor) -> tensor // CHECK: scf.for // CHECK: %[[K:.*]] = affine.min // CHECK: %[[EXT:.*]] = tensor.extract_slice %[[ARG3:.*]][0, 0, 0] [%[[D2:.*]], %[[D0:.*]], %[[K]]] [1, 1, 1] : tensor to tensor // CHECK: %[[R:.*]] = linalg.generic {indexing_maps = [#[[MAP1]], #[[MAP2]]], iterator_types = ["parallel", "parallel", "parallel"]} ins(%[[L:.*]] : tensor) outs(%[[EXT]] : tensor) // CHECK: %[[INS:.*]] = tensor.insert_slice %[[R]] into %[[ARG3]][0, 0, 0] [%[[D2]], %[[D0]], %[[K]]] [1, 1, 1] : tensor into tensor // CHECK: scf.yield {{.*}} : tensor // CHECK: } // CHECK: linalg.reduce // CHECK: return // ----- // Check that only one of the reduction dimension can be tiled (in this case outer). #map = affine_map<(d0, d1, d2) -> (d1, d2)> #map1 = affine_map<(d0, d1, d2) -> (d0, d1, d2)> #map2 = affine_map<(d0, d1, d2) -> (d0)> module { func.func @reduction_tile_single_of_multiple_reduction_outer( %arg0: tensor<86x128xf32>, %arg1: tensor<4096x86x128xf32>, %arg2: tensor<4096xf32>) -> tensor<4096xf32> { %0 = linalg.generic { indexing_maps = [#map, #map1, #map2], iterator_types = ["parallel", "reduction", "reduction"]} ins(%arg0, %arg1 : tensor<86x128xf32>, tensor<4096x86x128xf32>) outs(%arg2 : tensor<4096xf32>) { ^bb0(%in: f32, %in_0: f32, %out: f32): %1 = arith.mulf %in, %in_0 : f32 %2 = arith.addf %1, %out : f32 linalg.yield %2 : f32 } -> tensor<4096xf32> return %0 : tensor<4096xf32> } module attributes {transform.with_named_sequence} { transform.named_sequence @__transform_main(%arg0: !transform.any_op {transform.readonly}) { %0 = transform.structured.match ops{["linalg.generic"]} in %arg0 : (!transform.any_op) -> !transform.any_op %fill_op, %split_linalg_op, %combining_linalg_op, %for_op = transform.structured.tile_reduction_using_for %0 reduction_dims = [1] by tile_sizes = [0, 2] : (!transform.any_op) -> (!transform.any_op, !transform.any_op, !transform.any_op, !transform.any_op) transform.yield } } } // CHECK: #[[INIT_MAP:.+]] = affine_map<(d0, d1, d2) -> (d0, d1)> // CHECK: @reduction_tile_single_of_multiple_reduction_outer( // CHECK-SAME: %[[INIT:[a-zA-Z0-9]+]]: tensor<4096xf32> // CHECK-DAG: %[[C0:.+]] = arith.constant 0 : index // CHECK-DAG: %[[C2:.+]] = arith.constant 2 : index // CHECK-DAG: %[[C86:.+]] = arith.constant 86 : index // CHECK-DAG: %[[EMPTY:.+]] = tensor.empty() : tensor<4096x2xf32> // CHECK: %[[FILL:.+]] = linalg.fill // CHECK-SAME: outs(%[[EMPTY]] : // CHECK: %[[RESULT:.+]] = scf.for %[[IV:[a-zA-Z0-9]+]] = %[[C0]] to %[[C86]] step %[[C2]] // CHECK-SAME: iter_args(%[[ITER_ARG:.+]] = %[[FILL]]) // CHECK: %[[PARTIAL_RESULT:.+]] = linalg.generic // CHECK-SAME: indexing_maps = [#{{.+}}, #{{.+}}, #[[INIT_MAP]]] // CHECK-SAME: iterator_types = ["parallel", "parallel", "reduction"] // CHECK-SAME: outs(%[[ITER_ARG]] : // CHECK: scf.yield %[[PARTIAL_RESULT]] // CHECK: %[[REDUCE:.+]] = linalg.reduce // CHECK-SAME: ins(%[[RESULT]] : // CHECK-SAME: outs(%[[INIT]] : // CHECK-SAME: dimensions = [1] // CHECK: return %[[REDUCE]] // ----- // Check that only one of the reduction dimension can be tiled (in this case inner). #map = affine_map<(d0, d1, d2) -> (d1, d2)> #map1 = affine_map<(d0, d1, d2) -> (d0, d1, d2)> #map2 = affine_map<(d0, d1, d2) -> (d0)> module { func.func @reduction_tile_single_of_multiple_reduction_inner( %arg0: tensor<86x128xf32>, %arg1: tensor<4096x86x128xf32>, %arg2: tensor<4096xf32>) -> tensor<4096xf32> { %0 = linalg.generic { indexing_maps = [#map, #map1, #map2], iterator_types = ["parallel", "reduction", "reduction"]} ins(%arg0, %arg1 : tensor<86x128xf32>, tensor<4096x86x128xf32>) outs(%arg2 : tensor<4096xf32>) { ^bb0(%in: f32, %in_0: f32, %out: f32): %1 = arith.mulf %in, %in_0 : f32 %2 = arith.addf %1, %out : f32 linalg.yield %2 : f32 } -> tensor<4096xf32> return %0 : tensor<4096xf32> } module attributes {transform.with_named_sequence} { transform.named_sequence @__transform_main(%arg0: !transform.any_op {transform.readonly}) { %0 = transform.structured.match ops{["linalg.generic"]} in %arg0 : (!transform.any_op) -> !transform.any_op %fill_op, %split_linalg_op, %combining_linalg_op, %for_op = transform.structured.tile_reduction_using_for %0 reduction_dims = [2] by tile_sizes = [0, 0, 64] : (!transform.any_op) -> (!transform.any_op, !transform.any_op, !transform.any_op, !transform.any_op) transform.yield } } } // CHECK: #[[INIT_MAP:.+]] = affine_map<(d0, d1, d2) -> (d0, d2)> // CHECK: @reduction_tile_single_of_multiple_reduction_inner( // CHECK-SAME: %[[INIT:[a-zA-Z0-9]+]]: tensor<4096xf32> // CHECK-DAG: %[[C0:.+]] = arith.constant 0 : index // CHECK-DAG: %[[C64:.+]] = arith.constant 64 : index // CHECK-DAG: %[[C128:.+]] = arith.constant 128 : index // CHECK-DAG: %[[EMPTY:.+]] = tensor.empty() : tensor<4096x64xf32> // CHECK: %[[FILL:.+]] = linalg.fill // CHECK-SAME: outs(%[[EMPTY]] : // CHECK: %[[RESULT:.+]] = scf.for %[[IV:[a-zA-Z0-9]+]] = %[[C0]] to %[[C128]] step %[[C64]] // CHECK-SAME: iter_args(%[[ITER_ARG:.+]] = %[[FILL]]) // CHECK: %[[PARTIAL_RESULT:.+]] = linalg.generic // CHECK-SAME: indexing_maps = [#{{.+}}, #{{.+}}, #[[INIT_MAP]]] // CHECK-SAME: iterator_types = ["parallel", "reduction", "parallel"] // CHECK-SAME: outs(%[[ITER_ARG]] : // CHECK: scf.yield %[[PARTIAL_RESULT]] // CHECK: %[[REDUCE:.+]] = linalg.reduce // CHECK-SAME: ins(%[[RESULT]] : // CHECK-SAME: outs(%[[INIT]] : // CHECK-SAME: dimensions = [1] // CHECK: return %[[REDUCE]] // ----- // Check that both the reduction dimensions are tiled but the dimensions in the output are swapped. #map = affine_map<(d0, d1, d2) -> (d1, d2)> #map1 = affine_map<(d0, d1, d2) -> (d0, d1, d2)> #map2 = affine_map<(d0, d1, d2) -> (d0)> module { func.func @reduction_tile_single_of_multiple_reduction_reversed( %arg0: tensor<86x128xf32>, %arg1: tensor<4096x86x128xf32>, %arg2: tensor<4096xf32>) -> tensor<4096xf32> { %0 = linalg.generic { indexing_maps = [#map, #map1, #map2], iterator_types = ["parallel", "reduction", "reduction"]} ins(%arg0, %arg1 : tensor<86x128xf32>, tensor<4096x86x128xf32>) outs(%arg2 : tensor<4096xf32>) { ^bb0(%in: f32, %in_0: f32, %out: f32): %1 = arith.mulf %in, %in_0 : f32 %2 = arith.addf %1, %out : f32 linalg.yield %2 : f32 } -> tensor<4096xf32> return %0 : tensor<4096xf32> } module attributes {transform.with_named_sequence} { transform.named_sequence @__transform_main(%arg0: !transform.any_op {transform.readonly}) { %0 = transform.structured.match ops{["linalg.generic"]} in %arg0 : (!transform.any_op) -> !transform.any_op %fill_op, %split_linalg_op, %combining_linalg_op, %for_op = transform.structured.tile_reduction_using_for %0 reduction_dims = [2, 1] by tile_sizes = [0, 2, 64] : (!transform.any_op) -> (!transform.any_op, !transform.any_op, !transform.any_op, !transform.any_op) transform.yield } } } // CHECK: #[[INIT_MAP:.+]] = affine_map<(d0, d1, d2) -> (d0, d2, d1)> // CHECK: @reduction_tile_single_of_multiple_reduction_reversed( // CHECK-SAME: %[[INIT:[a-zA-Z0-9]+]]: tensor<4096xf32> // CHECK-DAG: %[[C0:.+]] = arith.constant 0 : index // CHECK-DAG: %[[C2:.+]] = arith.constant 2 : index // CHECK-DAG: %[[C64:.+]] = arith.constant 64 : index // CHECK-DAG: %[[C86:.+]] = arith.constant 86 : index // CHECK-DAG: %[[C128:.+]] = arith.constant 128 : index // CHECK-DAG: %[[EMPTY:.+]] = tensor.empty() : tensor<4096x64x2xf32> // CHECK: %[[FILL:.+]] = linalg.fill // CHECK-SAME: outs(%[[EMPTY]] : // CHECK: %[[RESULT:.+]] = scf.for %[[IV0:[a-zA-Z0-9]+]] = %[[C0]] to %[[C86]] step %[[C2]] // CHECK-SAME: iter_args(%[[ITER_ARG:.+]] = %[[FILL]]) // CHECK: %[[RESULT0:.+]] = scf.for %[[IV1:[a-zA-Z0-9]+]] = %[[C0]] to %[[C128]] step %[[C64]] // CHECK-SAME: iter_args(%[[ITER_ARG0:.+]] = %[[ITER_ARG]]) // CHECK: %[[PARTIAL_RESULT:.+]] = linalg.generic // CHECK-SAME: indexing_maps = [#{{.+}}, #{{.+}}, #[[INIT_MAP]]] // CHECK-SAME: iterator_types = ["parallel", "parallel", "parallel"] // CHECK-SAME: outs(%[[ITER_ARG0]] : // CHECK: scf.yield %[[PARTIAL_RESULT]] // CHECK scf.yield %[[RESULT0]] // CHECK: %[[REDUCE:.+]] = linalg.reduce // CHECK-SAME: ins(%[[RESULT]] : // CHECK-SAME: outs(%[[INIT]] : // CHECK-SAME: dimensions = [1, 2] // CHECK: return %[[REDUCE]] // ----- func.func @reduction_tile_parallel_using_tile_sizes( %arg0: tensor, %out: tensor) -> tensor { %red = linalg.generic {indexing_maps = [affine_map<(d0, d1) -> (d0, d1)>, affine_map<(d0, d1) -> (d0)>], iterator_types = ["parallel", "reduction"]} ins(%arg0 : tensor) outs(%out : tensor) { ^bb0(%arg7: f32, %arg9: f32): %1 = arith.mulf %arg7, %arg7 : f32 %2 = arith.addf %1, %arg9 : f32 linalg.yield %2 : f32 } -> tensor return %red : tensor } // CHECK-DAG: #[[MAP0:.*]] = affine_map<()[s0] -> (s0 ceildiv 5)> // CHECK-DAG: #[[MAP1:.*]] = affine_map<(d0)[s0] -> (-d0 + s0, 5)> // CHECK-DAG: #[[MAP2:.*]] = affine_map<()[s0] -> (s0 floordiv 5)> // CHECK: func @reduction_tile_parallel_using_tile_sizes(%[[ARG0:.+]]: tensor, %[[ARG1:.+]]: tensor // CHECK-DAG: %[[C0:.*]] = arith.constant 0 : index // CHECK-DAG: %[[C1:.*]] = arith.constant 1 : index // CHECK-DAG: %[[D0:.*]] = tensor.dim %[[ARG0]], %[[C0]] : tensor // CHECK-DAG: %[[D1:.*]] = tensor.dim %[[ARG0]], %[[C1]] : tensor // CHECK-DAG: %[[PARALLEL_DIM:.+]] = affine.apply #[[MAP0]]()[%[[D1]]] // CHECK: %[[E:.*]] = tensor.empty(%[[D0]], %[[PARALLEL_DIM]]) : tensor // CHECK: %[[F:.*]] = linalg.fill // CHECK-SAME: outs(%[[E]] : // CHECK: %[[L:.*]] = scf.forall (%[[IV:.+]]) = (0) to (%[[D1]]) step (5) shared_outs(%[[ARG3:.+]] = %[[F]]) // CHECK-DAG: %[[TS0:.+]] = affine.min #[[MAP1]](%[[IV]])[%[[D1]]] // CHECK-DAG: %[[INIT_OFFSET:.+]] = affine.apply #[[MAP2]]()[%[[IV]]] // CHECK-DAG: %[[INCHUNK:.+]] = tensor.extract_slice %[[ARG0]][0, %[[IV]]] [%[[D0]], %[[TS0]]] [1, 1] // CHECK-DAG: %[[ET:.+]] = tensor.extract_slice %[[ARG3]][0, %[[INIT_OFFSET]]] [%[[D0]], 1] [1, 1] // CHECK: %[[PARTIAL:.+]] = linalg.generic // CHECK-SAME: ins(%[[INCHUNK]] : // CHECK-SAME: outs(%[[ET]] : // CHECK: scf.forall.in_parallel { // CHECK: tensor.parallel_insert_slice %[[PARTIAL]] into %[[ARG3]][0, %[[INIT_OFFSET]]] [%[[D0]], 1] [1, 1] // CHECK: } // CHECK: } // CHECK: %[[R:.*]] = linalg.reduce ins(%[[L]] // CHECK-SAME: outs(%[[ARG1]] : // CHECK: return %[[R]] : tensor module attributes {transform.with_named_sequence} { transform.named_sequence @__transform_main(%arg1: !transform.any_op {transform.readonly}) { %0 = transform.structured.match ops{["linalg.generic"]} in %arg1 : (!transform.any_op) -> !transform.any_op %1, %2, %3, %loop = transform.structured.tile_reduction_using_forall %0 by tile_sizes = [0, 5] : (!transform.any_op) -> (!transform.any_op, !transform.any_op, !transform.any_op, !transform.any_op) transform.yield } } // ----- // Check that only one of the reduction dimension can be tiled (in this case inner). #map = affine_map<(d0, d1, d2) -> (d1, d2)> #map1 = affine_map<(d0, d1, d2) -> (d0, d1, d2)> #map2 = affine_map<(d0, d1, d2) -> (d0)> module { func.func @reduction_using_forall_tile_single_of_multiple_reduction_inner( %arg0: tensor<86x128xf32>, %arg1: tensor<4096x86x128xf32>, %arg2: tensor<4096xf32>) -> tensor<4096xf32> { %0 = linalg.generic { indexing_maps = [#map, #map1, #map2], iterator_types = ["parallel", "reduction", "reduction"]} ins(%arg0, %arg1 : tensor<86x128xf32>, tensor<4096x86x128xf32>) outs(%arg2 : tensor<4096xf32>) { ^bb0(%in: f32, %in_0: f32, %out: f32): %1 = arith.mulf %in, %in_0 : f32 %2 = arith.addf %1, %out : f32 linalg.yield %2 : f32 } -> tensor<4096xf32> return %0 : tensor<4096xf32> } module attributes {transform.with_named_sequence} { transform.named_sequence @__transform_main(%arg0: !transform.any_op {transform.readonly}) { %0 = transform.structured.match ops{["linalg.generic"]} in %arg0 : (!transform.any_op) -> !transform.any_op %fill_op, %split_linalg_op, %combining_linalg_op, %for_op = transform.structured.tile_reduction_using_forall %0 reduction_dims = [2] by tile_sizes = [0, 0, 64] : (!transform.any_op) -> (!transform.any_op, !transform.any_op, !transform.any_op, !transform.any_op) transform.yield } } } // CHECK-DAG: #[[MAP0:.*]] = affine_map<()[s0] -> (s0 floordiv 64)> // CHECK: func @reduction_using_forall_tile_single_of_multiple_reduction_inner(%[[ARG0:.+]]: tensor<86x128xf32>, %[[ARG1:.+]]: tensor<4096x86x128xf32>, %[[ARG2:.+]]: tensor<4096xf32>) // CHECK: %[[E:.*]] = tensor.empty() : tensor<4096x2xf32> // CHECK: %[[F:.*]] = linalg.fill // CHECK-SAME: outs(%[[E]] : // CHECK: %[[L:.*]] = scf.forall (%[[IV:.+]]) = (0) to (128) step (64) shared_outs(%[[ARG3:.+]] = %[[F]]) // CHECK-DAG: %[[INIT_OFFSET:.+]] = affine.apply #[[MAP0]]()[%[[IV]]] // CHECK-DAG: %[[ARG0_SLICE:.+]] = tensor.extract_slice %[[ARG0]][0, %[[IV]]] [86, 64] [1, 1] // CHECK-DAG: %[[ARG1_SLICE:.+]] = tensor.extract_slice %[[ARG1]][0, 0, %[[IV]]] [4096, 86, 64] [1, 1, 1] // CHECK-DAG: %[[ET:.+]] = tensor.extract_slice %[[ARG3]][0, %[[INIT_OFFSET]]] [4096, 1] [1, 1] // CHECK: %[[PARTIAL:.+]] = linalg.generic // CHECK-SAME: ins(%[[ARG0_SLICE]], %[[ARG1_SLICE]] : // CHECK-SAME: outs(%[[ET]] : // CHECK: scf.forall.in_parallel { // CHECK: tensor.parallel_insert_slice %[[PARTIAL]] into %[[ARG3]][0, %[[INIT_OFFSET]]] [4096, 1] [1, 1] // CHECK: } // CHECK: } // CHECK: %[[R:.*]] = linalg.reduce ins(%[[L]] // CHECK-SAME: outs(%[[ARG2]] : // CHECK: return %[[R]] // ----- // Check that specifying both reduction dimensions, but setting tile size to 0 for one of them behaves consistent with specifying single reduction dimension. #map = affine_map<(d0, d1, d2) -> (d1, d2)> #map1 = affine_map<(d0, d1, d2) -> (d0, d1, d2)> #map2 = affine_map<(d0, d1, d2) -> (d0)> module { func.func @reduction_using_forall_tilesize_0_of_multiple_reduction_inner( %arg0: tensor<86x128xf32>, %arg1: tensor<4096x86x128xf32>, %arg2: tensor<4096xf32>) -> tensor<4096xf32> { %0 = linalg.generic { indexing_maps = [#map, #map1, #map2], iterator_types = ["parallel", "reduction", "reduction"]} ins(%arg0, %arg1 : tensor<86x128xf32>, tensor<4096x86x128xf32>) outs(%arg2 : tensor<4096xf32>) { ^bb0(%in: f32, %in_0: f32, %out: f32): %1 = arith.mulf %in, %in_0 : f32 %2 = arith.addf %1, %out : f32 linalg.yield %2 : f32 } -> tensor<4096xf32> return %0 : tensor<4096xf32> } module attributes {transform.with_named_sequence} { transform.named_sequence @__transform_main(%arg0: !transform.any_op {transform.readonly}) { %0 = transform.structured.match ops{["linalg.generic"]} in %arg0 : (!transform.any_op) -> !transform.any_op %fill_op, %split_linalg_op, %combining_linalg_op, %for_op = transform.structured.tile_reduction_using_forall %0 reduction_dims = [1, 2] by tile_sizes = [0, 0, 64] : (!transform.any_op) -> (!transform.any_op, !transform.any_op, !transform.any_op, !transform.any_op) transform.yield } } } // CHECK-DAG: #[[MAP0:.*]] = affine_map<()[s0] -> (s0 floordiv 64)> // CHECK: func @reduction_using_forall_tilesize_0_of_multiple_reduction_inner(%[[ARG0:.+]]: tensor<86x128xf32>, %[[ARG1:.+]]: tensor<4096x86x128xf32>, %[[ARG2:.+]]: tensor<4096xf32>) // CHECK: %[[E:.*]] = tensor.empty() : tensor<4096x2xf32> // CHECK: %[[F:.*]] = linalg.fill // CHECK-SAME: outs(%[[E]] : // CHECK: %[[L:.*]] = scf.forall (%[[IV:.+]]) = (0) to (128) step (64) shared_outs(%[[ARG3:.+]] = %[[F]]) // CHECK-DAG: %[[INIT_OFFSET:.+]] = affine.apply #[[MAP0]]()[%[[IV]]] // CHECK-DAG: %[[ARG0_SLICE:.+]] = tensor.extract_slice %[[ARG0]][0, %[[IV]]] [86, 64] [1, 1] // CHECK-DAG: %[[ARG1_SLICE:.+]] = tensor.extract_slice %[[ARG1]][0, 0, %[[IV]]] [4096, 86, 64] [1, 1, 1] // CHECK-DAG: %[[ET:.+]] = tensor.extract_slice %[[ARG3]][0, %[[INIT_OFFSET]]] [4096, 1] [1, 1] // CHECK: %[[PARTIAL:.+]] = linalg.generic // CHECK-SAME: ins(%[[ARG0_SLICE]], %[[ARG1_SLICE]] : // CHECK-SAME: outs(%[[ET]] : // CHECK: scf.forall.in_parallel { // CHECK: tensor.parallel_insert_slice %[[PARTIAL]] into %[[ARG3]][0, %[[INIT_OFFSET]]] [4096, 1] [1, 1] // CHECK: } // CHECK: } // CHECK: %[[R:.*]] = linalg.reduce ins(%[[L]] // CHECK-SAME: outs(%[[ARG2]] : // CHECK: return %[[R]] // ----- // Check that linalg.index is correctly offset after partial reduction tiling. func.func @reduction_tile_with_linalg_index(%arg0: tensor<8x128xf32>, %out: tensor<8xi32>) -> tensor<8xi32> { %red = linalg.generic { indexing_maps = [affine_map<(d0, d1) -> (d0, d1)>, affine_map<(d0, d1) -> (d0)>], iterator_types = ["parallel", "reduction"]} ins(%arg0 : tensor<8x128xf32>) outs(%out : tensor<8xi32>) { ^bb0(%in: f32, %acc: i32): %idx = linalg.index 1 : index %idx_i32 = arith.index_cast %idx : index to i32 %sum = arith.addi %idx_i32, %acc : i32 linalg.yield %sum : i32 } -> tensor<8xi32> return %red : tensor<8xi32> } module attributes {transform.with_named_sequence} { transform.named_sequence @__transform_main(%arg1: !transform.any_op {transform.readonly}) { %0 = transform.structured.match ops{["linalg.generic"]} in %arg1 : (!transform.any_op) -> !transform.any_op %1, %2, %3, %loop = transform.structured.tile_reduction_using_for %0 by tile_sizes = [0, 32] : (!transform.any_op) -> (!transform.any_op, !transform.any_op, !transform.any_op, !transform.any_op) transform.yield } } // CHECK-DAG: #[[$INDEX_MAP:.*]] = affine_map<(d0)[s0] -> (d0 + s0)> // CHECK-LABEL: func @reduction_tile_with_linalg_index( // CHECK: scf.for %[[IV:[a-zA-Z0-9]+]] = // CHECK: linalg.generic // CHECK: %[[LOCAL_IDX:.+]] = linalg.index 1 : index // CHECK: affine.apply #[[$INDEX_MAP]](%[[IV]])[%[[LOCAL_IDX]]] // ----- // Verify that tile_reduction_using_forall handles output indexing maps that // contain constant expressions (e.g. `affine_map<(d0,d1,d2)->(d0,0,d2)>`) // without crashing. Previously, generateInitialTensorForPartialReduction // unconditionally cast every map result to AffineDimExpr, triggering an // assertion when a constant expression was present (issue #173025). func.func @reduction_tile_with_constant_in_output_map( %arg0: tensor<1x4096x64xf32>, %arg1: tensor<1x1x64xf32>) -> tensor<1x1x64xf32> { %0 = linalg.generic { indexing_maps = [ affine_map<(d0, d1, d2) -> (d0, d1, d2)>, affine_map<(d0, d1, d2) -> (d0, 0, d2)> ], iterator_types = ["parallel", "reduction", "parallel"] } ins(%arg0 : tensor<1x4096x64xf32>) outs(%arg1 : tensor<1x1x64xf32>) { ^bb0(%in: f32, %out: f32): %1 = arith.addf %in, %out : f32 linalg.yield %1 : f32 } -> tensor<1x1x64xf32> return %0 : tensor<1x1x64xf32> } module attributes {transform.with_named_sequence} { transform.named_sequence @__transform_main(%arg: !transform.any_op {transform.readonly}) { %0 = transform.structured.match ops{["linalg.generic"]} in %arg : (!transform.any_op) -> !transform.any_op %1:4 = transform.structured.tile_reduction_using_forall %0 by tile_sizes = [0, 4, 0] : (!transform.any_op) -> (!transform.any_op, !transform.any_op, !transform.any_op, !transform.any_op) transform.yield } } // CHECK-LABEL: func @reduction_tile_with_constant_in_output_map // CHECK-DAG: %[[CST:.*]] = arith.constant 0.000000e+00 : f32 // CHECK-DAG: %[[E:.*]] = tensor.empty() : tensor<1x1x64x1024xf32> // CHECK: %[[F:.*]] = linalg.fill ins(%[[CST]] : f32) outs(%[[E]] : tensor<1x1x64x1024xf32>) -> tensor<1x1x64x1024xf32> // CHECK: %[[L:.*]] = scf.forall (%[[IV:.+]]) = (0) to (4096) step (4) shared_outs(%[[ARG3:.+]] = %[[F]]) -> (tensor<1x1x64x1024xf32>) { // CHECK: %[[IN_SLICE:.+]] = tensor.extract_slice %arg0[0, %[[IV]], 0] [1, 4, 64] [1, 1, 1] // CHECK: %[[INIT_SLICE:.+]] = tensor.extract_slice %[[ARG3]][0, 0, 0, {{.*}}] [1, 1, 64, 1] [1, 1, 1, 1] // CHECK: %[[PARTIAL:.+]] = linalg.generic // CHECK-SAME: ins(%[[IN_SLICE]] : // CHECK-SAME: outs(%[[INIT_SLICE]] : // CHECK: scf.forall.in_parallel { // CHECK: tensor.parallel_insert_slice %[[PARTIAL]] into %[[ARG3]][0, 0, 0, {{.*}}] [1, 1, 64, 1] [1, 1, 1, 1] // CHECK: } // CHECK: } // CHECK: linalg.reduce ins(%[[L]] : tensor<1x1x64x1024xf32>) outs(%arg1 : tensor<1x1x64xf32>) dimensions = [3] // CHECK: return %{{.*}} : tensor<1x1x64xf32> // ----- // Verify tile_reduction_using_forall with a constant in the output map when // the constant-indexed dimension size is greater than 1 (K=3). The partial // init tensor must use the actual output dim size (3), not a hardcoded 1. func.func @reduction_tile_forall_constant_dim_k_gt_1( %arg0: tensor<1x4096x64xf32>, %arg1: tensor<1x3x64xf32>) -> tensor<1x3x64xf32> { %0 = linalg.generic { indexing_maps = [ affine_map<(d0, d1, d2) -> (d0, d1, d2)>, affine_map<(d0, d1, d2) -> (d0, 0, d2)> ], iterator_types = ["parallel", "reduction", "parallel"] } ins(%arg0 : tensor<1x4096x64xf32>) outs(%arg1 : tensor<1x3x64xf32>) { ^bb0(%in: f32, %out: f32): %1 = arith.addf %in, %out : f32 linalg.yield %1 : f32 } -> tensor<1x3x64xf32> return %0 : tensor<1x3x64xf32> } module attributes {transform.with_named_sequence} { transform.named_sequence @__transform_main(%arg: !transform.any_op {transform.readonly}) { %0 = transform.structured.match ops{["linalg.generic"]} in %arg : (!transform.any_op) -> !transform.any_op %1:4 = transform.structured.tile_reduction_using_forall %0 by tile_sizes = [0, 4, 0] : (!transform.any_op) -> (!transform.any_op, !transform.any_op, !transform.any_op, !transform.any_op) transform.yield } } // CHECK-LABEL: func @reduction_tile_forall_constant_dim_k_gt_1 // CHECK-DAG: %[[CST:.*]] = arith.constant 0.000000e+00 : f32 // CHECK-DAG: %[[E:.*]] = tensor.empty() : tensor<1x3x64x1024xf32> // CHECK: %[[F:.*]] = linalg.fill ins(%[[CST]] : f32) outs(%[[E]] : tensor<1x3x64x1024xf32>) -> tensor<1x3x64x1024xf32> // CHECK: %[[L:.*]] = scf.forall (%[[IV:.+]]) = (0) to (4096) step (4) shared_outs(%[[ARG3:.+]] = %[[F]]) -> (tensor<1x3x64x1024xf32>) { // CHECK: %[[IN_SLICE:.+]] = tensor.extract_slice %arg0[0, %[[IV]], 0] [1, 4, 64] [1, 1, 1] // CHECK: %[[INIT_SLICE:.+]] = tensor.extract_slice %[[ARG3]][0, 0, 0, {{.*}}] [1, 3, 64, 1] [1, 1, 1, 1] // CHECK: %[[PARTIAL:.+]] = linalg.generic // CHECK-SAME: ins(%[[IN_SLICE]] : // CHECK-SAME: outs(%[[INIT_SLICE]] : // CHECK: scf.forall.in_parallel { // CHECK: tensor.parallel_insert_slice %[[PARTIAL]] into %[[ARG3]][0, 0, 0, {{.*}}] [1, 3, 64, 1] [1, 1, 1, 1] // CHECK: } // CHECK: } // CHECK: linalg.reduce ins(%[[L]] : tensor<1x3x64x1024xf32>) outs(%arg1 : tensor<1x3x64xf32>) dimensions = [3] // CHECK: return %{{.*}} : tensor<1x3x64xf32> // ----- // Verify tile_reduction_using_for with a constant in the output map when // the constant-indexed dimension size is greater than 1 (K=3). The partial // init tensor must use the actual output dim size (3), not a hardcoded 1. func.func @reduction_tile_for_constant_dim_k_gt_1( %arg0: tensor<1x4096x64xf32>, %arg1: tensor<1x3x64xf32>) -> tensor<1x3x64xf32> { %0 = linalg.generic { indexing_maps = [ affine_map<(d0, d1, d2) -> (d0, d1, d2)>, affine_map<(d0, d1, d2) -> (d0, 0, d2)> ], iterator_types = ["parallel", "reduction", "parallel"] } ins(%arg0 : tensor<1x4096x64xf32>) outs(%arg1 : tensor<1x3x64xf32>) { ^bb0(%in: f32, %out: f32): %1 = arith.addf %in, %out : f32 linalg.yield %1 : f32 } -> tensor<1x3x64xf32> return %0 : tensor<1x3x64xf32> } module attributes {transform.with_named_sequence} { transform.named_sequence @__transform_main(%arg: !transform.any_op {transform.readonly}) { %0 = transform.structured.match ops{["linalg.generic"]} in %arg : (!transform.any_op) -> !transform.any_op %1, %2, %3, %loop = transform.structured.tile_reduction_using_for %0 by tile_sizes = [0, 4, 0] : (!transform.any_op) -> (!transform.any_op, !transform.any_op, !transform.any_op, !transform.any_op) transform.yield } } // CHECK-LABEL: func @reduction_tile_for_constant_dim_k_gt_1 // CHECK-DAG: %[[CST:.*]] = arith.constant 0.000000e+00 : f32 // CHECK-DAG: %[[E:.*]] = tensor.empty() : tensor<1x3x64x4xf32> // CHECK: %[[F:.*]] = linalg.fill ins(%[[CST]] : f32) outs(%[[E]] : tensor<1x3x64x4xf32>) -> tensor<1x3x64x4xf32> // CHECK: %[[L:.*]] = scf.for %{{.*}} = %{{.*}} to %{{.*}} step %{{.*}} iter_args(%[[ARG3:.+]] = %[[F]]) -> (tensor<1x3x64x4xf32>) { // CHECK: %[[PARTIAL:.+]] = linalg.generic // CHECK-SAME: outs(%[[ARG3]] : // CHECK: scf.yield %[[PARTIAL]] // CHECK: } // CHECK: linalg.reduce ins(%[[L]] : tensor<1x3x64x4xf32>) outs(%arg1 : tensor<1x3x64xf32>) dimensions = [3] // CHECK: return %{{.*}} : tensor<1x3x64xf32> // ----- // Verify tile_reduction_using_forall handles dynamic output shapes combined // with a constant expression in the output map. The partial init tensor must // use tensor.dim to query the dynamic dimension at the constant-indexed // position rather than a hardcoded 1. // CHECK-LABEL: func @reduction_tile_dynamic_constant_map func.func @reduction_tile_dynamic_constant_map( %arg0: tensor, %arg1: tensor) -> tensor { %0 = linalg.generic { indexing_maps = [ affine_map<(d0, d1, d2) -> (d0, d1, d2)>, affine_map<(d0, d1, d2) -> (d0, 0, d2)> ], iterator_types = ["parallel", "reduction", "parallel"] } ins(%arg0 : tensor) outs(%arg1 : tensor) { ^bb0(%in: f32, %out: f32): %1 = arith.addf %in, %out : f32 linalg.yield %1 : f32 } -> tensor return %0 : tensor } module attributes {transform.with_named_sequence} { transform.named_sequence @__transform_main(%arg: !transform.any_op {transform.readonly}) { %0 = transform.structured.match ops{["linalg.generic"]} in %arg : (!transform.any_op) -> !transform.any_op %1:4 = transform.structured.tile_reduction_using_forall %0 by tile_sizes = [0, 4, 0] : (!transform.any_op) -> (!transform.any_op, !transform.any_op, !transform.any_op, !transform.any_op) transform.yield } } // Verify that the partial init tensor uses the correct dynamic shape. The // constant-indexed dim 1 is static (size 3) so the partial tensor is // tensor. The extract_slice within the forall body uses // size 3 at the constant-indexed position, not a hardcoded 1. // CHECK-DAG: %[[CST:.*]] = arith.constant 0.000000e+00 : f32 // CHECK-DAG: %[[E:.*]] = tensor.empty({{.*}}) : tensor // CHECK: %[[F:.*]] = linalg.fill ins(%[[CST]] : f32) outs(%[[E]] : tensor) // CHECK: scf.forall // CHECK: tensor.extract_slice {{.*}} [{{.*}}, 3, {{.*}}, 1] // CHECK: linalg.reduce ins({{.*}} : tensor) {{.*}} dimensions = [3] // CHECK: return %{{.*}} // ----- // Verify tile_reduction_using_forall handles two consecutive constant // expressions in the same output map (e.g. `(d0,d1,d2)->(0,0,d2)`). // Both constant-indexed dimensions must use the actual output dim size. // CHECK-LABEL: func @reduction_tile_two_constants_in_map func.func @reduction_tile_two_constants_in_map( %arg0: tensor<1x4096x64xf32>, %arg1: tensor<3x5x64xf32>) -> tensor<3x5x64xf32> { %0 = linalg.generic { indexing_maps = [ affine_map<(d0, d1, d2) -> (d0, d1, d2)>, affine_map<(d0, d1, d2) -> (0, 0, d2)> ], iterator_types = ["reduction", "reduction", "parallel"] } ins(%arg0 : tensor<1x4096x64xf32>) outs(%arg1 : tensor<3x5x64xf32>) { ^bb0(%in: f32, %out: f32): %1 = arith.addf %in, %out : f32 linalg.yield %1 : f32 } -> tensor<3x5x64xf32> return %0 : tensor<3x5x64xf32> } module attributes {transform.with_named_sequence} { transform.named_sequence @__transform_main(%arg: !transform.any_op {transform.readonly}) { %0 = transform.structured.match ops{["linalg.generic"]} in %arg : (!transform.any_op) -> !transform.any_op %1:4 = transform.structured.tile_reduction_using_forall %0 by tile_sizes = [0, 4, 0] : (!transform.any_op) -> (!transform.any_op, !transform.any_op, !transform.any_op, !transform.any_op) transform.yield } } // CHECK-DAG: %[[CST:.*]] = arith.constant 0.000000e+00 : f32 // CHECK-DAG: %[[E:.*]] = tensor.empty() : tensor<3x5x64x1024xf32> // CHECK: %[[F:.*]] = linalg.fill ins(%[[CST]] : f32) outs(%[[E]] : tensor<3x5x64x1024xf32>) -> tensor<3x5x64x1024xf32> // CHECK: %[[L:.*]] = scf.forall (%[[IV:.+]]) = (0) to (4096) step (4) shared_outs(%[[ARG3:.+]] = %[[F]]) -> (tensor<3x5x64x1024xf32>) { // CHECK: tensor.extract_slice %arg0[0, %[[IV]], 0] [1, 4, 64] [1, 1, 1] // CHECK: %[[INIT_SLICE:.+]] = tensor.extract_slice %[[ARG3]][0, 0, 0, {{.*}}] [3, 5, 64, 1] [1, 1, 1, 1] // CHECK: linalg.generic // CHECK: scf.forall.in_parallel { // CHECK: tensor.parallel_insert_slice {{.*}} into %[[ARG3]][0, 0, 0, {{.*}}] [3, 5, 64, 1] [1, 1, 1, 1] // CHECK: } // CHECK: } // CHECK: linalg.reduce ins(%[[L]] : tensor<3x5x64x1024xf32>) outs(%arg1 : tensor<3x5x64xf32>) dimensions = [3] // CHECK: return %{{.*}} : tensor<3x5x64xf32> // ----- // Verify tile_reduction_using_for handles dynamic output shapes combined with // a constant expression in the output map. The partial init tensor must use // tensor.dim to query the dynamic dimensions rather than hardcoding 1. // CHECK-LABEL: func @reduction_tile_for_dynamic_constant_map func.func @reduction_tile_for_dynamic_constant_map( %arg0: tensor, %arg1: tensor) -> tensor { %0 = linalg.generic { indexing_maps = [ affine_map<(d0, d1, d2) -> (d0, d1, d2)>, affine_map<(d0, d1, d2) -> (d0, 0, d2)> ], iterator_types = ["parallel", "reduction", "parallel"] } ins(%arg0 : tensor) outs(%arg1 : tensor) { ^bb0(%in: f32, %out: f32): %1 = arith.addf %in, %out : f32 linalg.yield %1 : f32 } -> tensor return %0 : tensor } module attributes {transform.with_named_sequence} { transform.named_sequence @__transform_main(%arg: !transform.any_op {transform.readonly}) { %0 = transform.structured.match ops{["linalg.generic"]} in %arg : (!transform.any_op) -> !transform.any_op %1, %2, %3, %loop = transform.structured.tile_reduction_using_for %0 by tile_sizes = [0, 4, 0] : (!transform.any_op) -> (!transform.any_op, !transform.any_op, !transform.any_op, !transform.any_op) transform.yield } } // Verify the partial init tensor uses tensor.dim for dynamic dims and the // static constant-indexed position uses size 3. The extract_slice inside // the for loop body must use size 3 at the constant-indexed position. // CHECK-DAG: %[[CST:.*]] = arith.constant 0.000000e+00 : f32 // CHECK-DAG: %[[E:.*]] = tensor.empty({{.*}}) : tensor // CHECK: %[[F:.*]] = linalg.fill ins(%[[CST]] : f32) outs(%[[E]] : tensor) // CHECK: %[[L:.*]] = scf.for %{{.*}} = %{{.*}} to %{{.*}} step %{{.*}} iter_args(%[[ARG3:.+]] = %[[F]]) -> (tensor) { // CHECK: tensor.extract_slice %arg0[{{.*}}] [{{.*}}, 4, {{.*}}] [1, 1, 1] // CHECK: tensor.extract_slice %[[ARG3]][{{.*}}] [{{.*}}, 3, {{.*}}, 4] [1, 1, 1, 1] // CHECK: } // CHECK: linalg.reduce ins(%[[L]] : tensor) outs(%arg1 : tensor) dimensions = [3] // CHECK: return %{{.*}}