This is an archive of the discontinued LLVM Phabricator instance.

[mlir][linalg] Fix vectorisation of tensor.extract with dynamic shapes
ClosedPublic

Authored by awarzynski on Jul 15 2023, 9:32 AM.

Details

Summary

The Linalg vectoriser incorrectly recognises the following
tensor.extract as contiguous:

func.func @example(%in: tensor<123x321xf32>, %arg1: tensor<1x?x8xf32>) -> tensor<1x?x8xf32> {
  %c0 = arith.constant 1 : index
  %2 = linalg.generic {
    indexing_maps = [#map1],
    iterator_types = ["parallel", "parallel", "parallel"]
  } outs(%arg1 : tensor<1x?x8xf32>)
  {
  ^bb0(%arg3: f32):
    %idx_0 = linalg.index 0 : index
    %idx_1 = linalg.index 1 : index
    %idx = arith.addi %idx_0, %idx_1 : index
    %7 = tensor.extract %in[%c0, %idx] : tensor<123x321xf32>
    linalg.yield %7 : f32
  } -> tensor<1x?x8xf32>
  return %2 : tensor<1x?x8xf32>
}

However, the following index Op corresponds to the dynamic dimension
in the iteration space:

%idx_1 = linalg.index 1 : index

The vectoriser should assume that:

  • this index Op _is not_ loop invariant,
  • the resulting memory access is a gather load

This is what this patch fixes.

Diff Detail

Event Timeline

awarzynski created this revision.Jul 15 2023, 9:32 AM
Herald added a project: Restricted Project. · View Herald Transcript
awarzynski requested review of this revision.Jul 15 2023, 9:32 AM

Simplify the test

dcaballe accepted this revision.Jul 17 2023, 9:34 AM

Thanks for the quick fix, Andrzej! This is a good starting point. I think as a follow-up, I would make sure that the tensor.extract in lit test is vectorized as a broadcast if only dimension 2 is vectorized. There are probably other refinements that can be done. Thanks!

This revision is now accepted and ready to land.Jul 17 2023, 9:34 AM

Thanks for the quick fix, Andrzej! This is a good starting point. I think as a follow-up, I would make sure that the tensor.extract in lit test is vectorized as a broadcast if only dimension 2 is vectorized. There are probably other refinements that can be done. Thanks!

Thanks! I will add a TODO in the test before landing this. Also created https://github.com/llvm/llvm-project/issues/63918 for myself.

This revision was landed with ongoing or failed builds.Jul 17 2023, 10:28 AM
This revision was automatically updated to reflect the committed changes.