Skip to content

[Passes] Remove LoopInterchange from O1 pipeline #145071

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
merged 1 commit into from
Jun 23, 2025

Conversation

nikic
Copy link
Contributor

@nikic nikic commented Jun 20, 2025

This is a fairly exotic pass, I don't think it makes a lot of sense to run it at O1, esp. as vectorization wouldn't run at O1 anyway.

This is a fairly exotic pass, I don't think it makes a lot of sense
to run it at O1, esp. as vectorization wouldn't run at O1 anyway.
@nikic nikic requested review from sjoerdmeijer and kasuga-fj June 20, 2025 16:42
Copy link
Contributor

@kasuga-fj kasuga-fj left a comment

Choose a reason for hiding this comment

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

Looks reasonable to me. To begin with, I'm not sure why this is in the "simplification" pipeline. If it is for vectorization, I think it should be in the same place as LoopDistribute.

@nikic
Copy link
Contributor Author

nikic commented Jun 20, 2025

Looks reasonable to me. To begin with, I'm not sure why this is in the "simplification" pipeline. If it is for vectorization, I think it should be in the same place as LoopDistribute.

Good question. Do we expect that loop interchange helps other simplifications in some way? If not, it's probably better to make it part of the optimization pipeline. IIRC there was a patch that added versioning support to DA, and in that case we'd definitely don't want it in the simplification pipeline.

@kasuga-fj
Copy link
Contributor

kasuga-fj commented Jun 20, 2025

By default, LoopInterchange prioritizes improving data locality over increasing vectorizable loops, which may lead to more contiguous memory accesses. I’m not very familiar with other simplification passes, but for example, such a transformation could make a loop eligible for replacement with memcpy or memmove.

That is, the following transformations could occur:

// Original code
for (int j = 0; j < m; j++)
  for (int i = 0; i < n; i++)
    a[i][j] = b[i][j];

// LoopInterchange
for (int i = 0; i < n; i++)
  for (int j = 0; j < m; j++)
    a[i][j] = b[i][j];

// LoopIdiomRecognize
for (int i = 0; i < n; i++)
  memcpy(a[i], b[i], m * sizeof(a[i][0]));

However, this is just a possibility. I don't have a strong motivation to enhance other simplifications via loop interchange, and I don't know if others have added this pass for simplifications.

IIRC there was a patch that added versioning support to DA

Maybe #123436 ? At the moment, DA only has the functionality to collect assumptions and versioning hasn't been implemented yet (though I assume it's something they intend to add in the future). IMHO, if it is allowed (e.g., low impact on compile time), it would be ideal to keep LoopInterchange in the simplification pipeline for data locality (while suppressing some features like versioning), and then add another instance in the optimization pipeline for vectorization.

Copy link
Collaborator

@sjoerdmeijer sjoerdmeijer left a comment

Choose a reason for hiding this comment

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

LGTM too.

This definitely does not belong in the simplification pipeline, the optimisation pipeline is the right home for this.

@nikic nikic merged commit ae8c85c into llvm:main Jun 23, 2025
8 checks passed
@nikic nikic deleted the loop-interchange-o1 branch June 23, 2025 07:11
miguelcsx pushed a commit to miguelcsx/llvm-project that referenced this pull request Jun 23, 2025
This is a fairly exotic pass, I don't think it makes a lot of sense to
run it at O1, esp. as vectorization wouldn't run at O1 anyway.
Jaddyen pushed a commit to Jaddyen/llvm-project that referenced this pull request Jun 23, 2025
This is a fairly exotic pass, I don't think it makes a lot of sense to
run it at O1, esp. as vectorization wouldn't run at O1 anyway.
kasuga-fj added a commit to kasuga-fj/llvm-project that referenced this pull request Jun 24, 2025
As mentioned in llvm#145071,
LoopInterchange should be part of the optimization pipeline rather than
the simplification pipeline. This patch moves LoopInterchange into the
optimization pipeline.

More contexts:

- By default, LoopInterchange attempts to improve data locality,
  however, it also takes vectorization opportunities into account. Given
  that, it is reasonable to run it as close to vectorization as
  possible.
- I looked into previous changes related to the placement of
  LoopInterchange, but couldn’t find any strong motivation suggesting
  that it benefits other simplifications.
- As far as I tried some tests (including llvm-test-suite), removing
  LoopInterchange from the simplification pipeline does not affect other
  simplifications. Therefore, there doesn't seem to be much value in
  keeping it there.
kasuga-fj added a commit to kasuga-fj/llvm-project that referenced this pull request Jun 24, 2025
As mentioned in llvm#145071,
LoopInterchange should be part of the optimization pipeline rather than
the simplification pipeline. This patch moves LoopInterchange into the
optimization pipeline.

More contexts:

- By default, LoopInterchange attempts to improve data locality,
  however, it also takes vectorization opportunities into account. Given
  that, it is reasonable to run it as close to vectorization as
  possible.
- I looked into previous changes related to the placement of
  LoopInterchange, but couldn’t find any strong motivation suggesting
  that it benefits other simplifications.
- As far as I tried some tests (including llvm-test-suite), removing
  LoopInterchange from the simplification pipeline does not affect other
  simplifications. Therefore, there doesn't seem to be much value in
  keeping it there.
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.

3 participants