Skip to content

Fix implicit thread address space qualifier becoming explicit in metal 4.1 - #3963

Open
louen wants to merge 9 commits into
ml-explore:mainfrom
louen:val/fix-metal41
Open

Fix implicit thread address space qualifier becoming explicit in metal 4.1 #3963
louen wants to merge 9 commits into
ml-explore:mainfrom
louen:val/fix-metal41

Conversation

@louen

@louen louen commented Aug 1, 2026

Copy link
Copy Markdown
Contributor

Proposed changes

Metal 4.1 introduced the concept of "generic address space" and defaults pointers and references to this address space (where it used to implicitly be thread before)

In particular this means that this which previously was thread is now generic which causes code with functions that return a thread reference type to fail to compile, for example:

struct Foo {
  float v[4];
  thread float& at(short i) {
    return v[i];
  }
};

starting from Xcode 27.0 Beta 4 this will fail with

error: reference to type 'thread float' could not bind to an lvalue of type '__metal_generic float'

on previous versions of the toolchain, this would emit a warning (if enabled)

warning: missing 'thread' address space qualifier [-Wmetal-addr-spaces]

To fix the code under the new compiler rules the thread qualifier has to be added to such functions returning a reference to, but also to all member functions of classes that call them, propagating virally similarly to const member functions in C++.

This propagation stops at the creation of the object since as an automatic variable

Foo f;
foo.at(42);

Foo is still implicitly delclared as thread.

The fix has been applied automatically in most instances by adding -Xclang -fixit -Wmetal-addr-spaces to the metal compiler flags in CMake (the warning flag remains in this PR, as discussed below).
Extra fixes were necessary especially in the instancing macros which clang could not fix automatically.

To verify the fixes, I have taken the following steps

  • The code compiles without warning, and the tests succeed under Xcode 27.0 Beta 3 (Apple metal version 32023.918 (metalfe-32023.918.1)) and Beta 4 toolchains (Apple metal version 32023.921 (metalfe-32023.921)).
  • The behavior of the metal kernels is not altered : as the thread keyword only makes explicit what was previously implicit behavior. I checked produced air files are binary-identical under both toolchains.

As said above in addition to the addition of the keyword I have left the -Wmetal-addr-spaces so that older toolchains still warn about the construct.

Note that mlx-swift will have to upgrade its submodule to compile under the new Xcode toolchain

Checklist

Put an x in the boxes that apply.

  • [ x] I have read the CONTRIBUTING document
  • [ x] I have run pre-commit run --all-files to format my code / installed pre-commit prior to committing changes
  • [ x] I have updated the necessary documentation (if needed)

@louen louen changed the title Metal 4.1 fixes Fix implicit thread memory addressing becoming explicit in metal 4.1 Aug 1, 2026
@louen louen changed the title Fix implicit thread memory addressing becoming explicit in metal 4.1 Fix implicit thread address space qualifier becoming explicit in metal 4.1 Aug 1, 2026
@louen
louen marked this pull request as ready for review August 2, 2026 00:48
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.

1 participant