Skip to content

[Codegen][GPU] Prevent vector transfer fission from applying on loops with side-effecting ops - #21166

Merged
rkayaith merged 3 commits into
iree-org:mainfrom
rkayaith:is-943-fission-side-effecting-ops
Jun 24, 2025
Merged

rkayaith merged 3 commits into
iree-org:mainfrom
rkayaith:is-943-fission-side-effecting-ops

Conversation

@rkayaith

Copy link
Copy Markdown
Member

Fission should be skipped when there are side-effecting operations in the loop, besides the targeted vector.transfer_{read,write} ops. The fixes a bug where other side-effecting ops get erased during loop splitting.


// Only the read/write ops may have side effects, we assume we can
// re-order/erase the other ops freely.
if (!llvm::all_of(forOp.getOps(), [&](Operation &op) -> bool {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think this might be more efficient as a separate function called after we find the fisson target, so we dont keep walking all the ops in the scf.for in a loop.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Just to clarify I mean when we find a legal fisson target, we can do this check and just return failure in the loop itself.

@rkayaith rkayaith self-assigned this Jun 24, 2025

@nirvedhmeshram nirvedhmeshram left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM!

@jerryyin jerryyin 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.

Sorry I'm a bit confused on where the problem is.

I can certainly see why non-pure ops can caused a problem in fission, like the unit test you had. Thanks for fixing that!

But in particular in the lowering IR of the motivation example, I thought the fission pass is targeting a loop with only pure ops (except transfer_read/write), but multiple transfer_read and transfer_write? Below is what I see:

scf.for %arg6 = %c0 to %37 step %c1 {
  %subview_23 = memref.subview %alloca_21[%arg6, 0, 0, 0] [1, 1, 1, 1] [1, 1, 1, 1] : memref<?x1x1x8xbf16, #gpu.address_space<private>> to memref<1x1x1x1xbf16, strided<[8, 8, 8, 1], offset: ?>, #gpu.address_space<private>>
  %subview_24 = memref.subview %subview_14[%57#0, %60, %65, %arg6] [1, %62, %67, 1] [1, 1, 1, 1] : memref<16x192x128x?xbf16, strided<[983040, 5120, 40, 1], offset: ?>, #amdgpu.address_space<fat_raw_buffer>> to memref<1x?x?x1xbf16, strided<[983040, 5120, 40, 1], offset: ?>, #amdgpu.address_space<fat_raw_buffer>>
  %278 = arith.index_castui %62 : index to i1
  %279 = arith.index_castui %67 : index to i1
  %280 = arith.andi %278, %279 : i1
  %281 = vector.transfer_read %subview_24[%c0, %c0, %c0, %c0], %cst_0 {in_bounds = [true, true, true, true]} : memref<1x?x?x1xbf16, strided<[983040, 5120, 40, 1], offset: ?>, #amdgpu.address_space<fat_raw_buffer>>, vector<1x1x1x1xbf16>
  %282 = arith.select %280, %281, %cst_2 : vector<1x1x1x1xbf16>
  vector.transfer_write %282, %subview_23[%c0, %c0, %c0, %c0] {in_bounds = [true, true, true, true]} : vector<1x1x1x1xbf16>, memref<1x1x1x1xbf16, strided<[8, 8, 8, 1], offset: ?>, #gpu.address_space<private>>
  %subview_25 = memref.subview %alloca_21[%arg6, 0, 0, 1] [1, 1, 1, 1] [1, 1, 1, 1] : memref<?x1x1x8xbf16, #gpu.address_space<private>> to memref<1x1x1x1xbf16, strided<[8, 8, 8, 1], offset: ?>, #gpu.address_space<private>>
  %subview_26 = memref.subview %subview_14[%69#0, %72, %77, %arg6] [1, %74, %79, 1] [1, 1, 1, 1] : memref<16x192x128x?xbf16, strided<[983040, 5120, 40, 1], offset: ?>, #amdgpu.address_space<fat_raw_buffer>> to memref<1x?x?x1xbf16, strided<[983040, 5120, 40, 1], offset: ?>, #amdgpu.address_space<fat_raw_buffer>>
  %283 = arith.index_castui %74 : index to i1
  %284 = arith.index_castui %79 : index to i1
  %285 = arith.andi %283, %284 : i1
  %286 = vector.transfer_read %subview_26[%c0, %c0, %c0, %c0], %cst_0 {in_bounds = [true, true, true, true]} : memref<1x?x?x1xbf16, strided<[983040, 5120, 40, 1], offset: ?>, #amdgpu.address_space<fat_raw_buffer>>, vector<1x1x1x1xbf16>
  %287 = arith.select %285, %286, %cst_2 : vector<1x1x1x1xbf16>
  vector.transfer_write %287, %subview_25[%c0, %c0, %c0, %c0] {in_bounds = [true, true, true, true]} : vector<1x1x1x1xbf16>, memref<1x1x1x1xbf16, strided<[8, 8, 8, 1], offset: ?>, #gpu.address_space<private>>
  ...

I thought this is the root cause of the problem and also needs fixing? Or am I seeing the wrong loop?

On a second thought I see your approach here. This condition will see other transfer_read and transfer_write that were not the read/write op we've recorded, and bail out when this IR is encountered. Now it make sense to me.

@jerryyin jerryyin 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

@rkayaith
rkayaith merged commit 5fe38a7 into iree-org:main Jun 24, 2025
@rkayaith
rkayaith deleted the is-943-fission-side-effecting-ops branch June 24, 2025 17:55
AWoloszyn pushed a commit that referenced this pull request Dec 1, 2025
… with side-effecting ops (#21166)

Fission should be skipped when there are side-effecting operations in
the loop, besides the targeted `vector.transfer_{read,write}` ops. The
fixes a bug where other side-effecting ops get erased during loop
splitting.
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