Skip to content

Mlir Backend: Tensor Dialect Support#45

Merged
guillon merged 6 commits into
xtc-tools:mainfrom
liamsemeria:dev/sliam/mlir-tensor-dialect
Jun 5, 2026
Merged

Mlir Backend: Tensor Dialect Support#45
guillon merged 6 commits into
xtc-tools:mainfrom
liamsemeria:dev/sliam/mlir-tensor-dialect

Conversation

@liamsemeria
Copy link
Copy Markdown
Contributor

@liamsemeria liamsemeria commented Feb 10, 2026

Motivation

Support for ops in the tensor dialect allows for tracking of producer-consumer relationships and broadcasting, which allow for operator fusion and element-wise operations respectively.

Description

The mlir backend now has an option use_tensor_dialect that causes ops to be generated in the tensor dialect. The tensor dialect gets lowered into memref by a new bufferization pass after the transform pass is applied (can be printed with print_bufferization_ir=True).

Discussion

How the Tensor Dialect Affects the IR:

  • matmul and conv2d:
    The bufferization results in the exact same lowered mlir as the memref dialect ops (at least for unscheduled).

  • relu:
    Collapsing the shape to 1 dim requires the tensor to be expanded (unlike the memref), resulting in an extra memory allocation after bufferization. So the relu for the tensor dialect is non-collapsing, which is also required for consumer fusion to work properly in the future.

  • pad and unpad:
    The tensor implementation uses a linalg.generic which is needed for fusion. It has dynamic dims which requires mlir: updated extra-tools version #70 an update to the extra tools for the c backend to work properly.

@liamsemeria liamsemeria force-pushed the dev/sliam/mlir-tensor-dialect branch 3 times, most recently from 855d715 to 58ffe40 Compare February 11, 2026 14:08
@qaco
Copy link
Copy Markdown
Contributor

qaco commented Feb 26, 2026

It is really cool ! Congrats !

When you say that you wait for an xDSL release, is it because you know that the feature will be added ?

@liamsemeria
Copy link
Copy Markdown
Contributor Author

It is really cool ! Congrats !

When you say that you wait for an xDSL release, is it because you know that the feature will be added ?

Thanks! yeah I contributed tensor.pad but its probably easiest to just wait for the next release to add it to xtc.

@liamsemeria liamsemeria marked this pull request as ready for review April 10, 2026 08:59
@liamsemeria liamsemeria added the enhancement New feature or request label Apr 20, 2026
@liamsemeria liamsemeria requested a review from guillon April 21, 2026 13:40
@qaco
Copy link
Copy Markdown
Contributor

qaco commented Apr 27, 2026

I will need some time to review this! Pls ping me when it's ready!

@liamsemeria
Copy link
Copy Markdown
Contributor Author

I will need some time to review this! Pls ping me when it's ready!

@qaco Sorry for the delay during my break I thought I had an idea to make the pad better but turns out it wouldn't work as I expected. The PR is ready to review. I'm currently working on making the compile time not insanely terrible but the majority of the tensor dialect PR would be unchanged.

@liamsemeria liamsemeria force-pushed the dev/sliam/mlir-tensor-dialect branch 2 times, most recently from 62d2977 to c0fbf01 Compare May 18, 2026 15:25
@liamsemeria liamsemeria requested a review from qaco May 18, 2026 15:27
Copy link
Copy Markdown
Member

@guillon guillon left a comment

Choose a reason for hiding this comment

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

It's fine for me, @liamsemeria you can merge when ready.

Note for future work:

  • may use the sdsl pad directly
  • need to study performance issues with one-shot-bufferize

If there are further devs to do w.r.t. these points, we will make new PRs.

@guillon guillon assigned liamsemeria and unassigned guillon May 26, 2026
@liamsemeria liamsemeria force-pushed the dev/sliam/mlir-tensor-dialect branch from c0fbf01 to 1eca727 Compare May 26, 2026 15:14
@liamsemeria
Copy link
Copy Markdown
Contributor Author

@guillon I dont have the option to merge on my end, but I updated I rebased it with main and its ready to merge.

@liamsemeria liamsemeria requested a review from guillon May 27, 2026 07:39
@guillon
Copy link
Copy Markdown
Member

guillon commented May 27, 2026

I will need some time to review this! Pls ping me when it's ready!

@qaco The PR is ready to merge, can you check?

@liamsemeria liamsemeria force-pushed the dev/sliam/mlir-tensor-dialect branch from fc5a68b to a2b3b20 Compare May 27, 2026 10:23
@guillon guillon assigned qaco and unassigned liamsemeria May 27, 2026
@liamsemeria liamsemeria force-pushed the dev/sliam/mlir-tensor-dialect branch from a2b3b20 to f2b2881 Compare May 28, 2026 09:02
@liamsemeria liamsemeria force-pushed the dev/sliam/mlir-tensor-dialect branch 2 times, most recently from fe3ffe7 to 2738915 Compare June 2, 2026 10:53
@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Jun 2, 2026

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 93.54839% with 10 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/xtc/cli/explore.py 44.44% 5 Missing ⚠️
src/xtc/backends/mlir/MlirNodeBackend.py 33.33% 4 Missing ⚠️
src/xtc/backends/mlir/MlirOps.py 98.64% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@liamsemeria liamsemeria force-pushed the dev/sliam/mlir-tensor-dialect branch from 2d4619a to 8d527ff Compare June 2, 2026 15:57
liamsemeria and others added 4 commits June 3, 2026 15:03
also added multi-output graphs and cleanup passes needed for fusion
also removed pre tensor lowering ir dump
…added using_tensors_hint

also added matmul layout test, comments and readability changes
@liamsemeria liamsemeria force-pushed the dev/sliam/mlir-tensor-dialect branch from 8d527ff to bae99b3 Compare June 3, 2026 13:04
@guillon
Copy link
Copy Markdown
Member

guillon commented Jun 3, 2026

@liamsemeria why do we need an additional setting using_tensors_hint ?
Shouldn't it be the default?

@liamsemeria
Copy link
Copy Markdown
Contributor Author

@liamsemeria why do we need an additional setting using_tensors_hint ? Shouldn't it be the default?

@guillon It's needed to enable tensor specific changes to the compiler passes which are moving vectorization lowering patterns to after bufferization and using the FoldUnitDims pattern.

If it was enabled by default the major changes would be that there would be a lingering transform.sequence (the post vectorization patterns) in the IR Dump After Transform output of each test, and there is an annotation to the function that gets added in order to apply the vector patterns as well. Both are visible in this test. For the function annotation I was planning on double checking to see if there's a way around doing it at some point.

A way the using_tensors_hint can be removed is by doing a quick traversal of the IR to detect if tensors or memref are used. That way the changes I mentioned above would only apply to the tensor tests and not the memref tests. It could be done in the python bindings by looking at the function operands to see what dialect they are.

I can explain it more tomorrow if you need any clarifications.

Comment thread src/xtc/backends/mlir/MlirCompilerPasses.py Outdated
Copy link
Copy Markdown
Contributor

@qaco qaco left a comment

Choose a reason for hiding this comment

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

Great job!

@liamsemeria liamsemeria force-pushed the dev/sliam/mlir-tensor-dialect branch from bae99b3 to 48d6580 Compare June 4, 2026 08:38
@liamsemeria
Copy link
Copy Markdown
Contributor Author

@guillon @qaco its ready to merge unless you guys have any other changes in mind. Thanks for taking a look.

@guillon
Copy link
Copy Markdown
Member

guillon commented Jun 4, 2026

@guillon @qaco its ready to merge unless you guys have any other changes in mind. Thanks for taking a look.

I need to understand first the using_tensors_hint changes and the discussion on the buffer/vectorize post passes, I do not catch yet and I do not think it's good to have an additional flag which seems not "user" oriented.

@qaco
Copy link
Copy Markdown
Contributor

qaco commented Jun 4, 2026

@guillon @qaco its ready to merge unless you guys have any other changes in mind. Thanks for taking a look.

I need to understand first the using_tensors_hint changes and the discussion on the buffer/vectorize post passes, I do not catch yet and I do not think it's good to have an additional flag which seems not "user" oriented.

Should we make the integration of tensor transparent ? Like : if nothing happens at tensor level, the tensors are immediately bufferized, and then the buffer-level transformations occur

@guillon
Copy link
Copy Markdown
Member

guillon commented Jun 4, 2026

@guillon @qaco its ready to merge unless you guys have any other changes in mind. Thanks for taking a look.

I need to understand first the using_tensors_hint changes and the discussion on the buffer/vectorize post passes, I do not catch yet and I do not think it's good to have an additional flag which seems not "user" oriented.

Should we make the integration of tensor transparent ? Like : if nothing happens at tensor level, the tensors are immediately bufferized, and then the buffer-level transformations occur

Good point. Do you think it's feasible to have a single transform pass @liamsemeria?

Replaced using_tensor_hint with detecting tensors automatically, will be removed later since it requires updating all memref tests which is best done in a seperate PR.
Fixed an issue where applying UnitDims folding applied to the entire function instead of the specified vectorized loop.
@liamsemeria liamsemeria assigned guillon and unassigned qaco Jun 5, 2026
@guillon guillon merged commit 17dd217 into xtc-tools:main Jun 5, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants