Skip to content

Conversation

@Flakebi
Copy link
Contributor

@Flakebi Flakebi commented Dec 4, 2025

GPU targets have convergent operations that require careful handling
when running optimizations. E.g. they must not be duplicated.

An example convergent operation is a barrier/syncthreads.

We do not want to deal with convergent operations in mir optimizations,
so set the optimization level to 0 and skip all optimizations.

On targets with convergent operations, we need to add the convergent
attribute to all functions that run convergent operations. Following
clang, we can conservatively apply the attribute to all functions when
compiling for such a target and rely on LLVM optimizing away the
attribute in cases where it is not necessary.

The amdgpu and nvptx targets are marked as having convergent operations.

Fixes #137086, see this issue for details.
Tracking issue: #135024

cc @RDambrosio016 @kjetilkjeka for nvptx
cc @ZuseZ4

@rustbot
Copy link
Collaborator

rustbot commented Dec 4, 2025

These commits modify compiler targets.
(See the Target Tier Policy.)

@rustbot rustbot added A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Dec 4, 2025
@rustbot
Copy link
Collaborator

rustbot commented Dec 4, 2025

r? @nnethercote

rustbot has assigned @nnethercote.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

GPU targets have convergent operations that require careful handling
when running optimizations. E.g. they must not be duplicated.

An example convergent operation is a barrier/syncthreads.

We do not want to deal with convergent operations in mir optimizations,
so set the optimization level to 0 and skip all optimizations.

This affects the amdgpu and nvptx targets.
On targets with convergent operations, we need to add the convergent
attribute to all functions that run convergent operations. Following
clang, we can conservatively apply the attribute to all functions when
compiling for such a target and rely on LLVM optimizing away the
attribute in cases where it is not necessary.

This affects the amdgpu and nvptx targets.
@Flakebi Flakebi force-pushed the fix-convergent-mir-opts branch from 25d4193 to f17636b Compare December 4, 2025 21:40
@ZuseZ4 ZuseZ4 mentioned this pull request Dec 5, 2025
5 tasks
@nnethercote
Copy link
Contributor

cc @LegNeato @eddyb @FractalFir

@nnethercote nnethercote added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Dec 15, 2025
@Flakebi
Copy link
Contributor Author

Flakebi commented Dec 18, 2025

Hi @nnethercote, I see you moved this to waiting-on-author.
I wasn’t aware of any outstanding issues here. What can I do to move this forward?

@FractalFir
Copy link
Contributor

I have been meaning to raise some reservations about this.

I feel like this is a solution that is far too aggressive. A lot of MIR opts have nothing to do with convergence - disabling all of them outright feels excessive.

The proper fix would be, in my opinon:

  1. Add a new function to MIR passes(they all implement a trait, I believe), called is_covergence_safe(or something like that), with it defaulting to false. This is so that if a new pass is added, people don't accidentally mess something up.
  2. Not run passes which are not covergence-safe if a target has a notion of convergence
  3. Implement this function for all the passes which are safe.

And that would pretty much solve the problem while minimzing the impact. The downside is larger code size, and more thinking about covergence for the implementors of those passes.

Granted, this is just my opinion.

Copy link
Contributor

@nnethercote nnethercote left a comment

Choose a reason for hiding this comment

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

Apologies, I reviewed this days ago but forgot to hit "submit review" :(

View changes since this review

};

if tcx.sess.target.is_like_gpu {
// Conservatively apply convergent to all functions
Copy link
Contributor

Choose a reason for hiding this comment

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

The commit message explains that LLVM can subsequently optimize these away, which is useful info to replicate here.

let mut attrs = SmallVec::<[_; 4]>::new();

if cx.sess().target.is_like_gpu {
// Conservatively apply convergent to all functions
Copy link
Contributor

Choose a reason for hiding this comment

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

ditto

if self.target.is_like_gpu {
// Special care needs to be taken for convergent operations (i.e. not duplicating them).
// We do not want to handle these, so do not run any optimizations.
0
Copy link
Contributor

Choose a reason for hiding this comment

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

This feels like a very blunt instrument! Seems like things went from "disable JumpThreading where we know problems can occur" to the much broader "disable everything because problems might occur" without much discussion?

Copy link
Contributor

Choose a reason for hiding this comment

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

@FractalFir had a good suggestion: add a new method is_convergent_safe to MirPass, so we can have a more fine-grained control over which passes run. Whether it defaults to returning true or false is an open question; false would be more conservative (e.g. safer when new passes are added) but more verbose because it's true for many passes.

Copy link
Contributor

Choose a reason for hiding this comment

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

@Flakebi: what do you think about this idea?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MIR passes do not take into account if an operation is convergent

7 participants