Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SingleSource/Vectorizer] Add unit tests for conditional scalar assignment pattern #155

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

michaelmaitland
Copy link

@michaelmaitland michaelmaitland commented Aug 29, 2024

Dedicated unit tests for loops which contain a conditional assignment inside the loop body.

An earlier version of this patch was posted as https://reviews.llvm.org/D144909. Some updates have been made to cover more test cases.

I tested this patch with the CSA vectorization patch applied (I will add a link once the CSA patch is posted) using the following options:

-O3 -mllvm -enable-csa-vectorization -mcpu=sifive-x280

I also ran it with -mllvm -force-tail-folding-style=data-with-evl appended to the previous option set as well as -mllvm -force-tail-folding-style=none.

I also verified that vector code was generated for functions that we are currently able to vectorize using objdump. This patch contains some examples that we are not able to vectorize today but should be able to in the future.

An RFC that describes the class of problems being tested can be found here.

llvm/llvm-project#106560 is the patch that implements CSA vectorization.

…nment pattern.

Dedicated unit tests for loops which contain a conditional assignment inside
the loop body.

An earlier version of this patch was posted as https://reviews.llvm.org/D144909.
Some updates have been made to cover more test cases.

I tested this patch with the CSA patch applied (I will add a link once the CSA
patch is posted) using the following options:
```
-O3 -mllvm -enable-csa-vectorization -mcpu=sifive-x280
```
I also ran it with `-mllvm -force-tail-folding-style=data-with-evl` appended
to the previous option set as well as `-mllvm -force-tail-folding-style=none`.

I also verified that vector code was generated for functions that we are
currently able to vectorize using objdump. This patch contains some examples
that we are not able to vectorize today but should be able to in the future.
michaelmaitland added a commit to michaelmaitland/llvm-project that referenced this pull request Dec 27, 2024
This patch adds initial support for CSA vectorization LLVM. This new class
can be characterized by vectorization of assignment to a scalar in a loop,
such that the assignment is conditional from the perspective of its use.
An assignment is conditional in a loop if a value may or may not be assigned
in the loop body.

For example:

```
int t = init_val;
for (int i = 0; i < N; i++) {
  if (cond[i])
    t = a[i];
}
s = t; // use t
```

Using pseudo-LLVM code this can be vectorized as

```
vector.ph:
  ...
  %t = %init_val
  %init.mask = <all-false-vec>
  %init.data = <poison-vec> ; uninitialized
vector.body:
  ...
  %mask.phi = phi [%init.mask, %vector.ph], [%new.mask, %vector.body]
  %data.phi = phi [%data.mask, %vector.ph], [%new.mask, %vector.body]
  %cond.vec = <widened-cmp> ...
  %a.vec    = <widened-load> %a, %i
  %b        = <any-lane-active> %cond.vec
  %new.mask = select %b, %cond.vec, %mask.phi
  %new.data = select %b, %a.vec, %data.phi
  ...
middle.block:
  %s = <extract-last-active-lane> %new.mask, %new.data
```

On each iteration, we track whether any lane in the widened condition was active,
and if it was take the current mask and data as the new mask and data vector.
Then at the end of the loop, the scalar can be extracted only once.

This transformation works the same way for integer, pointer, and floating point
conditional assignment, since the transformation does not require inspection
of the data being assigned.

In the vectorization of a CSA, we will be introducing recipes into the vector
preheader, the vector body, and the middle block. Recipes that are introduced
into the preheader and middle block are executed only one time, and recipes
that are in the vector body will be possibly executed multiple times. The more
times that the vector body is executed, the less of an impact the preheader
and middle block cost have on the overall cost of a CSA.

A detailed explanation of the concept can be found [here](https://discourse.llvm.org/t/vectorization-of-conditional-scalar-assignment-csa/80964).

This patch is further tested in llvm/llvm-test-suite#155.

This patch contains only the non-EVL related code. The is based on the larger
patch of llvm#106560, which contained both EVL and non-EVL related parts.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant