Identify that a linalg.GenericOp imlpements tosa.ReduceSum

Hi,
Is there a way to tell from a given linalg.GenericOp that it actually implements a tosa.ReduceSum
(of other tosa reduce) operation for a single axis of the input tensor?

I guess I can do it by analyzing the affine maps, and determining that all is identity, except for one axis which is missing on the reduced output tensor, but maybe there is a simple way?
(I’m writing a pass which runs right after tosa-to-linalg)

thanks, Mirit

Analyzing indexing maps, iterator types and the body sounds like the right approach for this sort of raising. This is rather common so you might be able to compose this from the pieces of code available upstream, for example, here: https://github.com/llvm/llvm-project/blob/4e43ba25998075c5facd98675e75268e72259d71/mlir/include/mlir/Analysis/SliceAnalysis.h#L259.

I would also highly encourage you to create passes an abstraction that have the required information readily available instead of attempting to recover it after it was destroyed. For example, Linalg has an explicit reduction operation – 'linalg' Dialect - MLIR – that could be lowered to instead of using the generic.

thanks!
Maybe you know why tosa-to-linalg lowers tosa reduces to linalg.GenericOp and not to linalg.ReduceOp?

Unfortunately not. Someone like @rsuderman would probably know.

No worries, the explanation is pretty straightforward. The linalg.reduce op did not exist when tosa-to-linalg was written :slight_smile:. Happy to review a patch to port over to linalg.reduce.

thanks!
I think we may try to implement it.

Be sure to tag me on the review as some downstream projects may need to add support for linalg.reduce.

ok, thanks!