Skip to content

use threadgroup pointers instead of references in metal#9380

Open
39ali wants to merge 2 commits intogfx-rs:trunkfrom
39ali:metal-threadgroup
Open

use threadgroup pointers instead of references in metal#9380
39ali wants to merge 2 commits intogfx-rs:trunkfrom
39ali:metal-threadgroup

Conversation

@39ali
Copy link
Copy Markdown
Contributor

@39ali 39ali commented Apr 6, 2026

Connections
#4500

Description
threadgroups are broken in metal when referenced instead of using pointers(i suspect because of compiler reordering)
this seems to fix the issue

Testing
ran tests, and local test that uses threadgroups

Squash or Rebase?
either

Checklist

  • Run cargo fmt.
  • Run taplo format.
  • Run cargo clippy --tests. If applicable, add:
    • --target wasm32-unknown-unknown
  • Run cargo xtask test to run tests.
  • If this contains user-facing changes, add a CHANGELOG.md entry.

@39ali 39ali changed the title use threadgroup pointers instead of references use threadgroup pointers instead of references in metal Apr 6, 2026
@ErichDonGubler
Copy link
Copy Markdown
Member

@39ali: Before we have somebody do review, this needs to be rebased, and CI errors need to be fixed.

@inner-daemons inner-daemons self-requested a review April 8, 2026 15:28
@inner-daemons inner-daemons self-assigned this Apr 8, 2026
Copy link
Copy Markdown
Collaborator

@inner-daemons inner-daemons left a comment

Choose a reason for hiding this comment

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

Some initial thoughts, nothing major but will need to be addressed

Comment thread naga/src/back/msl/writer.rs Outdated
};
(coherent, space, access, "&")

let (suffix, reference) = if let crate::AddressSpace::WorkGroup = var.space {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

If let is probably less clear than simply var.space == .... Also, do you know whether this should affect AddressSpace::TaskPayload, or any ray payload space? If not that warrants a comment explaining.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

just saw the mesh shader changes, i'll look into it

Comment thread naga/src/back/msl/writer.rs Outdated
// but for it to work with the rest of the code we reference it in a temp var in the function body:
// threadgroup type& temp = *temp_ptr;
for (handle, var) in module.global_variables.iter() {
if var.space == crate::AddressSpace::WorkGroup && !fun_info[handle].is_empty() {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Again, TaskPayload?

Comment thread naga/src/back/msl/writer.rs Outdated
Comment on lines +7606 to +7608
// for threadgroup, we use pointer and not a reference to disable compiler reordering
// but for it to work with the rest of the code we reference it in a temp var in the function body:
// threadgroup type& temp = *temp_ptr;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This should probably link to an issue. "Compiler reordering" is not perfectly clear on what the problem actually is. And its not obvious why we can redeclare as a refernece later without issue.

Comment thread naga/src/back/msl/writer.rs Outdated
Ok(write!(
out,
"{}{}{}{}{}{}{} {}",
"{}{}{}{}{}{}{} {}{}",
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This does not adequately handle cases where there might be another variable with this name. You have to use the writer's namer.

Comment thread naga/src/back/msl/writer.rs Outdated

writeln!(
self.out,
" threadgroup {}& {} = *{}_ptr;",
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This _ptr will have to change according to the comment above.

@39ali
Copy link
Copy Markdown
Contributor Author

39ali commented Apr 12, 2026

@inner-daemons i didn't like that local reference hack so i changed it to actually use the pointer

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.

4 participants