-
Notifications
You must be signed in to change notification settings - Fork 28
[asm] Async memory iter_arg tying safety in register allocator #978
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
Draft
Hardcode84
wants to merge
1
commit into
iree-org:main
Choose a base branch
from
Hardcode84:vmem-iter-arg-tying
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
150 changes: 150 additions & 0 deletions
150
wave_lang/kernel/wave/asm/wave_asm/test/Transforms/vmem-iter-arg-tying-emit.mlir
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,150 @@ | ||
| // RUN: waveasm-translate --waveasm-linear-scan --emit-assembly %s | FileCheck %s | ||
| // | ||
| // Assembly emission tests for async memory iter_arg tying. | ||
| // | ||
| // Verifies that the emitter produces correct back-edge copies when iter_args | ||
| // and block args are NOT tied (unsafe async memory ops), and omits copies | ||
| // when they ARE tied (safe ordering or synchronous ops). | ||
|
|
||
| //===----------------------------------------------------------------------===// | ||
| // Test 1: UNSAFE buffer_load — emitter must produce 4 × v_mov_b32 copies | ||
| // to move the untied iter_arg into the block arg's register at the back edge. | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| // CHECK-LABEL: vmem_emit_unsafe: | ||
| waveasm.program @vmem_emit_unsafe | ||
| target = #waveasm.target<#waveasm.gfx942, 5> | ||
| abi = #waveasm.abi<> { | ||
|
|
||
| %c0 = waveasm.constant 0 : !waveasm.imm<0> | ||
| %c1 = waveasm.constant 1 : !waveasm.imm<1> | ||
| %c10 = waveasm.constant 10 : !waveasm.imm<10> | ||
| %srd = waveasm.precolored.sreg 0, 4 : !waveasm.psreg<0, 4> | ||
| %a = waveasm.precolored.vreg 0, 4 : !waveasm.pvreg<0, 4> | ||
| %b = waveasm.precolored.vreg 4, 4 : !waveasm.pvreg<4, 4> | ||
|
|
||
| %init_i = waveasm.s_mov_b32 %c0 : !waveasm.imm<0> -> !waveasm.sreg | ||
| %init_acc = waveasm.v_mov_b32 %c0 : !waveasm.imm<0> -> !waveasm.vreg<4, 4> | ||
| %init_data = waveasm.buffer_load_dwordx4 %srd, %c0, %c0 : !waveasm.psreg<0, 4>, !waveasm.imm<0>, !waveasm.imm<0> -> !waveasm.vreg<4, 4> | ||
|
|
||
| // CHECK: L_loop_0: | ||
| // CHECK: buffer_load_dwordx4 | ||
| // CHECK: v_mfma_f32_16x16x16_f16 | ||
| // Back-edge copies: 4 individual v_mov_b32 for the wide register. | ||
| // CHECK: v_mov_b32 | ||
| // CHECK: v_mov_b32 | ||
| // CHECK: v_mov_b32 | ||
| // CHECK: v_mov_b32 | ||
| // CHECK: s_cbranch_scc1 L_loop_0 | ||
| %ri, %racc, %rdata = waveasm.loop( | ||
| %i = %init_i, %acc = %init_acc, %data = %init_data) | ||
| : (!waveasm.sreg, !waveasm.vreg<4, 4>, !waveasm.vreg<4, 4>) | ||
| -> (!waveasm.sreg, !waveasm.vreg<4, 4>, !waveasm.vreg<4, 4>) { | ||
|
|
||
| %data_next = waveasm.buffer_load_dwordx4 %srd, %c0, %c0 | ||
| : !waveasm.psreg<0, 4>, !waveasm.imm<0>, !waveasm.imm<0> -> !waveasm.vreg<4, 4> | ||
| %acc_new = waveasm.v_mfma_f32_16x16x16_f16 %data, %b, %acc | ||
| : !waveasm.vreg<4, 4>, !waveasm.pvreg<4, 4>, !waveasm.vreg<4, 4> -> !waveasm.vreg<4, 4> | ||
|
|
||
| %next_i = waveasm.s_add_u32 %i, %c1 : !waveasm.sreg, !waveasm.imm<1> -> !waveasm.sreg | ||
| %cond = waveasm.s_cmp_lt_u32 %next_i, %c10 : !waveasm.sreg, !waveasm.imm<10> -> !waveasm.sreg | ||
| waveasm.condition %cond : !waveasm.sreg | ||
| iter_args(%next_i, %acc_new, %data_next) | ||
| : !waveasm.sreg, !waveasm.vreg<4, 4>, !waveasm.vreg<4, 4> | ||
| } | ||
|
|
||
| waveasm.s_endpgm | ||
| } | ||
|
|
||
| //===----------------------------------------------------------------------===// | ||
| // Test 2: SAFE buffer_load — block arg dead before load, no copies needed. | ||
| // The buffer_load writes directly into the block arg's register. | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| // CHECK-LABEL: vmem_emit_safe: | ||
| waveasm.program @vmem_emit_safe | ||
| target = #waveasm.target<#waveasm.gfx942, 5> | ||
| abi = #waveasm.abi<> { | ||
|
|
||
| %c0 = waveasm.constant 0 : !waveasm.imm<0> | ||
| %c1 = waveasm.constant 1 : !waveasm.imm<1> | ||
| %c10 = waveasm.constant 10 : !waveasm.imm<10> | ||
| %srd = waveasm.precolored.sreg 0, 4 : !waveasm.psreg<0, 4> | ||
| %b = waveasm.precolored.vreg 4, 4 : !waveasm.pvreg<4, 4> | ||
|
|
||
| %init_i = waveasm.s_mov_b32 %c0 : !waveasm.imm<0> -> !waveasm.sreg | ||
| %init_acc = waveasm.v_mov_b32 %c0 : !waveasm.imm<0> -> !waveasm.vreg<4, 4> | ||
| %init_data = waveasm.buffer_load_dwordx4 %srd, %c0, %c0 : !waveasm.psreg<0, 4>, !waveasm.imm<0>, !waveasm.imm<0> -> !waveasm.vreg<4, 4> | ||
|
|
||
| // CHECK: L_loop_0: | ||
| // CHECK: v_mfma_f32_16x16x16_f16 | ||
| // CHECK: buffer_load_dwordx4 | ||
| // No v_mov copies — tied registers, load writes to block arg directly. | ||
| // CHECK-NOT: v_mov_b32 | ||
| // CHECK: s_cbranch_scc1 L_loop_0 | ||
| %ri, %racc, %rdata = waveasm.loop( | ||
| %i = %init_i, %acc = %init_acc, %data = %init_data) | ||
| : (!waveasm.sreg, !waveasm.vreg<4, 4>, !waveasm.vreg<4, 4>) | ||
| -> (!waveasm.sreg, !waveasm.vreg<4, 4>, !waveasm.vreg<4, 4>) { | ||
|
|
||
| %acc_new = waveasm.v_mfma_f32_16x16x16_f16 %data, %b, %acc | ||
| : !waveasm.vreg<4, 4>, !waveasm.pvreg<4, 4>, !waveasm.vreg<4, 4> -> !waveasm.vreg<4, 4> | ||
| %data_next = waveasm.buffer_load_dwordx4 %srd, %c0, %c0 | ||
| : !waveasm.psreg<0, 4>, !waveasm.imm<0>, !waveasm.imm<0> -> !waveasm.vreg<4, 4> | ||
|
|
||
| %next_i = waveasm.s_add_u32 %i, %c1 : !waveasm.sreg, !waveasm.imm<1> -> !waveasm.sreg | ||
| %cond = waveasm.s_cmp_lt_u32 %next_i, %c10 : !waveasm.sreg, !waveasm.imm<10> -> !waveasm.sreg | ||
| waveasm.condition %cond : !waveasm.sreg | ||
| iter_args(%next_i, %acc_new, %data_next) | ||
| : !waveasm.sreg, !waveasm.vreg<4, 4>, !waveasm.vreg<4, 4> | ||
| } | ||
|
|
||
| waveasm.s_endpgm | ||
| } | ||
|
|
||
| //===----------------------------------------------------------------------===// | ||
| // Test 3: UNSAFE ds_read_b128 — same back-edge copy pattern via LDS. | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| // CHECK-LABEL: lds_emit_unsafe: | ||
| waveasm.program @lds_emit_unsafe | ||
| target = #waveasm.target<#waveasm.gfx942, 5> | ||
| abi = #waveasm.abi<> { | ||
|
|
||
| %c0 = waveasm.constant 0 : !waveasm.imm<0> | ||
| %c1 = waveasm.constant 1 : !waveasm.imm<1> | ||
| %c10 = waveasm.constant 10 : !waveasm.imm<10> | ||
| %b = waveasm.precolored.vreg 4, 4 : !waveasm.pvreg<4, 4> | ||
| %lds_addr = waveasm.precolored.vreg 0 : !waveasm.pvreg<0> | ||
|
|
||
| %init_i = waveasm.s_mov_b32 %c0 : !waveasm.imm<0> -> !waveasm.sreg | ||
| %init_acc = waveasm.v_mov_b32 %c0 : !waveasm.imm<0> -> !waveasm.vreg<4, 4> | ||
| %init_data = waveasm.ds_read_b128 %lds_addr : !waveasm.pvreg<0> -> !waveasm.vreg<4, 4> | ||
|
|
||
| // CHECK: L_loop_0: | ||
| // CHECK: ds_read_b128 | ||
| // CHECK: v_mfma_f32_16x16x16_f16 | ||
| // Back-edge copies: 4 individual v_mov_b32 for the wide register. | ||
| // CHECK: v_mov_b32 | ||
| // CHECK: v_mov_b32 | ||
| // CHECK: v_mov_b32 | ||
| // CHECK: v_mov_b32 | ||
| // CHECK: s_cbranch_scc1 L_loop_0 | ||
| %ri, %racc, %rdata = waveasm.loop( | ||
| %i = %init_i, %acc = %init_acc, %data = %init_data) | ||
| : (!waveasm.sreg, !waveasm.vreg<4, 4>, !waveasm.vreg<4, 4>) | ||
| -> (!waveasm.sreg, !waveasm.vreg<4, 4>, !waveasm.vreg<4, 4>) { | ||
|
|
||
| %data_next = waveasm.ds_read_b128 %lds_addr : !waveasm.pvreg<0> -> !waveasm.vreg<4, 4> | ||
| %acc_new = waveasm.v_mfma_f32_16x16x16_f16 %data, %b, %acc | ||
| : !waveasm.vreg<4, 4>, !waveasm.pvreg<4, 4>, !waveasm.vreg<4, 4> -> !waveasm.vreg<4, 4> | ||
|
|
||
| %next_i = waveasm.s_add_u32 %i, %c1 : !waveasm.sreg, !waveasm.imm<1> -> !waveasm.sreg | ||
| %cond = waveasm.s_cmp_lt_u32 %next_i, %c10 : !waveasm.sreg, !waveasm.imm<10> -> !waveasm.sreg | ||
| waveasm.condition %cond : !waveasm.sreg | ||
| iter_args(%next_i, %acc_new, %data_next) | ||
| : !waveasm.sreg, !waveasm.vreg<4, 4>, !waveasm.vreg<4, 4> | ||
| } | ||
|
|
||
| waveasm.s_endpgm | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather than have this as a heuristic in the pass, could we add this as a verification hook on ConditionOp or LoopOp that flags when an async memory result is passed as an iter_arg with an overlapping block arg use. This would catch the hazard at IR validation time rather than relying on the liveness pass to silently handle it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure what IR validation will give us besides random compilation failures for user. Also, checking non-local properties (use-def chains) in verifier is a bad practice.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The idea with the verifier is that we always validate this as an invariant in the IR, rather than only evaluate it in this pass. Okay then maybe we should model it as a normal form but we can do that in a separate PR.