Skip to content

Assign optimal affinities to allocations during ScheduleAllocation. - #20965

Merged
benvanik merged 1 commit into
mainfrom
users/benvanik/optimal-alloca
Jun 3, 2025
Merged

benvanik merged 1 commit into
mainfrom
users/benvanik/optimal-alloca

Conversation

@benvanik

@benvanik benvanik commented May 31, 2025 •

Copy link
Copy Markdown
Collaborator

Updates affinity analysis to better track operand/result usage independently from execution scope particularly with pinned ops. The improved tracking enables ScheduleAllocationPass to now join all potential affinities a result will be used on to produce the affinity for each allocation whether they are transients or constants/variables. By performing analysis at the point immediately prior to going from stream.async.* (with all optimizations/transfer elision/etc applied) to stream.cmd.* (where we can freely alias) we can produce the tightest usage affinities and segment each resource independently prior to packing.

Deallocations that can either be inserted directly during ScheduleAllocationPass or analyzed later during stream->hal lowering can reuse the joined affinities while the fallback case sets the prefer-origin flag indicating that the runtime should decide. The behavior at runtime will be the same for each but by preserving that information in the compiler for longer we can in the future set timepoint affinities more tightly (today we don't track them, but really should).

Example IR showing what this does today and what it can do once we elide transfers:
https://gist.github.com/benvanik/ecc9b37fb2b670ce1ed2fb0d7c694287

Fixes #20855.

@benvanik
benvanik force-pushed the users/benvanik/optimal-alloca branch from be7f79a to 8d79459 Compare June 2, 2025 20:37
@benvanik benvanik changed the title [WIP] Assign optimal affinities to allocations during ScheduleAllocation. Assign optimal affinities to allocations during ScheduleAllocation. Jun 2, 2025
@benvanik
benvanik requested review from ScottTodd and hanhanW June 2, 2025 21:42
@benvanik
benvanik marked this pull request as ready for review June 2, 2025 21:42
util.global private @device_b : !hal.device

// CHECK-LABEL: @file_constant_optimal
// CHECK-SAME: (%[[BUFFER:.+]]: !util.buffer)

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 sharing a tip: I usually match %[[BUFFER:[a-zA-Z0-9]+]] to get rid of matching the argument type. It is very useful when there are multiple function arguments.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I've dreamed of a shorthand for that - like [[BUFFER:\id]] or something. It's such a common thing in these tests that it always feels silly we have to spell out the regex!

@benvanik
benvanik merged commit 63d171d into main Jun 3, 2025
@benvanik
benvanik deleted the users/benvanik/optimal-alloca branch June 3, 2025 15:52
AWoloszyn pushed a commit that referenced this pull request Dec 1, 2025
…20965)

Updates affinity analysis to better track operand/result usage
independently from execution scope particularly with pinned ops. The
improved tracking enables ScheduleAllocationPass to now join all
potential affinities a result will be used on to produce the affinity
for each allocation whether they are transients or constants/variables.
By performing analysis at the point immediately prior to going from
`stream.async.*` (with all optimizations/transfer elision/etc applied)
to `stream.cmd.*` (where we can freely alias) we can produce the
tightest usage affinities and segment each resource independently prior
to packing.

Deallocations that can either be inserted directly during
ScheduleAllocationPass or analyzed later during stream->hal lowering can
reuse the joined affinities while the fallback case sets the
prefer-origin flag indicating that the runtime should decide. The
behavior at runtime will be the same for each but by preserving that
information in the compiler for longer we can in the future set
timepoint affinities more tightly (today we don't track them, but really
should).

Example IR showing what this does today and what it can do once we elide
transfers:
https://gist.github.com/benvanik/ecc9b37fb2b670ce1ed2fb0d7c694287

Fixes #20855.
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.

[Stream] Assign resource affinity based on usage (not just assigned execution affinity) with multiple potential affinities.

2 participants