Skip to content

[SYCL-MLIR][DetectReduction] Disallow transformation when exists may alias operations in loop #9055

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

Merged

Conversation

whitneywhtsang
Copy link
Contributor

@whitneywhtsang whitneywhtsang commented Apr 13, 2023

Original loop:

for (i) 
  A[0] += B[i];

Optimized loop:

a = A[0];
for (i)
  a += B[i];
A[0] = a;

This is incorrect if A and B may alias because B[i] may be different after updating A[0].

@whitneywhtsang whitneywhtsang requested a review from etiotto as a code owner April 13, 2023 01:00
@whitneywhtsang whitneywhtsang self-assigned this Apr 13, 2023
@whitneywhtsang whitneywhtsang added the sycl-mlir Pull requests or issues for sycl-mlir branch label Apr 13, 2023
@whitneywhtsang whitneywhtsang marked this pull request as draft April 13, 2023 01:14
@whitneywhtsang whitneywhtsang added the bug Something isn't working label Apr 13, 2023
@whitneywhtsang whitneywhtsang force-pushed the detectreduction_mayalias branch from 8fc8a97 to b8a1a3a Compare April 13, 2023 02:46
@whitneywhtsang whitneywhtsang marked this pull request as ready for review April 13, 2023 03:19
@whitneywhtsang
Copy link
Contributor Author

Plan to merge this PR after implementing a fix. But please still review it.

@whitneywhtsang whitneywhtsang requested a review from etiotto April 13, 2023 21:54
Copy link

@etiotto etiotto left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes from commit 3f4755e looks fine to me.

@whitneywhtsang whitneywhtsang force-pushed the detectreduction_mayalias branch 3 times, most recently from 28fc12b to 944b69f Compare April 19, 2023 16:30
@whitneywhtsang whitneywhtsang force-pushed the detectreduction_mayalias branch from 944b69f to 8c7f199 Compare April 26, 2023 02:07
whitneywhtsang added a commit that referenced this pull request Apr 26, 2023
Verified that with this PR and
`-D__SYCL_DISABLE_PARALLEL_FOR_RANGE_ROUNDING__`, SYCL-MLIR is able to
perform scalar replacement on reduction loop on the SYCL-Bench workloads
identified before, even with the fix in
#9055.
O3 | Previously measured gains | Only PR9187 | PR9187+PR9055
-- | -- | -- | --
2mm | 12% | 12% | 12%
3mm | 13% | 12% | 11%
covariance | 50% | 50% | 0%
gemm | 12% | 12% | 11%
gramschmidt | 14% + 18% (LICM versioning)  | 33% | 33%
syrk | 5% | 5% | 5%

=> No performance regressions with just this PR.
=> Lost `covariance` 50% gain after specializing the function, because 2
of the 3 accessors actually overlap!
```
auto data = data_buffer.get_access<access::mode::read>(cgh);
auto symmat = symmat_buffer.get_access<access::mode::discard_write>(cgh);
auto symmat2 = symmat_buffer.get_access<access::mode::discard_write>(cgh);
```
Not sure why it is written that way, with the simple source code change
below (which remove `symmat2` and always use `symmat`), the 50% gain is
recovered.
```
+++ b/polybench/covariance.cpp
@@ -110,7 +110,6 @@ public:
-                       auto symmat2 = symmat_buffer.get_access<access::mode::discard_write>(cgh);
@@ -122,7 +121,7 @@ public:
-                                       symmat2[{j2, j1}] = symmat[{j1, j2}];
+                                       symmat[{j2, j1}] = symmat[{j1, j2}];
```
If we want to get the gain without source code change, then we need to
version by only checking if `symmat` and `data` overlap, which we need
the context, we may want to perform loop versioning in detect reduction
pass.
Notice the inner loop with the opportunity doesn't use `symmat2`: 
```
for(size_t i = 1; i <= N_; i++)
  symmat[{j1, j2}] += data[{i, j1}] * data[{i, j2}];
```

Note: A number of KernelFusion test cases are moved to xfail, as
accessors cannot be internalize when they are used by a ptrtoint
operation. (#9188)

---------

Signed-off-by: Tsang, Whitney <whitney.tsang@intel.com>
@whitneywhtsang whitneywhtsang force-pushed the detectreduction_mayalias branch 2 times, most recently from 24e2b6a to ce4242c Compare April 27, 2023 00:50
…e are may alias operations in loop

Signed-off-by: Tsang, Whitney <whitney.tsang@intel.com>
Signed-off-by: Tsang, Whitney <whitney.tsang@intel.com>
Signed-off-by: Tsang, Whitney <whitney.tsang@intel.com>
@whitneywhtsang whitneywhtsang force-pushed the detectreduction_mayalias branch from ce4242c to b6bffe8 Compare April 27, 2023 03:45
Signed-off-by: Tsang, Whitney <whitney.tsang@intel.com>
@whitneywhtsang whitneywhtsang marked this pull request as draft April 27, 2023 16:21
@whitneywhtsang whitneywhtsang marked this pull request as ready for review April 28, 2023 15:15
@whitneywhtsang whitneywhtsang merged commit 6db63ae into intel:sycl-mlir Apr 28, 2023
@whitneywhtsang whitneywhtsang deleted the detectreduction_mayalias branch April 28, 2023 15:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working sycl-mlir Pull requests or issues for sycl-mlir branch
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants