dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
14.93k stars 4.64k forks source link

ARM64-SVE: No obvious way to mask an Across function #101770

Closed a74nh closed 4 months ago

a74nh commented 4 months ago

ConditionalSelect is used to mask an API, for example:

a = Sve.ConditionalSelect(mask, Sve.Add(a,b), a);

Is equivalent to:

ADD zA, zMask/M, zA, zB

This does not work when using the across methods.

a = Sve.ConditionalSelect(mask, Sve.AddAcross(a), a);

Is saying to add together all the elements in a and place in field 0, zeroing all other elements. Then use mask to select which elements to pick from the result. That is valid C#, but is a weird thing to do.

the more likely use case is to take all the elements in a that are marked as active in mask and add them together, putting them in field 0, zeroing all other elements. There is no obvious way to do this?

a74nh commented 4 months ago

Having said that, it now occurs to me that for AddAcross you could do:

a = Sve.AddAcross(Sve.ConditionalSelect(mask, a, zero));

But that doesn't work for all Across methods. For example, MinAcross().

a74nh commented 4 months ago

@kunalspathak @tannergooding - this came up as part of implementing https://github.com/dotnet/runtime/pull/101674

kunalspathak commented 4 months ago

We discussed it in https://github.com/dotnet/runtime/pull/101674#discussion_r1583392032

tannergooding commented 4 months ago

Cases like MinAcross could be handled by doing Sve.MinAcross(Sve.ConditionalSelect(mask, a, Vector.Create(max)) (where max for float is +Infinity). That being said, predication of "across" APIs is in general a more complex case and so may be one of the scenarios that warrants having an overload that explicitly takes the mask.

We simply want to prefer ConditionSelect in the cases where pattern matching and the pattern a user would need to write is "obvious" as it saves us from needing to expose thousands of additional API overloads. It also has the added benefit that it matches the patterns users already have to write for ISAs without built-in masking support, such as AdvSimd (NEON), and so allows existing code to be shared across these ISAs and to implicitly light up, making the entire ecosystem trivially better.

a74nh commented 4 months ago

I think in these cases extra API methods would make sense.

This probably needs splitting into two issues then:

  1. An API issue with the new methods in it
  2. For the cases where the user does write code in the *Across(ConditionalSelect) style, something in lowering to detect and optimise.
dotnet-policy-service[bot] commented 4 months ago

Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch See info in area-owners.md if you want to be subscribed.

a74nh commented 4 months ago

Opened:

Closing this issue in favour of those