Skip to content

[Codegen][GPU]Fixing barrier placement for 3+ stages pipelining - #22725

Merged
jerryyin merged 1 commit into
mainfrom
users/zyin/refactor-pipeliner-using-upstream-v4-barrier
Nov 24, 2025
Merged

jerryyin merged 1 commit into
mainfrom
users/zyin/refactor-pipeliner-using-upstream-v4-barrier

Conversation

@jerryyin

Copy link
Copy Markdown
Member

In this PR, I created a new function insertBarriersInRange() that enforces shared-memory ordering by tracking two flags (needBarrierBeforeRead, needBarrierBeforeWrite) as it scans a block. This replaces the old barrier insertion routine as more shared read/write can be pushed to prologue and epilogue with 3+ stages of pipelining.

Detailed description (copied from comments):

Inserts synchronization barriers before shared memory accesses in the given range using a running SharedBarrierState. Conceptually, we track whether the next shared read (or write) must be preceded by a barrier, and only emit one when that flag is set. For example,

  1. if the iteration sequence observes a shared read (R) followed by another read, nothing is inserted; the state only toggles needBarrierBeforeWrite, so the next shared write (W) will emit a barrier before it.
  2. if the iteration sequence observes a shared write first toggles needBarrierBeforeRead, so the following read receives the barrier.
    This keeps the minimum number of synchronizations while still enforcing the R↔W ordering required by the pipelined schedule.

Notes that all existing unit tests already have good coverage of barrier placement so this PR doesn't need one. The new functionalities will later be covered when 3 stage pipeline is added.

Signed-off-by: jerryyin <zhuoryin@amd.com>

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

Makes sense, LGTM!

@jerryyin
jerryyin merged commit 9269e03 into main Nov 24, 2025
49 of 51 checks passed
@jerryyin
jerryyin deleted the users/zyin/refactor-pipeliner-using-upstream-v4-barrier branch November 24, 2025 15:16
bangtianliu pushed a commit to bangtianliu/iree that referenced this pull request Nov 25, 2025
…-org#22725)

In this PR, I created a new function insertBarriersInRange() that
enforces shared-memory ordering by tracking two flags
(needBarrierBeforeRead, needBarrierBeforeWrite) as it scans a block.
This replaces the old barrier insertion routine as more shared
read/write can be pushed to prologue and epilogue with 3+ stages of
pipelining.

Detailed description (copied from comments): 

Inserts synchronization barriers before shared memory accesses in the
given range using a running `SharedBarrierState`. Conceptually, we track
whether the next shared read (or write) must be preceded by a barrier,
and only emit one when that flag is set. For example,
1) if the iteration sequence observes a shared read (R) followed by
another read, nothing is inserted; the state only toggles
`needBarrierBeforeWrite`, so the next shared write (W) will emit a
barrier before it.
2) if the iteration sequence observes a shared write first toggles
`needBarrierBeforeRead`, so the following read receives the barrier.
This keeps the minimum number of synchronizations while still enforcing
the R↔W ordering required by the pipelined schedule.

Notes that all existing unit tests already have good coverage of barrier
placement so this PR doesn't need one. The new functionalities will
later be covered when 3 stage pipeline is added.

Signed-off-by: jerryyin <zhuoryin@amd.com>
pstarkcdpr pushed a commit to pstarkcdpr/iree that referenced this pull request Nov 28, 2025
…-org#22725)

In this PR, I created a new function insertBarriersInRange() that
enforces shared-memory ordering by tracking two flags
(needBarrierBeforeRead, needBarrierBeforeWrite) as it scans a block.
This replaces the old barrier insertion routine as more shared
read/write can be pushed to prologue and epilogue with 3+ stages of
pipelining.

Detailed description (copied from comments): 

Inserts synchronization barriers before shared memory accesses in the
given range using a running `SharedBarrierState`. Conceptually, we track
whether the next shared read (or write) must be preceded by a barrier,
and only emit one when that flag is set. For example,
1) if the iteration sequence observes a shared read (R) followed by
another read, nothing is inserted; the state only toggles
`needBarrierBeforeWrite`, so the next shared write (W) will emit a
barrier before it.
2) if the iteration sequence observes a shared write first toggles
`needBarrierBeforeRead`, so the following read receives the barrier.
This keeps the minimum number of synchronizations while still enforcing
the R↔W ordering required by the pipelined schedule.

Notes that all existing unit tests already have good coverage of barrier
placement so this PR doesn't need one. The new functionalities will
later be covered when 3 stage pipeline is added.

Signed-off-by: jerryyin <zhuoryin@amd.com>
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.

2 participants