Skip to content

[LinalgExt] support converting argcompare to loops. - #21138

Merged
bangtianliu merged 5 commits into
iree-org:mainfrom
bangtianliu:arg_compare_loops
Jun 24, 2025
Merged

bangtianliu merged 5 commits into
iree-org:mainfrom
bangtianliu:arg_compare_loops

Conversation

@bangtianliu

@bangtianliu bangtianliu commented Jun 19, 2025 •

Copy link
Copy Markdown
Contributor

On top of #21106.

This PR Adds generateScalarImplementation for ArgCompareOp, defining the scalar behavior within the innermost loop during loop lowering.

The lowering performs the following:

  • Loads the current best value and corresponding index (accumulator) from the output tensor.
  • Loads the candidate value at the current loop indices.
  • Invokes the provided comparator region (T a, T b) -> i1 to compare values.
  • Selects the new best value and index based on the comparison result.
  • Supports an optional index_base operand, which is added to the computed index before storing.

Next, will add tiling support.

@bangtianliu
bangtianliu marked this pull request as draft June 19, 2025 16:24
@bangtianliu
bangtianliu requested review from IanWood1, Max191 and kuhar June 19, 2025 20:41
@bangtianliu
bangtianliu marked this pull request as ready for review June 19, 2025 20:41
Signed-off-by: Bangtian Liu <[email protected]>
@bangtianliu
bangtianliu requested review from IanWood1 and kuhar June 20, 2025 21:19
Signed-off-by: Bangtian Liu <[email protected]>
@bangtianliu
bangtianliu requested a review from kuhar June 23, 2025 13:43

@IanWood1 IanWood1 left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM but I don't have enough context on codegen to know if the scf.if is going to be problematic.

It looks possible to move it out of the loop but the memref.load might make that difficult (could be solved by duplicating/cloning into both the "then" and "else" regions when building the op).

@bangtianliu

Copy link
Copy Markdown
Contributor Author

LGTM but I don't have enough context on codegen to know if the scf.if is going to be problematic.

It looks possible to move it out of the loop but the memref.load might make that difficult (could be solved by duplicating/cloning into both the "then" and "else" regions when building the op).

Thanks for your suggestion. but how about the case in which the reduction dim is not innermost?

@IanWood1

Copy link
Copy Markdown
Member

Thanks for your suggestion. but how about the case in which the reduction dim is not innermost?

I don't think it works because memref.load/memref.store aren't speculatable so they can't be LICM-ed. But feedback from someone more familiar would be helpful (not sure who specifically that is).

@MaheshRavishankar

Copy link
Copy Markdown
Collaborator

We need to add an end-to-end test for this. Even if it runs just on CPU. Its not a requirement for this PR, but really that is the only way to know this is all correct?

@MaheshRavishankar MaheshRavishankar left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Think there is a mistake here.

@MaheshRavishankar MaheshRavishankar left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

(damn github, let me say request changes please!)

@bangtianliu

bangtianliu commented Jun 23, 2025 •

Copy link
Copy Markdown
Contributor Author

We need to add an end-to-end test for this. Even if it runs just on CPU. Its not a requirement for this PR, but really that is the only way to know this is all correct?

Sure, I'll look into adding end-to-end tests in this PR.

@bangtianliu

Copy link
Copy Markdown
Contributor Author

@MaheshRavishankar It looks like the e2e test requires the tiling interface to be in place. Perhaps we can land this PR first, and if any issues arise, I’ll follow up with a separate fix.

@bangtianliu
bangtianliu merged commit ed43694 into iree-org:main Jun 24, 2025
AWoloszyn pushed a commit that referenced this pull request Dec 1, 2025
On top of #21106.

This PR Adds `generateScalarImplementation` for ArgCompareOp, defining
the scalar behavior within the innermost loop during loop lowering.

The lowering performs the following: 
- Loads the current best value and corresponding index (accumulator)
from the output tensor.
- Loads the candidate value at the current loop indices.
- Invokes the provided comparator region (T a, T b) -> i1 to compare
values.
- Selects the new best value and index based on the comparison result.
- Supports an optional index_base operand, which is added to the
computed index before storing.

Next, will add tiling support.

---------

Signed-off-by: Bangtian Liu <[email protected]>
bangtianliu added a commit that referenced this pull request Jan 19, 2026
…de (#23193)

This PR extends the tiling interface implementation for `ArgCompareOp`
to support explicit-index mode (2 inputs: value + pre-computed indices),
which was introduced in PR #23153.

 **Key changes:**
- Extended `getTiledImplementation` (introduced in PR #21077) to slice
both value and index inputs in explicit-index mode
- Extended `generateScalarImplementation` (convert_to_loops) (introduced
in PR #21138) to load indices from input instead of computing from
iteration variables in explicit-index mode

Signed-off-by: Bangtian Liu <[email protected]>
keshavvinayak01 pushed a commit that referenced this pull request Jan 27, 2026
…de (#23193)

This PR extends the tiling interface implementation for `ArgCompareOp`
to support explicit-index mode (2 inputs: value + pre-computed indices),
which was introduced in PR #23153.

 **Key changes:**
- Extended `getTiledImplementation` (introduced in PR #21077) to slice
both value and index inputs in explicit-index mode
- Extended `generateScalarImplementation` (convert_to_loops) (introduced
in PR #21138) to load indices from input instead of computing from
iteration variables in explicit-index mode

Signed-off-by: Bangtian Liu <[email protected]>
Signed-off-by: Keshav Vinayak Jha <[email protected]>
MaheshRavishankar pushed a commit to MaheshRavishankar/iree that referenced this pull request Feb 24, 2026
…de (iree-org#23193)

This PR extends the tiling interface implementation for `ArgCompareOp`
to support explicit-index mode (2 inputs: value + pre-computed indices),
which was introduced in PR iree-org#23153.

 **Key changes:**
- Extended `getTiledImplementation` (introduced in PR iree-org#21077) to slice
both value and index inputs in explicit-index mode
- Extended `generateScalarImplementation` (convert_to_loops) (introduced
in PR iree-org#21138) to load indices from input instead of computing from
iteration variables in explicit-index mode

Signed-off-by: Bangtian Liu <[email protected]>
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.

4 participants