Skip to content

Instantly share code, notes, and snippets.

@cathyzhyi
Created March 19, 2024 19:25
Show Gist options
  • Save cathyzhyi/d27901c5ebd44be1f6fd7413db43c6af to your computer and use it in GitHub Desktop.
Save cathyzhyi/d27901c5ebd44be1f6fd7413db43c6af to your computer and use it in GitHub Desktop.
#map = affine_map<(d0, d1) -> (d0, d1)>
func.func @linalg_transpose_2d(%arg0: tensor<?x?xf32>, %permutation: tensor<2xindex>, %1 : tensor<?x?xf32>) -> tensor<?x?xf32> {
%2 = linalg.generic {indexing_maps = [#map, #map], iterator_types = ["parallel", "parallel"]} ins(%arg0 : tensor<?x?xf32>) outs(%1 : tensor<?x?xf32>) {
^bb0(%in: f32, %out: f32):
%idx0 = linalg.index 0 : index
%idx1 = linalg.index 1 : index
%indexes = tensor.from_elements %idx0, %idx1 : tensor<2xindex>
%cst0 = arith.constant 0 : index
%cst1 = arith.constant 1 : index
%permutationIdx0 = tensor.extract %permutation[%cst0] : tensor<2xindex>
%permutationIdx1 = tensor.extract %permutation[%cst1] : tensor<2xindex>
%outputIdx0 = tensor.extract %indexes[%permutationIdx0] : tensor<2xindex>
%outputIdx1 = tensor.extract %indexes[%permutationIdx1] : tensor<2xindex>
%3 = tensor.extract %arg0[%outputIdx0, %outputIdx1] : tensor<?x?xf32>
linalg.yield %3 : f32
} -> tensor<?x?xf32>
return %2 : tensor<?x?xf32>
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment