Skip to content
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

Make realization order invariant to unique_name suffixes #8124

Merged
merged 4 commits into from
Mar 5, 2024

Conversation

abadams
Copy link
Member

@abadams abadams commented Feb 27, 2024

Currently the realization order is alphabetical in the case of ties, which may depend on suffixes introduced by unique_name, which will not have a consistent alphabetical order if you define the same pipeline multiple times, e.g. in a multi-target compilation scenario (because numeric order isn't alphabetical order if you don't have leading zeros).

This is a problem for machine-generated schedules, because they depend on topological order of the pipeline. So in a multi-target compilation, a machine-generated schedule that was checked in may be valid for the first few targets, but as soon as your unique_name counters start exceed 10, the ordering may no longer be correct.

This PR makes the realization order invariant to any unique_name suffixes. It works by stripping any potential suffixes, and then sorting primarily by any prefix that remains (for maximum consistency with existing machine-generated schedules), and secondarily by the order in which Funcs were given definitions (which is now tracked).

Copy link
Contributor

@steven-johnson steven-johnson left a comment

Choose a reason for hiding this comment

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

LGTM, but let me run an overnight test.

@abadams abadams added the dev_meeting Topic to be discussed at the next dev meeting label Feb 27, 2024
@steven-johnson
Copy link
Contributor

Overnight test is good, ready to land (failure is irrelevant and will be fixed when llvm/llvm-project#83151 lands)

@abadams
Copy link
Member Author

abadams commented Feb 27, 2024

There's one issue I'd like to raise before I hit merge:

The goal for this PR is for the topological order of Funcs to be invariant to unique_name nonsense so that machine-generated pipelines are more likely to remain valid, and so that stmts don't arbitrarily change the order in which things get realized based across a multi-target build.

This PR does that by sorting by name and then definition time. Name is so that we're as similar as possible to existing behavior, rather than being a good idea a priori. Let's assume for now that someone doesn't name their Funcs, so the entire responsibility falls on the secondary key.

Definition time is good because it's resistant to changes to the RHS of Funcs. But it's not resistant to refactorings with no actual changes to any definitions that move around when different subpipelines are created, and it's not resistant to helper Funcs that are defined once and then reused in multiple pipelines (e.g. in a multi-target build).

An alternative for the second sorting key would be the order in which Funcs are encountered in an IRVisitor traversal of the entire Pipeline starting from the output. This would be resistant to the problems above, but not resistant to things like commuting an addition in the RHS of a Func. I think I might prefer this, because multi-target with reused helper Funcs would be fine, and refactorings with no functional changes intended would be fine. If you tweak the RHS of a Func you may want to reschedule anyway.

Thoughts?

@abadams
Copy link
Member Author

abadams commented Mar 4, 2024

I changed the secondary sorting key to the order in which Funcs are visited during a traversal, rather than the order in which Funcs are given definitions.

@abadams abadams merged commit d33ffa2 into main Mar 5, 2024
19 checks passed
@abadams abadams added the release_notes For changes that may warrant a note in README for official releases. label Mar 13, 2024
@abadams
Copy link
Member Author

abadams commented Mar 13, 2024

Worth mentioning in the release notes, because as @vksnk found, it may change the realization order in existing pipelines, which can affect peak memory usage, possibly causing stack overflows in pipelines that were close to the limit before.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dev_meeting Topic to be discussed at the next dev meeting release_notes For changes that may warrant a note in README for official releases.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants